Skip to content
Open
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
24 changes: 12 additions & 12 deletions UI/GUIs/CraftingGUI.ItemSlotMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,21 +41,21 @@ internal static Item GetIngredient(int slot, ref int context)
if (ProcessGroupsForText(selectedRecipe, item.type, out string nameOverride))
item.SetNameOverride(nameOverride);

// OPTIMIZATION: Use itemCounts dictionary for O(1) lookups instead of O(n*m*k*p) nested loops through storageItems
// This fixes severe lag when player has multiple stacks of certain items (stone, mud, etc.)

int directItemCount = itemCounts.TryGetValue(item.type, out int count) ? count : 0;

ClampedArithmetic totalGroupStack = 0;
// Local capturing
Item i = item;
Item storageItem = storageItems.FirstOrDefault(stored => stored.type == i.type) ?? new Item();

// Calculate recipe group totals by dictionary lookup instead of iterating storageItems
foreach (RecipeGroup rec in selectedRecipe.acceptedGroups.Select(index => RecipeGroup.recipeGroups[index])) {
if (rec.ContainsItem(item.type)) {
foreach (int type in rec.ValidItems) {
foreach (Item storedItem in storageItems) {
if (storedItem.type == type) {
totalGroupStack += storedItem.stack;
if (itemCounts.TryGetValue(type, out int groupItemCount)) {
totalGroupStack += groupItemCount;

if (totalGroupStack >= item.stack)
goto StopCheckingRecipeGroups;
}
if (totalGroupStack >= item.stack)
goto StopCheckingRecipeGroups;
}
}
}
Expand All @@ -64,9 +64,9 @@ internal static Item GetIngredient(int slot, ref int context)
StopCheckingRecipeGroups:

if (!item.IsAir) {
if (storageItem.IsAir && (int)totalGroupStack == 0)
if (directItemCount == 0 && (int)totalGroupStack == 0)
context = MagicSlotContext.IngredientNotCraftable;
else if (storageItem.stack < item.stack && totalGroupStack < item.stack)
else if (directItemCount < item.stack && totalGroupStack < item.stack)
context = MagicSlotContext.IngredientPartiallyInStock;

if (context != MagicSlotContext.Normal) {
Expand Down