eternalclickbait

Untitled

Nov 16th, 2019
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.47 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
  11. local reactorMaxTemp = 200
  12. --The maximum energy in our turbine's buffer. Max a turbine can hold is 1M (1,000,000)
  13. local turbineMaxEnergy = 500000
  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. --Prints a message to all monitors/windows
  22. function log(message)
  23. print(message)
  24. for _, side in ipairs(monitors) do
  25. local m = peripheral.wrap(side)
  26. m.write(message)
  27. --Move down a line
  28. local x,y = m.getCursorPos()
  29. m.setCursorPos(1,y+1)
  30. end
  31. end
  32.  
  33. -- maps a given range from a specific iterator to a new range
  34. local function map(n, start, stop, newStart, newStop, withinBounds)
  35. local value = ((n - start) / (stop - start)) * (newStop - newStart) + newStart
  36.  
  37. --// Returns basic value
  38. if not withinBounds then
  39. return value
  40. end
  41.  
  42. --// Returns values constrained to exact range
  43. if newStart < newStop then
  44. return math.max(math.min(value, newStop), newStart)
  45. else
  46. return math.max(math.min(value, newStart), newStop)
  47. end
  48. end
  49.  
  50. --Sets the scales on all monitors for easily visible text
  51. function setMonitorScales()
  52. for _, side in ipairs(monitors) do
  53. local m = peripheral.wrap(side)
  54. m.setTextScale(2)
  55. end
  56. end
  57.  
  58. --Clears all monitors/windows
  59. function clear()
  60. term.clear()
  61. for _, side in ipairs(monitors) do
  62. peripheral.wrap(side).clear()
  63. peripheral.wrap(side).setCursorPos(1,1)--Reset the cursor position
  64. end
  65. term.setCursorPos(1,1)
  66. end
  67.  
  68. function clearTable(t)
  69. for i=0, #t do table.remove(t, i) end
  70. end
  71.  
  72. --Updates the list of reactors, turbines and monitor
  73. function updatePeripherals()
  74. local peripherals = peripheral.getNames() --Get a list of all the peripherals
  75. --Clear our tables
  76. clearTable(reactors)
  77. clearTable(turbines)
  78. clearTable(monitors)
  79.  
  80. for _, pSide in ipairs(peripherals) do
  81. local pType = peripheral.getType(pSide)
  82. local pFunctions = peripheral.wrap(pSide)
  83.  
  84. if(pType == "BigReactors-Reactor") then --If we found a reactor
  85. table.insert(reactors, pSide)
  86. elseif (pType == "BigReactors-Turbine") then
  87. table.insert(turbines, pSide)
  88. elseif (pType == "monitor") then
  89. table.insert(monitors, pSide)
  90. end
  91. end
  92. end
  93.  
  94. --Lists all of the peripherals connected
  95. function listPeripherals()
  96. log("Reactors:")
  97. for i, pSide in ipairs(reactors) do
  98. log("\t" ..i.. ":\t" ..pSide)
  99. end
  100.  
  101. log("Turbines:")
  102. for i, pSide in ipairs(turbines) do
  103. log("\t" ..i.. ":\t" ..pSide)
  104. end
  105.  
  106. log("Monitors:")
  107. for i, pSide in ipairs(monitors) do
  108. log("\t" ..i.. ":\t" ..pSide)
  109. end
  110. end
  111.  
  112.  
  113.  
  114. --Updates all info on screen
  115. function update()
  116. --Clear the window(s)
  117. clear()
  118.  
  119. log ("Turbine max energy: " ..turbineMaxEnergy)
  120. log ("Reactor max temp:" ..reactorMaxTemp)
  121.  
  122. updatePeripherals()
  123. listPeripherals()
  124.  
  125. --Now print out the info for each turbine/reactor, and enable/disable them as needed
  126. for _,pSide in ipairs(reactors) do
  127. local r = peripheral.wrap(pSide)
  128.  
  129. local temp = r.getFuelTemperature()
  130.  
  131. local shouldBeActive = true
  132.  
  133. --If the reactor's at or above the max temp, it shouldn't run
  134. if(temp >= reactorMaxTemp) then shouldBeActive = false end
  135.  
  136. --If it has less than 1 bucket of water (coolant), it shoudn't run
  137. if(r.getCoolantAmount() <= 1000) then shouldBeActive = false end
  138.  
  139. --Set the reactor's active state
  140. r.setActive(shouldBeActive)
  141.  
  142. log(pSide.. ":")
  143. log("\t" ..round(temp).. " C")
  144. end
  145.  
  146. for _,pSide in ipairs(turbines) do
  147. local t = peripheral.wrap(pSide)
  148.  
  149. local energy = t.getEnergyStored()
  150. local rpm = t.getRotorSpeed()
  151.  
  152. --If the turbine's above the amount of energy allowed, disable the coils
  153. if(energy >= turbineMaxEnergy) then
  154. t.setInductorEngaged(false)
  155. else
  156. t.setInductorEngaged(true)
  157. end
  158.  
  159. log(pSide.. ":")
  160. log("\t" ..round(energy).. " RF\t" ..round(rpm).. " RPM")
  161. end
  162. end
  163.  
  164. setMonitorScales()
  165. while true do
  166. update()
  167. sleep(0.1)
  168. end
Advertisement
Add Comment
Please, Sign In to add comment