local missing = {}
for name, amount in pairs(item_amounts) do
- missing[name].amount = amount
+ missing[name] = amount
end
local index = {}
local found_all = false
for slot, item in pairs(slots) do
- if missing == {} then
- found_all = true
- break
- end
if item.name and missing[item.name] then
index[item.name] = index[item.name] or { amount = 0, slots = {} }
if item.count < missing[item.name] then
missing[item.name] = nil
end
end
+ if next(missing) == nil then
+ found_all = true
+ break
+ end
end
return found_all, index
end
--- /dev/null
+print [[
+stock-redstone.lua is responsible for stocking the
+deployers and switching the carriage's direction
+after a restock.
+
+It listens for `peripheral` events that signals a
+deployer has conected. It waits for four deployers
+to be connected then fills them each with as much
+redstone as necessary before activating a redstone
+pulse, signaling that the carriage may continue.
+]]
--- /dev/null
+local inv = dofile("/lib/inventory.lua")
+
+local redstone_inv = "minecraft:barrel_0"
+-- local trash = "back"
+
+local signal = function()
+ rs.setOutput("left", true)
+ os.sleep(0.5)
+ rs.setOutput("left", false)
+end
+
+local status = function(...)
+ term.clear()
+ term.setCursorPos(1,1)
+ print(...)
+end
+
+local stock = function(deployers)
+ for _,d in pairs(deployers) do
+ local deployer = peripheral.wrap(d)
+ local empty = { count = 0, maxCount = 64 }
+ local item = deployer.getItemDetail(1) or empty
+ if item.name ~= "minecraft:redstone" then
+ -- Deployer is either holding the wrong item or it's holding nothing.
+ -- In this case, empty the deployers hand into the trash.
+ -- this is probably unecessarily cautious
+ --peripheral.call(trash, "pullItems", d, 1)
+-- peripheral.call(d, "pushItems", trash, 1)
+ item = empty
+ end
+ local found_all, index =
+ inv.find(redstone_inv, { ["minecraft:redstone"] = item.maxCount - item.count })
+ if not found_all then
+ print("Running low on redstone")
+ end
+ if not index["minecraft:redstone"] then
+ print("Out of redstone")
+ else
+-- local rsp = peripheral.wrap(redstone_inv)
+ for slot, amount in pairs(index["minecraft:redstone"].slots) do
+-- rsp.pushItems(d, slot, amount)
+ peripheral.call(redstone_inv, "pushItems", d, slot, amount)
+ end
+ end
+ end
+end
+
+while true do
+ signal()
+ os.sleep(1)
+ local deployers = {}
+ while #deployers < 4 do
+ local _, p = os.pullEvent("peripheral")
+ if string.find(p, "create:deployer") then
+ table.insert(deployers, p)
+ end
+ end
+ stock(deployers)
+end
+