-- 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
-- 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)
-- 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