Some bugfixes to compacting-storage.lua.

This commit is contained in:
Emerson Rosen-Jones 2025-09-14 12:58:09 -04:00
parent eeb2891e9c
commit 26016b36d9

View file

@ -7,7 +7,8 @@
-- constants
local INVENTORY = ""
local SLEEP_TIME = 5
local SLEEP_TIME = 30
local AMOUNT_PER = 63 -- the amount of item change per request
local ITEM_TYPES = { "iron", "copper", "zinc", "gold", "electrum" }
local PRIORITY = { "ingot", "block", "nugget" }
@ -17,8 +18,16 @@ end
-- TODO map targets to specific relays
local target = {}
target.iron = peripheral.wrap""
target.copper = peripheral.wrap""
target.zinc = peripheral.wrap""
target.gold = peripheral.wrap""
target.electrum = peripheral.wrap""
-- Levels
local limits = {}
for _, type in ipairs(ITEM_TYPES) do
target[type] = {
limits[type] = {
"nugget" = { stacks(4), stacks(8) },
"ingot" = { stacks(4), stacks(8) },
"block" = { stacks(4), stacks(1000) }
@ -51,9 +60,9 @@ end
-- basic actions
function request (item_type, conversion_type)
target[item_type].setOutput(convert.conversion_type, true)
target[item_type].setOutput(convert[conversion_type], true)
os.sleep(0.05)
target[item_type].setOutput(convert.conversion_type, false)
target[item_type].setOutput(convert[conversion_type], false)
end
function request_multiple (item_type, conversion_type, count)
@ -68,8 +77,6 @@ function request_multiple (item_type, conversion_type, count)
end
-- fundamental actions
-- assumptions: a drain or fill of 1 will increase or decrease the amount of
-- that item by 63
function drain (type, form, amount)
local conversion = nil
if form == "nugget" then
@ -95,14 +102,12 @@ function fill (type, form, amount)
end
-- making the decision what to do
-- assumptions: a drain or fill of 1 will increase or decrease the amount of
-- that item by 63
function should_drain (type, counts)
local drain_form, amount = nil, 0
for _, form in ipairs(PRIORITY) do
local diff = counts[form] - target[type][form][2]
if diff > 0 then
amount = math.ceil(diff / 63)
amount = math.ceil(diff / AMOUNT_PER)
drain_form = form
return drain_form, amount
end
@ -115,7 +120,7 @@ function should_fill (type, counts)
local diff = target[type][form][1] - counts[form]
if diff > 0 then
if fill_dest == nil then
amount = math.ceil(diff / 63)
amount = math.ceil(diff / AMOUNT_PER)
fill_dest = form
end
else