Skip to content

Commit

Permalink
feat: prefer selected fuel
Browse files Browse the repository at this point in the history
  • Loading branch information
Dorus committed May 5, 2024
1 parent bca42c8 commit d80260e
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions Yafc/Workspace/ProductionTable/ProductionTableView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -595,12 +595,13 @@ public override void CreateModelDropdown(ImGui gui, Type type, Project project)
}

private static readonly IComparer<Goods> DefaultVariantOrdering = new DataUtils.FactorioObjectComparer<Goods>((x, y) => (y.ApproximateFlow() / MathF.Abs(y.Cost())).CompareTo(x.ApproximateFlow() / MathF.Abs(x.Cost())));
private RecipeRow AddRecipe(ProductionTable table, Recipe recipe) {
private RecipeRow AddRecipe(ProductionTable table, Recipe recipe, Goods SelectedFuel = null) {
RecipeRow recipeRow = new RecipeRow(table, recipe);
table.RecordUndo().recipes.Add(recipeRow);
recipeRow.entity = recipe.crafters.AutoSelect(DataUtils.FavoriteCrafter);
EntityCrafter SelectedFuelCrafter = SelectedFuel?.fuelFor.OfType<EntityCrafter>().Where(e => e.recipes.OfType<Recipe>().Any(e => e == recipe)).AutoSelect(DataUtils.FavoriteCrafter);
recipeRow.entity = SelectedFuelCrafter ?? recipe.crafters.AutoSelect(DataUtils.FavoriteCrafter);
if (recipeRow.entity != null) {
recipeRow.fuel = recipeRow.entity.energy.fuels.AutoSelect(DataUtils.FavoriteFuel);
recipeRow.fuel = recipeRow.entity.energy.fuels.FirstOrDefault(e => e == SelectedFuel) ?? recipeRow.entity.energy.fuels.AutoSelect(DataUtils.FavoriteFuel);
}

foreach (var ingr in recipeRow.recipe.ingredients) {
Expand Down Expand Up @@ -657,6 +658,7 @@ bool recipeExists(Recipe rec) {
return allRecipes.Contains(rec);
}

Goods selectedFuel = null;
async void addRecipe(Recipe rec) {
if (variants == null) {
CreateLink(context, goods);
Expand All @@ -674,7 +676,7 @@ async void addRecipe(Recipe rec) {
}
}
if (!allRecipes.Contains(rec) || (await MessageBox.Show("Recipe already exists", $"Add a second copy of {rec.locName}?", "Add a copy", "Cancel")).choice) {
_ = AddRecipe(context, rec);
_ = AddRecipe(context, rec, selectedFuel);
}
}

Expand Down Expand Up @@ -774,7 +776,7 @@ void DropDownContent(ImGui gui) {
}

if (type != ProductDropdownType.Fuel && goods != null && type != ProductDropdownType.Ingredient && fuelUseList.Length > 0) {
gui.BuildInlineObjectListAndButton(fuelUseList, DataUtils.AlreadySortedRecipe, addRecipe, "Add fuel usage", type == ProductDropdownType.Product ? 6 : 3, true, recipeExists);
gui.BuildInlineObjectListAndButton(fuelUseList, DataUtils.AlreadySortedRecipe, (x) => { selectedFuel = goods; addRecipe(x); }, "Add fuel usage", type == ProductDropdownType.Product ? 6 : 3, true, recipeExists);
}

if (type == ProductDropdownType.Product && goods != null && allProduction.Length > 0) {
Expand Down

0 comments on commit d80260e

Please sign in to comment.