Skip to content

Commit

Permalink
Hovering over desired product shows duplicate recipe (#404)
Browse files Browse the repository at this point in the history
This fixes #403. It also fixes a similar issue in the net production
calculations, where recipes with repeated outputs (such as Vrauk MK02)
could be treated as consumption recipes instead production recipes.
  • Loading branch information
shpaass authored Feb 2, 2025
2 parents b6cbe02 + 5e53570 commit f79dedc
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Yafc.Model/Analysis/DependencyNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ public override void Draw(ImGui gui, Action<ImGui, IReadOnlyList<FactorioId>, Fl
/// </summary>
/// <param name="dependencies">The <see cref="DependencyList"/> whose behavior should be matched by this <see cref="ListNode"/>.</param>
private sealed class ListNode(IEnumerable<FactorioObject> elements, Flags flags) : DependencyNode {
private readonly ReadOnlyCollection<FactorioId> elements = elements.Select(e => e.id).ToList().AsReadOnly();
private readonly ReadOnlyCollection<FactorioId> elements = elements.Select(e => e.id).Distinct().ToList().AsReadOnly();

internal override IEnumerable<FactorioId> Flatten() => elements;

Expand Down
14 changes: 7 additions & 7 deletions Yafc.Parser/Data/FactorioDataDeserializer_Context.cs
Original file line number Diff line number Diff line change
Expand Up @@ -362,23 +362,23 @@ private void CalculateMaps(bool netProduction) {
case Recipe recipe:
allRecipes.Add(recipe);

foreach (var product in recipe.products) {
foreach (var product in recipe.products.GroupBy(p => p.goods)) {
// If the ingredient has variants and is an output, we aren't doing catalyst things: water@15-90 to water@90 does produce water@90,
// even if it consumes 10 water@15-90 to produce 9 water@90.
Ingredient? ingredient = recipe.ingredients.FirstOrDefault(i => i.goods == product.goods && i.variants is null);
Ingredient? ingredient = recipe.ingredients.FirstOrDefault(i => i.goods == product.Key && i.variants is null);
float inputAmount = netProduction ? (ingredient?.amount ?? 0) : 0;
float outputAmount = product.amount;
float outputAmount = product.Sum(p => p.amount);

if (outputAmount > inputAmount) {
itemProduction.Add(product.goods, recipe);
itemProduction.Add(product.Key, recipe);
}
}

foreach (var ingredient in recipe.ingredients) {
// The reverse also applies. 9 water@15-90 to produce 10 water@15 consumes water@90, even though it's a net water producer.
float inputAmount = ingredient.amount;
Product? product = ingredient.variants is null ? recipe.products.FirstOrDefault(p => p.goods == ingredient.goods) : null;
float outputAmount = netProduction ? (product?.amount ?? 0) : 0;
IEnumerable<Product> products = recipe.products.Where(p => p.goods == ingredient.goods);
float outputAmount = netProduction && ingredient.variants is null ? products.Sum(p => p.amount) : 0;

if (ingredient.variants == null && inputAmount > outputAmount) {
itemUsages.Add(ingredient.goods, recipe);
Expand Down Expand Up @@ -478,7 +478,7 @@ private void CalculateMaps(bool netProduction) {
break;
case Goods goods:
goods.usages = itemUsages.GetArray(goods);
goods.production = itemProduction.GetArray(goods);
goods.production = itemProduction.GetArray(goods).Distinct().ToArray();
goods.miscSources = miscSources.GetArray(goods);

if (o is Item item) {
Expand Down

0 comments on commit f79dedc

Please sign in to comment.