More work on compacting-storage.lua.

This commit is contained in:
Emerson Rosen-Jones 2025-09-14 13:40:11 -04:00
parent 1fc836d144
commit 52555e6b84

View file

@ -6,9 +6,10 @@
-- constants -- constants
local INVENTORY = "" local INVENTORY = "left"
local SLEEP_TIME = 30 local SLEEP_TIME = 30
local AMOUNT_PER = 63 -- the amount of item change per request local AMOUNT_USED = 63 -- the amount of item used per request
local AMOUNT_RETURNED = 7 -- the amount of item returned per request
local ITEM_TYPES = { "iron", "copper", "zinc", "gold", "electrum" } local ITEM_TYPES = { "iron", "copper", "zinc", "gold", "electrum" }
local PRIORITY = { "ingot", "block", "nugget" } local PRIORITY = { "ingot", "block", "nugget" }
@ -18,11 +19,11 @@ end
-- TODO map targets to specific relays -- TODO map targets to specific relays
local target = {} local target = {}
target.iron = peripheral.wrap"" target.iron = peripheral.wrap"redstone_relay_0"
target.copper = peripheral.wrap"" target.copper = peripheral.wrap"redstone_relay_1"
target.zinc = peripheral.wrap"" target.zinc = peripheral.wrap"redstone_relay_2"
target.gold = peripheral.wrap"" target.gold = peripheral.wrap"redstone_relay_3"
target.electrum = peripheral.wrap"" target.electrum = peripheral.wrap"redstone_relay_4"
-- Levels -- Levels
local limits = {} local limits = {}
@ -52,7 +53,7 @@ function sum_items (inv)
end end
for slot, item in pairs(inv.list()) do for slot, item in pairs(inv.list()) do
local type, form = item.name:match".-:(%l-)_(%l*)" local type, form = item.name:match".-:(%l-)_(%l*)"
if type ~= nil and form ~= nil then if result[type] ~= nil then
result[type][form] = result[type][form] + item.count result[type][form] = result[type][form] + item.count
end end
end end
@ -85,10 +86,11 @@ function drain (type, form, amount)
elseif form == "ingot" then elseif form == "ingot" then
conversion = "ingot_to_block" conversion = "ingot_to_block"
end end
print("Draining %d %s %ss":format(amount, type, form))
request_multiple(type, conversion, amount) request_multiple(type, conversion, amount)
end end
function fill (type, form, amount) function fill (type, dest, amount)
local conversion = nil local conversion = nil
if dest == "block" then if dest == "block" then
conversion = "ingot_to_block" conversion = "ingot_to_block"
@ -99,6 +101,7 @@ function fill (type, form, amount)
conversion = "block_to_ingot" conversion = "block_to_ingot"
end end
end end
print("Converting %d %s %ss to %ss":format(amount, type, src, dest))
request_multiple(type, conversion, amount) request_multiple(type, conversion, amount)
end end
@ -106,7 +109,7 @@ end
function should_drain (type, counts) function should_drain (type, counts)
local drain_form, amount = nil, 0 local drain_form, amount = nil, 0
for _, form in ipairs(PRIORITY) do for _, form in ipairs(PRIORITY) do
local diff = counts[form] - target[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) amount = math.ceil(diff / AMOUNT_PER)
drain_form = form drain_form = form
@ -118,7 +121,7 @@ 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 = nil, nil, 0
for _, form in ipairs(PRIORITY) do for _, form in ipairs(PRIORITY) do
local diff = target[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) amount = math.ceil(diff / AMOUNT_PER)