From 8981badcf99b926bbe9277d0b3746d49b61f3e77 Mon Sep 17 00:00:00 2001 From: Emerson Rosen-Jones Date: Mon, 17 Nov 2025 20:09:34 -0500 Subject: [PATCH] compacting-storage.lua: bugfixes and also I forgot to implement a function --- compacting-storage.lua | 42 ++++++++++++++++++++++++++++++++---------- 1 file changed, 32 insertions(+), 10 deletions(-) diff --git a/compacting-storage.lua b/compacting-storage.lua index 93de513..ef99774 100644 --- a/compacting-storage.lua +++ b/compacting-storage.lua @@ -15,7 +15,7 @@ local ITEM_TYPES = { "iron", "copper", "zinc", "gold", "electrum" } local STACKS = 64 local NUGGETS = 1 local INGOTS = 9 -local BLOCKS = 64 +local BLOCKS = 81 -- Levels 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 if type == "nugget" then if num_crafts > 0 then + print(("Crafting %s nuggets"):format(item_type)) craft_multiple(item_type, 4, num_crafts) elseif num_crafts < 0 then + print(("Crafting %s ingots"):format(item_type)) craft_multiple(item_type, 1, num_crafts) end elseif type == "block" then if num_crafts > 0 then + print(("Crafting %s blocks"):format(item_type)) craft_multiple(item_type, 2, num_crafts) elseif num_crafts < 0 then + print(("Crafting %s ingots"):format(item_type)) craft_multiple(item_type, 3, num_crafts) end end @@ -98,11 +102,13 @@ end function set_production (item_type, produce) local target_string = target[item_type][5] if target_string == nil then return end - local relay_num, face = string.match(target_string, - "(d+):(%l+)" + local relay_num, face = string.match( + target_string, + "(%d+):(%l+)" ) local periph = string.format("redstone_relay_%d", relay_num) -- ON is disable production + if produce then print(("Producing %s"):format(item_type)) peripheral.call(periph, "setOutput", face, not produce) end @@ -123,15 +129,30 @@ end function get_dist (item_type, items) local dist = { nuggets = 0, ingots = 0, blocks = 0 } - for _, item in pairs(items) do - local type, form = item.name:match".-:(%l-)_(%l*)" + for item, count in pairs(items) do + local type, form = item:match":(%l-)_(%l*)" + form = string.format("%ss", form) if type == item_type and dist[form] ~= nil then - dist[form] = dist[form] + item.count + dist[form] = dist[form] + count end end return dist 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) return { nuggets = dist1.nuggets - dist2.nuggets, @@ -169,9 +190,10 @@ function diff_to_crafts (diff) end function print_counts (items) - for type, counts in pairs(items) do - for form, count in pairs(counts) do - print(("%s %s: %d"):format(type, form, count)) + for item, amount in pairs(items) do + local type, form = string.match(item, ":(%l+)_(%l+)") + if type ~= nil and form ~= nil then + print(("%s %s: %d"):format(type, form, amount)) end end end @@ -185,7 +207,7 @@ while true do for _, item_type in ipairs(ITEM_TYPES) do local current_dist = get_dist(item_type, items) 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 local desired_dist = decide_dist(num) local diff = get_diff(current_dist, desired_dist)