eternalclickbait

Untitled

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