eternalclickbait

Untitled

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