Rebuilding blasters
authorOlav Bakke Svendsen <mail@olavbs.no>
Wed, 18 Oct 2023 01:07:25 +0000 (03:07 +0200)
committerOlav Bakke Svendsen <mail@olavbs.no>
Wed, 18 Oct 2023 01:07:25 +0000 (03:07 +0200)
steam/temporary-coal-feeder.lua

index 677328110d4c8504cfca0d020ee25f4838cdbf93..d128340b9d5638f8dd448a11722da1afd4299b02 100644 (file)
@@ -5,21 +5,20 @@ local refill_amt  = 2
 
 local start_wood_when = function(amt) return amt < 1000 end
 local stop_wood_when = function(amt) return amt > 40000 end
-local rs_wood_side = "back"
-local start_coal_when = function(amt) return amt < 500  end
+local rs_wood_side = "top"
+local start_coal_when = function(amt) return amt < 500 end
 local stop_coal_when = function(amt) return amt > 8000 end
-local rs_coal_side = "left"
+local rs_coal_side = "back"
 
-local main_monitor_name = "monitor_3"
-local main_monitor = peripheral.wrap(main_monitor_name)
+local wood_to_keep_in_blaster = 10000
 
-local coal_barrels = {}
-for _,n in pairs({ "1", "0" }) do
-  local prefix = "sophisticatedstorage:limited_barrel_"
-  coal_barrels[prefix..n] = peripheral.wrap(prefix..n)
-end
+-- peripherals
+
+local main_monitor = peripheral.wrap("monitor_0")
 
-local tree_barrel = peripheral.wrap("sophisticatedstorage:limited_barrel_2")
+local coal_drawer = peripheral.wrap("storagedrawers:standard_drawers_1_1")
+local wood_drawer = peripheral.wrap("storagedrawers:standard_drawers_1_0")
+local farm_barrel = peripheral.wrap("sophisticatedstorage:limited_barrel_0")
 
 local deployers = {}
 local num_deployers = 0
@@ -31,74 +30,74 @@ for _,p in pairs(peripheral.getNames()) do
   end
 end
 
+-- program
+
 local take_stock = function()
-  local coal_stock = {}
-  for addr, barrel in pairs(coal_barrels) do
-    local item = barrel.getItemDetail(1)
-    local amt = item and item.count or 0
-    coal_stock[addr] = amt
-  end
-  local wood_stock = 0
-  for slot, item in pairs(tree_barrel.list()) do
+  local coal_item_detail = coal_drawer.getItemDetail(1)
+  local coal_amount = coal_item_detail and coal_item_detail.count or 0
+
+  local wood_item_detail = wood_drawer.getItemDetail(1)
+  local wood_amount = wood_item_detail and wood_item_detail.count or 0
+
+  local farm_amount = 0
+  local farm_slot = false
+  for slot, item in pairs(farm_barrel.list()) do
     if item.name and item.name == "minecraft:spruce_log" then
-      wood_stock = item.count
+      farm_amount = farm_amount + item.count
+      farm_slot = slot
       break
     end
   end
-  return coal_stock, wood_stock
+
+  return coal_amount, wood_amount, farm_amount, farm_slot
 end
 
-local fullest_coal_barrel = function(coal_stock)
-  local r = { addr = false, count = 0 }
-  for barrel_addr, count in pairs(coal_stock) do
-    if count >= r.count then
-      r = { addr = barrel_addr, count = count }
-    end
+local move_wood = function(wood_amount, farm_amount, farm_slot)
+  local wood_amount, farm_amount = wood_amount, farm_amount
+  local to_move = wood_to_keep_in_blaster - wood_amount
+  if to_move > 0 and to_move <= farm_amount then
+    local moved = farm_barrel.pushItems(peripheral.getName(wood_drawer), farm_slot, to_move)
   end
-  return r.addr, r.count
+  if moved then
+    wood_amount = wood_amount + moved
+    farm_amount = farm_amount - moved
+  end
+  return wood_amount, farm_amount
 end
 
-local feed = function(coal_stock)
-  local coal_stock = coal_stock
+local feed = function(coal_amount)
+  local coal_amount = coal_amount
   for deployer_addr,deployer in pairs(deployers) do
     local deployer_item = deployer.getItemDetail(1)
     if refill_when(deployer_item and deployer_item.count or 0) then
       local f = function() return false, "No coal available" end
-      local barrel_addr, count = fullest_coal_barrel(coal_stock)
---       for barrel_addr,count in pairs(stock) do
-      if count >= refill_amt then
-        coal_stock[barrel_addr] = count - refill_amt
-        f = function() return deployer.pullItems(barrel_addr, 1, refill_amt, 1) end
---           break
+      if coal_amount >= refill_amt then
+        coal_amount = coal_amount - refill_amt
+        f = function() return deployer.pullItems(peripheral.getName(coal_drawer), 1, refill_amt, 1) end
       end
---       end
       local ok, e = f()
       if not ok then
         print("Could not feed "..deployer_addr.."\n"..e)
       end
     end
   end
+  return coal_amount
 end
 
-local manage_farms = function(coal_stock, wood_stock)
+local manage_farms = function(coal_amount, wood_amount, farm_amount)
   local coal_online = not rs.getOutput(rs_coal_side)
   local wood_online = not rs.getOutput(rs_wood_side)
 
