Xonoa

coreMonitor

Jan 28th, 2021 (edited)
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.20 KB | None | 0 0
  1. modem = peripheral.find("modem")
  2. mon = peripheral.find("monitor")
  3. modem.open(24816)
  4.  
  5. local maxPower = 2140000000000
  6. local monitorWidth = mon.getSize()
  7.  
  8. mon.clear()
  9.  
  10.  
  11. --adds commas to long numbers
  12. function comma_value(amount)
  13. local formatted = amount
  14. while true do
  15. formatted, k = string.gsub(formatted, "^(-?%d+)(%d%d%d)", '%1,%2')
  16. if (k==0) then
  17. break
  18. end
  19. end
  20. return formatted
  21. end
  22.  
  23.  
  24. --rounds number to specified number of decimals
  25. function round(val, decimal)
  26. if (decimal) then
  27. return math.floor( (val * 10^decimal) + 0.5) / (10^decimal)
  28. else
  29. return math.floor(val+0.5)
  30. end
  31. end
  32.  
  33.  
  34. --draws bars
  35. function drawBar(barColor, BGColor, maxLength, barLength, startX, startY)
  36. --draws the background color
  37. mon.setCursorPos(startX, startY)
  38. mon.setBackgroundColor(BGColor)
  39. for len=1,maxLength do
  40. mon.write(" ")
  41. end
  42.  
  43. --draws the bar
  44. mon.setCursorPos(startX, startY)
  45. mon.setBackgroundColor(barColor)
  46. for length=1,barLength do
  47. mon.write(" ")
  48. end
  49. end
  50.  
  51.  
  52. --writes text
  53. function writeText(startX, startY, text, FGColor, BGColor, clear)
  54. mon.setCursorPos(startX, startY)
  55. mon.setTextColor(FGColor)
  56. mon.setBackgroundColor(BGColor)
  57. if clear then
  58. mon.clearLine()
  59. end
  60. mon.write(text)
  61. end
  62.  
  63.  
  64. --calculates order of magnitude for large numbers and returns smaller number and prefix
  65. function enshrinkenNumber(number, decimal)
  66. local levels = 0
  67. local remainder = number
  68. local failout = false
  69.  
  70. while (remainder > 1000) do
  71. remainder = remainder / 1000
  72. levels = levels+1
  73. if (levels > 10) or (remainder < 0) then
  74. return "",""
  75. end
  76. end
  77.  
  78. if decimal then
  79. remainder = round(remainder, decimal)
  80. end
  81.  
  82. if levels == 0 then
  83. return remainder, ""
  84. elseif levels == 1 then
  85. return remainder, "k"
  86. elseif levels == 2 then
  87. return remainder, "M"
  88. elseif levels == 3 then
  89. return remainder, "G"
  90. elseif levels == 4 then
  91. return remainder, "T"
  92. elseif levels == 5 then
  93. return remainder, "P"
  94. elseif levels == 6 then
  95. return remainder, "E"
  96. elseif levels == 7 then
  97. return remainder, "Z"
  98. elseif levels == 8 then
  99. return remainder, "Y"
  100. else
  101. return nil, nil
  102. end
  103.  
  104. end
  105.  
  106.  
  107. --formats a number of seconds into a more usable format HH:MM:SS
  108. function formatTime(time)
  109. time = math.floor(time)
  110. local hours = math.floor(time/3600)
  111. local minutes = math.floor(time/60 - (hours*60))
  112. local seconds = math.floor(time - (hours*3600) - (minutes*60))
  113.  
  114. return (hours..":"..minutes..":"..seconds)
  115. end
  116.  
  117.  
  118. --draws the current power bar
  119. function drawPowerLevel(currE, row)
  120.  
  121. --gets the percent fill to 2 decimals
  122. local fillPercent = (round((currE / maxPower), 4)) * 100
  123.  
  124. local maxBarLength = monitorWidth - 2
  125.  
  126. local barLength = math.floor((fillPercent * maxBarLength) / 100)
  127.  
  128. local barColor
  129.  
  130. if fillPercent<10 then
  131. barColor = colors.red
  132. elseif fillPercent < 25 then
  133. barColor = colors.yellow
  134. elseif fillPercent <= 100 then
  135. barColor = colors.lime
  136. else
  137. barColor = colors.gray
  138. end
  139.  
  140. drawBar(barColor, colors.gray, maxBarLength, barLength, 2, row)
  141. drawBar(barColor, colors.gray, maxBarLength, barLength, 2, row+1)
  142.  
  143. local currENum, currEPrefix = enshrinkenNumber(currE, 3)
  144. local maxNum, maxPrefix = enshrinkenNumber(maxPower, 3)
  145.  
  146. local text = "Energy: "..currENum.." "..currEPrefix.."rf / "..maxNum.." "..maxPrefix.."rf"
  147. writeText(1, row+3, text, colors.white, colors.black, true)
  148. writeText(1, row+4, "Fill percentage: "..fillPercent.."%", colors.white, colors.black, true)
  149. end
  150.  
  151.  
  152. --draws power in/out
  153. function drawPowerFlux(deltaE, row)
  154.  
  155. writeText(1, row, "Energy Flux: ", colors.white, colors.black, true)
  156.  
  157. local textColor
  158. local symbol
  159. if deltaE>0 then
  160. textColor = colors.lime
  161. symbol = "+"
  162. elseif deltaE<0 then
  163. textColor = colors.red
  164. symbol = "-"
  165. else
  166. textColor = colors.white
  167. symbol = ""
  168. end
  169.  
  170. writeText(14, row, symbol..deltaE, textColor, colors.black)
  171. local currPos = mon.getCursorPos()
  172. writeText(currPos+1, row, "rf/t", colors.white, colors.black)
  173. end
  174.  
  175.  
  176. --draws the uranium level in the reactor
  177. function drawUraniumLevel(count, row)
  178.  
  179. count = round(count/1000, 3)
  180.  
  181. local volNum, volPrefix = enshrinkenNumber(count, 3)
  182.  
  183. local text = "Total Nuclear Fuel: "..volNum.." "..volPrefix.."b"
  184. writeText(1, row, text, colors.white, colors.black, true)
  185. end
  186.  
  187.  
  188. --draws runtime for reactor
  189. function drawReactorTime(runTime, row)
  190.  
  191. local text = "Run Time: "..formatTime(runTime)
  192. writeText(1, row, text, colors.white, colors.black, true)
  193. end
  194.  
  195.  
  196. --draws total power expected from reactor given current fuel
  197. function drawTotalPower(totalPower, row)
  198. mon.clearLine(row)
  199.  
  200. local totalNum, totalPrefix = enshrinkenNumber(totalPower, 3)
  201. local text = "Total Power: "..totalNum.." "..totalPrefix.."rf"
  202. writeText(1, row, text, colors.white, colors.black, true)
  203. end
  204.  
  205. while true do
  206. event = {os.pullEvent("modem_message")}
  207.  
  208. if event[4] == 1 then
  209. drawPowerLevel(event[5], 2)
  210. elseif event[4] == 2 then
  211. drawPowerFlux(event[5], 7)
  212. elseif event[4] == 3 then
  213. drawUraniumLevel(event[5], 9)
  214. elseif event[4] == 4 then
  215. drawReactorTime(event[5], 10)
  216. elseif event[4] == 5 then
  217. drawTotalPower(event[5], 11)
  218. else
  219. os.sleep(0.1)
  220. end
  221. end
Add Comment
Please, Sign In to add comment