From: Olav Bakke Svendsen Date: Mon, 27 May 2024 05:16:40 +0000 (+0200) Subject: Skystone X-Git-Tag: skystone~3 X-Git-Url: http://git.olavbs.no/?a=commitdiff_plain;h=a2e06b6463431d32d006cfe5744cd4bcb709a279;p=cc.git Skystone --- diff --git a/lib/inventory.lua b/lib/inventory.lua index c047b76..f36b560 100644 --- a/lib/inventory.lua +++ b/lib/inventory.lua @@ -44,16 +44,12 @@ t.find = function(inventory_address, item_amounts) 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 @@ -66,6 +62,10 @@ t.find = function(inventory_address, item_amounts) missing[item.name] = nil end end + if next(missing) == nil then + found_all = true + break + end end return found_all, index end diff --git a/skystone/README b/skystone/README new file mode 100644 index 0000000..8b0f790 --- /dev/null +++ b/skystone/README @@ -0,0 +1,11 @@ +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. +]] diff --git a/skystone/package b/skystone/package new file mode 100644 index 0000000..9d39283 --- /dev/null +++ b/skystone/package @@ -0,0 +1,2 @@ +cc skystone/stock-redstone.lua:/bin/stock-redstone.lua:o +cc lib/inventory.lua:/lib/inventory.lua:o diff --git a/skystone/stock-redstone.lua b/skystone/stock-redstone.lua new file mode 100644 index 0000000..f86d3f9 --- /dev/null +++ b/skystone/stock-redstone.lua @@ -0,0 +1,60 @@ +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 +