eternalclickbait

Untitled

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