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

[LookupAnything] Add integration with Extra Machine Config's multiple output items feature #1028

Open
wants to merge 7 commits into
base: develop
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,8 @@ public interface IExtraMachineConfigApi
/// <summary>Retrieves the extra tag-defined fuels consumed by this recipe.</summary>
/// <param name="outputData">The output rule to check.</param>
IList<(string, int)> GetExtraTagsRequirements(MachineItemOutput outputData);

/// <summary>Retrieves the extra output items produced by this recipe.</summary>
/// <param name="outputData">The output rule to check.</param>
IList<MachineItemOutput> GetExtraOutputs(MachineItemOutput outputData, MachineData? machine);
}
162 changes: 90 additions & 72 deletions LookupAnything/DataParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -474,93 +474,111 @@ 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)
if (mainOutputItem is null)
continue;

// get conditions
List<string>? conditions = null;
// if there are extra outputs added by the Extra Machine Config mod, add them here
List<MachineItemOutput> allOutputItems = [mainOutputItem];
if (extraMachineConfig.IsLoaded)
{
allOutputItems.AddRange(extraMachineConfig.ModApi.GetExtraOutputs(mainOutputItem, machineData));
}

foreach (var outputItem in allOutputItems)
{
// extract raw conditions
string? rawConditions = null;
if (!string.IsNullOrWhiteSpace(trigger.Condition))
rawConditions = trigger.Condition;
if (!string.IsNullOrWhiteSpace(outputItem.Condition))
// get conditions
List<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;
// add condition of primary output
if (!string.IsNullOrWhiteSpace(mainOutputItem.Condition))
{
rawConditions = rawConditions != null
? 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().ToList();
}

// 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))
continue;

// 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;

// track whether some recipes are too complex to fully display
if (outputItem.OutputMethod != null)
someRulesTooComplex = true;
// add ingredients
List<RecipeIngredientModel> ingredients = [
new RecipeIngredientModel(RecipeType.MachineInput, inputId, trigger.RequiredCount, inputContextTags)
];
ingredients.AddRange(additionalConsumedItems);

// add ingredients
List<RecipeIngredientModel> 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));

// 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(",")));
}

foreach ((string extraContextTags, int extraCount) in extraMachineConfig.ModApi.GetExtraTagsRequirements(outputItem))
ingredients.Add(new RecipeIngredientModel(RecipeType.MachineInput, null, extraCount, extraContextTags.Split(",")));
}
// add produced item
IList<ItemQueryResult> itemQueryResults;
if (outputItem.ItemId != null || outputItem.RandomItemId != null)
{
ItemQueryContext itemQueryContext = new();
itemQueryResults = ItemQueryResolver.TryResolve(
outputItem,
itemQueryContext,
formatItemId: id => id?.Replace("DROP_IN_ID", "0").Replace("DROP_IN_PRESERVE", "0").Replace("NEARBY_FLOWER_ID", "0")
);
}
else
{
itemQueryResults = [];
someRulesTooComplex = true;
}

// add produced item
IList<ItemQueryResult> itemQueryResults;
if (outputItem.ItemId != null || outputItem.RandomItemId != null)
{
ItemQueryContext itemQueryContext = new();
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
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?.ToArray()
)
);
}
else
{
itemQueryResults = [];
someRulesTooComplex = true;
}

// 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?.ToArray()
)
);
}
}
}
Expand Down