Going event-based and other things
authorOlav Bakke Svendsen <mail@olavbs.no>
Thu, 12 Oct 2023 03:17:07 +0000 (05:17 +0200)
committerOlav Bakke Svendsen <mail@olavbs.no>
Thu, 12 Oct 2023 03:17:07 +0000 (05:17 +0200)
steam/temporary-coal-feeder.lua

index 02ff8441723abcc56053f8e645865cb6939cd71e..f14f663edc80791957ee000a544b3018b1ffa18c 100644 (file)
@@ -4,31 +4,34 @@ local refill_when = function(amt) return amt < 2 end
 local refill_amt  = 2
 
 local monitor = peripheral.wrap("monitor_1")
-term.redirect(monitor)
+local treefarm_monitor = peripheral.wrap("monitor_0")
+
+local coal_barrels_1 = {}
+for _,n in pairs({ "0", "1" }) do
+  local prefix = "sophisticatedstorage:limited_barrel_"
+  coal_barrels[prefix..n] = peripheral.wrap(prefix..n)
+end
+
+local tree_barrel = peripheral.wrap("sophisticatedstorage:limited_barrel_2")
 
 local deployers = {}
 local num_deployers = 0
-local barrels = {}
-local num_barrels = 0
-
 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)
     num_deployers = num_deployers + 1
-  elseif barrel then
-    barrels[barrel] = peripheral.wrap(barrel)
-    num_barrels = num_barrels + 1
   end
 end
 
-local locate_coal = function()
+local balance_coal = function()
   local stock = {}
-  for addr,barrel in pairs(barrels) do
+  local min = { "", math.huge }
+  for addr,barrel in pairs(coal_barrels) do
     local item = barrel.getItemDetail(1)
     stock[addr] = item and item.count or 0
   end
+
   return stock
 end
 
@@ -53,21 +56,43 @@ local feed = function(stock)
   end
 end
 
-while true do
-  local stock = locate_coal()
+local print_stats = function(to, stock)
   local total_coal = 0
-
   local str = ""
   for k,v in pairs(stock) do
     total_coal = total_coal + v
     str = str..tostring(v).." in "..string.match(k, "barrel_%d+$").."\n"
   end
-  term.clear()
-  term.setCursorPos(1,1)
-  print(tostring(num_barrels).." barrels and "..tostring(num_deployers).." deployers connected\n")
-  print(tostring(num_deployers/2).." charcoal/minute needed")
-  print(tostring(total_coal).." available\n"..str)
-  print("refreshes once every minute")
-  feed(stock)
-  os.sleep(60)
+  m.clear()
+  m.setCursorPos(1,1)
+  m.write(tostring(num_deployers).." deployers connected\n\n")
+  m.write(tostring(num_deployers/2).." charcoal/minute needed\n")
+  m.write(tostring(total_coal).." available\n"..str)
+  m.write("refreshes once every minute")
+end
+
+local spinner_chars = {"\\","-","/","|"}
+local spinner = function()
+  local c = table.remove(spinner_chars)
+  table.insert(spinner_chars, 1, c)
+end
+
+local main_timer = os.startTimer(0)
+local spinner_timer = os.startTimer(0)
+
+while true do
+  local e = table.pack(os.pullSignal())
+  if e[1] == "monitor_touch" and e[2] == treefarm_monitor then
+    treefarm_monitor.write(spinner())
+  elseif e[1] == "timer" and e[2] == main_timer then
+    main_timer = os.startTimer(60)
+    local stock = balance_coal()
+    print_stats(main_monitor, stock)
+    feed(stock)
+  elseif e[1] == "timer" and e[2] == spinner_timer then
+    spinner_timer(0.5)
+    local s = spinner()
+    main_monitor.setCursorPos(29,12)
+    main_monitor.write(s)
+  end
 end