eternalclickbait

Untitled

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