More bugfixes for compacting-storage.lua

This commit is contained in:
Emerson Rosen-Jones 2025-09-14 14:37:35 -04:00
parent ca8d0fea41
commit 892db3d65f

View file

@ -10,6 +10,7 @@ local INVENTORY = "left"
local SLEEP_TIME = 30
local AMOUNT_USED = 63 -- the amount of item used per request
local AMOUNT_RETURNED = 7 -- the amount of item returned per request
local REDUCTION_FACTOR = 0.5 -- when draining, what percent to do at a time
local ITEM_TYPES = { "iron", "copper", "zinc", "gold", "electrum" }
local PRIORITY = { "ingot", "block", "nugget" }
@ -113,7 +114,7 @@ function should_drain (type, counts)
for _, form in ipairs(PRIORITY) do
local diff = counts[form] - limits[type][form][2]
if diff > 0 then
return form, diff
return form, diff * REDUCTION_FACTOR
end
end
end
@ -151,20 +152,18 @@ end
function decide_action (type, counts)
-- using the levels of each form of the item, decide what to do
for _, form in ipairs(PRIORITY) do
local drain_form, drain_amt = should_drain(type, counts)
local fill_dest, fill_src, fill_amt = should_fill(type, counts)
if drain_form ~= nil then
drain(type, form, drain_amt)
elseif fill_dest ~= nil then
elseif fill_dest ~= nil and fill_src ~= nil then
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
for form, count in pairs(counts) do
print(("%s %s: %d"):format(type, form, count))
end
end
@ -173,7 +172,7 @@ end
while true do
-- 1. Sum items in inventory and organize by item type
local items = sum_items(peripheral.wrap(INVENTORY))
print_counts(items)
-- print_counts(items)
-- 2. take one action for each item type per cycle
for item_type, counts in pairs(items) do