Added temporary coal feeder
authorOlav Bakke Svendsen <mail@olavbs.no>
Wed, 11 Oct 2023 21:16:32 +0000 (23:16 +0200)
committerOlav Bakke Svendsen <mail@olavbs.no>
Wed, 11 Oct 2023 21:16:32 +0000 (23:16 +0200)
steam/temporary-coal-feeder.lua [new file with mode: 0644]

diff --git a/steam/temporary-coal-feeder.lua b/steam/temporary-coal-feeder.lua
new file mode 100644 (file)
index 0000000..ba1b7bf
--- /dev/null
@@ -0,0 +1,53 @@
+-- very temporary coal feeder.
+
+-- local config = dofile=("temporary-coal-feeder.config")
+local deployers = {}
+local barrels = {}
+
+local refill_when = function(amt) return amt < 2 end
+local refill_amt  = 2
+
+-- for peripherals, mactch deployers and barrels
+
+for _,p in pairs(peripheral.getNames()) do
+  local deployer = string.match(p, "^create\:deployer_%d+$")
+  local barrel = string.match(p, "^sophisticatedstorage\:limited_barrel_%d+$")
+  if deployer then
+    deployers[deployer] = peripheral.wrap(deployer)
+  else if barrel then
+    barrels[barrel] = peripheral.wrap(barrel)
+  end
+end
+
+local locate_coal = function()
+  local t = {}
+  for addr,barrel in pairs(barrels) do
+    local item = barrel.getItemDetail(1)
+    t[addr] = item.count()
+  end
+end
+
+local feed = function(stock)
+  local stock = stock
+  for deployer_addr,deployer in pairs(deployers) do
+    if refill_when(d.getItemDetail(1).count) then
+      local f = function() return false, "No coal available" end
+      for barrel_addr,count in pairs(stock) do
+        if count >= refill_amt then
+          stock[barrel_addr] = count - refill_amt
+          f = function() deployer.pullItems(barrel_addr, 1, refill_amt, 1) end
+          break
+        end
+      end
+      local ok, e = f()
+      if not ok then
+        print("Could not feed "..deployer_addr.."\n"..e)
+      end
+    end
+  end
+end
+
+while true do
+  feed(locate_coal())
+  os.sleep(60)
+end