compacting-storage.lua: bugfixes and also I forgot to implement a function

This commit is contained in:
Emerson Rosen-Jones 2025-11-17 20:09:34 -05:00
parent cce18737d2
commit 8981badcf9

View file

@ -15,7 +15,7 @@ local ITEM_TYPES = { "iron", "copper", "zinc", "gold", "electrum" }
local STACKS = 64 local STACKS = 64
local NUGGETS = 1 local NUGGETS = 1
local INGOTS = 9 local INGOTS = 9
local BLOCKS = 64 local BLOCKS = 81
-- Levels -- Levels
local MIN_NUM = (4 * STACKS * NUGGETS) + (4 * STACKS * INGOTS) local MIN_NUM = (4 * STACKS * NUGGETS) + (4 * STACKS * INGOTS)
@ -81,14 +81,18 @@ function execute_crafts (item_type, crafts)
for type, num_crafts in pairs(crafts) do for type, num_crafts in pairs(crafts) do
if type == "nugget" then if type == "nugget" then
if num_crafts > 0 then if num_crafts > 0 then
print(("Crafting %s nuggets"):format(item_type))
craft_multiple(item_type, 4, num_crafts) craft_multiple(item_type, 4, num_crafts)
elseif num_crafts < 0 then elseif num_crafts < 0 then
print(("Crafting %s ingots"):format(item_type))
craft_multiple(item_type, 1, num_crafts) craft_multiple(item_type, 1, num_crafts)
end end
elseif type == "block" then elseif type == "block" then
if num_crafts > 0 then if num_crafts > 0 then
print(("Crafting %s blocks"):format(item_type))
craft_multiple(item_type, 2, num_crafts) craft_multiple(item_type, 2, num_crafts)
elseif num_crafts < 0 then elseif num_crafts < 0 then
print(("Crafting %s ingots"):format(item_type))
craft_multiple(item_type, 3, num_crafts) craft_multiple(item_type, 3, num_crafts)
end end
end end
@ -98,11 +102,13 @@ end
function set_production (item_type, produce) function set_production (item_type, produce)
local target_string = target[item_type][5] local target_string = target[item_type][5]
if target_string == nil then return end if target_string == nil then return end
local relay_num, face = string.match(target_string, local relay_num, face = string.match(
"(d+):(%l+)" target_string,
"(%d+):(%l+)"
) )
local periph = string.format("redstone_relay_%d", relay_num) local periph = string.format("redstone_relay_%d", relay_num)
-- ON is disable production -- ON is disable production
if produce then print(("Producing %s"):format(item_type))
peripheral.call(periph, "setOutput", face, not produce) peripheral.call(periph, "setOutput", face, not produce)
end end
@ -123,15 +129,30 @@ end
function get_dist (item_type, items) function get_dist (item_type, items)
local dist = { nuggets = 0, ingots = 0, blocks = 0 } local dist = { nuggets = 0, ingots = 0, blocks = 0 }
for _, item in pairs(items) do for item, count in pairs(items) do
local type, form = item.name:match".-:(%l-)_(%l*)" local type, form = item:match":(%l-)_(%l*)"
form = string.format("%ss", form)
if type == item_type and dist[form] ~= nil then if type == item_type and dist[form] ~= nil then
dist[form] = dist[form] + item.count dist[form] = dist[form] + count
end end
end end
return dist return dist
end end
function decide_dist (num)
local dist = { nuggets = 0, ingots = 0, blocks = 0 }
if num > MAX_NUM then
local excess = num - MAX_NUM
dist.blocks = math.floor(excess / BLOCKS)
num = num - (dist.blocks * BLOCKS)
end
local balance = math.floor(num / 10)
dist.nuggets, dist.ingots = balance, balance
num = num - (balance * 10)
dist.nuggets = dist.nuggets + num
return dist
end
function get_diff (dist1, dist2) function get_diff (dist1, dist2)
return { return {
nuggets = dist1.nuggets - dist2.nuggets, nuggets = dist1.nuggets - dist2.nuggets,
@ -169,9 +190,10 @@ function diff_to_crafts (diff)
end end
function print_counts (items) function print_counts (items)
for type, counts in pairs(items) do for item, amount in pairs(items) do
for form, count in pairs(counts) do local type, form = string.match(item, ":(%l+)_(%l+)")
print(("%s %s: %d"):format(type, form, count)) if type ~= nil and form ~= nil then
print(("%s %s: %d"):format(type, form, amount))
end end
end end
end end
@ -185,7 +207,7 @@ while true do
for _, item_type in ipairs(ITEM_TYPES) do for _, item_type in ipairs(ITEM_TYPES) do
local current_dist = get_dist(item_type, items) local current_dist = get_dist(item_type, items)
local num = dist_to_num(current_dist) local num = dist_to_num(current_dist)
if num > MAX_NUM then set_production(item_type, false) if num > MAX_NUM then set_production(item_type, false) end
if num > MIN_NUM then if num > MIN_NUM then
local desired_dist = decide_dist(num) local desired_dist = decide_dist(num)
local diff = get_diff(current_dist, desired_dist) local diff = get_diff(current_dist, desired_dist)