From: Olav Bakke Svendsen Date: Fri, 27 Oct 2023 06:30:47 +0000 (+0200) Subject: Fixes and adding debug messages X-Git-Tag: skystone~27 X-Git-Url: http://git.olavbs.no/?a=commitdiff_plain;h=1b070cc230e1cb5706b9792780e096dcaa961fe1;p=cc.git Fixes and adding debug messages --- diff --git a/cobble-loader.lua b/cobble-loader.lua index 89e35a8..33d4f09 100644 --- a/cobble-loader.lua +++ b/cobble-loader.lua @@ -10,34 +10,48 @@ local sleep_time_working = 5 -- the amount of time (in seconds) between attempted refills when full. local sleep_time_idle = 5 +-- whether or not to print debug messages +local print_debug = false + -------------------------------------------------------------------------------- +local args = {...} + +local debug = function(...) if print_debug or args[1] == "debug" then print(...) end end + +local shorten = function(address) return string.match(address, "%d$") end + local outputs = {} local inputs = {} -for _,address = in pairs(peripheral.getNames()) do +for _, address in pairs(peripheral.getNames()) do local is_drawer = string.match(address, "standard_drawers") local is_barrel = string.match(address, "limited_barrel") if is_drawer then + debug("Found output drawer:", shorten(address), address) table.insert(outputs, address) elseif is_barrel then + debug("Found input barrel:", shorten(address), address) table.insert(inputs, address) end end +debug(tostring(#inputs).." inputs and "..tostring(#outputs).." outputs connected") + -- returns ordered list of outputs and how much cobble they have space for. -- ordered by free space in decreasing order. -- outputs with no empty space are not included in return table. -- calculates free space on the assumtion that outputs are drawers without void. local output_amounts = function() local t = {} - for _,output in ipairs(outputs) do + for _, output in ipairs(outputs) do local p = peripheral.wrap(output) local item = p.getItemDetail(2) local count = item and item.name == "minecraft:cobblestone" and item.count or 0 local free_space = p.getItemLimit(2) - count + debug("Output "..shorten(output).." needs "..free_space.." cobble") if free_space > 0 then table.insert(t, { address = output, free_space = free_space }) end @@ -51,12 +65,13 @@ end -- assumes inputs are limited barrels. local input_amounts = function() local t = {} - for _,input in ipairs(inputs) do + for _, input in ipairs(inputs) do local p = peripheral.wrap(input) local item = p.getItemDetail(1) local count = item and item.name == "minecraft:cobblestone" - and item.count + and item.count or 0 + debug("Input "..shorten(input).." has"..count.." cobble") table.insert(t, { address = input, count = count }) end table.sort(t, function(a, b) return a.count > b.count end) @@ -66,35 +81,50 @@ end -- moves as much cobble as possible to each output, returning whether or not -- there is space for more local move_cobble = function() + debug("Moving cobble...") local to_move_total = 0 - for _,output in ipairs(output_amounts()) do + for _, output in ipairs(output_amounts()) do local p = peripheral.wrap(output.address) local to_move = output.free_space to_move_total = to_move_total + output.free_space - for _,input in ipairs(input_amounts()) do - if to_move <= 0 then break end - local moved = p.pullItems(input, 1, math.min(input.count, to_move), 2) + for _, input in ipairs(input_amounts()) do + if to_move <= 0 then + debug("Output "..shorten(output).." full") + break + end + local moved = p.pullItems(input.address, 1, math.min(input.count, to_move), 2) -- moved may not be integer if pullItems failed.. look out to_move = to_move - moved to_move_total = to_move_total - moved + debug("Moved "..tostring(moved).." cobble from input "..shorten(input).." to output "..shorten(output)) + if to_move > 0 then debug("Output "..shorten(output).." still needs "..tostring(to_move).." cobble") end end end + debug("Done moving cobble") + if to_move_total > 0 then + debug(tostring(to_move_total).." cobble until all outputs are full") + else + debug("All outputs are full") + end return to_move_total > 0 end -- set redstone output on all sides simultaneously local generate_cobble = function(bool) - for _,side in ipairs({"top","bottom","left","right","front","back"}) do + for _,side in ipairs({"top", "bottom", "left", "right", "front", "back"}) do rs.setOutput(side, bool) + debug("Cobble generator "..(bool and "online" or "offline")) end end -- main loop while true do + debug("Refilling outputs...") while move_cobble() do generate_cobble(true) os.sleep(sleep_time_working) end + debug("All outputs are full") generate_cobble(false) os.sleep(sleep_time_idle) end