From f427bae557f8376e51d296f871f33d56dc541e12 Mon Sep 17 00:00:00 2001 From: Tim Date: Mon, 26 Jan 2015 21:34:32 +0100 Subject: also cache the recipe consumption to get rid of an additional loop per craft --- autocrafter.lua | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) (limited to 'autocrafter.lua') diff --git a/autocrafter.lua b/autocrafter.lua index a60b0d8..0fc5c78 100644 --- a/autocrafter.lua +++ b/autocrafter.lua @@ -39,7 +39,7 @@ local function on_recipe_change(pos, inventory) if recipe_changed then 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 end @@ -75,15 +75,7 @@ local function autocraft(inventory, pos) local output_item = craft.output.item 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 = {} - 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 consumption = craft.consumption local inv_index = count_index(inventory:get_list("src")) -- check if we have enough materials available for itemname, number in pairs(consumption) do -- cgit v1.2.3