Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- API = require("buttonAPI")
- local filesystem = require("filesystem")
- local component = require("component")
- local keyboard = require("keyboard")
- local event = require("event")
- local gpu = component.gpu
- local reactor = component.br_reactor
- local DEBUG = false
- local debugList = {}
- local debugVars = {}
- local colors = { blue = 0x4286F4, purple = 0xB673d6, red = 0xC14141, green = 0xDA841,
- black = 0x000000, white = 0xFFFFFF, grey = 0x47494C, lightGrey = 0xBBBBBB, warnorange = 0xff6700, warnyellow = 0xffae42 }
- -- set size of the screen for lvl 3
- gpu.setResolution(160,38)
- gpu.setBackground(colors.black)
- gpu.fill(1, 1, 160, 38, " ")
- local sections = {}
- local graphs = {}
- local infos = {}
- -- defninitions
- reactor["stats"] = {}
- local running = true
- local maxRF = 0
- local reactorRodsLevel = {}
- local currentRodLevel = 0
- local currentRf = 0
- local currentRfTick = 0
- local currenFuel = 0
- local active = "false"
- local fuelreactivity = 0
- local totalfuel = 0
- local totalwaste = 0
- local totalfuelandwaste = 0
- local casingtemperature = 0
- local fueltemperature = 0
- local minPowerRod = 0
- local maxPowerRod = 100
- -- functions
- function toint(n)
- local s = tostring(n)
- local i, j = s:find('%.')
- if i then
- return tonumber(s:sub(1, i-1))
- else
- return n
- end
- end
- function setSections()
- sections["graph"] = { x = 2, y = 3, width = 78, height= 33, title = " INFOS "}
- sections["controls"] = { x = 82, y = 3, width = 40, height = 33, title = " CONTROLS "}
- sections["info"] = { x = 124, y = 3, width = 35, height= 33, title = " STATISTICS "}
- sections["info2"] = { x = 127, y = 15, width = 29, height= 10, title = " FUEL/WASTE "}
- sections["info3"] = { x = 127, y = 27, width = 29, height= 6, title = " TEMPERATURES "}
- end
- function setGraphs()
- graphs["tick"] = { x = 6, y = 6, width = 73, height= 8, title = "ENERGY LAST TICK"}
- graphs["stored"] = { x = 6, y = 16, width = 73, height = 8, title = "ENERGY STORED"}
- graphs["rods"] = { x = 6, y = 26, width = 73, height= 8, title = "CONTROL RODS INSERTION"}
- end
- function setInfos()
- infos["active"] = { x = 127, y = 5, width = 73, height= 1, title = "STATUS : ", unit = " "}
- infos["tick"] = { x = 127, y = 7, width = 73, height= 1, title = "RF PER TICK : ", unit = " RF"}
- infos["stored"] = { x = 127, y = 9, width = 73, height = 1, title = "ENERGY STORED : ", unit = " RF"}
- infos["rods"] = { x = 127, y = 11, width = 73, height= 1, title = "CONTROL ROD INSERTION : ", unit = " %"}
- infos["activity"] = { x = 127, y = 13, width = 73, height= 1, title = "CORE REACTIVITY : ", unit = " %"}
- infos["fuel"] = { x = 130, y = 17, width = 73, height= 1, title = "FUEL USAGE : ", unit = " Mb/t"}
- infos["totalfuel"] = { x = 130, y = 19, width = 73, height= 1, title = "TOTAL FUEL : ", unit = " Mb"}
- infos["totalwaste"] = { x = 130, y = 21, width = 73, height= 1, title = "TOTAL WASTE : ", unit = " Mb"}
- infos["total"] = { x = 130, y = 23, width = 73, height= 1, title = "MAX : ", unit = " Mb"}
- infos["casingtemp"] = { x = 130, y = 29, width = 73, height= 1, title = "CASING : ", unit = " C°"}
- infos["fueltemp"] = { x = 130, y = 31, width = 73, height= 1, title = "FUEL : ", unit = " C°"}
- end
- function debugInfos()
- debug["print"] = { x = 1, y = 38, width = 73, height= 1, title = "DBG : "}
- end
- function setButtons()
- API.setTable("ON", powerOn, 86, 5, 99, 7,"ON", {on = colors.green, off = colors.green})
- API.setTable("AZ-5", powerOff, 105, 5, 118, 7,"AZ-5", {on = colors.red, off = colors.red})
- API.setTable("lowerMinLimit", lowerMinLimit, 86, 15, 99, 17,"-10", {on = colors.warnyellow, off = colors.warnyellow})
- API.setTable("lowerMaxLimit", lowerMaxLimit, 105, 15, 118, 17,"-10", {on = colors.warnyellow, off = colors.warnyellow})
- API.setTable("augmentMinLimit", augmentMinLimit, 86, 19, 99, 21,"+10", {on = colors.warnyellow, off = colors.warnyellow})
- API.setTable("augmentMaxLimit", augmentMaxLimit, 105, 19, 118, 21,"+10", {on = colors.warnyellow, off = colors.warnyellow})
- API.setTable("EJECT WASTE", ejectWaste, 86, 23, 118, 25,"EJECT WASTE", {on = colors.warnorange, off = colors.warnorange})
- end
- function printBorders(sectionName)
- local s = sections[sectionName]
- -- set border
- gpu.setBackground(colors.grey)
- gpu.fill(s.x, s.y, s.width, 1, " ")
- gpu.fill(s.x, s.y, 1, s.height, " ")
- gpu.fill(s.x, s.y + s.height, s.width, 1, " ")
- gpu.fill(s.x + s.width, s.y, 1, s.height + 1, " ")
- -- set title
- gpu.setBackground(colors.black)
- gpu.set(s.x + 2, s.y, s.title)
- end
- function printGraphs(graphName)
- local g = graphs[graphName]
- -- set graph
- gpu.setBackground(colors.lightGrey)
- gpu.fill(g.x, g.y, g.width, g.height, " ")
- -- set title
- gpu.setBackground(colors.black)
- gpu.set(g.x, g.y - 1, g.title)
- end
- function printActiveGraphs(activeGraph)
- local g = activeGraph
- -- set graph
- gpu.setBackground(colors.green)
- gpu.fill(g.x, g.y, g.width, g.height, " ")
- gpu.setBackground(colors.black)
- end
- function printActiveGraphStore(graphObj)
- local g = graphObj
- if g.stored > 7500000 then
- gpu.setBackground(colors.green)
- gpu.fill(g.x, g.y, g.width, g.height, " ")
- gpu.setBackground(colors.black)
- elseif g.stored > 5000000 then
- gpu.setBackground(colors.warnyellow)
- gpu.fill(g.x, g.y, g.width, g.height, " ")
- gpu.setBackground(colors.black)
- elseif g.stored > 2500000 then
- gpu.setBackground(colors.warnorange)
- gpu.fill(g.x, g.y, g.width, g.height, " ")
- gpu.setBackground(colors.black)
- else
- gpu.setBackground(colors.red)
- gpu.fill(g.x, g.y, g.width, g.height, " ")
- gpu.setBackground(colors.black)
- end
- end
- function printStaticControlText()
- gpu.setForeground(colors.white)
- gpu.set(89,12, "MIN")
- gpu.setForeground(colors.white)
- gpu.set(108,12, "MAX")
- gpu.setForeground(colors.white)
- gpu.set(97,10, "AUTO-CONTROL")
- gpu.set(101.5,13, "--")
- end
- function printControlInfos()
- gpu.setForeground(colors.white)
- gpu.set(94,12, minPowerRod .. "% ")
- gpu.setForeground(colors.white)
- gpu.set(113,12, maxPowerRod .. "% ")
- gpu.setForeground(colors.white)
- end
- function printInfos(infoName)
- local maxLength = 15
- local i = infos[infoName]
- local spaces = string.rep(" ", maxLength - string.len(reactor.stats[infoName] .. i.unit))
- --gpu.set(i.x, i.y , i.title .. reactor.stats[infoName] .. i.unit .. spaces)
- gpu.set(i.x, i.y , i.title .. reactor.stats[infoName] .. i.unit)
- end
- function printInfos2()
- local i = infos["active"]
- if reactor.stats["active"] == "false" then
- gpu.set(i.x, i.y , i.title .. "Offline")
- else
- gpu.set(i.x, i.y , i.title .. "Online")
- end
- end
- function getInfoFromReactor()
- local reactorEnergyStats = reactor.getEnergyStats()
- local reactorFuelStats = reactor.getFuelStats()
- local reactorStatus = reactor.getActive()
- local reactorReactivity = reactor.getFuelReactivity()
- local reactorTotalFuel = reactor.getFuelAmount()
- local reactorTotalWaste = reactor.getWasteAmount()
- local reactorTotalWasteAndFuel = reactor.getFuelAmountMax()
- local reactorCasingTemperature = reactor.getCasingTemperature()
- local reactorFuelTemperature = reactor.getFuelTemperature()
- reactorRodsLevel = reactor.getControlRodsLevels()
- currentRf = reactor.stats["stored"]
- reactor.stats["active"] = tostring(reactorStatus)
- reactor.stats["tick"] = toint(math.ceil(reactorEnergyStats["energyProducedLastTick"]))
- reactor.stats["stored"] = toint(reactorEnergyStats["energyStored"])
- reactor.stats["rods"] = toint(reactorRodsLevel[0])
- reactor.stats["fuel"] = round(reactorFuelStats["fuelConsumedLastTick"], 2)
- reactor.stats["activity"] = toint(math.ceil(reactorReactivity))
- reactor.stats["totalfuel"] = reactorTotalFuel
- reactor.stats["totalwaste"] = reactorTotalWaste
- reactor.stats["total"] = reactorTotalWasteAndFuel
- reactor.stats["casingtemp"] = toint(math.ceil(reactorCasingTemperature))
- reactor.stats["fueltemp"] = toint(math.ceil(reactorFuelTemperature))
- end
- function augmentMinLimit()
- modifyRods("min", 10)
- end
- function lowerMinLimit()
- modifyRods("min", -10)
- end
- function augmentMaxLimit()
- modifyRods("max", 10)
- end
- function lowerMaxLimit()
- modifyRods("max", -10)
- end
- function powerOn()
- reactor.setActive(true)
- end
- function powerOff()
- reactor.setActive(false)
- end
- function ejectWaste()
- reactor.doEjectWaste()
- end
- function modifyRods(limit, number)
- local tempLevel = 0
- if limit == "min" then
- tempLevel = minPowerRod + number
- if tempLevel <= 0 then
- minPowerRod = 0
- end
- if tempLevel >= maxPowerRod then
- minPowerRod = maxPowerRod -10
- end
- if tempLevel < maxPowerRod and tempLevel > 0 then
- minPowerRod = tempLevel
- end
- else
- tempLevel = maxPowerRod + number
- if tempLevel <= minPowerRod then
- maxPowerRod = minPowerRod +10
- end
- if tempLevel >= 100 then
- maxPowerRod = 100
- end
- if tempLevel > minPowerRod and tempLevel < 100 then
- maxPowerRod = tempLevel
- end
- end
- setInfoToFile()
- calculateAdjustRodsLevel()
- end
- -- Calculate and adjusts the level of the rods
- function calculateAdjustRodsLevel()
- local rfTotalMax = 10000000
- currentRf = reactor.stats["stored"]
- differenceMinMax = 100
- local maxPower = (rfTotalMax/100) * 100
- local minPower = (rfTotalMax/100) * 0
- if currentRf >= maxPower then
- currentRf = maxPower
- end
- if currentRf <= minPower then
- currentRf = minPower
- end
- currentRf = toint(currentRf - (rfTotalMax/100) * minPowerRod)
- local rfInBetween = (rfTotalMax/100) * differenceMinMax
- local rodLevel = toint(math.ceil((currentRf/rfInBetween)*100))
- AdjustRodsLevel(rodLevel)
- end
- function AdjustRodsLevel(rodLevel)
- for key,value in pairs(reactorRodsLevel) do
- reactor.setControlRodLevel(key, rodLevel)
- end
- end
- function printDebug()
- local maxLength = 132
- local i = debug["print"]
- local rodsvalues = ""
- rodsvalues = "[0]" .. reactorRodsLevel[0] .. "[1]" .. reactorRodsLevel[1] .. "[2]" .. reactorRodsLevel[2] .. "[Z]" .. reactor.stats["rods"]
- local debugInformations = "maxRF:" .. maxRF .. ", RodsLev:" .. rodsvalues .. ", curRodLev:" .. currentRodLevel .. ", curRf:" .. currentRf .. ", curRfT:" .. currentRfTick .. ", min-max:" .. minPowerRod .. "-" .. maxPowerRod
- local spaces = string.rep(" ", maxLength - string.len(debugInformations))
- gpu.set(i.x, i.y , i.title .. debugInformations .. spaces)
- end
- function draw()
- if maxRF < reactor.stats["tick"] then
- maxRF = reactor.stats["tick"]
- end
- if currentRfTick ~= reactor.stats["tick"] then
- currentRfTick = reactor.stats["tick"]
- local max = math.ceil(graphs["tick"].width * (currentRfTick/maxRF))
- local currentRFTickObj = {x = graphs["tick"].x, y = graphs["tick"].y, width = max, height = graphs["tick"].height}
- printInfos("tick")
- printGraphs("tick")
- printActiveGraphs(currentRFTickObj)
- end
- if currentRF ~= reactor.stats["stored"] then
- currentRF = reactor.stats["stored"]
- local max = math.ceil(graphs["stored"].width * (currentRF/10000000))
- local currentRFObj = {x = graphs["stored"].x, y = graphs["stored"].y, width = max, height = graphs["stored"].height, stored = currentRF}
- printInfos("stored")
- printGraphs("stored")
- printActiveGraphStore(currentRFObj)
- end
- if currentRodLevel ~= reactor.stats["rods"] then
- currentRodLevel = reactor.stats["rods"]
- local max = math.ceil(graphs["rods"].width * (currentRodLevel/100))
- local currentRodObj = {x = graphs["rods"].x, y = graphs["rods"].y, width = max, height = graphs["rods"].height}
- printInfos("rods")
- printGraphs("rods")
- printActiveGraphs(currentRodObj)
- end
- if currenFuel ~= reactor.stats["fuel"] then
- currenFuel = reactor.stats["fuel"]
- printInfos("fuel")
- end
- if active ~= reactor.stats["active"] then
- active = reactor.stats["active"]
- printInfos2("active")
- end
- if fuelreactivity ~= reactor.stats["activity"] then
- fuelreactivity = reactor.stats["activity"]
- printInfos("activity")
- end
- if totalfuel ~= reactor.stats["totalfuel"] then
- totalfuel = reactor.stats["totalfuel"]
- printInfos("totalfuel")
- end
- if totalwaste ~= reactor.stats["totalwaste"] then
- totalwaste = reactor.stats["totalwaste"]
- printInfos("totalwaste")
- end
- if casingtemperature ~= reactor.stats["casingtemp"] then
- casingtemperature = reactor.stats["casingtemp"]
- printInfos("casingtemp")
- end
- if fueltemperature ~= reactor.stats["fueltemp"] then
- fueltemperature = reactor.stats["fueltemp"]
- printInfos("fueltemp")
- end
- printControlInfos()
- if DEBUG == true then
- printDebug()
- end
- end
- function startup()
- getInfoFromFile()
- getInfoFromReactor()
- setSections()
- setGraphs()
- setInfos()
- setButtons()
- if DEBUG == true then
- debugInfos()
- printDebug()
- end
- for name, data in pairs(sections) do
- printBorders(name)
- end
- for name, data in pairs(graphs) do
- printGraphs(name)
- end
- for name, data in pairs(infos) do
- printInfos(name)
- end
- printInfos2("active")
- printStaticControlText()
- end
- -- helpers
- function round(val, decimal)
- if (decimal) then
- return math.floor( (val * 10^decimal) + 0.5) / (10^decimal)
- else
- return math.floor(val+0.5)
- end
- end
- function file_exists(name)
- local f=io.open(name,"r")
- if f~=nil then
- io.close(f)
- return false
- else
- return true
- end
- end
- function getInfoFromFile()
- if file_exists("reactor.txt") then
- file = io.open("reactor.txt","w")
- file:write("0", "\n")
- file:write("100", "\n")
- file:close()
- else
- file = io.open("reactor.txt","r")
- minPowerRod = tonumber(file:read("*l"))
- maxPowerRod = tonumber(file:read("*l"))
- file:close()
- end
- end
- function setInfoToFile()
- file = io.open("reactor.txt","w")
- file:write(minPowerRod, "\n")
- file:write(maxPowerRod, "\n")
- file:flush()
- file:close()
- end
- function testVersion()
- reactor.getEnergyStats()
- end
- -- starting
- xpcall(testVersion, setOldVersion)
- startup()
- API.screen()
- event.listen("touch", API.checkxy)
- while event.pull(0.1, "interrupted") == nil do
- if reactor.mbIsConnected() == true and reactor.mbIsAssembled() == true then
- getInfoFromReactor()
- end
- calculateAdjustRodsLevel()
- draw()
- local event, address, arg1, arg2, arg3 = event.pull(1)
- if type(address) == "string" and component.isPrimary(address) then
- if event == "key_down" and arg2 == keyboard.keys.q then
- os.exit()
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement