SHARE
TWEET

Untitled

eternalclickbait Nov 16th, 2019 80 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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
  11. local reactorMaxTemp = 1000
  12. --The maximum energy in our turbine's buffer. Max a turbine can hold is 1M (1,000,000)
  13. local turbineMaxEnergy = 0.99*1000000
  14. --------------------------------------------
  15.  
  16. function round(num, numDecimalPlaces)
  17.   local mult = 10^(numDecimalPlaces or 0)
  18.   return math.floor(num * mult + 0.5) / mult
  19. end
  20.  
  21. function log(message, textColour, backgroundColour, newline)
  22.   local oldTextCol = term.getTextColor()
  23.   local oldBackCol = term.getBackgroundColor()
  24.  
  25.   term.setTextColour(textColour or colors.white)
  26.   term.setBackgroundColour(backgroundColour or colors.black)
  27.   print(message)
  28.   term.setTextColour(oldTextCol)
  29.   term.setBackgroundColour(oldBackCol)
  30.  
  31.   for _, side in ipairs(monitors) do
  32.     local m = peripheral.wrap(side)
  33.    
  34.     oldTextCol = term.getTextColor()
  35.     oldBackCol = term.getBackgroundColor()
  36.  
  37.     term.setTextColour(textColour or colors.white)
  38.     term.setBackgroundColour(backgroundColour or colors.black)
  39.     m.write(message)
  40.     term.setTextColour(oldTextCol)
  41.     term.setBackgroundColour(oldBackCol)
  42.    
  43.     --Move down a line
  44.     if (newline == true) then
  45.       local x,y = m.getCursorPos()
  46.       m.setCursorPos(1,y+1)
  47.     end
  48.   end
  49. end
  50.  
  51. -- maps a given range from a specific iterator to a new range
  52. local function map(n, start, stop, newStart, newStop, withinBounds)
  53.     local value = ((n - start) / (stop - start)) * (newStop - newStart) + newStart
  54.    
  55.     --// Returns basic value
  56.     if not withinBounds then
  57.         return value
  58.     end
  59.    
  60.     --// Returns values constrained to exact range
  61.     if newStart < newStop then
  62.         return math.max(math.min(value, newStop), newStart)
  63.     else
  64.         return math.max(math.min(value, newStart), newStop)
  65.     end
  66. end
  67.  
  68. --Sets the scales on all monitors for easily visible text
  69. function setMonitorScales()
  70.   for _, side in ipairs(monitors) do
  71.     local m = peripheral.wrap(side)
  72.     m.setTextScale(2)
  73.   end
  74. end
  75.  
  76. --Clears all monitors/windows
  77. function clear()
  78.   term.clear()
  79.   for _, side in ipairs(monitors) do
  80.     peripheral.wrap(side).clear()
  81.     peripheral.wrap(side).setCursorPos(1,1)--Reset the cursor position
  82.   end
  83.   term.setCursorPos(1,1)
  84. end
  85.  
  86. function clearTable(t)
  87.   for i=0, #t do table.remove(t, i) end
  88. end
  89.  
  90. --Updates the list of reactors, turbines and monitor
  91. function updatePeripherals()
  92.   local peripherals = peripheral.getNames() --Get a list of all the peripherals
  93.   --Clear our tables
  94.   clearTable(reactors)
  95.   clearTable(turbines)
  96.   clearTable(monitors)
  97.  
  98.   for _, pSide in ipairs(peripherals) do
  99.     local pType = peripheral.getType(pSide)
  100.     local pFunctions = peripheral.wrap(pSide)
  101.    
  102.     if(pType == "BigReactors-Reactor") then --If we found a reactor
  103.       table.insert(reactors, pSide)
  104.     elseif (pType == "BigReactors-Turbine") then
  105.       table.insert(turbines, pSide)
  106.     elseif (pType == "monitor") then
  107.       table.insert(monitors, pSide)
  108.     end
  109.   end
  110. end
  111.  
  112. --Lists all of the peripherals connected
  113. function listPeripherals()
  114.   log("Reactors:")
  115.   for i, pSide in ipairs(reactors) do
  116.     log("\t" ..i.. ":\t" ..pSide)
  117.   end
  118.  
  119.   log("Turbines:")
  120.   for i, pSide in ipairs(turbines) do
  121.     log("\t" ..i.. ":\t" ..pSide)
  122.   end
  123.  
  124.   log("Monitors:")
  125.   for i, pSide in ipairs(monitors) do
  126.     log("\t" ..i.. ":\t" ..pSide)
  127.   end
  128. end
  129.  
  130.  
  131.  
  132. --Updates all info on screen
  133. function update()
  134.   --Clear the window(s)
  135.   clear()
  136.  
  137.   log ("Turbine max energy: " ..turbineMaxEnergy)
  138.   log ("Reactor max temp:   " ..reactorMaxTemp)
  139.  
  140.   updatePeripherals()
  141.   listPeripherals()
  142.  
  143.   --Now print out the info for each turbine/reactor, and enable/disable them as needed
  144.   for _,pSide in ipairs(reactors) do
  145.     local r = peripheral.wrap(pSide)
  146.    
  147.     local temp = r.getFuelTemperature()
  148.  
  149.     local shouldBeActive = true
  150.  
  151.     --If the reactor's at or above the max temp, it shouldn't run
  152.     if(temp >= reactorMaxTemp) then shouldBeActive = false end
  153.    
  154.     --If it has less than 1 bucket of water (coolant), it shoudn't run
  155.     if(r.getCoolantAmount() <= 1000) then shouldBeActive = false end
  156.    
  157.     --Set the reactor's active state
  158.     r.setActive(shouldBeActive)
  159.    
  160.     log(pSide.. ":")
  161.     local tempCol
  162.     if(temp > 1000) then
  163.       tempCol = colors.red
  164.     elseif(temp > 500) then
  165.       tempCol = colors.yellow
  166.     elseif (temp > 100) then
  167.       tempCol = colors.green
  168.     elseif (temp > 0) then
  169.       tempCol = colors.blue
  170.     end
  171.     log("\t" ..round(temp).. " C", tempCol)
  172.   end
  173.  
  174.   for _,pSide in ipairs(turbines) do
  175.     local t = peripheral.wrap(pSide)
  176.    
  177.     local energy = t.getEnergyStored()
  178.       local rpm = t.getRotorSpeed()
  179.  
  180.     --If the turbine's above the amount of energy allowed, disable the coils
  181.     if(energy >= turbineMaxEnergy) then
  182.       t.setInductorEngaged(false)
  183.     else
  184.       t.setInductorEngaged(true)
  185.     end
  186.    
  187.     log(pSide.. ":\t")
  188.     local energyCol
  189.     if(energy > 900000) then
  190.       energyCol = colors.red
  191.     elseif(energy > 500000) then
  192.       energyCol = colors.yellow
  193.     elseif (energy > 100000) then
  194.       energyCol = colors.green
  195.     elseif (energy > 0) then
  196.       energyCol = colors.blue
  197.     end
  198.     local rpmCol
  199.     if(rpm > 10000) then
  200.       rpmCol = colors.red
  201.     elseif(rpm > 5000) then
  202.       rpmCol = colors.yellow
  203.     elseif (rpm > 1000) then
  204.       rpmCol = colors.green
  205.     elseif (rpm > 0) then
  206.       rpmCol = colors.blue
  207.     end
  208.    
  209.     log(round(energy).. " RF\t", energyCol, colors.black, false)
  210.     log(round(rpm).. " RPM", rpmCol, colors.black)
  211.   end
  212. end
  213.  
  214. setMonitorScales()
  215. while true do
  216.   update()
  217.   sleep(0.1)
  218. end
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top