Advertisement
minecraftboii

reactor monitor v2

Jul 30th, 2019
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.32 KB | None | 0 0
  1. local reactorName = "BigReactors-Reactor_3"
  2. local monitorName = "monitor_3"
  3. local powerMonitorName = "right"
  4. local autoScaling = true
  5. local surplusThreshold = 0.7 -- Percentage of internal buffer where below the reactor will generate a surplus of energy
  6. local allowedSurplusStatic = 50 -- RF/t delta surplus when above surplus threshold
  7. local allowedSurplusGrowth = 250 -- RF/t of delta surplus if reactor below surplus threshold
  8.  
  9. -- util
  10. function percentage(value, max)
  11. return math.ceil(value / max * 10000) * 0.01
  12. end
  13.  
  14. function convertTimeToTick(time)
  15. return (time * 1000 + 18000) % 24000
  16. end
  17.  
  18. function getDeltaTicks(lastTick)
  19. local now = os.time()
  20. now = convertTimeToTick(now)
  21. if now < lastTick then lastTick = lastTick - 24000 end
  22. local delta = now - lastTick
  23. if delta < 1 then delta = 1 end
  24. return delta
  25. end
  26.  
  27. function round(value, precision)
  28. local multiplier = 10 ^ precision
  29. if multiplier > 1 then
  30. return math.ceil(value * multiplier) / multiplier
  31. else
  32. return math.ceil(value * multiplier)
  33. end
  34. end
  35.  
  36. -- formatting
  37. function format_int(number)
  38. if number == nil then number = 0 end
  39. local i, j, minus, int, fraction = tostring(number):find('([-]?)(%d+)([.]?%d*)')
  40.  
  41. -- reverse the int-string and append a comma to all blocks of 3 digits
  42. int = int:reverse():gsub("(%d%d%d)", "%1,")
  43. -- reverse the int-string back remove an optional comma and put the
  44. -- optional minus and fractional part back
  45. return minus .. int:reverse():gsub("^,", "") .. fraction
  46. end
  47.  
  48. -- monitor related
  49.  
  50. --display text text on monitor, "mon" peripheral
  51. function draw_text(mon, x, y, text, text_color, bg_color)
  52. mon.monitor.setBackgroundColor(bg_color)
  53. mon.monitor.setTextColor(text_color)
  54. mon.monitor.setCursorPos(x,y)
  55. mon.monitor.write(text)
  56. end
  57.  
  58. function draw_text_right(mon, offset, y, text, text_color, bg_color)
  59. mon.monitor.setBackgroundColor(bg_color)
  60. mon.monitor.setTextColor(text_color)
  61. mon.monitor.setCursorPos(mon.X-string.len(tostring(text))-offset,y)
  62. mon.monitor.write(text)
  63. end
  64.  
  65. function draw_text_lr(mon, x, y, offset, text1, text2, text1_color, text2_color, bg_color)
  66. draw_text(mon, x, y, text1, text1_color, bg_color)
  67. draw_text_right(mon, offset, y, text2, text2_color, bg_color)
  68. end
  69.  
  70. --draw line on computer terminal
  71. function draw_line(mon, x, y, length, color)
  72. if length < 0 then
  73. length = 0
  74. end
  75. mon.monitor.setBackgroundColor(color)
  76. mon.monitor.setCursorPos(x,y)
  77. mon.monitor.write(string.rep(" ", length))
  78. end
  79.  
  80. function draw_line_vertical(mon, x, y, length, color)
  81. if length < 0 then
  82. length = 0
  83. end
  84. mon.monitor.setBackgroundColor(color)
  85. for i = 0, length - 1 do
  86. mon.monitor.setCursorPos(x,y + i)
  87. mon.monitor.write(" ")
  88. end
  89. end
  90.  
  91. --create progress bar
  92. --draws two overlapping lines
  93. --background line of bg_color
  94. --main line of bar_color as a percentage of minVal/maxVal
  95. function progress_bar(mon, x, y, length, minVal, maxVal, bar_color, bg_color)
  96. draw_line(mon, x, y, length, bg_color) --background bar
  97. local barSize = math.floor((minVal/maxVal) * length)
  98. draw_line(mon, x, y, barSize, bar_color) --progress so far
  99. end
  100.  
  101. -- create stacked progress bar
  102. function stacked_progress_bar(mon, x, y, length, a_val, b_val, maxVal, a_color, b_color, bg_color)
  103. draw_line(mon, x, y, length, bg_color) --background bar
  104. local a_size = math.floor((a_val/maxVal) * length)
  105. local b_size = math.ceil((b_val/maxVal) * length)
  106. draw_line(mon, x, y, a_size, a_color)
  107. draw_line(mon, x + a_size, y, b_size, b_color)
  108. end
  109.  
  110. function vertical_progress_bar(mon, x, y, length, minVal, maxVal, bar_color, bg_color)
  111. draw_line_vertical(mon, x, y, length, bg_color) --background bar
  112. local barSize = math.floor((minVal/maxVal) * length)
  113. draw_line_vertical(mon, x, y, barSize, bar_color) --progress so far
  114. end
  115.  
  116. function clear(mon)
  117. term.clear()
  118. term.setCursorPos(1,1)
  119. mon.monitor.setBackgroundColor(colors.black)
  120. mon.monitor.clear()
  121. mon.monitor.setCursorPos(1,1)
  122. end
  123.  
  124. -- monitor
  125. local mon, monitor, monX, monY
  126. monitor = peripheral.wrap(monitorName)
  127. monX, monY = monitor.getSize()
  128. mon = {}
  129. mon.monitor, mon.X, mon.Y = monitor, monX, monY
  130.  
  131. -- reactor
  132. local reactor = peripheral.wrap(reactorName)
  133. local maxHeat = 2000
  134. local maxEnergy = 10000000
  135. local controlRods = {}
  136. for i = 1, reactor.getNumberOfControlRods() do
  137. controlRods[i] = i - 1 -- Control rod indices start at zero
  138. end
  139.  
  140. -- Power monitor
  141. local powerMonitor = peripheral.wrap(powerMonitorName)
  142.  
  143. function getMostInsertedFuelRod()
  144. local selected = -1
  145. local previousBestInsertion = -10
  146. for _, rodIndex in ipairs(controlRods) do
  147. if reactor.getControlRodLevel(rodIndex) > previousBestInsertion then
  148. selected = rodIndex
  149. previousBestInsertion = reactor.getControlRodLevel(rodIndex)
  150. end
  151. end
  152. return selected, previousBestInsertion
  153. end
  154.  
  155. function getLeastInsertedFuelRod()
  156. local selected = -1
  157. local previousBestInsertion = 110
  158. for _, rodIndex in ipairs(controlRods) do
  159. if reactor.getControlRodLevel(rodIndex) < previousBestInsertion then
  160. selected = rodIndex
  161. previousBestInsertion = reactor.getControlRodLevel(rodIndex)
  162. end
  163. end
  164. return selected, previousBestInsertion
  165. end
  166.  
  167. function scaleUp()
  168. local rod, amount = getMostInsertedFuelRod()
  169. print(rod, amount)
  170. if amount > 0 then reactor.setControlRodLevel(rod, amount - 1) end
  171. end
  172.  
  173. function scaleDown()
  174. local rod, amount = getLeastInsertedFuelRod()
  175. if amount < 100 then reactor.setControlRodLevel(rod, amount + 1) end
  176. end
  177.  
  178. -- Autoscaling
  179. function autoscale()
  180. if not autoScaling then
  181. return
  182. end
  183.  
  184. while true do
  185. local energy = reactor.getEnergyStored()
  186. local produced = reactor.getEnergyProducedLastTick()
  187. local consumed = powerMonitor.getAverageEnergySent()
  188. local deltaEnergy = produced - consumed
  189.  
  190. if deltaEnergy < 0 then
  191. scaleUp()
  192. elseif energy < maxEnergy * surplusThreshold then
  193. local acceptableAddition = allowedSurplusGrowth
  194. if deltaEnergy < acceptableAddition then
  195. scaleUp()
  196. elseif deltaEnergy > acceptableAddition then
  197. scaleDown()
  198. end
  199. elseif deltaEnergy > allowedSurplusStatic then
  200. scaleDown()
  201. end
  202. sleep(0.1)
  203. end
  204. end
  205.  
  206. function drawScreen()
  207. mon.monitor.setTextScale(0.5)
  208. mon.monitor.setTextScale(1)
  209. local lastTick = convertTimeToTick(os.time())
  210. local previousEnergy = reactor.getEnergyStored()
  211. while true do
  212.  
  213. clear(mon)
  214. print("working")
  215.  
  216. if reactor == nil or not reactor.getConnected() then
  217. draw_text(mon, 1, 1, "No reactor found", colors.red, colors.black)
  218. return
  219. end
  220.  
  221. -- Reactor status
  222. local status = "INACTIVE"
  223. local statusColor = colors.red
  224. local active = reactor.getActive()
  225. if active then
  226. status = "ACTIVE"
  227. statusColor = colors.lime
  228. end
  229. draw_text_lr(mon, 2, 2, 1, "Reactor Status", status, colors.white, statusColor, colors.black)
  230.  
  231. -- Power generation
  232. local energy = reactor.getEnergyStored()
  233. local produced = reactor.getEnergyProducedLastTick()
  234. local consumed = powerMonitor.getAverageEnergySent()
  235. local deltaEnergy = produced - consumed
  236. draw_text_lr(mon, 2, 4, 1, "Delta power", format_int(round(deltaEnergy, 0)) .. " rf/t", colors.white, colors.green, colors.black)
  237.  
  238. -- Casing
  239. local fuelTemp = reactor.getFuelTemperature()
  240. local fuelTemperature = reactor.getFuelTemperature()
  241. local fuelColor = colors.red
  242. if fuelTemperature < 1200 then fuelColor = colors.orange end
  243. if fuelTemperature < 1000 then fuelColor = colors.yellow end
  244. if fuelTemperature < 750 then fuelColor = colors.lime end
  245.  
  246. local casingTemp = reactor.getCasingTemperature()
  247. local casingColor = colors.red
  248. if casingTemp < 1200 then casingColor = colors.orange end
  249. if casingTemp < 1000 then casingColor = colors.yellow end
  250. if casingTemp < 750 then casingColor = colors.lime end
  251.  
  252. draw_text_lr(mon, 2, 6, 1, "Fuel " .. round(fuelTemp, 0) .. " C", "Casing " .. round(casingTemp, 0) .. " C", fuelColor, casingColor, colors.black)
  253.  
  254. -- Fuel and waste
  255. local fuelAmount = reactor.getFuelAmount()
  256. local wasteAmount = reactor.getWasteAmount()
  257. local maxFuelAmount = reactor.getFuelAmountMax()
  258. local fuelConsumed = reactor.getFuelConsumedLastTick()
  259. draw_text_lr(mon, 2, 8, 1, "Core fuel", format_int(round(fuelConsumed, 3)) .. " mB/t", colors.white, colors.white, colors.black)
  260. stacked_progress_bar(mon, 2, 9, mon.X-2, wasteAmount, fuelAmount, maxFuelAmount, colors.blue, colors.yellow, colors.gray)
  261.  
  262. -- Control rods
  263. draw_text(mon, 2, 11, "Control rods", colors.white, colors.black)
  264.  
  265. for _, rodIndex in ipairs(controlRods) do
  266. local insertion = reactor.getControlRodLevel(rodIndex)
  267. vertical_progress_bar(mon, 2 + rodIndex * 2, 12, 7, insertion, 100, colors.lightGray, colors.gray)
  268. end
  269.  
  270. -- Fuel temperature
  271. local fuelTemperature = reactor.getFuelTemperature()
  272. local fuelColor = colors.red
  273. if fuelTemperature < 1200 then fuelColor = colors.orange end
  274. if fuelTemperature < 1000 then fuelColor = colors.yellow end
  275. if fuelTemperature < 750 then fuelColor = colors.lime end
  276. -- Casing temperature
  277.  
  278. -- Autoscaling
  279. local autoScalingText = "Autoscaling on"
  280. local autoScalingColor = colors.green
  281. if not autoScaling then
  282. autoScalingText = "Autoscaling off"
  283. autoScalingColor = colors.gray
  284. end
  285.  
  286. draw_text_right(mon, 0, mon.Y, autoScalingText, autoScalingColor, colors.black)
  287. sleep(1)
  288. end
  289. end
  290.  
  291. parallel.waitForAll(autoscale, drawScreen)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement