1
0
mirror of https://github.com/mt-mods/pipeworks.git synced 2025-05-11 21:30:22 +02:00

use has_room_for_output

This commit is contained in:
Luke aka SwissalpS 2023-06-15 03:47:57 +02:00
parent 2638d41d48
commit a8570f6071

View File

@ -148,31 +148,19 @@ local function calculate_consumption(inv_index, consumption_with_groups)
return consumption return consumption
end 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 function has_room_for_output(list_output, index_output)
local name local name
local empty_count = 0 local empty_count = 0
for _,item in pairs(inventory:get_list("dst")) do
for _, item in pairs(list_output) do for _, item in pairs(list_output) do
if item:is_empty() then if item:is_empty() then
empty_count = empty_count + 1 empty_count = empty_count + 1
else 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() name = item:get_name()
if index_output[name] then if index_output[name] then
index_output[name] = index_output[name] - item:get_free_space() index_output[name] = index_output[name] - item:get_free_space()
end end
end end
end end
for _,count in pairs(out_items) do
for _, count in pairs(index_output) do for _, count in pairs(index_output) do
if count > 0 then if count > 0 then
empty_count = empty_count - 1 empty_count = empty_count - 1
@ -185,6 +173,17 @@ local function has_room_for_output(list_output, index_output)
return true return true
end 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 -- check if we have enough material available
local inv_index = count_index(inventory:get_list("src")) local inv_index = count_index(inventory:get_list("src"))
local consumption = calculate_consumption(inv_index, craft.consumption) local consumption = calculate_consumption(inv_index, craft.consumption)