Advertisement
fatboychummy

reactorBackwardsCompat

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