Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle research trigger 'craft-item' #327

Merged
merged 1 commit into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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