diff --git a/craft.lua b/craft.lua index aaefad5..03899a4 100644 --- a/craft.lua +++ b/craft.lua @@ -217,6 +217,8 @@ local function craft_recipe_recursion(recipe, available_items, currently_availab local total_required_items = {} local total_crafting_order = {} + -- print('Crafting ' .. recipe.result) + -- go through all the required ingredients for item, count in pairs(recipe.requires) do -- check if some crafting result provided this item @@ -224,11 +226,16 @@ local function craft_recipe_recursion(recipe, available_items, currently_availab if not available then available = 0 end + -- print('Requires ' .. count .. 'x ' .. item ) + -- print('I have ' .. available .. ' ' .. item .. ' from previous crafting') + if available >= count then - -- print('I have ' .. count .. ' ' .. item .. ' from previous crafting') -- remove it from tracked available items so if other recipe requires it, it needs to craft it currently_available[item] = currently_available[item] - count else + -- remove the items it has from tracked available items so if other recipe requires it, it needs to craft it + currently_available[item] = 0 + local required_count = count - available -- check if there is enough in the main available storage @@ -237,15 +244,23 @@ local function craft_recipe_recursion(recipe, available_items, currently_availab available = 0 end if available >= required_count then - -- print('I have ' .. required_count .. ' ' .. item .. ' available') + -- print('I have ' .. available .. ' ' .. item .. ' available') + -- add the required amount to required items add(total_required_items, item, required_count) -- remove it from tracked available items so if other recipe requires it, it needs to craft it - available_items[item] = available_items[item] - count + available_items[item] = available_items[item] - required_count else + if available > 0 then + -- add what it has to required items + add(total_required_items, item, available) + -- remove it from tracked available items so if other recipe requires it, it needs to craft it + available_items[item] = 0 + end + -- update count required_count = required_count - available - -- print('Missing ' .. required_count .. ' ' .. item) + -- print('Have ' .. available .. ' and is missing ' .. required_count .. ' ' .. item) -- check if I can craft it local sub_recipe = recipes[item] @@ -272,7 +287,7 @@ local function craft_recipe_recursion(recipe, available_items, currently_availab end else -- print('Need ' .. required_count .. ' more ' .. item) - add(total_required_items, item, count) + add(total_required_items, item, required_count) end end end