More work on compacting-storage.lua.

This commit is contained in:
Emerson Rosen-Jones 2025-09-14 14:11:35 -04:00
parent ed0671d50b
commit 562303e98f

View file

@ -86,12 +86,14 @@ function drain (type, form, amount)
elseif form == "ingot" then elseif form == "ingot" then
conversion = "ingot_to_block" conversion = "ingot_to_block"
end end
local num_requests = math.ceil(amount / AMOUNT_USED)
print(("Draining %d %s %ss"):format(amount, type, form)) print(("Draining %d %s %ss"):format(amount, type, form))
request_multiple(type, conversion, amount) request_multiple(type, conversion, num_requests)
end end
function fill (type, dest, amount) function fill (type, src, dest, amount)
local conversion = nil local conversion = nil
local num_requests = math.ceil(amount / AMOUNT_USED)
if dest == "block" then if dest == "block" then
conversion = "ingot_to_block" conversion = "ingot_to_block"
elseif dest == "ingot" then elseif dest == "ingot" then
@ -99,51 +101,72 @@ function fill (type, dest, amount)
conversion = "nugget_to_ingot" conversion = "nugget_to_ingot"
elseif src == "block" then elseif src == "block" then
conversion = "block_to_ingot" conversion = "block_to_ingot"
num_requests = math.ceil(amount / AMOUNT_RETURNED)
end end
end end
print(("Converting %d %s %ss to %ss"):format(amount, type, src, dest)) print(("Converting %d %s %ss to %ss"):format(amount, type, src, dest))
request_multiple(type, conversion, amount) request_multiple(type, conversion, num_requests)
end end
-- making the decision what to do -- making the decision what to do
function should_drain (type, counts) function should_drain (type, counts)
local drain_form, amount = nil, 0
for _, form in ipairs(PRIORITY) do for _, form in ipairs(PRIORITY) do
local diff = counts[form] - limits[type][form][2] local diff = counts[form] - limits[type][form][2]
if diff > 0 then if diff > 0 then
amount = math.ceil(diff / AMOUNT_PER) return form, diff
drain_form = form
return drain_form, amount
end end
end end
end end
function should_fill (type, counts) function should_fill (type, counts)
local fill_dest, fill_src, amount = nil, nil, 0 local fill_dest, fill_src, amount_needed, amount_spare = nil, nil, 0, 0
for _, form in ipairs(PRIORITY) do for _, form in ipairs(PRIORITY) do
local diff = limits[type][form][1] - counts[form] local diff = limits[type][form][1] - counts[form]
if diff > 0 then if diff > 0 then
if fill_dest == nil then if fill_dest == nil then
amount = math.ceil(diff / AMOUNT_PER)
fill_dest = form fill_dest = form
amount_needed = diff
end end
else else
fill_src = form fill_src = form
amount_spare = counts[form]
end end
if fill_src ~= nil and fill_dest ~= nil then break end if fill_src ~= nil and fill_dest ~= nil then break end
end end
return fill_dest, fill_src, amount local amount_to_use = do
local ratio = AMOUNT_USED / AMOUNT_RETURNED
local amount_needed_src = nil
if fill_src == "block" then
amount_needed_src = amount_needed / ratio
else
amount_needed_src = amount_needed * ratio
end
if amount_needed_src < amount_spare then
return amount_needed_src
else
return amount_spare
end
end
return fill_dest, fill_src, amount_to_use
end end
function decide_action (type, counts) function decide_action (type, counts)
-- using the levels of each form of the item, decide what to do -- using the levels of each form of the item, decide what to do
for _, form in ipairs(PRIORITY) do for _, form in ipairs(PRIORITY) do
local drain_form, drain_amt = should_drain(type, counts) local drain_form, drain_amt = should_drain(type, counts)
local fill_dest, fill_src, fill_amt local fill_dest, fill_src, fill_amt = should_fill(type, counts)
if drain_form ~= nil then if drain_form ~= nil then
drain(type, form, drain_amt) drain(type, form, drain_amt)
elseif fill_dest ~= nil then elseif fill_dest ~= nil then
fill(type, form, fill_amt) fill(type, fill_src, fill_dest, fill_amt)
end
end
end
function print_counts (items)
for type, counts in pairs(items) do
for form, count in ipairs(counts) do
print(("%s %s: %d"):format(type, form, count))
end end
end end
end end
@ -151,6 +174,7 @@ end
while true do while true do
-- 1. Sum items in inventory and organize by item type -- 1. Sum items in inventory and organize by item type
local items = sum_items(peripheral.wrap(INVENTORY)) local items = sum_items(peripheral.wrap(INVENTORY))
print_counts(items)
-- 2. take one action for each item type per cycle -- 2. take one action for each item type per cycle
for item_type, counts in pairs(items) do for item_type, counts in pairs(items) do