Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local component = require("component")
- local gpu = component.gpu
- local gui = require("gui")
- local reactor = component.br_reactor
- local ser = require("serialization")
- local os = require("os")
- local prgName = "Reactor Control v"
- local version = "1.2"
- local optionsFile = "/etc/reactorSettings"
- local settingsTable = gui.loadTable(optionsFile)
- local rods = reactor.getNumberOfControlRods()
- if rods > 25 then
- rods = 25 -- max 25 rods
- end
- local rod = {}
- if settingsTable == nil then
- maxLevel = 9000000
- boosterLevel = 5000000
- alarmLevel = 100000
- shutdownOnExit = true
- rodLevelOnExit = 0
- autoEjectWaste = true
- else
- maxLevel = settingsTable.maxLevel
- boosterLevel = settingsTable.boosterLevel
- alarmLevel = settingsTable.alarmLevel
- shutdownOnExit = settingsTable.shutdownOnExit
- rodLevelOnExit = settingsTable.rodLevelOnExit
- autoEjectWaste = settingsTable.autoEjectWaste
- end
- local isRunning = false
- reactor.setActive(false)
- local setup = false
- local chart = false
- chartGui = gui.newGui(4, 5, 54, 16, true, "Power production")
- powerChart = gui.newChart(chartGui, 1, 1, 0, 10000, {}, 50, 13)
- function optionsboostDownButtonCallback(guiID, id)
- if boosterLevel > 0 then
- boosterLevel = boosterLevel - 100000
- end
- gui.setText(optionsGui, optionsboostLabel, string.format("%8d RF", boosterLevel))
- gui.setText(optionsGui, optionsboostProzLabel, string.format("(%3d%%)", 100/10000000*boosterLevel))
- end
- function optionsboostUpButtonCallback(guiID, id)
- if boosterLevel < maxLevel then
- boosterLevel = boosterLevel + 100000
- end
- gui.setText(optionsGui, optionsboostLabel, string.format("%8d RF", boosterLevel))
- gui.setText(optionsGui, optionsboostProzLabel, string.format("(%3d%%)", 100/10000000*boosterLevel))
- end
- function optionsMaxUpButtonCallback(guiID, id)
- if maxLevel < 10000000 then
- maxLevel = maxLevel + 100000
- end
- gui.setText(optionsGui, optionsMaxLabel, string.format("%8d RF", maxLevel))
- gui.setText(optionsGui, optionsMaxProzLabel, string.format("(%3d%%)", 100/10000000*maxLevel))
- end
- function optionsMaxDownButtonCallback(guiID, id)
- if maxLevel > boosterLevel then
- maxLevel = maxLevel - 100000
- end
- gui.setText(optionsGui, optionsMaxLabel, string.format("%8d RF", maxLevel))
- gui.setText(optionsGui, optionsMaxProzLabel, string.format("(%3d%%)", 100/10000000*maxLevel))
- end
- function optionsAlarmUpButtonCallback(guiID, id)
- if alarmLevel < 10000000 then
- alarmLevel = alarmLevel + 100000
- end
- gui.setText(optionsGui, optionsAlarmLabel, string.format("%8d RF", alarmLevel))
- gui.setText(optionsGui, optionsAlarmProzLabel, string.format("(%3d%%)", 100/10000000*alarmLevel))
- end
- function optionsAlarmDownButtonCallback(guiID, id)
- if alarmLevel > 0 then
- alarmLevel = alarmLevel - 100000
- end
- gui.setText(optionsGui, optionsAlarmLabel, string.format("%8d RF", alarmLevel))
- gui.setText(optionsGui, optionsAlarmProzLabel, string.format("(%3d%%)", 100/10000000*alarmLevel))
- end
- function optionsDoneButtonCallback(guiID, id)
- shutdownOnExit=gui.getCheckboxStatus(guiID, optionsSchutdownExitCheckbox)
- if gui.getRadio(optionsGui) == optionsRod0Radio then
- rodLevelOnExit = 0
- else
- rodLevelOnExit = 100
- end
- autoEjectWaste=gui.getCheckboxStatus(guiID, optionsAutoEjectWasteCheckbox)
- if settingsTable == nil then
- settingsTable = {}
- end
- settingsTable.boosterLevel = boosterLevel
- settingsTable.maxLevel = maxLevel
- settingsTable.alarmLevel = alarmLevel
- settingsTable.shutdownOnExit = shutdownOnExit
- settingsTable.rodLevelOnExit = rodLevelOnExit
- settingsTable.autoEjectWaste = autoEjectWaste
- gui.saveTable(settingsTable, optionsFile)
- setup = false
- gui.displayGui(myGui)
- end
- optionsGui = gui.newGui(14, 6, 54, 16, true, "Setup")
- optionsExitButton = gui.newButton(optionsGui, "center", 14, "done", optionsDoneButtonCallback)
- gui.newLabel(optionsGui, 2, 2, "Boost level : ")
- optionsboostDownButton = gui.newButton(optionsGui, 16, 2, "down", optionsboostDownButtonCallback)
- optionsboostLabel = gui.newLabel(optionsGui, 23, 2, string.format("%8d RF", boosterLevel))
- optionsboostProzLabel = gui.newLabel(optionsGui, 35, 2, string.format("(%3d%%)", 100/10000000*boosterLevel))
- optionsboostUpButton = gui.newButton(optionsGui, 43, 2, "up", optionsboostUpButtonCallback)
- gui.newLabel(optionsGui, 2, 4, "Max level : ")
- optionsMaxDownButton = gui.newButton(optionsGui, 16, 4, "down", optionsMaxDownButtonCallback)
- optionsMaxLabel = gui.newLabel(optionsGui, 23, 4, string.format("%8d RF", maxLevel))
- optionsMaxProzLabel = gui.newLabel(optionsGui, 35, 4, string.format("(%3d%%)", 100/10000000*maxLevel))
- optionsMaxUpButton = gui.newButton(optionsGui, 43, 4, "up", optionsMaxUpButtonCallback)
- gui.newLabel(optionsGui, 2, 6, "Alarm level : ")
- optionsAlarmDownButton = gui.newButton(optionsGui, 16, 6, "down", optionsAlarmDownButtonCallback)
- optionsAlarmLabel = gui.newLabel(optionsGui, 23, 6, string.format("%8d RF", alarmLevel))
- optionsAlarmProzLabel = gui.newLabel(optionsGui, 35, 6, string.format("(%3d%%)", 100/10000000*alarmLevel))
- optionsAlarmUpButton = gui.newButton(optionsGui, 43, 6, "up", optionsAlarmUpButtonCallback)
- gui.newLabel(optionsGui, 2, 8, "Shutdown reactor on exit ")
- optionsSchutdownExitCheckbox = gui.newCheckbox(optionsGui, 28, 8, shutdownOnExit)
- gui.newLabel(optionsGui, 2, 10, "On exit set rods to 0% or 100%")
- optionsRod0Radio=gui.newRadio(optionsGui, 25, 10)
- optionsRod100Radio=gui.newRadio(optionsGui, 37, 10)
- if rodLevelOnExit == 0 then
- gui.setRadio(optionsGui, optionsRod0Radio)
- else
- gui.setRadio(optionsGui, optionsRod100Radio)
- end
- gui.newLabel(optionsGui, 2, 12, "Auto eject waste ")
- optionsAutoEjectWasteCheckbox = gui.newCheckbox(optionsGui, 19, 12, autoEjectWaste)
- -- Callbacks Main gui
- function exitButtonCallback(guiID, id)
- local result = gui.getYesNo("", "Do you really want to exit?", "")
- if result == true then
- reactor.setAllControlRodLevels(rodLevelOnExit)
- if shutdownOnExit == true then
- reactor.setActive(false)
- end
- gui.exit()
- end
- gui.displayGui(myGui)
- end
- function ejectFuelButtonCallback(guiID, id)
- reactor.doEjectFuel()
- end
- function ejectWasteButtonCallback(guiID, id)
- reactor.doEjectWaste()
- end
- function shutdownButtonCallback(guiID, id)
- isRunning = false
- reactor.setActive(false)
- gui.setEnable(guiID, id, false)
- gui.setEnable(guiID, startButton, true)
- if chart == true then
- gui.setEnable(myGui, chartButton, false)
- gui.setEnable(myGui, rodButton, true)
- gui.displayGui(chartGui)
- else
- gui.setEnable(myGui, rodButton, false)
- gui.setEnable(myGui, chartButton, true)
- gui.displayGui(myGui)
- reactor.setAllControlRodLevels(100)
- end
- end
- function startButtonCallback(guiID, id)
- isRunning = true
- reactor.setActive(true)
- gui.setEnable(guiID, id, false)
- gui.setEnable(guiID, shutdownButton, true)
- if chart == true then
- gui.setEnable(myGui, chartButton, false)
- gui.setEnable(myGui, rodButton, true)
- gui.displayGui(chartGui)
- else
- gui.setEnable(myGui, rodButton, false)
- gui.setEnable(myGui, chartButton, true)
- gui.displayGui(myGui)
- end
- end
- function optionsButtonCallback(guiID, id)
- setup = true
- gui.displayGui(optionsGui)
- end
- function chartButtonCallback(guiID, id)
- chart = true
- gui.setEnable(myGui, chartButton, false)
- gui.setEnable(myGui, rodButton, true)
- gui.displayGui(chartGui)
- end
- function rodButtonCallback(guiID, id)
- chart = false
- gui.setEnable(myGui, rodButton, false)
- gui.setEnable(myGui, chartButton, true)
- gui.displayGui(myGui)
- end
- -- Setup Main gui
- myGui = gui.newGui(2, 2, 78, 23, true)
- optionsButton = gui.newButton(myGui, 2, 19, "options", optionsButtonCallback)
- chartButton = gui.newButton(myGui, 12, 19, "show chart", chartButtonCallback)
- rodButton = gui.newButton(myGui, 25, 19, "show rods", rodButtonCallback)
- startButton = gui.newButton(myGui, 2, 21, "start", startButtonCallback)
- shutdownButton = gui.newButton(myGui, 10, 21, "shutdown", shutdownButtonCallback)
- ejectFuelButton = gui.newButton(myGui, 21, 21, "eject fuel", ejectFuelButtonCallback)
- ejectWasteButton = gui.newButton(myGui, 34, 21, "eject waste", ejectWasteButtonCallback)
- exitbutton = gui.newButton(myGui, 48, 21, "exit", exitButtonCallback)
- gui.setEnable(myGui, rodButton, false)
- gui.newLabel(myGui, 2, 1, "Reactor address : " )
- reactorAddressLabel = gui.newLabel(myGui, 20, 1, reactor.address)
- statusLabel = gui.newLabel(myGui, 62, 1, "")
- gui.newFrame(myGui, 2, 3, 54, 16, string.format("Control rods %d/25",rods))
- for i = 1, rods do
- rod[i] = gui.newVProgress(myGui, i * 2 + 2, 4, 14, 1, 100, 0, nil, 1)
- reactor.setControlRodLevel(i-1, 100)
- end
- gui.newFrame(myGui, 56, 3, 20, 5, "Energy level")
- energyLevelProgress = gui.newProgress(myGui, 58, 5, 16, 10000000, 0, nil, true)
- energyLevelLabel = gui.newLabel(myGui, 58, 6, "")
- gui.newFrame(myGui, 56, 8, 20, 3, "Energy prod.")
- energyProdLabel = gui.newLabel(myGui, 58, 9, "")
- gui.newFrame(myGui, 56, 11, 20, 6, "Temperatures")
- casingTempProgress = gui.newProgress(myGui, 58, 13, 16, 2000, 0)
- casingTempLabel = gui.newLabel(myGui, 58, 12, "")
- fuelTempProgress = gui.newProgress(myGui, 58, 15, 16, 2000, 0)
- fuelTempLabel = gui.newLabel(myGui, 58, 14, "")
- gui.newFrame(myGui, 56, 17, 20, 5, "Fuel levels")
- fuelAmountLabel = gui.newLabel(myGui, 58, 18, "Fuel : ")
- wasteAmountLabel = gui.newLabel(myGui, 58, 19, "Waste : ")
- fuelAmountProgress = gui.newProgress(myGui, 58, 20, 16, reactor.getFuelAmountMax(), 0)
- local oldEnergyLevel = 0
- gui.setEnable(myGui, shutdownButton, false)
- gui.clearScreen()
- gui.setTop(prgName .. version)
- local guiVersion, guiMajor, guiMinor = gui.Version()
- gui.setBottom("(c)2017 by S.Kempa Gui lib V" .. guiVersion)
- if gui.checkVersion(2,0) == false then
- gui.showError("Wrong Gui library", "Found version " .. gui.Version(), "Needed version 2.0")
- end
- gui.displayGui(myGui)
- local loopCounter = 0
- local statisticsCounter = 1
- local statisticsTable = {}
- -- Loop
- while true do
- if setup == false then
- gui.runGui(myGui)
- if setup == false then
- local storedEnergy = reactor.getEnergyStored()
- if storedEnergy < alarmLevel then
- gui.setBackground(myGui, reactorAddressLabel, 0xFF0000) -- red
- else
- gui.setBackground(myGui, reactorAddressLabel, 0xC0C0C0) -- gray
- end
- gui.setValue(myGui, energyLevelProgress, storedEnergy)
- gui.setText(myGui, energyLevelLabel, string.format("%8d RF",storedEnergy))
- if reactor.getActive() == true then
- gui.setText(myGui, statusLabel, "(online) ")
- if oldEnergyLevel >= storedEnergy and storedEnergy > boosterLevel and storedEnergy < maxLevel then
- for i = 0, rods - 1 do
- x = reactor.getControlRodLevel(i)
- if x > 0 then
- x = x - 1
- reactor.setControlRodLevel(i,x)
- break
- end
- end
- elseif oldEnergyLevel < storedEnergy and storedEnergy > boosterLevel and storedEnergy < maxLevel then
- for i = rods - 1, 0, -1 do
- x = reactor.getControlRodLevel(i)
- if x < 100 then
- x = x + 1
- reactor.setControlRodLevel(i,x)
- break
- end
- end
- elseif storedEnergy > maxLevel then -- all rods will be off
- reactor.setAllControlRodLevels(100)
- if chart == false then
- for i = 0, rods - 1 do
- gui.setValue(myGui, rod[i+1], 100)
- end
- end
- reactor.setActive(false)
- elseif storedEnergy < boosterLevel then
- reactor.setAllControlRodLevels(0)
- if chart == false then
- for i = 0, rods - 1 do
- gui.setValue(myGui, rod[i+1], 0) -- all rods on full power, for an energy production boost
- end
- end
- end
- else
- if isRunning == true then
- gui.setText(myGui, statusLabel, "(offline)")
- if chart == false then
- for i = 0, rods - 1 do
- gui.setValue(myGui, rod[i+1], 0)
- end
- end
- if storedEnergy < maxLevel then
- reactor.setActive(true)
- if chart == false then
- for i = 0, rods - 1 do
- gui.setValue(myGui, rod[i+1], 100)
- end
- end
- end
- end
- end
- if chart == false then
- for i = 0, rods - 1 do
- gui.setValue(myGui, rod[i+1], 100 - reactor.getControlRodLevel(i))
- end
- end
- oldEnergyLevel = storedEnergy
- local energyProduced = reactor.getEnergyProducedLastTick()
- gui.setText(myGui, energyProdLabel, string.format("%8d RF/t", energyProduced))
- local casingTemp = reactor.getCasingTemperature()
- gui.setValue(myGui, casingTempProgress, casingTemp)
- gui.setText(myGui, casingTempLabel, string.format("Casing : %4d C", casingTemp))
- local fuelTemp = reactor.getFuelTemperature()
- gui.setValue(myGui, fuelTempProgress, casingTemp)
- gui.setText(myGui, fuelTempLabel, string.format("Fuel : %4d C", fuelTemp))
- local fuelAmount = reactor.getFuelAmount()
- local wasteAmount = reactor.getWasteAmount()
- gui.setText(myGui, fuelAmountLabel, string.format("Fuel :%7d mB", fuelAmount))
- gui.setText(myGui, wasteAmountLabel, string.format("Waste:%7d mB", wasteAmount))
- gui.setValue(myGui, fuelAmountProgress, fuelAmount)
- if wasteAmount >= 1000 and autoEjectWaste == true then
- reactor.doEjectWaste()
- end
- end
- elseif setup == true then
- gui.runGui(optionsGui)
- end
- if chart == true then
- -- statistics
- if loopCounter == 1 then
- statisticsTable[statisticsCounter] = reactor.getEnergyProducedLastTick()
- gui.setChartData(chartGui, powerChart, statisticsTable)
- statisticsCounter = statisticsCounter + 1
- if statisticsCounter == 50 then
- statisticsCounter = 1
- end
- loopCounter = 0
- end
- loopCounter = loopCounter + 1
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement