Advertisement
eternalclickbait

Untitled

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