Advertisement
D_Puppy

Passive reactor control

Mar 11th, 2017
827
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 13.67 KB | None | 0 0
  1. local component = require("component")
  2. local gpu = component.gpu
  3. local gui = require("gui")
  4. local reactor = component.br_reactor
  5. local ser = require("serialization")
  6. local os = require("os")
  7.  
  8. local prgName = "Reactor Control v"
  9. local version = "1.2"
  10.  
  11. local optionsFile = "/etc/reactorSettings"
  12. local settingsTable = gui.loadTable(optionsFile)
  13.  
  14. local rods = reactor.getNumberOfControlRods()
  15. if rods > 25 then
  16.   rods = 25     -- max 25 rods
  17. end
  18. local rod = {}
  19.  
  20.  
  21.  
  22. if settingsTable == nil then
  23.   maxLevel = 9000000
  24.   boosterLevel = 5000000
  25.   alarmLevel = 100000
  26.   shutdownOnExit = true
  27.   rodLevelOnExit = 0
  28.   autoEjectWaste = true
  29. else
  30.   maxLevel = settingsTable.maxLevel
  31.   boosterLevel = settingsTable.boosterLevel
  32.   alarmLevel = settingsTable.alarmLevel
  33.   shutdownOnExit = settingsTable.shutdownOnExit
  34.   rodLevelOnExit = settingsTable.rodLevelOnExit
  35.   autoEjectWaste = settingsTable.autoEjectWaste
  36. end
  37.  
  38.  
  39. local isRunning = false
  40. reactor.setActive(false)
  41. local setup = false
  42. local chart = false
  43.  
  44.  
  45. chartGui = gui.newGui(4, 5, 54, 16, true, "Power production")
  46. powerChart = gui.newChart(chartGui, 1, 1, 0, 10000, {}, 50, 13)
  47.  
  48.  
  49. function optionsboostDownButtonCallback(guiID, id)
  50.   if boosterLevel > 0 then
  51.     boosterLevel = boosterLevel - 100000
  52.   end
  53.   gui.setText(optionsGui, optionsboostLabel, string.format("%8d RF", boosterLevel))
  54.   gui.setText(optionsGui, optionsboostProzLabel, string.format("(%3d%%)", 100/10000000*boosterLevel))
  55. end
  56. function optionsboostUpButtonCallback(guiID, id)
  57.   if boosterLevel < maxLevel then
  58.     boosterLevel = boosterLevel + 100000
  59.   end
  60.   gui.setText(optionsGui, optionsboostLabel, string.format("%8d RF", boosterLevel))
  61.   gui.setText(optionsGui, optionsboostProzLabel, string.format("(%3d%%)", 100/10000000*boosterLevel))
  62. end
  63.  
  64. function optionsMaxUpButtonCallback(guiID, id)
  65.   if maxLevel < 10000000 then
  66.     maxLevel = maxLevel + 100000
  67.   end
  68.   gui.setText(optionsGui, optionsMaxLabel, string.format("%8d RF", maxLevel))
  69.   gui.setText(optionsGui, optionsMaxProzLabel, string.format("(%3d%%)", 100/10000000*maxLevel))
  70. end
  71. function optionsMaxDownButtonCallback(guiID, id)
  72.   if maxLevel > boosterLevel then
  73.     maxLevel = maxLevel - 100000
  74.   end
  75.   gui.setText(optionsGui, optionsMaxLabel, string.format("%8d RF", maxLevel))
  76.   gui.setText(optionsGui, optionsMaxProzLabel, string.format("(%3d%%)", 100/10000000*maxLevel))
  77. end
  78.  
  79. function optionsAlarmUpButtonCallback(guiID, id)
  80.   if alarmLevel < 10000000 then
  81.     alarmLevel = alarmLevel + 100000
  82.   end
  83.   gui.setText(optionsGui, optionsAlarmLabel, string.format("%8d RF", alarmLevel))
  84.   gui.setText(optionsGui, optionsAlarmProzLabel, string.format("(%3d%%)", 100/10000000*alarmLevel))
  85. end
  86. function optionsAlarmDownButtonCallback(guiID, id)
  87.   if alarmLevel > 0 then
  88.     alarmLevel = alarmLevel - 100000
  89.   end
  90.   gui.setText(optionsGui, optionsAlarmLabel, string.format("%8d RF", alarmLevel))
  91.   gui.setText(optionsGui, optionsAlarmProzLabel, string.format("(%3d%%)", 100/10000000*alarmLevel))
  92. end
  93.  
  94.  
  95. function optionsDoneButtonCallback(guiID, id)
  96.   shutdownOnExit=gui.getCheckboxStatus(guiID, optionsSchutdownExitCheckbox)
  97.   if gui.getRadio(optionsGui) == optionsRod0Radio then
  98.     rodLevelOnExit = 0
  99.   else
  100.     rodLevelOnExit = 100
  101.   end
  102.   autoEjectWaste=gui.getCheckboxStatus(guiID, optionsAutoEjectWasteCheckbox)
  103.   if settingsTable == nil then
  104.     settingsTable = {}
  105.   end
  106.   settingsTable.boosterLevel = boosterLevel
  107.   settingsTable.maxLevel = maxLevel
  108.   settingsTable.alarmLevel = alarmLevel
  109.   settingsTable.shutdownOnExit = shutdownOnExit
  110.   settingsTable.rodLevelOnExit = rodLevelOnExit
  111.   settingsTable.autoEjectWaste = autoEjectWaste
  112.   gui.saveTable(settingsTable, optionsFile)
  113.   setup = false
  114.   gui.displayGui(myGui)
  115. end
  116.  
  117.  
  118.  
  119. optionsGui = gui.newGui(14, 6, 54, 16, true, "Setup")
  120. optionsExitButton = gui.newButton(optionsGui, "center", 14, "done", optionsDoneButtonCallback)
  121. gui.newLabel(optionsGui, 2, 2, "Boost level : ")
  122. optionsboostDownButton = gui.newButton(optionsGui, 16, 2, "down", optionsboostDownButtonCallback)
  123. optionsboostLabel = gui.newLabel(optionsGui, 23, 2, string.format("%8d RF", boosterLevel))
  124. optionsboostProzLabel = gui.newLabel(optionsGui, 35, 2, string.format("(%3d%%)", 100/10000000*boosterLevel))
  125. optionsboostUpButton = gui.newButton(optionsGui, 43, 2, "up", optionsboostUpButtonCallback)
  126. gui.newLabel(optionsGui, 2, 4, "Max level   : ")
  127. optionsMaxDownButton = gui.newButton(optionsGui, 16, 4, "down", optionsMaxDownButtonCallback)
  128. optionsMaxLabel = gui.newLabel(optionsGui, 23, 4, string.format("%8d RF", maxLevel))
  129. optionsMaxProzLabel = gui.newLabel(optionsGui, 35, 4, string.format("(%3d%%)", 100/10000000*maxLevel))
  130. optionsMaxUpButton = gui.newButton(optionsGui, 43, 4, "up", optionsMaxUpButtonCallback)
  131. gui.newLabel(optionsGui, 2, 6, "Alarm level : ")
  132. optionsAlarmDownButton = gui.newButton(optionsGui, 16, 6, "down", optionsAlarmDownButtonCallback)
  133. optionsAlarmLabel = gui.newLabel(optionsGui, 23, 6, string.format("%8d RF", alarmLevel))
  134. optionsAlarmProzLabel = gui.newLabel(optionsGui, 35, 6, string.format("(%3d%%)", 100/10000000*alarmLevel))
  135. optionsAlarmUpButton = gui.newButton(optionsGui, 43, 6, "up", optionsAlarmUpButtonCallback)
  136. gui.newLabel(optionsGui, 2, 8, "Shutdown reactor on exit ")
  137. optionsSchutdownExitCheckbox = gui.newCheckbox(optionsGui, 28, 8, shutdownOnExit)
  138. gui.newLabel(optionsGui, 2, 10, "On exit set rods to 0%     or 100%")
  139. optionsRod0Radio=gui.newRadio(optionsGui, 25, 10)
  140. optionsRod100Radio=gui.newRadio(optionsGui, 37, 10)
  141. if rodLevelOnExit == 0 then
  142.   gui.setRadio(optionsGui, optionsRod0Radio)
  143. else
  144.   gui.setRadio(optionsGui, optionsRod100Radio)
  145. end
  146. gui.newLabel(optionsGui, 2, 12, "Auto eject waste ")
  147. optionsAutoEjectWasteCheckbox = gui.newCheckbox(optionsGui, 19, 12, autoEjectWaste)
  148.  
  149. -- Callbacks Main gui
  150.  
  151. function exitButtonCallback(guiID, id)
  152.   local result = gui.getYesNo("", "Do you really want to exit?", "")
  153.   if result == true then
  154.   reactor.setAllControlRodLevels(rodLevelOnExit)
  155.     if shutdownOnExit == true then
  156.       reactor.setActive(false)
  157.     end
  158.     gui.exit()
  159.   end
  160.   gui.displayGui(myGui)
  161. end
  162.  
  163. function ejectFuelButtonCallback(guiID, id)
  164.   reactor.doEjectFuel()
  165. end
  166.  
  167. function ejectWasteButtonCallback(guiID, id)
  168.   reactor.doEjectWaste()
  169. end
  170.  
  171. function shutdownButtonCallback(guiID, id)
  172.   isRunning = false
  173.   reactor.setActive(false)
  174.   gui.setEnable(guiID, id, false)
  175.   gui.setEnable(guiID, startButton, true)
  176.   if chart == true then
  177.     gui.setEnable(myGui, chartButton, false)
  178.     gui.setEnable(myGui, rodButton, true)
  179.     gui.displayGui(chartGui)
  180.   else
  181.     gui.setEnable(myGui, rodButton, false)
  182.     gui.setEnable(myGui, chartButton, true)
  183.     gui.displayGui(myGui)
  184.     reactor.setAllControlRodLevels(100)
  185.   end
  186. end
  187.  
  188. function startButtonCallback(guiID, id)
  189.   isRunning = true
  190.   reactor.setActive(true)
  191.   gui.setEnable(guiID, id, false)
  192.   gui.setEnable(guiID, shutdownButton, true)
  193.  
  194.   if chart == true then
  195.     gui.setEnable(myGui, chartButton, false)
  196.     gui.setEnable(myGui, rodButton, true)
  197.     gui.displayGui(chartGui)
  198.   else
  199.     gui.setEnable(myGui, rodButton, false)
  200.     gui.setEnable(myGui, chartButton, true)
  201.     gui.displayGui(myGui)
  202.   end
  203. end
  204.  
  205.  
  206.  
  207. function optionsButtonCallback(guiID, id)
  208.   setup = true
  209.   gui.displayGui(optionsGui)
  210. end
  211.  
  212. function chartButtonCallback(guiID, id)
  213.   chart = true
  214.   gui.setEnable(myGui, chartButton, false)
  215.   gui.setEnable(myGui, rodButton, true)
  216.   gui.displayGui(chartGui)
  217. end
  218.  
  219. function rodButtonCallback(guiID, id)
  220.   chart = false
  221.   gui.setEnable(myGui, rodButton, false)
  222.   gui.setEnable(myGui, chartButton, true)
  223.   gui.displayGui(myGui)
  224. end
  225.  
  226. -- Setup Main gui
  227.  
  228. myGui = gui.newGui(2, 2, 78, 23, true)
  229. optionsButton = gui.newButton(myGui, 2, 19, "options", optionsButtonCallback)
  230. chartButton = gui.newButton(myGui, 12, 19, "show chart", chartButtonCallback)
  231. rodButton = gui.newButton(myGui, 25, 19, "show rods", rodButtonCallback)
  232. startButton = gui.newButton(myGui, 2, 21, "start", startButtonCallback)
  233. shutdownButton = gui.newButton(myGui, 10, 21, "shutdown", shutdownButtonCallback)
  234. ejectFuelButton = gui.newButton(myGui, 21, 21, "eject fuel", ejectFuelButtonCallback)
  235. ejectWasteButton = gui.newButton(myGui, 34, 21, "eject waste", ejectWasteButtonCallback)
  236. exitbutton = gui.newButton(myGui, 48, 21, "exit", exitButtonCallback)
  237.  
  238. gui.setEnable(myGui, rodButton, false)
  239.  
  240. gui.newLabel(myGui, 2, 1, "Reactor address : " )
  241. reactorAddressLabel = gui.newLabel(myGui, 20, 1, reactor.address)
  242.  
  243. statusLabel = gui.newLabel(myGui, 62, 1, "")
  244.  
  245. gui.newFrame(myGui, 2, 3, 54, 16, string.format("Control rods %d/25",rods))
  246. for i = 1, rods do
  247.   rod[i] = gui.newVProgress(myGui, i * 2 + 2, 4, 14, 1, 100, 0, nil, 1)
  248.   reactor.setControlRodLevel(i-1, 100)
  249. end
  250. gui.newFrame(myGui, 56, 3, 20, 5, "Energy level")
  251. energyLevelProgress = gui.newProgress(myGui, 58, 5, 16, 10000000, 0, nil, true)
  252. energyLevelLabel = gui.newLabel(myGui, 58, 6, "")
  253.  
  254. gui.newFrame(myGui, 56, 8, 20, 3, "Energy prod.")
  255. energyProdLabel = gui.newLabel(myGui, 58, 9, "")
  256.  
  257. gui.newFrame(myGui, 56, 11, 20, 6, "Temperatures")
  258. casingTempProgress = gui.newProgress(myGui, 58, 13, 16, 2000, 0)
  259. casingTempLabel = gui.newLabel(myGui, 58, 12, "")
  260. fuelTempProgress = gui.newProgress(myGui, 58, 15, 16, 2000, 0)
  261. fuelTempLabel = gui.newLabel(myGui, 58, 14, "")
  262.  
  263. gui.newFrame(myGui, 56, 17, 20, 5, "Fuel levels")
  264. fuelAmountLabel = gui.newLabel(myGui, 58, 18, "Fuel  : ")
  265. wasteAmountLabel = gui.newLabel(myGui, 58, 19, "Waste : ")
  266. fuelAmountProgress = gui.newProgress(myGui, 58, 20, 16, reactor.getFuelAmountMax(), 0)
  267.  
  268. local oldEnergyLevel = 0
  269. gui.setEnable(myGui, shutdownButton, false)
  270.  
  271. gui.clearScreen()
  272. gui.setTop(prgName .. version)
  273. local guiVersion, guiMajor, guiMinor = gui.Version()
  274. gui.setBottom("(c)2017 by S.Kempa                                           Gui lib V" .. guiVersion)
  275.  
  276. if gui.checkVersion(2,0) == false then
  277.   gui.showError("Wrong Gui library", "Found version " .. gui.Version(), "Needed version 2.0")
  278. end
  279.  
  280. gui.displayGui(myGui)
  281.  
  282.  
  283. local loopCounter = 0
  284. local statisticsCounter = 1
  285. local statisticsTable = {}
  286.  
  287. -- Loop
  288.  
  289. while true do
  290.   if setup == false then
  291.     gui.runGui(myGui)
  292.     if setup == false then
  293.       local storedEnergy = reactor.getEnergyStored()
  294.       if storedEnergy < alarmLevel then
  295.     gui.setBackground(myGui, reactorAddressLabel, 0xFF0000) -- red
  296.       else
  297.     gui.setBackground(myGui, reactorAddressLabel, 0xC0C0C0) -- gray
  298.       end  
  299.       gui.setValue(myGui, energyLevelProgress, storedEnergy)
  300.       gui.setText(myGui, energyLevelLabel, string.format("%8d RF",storedEnergy))
  301.       if reactor.getActive() == true then
  302.     gui.setText(myGui, statusLabel, "(online) ")
  303.     if oldEnergyLevel >= storedEnergy and storedEnergy > boosterLevel and storedEnergy < maxLevel then
  304.       for i = 0, rods - 1 do
  305.         x = reactor.getControlRodLevel(i)
  306.         if x > 0 then
  307.           x = x - 1
  308.           reactor.setControlRodLevel(i,x)
  309.           break
  310.         end
  311.       end
  312.     elseif oldEnergyLevel < storedEnergy and storedEnergy > boosterLevel and storedEnergy < maxLevel then
  313.       for i = rods - 1, 0, -1 do
  314.         x = reactor.getControlRodLevel(i)
  315.         if x < 100 then
  316.           x = x + 1
  317.           reactor.setControlRodLevel(i,x)
  318.           break
  319.         end
  320.       end
  321.     elseif storedEnergy > maxLevel then -- all rods will be off
  322.       reactor.setAllControlRodLevels(100)
  323.       if chart == false then
  324.         for i = 0, rods - 1 do
  325.           gui.setValue(myGui, rod[i+1], 100)
  326.         end
  327.       end
  328.       reactor.setActive(false)
  329.     elseif storedEnergy < boosterLevel then
  330.       reactor.setAllControlRodLevels(0)
  331.       if chart == false then
  332.         for i = 0, rods - 1 do
  333.           gui.setValue(myGui, rod[i+1], 0)  -- all rods on full power, for an energy production boost
  334.         end
  335.       end
  336.     end
  337.       else
  338.     if isRunning == true then
  339.       gui.setText(myGui, statusLabel, "(offline)")
  340.       if chart == false then
  341.         for i = 0, rods - 1 do
  342.           gui.setValue(myGui, rod[i+1], 0)
  343.         end
  344.       end
  345.       if storedEnergy < maxLevel then  
  346.         reactor.setActive(true)
  347.         if chart == false then
  348.           for i = 0, rods - 1 do
  349.         gui.setValue(myGui, rod[i+1], 100)
  350.           end
  351.         end
  352.       end
  353.     end
  354.       end
  355.  
  356.       if chart == false then
  357.     for i = 0, rods - 1 do
  358.       gui.setValue(myGui, rod[i+1], 100 - reactor.getControlRodLevel(i))
  359.     end
  360.       end
  361.  
  362.       oldEnergyLevel = storedEnergy
  363.       local energyProduced = reactor.getEnergyProducedLastTick()
  364.       gui.setText(myGui, energyProdLabel, string.format("%8d RF/t", energyProduced))
  365.  
  366.       local casingTemp = reactor.getCasingTemperature()
  367.       gui.setValue(myGui, casingTempProgress, casingTemp)
  368.       gui.setText(myGui, casingTempLabel, string.format("Casing : %4d C", casingTemp))
  369.  
  370.       local fuelTemp = reactor.getFuelTemperature()
  371.       gui.setValue(myGui, fuelTempProgress, casingTemp)
  372.       gui.setText(myGui, fuelTempLabel, string.format("Fuel   : %4d C", fuelTemp))
  373.  
  374.       local fuelAmount = reactor.getFuelAmount()
  375.       local wasteAmount = reactor.getWasteAmount()
  376.       gui.setText(myGui, fuelAmountLabel, string.format("Fuel :%7d mB", fuelAmount))
  377.       gui.setText(myGui, wasteAmountLabel, string.format("Waste:%7d mB", wasteAmount))
  378.       gui.setValue(myGui, fuelAmountProgress, fuelAmount)
  379.       if wasteAmount >= 1000 and autoEjectWaste == true then
  380.     reactor.doEjectWaste()
  381.       end
  382.     end
  383.  
  384.   elseif setup == true then
  385.     gui.runGui(optionsGui)
  386.   end
  387.   if chart == true then
  388.     -- statistics
  389.       if loopCounter == 1 then
  390.         statisticsTable[statisticsCounter] = reactor.getEnergyProducedLastTick()
  391.         gui.setChartData(chartGui, powerChart, statisticsTable)
  392.         statisticsCounter = statisticsCounter + 1
  393.         if statisticsCounter == 50 then
  394.           statisticsCounter = 1
  395.         end
  396.         loopCounter = 0
  397.       end
  398.       loopCounter = loopCounter + 1
  399.   end
  400. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement