Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- BUTTON = require("buttonAPI")
- local component = require("component")
- local event = require("event")
- local keyboard = require("keyboard")
- local gpu = component.gpu
- if not component.isAvailable("br_reactor") then
- print("No reactor connected.")
- os.exit()
- end
- local reactor = component.br_reactor
- local colors = { red = 0xC14141, green = 0xDA841, grey = 0x47494C, lightGrey = 0xBBBBBB, black = 0x000000, white = 0xFFFFFF, blue = 0x4286F4 }
- local infos = {}
- local sections = {}
- local energy = { x = 102, y = 4, w = 27, h = 36 }
- reactor["stats"] = {}
- local programRunning = true
- local reactorRunning = false
- local autoTempControl = false
- local autoBufferControl = false
- local currentStored = 0
- local currentProduce = 0
- local currentCasetemp = 0
- local currentFueltemp = 0
- local currentFuelAmount = 0
- local maxCaseTemp = 0
- local maxFuelTemp = 0
- local minBuffer = 0
- local maxBuffer = 0
- function setInfos()
- infos["online"] = { x = 7, y = 29, w = 73, h = 1, title = "Reactor status: ", unit = "" }
- infos["produce"] = { x = 7, y = 31, w = 73, h = 1, title = "RF per tick: ", unit = " RF" }
- infos["stored"] = { x = 7, y = 33, w = 73, h = 1, title = "Energy Stored: ", unit = " RF" }
- infos["casetemp"] = { x = 7, y = 35, w = 73, h = 1, title = "Case temp: ", unit = " C" }
- infos["fueltemp"] = { x = 7, y = 37, w = 73, h = 1, title = "Fuel temp: ", unit = " C" }
- infos["fuelamount"] = { x = 7, y = 39, w = 73, h = 1, title = "Fuel remaining: ", unit = " mB" }
- end
- function setSections()
- sections["control"] = { x = 2, y = 2, w = 95, h = 23, title = " CONTROL " }
- sections["status"] = { x = 2, y = 27, w = 95, h = 14, title = " STATUS " }
- sections["stored"] = { x = 100, y = 2, w = 30, h = 39, title = " BUFFER " }
- end
- function setButtons()
- BUTTON.setTable("reactor", toggleReactor, 7, 5, 27, 7, true, {on = "Reactor On", off = "Reactor Off"}, {on = colors.green, off = colors.red})
- BUTTON.setTable("autoTemp", toggleAutoTemp, 7, 10, 27, 12, true, {on = "Temp Control ON", off = "Temp Control OFF"}, {on = colors.green, off = colors.red})
- BUTTON.setTable("autoBuffer", toggleAutoBuffer, 7, 15, 27, 17, true, {on = "Buffer Control ON", off = "Buffer Control OFF"}, {on = colors.green, off = colors.red})
- BUTTON.setTable("quitProgram", quitProgram, 7, 20, 27, 22, false, "Exit", colors.blue)
- BUTTON.setTable("addCaseTemp", addCaseTemp, 58, 5, 68, 7, false, "+100", colors.blue)
- BUTTON.setTable("decCaseTemp", decCaseTemp, 82, 5, 92, 7, false, "-100", colors.blue)
- BUTTON.setTable("addFuelTemp", addFuelTemp, 58, 10, 68, 12, false, "+100", colors.blue)
- BUTTON.setTable("decFuelTemp", decFuelTemp, 82, 10, 92, 12, false, "-100", colors.blue)
- BUTTON.setTable("addMaxBuffer", addMaxBuffer, 58, 15, 68, 17, false, "+10", colors.blue)
- BUTTON.setTable("decMaxBuffer", decMaxBuffer, 82, 15, 92, 17, false, "-10", colors.blue)
- BUTTON.setTable("addMinBuffer", addMinBuffer, 58, 20, 68, 22, false, "+10", colors.blue)
- BUTTON.setTable("decMinBuffer", decMinBuffer, 82, 20, 92, 22, false, "-10", colors.blue)
- end
- function printInfos(infoName)
- local info = infos[infoName]
- if infoName == "online" then
- gpu.set(info.x, info.y, info.title)
- if reactorRunning then
- gpu.setForeground(colors.green)
- gpu.set(info.x + string.len(info.title), info.y, reactor.stats[infoName] .. spaceout(" ", 10))
- else
- gpu.setForeground(colors.red)
- gpu.set(info.x + string.len(info.title), info.y, reactor.stats[infoName] .. spaceout(" ", 10))
- end
- gpu.setForeground(colors.white)
- else
- gpu.set(info.x, info.y, info.title .. reactor.stats[infoName] .. info.unit .. spaceout(" ", 10))
- end
- end
- function printBorders(sectionName)
- local section = sections[sectionName]
- gpu.setBackground(colors.grey)
- gpu.fill(section.x, section.y, section.w, 1, " ")
- gpu.fill(section.x, section.y, 1, section.h, " ")
- gpu.fill(section.x, section.y + section.h, section.w, 1, " ")
- gpu.fill(section.x + section.w, section.y, 1, section.h + 1, " ")
- gpu.setBackground(colors.black)
- gpu.set(section.x + 1, section.y, section.title)
- end
- function printEnergyBar()
- gpu.setBackground(colors.green)
- gpu.fill(energy.x, energy.y, energy.w, energy.h, " ")
- gpu.setBackground(colors.black)
- end
- function printEnergyBarOverlay(bar)
- gpu.setBackground(colors.lightGrey)
- gpu.fill(bar.x, bar.y, bar.w, bar.h, " ")
- gpu.setBackground(colors.black)
- end
- function printControlStatics()
- gpu.set(43, 6, "Max Case Temp")
- gpu.set(43, 11, "Max Fuel Temp")
- gpu.set(43, 16, " Max Buffer")
- gpu.set(43, 21, " Min Buffer")
- end
- function printControlInfos()
- gpu.set(73, 6, spaceout(maxCaseTemp, 4) .. maxCaseTemp .. " C")
- gpu.set(73, 11, spaceout(maxFuelTemp, 4) .. maxFuelTemp .. " C")
- gpu.set(72, 16, spaceout (maxBuffer, 4) .. maxBuffer .. " %")
- gpu.set(72, 21, spaceout (minBuffer, 4) .. minBuffer .. " %")
- gpu.set(58, 29, "Auto temp control: ")
- if autoTempControl then
- gpu.setForeground(colors.green)
- gpu.set(79, 29, "Enabled" .. spaceout(" ", 4))
- else
- gpu.setForeground(colors.red)
- gpu.set(79, 29, "Disabled" .. spaceout(" ", 4))
- end
- gpu.setForeground(colors.white)
- gpu.set(58, 31, "Stop at core temp: " .. maxCaseTemp .. " C" .. spaceout(" ", 4))
- gpu.set(58, 33, "Stop at fuel temp: " .. maxFuelTemp .. " C" .. spaceout(" ", 4))
- gpu.set(58, 35, "Auto buffer control: ")
- if autoBufferControl then
- gpu.setForeground(colors.green)
- gpu.set(79, 35, "Enabled" .. spaceout(" ", 4))
- else
- gpu.setForeground(colors.red)
- gpu.set(79, 35, "Disabled" .. spaceout(" ", 4))
- end
- gpu.setForeground(colors.white)
- gpu.set(58, 37, "Stop if buffer: " .. maxBuffer .. " %" .. spaceout(" ", 4))
- gpu.set(58, 39, "Start if buffer: " .. minBuffer .. " %" .. spaceout(" ", 4))
- end
- function toggleReactor(name)
- if not reactorRunning then
- startReactor()
- else
- stopReactor()
- end
- end
- function startReactor()
- BUTTON.setActive("reactor", true)
- reactor.setActive(true)
- end
- function stopReactor()
- BUTTON.setActive("reactor", false)
- reactor.setActive(false)
- end
- function toggleAutoBuffer(name)
- local status = BUTTON.getActive(name)
- BUTTON.toggleButton(name)
- if not status then
- autoBufferControl = true
- else
- autoBufferControl = false
- end
- writeToFile()
- printControlInfos()
- end
- function toggleAutoTemp(name)
- local status = BUTTON.getActive(name)
- BUTTON.toggleButton(name)
- if not status then
- autoTempControl = true
- else
- autoTempControl = false
- end
- writeToFile()
- printControlInfos()
- end
- function quitProgram()
- programRunning = false
- end
- function addCaseTemp()
- modifyCaseTemp("add")
- end
- function decCaseTemp()
- modifyCaseTemp("dec")
- end
- function addFuelTemp()
- modifyFuelTemp("add")
- end
- function decFuelTemp()
- modifyFuelTemp("dec")
- end
- function addMinBuffer()
- modifyMinBuffer("add")
- end
- function decMinBuffer()
- modifyMinBuffer("dec")
- end
- function addMaxBuffer()
- modifyMaxBuffer("add")
- end
- function decMaxBuffer()
- modifyMaxBuffer("dec")
- end
- function modifyCaseTemp(func)
- if func == "add" then
- maxCaseTemp = maxCaseTemp + 100
- if maxCaseTemp >= 2000 then
- maxCaseTemp = 2000
- end
- else
- maxCaseTemp = maxCaseTemp - 100
- if maxCaseTemp <= 0 then
- maxCaseTemp = 0
- end
- end
- writeToFile()
- printControlInfos()
- end
- function modifyFuelTemp(func)
- if func == "add" then
- maxFuelTemp = maxFuelTemp + 100
- if maxFuelTemp >= 2000 then
- maxFuelTemp = 2000
- end
- else
- maxFuelTemp = maxFuelTemp - 100
- if maxFuelTemp <= 0 then
- maxFuelTemp = 0
- end
- end
- writeToFile()
- printControlInfos()
- end
- function modifyMinBuffer(func)
- if func == "add" then
- minBuffer = minBuffer + 10
- if minBuffer >= 100 then
- minBuffer = 100
- end
- else
- minBuffer = minBuffer - 10
- if minBuffer <= 0 then
- minBuffer = 0
- end
- end
- writeToFile()
- printControlInfos()
- end
- function modifyMaxBuffer(func)
- if func == "add" then
- maxBuffer = maxBuffer + 10
- if maxBuffer >= 100 then
- maxBuffer = 100
- end
- else
- maxBuffer = maxBuffer - 10
- if maxBuffer <= 0 then
- maxBuffer = 0
- end
- end
- writeToFile()
- printControlInfos()
- end
- function isBufferFull()
- return reactor.stats["stored"] == 10000000
- end
- function isMaxBuf()
- if not autoBufferControl then
- return false
- end
- local maxBuf = maxBuffer * 100000
- return maxBuf <= reactor.stats["stored"]
- end
- function isMinBuf()
- if not autoBufferControl then
- return true
- end
- local minBuf = minBuffer * 100000
- return minBuf >= reactor.stats["stored"]
- end
- function isMaxTemp()
- if not autoTempControl then
- return false
- end
- return maxCaseTemp < reactor.stats["casetemp"] or maxFuelTemp < reactor.stats["fueltemp"]
- end
- function setActiveButtons()
- BUTTON.setActive("autoTemp", autoTempControl)
- BUTTON.setActive("autoBuffer", autoBufferControl)
- end
- function getInfoFromReactor()
- local online = ""
- if reactor.getActive() then
- online = "Running"
- else
- online = "Stopped"
- end
- reactor.stats["running"] = reactor.getActive()
- reactor.stats["online"] = online
- reactor.stats["produce"] = toint(math.ceil(reactor.getEnergyProducedLastTick()))
- reactor.stats["stored"] = toint(reactor.getEnergyStored())
- reactor.stats["casetemp"] = toint(reactor.getCasingTemperature())
- reactor.stats["fueltemp"] = toint(reactor.getFuelTemperature())
- reactor.stats["fuelamount"] = reactor.getFuelAmount()
- end
- function doCycle()
- if reactorRunning ~= reactor.stats["running"] then
- reactorRunning = reactor.stats["running"]
- printInfos("online")
- end
- if currentProduce ~= reactor.stats["produce"] then
- currentProduce = reactor.stats["produce"]
- printInfos("produce")
- end
- if currentCasetemp ~= reactor.stats["casetemp"] then
- currentCasetemp = reactor.stats["casetemp"]
- printInfos("casetemp")
- end
- if currentFueltemp ~= reactor.stats["fueltemp"] then
- currentFueltemp = reactor.stats["fueltemp"]
- printInfos("fueltemp")
- end
- if currentFuelAmount ~= reactor.stats["fuelamount"] then
- currentFuelAmount = reactor.stats["fuelamount"]
- printInfos("fuelamount")
- end
- if currentStored ~= reactor.stats["stored"] then
- currentStored = reactor.stats["stored"]
- printInfos("stored")
- local height = math.ceil(energy.h - energy.h * (currentStored/10000000))
- local bar = { x = energy.x, y = energy.y, w = energy.w, h = height }
- printEnergyBar()
- printEnergyBarOverlay(bar)
- end
- if isBufferFull() then
- stopReactor()
- end
- if autoTempControl then
- if reactorRunning then
- if isMaxTemp() then
- stopReactor()
- end
- else
- if not isMaxTemp() and isMinBuf() and not isBufferFull() then
- startReactor()
- end
- end
- end
- if autoBufferControl then
- if reactorRunning then
- if isMaxBuf() then
- stopReactor()
- end
- else
- if isMinBuf() and not isMaxTemp() and not isBufferFull() then
- startReactor()
- end
- end
- end
- end
- function toint(input)
- local str = tostring(input)
- local num, dec = str:find('%.')
- if num then
- return tonumber(str:sub(1, num - 1))
- else
- return input
- end
- end
- function spaceout(str, length)
- return string.rep(" ", length - string.len(str))
- end
- function file_exists(name)
- local file = io.open(name, "r")
- if file ~= nil then io.close(file) return false else return true end
- end
- function loadFromFile()
- if file_exists("reactor.conf") then
- writeToFile()
- else
- file = io.open("reactor.conf", "r")
- maxCaseTemp = tonumber(file:read("*l"))
- maxFuelTemp = tonumber(file:read("*l"))
- minBuffer = tonumber(file:read("*l"))
- maxBuffer = tonumber(file:read("*l"))
- autoTempControl = tostring(file:read("*l")) == "true"
- autoBufferControl = tostring(file:read("*l")) == "true"
- file:close()
- end
- end
- function writeToFile()
- file = io.open("reactor.conf","w")
- file:write(maxCaseTemp, "\n")
- file:write(maxFuelTemp, "\n")
- file:write(minBuffer, "\n")
- file:write(maxBuffer, "\n")
- file:write(tostring(autoTempControl), "\n")
- file:write(tostring(autoBufferControl), "\n")
- file:close()
- end
- function clearScreen()
- local w, h = gpu.getResolution()
- gpu.fill(1, 1, w, h, " ")
- end
- function startup()
- loadFromFile()
- getInfoFromReactor()
- BUTTON.clearTable()
- setSections()
- setButtons()
- setInfos()
- for name, data in pairs(sections) do
- printBorders(name)
- end
- for name, data in pairs(infos) do
- printInfos(name)
- end
- setActiveButtons()
- BUTTON.screen()
- printEnergyBar()
- printEnergyBarOverlay(energy)
- printControlInfos()
- printControlStatics()
- end
- gpu.setResolution(132, 42)
- clearScreen()
- startup()
- event.listen("touch", BUTTON.checkxy)
- while event.pull(0.1, "interrupted") == nil do
- getInfoFromReactor()
- doCycle()
- local event, address, _, key, _ = event.pull(1)
- if type(address) == "string" and component.isPrimary(address) then
- if event == "key_down" and key == keyboard.keys.q then
- reactor.setActive(false)
- clearScreen()
- os.exit()
- end
- end
- if not programRunning then
- reactor.setActive(false)
- clearScreen()
- os.exit()
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement