eternalclickbait

Reactor Controller (Coloured)

Nov 16th, 2019
788
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.91 KB | None | 0 0
  1. --Reactor and turbine control script by EternalClickbait
  2. --Reactor, turbine and monitor must be connected with wired modems to the computer
  3. --Only works for actively cooled reactors
  4.  
  5. local reactors = {}
  6. local turbines = {}
  7. local monitors = {}
  8.  
  9. -----VARIABLES TO CHANGE FOR YOUR NEEDS-----
  10. --The maximum temperature for the reactor. Above 1000C and efficiency drops fast
  11. local reactorMaxTemp = 900
  12. --The maximum energy in our turbine's buffer. Max a turbine can hold is 1M (1,000,000)
  13. local turbineMaxEnergy = 0.7*1000000
  14. --How long between updates. Shouldn't be more than 0.5
  15. local updateInterval = 0.25
  16. --How big the text should be on the monitor. Range 0.5 - 5.5
  17. local monitorTextScale = 2
  18. --------------------------------------------
  19.  
  20. function round(num, numDecimalPlaces)
  21.   local mult = 10^(numDecimalPlaces or 0)
  22.   return math.floor(num * mult + 0.5) / mult
  23. end
  24.  
  25. function log(message, textColour, backgroundColour, newline)
  26.   local oldTextCol = term.getTextColor()
  27.   local oldBackCol = term.getBackgroundColor()
  28.  
  29.   term.setTextColour(textColour or colors.white)
  30.   term.setBackgroundColour(backgroundColour or colors.black)
  31.   print(message)
  32.   term.setTextColour(oldTextCol)
  33.   term.setBackgroundColour(oldBackCol)
  34.  
  35.   for _, side in ipairs(monitors) do
  36.     local m = peripheral.wrap(side)
  37.    
  38.     oldTextCol = term.getTextColor()
  39.     oldBackCol = term.getBackgroundColor()
  40.  
  41.     m.setTextColour(textColour or colors.white)
  42.     m.setBackgroundColour(backgroundColour or colors.black)
  43.     m.write(message)
  44.     m.setTextColour(oldTextCol)
  45.     m.setBackgroundColour(oldBackCol)
  46.    
  47.     --Move down a line
  48.     if (newline ~= false) then
  49.       local x,y = m.getCursorPos()
  50.       m.setCursorPos(1,y+1)
  51.     end
  52.   end
  53. end
  54.  
  55. -- maps a given range from a specific iterator to a new range
  56. local function map(n, start, stop, newStart, newStop, withinBounds)
  57.     local value = ((n - start) / (stop - start)) * (newStop - newStart) + newStart
  58.    
  59.     --// Returns basic value
  60.     if not withinBounds then
  61.         return value
  62.     end
  63.    
  64.     --// Returns values constrained to exact range
  65.     if newStart < newStop then
  66.         return math.max(math.min(value, newStop), newStart)
  67.     else
  68.         return math.max(math.min(value, newStart), newStop)
  69.     end
  70. end
  71.  
  72. --Sets the scales on all monitors for easily visible text
  73. function setMonitorScales()
  74.   for _, side in ipairs(monitors) do
  75.     local m = peripheral.wrap(side)
  76.     m.setTextScale(monitorTextScale)
  77.   end
  78. end
  79.  
  80. --Clears all monitors/windows
  81. function clear()
  82.   term.clear()
  83.   for _, side in ipairs(monitors) do
  84.     peripheral.wrap(side).clear()
  85.     peripheral.wrap(side).setCursorPos(1,1)--Reset the cursor position
  86.   end
  87.   term.setCursorPos(1,1)
  88. end
  89.  
  90. function clearTable(t)
  91.   for i=0, #t do table.remove(t, i) end
  92. end
  93.  
  94. --Updates the list of reactors, turbines and monitor
  95. function updatePeripherals()
  96.   local peripherals = peripheral.getNames() --Get a list of all the peripherals
  97.   --Clear our tables
  98.   clearTable(reactors)
  99.   clearTable(turbines)
  100.   clearTable(monitors)
  101.  
  102.   for _, pSide in ipairs(peripherals) do
  103.     local pType = peripheral.getType(pSide)
  104.     local pFunctions = peripheral.wrap(pSide)
  105.    
  106.     if(pType == "BigReactors-Reactor") then --If we found a reactor
  107.       table.insert(reactors, pSide)
  108.     elseif (pType == "BigReactors-Turbine") then
  109.       table.insert(turbines, pSide)
  110.     elseif (pType == "monitor") then
  111.       table.insert(monitors, pSide)
  112.     end
  113.   end
  114. end
  115.  
  116. --Lists all of the peripherals connected
  117. function listPeripherals()
  118.   log("Reactors:")
  119.   for i, pSide in ipairs(reactors) do
  120.     log("\t" ..i.. ":\t" ..pSide)
  121.   end
  122.  
  123.   log("Turbines:")
  124.   for i, pSide in ipairs(turbines) do
  125.     log("\t" ..i.. ":\t" ..pSide)
  126.   end
  127.  
  128.   log("Monitors:")
  129.   for i, pSide in ipairs(monitors) do
  130.     log("\t" ..i.. ":\t" ..pSide)
  131.   end
  132. end
  133.  
  134.  
  135.  
  136. --Updates all info on screen
  137. function update()
  138.   --Clear the window(s)
  139.   clear()
  140.  
  141.   log ("Turbine max energy: " ..turbineMaxEnergy)
  142.   log ("Reactor max temp:   " ..reactorMaxTemp)
  143.  
  144.   updatePeripherals()
  145.   --listPeripherals()
  146.  
  147.   --Now print out the info for each turbine/reactor, and enable/disable them as needed
  148.   for _,pSide in ipairs(reactors) do
  149.     local r = peripheral.wrap(pSide)
  150.    
  151.     local temp = r.getFuelTemperature()
  152.     local mbTick = r.getHotFluidProducedLastTick()
  153.  
  154.     local shouldBeActive = true
  155.  
  156.     --If the reactor's at or above the max temp, it shouldn't run
  157.     if(temp >= reactorMaxTemp) then shouldBeActive = false end
  158.    
  159.     --If it has less than 1 bucket of water (coolant), it shoudn't run
  160.     if(r.getCoolantAmount() <= 1000) then shouldBeActive = false end
  161.    
  162.     --Set the reactor's active state
  163.     r.setActive(shouldBeActive)
  164.    
  165.     log(pSide.. ":")
  166.     local tempCol
  167.     if(temp > 1200) then
  168.       tempCol = colors.red
  169.     elseif(temp > 500) then
  170.       tempCol = colors.yellow
  171.     elseif (temp > 100) then
  172.       tempCol = colors.green
  173.     else
  174.       tempCol = colors.blue
  175.     end
  176.    
  177.     local mbTickCol
  178.     if(mbTick > 1900) then
  179.       mbTickCol = colors.red
  180.     elseif(mbTick > 1700) then
  181.       mbTickCol = colors.yellow
  182.     elseif (mbTick > 1500) then
  183.       mbTickCol = colors.green
  184.     else
  185.       mbTickCol = colors.blue
  186.     end
  187.    
  188.     log("\t" ..round(temp).. " C", tempCol, colors.black, false)
  189.     log("\t" ..round(mbTick).. " MB/T", mbTickCol, colors.black, true)
  190.   end
  191.  
  192.   for _,pSide in ipairs(turbines) do
  193.     local t = peripheral.wrap(pSide)
  194.    
  195.     local rfStored = t.getEnergyStored()
  196.     local rpm = t.getRotorSpeed()
  197.     local rfTick = t.getEnergyProducedLastTick()
  198.  
  199.     --If the turbine's above the amount of energy allowed, disable the coils
  200.     if(rfStored >= turbineMaxEnergy) then
  201.       t.setInductorEngaged(false)
  202.     else
  203.       t.setInductorEngaged(true)
  204.     end
  205.    
  206.     log(pSide.. ":")
  207.     local energyCol
  208.     if(rfStored > 900000) then
  209.       energyCol = colors.red
  210.     elseif(rfStored > 500000) then
  211.       energyCol = colors.yellow
  212.     elseif (rfStored > 100000) then
  213.       energyCol = colors.green
  214.     else
  215.       energyCol = colors.blue
  216.     end
  217.    
  218.     local rpmCol
  219.     if(rpm > 10000) then
  220.       rpmCol = colors.red
  221.     elseif(rpm > 5000) then
  222.       rpmCol = colors.yellow
  223.     elseif (rpm > 1000) then
  224.       rpmCol = colors.green
  225.     else
  226.       rpmCol = colors.blue
  227.     end
  228.    
  229.     local rfTickCol
  230.     if(rfTick > 20000) then
  231.       rfTickCol = colors.red
  232.     elseif(rfTick > 12000) then
  233.       rfTickCol = colors.yellow
  234.     elseif (rfTick > 5000) then
  235.       rfTickCol = colors.green
  236.     else
  237.       rfTickCol = colors.blue
  238.     end
  239.    
  240.     log("\t" ..round(rfStored).. " RF", energyCol, colors.black, false)
  241.     log("\t" ..round(rpm).. " RPM", rpmCol, colors.black, false)
  242.     log("\t" ..round(rfTick).. " RF/T", rfTickCol, colors.black)
  243.   end
  244. end
  245.  
  246. while true do
  247.   setMonitorScales()
  248.   update()
  249.   sleep(updateInterval)
  250. end
Advertisement
Add Comment
Please, Sign In to add comment