eternalclickbait

Untitled

Nov 16th, 2019
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.43 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)
  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. local x,y = m.getSize()
  47. --Normalize the scales
  48. x = map (x, 7,56, 0.5,5.5)
  49. y = map (y, 5,30, 0.5,5.5)
  50. local avg = (x+y)/2 --And average them
  51. scale = map(scale, 0.5,5.5, 0,1)
  52. local s = (tonumber(scale) * tonumber(avg))
  53. m.write(avg)
  54. m.write("\n")
  55. m.write(scale)
  56. m.write("\n")
  57. m.write(s)
  58. m.setTextScale(s)
  59. end
  60. end
  61.  
  62. --Clears all monitors/windows
  63. function clear()
  64. term.clear()
  65. for _, side in ipairs(monitors) do
  66. peripheral.wrap(side).clear()
  67. peripheral.wrap(side).setCursorPos(1,1)--Reset the cursor position
  68. end
  69. term.setCursorPos(1,1)
  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. log("Peripherals:")
  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. --Updates all info on screen
  110. function update()
  111. --Clear the window(s)
  112. clear()
  113.  
  114. --Now print out the info for each turbine/reactor, and enable/disable them as needed
  115. for _,pSide in ipairs(reactors) do
  116. local r = peripheral.wrap(pSide)
  117.  
  118. local temp = r.getFuelTemperature()
  119.  
  120. local shouldBeActive = true
  121.  
  122. --If the reactor's at or above the max temp, it shouldn't run
  123. if(temp >= reactorMaxTemp) then shouldBeActive = false end
  124.  
  125. --If it has less than 1 bucket of water (coolant), it shoudn't run
  126. if(r.getCoolantAmount() <= 1000) then shouldBeActive = false end
  127.  
  128. --Set the reactor's active state
  129. r.setActive(shouldBeActive)
  130.  
  131. log(pSide.. ":\t" ..temp.. " C\t"
  132. end
  133.  
  134. for _,pSide in ipairs(turbines) do
  135. local t = peripheral.wrap(pSide)
  136.  
  137. local energy = t.getEnergyStored()
  138. local rpm = t.getRotorSpeed()
  139.  
  140. --If the turbine's above the amount of energy allowed, disable the coils
  141. if(energy >= turbineMaxEnergy) then
  142. t.setInductorEngaged(false)
  143. else
  144. t.setInductorEngaged(true)
  145. end
  146.  
  147. log(pSide.. ":\t" ..energy.. " RF\t" ..rpm.. " RPM")
  148. end
  149. end
  150.  
  151. log ("Turbine max energy: " ..turbineMaxEnergy)
  152. log ("Reactor max temp:" ..reactorMaxTemp)
  153.  
  154. updatePeripherals()
  155. listPeripherals()
  156. sleep(2)
  157. local i = 1
  158. while true do
  159. update()
  160. setMonitorScales(i)
  161. log("Current scale: " ..i)
  162. i = i +0
  163. sleep(2)
  164. end
Advertisement
Add Comment
Please, Sign In to add comment