From a8570f6071d70a09fc3998f06c99927915ebe797 Mon Sep 17 00:00:00 2001 From: Luke aka SwissalpS Date: Thu, 15 Jun 2023 03:47:57 +0200 Subject: [PATCH] use has_room_for_output --- autocrafter.lua | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/autocrafter.lua b/autocrafter.lua index fa77973..29056d0 100644 --- a/autocrafter.lua +++ b/autocrafter.lua @@ -148,31 +148,19 @@ local function calculate_consumption(inv_index, consumption_with_groups) return consumption end -local function autocraft(inventory, craft) - if not craft then return false end - - -- check if output and all replacements fit in dst - local output = craft.output.item - local out_items = count_index(craft.decremented_input.items) - out_items[output:get_name()] = (out_items[output:get_name()] or 0) + output:get_count() local function has_room_for_output(list_output, index_output) local name local empty_count = 0 - for _,item in pairs(inventory:get_list("dst")) do for _, item in pairs(list_output) do if item:is_empty() then empty_count = empty_count + 1 else - local name = item:get_name() - if out_items[name] then - out_items[name] = out_items[name] - item:get_free_space() name = item:get_name() if index_output[name] then index_output[name] = index_output[name] - item:get_free_space() end end end - for _,count in pairs(out_items) do for _, count in pairs(index_output) do if count > 0 then empty_count = empty_count - 1 @@ -185,6 +173,17 @@ local function has_room_for_output(list_output, index_output) return true end +local function autocraft(inventory, craft) + if not craft then return false end + + -- check if output and all replacements fit in dst + local output = craft.output.item + local out_items = count_index(craft.decremented_input) + out_items[output:get_name()] = (out_items[output:get_name()] or 0) + output:get_count() + if not has_room_for_output(inventory:get_list("dst"), out_items) then + return false + end + -- check if we have enough material available local inv_index = count_index(inventory:get_list("src")) local consumption = calculate_consumption(inv_index, craft.consumption)