also cache the recipe consumption to get rid of an additional loop per craft

This commit is contained in:
Tim 2015-01-26 21:34:32 +01:00
parent cee16932be
commit f427bae557

@ -39,7 +39,7 @@ local function on_recipe_change(pos, inventory)
if recipe_changed then if recipe_changed then
local output, decremented_input = minetest.get_craft_result({method = "normal", width = 3, items = recipe}) local output, decremented_input = minetest.get_craft_result({method = "normal", width = 3, items = recipe})
craft = {recipe = recipe, output = output, decremented_input = decremented_input} craft = {recipe = recipe, consumption=count_index(recipe), output = output, decremented_input = decremented_input}
autocrafterCache[hash] = craft autocrafterCache[hash] = craft
end end
@ -75,15 +75,7 @@ local function autocraft(inventory, pos)
local output_item = craft.output.item local output_item = craft.output.item
if output_item:is_empty() or not inventory:room_for_item("dst", output_item) then return end if output_item:is_empty() or not inventory:room_for_item("dst", output_item) then return end
-- determine how much we have to consume each craft local consumption = craft.consumption
local consumption = {}
for _, item in ipairs(recipe) do
if item and not item:is_empty() then
local item_name = item:get_name()
consumption[item_name] = (consumption[item_name] or 0) + 1
end
end
local inv_index = count_index(inventory:get_list("src")) local inv_index = count_index(inventory:get_list("src"))
-- check if we have enough materials available -- check if we have enough materials available
for itemname, number in pairs(consumption) do for itemname, number in pairs(consumption) do