Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- latest update: 2020 - April - 09
- -- Fixed right-align so it changes depending on monitor size.
- local cells = {}
- local rsInts = {}
- local mon = settings.get("gen.monitor")
- local slpRate = settings.get("gen.energyRefreshRate")
- local on = settings.get("gen.on")
- local min = settings.get("gen.minPercent")
- local max = settings.get("gen.maxPercent")
- if not min then
- settings.set("gen.minPercent", 10)
- min = 10
- end
- if not max then
- settings.set("gen.maxPercent", 95)
- max = 95
- end
- if on == nil then
- settings.set("gen.on", true)
- on = true
- end
- if not slpRate then
- slpRate = 2
- settings.set("gen.energyRefreshRate", 2)
- end
- local sides = {
- "top",
- "bottom",
- "north",
- "east",
- "south",
- "west"
- }
- local fdp = "thermaldynamics:duct_energy_"
- local fluxDuctNames = {
- fdp .. "basic",
- fdp .. "hardened",
- fdp .. "reinforced",
- fdp .. "signalum",
- fdp .. "resonant"
- }
- local currentMax = 0
- local currentLevel = 0
- local lastLevel = 0
- local function forEach(tblThings, func)
- for i = 1, #tblThings do
- func(tblThings[i])
- end
- end
- local function forEachAsync(tblThings, func, endFunc)
- if #tblThings == 0 then
- return -- why iterate through nothing?
- end
- local funcs = {}
- local tmp = {}
- -- for each item in the table, set
- -- a function and add it to funcs
- for i = 0, #tblThings do
- funcs[i] = function() tmp[i] = func(tblThings[i]) end
- end
- -- run each function in parallel
- parallel.waitForAll(table.unpack(funcs))
- -- if you added the optional 3rd function...
- if type(endFunc) == "function" then
- for i = 1, #tmp do
- -- iterate through each item and pass it into function 3
- endFunc(tmp[i])
- end
- end
- end
- --[[
- local i = 1
- local funcs2 = {}
- while #funcs > 0 do
- funcs2[i] =
- end
- endFunc(tmp)
- end]]
- -- get all the energy cells into easy table
- local function updateCells()
- cells = {peripheral.find("thermalexpansion:storage_cell")}
- end
- -- get all the redstone integrators into easy table
- local function updateInts()
- rsInts = {peripheral.find("redstone_integrator")}
- end
- local function updates()
- updateCells()
- updateInts()
- while true do
- os.pullEvent("peripheral")
- updateCells() -- update cells (if any were attached/removed, don't crash)
- updateInts() -- update integrators ^^
- end
- end
- local function createTimeString() -- taken from another program, lazily adapted
- local dif = currentLevel - lastLevel
- local c = currentLevel
- local tString = ""
- local t2 = ""
- local ts
- local startup = (min / 100) * currentMax
- local shutoff = (max / 100) * currentMax
- if dif > 0 then
- ts = math.abs(math.floor((shutoff - c) / dif * slpRate))
- elseif dif < 0 then
- ts = math.abs(math.floor((c - startup) / math.abs(dif) * slpRate))
- else
- tString = "Idle"
- end
- if dif ~= 0 then
- local tm = math.floor(ts / 60)
- ts = ts - (60 * tm)
- local th = math.floor(tm / 60)
- tm = tm - (60 * th)
- tString = tString .. tostring(th) .. "h, " .. tostring(tm) .. "m, " .. tostring(ts) .. "s"
- end
- return tString
- end
- local function rAlign(n, spaces)
- n = tostring(n)
- local len = string.len(n)
- local rep = spaces - len
- if rep < 0 then rep = 0 end
- local out = string.rep(' ', rep)
- return out .. n
- end
- local function watchPowerLevels()
- os.sleep(2)
- while true do
- local startT = os.clock()
- tmpMax = 0
- tmpLevel = 0 -- reset stuff
- -- for each cell, get their max and current energy levels
- forEachAsync(
- cells,
- function (cell)
- return {cap = cell.getRFCapacity(), lvl = cell.getRFStored()}
- end,
- function (tbl)
- if tbl.cap then
- tmpMax = tmpMax + tbl.cap
- end
- if tbl.lvl then
- tmpLevel = tmpLevel + tbl.lvl
- end
- end
- )
- lastLevel = currentLevel
- currentMax = tmpMax
- currentLevel = tmpLevel
- local endT = os.clock()
- local slp = slpRate - (endT - startT)
- if slp < 0 then slp = 0 end
- os.sleep(slp)
- end
- end
- local function redstonia()
- while true do
- local maxP = (max / 100) * currentMax -- resolve the actual amounts
- local minP = (min / 100) * currentMax -- from the percentages
- if currentLevel < minP then
- on = true -- if we fall below threshold, turn gens on
- settings.set("gen.on", true)
- elseif currentLevel > maxP then
- on = false -- if we go over threshhold, turn gens off
- settings.set("gen.on", false)
- end
- -- actually switch on or off
- forEachAsync(
- rsInts,
- function (integrator)
- forEach(
- sides,
- function (side)
- integrator.setOutput(side, on)
- end
- )
- end
- )
- os.sleep(10)
- end
- end
- local function hit()
- while true do
- local ev, _, x, y = os.pullEvent()
- if ev == "mouse_click" then
- if x >= 1 and ((on and x <= 8) or (not on and x <= 7)) then
- if y == 8 then
- on = not on
- settings.set("gen.on", on)
- end
- end
- end
- end
- end
- local function info()
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- while true do
- term.clear()
- term.setCursorPos(1, 1)
- local mx = term.getSize()
- mx = mx - 13
- print("RF Max : " .. rAlign(tostring(currentMax), mx))
- print("RF Stored: " .. rAlign(tostring(currentLevel), mx))
- print("RF On at : " .. rAlign(tostring(math.floor((min / 100) * currentMax)), mx))
- print("RF Off at: " .. rAlign(tostring(math.floor((max / 100) * currentMax)), mx))
- local calc = (currentLevel - lastLevel) / (slpRate * 20)
- print("RF/t Diff : " .. rAlign((calc > 1 or calc < -1) and tostring(math.floor(calc + 0.5)) or calc, mx))
- print("Generating?: " .. rAlign((on and "true" or "false"), mx))
- print("Time left : " .. rAlign(createTimeString(), mx))
- term.setBackgroundColor(
- not term.isColor() and colors.gray
- or on and colors.red
- or colors.blue
- )
- io.write(on and "TURN OFF" or "TURN ON") -- at 8
- term.setBackgroundColor(colors.black)
- os.sleep(1)
- end
- end
- parallel.waitForAny(info, watchPowerLevels, updates, redstonia, hit)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement