From ca9e45d037b3c5688845e4b278394bf1155a041f Mon Sep 17 00:00:00 2001 From: zombifier Date: Fri, 23 Aug 2024 17:27:51 -0400 Subject: [PATCH 1/4] [LookupAnything] Add integration with Extra Machine Config's multiple output items. --- .../IExtraMachineConfigApi.cs | 4 + LookupAnything/DataParser.cs | 97 ++++++++++--------- 2 files changed, 56 insertions(+), 45 deletions(-) diff --git a/Common/Integrations/ExtraMachineConfig/IExtraMachineConfigApi.cs b/Common/Integrations/ExtraMachineConfig/IExtraMachineConfigApi.cs index bed319318..8200410da 100644 --- a/Common/Integrations/ExtraMachineConfig/IExtraMachineConfigApi.cs +++ b/Common/Integrations/ExtraMachineConfig/IExtraMachineConfigApi.cs @@ -13,5 +13,9 @@ public interface IExtraMachineConfigApi /// Retrieves the extra tag-defined fuels consumed by this recipe. /// The output rule to check. IList<(string, int)> GetExtraTagsRequirements(MachineItemOutput outputData); + + /// Retrieves the extra output items produced by this recipe. + /// The output rule to check. + IList GetExtraOutputs(MachineItemOutput outputData); } } diff --git a/LookupAnything/DataParser.cs b/LookupAnything/DataParser.cs index d9d37a41e..238c850b8 100644 --- a/LookupAnything/DataParser.cs +++ b/LookupAnything/DataParser.cs @@ -448,7 +448,9 @@ public RecipeModel[] GetRecipes(Metadata metadata, IMonitor monitor, ExtraMachin ]; ingredients.AddRange(additionalConsumedItems); - // if there are extra fuels added by the Extra Machine Config mod, add them here + List allOutputItems = [outputItem]; + + // if there are extra fuels or outputs added by the Extra Machine Config mod, add them here if (extraMachineConfig.IsLoaded) { foreach ((string extraItemId, int extraCount) in extraMachineConfig.ModApi.GetExtraRequirements(outputItem)) @@ -456,57 +458,62 @@ public RecipeModel[] GetRecipes(Metadata metadata, IMonitor monitor, ExtraMachin foreach ((string extraContextTags, int extraCount) in extraMachineConfig.ModApi.GetExtraTagsRequirements(outputItem)) ingredients.Add(new RecipeIngredientModel(RecipeType.MachineInput, null, extraCount, extraContextTags.Split(","))); + + allOutputItems.AddRange(extraMachineConfig.ModApi.GetExtraOutputs(outputItem)); } - // add produced item - ItemQueryContext itemQueryContext = new(); - IList itemQueryResults = ItemQueryResolver.TryResolve( - outputItem, - itemQueryContext, - formatItemId: id => id?.Replace("DROP_IN_ID", "0").Replace("DROP_IN_PRESERVE", "0").Replace("NEARBY_FLOWER_ID", "0") - ); + foreach (var outputItemData in allOutputItems) { + // add produced item + ItemQueryContext itemQueryContext = new(); + IList itemQueryResults = ItemQueryResolver.TryResolve( + outputItemData, + itemQueryContext, + formatItemId: id => id?.Replace("DROP_IN_ID", "0").Replace("DROP_IN_PRESERVE", "0").Replace("NEARBY_FLOWER_ID", "0") + ); - // get conditions - string[]? conditions = null; - { - // extract raw conditions - string? rawConditions = null; - if (!string.IsNullOrWhiteSpace(trigger.Condition)) - rawConditions = trigger.Condition; - if (!string.IsNullOrWhiteSpace(outputItem.Condition)) + + // get conditions + string[]? conditions = null; { - rawConditions = rawConditions != null - ? rawConditions + ", " + outputItem.Condition - : outputItem.Condition; + // extract raw conditions + string? rawConditions = null; + if (!string.IsNullOrWhiteSpace(trigger.Condition)) + rawConditions = trigger.Condition; + if (!string.IsNullOrWhiteSpace(outputItemData.Condition)) + { + rawConditions = rawConditions != null + ? rawConditions + ", " + outputItemData.Condition + : outputItemData.Condition; + } + + // parse + if (rawConditions != null) + conditions = GameStateQuery.SplitRaw(rawConditions).Distinct().ToArray(); } - // parse - if (rawConditions != null) - conditions = GameStateQuery.SplitRaw(rawConditions).Distinct().ToArray(); + // add to list + recipes.AddRange( + from result in itemQueryResults + select new RecipeModel( + key: null, + type: RecipeType.MachineInput, + displayType: ItemRegistry.GetDataOrErrorItem(qualifiedMachineId).DisplayName, + ingredients, + goldPrice: 0, + item: _ => ItemRegistry.Create(result.Item.QualifiedItemId), + isKnown: () => true, + machineId: qualifiedMachineId, + //exceptIngredients: recipe.ExceptIngredients.Select(id => new RecipeIngredientModel(id!.Value, 1)), + exceptIngredients: null, + outputQualifiedItemId: result.Item.QualifiedItemId, + minOutput: outputItemData.MinStack > 0 ? outputItemData.MinStack : 1, + maxOutput: outputItemData.MaxStack > 0 ? outputItemData.MaxStack : null, // TODO: Calculate this better + quality: outputItemData.Quality, + outputChance: 100 / outputRule.OutputItem.Count / itemQueryResults.Count, + conditions: conditions + ) + ); } - - // add to list - recipes.AddRange( - from result in itemQueryResults - select new RecipeModel( - key: null, - type: RecipeType.MachineInput, - displayType: ItemRegistry.GetDataOrErrorItem(qualifiedMachineId).DisplayName, - ingredients, - goldPrice: 0, - item: _ => ItemRegistry.Create(result.Item.QualifiedItemId), - isKnown: () => true, - machineId: qualifiedMachineId, - //exceptIngredients: recipe.ExceptIngredients.Select(id => new RecipeIngredientModel(id!.Value, 1)), - exceptIngredients: null, - outputQualifiedItemId: result.Item.QualifiedItemId, - minOutput: outputItem.MinStack > 0 ? outputItem.MinStack : 1, - maxOutput: outputItem.MaxStack > 0 ? outputItem.MaxStack : null, // TODO: Calculate this better - quality: outputItem.Quality, - outputChance: 100 / outputRule.OutputItem.Count / itemQueryResults.Count, - conditions: conditions - ) - ); } } } From 4a8ca038e5cf73cad3402362594a8e196cc45419 Mon Sep 17 00:00:00 2001 From: zombifier Date: Fri, 23 Aug 2024 18:26:03 -0400 Subject: [PATCH 2/4] Merge with condition changes --- LookupAnything/DataParser.cs | 118 +++++++++++++++++------------------ 1 file changed, 57 insertions(+), 61 deletions(-) diff --git a/LookupAnything/DataParser.cs b/LookupAnything/DataParser.cs index 26421611c..6e1ee054b 100644 --- a/LookupAnything/DataParser.cs +++ b/LookupAnything/DataParser.cs @@ -429,87 +429,83 @@ public RecipeModel[] GetRecipes(Metadata metadata, IMonitor monitor, ExtraMachin continue; // build output list - foreach (MachineItemOutput? outputItem in outputRule.OutputItem) + foreach (MachineItemOutput? mainOutputItem in outputRule.OutputItem) { - if (outputItem is null) - continue; - - // get conditions - List? conditions = null; - { - // extract raw conditions - string? rawConditions = null; - if (!string.IsNullOrWhiteSpace(trigger.Condition)) - rawConditions = trigger.Condition; - if (!string.IsNullOrWhiteSpace(outputItem.Condition)) - { - rawConditions = rawConditions != null - ? rawConditions + ", " + outputItem.Condition - : outputItem.Condition; - } - - // parse - if (rawConditions != null) - conditions = GameStateQuery.SplitRaw(rawConditions).Distinct().ToList(); - } - - // get ingredient - if (!this.TryGetMostSpecificIngredientIds(trigger.RequiredItemId, trigger.RequiredTags, ref conditions, out string? inputId, out string[] inputContextTags)) + if (mainOutputItem is null) continue; - // track whether some recipes are too complex to fully display - if (outputItem.OutputMethod != null) - someRulesTooComplex = true; + // if there are extra outputs added by the Extra Machine Config mod, add them here + List allOutputItems = [mainOutputItem]; - // add ingredients - List ingredients = [ - new RecipeIngredientModel(RecipeType.MachineInput, inputId, trigger.RequiredCount, inputContextTags) - ]; - ingredients.AddRange(additionalConsumedItems); - - List allOutputItems = [outputItem]; - - // if there are extra fuels or outputs added by the Extra Machine Config mod, add them here if (extraMachineConfig.IsLoaded) { - foreach ((string extraItemId, int extraCount) in extraMachineConfig.ModApi.GetExtraRequirements(outputItem)) - ingredients.Add(new RecipeIngredientModel(RecipeType.MachineInput, extraItemId, extraCount)); - - foreach ((string extraContextTags, int extraCount) in extraMachineConfig.ModApi.GetExtraTagsRequirements(outputItem)) - ingredients.Add(new RecipeIngredientModel(RecipeType.MachineInput, null, extraCount, extraContextTags.Split(","))); - - allOutputItems.AddRange(extraMachineConfig.ModApi.GetExtraOutputs(outputItem)); + allOutputItems.AddRange(extraMachineConfig.ModApi.GetExtraOutputs(mainOutputItem)); } - foreach (var outputItemData in allOutputItems) { - // add produced item - ItemQueryContext itemQueryContext = new(); - IList itemQueryResults = ItemQueryResolver.TryResolve( - outputItemData, - itemQueryContext, - formatItemId: id => id?.Replace("DROP_IN_ID", "0").Replace("DROP_IN_PRESERVE", "0").Replace("NEARBY_FLOWER_ID", "0") - ); - + foreach (var outputItem in allOutputItems) { // get conditions - string[]? conditions = null; + List? conditions = null; { // extract raw conditions string? rawConditions = null; if (!string.IsNullOrWhiteSpace(trigger.Condition)) rawConditions = trigger.Condition; - if (!string.IsNullOrWhiteSpace(outputItemData.Condition)) + // add condition of primary output + if (!string.IsNullOrWhiteSpace(mainOutputItem.Condition)) { rawConditions = rawConditions != null - ? rawConditions + ", " + outputItemData.Condition - : outputItemData.Condition; + ? rawConditions + ", " + mainOutputItem.Condition + : mainOutputItem.Condition; + } + // add condition of secondary outputs from EMC if any + if (outputItem != mainOutputItem && !string.IsNullOrWhiteSpace(outputItem.Condition)) + { + rawConditions = rawConditions != null + ? rawConditions + ", " + outputItem.Condition + : outputItem.Condition; + } // parse if (rawConditions != null) - conditions = GameStateQuery.SplitRaw(rawConditions).Distinct().ToArray(); + conditions = GameStateQuery.SplitRaw(rawConditions).Distinct().ToList(); + } + + // get ingredient + if (!this.TryGetMostSpecificIngredientIds(trigger.RequiredItemId, trigger.RequiredTags, ref conditions, out string? inputId, out string[] inputContextTags)) + continue; + + // track whether some recipes are too complex to fully display + if (outputItem.OutputMethod != null) + someRulesTooComplex = true; + + // add ingredients + List ingredients = [ + new RecipeIngredientModel(RecipeType.MachineInput, inputId, trigger.RequiredCount, inputContextTags) + ]; + ingredients.AddRange(additionalConsumedItems); + + // if there are extra fuels added by the Extra Machine Config mod, add them here + if (extraMachineConfig.IsLoaded) + { + foreach ((string extraItemId, int extraCount) in extraMachineConfig.ModApi.GetExtraRequirements(outputItem)) + ingredients.Add(new RecipeIngredientModel(RecipeType.MachineInput, extraItemId, extraCount)); + + foreach ((string extraContextTags, int extraCount) in extraMachineConfig.ModApi.GetExtraTagsRequirements(outputItem)) + ingredients.Add(new RecipeIngredientModel(RecipeType.MachineInput, null, extraCount, extraContextTags.Split(","))); + + allOutputItems.AddRange(extraMachineConfig.ModApi.GetExtraOutputs(outputItem)); } + // add produced item + ItemQueryContext itemQueryContext = new(); + IList itemQueryResults = ItemQueryResolver.TryResolve( + outputItem, + itemQueryContext, + formatItemId: id => id?.Replace("DROP_IN_ID", "0").Replace("DROP_IN_PRESERVE", "0").Replace("NEARBY_FLOWER_ID", "0") + ); + // add to list recipes.AddRange( from result in itemQueryResults @@ -525,9 +521,9 @@ from result in itemQueryResults //exceptIngredients: recipe.ExceptIngredients.Select(id => new RecipeIngredientModel(id!.Value, 1)), exceptIngredients: null, outputQualifiedItemId: result.Item.QualifiedItemId, - minOutput: outputItemData.MinStack > 0 ? outputItemData.MinStack : 1, - maxOutput: outputItemData.MaxStack > 0 ? outputItemData.MaxStack : null, // TODO: Calculate this better - quality: outputItemData.Quality, + minOutput: outputItem.MinStack > 0 ? outputItem.MinStack : 1, + maxOutput: outputItem.MaxStack > 0 ? outputItem.MaxStack : null, // TODO: Calculate this better + quality: outputItem.Quality, outputChance: 100 / outputRule.OutputItem.Count / itemQueryResults.Count, conditions: conditions?.ToArray() ) From 58ba70ef28f563a3a08cb59daaad137e198d2e44 Mon Sep 17 00:00:00 2001 From: zombifier Date: Fri, 23 Aug 2024 21:39:05 -0400 Subject: [PATCH 3/4] Fixes --- LookupAnything/DataParser.cs | 5 ----- 1 file changed, 5 deletions(-) diff --git a/LookupAnything/DataParser.cs b/LookupAnything/DataParser.cs index 6e1ee054b..0b5e38389 100644 --- a/LookupAnything/DataParser.cs +++ b/LookupAnything/DataParser.cs @@ -436,14 +436,12 @@ public RecipeModel[] GetRecipes(Metadata metadata, IMonitor monitor, ExtraMachin // if there are extra outputs added by the Extra Machine Config mod, add them here List allOutputItems = [mainOutputItem]; - if (extraMachineConfig.IsLoaded) { allOutputItems.AddRange(extraMachineConfig.ModApi.GetExtraOutputs(mainOutputItem)); } foreach (var outputItem in allOutputItems) { - // get conditions List? conditions = null; { @@ -464,7 +462,6 @@ public RecipeModel[] GetRecipes(Metadata metadata, IMonitor monitor, ExtraMachin rawConditions = rawConditions != null ? rawConditions + ", " + outputItem.Condition : outputItem.Condition; - } // parse @@ -494,8 +491,6 @@ public RecipeModel[] GetRecipes(Metadata metadata, IMonitor monitor, ExtraMachin foreach ((string extraContextTags, int extraCount) in extraMachineConfig.ModApi.GetExtraTagsRequirements(outputItem)) ingredients.Add(new RecipeIngredientModel(RecipeType.MachineInput, null, extraCount, extraContextTags.Split(","))); - - allOutputItems.AddRange(extraMachineConfig.ModApi.GetExtraOutputs(outputItem)); } // add produced item From f07f5ecaa7065399b9ae27744d5b1b14d3a130be Mon Sep 17 00:00:00 2001 From: zombifier Date: Tue, 27 Aug 2024 20:40:44 -0400 Subject: [PATCH 4/4] Update EMC API to the new version in 1.10.0 --- .../Integrations/ExtraMachineConfig/IExtraMachineConfigApi.cs | 2 +- LookupAnything/DataParser.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Common/Integrations/ExtraMachineConfig/IExtraMachineConfigApi.cs b/Common/Integrations/ExtraMachineConfig/IExtraMachineConfigApi.cs index 8200410da..6bcabafaa 100644 --- a/Common/Integrations/ExtraMachineConfig/IExtraMachineConfigApi.cs +++ b/Common/Integrations/ExtraMachineConfig/IExtraMachineConfigApi.cs @@ -16,6 +16,6 @@ public interface IExtraMachineConfigApi /// Retrieves the extra output items produced by this recipe. /// The output rule to check. - IList GetExtraOutputs(MachineItemOutput outputData); + IList GetExtraOutputs(MachineItemOutput outputData, MachineData? machine); } } diff --git a/LookupAnything/DataParser.cs b/LookupAnything/DataParser.cs index 0b5e38389..ac66b1a9f 100644 --- a/LookupAnything/DataParser.cs +++ b/LookupAnything/DataParser.cs @@ -438,7 +438,7 @@ public RecipeModel[] GetRecipes(Metadata metadata, IMonitor monitor, ExtraMachin List allOutputItems = [mainOutputItem]; if (extraMachineConfig.IsLoaded) { - allOutputItems.AddRange(extraMachineConfig.ModApi.GetExtraOutputs(mainOutputItem)); + allOutputItems.AddRange(extraMachineConfig.ModApi.GetExtraOutputs(mainOutputItem, machineData)); } foreach (var outputItem in allOutputItems) {