NexAdn

Big Reactors — Reactor Controller (PASV mode) v1.0

Jul 3rd, 2017
690
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.37 KB | None | 0 0
  1. --[[
  2.     Big Reactors Controller Suite v1.0
  3.    =====================================
  4.     Reactor Controller for passive mode
  5.    
  6.    by NexAdn
  7. ]]
  8.  
  9. --[[===== USER CONFIGURATION =====]]--
  10. local tConfig = {
  11.     -- Priority list for Reactor Computer Ports
  12.     priorityPorts = { "back" },
  13.    
  14.     -- Tick interval (Time between checks in seconds)
  15.     tickInterval = 0.5,
  16.    
  17.     -- Automatic security and economical mechanisms
  18.     preventOverheat = true,
  19.     fuelSave = true,
  20.    
  21.     ---- Overheat prevention
  22.     -- Unit: Degrees Celsius
  23.     -- When to start inserting fuel rods
  24.     tempLimitSoft = 1500,
  25.     -- When to shut down the reactor
  26.     tempLimitHard = 1900,
  27.     -- When to restart the reactor after overheat shutdown (IMPORTANT: The lowest possible temperature is 20 degrees!)
  28.     tempRestartTarget = 30,
  29.    
  30.     ---- Fuel save (Rod insertion according to energy buffer level)
  31.     -- Unit: Per cent
  32.     -- Start inserting the control rod
  33.     energyMarkLow  = 20,
  34.     -- Fully insert the control rod
  35.     energyMarkHigh = 90,
  36. }
  37.  
  38. --[[===== DO NOT CHANGE ANYTHING BELOW THIS LINE =====]]--
  39. -- Change if needed
  40. local portName      = "BigReactors-Reactor"
  41. local maxEnergy     = 10000000
  42. local maxTemp       = 2000
  43.        
  44. ------------------------
  45. ---- INITIALIZATION ----
  46. ------------------------
  47. local overheatShutdown = false
  48. local dispHeight    = 0
  49. local dispWidth     = 0
  50. local reactorPort = {}
  51. targetRodInsertion  = 0
  52.  
  53. dispWidth, dispHeight = term.getSize()
  54.  
  55. function multiblockSleep ()
  56.     while not reactorPort.getConnected() do
  57.         print("Not connected to a valid multiblock.\nWaiting...")
  58.         sleep(3)
  59.     end
  60. end
  61.  
  62. function connectReactorPort ()
  63.     reactorPort = peripheral.find(portName)
  64.     for k,v in pairs(tConfig.priorityPorts) do
  65.         if peripheral.getType(v) == portName then
  66.             reactorPort = peripheral.wrap(v)
  67.         end
  68.     end
  69.     multiblockSleep()
  70. end
  71.  
  72. ---------------------
  73. ---- GENERAL I/O ----
  74. ---------------------
  75.  
  76. function getStorageRate ()
  77.     multiblockSleep()
  78.     return reactorPort.getEnergyStored()*100/maxEnergy
  79. end
  80.  
  81. function getTemperature ()
  82.     multiblockSleep()
  83.     return math.max(reactorPort.getFuelTemperature(), reactorPort.getCasingTemperature())
  84. end
  85.  
  86. function getActive ()
  87.     multiblockSleep()
  88.     return reactorPort.getActive()
  89. end
  90.  
  91. function setRodInsertion (insertionLevel)
  92.     multiblockSleep()
  93.     reactorPort.setAllControlRodLevels(math.min(100,math.max(0,insertionLevel)))
  94. end
  95.  
  96. function setActive (isActive)
  97.     multiblockSleep()
  98.     reactorPort.setActive(isActive)
  99. end
  100.  
  101. function setRS(color)
  102.     rs.setBundledOutput(tConfig.rsOutput, color)
  103. end
  104.  
  105. ------------------------
  106. ---- GUI AND ENGINE ----
  107. ------------------------
  108.  
  109. function calcSet ()
  110.     multiblockSleep()
  111.     local storageRate = getStorageRate()
  112.     local temp = getTemperature()
  113.    
  114.     targetRodInsertion = 0
  115.    
  116.     if tConfig.preventOverheat then
  117.         if temp >= tConfig.tempLimitSoft then
  118.             targetRodInsertion = (temp - tConfig.tempLimitSoft) * 100 / (tConfig.tempLimitHard - tConfig.tempLimitSoft)
  119.         end
  120.         if temp > tConfig.tempLimitHard then
  121.             setActive(false)
  122.             overheatShutdown = true
  123.         end
  124.         if getActive() and overheatShutdown and temp <= tConfig.tempRestartTarget then
  125.             setActive(true)
  126.             overheatShutdown = false
  127.         end
  128.     end
  129.     if tConfig.fuelSave then
  130.         if storageRate > tConfig.energyMarkLow then
  131.             targetRodInsertion = math.max( (storageRate - tConfig.energyMarkLow) * 100 / (tConfig.energyMarkHigh - tConfig.energyMarkLow), targetRodInsertion)
  132.         end
  133.         if storageRate > tConfig.energyMarkHigh then
  134.             targetRodInsertion = 100
  135.         end
  136.     end
  137.     setRodInsertion(targetRodInsertion)
  138. end
  139.  
  140. function getCol(num, maxcols)
  141.     --return math.floor((dispWidth / (2*maxcols)) * num)
  142.     return math.floor(((dispWidth / maxcols)) * num - (dispWidth/(2*maxcols)))
  143. end
  144.  
  145. function initCol(num, maxcols, prefix)
  146.     term.setTextColor(colors.black)
  147.     term.setBackgroundColor(colors.white)
  148.     term.setCursorPos(getCol(num,maxcols), 1)
  149.     term.write(prefix)
  150.     for i=2,dispHeight-1,1 do
  151.         for j=1,3,1 do
  152.             term.setCursorPos(getCol(num,maxcols)-2+j, i)
  153.             term.setBackgroundColor(colors.gray)
  154.             term.write(" ")
  155.         end
  156.     end
  157. end
  158.  
  159. function drawCol(num, maxcols, rela)
  160.     local height =math.min(math.max(dispHeight-2-math.floor( ( rela * (dispHeight-2) )/100 ), 0), dispHeight-2)
  161.     for i=height+1,dispHeight-1,1 do
  162.         for j=1,3,1 do
  163.             term.setCursorPos(getCol(num,maxcols)-2+j,i)
  164.             term.write(" ")
  165.         end
  166.     end
  167. end
  168.  
  169. function drawGUI()
  170.     -- INIT
  171.     term.clear()
  172.     if term.isColor() then
  173.         term.setBackgroundColor(colors.white)
  174.         term.clear()
  175.         term.setTextColor(colors.black)
  176.        
  177.         initCol(1, 4, "T")
  178.         initCol(2, 4, "E")
  179.         initCol(3, 4, "I")
  180.        
  181.         term.setTextColor(colors.black)
  182.         term.setBackgroundColor(colors.red)
  183.        
  184.         drawCol(1, 4, getTemperature()*100/maxTemp)
  185.         drawCol(2, 4, getStorageRate())
  186.         drawCol(3, 4, targetRodInsertion)
  187.        
  188.         term.setBackgroundColor(colors.white)
  189.         if getTemperature() < 1000 then
  190.             term.setCursorPos(getCol(1,4)-1, dispHeight)
  191.         else
  192.             term.setCursorPos(getCol(1,4)-2, dispHeight)
  193.         end
  194.         term.write(math.floor(getTemperature()))
  195.        
  196.         term.setBackgroundColor(colors.white)
  197.         term.setCursorPos(getCol(2,4), dispHeight)
  198.         term.write(math.floor(getStorageRate()))
  199.        
  200.         term.setBackgroundColor(colors.white)
  201.         term.setCursorPos(getCol(3,4), dispHeight)
  202.         term.write(math.floor(targetRodInsertion))
  203.     else
  204.         print("Please use an advanced computer/monitor to enable GUI functionality.")
  205.     end
  206. end
  207.  
  208. function main()
  209.     if term.isColor() then
  210.         term.setCursorBlink(false)
  211.     end
  212.     connectReactorPort()
  213.     if tConfig.preventOverheat and getTemperature() > tConfig.tempLimitHard then
  214.         setActive(false)
  215.         overheatShutdown = true
  216.     else
  217.         setActive(true)
  218.         overheatShutdown = false
  219.     end
  220.     while true do
  221.         calcSet()
  222.         drawGUI()
  223.         sleep(tConfig.tickInterval)
  224.     end
  225. end
  226.  
  227. main()
Advertisement
Add Comment
Please, Sign In to add comment