Sparkybearbomb

Wireless Reactor Portable Display

Dec 26th, 2020 (edited)
1,029
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- Wireless BigReactor Control
  2. -- by SparkyBearBomb
  3. -- Original by jaranvil aka jared314
  4. --
  5. -- feel free to use and/or modify this code
  6. --
  7. -----------------------------------------------
  8. -- Wireless Reactor Control - Version History
  9. --
  10. -- Version 2.0 - 11 Jul 21
  11. -- Upodated to work with the new transmitted message
  12. --
  13. -- Version 1.1 - 26.12.2020
  14. -- - original program forked from ELJptBRk
  15. --
  16. -----------------------------------------------
  17.  
  18. --Port Number
  19. port = 0
  20.  
  21. --monitor size
  22. local monX
  23. local monY
  24.  
  25. term.clear()
  26. -------------------FORMATTING-------------------------------
  27. function clear()
  28.   term.setBackgroundColor(colors.black)
  29.   term.clear()
  30.   term.setCursorPos(1,1)
  31. end
  32.  
  33. --display text text on monitor, "mon" peripheral
  34. function draw_text(x, y, text, text_color, bg_color)
  35.   term.setBackgroundColor(bg_color)
  36.   term.setTextColor(text_color)
  37.   term.setCursorPos(x,y)
  38.   term.write(text)
  39. end
  40.  
  41. --draw line on computer terminal
  42. function draw_line(x, y, length, color)
  43.     term.setBackgroundColor(color)
  44.     term.setCursorPos(x,y)
  45.     term.write(string.rep(" ", length))
  46. end
  47.  
  48. --draw line on computer terminal
  49. function draw_line_term(x, y, length, color)
  50.     term.setBackgroundColor(color)
  51.     term.setCursorPos(x,y)
  52.     term.write(string.rep(" ", length))
  53. end
  54.  
  55. -- Round Decimal Numbers
  56. function roundNumber(num, n)
  57.   local mult = 10^(n or 0)
  58.   return math.floor(num * mult + 0.5) / mult
  59. end
  60.  
  61. --create progress bar
  62. --draws two overlapping lines
  63. --background line of bg_color
  64. --main line of bar_color as a percentage of minVal/maxVal
  65. function progress_bar(x, y, length, minVal, maxVal, bar_color, bg_color)
  66.   draw_line(x, y, length, bg_color) --backgoround bar
  67.   local barSize = math.floor((minVal/maxVal) * length)
  68.   draw_line(x, y, barSize, bar_color) --progress so far
  69. end
  70.  
  71. --create status bar
  72. --draws three overlapping lines
  73. --background line of bg_color
  74. function status_bar(x, y, length, minVal, medVal, maxVal, bar_color1, bar_color2, bg_color)
  75.   draw_line(x, y, length, bg_color) --backgoround bar
  76.   local barSize1 = math.floor((medVal/maxVal) * length)
  77.   local barSize2 = math.floor((minVal/maxVal) * length)
  78.   draw_line(x, y, barSize1, bar_color1) --progress so far
  79.   draw_line(x, y, barSize2, bar_color2) --progress so far
  80. end
  81.  
  82. --same as above but on the computer terminal
  83. function progress_bar_term(x, y, length, minVal, maxVal, bar_color, bg_color)
  84.   draw_line_term(x, y, length, bg_color) --backgoround bar
  85.   local barSize = math.floor((minVal/maxVal) * length)
  86.   draw_line_term(x, y, barSize, bar_color)  --progress so far
  87. end
  88.  
  89. --header and footer bars on monitor
  90. function menu_bar()
  91.   draw_line(0, 1, monX, colors.purple)
  92.   draw_text(1, 1, " Wireless Reactor Receiver", colors.white, colors.purple)
  93.   draw_line(0, 20, monX, colors.purple)
  94.   draw_text(1, 20, " Wireless Reactor Receiver", colors.white, colors.purple)
  95. end
  96.  
  97.  
  98. ------------------------END FORMATTING--------------------------
  99.  
  100. function pharseMessage(message)
  101.     local results = {}
  102.     for pharseData in string.gmatch(message, "[^, ]+") do
  103.         table.insert(results,pharseData)
  104.     end
  105.     active = results[1]
  106.     energyQty = results[2]
  107.   energyMax = results[3]
  108.   energyTick = results[4]
  109.     fuelTemp = results[5]
  110.     caseTemp = results[6]
  111.     fuelQty = results[7]
  112.     wasteQty = results[8]
  113.     fuelMax = results[9]
  114.   fuelTick = results[10]
  115.     rodLevel = results[11]
  116.     isCooled = results[12]
  117. end
  118.  
  119. function rfScale(number)
  120.     if number == nil or number == 0 then
  121.         value = "0 RF/T"
  122.     elseif number < 1000 then
  123.         number = (math.floor(number)/1)
  124.         value = number.." RF/T"
  125.     elseif number < 1000000 then
  126.         number = (math.floor(number)/1000)
  127.         value = number.." KRF/T"
  128.     elseif number < 1000000000 then
  129.         number = (math.floor(number)/1000000)
  130.         value = number.." MF/T"
  131.     else
  132.         number = (math.floor(number)/1000000000)
  133.         value = number.." GF/T"
  134.     end
  135.     return value
  136. end
  137.  
  138. modem = peripheral.wrap("back")
  139. modem.closeAll()
  140. modem.open(port)
  141. monX,monY = term.getSize()
  142.  
  143. while true do
  144.     event, side, frequency, replyFrequency, message, distance = os.pullEvent("modem_message")
  145.     pharseMessage(message)
  146.     clear()
  147.     menu_bar()
  148.    
  149.     --------POWER STAT--------------
  150.     draw_text(2, 3, "Power:", colors.lightGray, colors.black)
  151.     if active == "true" then
  152.       draw_text(9, 3, " ONLINE ", colors.gray, colors.green)
  153.       draw_text(17, 3, " OFFLINE ", colors.black, colors.gray)
  154.     else
  155.       draw_text(9, 3, " ONLINE ", colors.black, colors.gray)
  156.       draw_text(17, 3, " OFFLINE ", colors.gray, colors.red)
  157.     end
  158.  
  159.  
  160.     -----------FUEL---------------------
  161.     draw_text(2, 5, "Fuel Level:", colors.lightGray, colors.black)
  162.     local maxVal = fuelMax
  163.     local waste = wasteQty
  164.     local minVal = fuelQty
  165.     local medVal = waste + minVal
  166.     local percent = math.floor((minVal/maxVal)*100)
  167.     draw_text(15, 5, percent.."%", colors.white, colors.black)
  168.  
  169.     if percent < 20 then
  170.     status_bar(2, 6, 24, minVal, medVal, maxVal, colors.blue, colors.red, colors.gray)
  171.     elseif percent < 40 then
  172.     status_bar(2, 6, 24, minVal, medVal, maxVal, colors.blue, colors.orange, colors.gray)
  173.     elseif percent < 60 then
  174.     status_bar(2, 6, 24, minVal, medVal, maxVal, colors.blue, colors.yellow, colors.gray)
  175.     elseif percent <= 80 then
  176.     status_bar(2, 6, 24, minVal, medVal, maxVal, colors.blue, colors.lime, colors.gray)
  177.     elseif percent <= 100 then
  178.     status_bar(2, 6, 24, minVal, medVal, maxVal, colors.blue, colors.green, colors.gray)
  179.     end
  180.  
  181.      -----------ROD HEAT---------------
  182.     draw_text(2, 8, "Fuel Temp:", colors.lightGray, colors.black)
  183.     local maxVal = 2000
  184.     local minVal = math.floor(fuelTemp)
  185.  
  186.     if minVal < 500 then
  187.     progress_bar(2, 9, 24, minVal, maxVal, colors.cyan, colors.gray)
  188.     elseif minVal < 1000 then
  189.     progress_bar(2, 9, 24, minVal, maxVal, colors.lime, colors.gray)
  190.     elseif minVal < 1500 then  
  191.     progress_bar(2, 9, 24, minVal, maxVal, colors.yellow, colors.gray)
  192.     elseif minVal < 2000 then
  193.     progress_bar(2, 9, 24, minVal, maxVal, colors.orange, colors.gray)
  194.     elseif minVal >= 2000 then
  195.     progress_bar(2, 9, 24, 2000, maxVal, colors.red, colors.gray)
  196.     end
  197.  
  198.     draw_text(15, 8, math.floor(minVal).." C", colors.white, colors.black)
  199.  
  200.     -------------FUEL CONSUMPTION-------------------
  201.  
  202.     draw_text(2, 11, "Fuel/Tick:", colors.lightGray, colors.black)
  203.     fuel_usage = fuelTick
  204.     fuelConsumed = roundNumber(fuel_usage, 5)
  205.     draw_text(13, 11, fuelConsumed.." mB/T", colors.white, colors.black)
  206.  
  207.     -------------OUTPUT-------------------
  208.     if isCooled == "true" then
  209.       draw_text(2, 12, "mB/tick:", colors.lightGray, colors.black)
  210.       mbt = math.floor(energyTick)
  211.       draw_text(13, 12, mbt.." mB/t", colors.white, colors.black)
  212.     else
  213.       draw_text(2, 12, "RF/Tick:", colors.lightGray, colors.black)
  214.       rft = math.floor(energyTick)
  215.         rft = rfScale(rft)
  216.       draw_text(13, 12, rft, colors.white, colors.black)
  217.     end
  218.  
  219.     ------------STORAGE------------
  220.     energy_stored_percent = math.floor((energyQty/energyMax)*100)
  221.       if isCooled == "true" then
  222.       draw_text(2, 14, "mB Stored:", colors.lightGray, colors.black)
  223.     else
  224.       draw_text(2, 14, "RF Stored:", colors.lightGray, colors.black)
  225.     end
  226.     draw_text(13, 14, energy_stored_percent.."%", colors.white, colors.black)
  227.     progress_bar(2, 15, 24, energy_stored_percent, 100, colors.green, colors.gray)
  228.     ------------CONTROL RODS------------
  229.      
  230.     draw_text(2, 17, "Control Rods:", colors.lightGray, colors.black)
  231.     draw_text(16, 17, rodLevel.."%", colors.white, colors.black)
  232.     progress_bar(2, 18, 24, rodLevel, 100, colors.cyan, colors.gray)
  233. end
Add Comment
Please, Sign In to add comment