-  local total_coal = 0
-  for _, v in pairs(coal_stock) do
-    total_coal = total_coal + v
-  end
-
   if coal_online then
-    if stop_coal_when(total_coal) then rs.setOutput(rs_coal_side, true) end
+    if stop_coal_when(coal_amount) then rs.setOutput(rs_coal_side, true) end
   else
-    if start_coal_when(total_coal) then rs.setOutput(rs_coal_side, false) end
+    if start_coal_when(coal_amount) then rs.setOutput(rs_coal_side, false) end
   end
 
   if wood_online then
-    if stop_wood_when(wood_stock) then rs.setOutput(rs_wood_side, true) end
+    if stop_wood_when(wood_amount + farm_amount) then rs.setOutput(rs_wood_side, true) end
   else
-    if start_wood_when(wood_stock) then rs.setOutput(rs_wood_side, false) end
+    if start_wood_when(wood_amount + farm_amount) then rs.setOutput(rs_wood_side, false) end
   end
 
   coal_online = not rs.getOutput(rs_coal_side)
@@ -109,13 +108,7 @@ end
 local coal_at_boot = false
 local minutes_passed = -1
 
-local print_stats = function(monitor, coal_stock, wood_stock, coal_online, wood_online)
-  local total_coal = 0
-  local str = ""
-  for k,v in pairs(coal_stock) do
-    total_coal = total_coal + v
-    str = str.." + "..tostring(v)
-  end
+local print_stats = function(monitor, coal_amount, wood_amount, farm_amount, coal_online, wood_online)
   monitor.clear()
   monitor.setTextColor(colors.white)
   monitor.setCursorPos(1,1)
@@ -123,10 +116,9 @@ local print_stats = function(monitor, coal_stock, wood_stock, coal_online, wood_
   monitor.setCursorPos(1,2)
   monitor.write(tostring(num_deployers/2).." charcoal/minute needed")
   monitor.setCursorPos(1,3)
-  monitor.write(tostring(total_coal).." available")
+  monitor.write(tostring(coal_amount).." charcoal available")
   monitor.setCursorPos(1,4)
-  monitor.setTextColor(colors.gray)
-  monitor.write(string.gsub(str, "^%s++%s*", ""))
+  monitor.write(tostring(wood_amount + farm_amount).." wood available")
 
   monitor.setCursorPos(1,6)
   monitor.setTextColor(colors.white)
@@ -143,16 +135,6 @@ local print_stats = function(monitor, coal_stock, wood_stock, coal_online, wood_
   monitor.setCursorPos(1,12)
   monitor.setTextColor(colors.gray)
   monitor.write("refreshes once every minute")
-  
-  -- this doesn't work for some reason..
---   monitor.setTextColor(colors.white)
---   coal_at_boot = coal_at_boot or total_coal
---   minutes_passed = minutes_passed + 1
---   if minutes_passed < 0 then
---     local avg = (coal_at_boot - total_coal)/minutes_passed
---     monitor.setCursorPos(1,6)
---     monitor.write("losing on average "..tostring(avg).."cc/m")
---   end
 end
 
 local spinner_chars = {"\\","-","/","|"}
@@ -161,11 +143,12 @@ local spin = function()
   table.insert(spinner_chars, 1, c)
   return c
 end
+
 local print_spinner = function(monitor, x, y, char)
   local s = spin()
-  main_monitor.setTextColor(colors.white)
-  main_monitor.setCursorPos(29,12)
-  main_monitor.write(char or s)
+  monitor.setTextColor(colors.white)
+  monitor.setCursorPos(29,12)
+  monitor.write(char or s)
 end
 
 local main_timer = os.startTimer(0)
@@ -173,7 +156,7 @@ local spinner_timer = os.startTimer(0)
 
 while true do
   local e = table.pack(os.pullEvent())
-  if e[1] == "monitor_touch" and e[2] == main_monitor_name then
+  if e[1] == "monitor_touch" and e[2] == peripheral.getName(main_monitor) then
     main_monitor.clear()
     main_monitor.setCursorPos(1,1)
     main_monitor.write("rebooting...")
@@ -182,11 +165,12 @@ while true do
     main_timer = os.startTimer(60)
     os.cancelTimer(spinner_timer)
     print_spinner(main_monitor, 29,12, "?")
-    local coal_stock, wood_stock = take_stock()
-    local coal_online, wood_online = manage_farms(coal_stock, wood_stock)
-    print_stats(main_monitor, coal_stock, wood_stock, coal_online, wood_online)
+    local coal_amount, wood_amount, farm_amount, farm_slot = take_stock()
+    wood_amount, farm_amount, farm_slot = move_wood(wood_amount, farm_amount, farm_slot)
+    local coal_online, wood_online = manage_farms(coal_amount, wood_amount, farm_amount)
     print_spinner(main_monitor, 29,12, "*")
-    feed(coal_stock)
+    coal_amount = feed(coal_amount)
+    print_stats(main_monitor, coal_amount, wood_amount, farm_amount, coal_online, wood_online)
     spinner_timer = os.startTimer(0)
   elseif e[1] == "timer" and e[2] == spinner_timer then
     spinner_timer = os.startTimer(0.5)