Overcomplicated skystone. Fix me, I'm full of bugs
authorOlav Bakke Svendsen <mail@olavbs.no>
Thu, 30 May 2024 12:54:40 +0000 (14:54 +0200)
committerOlav Bakke Svendsen <mail@olavbs.no>
Thu, 30 May 2024 12:54:40 +0000 (14:54 +0200)
lib/inventory.lua
skystone/stock-redstone.lua

index f36b5609441f028d54e47fcf1c77335c9164386e..bf8996caeff2d8f3e3bc321fea234834018a3fde 100644 (file)
@@ -1,3 +1,14 @@
+---------------------------------------   for now
+-- t a -> a -> bool
+-- returns whether a is found in t
+local oneOf = function(t, a)
+  for _,b in pairs(t) do
+    if b == a then return true end
+  end
+  return false
+end
+--------------------------------------
+
 local t = {}
 
 -- index: find items in inventories
@@ -92,6 +103,15 @@ t.move = function(source_address, destination_address, item_amounts)
   return not err or table.unpack({ false, "Could not move: "..err })
 end
 
-
+t.count = function(address, item_names)
+  local slots = peripheral.call(address, "list")
+  local count = 0
+  for _,item in pairs(slots) do
+    if oneOf(item_names, item.name) then
+      count = count + item.count
+    end
+  end
+  return count
+end
 
 return t
index f86d3f98754a9f4d132bf6aca46c7f0f4ff7deb8..05f25321706b8523df716b053a5d4893bbba74be 100644 (file)
@@ -1,21 +1,41 @@
 local inv = dofile("/lib/inventory.lua")
+-- local config = dofile("/skystone_config.lua")
 
-local redstone_inv = "minecraft:barrel_0"
--- local trash = "back"
+local redstone_inv = "minecraft:barrel_2"
+local skystone_inv = "functionalstorage:oak_1_1"
+
+local redstone_min = 16
+local skystone_min = 1000
+local skystone_max = 100000
+
+local drilling_condition = function(running, rs, ss)
+  if rs < redstone_min then
+    return false, "stopped"
+  end
+  if running then
+    if ss > skystone_max then return false, "standing by" end
+  else
+    if ss > skystone_min then return false, "standing by" end
+  end
+  return true, "working"
+end
 
 local signal = function()
-  rs.setOutput("left", true)
-  os.sleep(0.5)
-  rs.setOutput("left", false)
+  rs.setOutput("top", true)
+  os.sleep(1)
+  rs.setOutput("top", false)
 end
   
-local status = function(...)
-  term.clear()
-  term.setCursorPos(1,1)
-  print(...)
-end
-
-local stock = function(deployers)
+local stock_redstone = function()
+  local deployers = {}
+  do
+    local ps = peripheral.getNames()
+    for _,p in pairs(ps) do
+      if string.find(p, "create:deployer") then
+        table.insert(deployers, p)
+      end
+    end
+  end
   for _,d in pairs(deployers) do
     local deployer = peripheral.wrap(d)
     local empty = { count = 0, maxCount = 64 }
@@ -31,10 +51,10 @@ local stock = function(deployers)
     local found_all, index =
       inv.find(redstone_inv, { ["minecraft:redstone"] = item.maxCount - item.count })
     if not found_all then
-       print("Running low on redstone")
+--        print("Running low on redstone")
      end
     if not index["minecraft:redstone"] then
-      print("Out of redstone")
+--       print("Out of redstone")
     else
 --       local rsp = peripheral.wrap(redstone_inv)
       for slot, amount in pairs(index["minecraft:redstone"].slots) do
@@ -45,16 +65,127 @@ local stock = function(deployers)
   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)
+local st =
+  { drills_enabled = false
+  , drilling = false
+  , drill_status = ""
+  , redrill_timer = nil
+  , redstone_amt = 0
+  , skystone_amt = 0
+  }
+
+local status = function()
+  term.clear()
+  term.setTextColor(colors.white)
+  term.setBackgroundColor(colors.black)
+
+  term.setCursorPos(1,1)
+  term.setTextColor(colors.gray)
+  print("Drilling starts at "..tostring(skystone_min).." and stops at "..tostring(skystone_max)..".")
+  term.setTextColor(colors.white)
+
+  term.setCursorPos(1,5)
+  term.write("Skystone:")
+  term.setCursorPos(15,5)
+  term.write(st.skystone_amt)
+
+
+  term.setCursorPos(1,6)
+  term.write("Redstone:")
+  term.setCursorPos(15,6)
+  if st.redstone_amt > redstone_min then
+    term.blit("ok", "00", "ff")
+  else
+    term.blit("low", "eee", "fff")
+  end
+  term.setCursorPos(1,7)
+  term.write("Status:")
+  term.setCursorPos(15,7)
+  if st.drills_enabled then
+    term.write(st.drill_status)
+  else
+    term.blit("offline" ,"7777777", "fffffff")
+  end
+
+end
+
+local events = {}
+local pull_events = function()
+  while true do
+    local ev = { os.pullEvent() }
+    if ev[1] ~= "task_complete" then -- annoying problem, annoying fix
+      table.insert(events, ev)
+    end
+  end
+end
+
+local timer
+local loop
+loop = function()
+  if timer then os.cancelTimer(timer) end
+  st.redstone_amt = inv.count(redstone_inv, { "minecraft:redstone" })
+  st.skystone_amt = inv.count(skystone_inv, { "ae2:sky_stone_block" })
+
+  st.drilling, st.drill_status = drilling_condition(st.drilling, st.redstone_amt, st.skystone_amt)
+
+  status()
+
+  term.setCursorPos(3,3)
+  term.blit("   ", "fff", st.drills_enabled and "dd0" or "0ee")
+  term.setCursorPos(8,3)
+  term.blit(" start ", "fffffff", st.drills_enabled and "1111111" or "7777777")
+
+  term.setCursorPos(1,9)
+
+  local ev = table.remove(events, 1)
+  if ev == nil then
+    timer = os.startTimer(1)
+    coroutine.yield()
+    return loop()
+  end
+
+  if ev[1] == "mouse_click" then
+    local _, button, x, y = table.unpack(ev)
+    if y == 3 and x >= 3 and x < 3+3 then
+      os.queueEvent("toggle_drills_enabled")
+    elseif y == 3 and x >= 8 and x < 8+7 then
+      os.queueEvent("drill")
+    end
+
+  elseif ev[1] == "char" then
+    if ev[2] == "t" then -- t
+      os.queueEvent("toggle_drills_enabled")
+    elseif ev[2] == "s" then -- s
+      os.queueEvent("drill")
+    end
+
+  elseif ev[1] == "peripheral" and string.find(ev[2], "create:deployer") then
+    sleep(0.2)
+    stock_redstone()
+    os.queueEvent("redrill")
+
+  elseif ev[1] == "toggle_drills_enabled" then
+    st.drills_enabled = not st.drills_enabled
+
+  elseif ev[1] == "drill" then
+    if st.drills_enabled then signal() end
+
+  elseif ev[1] == "redrill" then
+    if st.drills_enabled then
+      if st.drilling then
+        signal()
+      else
+        st.redrill_timer = os.startTimer(10)
+      end
     end
+
+  elseif ev[1] == "timer" and ev[2] == st.redrill_timer then
+    st.redrill_timer = nil
+    os.queueEvent("redrill")
+
   end
-  stock(deployers)
+  return loop()
 end
 
+parallel.waitForAny(pull_events, loop)