Skip to content

Commit

Permalink
Handle research trigger 'craft-item' (#327)
Browse files Browse the repository at this point in the history
While checking for the Space Age changes of #320, I noticed 'new' errors
when loading a project. Which was triggered by the (lack of) support of
the new [research
triggers](https://lua-api.factorio.com/latest/concepts/ResearchTrigger.html).

Adding the `craft-item` type was fairly easy (I was fiddling a bit and
it worked):
_\<removed outdated screenshot, see comments for newer one\>_

So I cleaned the code a little and made this PR.

Note that supporting `mine-entity` was also easy, but for some reason it
makes most of the items inaccessible... So I did not include it in this
PR. We need to take a better look.
The other types, I did not try as their implementation was not directly
apparent to me (and `craft-fluid` is not in use, so I could not
try/test)
  • Loading branch information
shpaass authored Oct 29, 2024
2 parents 149d2e3 + 7013ed3 commit de43a51
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 5 deletions.
2 changes: 2 additions & 0 deletions Yafc.Model/Data/DataClasses.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ public enum RecipeFlags {
UsesMiningProductivity = 1 << 0,
UsesFluidTemperature = 1 << 2,
ScaleProductionWithPower = 1 << 3,
/// <summary>Set when the technology has a research trigger to craft an item</summary>
HasResearchTriggerCraft = 1 << 4,
}

public abstract class RecipeOrTechnology : FactorioObject {
Expand Down
31 changes: 27 additions & 4 deletions Yafc.Parser/Data/FactorioDataDeserializer_RecipeAndTechnology.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,13 @@ private void LoadTechnologyData(Technology technology, LuaTable table, ErrorColl
technology.ingredients = LoadResearchIngredientList(unit, technology.typeDotName, errorCollector);
recipeCategories.Add(SpecialNames.Labs, technology);
}
else if (table.Get("research_trigger", out LuaTable? researchTrigger)) {
technology.ingredients = [];
else if (table.Get("research_trigger", out LuaTable? researchTriggerTable)) {
LoadResearchTrigger(researchTriggerTable, ref technology, errorCollector);
technology.ingredients ??= [];
recipeCategories.Add(SpecialNames.TechnologyTrigger, technology);
errorCollector.Error($"Research trigger not yet supported for {technology.name}", ErrorSeverity.MinorDataLoss);
}
else {
errorCollector.Error($"Could not get science packs for {technology.name}.", ErrorSeverity.AnalysisWarning);
errorCollector.Error($"Could not get requirement(s) to unlock {technology.name}.", ErrorSeverity.AnalysisWarning);
}

DeserializeFlags(table, technology);
Expand Down Expand Up @@ -229,6 +229,29 @@ private Ingredient[] LoadResearchIngredientList(LuaTable table, string typeDotNa
}).Where(x => x is not null).ToArray() ?? [];
}

private void LoadResearchTrigger(LuaTable researchTriggerTable, ref Technology technology, ErrorCollector errorCollector) {
if (!researchTriggerTable.Get("type", out string? type)) {
errorCollector.Error($"Research trigger of {technology.typeDotName} does not have a type field", ErrorSeverity.MinorDataLoss);
return;
}

switch (type) {
case "craft-item":
if (!researchTriggerTable.Get("item", out string? craftItemName)) {
errorCollector.Error($"Research trigger craft-item of {technology.typeDotName} does not have a item field", ErrorSeverity.MinorDataLoss);
break;
}
float craftCount = researchTriggerTable.Get("count", 1);
technology.ingredients = [new Ingredient(GetObject<Item>(craftItemName), craftCount)];
technology.flags = RecipeFlags.HasResearchTriggerCraft;

break;
default:
errorCollector.Error($"Research trigger of {technology.typeDotName} has an unsupported type {type}", ErrorSeverity.MinorDataLoss);
break;
}
}

private void LoadRecipeData(Recipe recipe, LuaTable table, ErrorCollector errorCollector) {
recipe.ingredients = LoadIngredientList(table, recipe.typeDotName, errorCollector);
recipe.products = LoadProductList(table, recipe.typeDotName, allowSimpleSyntax: false);
Expand Down
19 changes: 18 additions & 1 deletion Yafc/Widgets/ObjectTooltip.cs
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,15 @@ private void BuildRecipe(RecipeOrTechnology recipe, ImGui gui) {
}

private void BuildTechnology(Technology technology, ImGui gui) {
BuildRecipe(technology, gui);
bool isResearchTriggerCraft = (technology.flags & RecipeFlags.HasResearchTriggerCraft) == RecipeFlags.HasResearchTriggerCraft;
if (isResearchTriggerCraft) {
BuildCommon(technology, gui);

}
else {
BuildRecipe(technology, gui);
}

if (technology.hidden && !technology.enabled) {
using (gui.EnterGroup(contentPadding)) {
gui.BuildText("This technology is hidden from the list and cannot be researched.", TextBlockDisplayStyle.WrappedText);
Expand All @@ -514,6 +522,15 @@ private void BuildTechnology(Technology technology, ImGui gui) {
}
}

if (isResearchTriggerCraft) {
BuildSubHeader(gui, "Item crafting required");
using (gui.EnterGroup(contentPadding)) {
using var grid = gui.EnterInlineGrid(3f);
grid.Next();
_ = gui.BuildFactorioObjectWithAmount(technology.ingredients[0].goods, technology.ingredients[0].amount, ButtonDisplayStyle.ProductionTableUnscaled);
}
}

if (technology.unlockRecipes.Length > 0) {
BuildSubHeader(gui, "Unlocks recipes");
using (gui.EnterGroup(contentPadding)) {
Expand Down

0 comments on commit de43a51

Please sign in to comment.