Advertisement
fatboychummy

BetterGenerators.lua

Nov 4th, 2019
415
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.20 KB | None | 0 0
  1. -- latest update: 2020 - April - 09
  2. -- Fixed right-align so it changes depending on monitor size.
  3. local cells = {}
  4. local rsInts = {}
  5. local mon = settings.get("gen.monitor")
  6. local slpRate = settings.get("gen.energyRefreshRate")
  7. local on = settings.get("gen.on")
  8. local min = settings.get("gen.minPercent")
  9. local max = settings.get("gen.maxPercent")
  10. if not min then
  11.   settings.set("gen.minPercent", 10)
  12.   min = 10
  13. end
  14. if not max then
  15.   settings.set("gen.maxPercent", 95)
  16.   max = 95
  17. end
  18. if on == nil then
  19.   settings.set("gen.on", true)
  20.   on = true
  21. end
  22. if not slpRate then
  23.   slpRate = 2
  24.   settings.set("gen.energyRefreshRate", 2)
  25. end
  26. local sides = {
  27.   "top",
  28.   "bottom",
  29.   "north",
  30.   "east",
  31.   "south",
  32.   "west"
  33. }
  34. local fdp = "thermaldynamics:duct_energy_"
  35. local fluxDuctNames = {
  36.   fdp .. "basic",
  37.   fdp .. "hardened",
  38.   fdp .. "reinforced",
  39.   fdp .. "signalum",
  40.   fdp .. "resonant"
  41. }
  42. local currentMax = 0
  43. local currentLevel = 0
  44. local lastLevel = 0
  45.  
  46. local function forEach(tblThings, func)
  47.   for i = 1, #tblThings do
  48.     func(tblThings[i])
  49.   end
  50. end
  51.  
  52. local function forEachAsync(tblThings, func, endFunc)
  53.   if #tblThings == 0 then
  54.     return -- why iterate through nothing?
  55.   end
  56.  
  57.   local funcs = {}
  58.   local tmp = {}
  59.  
  60.   -- for each item in the table, set
  61.   -- a function and add it to funcs
  62.   for i = 0, #tblThings do
  63.     funcs[i] = function() tmp[i] = func(tblThings[i]) end
  64.   end
  65.  
  66.   -- run each function in parallel
  67.   parallel.waitForAll(table.unpack(funcs))
  68.  
  69.   -- if you added the optional 3rd function...
  70.   if type(endFunc) == "function" then
  71.     for i = 1, #tmp do
  72.       -- iterate through each item and pass it into function 3
  73.       endFunc(tmp[i])
  74.     end
  75.   end
  76. end
  77. --[[
  78.   local i = 1
  79.   local funcs2 = {}
  80.   while #funcs > 0 do
  81.     funcs2[i] =
  82.   end
  83.   endFunc(tmp)
  84. end]]
  85.  
  86. -- get all the energy cells into easy table
  87. local function updateCells()
  88.   cells = {peripheral.find("thermalexpansion:storage_cell")}
  89. end
  90.  
  91. -- get all the redstone integrators into easy table
  92. local function updateInts()
  93.   rsInts = {peripheral.find("redstone_integrator")}
  94. end
  95.  
  96. local function updates()
  97.   updateCells()
  98.   updateInts()
  99.   while true do
  100.     os.pullEvent("peripheral")
  101.     updateCells() -- update cells (if any were attached/removed, don't crash)
  102.     updateInts()  -- update integrators ^^
  103.   end
  104. end
  105.  
  106. local function createTimeString() -- taken from another program, lazily adapted
  107.   local dif = currentLevel - lastLevel
  108.   local c = currentLevel
  109.   local tString = ""
  110.   local t2 = ""
  111.   local ts
  112.   local startup = (min / 100) * currentMax
  113.   local shutoff = (max / 100) * currentMax
  114.   if dif > 0 then
  115.     ts = math.abs(math.floor((shutoff - c) / dif * slpRate))
  116.   elseif dif < 0 then
  117.     ts = math.abs(math.floor((c - startup) / math.abs(dif) * slpRate))
  118.   else
  119.     tString = "Idle"
  120.   end
  121.   if dif ~= 0 then
  122.     local tm = math.floor(ts / 60)
  123.     ts = ts - (60 * tm)
  124.     local th = math.floor(tm / 60)
  125.     tm = tm - (60 * th)
  126.     tString = tString .. tostring(th) .. "h, " .. tostring(tm) .. "m, " .. tostring(ts) .. "s"
  127.   end
  128.   return tString
  129. end
  130.  
  131. local function rAlign(n, spaces)
  132.   n = tostring(n)
  133.   local len = string.len(n)
  134.   local rep = spaces - len
  135.   if rep < 0 then rep = 0 end
  136.   local out = string.rep(' ', rep)
  137.  
  138.   return out .. n
  139. end
  140.  
  141. local function watchPowerLevels()
  142.   os.sleep(2)
  143.  
  144.   while true do
  145.     local startT = os.clock()
  146.     tmpMax = 0
  147.     tmpLevel = 0 -- reset stuff
  148.  
  149.     -- for each cell, get their max and current energy levels
  150.     forEachAsync(
  151.       cells,
  152.       function (cell)
  153.         return {cap = cell.getRFCapacity(), lvl = cell.getRFStored()}
  154.       end,
  155.       function (tbl)
  156.         if tbl.cap then
  157.           tmpMax = tmpMax + tbl.cap
  158.         end
  159.         if tbl.lvl then
  160.           tmpLevel = tmpLevel + tbl.lvl
  161.         end
  162.       end
  163.     )
  164.  
  165.     lastLevel = currentLevel
  166.     currentMax = tmpMax
  167.     currentLevel = tmpLevel
  168.  
  169.     local endT = os.clock()
  170.     local slp = slpRate - (endT - startT)
  171.  
  172.     if slp < 0 then slp = 0 end
  173.     os.sleep(slp)
  174.   end
  175. end
  176.  
  177. local function redstonia()
  178.   while true do
  179.     local maxP = (max / 100) * currentMax -- resolve the actual amounts
  180.     local minP = (min / 100) * currentMax -- from the percentages
  181.     if currentLevel < minP then
  182.       on = true -- if we fall below threshold, turn gens on
  183.       settings.set("gen.on", true)
  184.     elseif currentLevel > maxP then
  185.       on = false -- if we go over threshhold, turn gens off
  186.       settings.set("gen.on", false)
  187.     end
  188.  
  189.     --  actually switch on or off
  190.     forEachAsync(
  191.       rsInts,
  192.       function (integrator)
  193.         forEach(
  194.           sides,
  195.           function (side)
  196.             integrator.setOutput(side, on)
  197.           end
  198.         )
  199.       end
  200.     )
  201.  
  202.     os.sleep(10)
  203.   end
  204. end
  205.  
  206. local function hit()
  207.   while true do
  208.     local ev, _, x, y = os.pullEvent()
  209.  
  210.     if ev == "mouse_click" then
  211.       if x >= 1 and ((on and x <= 8) or (not on and x <= 7)) then
  212.         if y == 8 then
  213.           on = not on
  214.           settings.set("gen.on", on)
  215.         end
  216.       end
  217.     end
  218.  
  219.   end
  220. end
  221.  
  222. local function info()
  223.   term.setBackgroundColor(colors.black)
  224.   term.setTextColor(colors.white)
  225.   while true do
  226.     term.clear()
  227.     term.setCursorPos(1, 1)
  228.     local mx = term.getSize()
  229.     mx = mx - 13
  230.     print("RF   Max   : " .. rAlign(tostring(currentMax), mx))
  231.     print("RF   Stored: " .. rAlign(tostring(currentLevel), mx))
  232.     print("RF   On at : " .. rAlign(tostring(math.floor((min / 100) * currentMax)), mx))
  233.     print("RF   Off at: " .. rAlign(tostring(math.floor((max / 100) * currentMax)), mx))
  234.     local calc = (currentLevel - lastLevel) / (slpRate * 20)
  235.     print("RF/t Diff  : " .. rAlign((calc > 1 or calc < -1) and tostring(math.floor(calc + 0.5)) or calc, mx))
  236.     print("Generating?: " .. rAlign((on and "true" or "false"), mx))
  237.     print("Time left  : " .. rAlign(createTimeString(), mx))
  238.     term.setBackgroundColor(
  239.       not term.isColor() and colors.gray
  240.       or on and colors.red
  241.       or colors.blue
  242.     )
  243.     io.write(on and "TURN OFF" or "TURN ON") -- at 8
  244.     term.setBackgroundColor(colors.black)
  245.     os.sleep(1)
  246.   end
  247. end
  248.  
  249. parallel.waitForAny(info, watchPowerLevels, updates, redstonia, hit)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement