Skip to content

Commit

Permalink
Move AddRecipe to the model, where it belongs.
Browse files Browse the repository at this point in the history
  • Loading branch information
DaleStan authored and shpaass committed Jul 2, 2024
1 parent 2bc96ec commit 7092d50
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 21 deletions.
23 changes: 23 additions & 0 deletions Yafc.Model/Model/ProductionTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,29 @@ public bool Search(SearchQuery query) {
return false;
}

/// <summary>
/// Add a recipe to this table, and configure the recipe's crafter and fuel to reasonable values.
/// </summary>
/// <param name="recipe">The recipe to add.</param>
/// <param name="ingredientVariantComparer">If not <see langword="null"/>, the comparer to use when deciding which fluid variants to use.</param>
/// <param name="selectedFuel">If not <see langword="null"/>, this method will select a crafter that can use this fuel, assuming such an entity exists.
/// For example, if the selected fuel is coal, the recipe will be configured with a burner assembler if any are available.</param>
public void AddRecipe(Recipe recipe, IComparer<Goods>? ingredientVariantComparer = null, Goods? selectedFuel = null) {
RecipeRow recipeRow = new RecipeRow(this, recipe);
this.RecordUndo().recipes.Add(recipeRow);
EntityCrafter? selectedFuelCrafter = selectedFuel?.fuelFor.OfType<EntityCrafter>().Where(e => e.recipes.OfType<Recipe>().Contains(recipe)).AutoSelect(DataUtils.FavoriteCrafter);
recipeRow.entity = selectedFuelCrafter ?? recipe.crafters.AutoSelect(DataUtils.FavoriteCrafter);
if (recipeRow.entity != null) {
recipeRow.fuel = recipeRow.entity.energy.fuels.FirstOrDefault(e => e == selectedFuel) ?? recipeRow.entity.energy.fuels.AutoSelect(DataUtils.FavoriteFuel);
}

foreach (Ingredient ingredient in recipeRow.recipe.ingredients) {
if (ingredient.variants != null) {
_ = recipeRow.variants.Add(ingredient.variants.AutoSelect(ingredientVariantComparer)!); // null-forgiving: variants is never empty, and AutoSelect never returns null from a non-empty collection (of non-null items).
}
}
}

/// <summary>
/// Get all <see cref="RecipeRow"/>s contained in this <see cref="ProductionTable"/>, in a depth-first ordering. (The same as in the UI when all nested tables are expanded.)
/// </summary>
Expand Down
25 changes: 4 additions & 21 deletions Yafc/Workspace/ProductionTable/ProductionTableView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public override void BuildElement(ImGui gui, RecipeRow recipe) {
}

if (recipe.subgroup != null && imgui.BuildButton("Add raw recipe") && imgui.CloseDropdown()) {
SelectMultiObjectPanel.Select(Database.recipes.all, "Select raw recipe", r => view.AddRecipe(recipe.subgroup, r), checkMark: r => recipe.subgroup.recipes.Any(rr => rr.recipe == r));
SelectMultiObjectPanel.Select(Database.recipes.all, "Select raw recipe", r => recipe.subgroup.AddRecipe(r, DefaultVariantOrdering), checkMark: r => recipe.subgroup.recipes.Any(rr => rr.recipe == r));
}

if (recipe.subgroup != null && imgui.BuildButton("Unpack nested table").WithTooltip(imgui, recipe.subgroup.expanded ? "Shortcut: right-click" : "Shortcut: Expand, then right-click") && imgui.CloseDropdown()) {
Expand Down Expand Up @@ -180,7 +180,7 @@ private void RemoveZeroRecipes(ProductionTable productionTable) {

public override void BuildMenu(ImGui gui) {
if (gui.BuildButton("Add recipe") && gui.CloseDropdown()) {
SelectMultiObjectPanel.Select(Database.recipes.all, "Select raw recipe", r => view.AddRecipe(view.model, r), checkMark: r => view.model.recipes.Any(rr => rr.recipe == r));
SelectMultiObjectPanel.Select(Database.recipes.all, "Select raw recipe", r => view.model.AddRecipe(r, DefaultVariantOrdering), checkMark: r => view.model.recipes.Any(rr => rr.recipe == r));
}

gui.BuildText("Export inputs and outputs to blueprint with constant combinators:", wrap: true);
Expand Down Expand Up @@ -224,7 +224,7 @@ public override void BuildMenu(ImGui gui) {
view.CreateLink(view.model, product.goods);
}

var row = view.AddRecipe(view.model, recipe);
view.model.AddRecipe(recipe, DefaultVariantOrdering);
goodsHaveNoProduction:;
}
}
Expand Down Expand Up @@ -642,23 +642,6 @@ public override void CreateModelDropdown(ImGui gui, Type type, Project project)
public static void CreateProductionSheet() => ProjectPageSettingsPanel.Show(null, (name, icon) => MainScreen.Instance.AddProjectPage(name, icon, typeof(ProductionTable), true, true));

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, Goods? selectedFuel = null) {
RecipeRow recipeRow = new RecipeRow(table, recipe);
table.RecordUndo().recipes.Add(recipeRow);
EntityCrafter? selectedFuelCrafter = selectedFuel?.fuelFor.OfType<EntityCrafter>().Where(e => e.recipes?.OfType<Recipe>().Contains(recipe) ?? false).AutoSelect(DataUtils.FavoriteCrafter);
recipeRow.entity = selectedFuelCrafter ?? recipe.crafters.AutoSelect(DataUtils.FavoriteCrafter);
if (recipeRow.entity != null) {
recipeRow.fuel = recipeRow.entity.energy.fuels.FirstOrDefault(e => e == selectedFuel) ?? recipeRow.entity.energy.fuels.AutoSelect(DataUtils.FavoriteFuel);
}

foreach (var ingr in recipeRow.recipe.ingredients) {
if (ingr.variants != null) {
_ = recipeRow.variants.Add(ingr.variants.AutoSelect(DefaultVariantOrdering)!); // null-forgiving: variants is never empty, and AutoSelect never returns null from a non-empty collection (of non-null items).
}
}

return recipeRow;
}

private enum ProductDropdownType {
DesiredProduct,
Expand Down Expand Up @@ -723,7 +706,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, selectedFuel);
context.AddRecipe(rec, selectedFuel: selectedFuel);
}
}

Expand Down

0 comments on commit 7092d50

Please sign in to comment.