fatboychummy

EnergyWatcher.lua

Mar 31st, 2019
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.15 KB | None | 0 0
  1. local rsSide = "top"
  2. local cellSide = "left"
  3. local maxPercent = 95
  4. local minPercent = 20
  5. local cell = peripheral.wrap(cellSide)
  6. if not cell then
  7.   error("No energy cell or it's side is wrong!")
  8. end
  9. local max = cell.getRFCapacity()
  10. local shutoff = math.floor(max - (max * (100 - maxPercent) * 0.01))
  11. local startup = math.floor(max * minPercent * 0.01)
  12. print(shutoff, startup)
  13. local on = true
  14. local last = 0
  15. local time = 1
  16. local olds = {}
  17.  
  18.  
  19. local function createTimeString(dif, c)
  20.   local tString = ""
  21.   local t2 = ""
  22.   local ts
  23.   if dif > 0 then
  24.     tString = tString .. "shutoff: "
  25.     ts = math.abs(math.floor((shutoff - c) / dif * time))
  26.   elseif dif < 0 then
  27.     tString = tString .. "startup: "
  28.     ts = math.abs(math.floor((c - startup) / math.abs(dif) * time))
  29.   else
  30.     tString = "Idle"
  31.   end
  32.   if dif ~= 0 then
  33.     local tm = math.floor(ts / 60)
  34.     ts = ts - (60 * tm)
  35.     local th = math.floor(tm / 60)
  36.     tm = tm - (60 * th)
  37.     tString = tString .. tostring(th) .. "h, " .. tostring(tm) .. "m, " .. tostring(ts) .. "s"
  38.   end
  39.   return tString
  40. end
  41.  
  42. local function addToOlds(n)
  43.   if #olds > 8 then
  44.     table.remove(olds, 1)
  45.   end
  46.   table.insert(olds, n)
  47. end
  48.  
  49. local function average(inp)
  50.   local ttl = 0
  51.   local sz = #inp
  52.   for i = 1, sz do
  53.     ttl = ttl + inp[i]
  54.   end
  55.   ttl = ttl / sz
  56.   return ttl
  57. end
  58.  
  59.  
  60. while true do
  61.   local c = cell.getRFStored()
  62.   local dif = c - last
  63.   last = c
  64.   addToOlds(dif)
  65.  
  66.   if c < startup then
  67.     on = true
  68.   elseif c > shutoff then
  69.     on = false
  70.   end
  71.   term.clear()
  72.   term.setCursorPos(1,1)
  73.   print(tostring(c), "/", max)
  74.   print(tostring(startup) .. " / " .. tostring(shutoff) .. " (startup/shutoff)")
  75.   print(on and "Generators running" or "Generators offline")
  76.   print()
  77.   print(dif < 0 and tostring(math.abs(dif)) .. " v RF/s" or dif == 0
  78.         and "0 - RF/s" or tostring(dif) .. " ^ RF/s")
  79.   print()
  80.   print(tostring(math.floor(dif / (time * 20))) .. " RF/t")
  81.   print()
  82.   print("Average: " .. tostring(math.floor(average(olds) / (time * 20)))
  83.         .. " RF/t")
  84.   print()
  85.   print(createTimeString(average(olds), c))
  86.   rs.setOutput(rsSide,on)
  87.   os.sleep(time)
  88. end
Add Comment
Please, Sign In to add comment