Sparkybearbomb

Wireless Reactor Receiver

Dec 23rd, 2020 (edited)
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 14.46 KB | None | 0 0
  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. --
  11. -- Version 1.1 - 26.12.2020
  12. -- - original program forked from ELJptBRk
  13. --
  14.  
  15. --peripherals
  16. local modem
  17. local mon
  18.  
  19. local version = 3.0
  20.  
  21. --monitor size
  22. local monX
  23. local monY
  24.  
  25. term.clear()
  26. -------------------FORMATTING-------------------------------
  27. function clear()
  28.   mon.setBackgroundColor(colors.black)
  29.   mon.clear()
  30.   mon.setCursorPos(1,1)
  31. end
  32.  
  33. --display text on computer's terminal screen
  34. function draw_text_term(x, y, text, text_color, bg_color)
  35.   term.setTextColor(text_color)
  36.   term.setBackgroundColor(bg_color)
  37.   term.setCursorPos(x,y)
  38.   write(text)
  39. end
  40.  
  41. --display text text on monitor, "mon" peripheral
  42. function draw_text(x, y, text, text_color, bg_color)
  43.   mon.setBackgroundColor(bg_color)
  44.   mon.setTextColor(text_color)
  45.   mon.setCursorPos(x,y)
  46.   mon.write(text)
  47. end
  48.  
  49. --draw line on computer terminal
  50. function draw_line(x, y, length, color)
  51.     mon.setBackgroundColor(color)
  52.     mon.setCursorPos(x,y)
  53.     mon.write(string.rep(" ", length))
  54. end
  55.  
  56. --draw line on computer terminal
  57. function draw_line_term(x, y, length, color)
  58.     term.setBackgroundColor(color)
  59.     term.setCursorPos(x,y)
  60.     term.write(string.rep(" ", length))
  61. end
  62.  
  63. -- Round Decimal Numbers
  64. function roundNumber(num, n)
  65.   local mult = 10^(n or 0)
  66.   return math.floor(num * mult + 0.5) / mult
  67. end
  68.  
  69. --create progress bar
  70. --draws two overlapping lines
  71. --background line of bg_color
  72. --main line of bar_color as a percentage of minVal/maxVal
  73. function progress_bar(x, y, length, minVal, maxVal, bar_color, bg_color)
  74.   draw_line(x, y, length, bg_color) --backgoround bar
  75.   local barSize = math.floor((minVal/maxVal) * length)
  76.   draw_line(x, y, barSize, bar_color) --progress so far
  77. end
  78.  
  79. --create status bar
  80. --draws three overlapping lines
  81. --background line of bg_color
  82. function status_bar(x, y, length, minVal, medVal, maxVal, bar_color1, bar_color2, bg_color)
  83.   draw_line(x, y, length, bg_color) --backgoround bar
  84.   local barSize1 = math.floor((medVal/maxVal) * length)
  85.   local barSize2 = math.floor((minVal/maxVal) * length)
  86.   draw_line(x, y, barSize1, bar_color1) --progress so far
  87.   draw_line(x, y, barSize2, bar_color2) --progress so far
  88. end
  89.  
  90. --same as above but on the computer terminal
  91. function progress_bar_term(x, y, length, minVal, maxVal, bar_color, bg_color)
  92.   draw_line_term(x, y, length, bg_color) --backgoround bar
  93.   local barSize = math.floor((minVal/maxVal) * length)
  94.   draw_line_term(x, y, barSize, bar_color)  --progress so far
  95. end
  96.  
  97. --header and footer bars on monitor
  98. function menu_bar()
  99.   draw_line(1, 1, monX, colors.purple)
  100.   draw_text(2, 1, " Wireless Reactor Receiver", colors.white, colors.purple)
  101.   draw_line(1, 26, monX, colors.purple)
  102.   draw_text(2, 26, " Wireless Reactor Receiver", colors.white, colors.purple)
  103. end
  104.  
  105.  
  106. --basic popup screen with title bar and exit button
  107. function popup_screen(y, title, height)
  108.   clear()
  109.   menu_bar()
  110.  
  111.   draw_line(4, y, 22, colors.purple)
  112.   draw_line(25, y, 1, colors.red)
  113.  
  114.   for counter = y+1, height+y do
  115.     draw_line(4, counter, 22, colors.white)
  116.   end
  117.  
  118.   draw_text(25, y, "X", colors.white, colors.red)
  119.   draw_text(5, y, title, colors.white, colors.purple)
  120. end
  121.  
  122. --write settings to config file
  123. function save_config()
  124.   sw = fs.open("config.txt", "w")  
  125.     sw.writeLine(version)
  126.     sw.writeLine(auto_string)
  127.     sw.writeLine(on)
  128.     sw.writeLine(off)
  129.     sw.writeLine(auto_rods)
  130.     sw.writeLine(auto_rf)
  131.   sw.close()
  132. end
  133.  
  134. --read settings from file
  135. function load_config()
  136.   sr = fs.open("config.txt", "r")
  137.     version = tonumber(sr.readLine())
  138.     auto_string = sr.readLine()
  139.     on = tonumber(sr.readLine())
  140.     off = tonumber(sr.readLine())
  141.     auto_rods = sr.readLine()
  142.     auto_rf = tonumber(sr.readLine())
  143.   sr.close()
  144. end
  145.  
  146. ------------------------END FORMATTING--------------------------
  147.  
  148. -- peripheral searching thanks to /u/kla_sch
  149. function modemSearch()
  150.    local names = peripheral.getNames()
  151.    local i, name
  152.    for i, name in pairs(names) do
  153.       if peripheral.getType(name) == "modem" then
  154.          return peripheral.wrap(name)
  155.       else
  156.          --return null
  157.       end
  158.    end
  159. end
  160.  
  161. function monitorSearch()
  162.    local names = peripheral.getNames()
  163.    local i, name
  164.    for i, name in pairs(names) do
  165.       if peripheral.getType(name) == "monitor" then
  166.         test = name
  167.          return peripheral.wrap(name)
  168.       else
  169.          --return null
  170.       end
  171.    end
  172. end
  173.  
  174. function pharseMessage(message)
  175.     local results = {}
  176.     for pharseData in string.gmatch(message, "[^, ]+") do
  177.         table.insert(results,pharseData)
  178.     end
  179.     active = results[1]
  180.     numRod = results[2]
  181.     energyStored = results[3]
  182.     fuelTemp = results[4]
  183.     caseTemp = results[5]
  184.     fuelQty = results[6]
  185.     wasteQty = results[7]
  186.     fuelMax = results[8]
  187.     rodLevel = results[9]
  188.     energyTick = results[10]
  189.     fluidTick = results[11]
  190.     coolType = results[12]
  191.     coolQty = results[13]
  192.     fluidType = results[14]
  193.     react = results[15]
  194.     fuelTick = results[16]
  195.     isCooled = results[17]
  196.     sleep(pharseSleep)
  197.     call_homepage()
  198. end
  199.  
  200. -----------------------Settings--------------------------------
  201.  
  202. function rf_mode()
  203.   wait = read()
  204. end
  205.  
  206. function steam_mode()
  207.   wait = read()
  208. end
  209.  
  210.  
  211. function terminal_screen()
  212.   term.clear()
  213.   draw_line_term(1, 1, 55, colors.purple)
  214.   draw_text_term(13, 1, "BigReactor Controls", colors.white, colors.purple)
  215.   draw_line_term(1, 19, 55, colors.purple)
  216.   draw_text_term(13, 19, "by SparkyBearBomb", colors.white, colors.purple)
  217.  
  218.   draw_text_term(1, 3, "Current program:", colors.white, colors.black)
  219.   draw_text_term(1, 4, "Reactor Control v"..version, colors.blue, colors.black)
  220.  
  221.   draw_text_term(1, 6, "Installer:", colors.white, colors.black)
  222.   draw_text_term(1, 7, "pastebin.com/ELJptBRk", colors.blue, colors.black)
  223.  
  224.   draw_text_term(1, 9, "Please give me your feedback, suggestions,", colors.white, colors.black)
  225.   draw_text_term(1, 10, "and errors!", colors.white, colors.black)
  226.  
  227.   draw_text_term(1, 11, "http://pastebin.com/u/Sparkybearbomb", colors.blue, colors.black)
  228. end
  229.  
  230. --run both homepage() and stop() until one returns
  231. function call_homepage()
  232.   if stop_function == "terminal_screen" then
  233.     stop_function = "nothing"
  234.     setup_wizard()
  235.   else if stop_function == "monitor_touch" then
  236.     stop_function = "nothing"
  237.     mon_touch()
  238.   end
  239.   end
  240. end
  241.  
  242. --test if the entered monitor and reactor can be wrapped
  243. function test_configs()
  244.   term.clear()
  245.  
  246.   draw_line_term(1, 1, 55, colors.purple)
  247.   draw_text_term(10, 1, "BigReactors Controls", colors.white, colors.purple)
  248.  
  249.   draw_line_term(1, 19, 55, colors.purple)
  250.   draw_text_term(10, 19, "by SparkyBearBomb", colors.white, colors.purple)
  251.  
  252.   draw_text_term(1, 3, "Searching for a peripherals...", colors.white, colors.black)
  253.   sleep(1)
  254.  
  255.   modem = modemSearch()
  256.   mon = monitorSearch()
  257.  
  258.   modem = modemSearch()
  259.   port = 16969
  260.   modem.closeAll()
  261.   modem.open(port)
  262.  
  263.   draw_text_term(2, 5, "Connecting to reactor...", colors.white, colors.black)
  264.   sleep(0.5)
  265.   if modem == null then
  266.       draw_text_term(1, 8, "Error:", colors.red, colors.black)
  267.       draw_text_term(1, 9, "Could not connect to reactor transmitter", colors.red, colors.black)
  268.       draw_text_term(1, 10, "a Wireless Modem must be connected to the computer", colors.white, colors.black)
  269.       draw_text_term(1, 12, "Press Enter to continue...", colors.gray, colors.black)
  270.       wait = read()
  271.       setup_wizard()
  272.   else
  273.       draw_text_term(27, 5, "success", colors.green, colors.black)
  274.       sleep(0.5)
  275.   end
  276.  
  277.   draw_text_term(2, 6, "Connecting to monitor...", colors.white, colors.black)
  278.   sleep(0.5)
  279.   if mon == null then
  280.       draw_text_term(1, 7, "Error:", colors.red, colors.black)
  281.       draw_text_term(1, 8, "Could not connect to a monitor. Place a 3x4 advanced monitor", colors.red, colors.black)
  282.       draw_text_term(1, 11, "Press Enter to continue...", colors.gray, colors.black)
  283.       wait = read()
  284.       setup_wizard()
  285.   else
  286.       monX, monY = mon.getSize()
  287.       draw_text_term(27, 6, "success", colors.green, colors.black)
  288.       sleep(0.5)
  289.   end
  290.     draw_text_term(2, 7, "saving configuration...", colors.white, colors.black)  
  291.  
  292.     save_config()
  293.  
  294.     sleep(0.1)
  295.     draw_text_term(1, 9, "Setup Complete!", colors.green, colors.black)
  296.     sleep(1)
  297.  
  298.     auto = auto_string == "true"
  299. end
  300. ----------------SETUP-------------------------------
  301.  
  302. function setup_wizard()
  303.  
  304.   term.clear()
  305.  
  306.      draw_text_term(1, 1, "BigReactor Controls v"..version, colors.green, colors.black)
  307.      draw_text_term(1, 2, "Peripheral setup", colors.white, colors.black)
  308.      draw_text_term(1, 4, "Step 1:", colors.green, colors.black)
  309.      draw_text_term(1, 5, "-Place 3 wide x 4 tall advanced monitors next to computer.", colors.white, colors.black)
  310.      draw_text_term(1, 7, "Step 2:", colors.green, colors.black)
  311.      draw_text_term(1, 8, "-Place a wireless modem on this computer", colors.white, colors.black)
  312.      draw_text_term(1, 10, "Press Enter when ready...", colors.gray, colors.black)
  313.    
  314.      wait = read()
  315.      test_configs()
  316. end
  317.  
  318. function start()
  319.   --if configs exists, load values and test
  320.   if fs.exists("config.txt") then
  321.       load_config()
  322.       test_configs()
  323.   else
  324.     setup_wizard()
  325.   end
  326. end
  327.  
  328. start()
  329.  
  330. while true do
  331.     event, side, frequency, replyFrequency, message, distance = os.pullEvent("modem_message")
  332.     pharseMessage(message)
  333.     clear()
  334.     menu_bar()
  335.     terminal_screen()
  336.    
  337.     --------POWER STAT--------------
  338.     draw_text(2, 3, "Power:", colors.lightGray, colors.black)
  339.     if active == "true" then
  340.       draw_text(9, 3, " ONLINE ", colors.gray, colors.green)
  341.       draw_text(17, 3, " OFFLINE ", colors.black, colors.gray)
  342.     else
  343.       draw_text(9, 3, " ONLINE ", colors.black, colors.gray)
  344.       draw_text(17, 3, " OFFLINE ", colors.gray, colors.red)
  345.     end
  346.  
  347.  
  348.     -----------FUEL---------------------
  349.     draw_text(2, 5, "Fuel Level:", colors.lightGray, colors.black)
  350.     local maxVal = fuelMax
  351.     local waste = wasteQty
  352.     local minVal = fuelQty
  353.     local medVal = waste + minVal
  354.     local percent = math.floor((minVal/maxVal)*100)
  355.     draw_text(15, 5, percent.."%", colors.white, colors.black)
  356.  
  357.     if percent < 20 then
  358.     status_bar(2, 6, monX-2, minVal, medVal, maxVal, colors.blue, colors.red, colors.gray)
  359.     else if percent < 40 then
  360.     status_bar(2, 6, monX-2, minVal, medVal, maxVal, colors.blue, colors.orange, colors.gray)
  361.     else if percent < 60 then
  362.     status_bar(2, 6, monX-2, minVal, medVal, maxVal, colors.blue, colors.yellow, colors.gray)
  363.     else if percent <= 80 then
  364.     status_bar(2, 6, monX-2, minVal, medVal, maxVal, colors.blue, colors.lime, colors.gray)
  365.     else if percent <= 100 then
  366.     status_bar(2, 6, monX-2, minVal, medVal, maxVal, colors.blue, colors.green, colors.gray)
  367.     end
  368.     end
  369.     end
  370.     end
  371.     end
  372.  
  373.      -----------ROD HEAT---------------
  374.     draw_text(2, 8, "Fuel Temp:", colors.lightGray, colors.black)
  375.     local maxVal = 2000
  376.     local minVal = math.floor(fuelTemp)
  377.  
  378.     if minVal < 500 then
  379.     progress_bar(2, 9, monX-2, minVal, maxVal, colors.cyan, colors.gray)
  380.     else if minVal < 1000 then
  381.     progress_bar(2, 9, monX-2, minVal, maxVal, colors.lime, colors.gray)
  382.     else if minVal < 1500 then  
  383.     progress_bar(2, 9, monX-2, minVal, maxVal, colors.yellow, colors.gray)
  384.     else if minVal < 2000 then
  385.     progress_bar(2, 9, monX-2, minVal, maxVal, colors.orange, colors.gray)
  386.     else if minVal >= 2000 then
  387.       progress_bar(2, 9, monX-2, 2000, maxVal, colors.red, colors.gray)
  388.     end
  389.     end
  390.     end
  391.     end
  392.     end
  393.  
  394.     draw_text(15, 8, math.floor(minVal).." C", colors.white, colors.black)
  395.  
  396.     -----------CASING HEAT---------------
  397.     draw_text(2, 11, "Casing Temp:", colors.lightGray, colors.black)
  398.     local maxVal = 2000
  399.     local minVal = math.floor(caseTemp)
  400.     if minVal < 500 then
  401.     progress_bar(2, 12, monX-2, minVal, maxVal, colors.cyan, colors.gray)
  402.     else if minVal < 1000 then
  403.     progress_bar(2, 12, monX-2, minVal, maxVal, colors.lime, colors.gray)
  404.     else if minVal < 1500 then  
  405.     progress_bar(2, 12, monX-2, minVal, maxVal, colors.yellow, colors.gray)
  406.     else if minVal < 2000 then
  407.     progress_bar(2, 12, monX-2, minVal, maxVal, colors.orange, colors.gray)
  408.     else if minVal >= 2000 then
  409.       progress_bar(2, 12, monX-2, 2000, maxVal, colors.red, colors.gray)
  410.     end
  411.     end
  412.     end
  413.     end
  414.     end
  415.     draw_text(15, 11, math.floor(minVal).." C", colors.white, colors.black)
  416.  
  417.     -------------FUEL CONSUMPTION-------------------
  418.  
  419.     draw_text(2, 14, "Fuel/Tick:", colors.lightGray, colors.black)
  420.     fuel_usage = fuelTick
  421.     fuelConsumed = roundNumber(fuel_usage, 5)
  422.     draw_text(13, 14, fuelConsumed.." mB/T", colors.white, colors.black)
  423.  
  424.     -------------OUTPUT-------------------
  425.     if isCooled == "true" then
  426.  
  427.       draw_text(2, 16, "mB/tick:", colors.lightGray, colors.black)
  428.       mbt = math.floor(fluidTick)
  429.       draw_text(13, 16, mbt.." mB/t", colors.white, colors.black)
  430.  
  431.     else
  432.  
  433.       draw_text(2, 16, "RF/Tick:", colors.lightGray, colors.black)
  434.       rft = math.floor(energyTick)
  435.       draw_text(13, 16, rft.." RF/T", colors.white, colors.black)
  436.  
  437.     end
  438.  
  439.     ------------STORAGE------------
  440.     energy_stored = energyStored
  441.     if isCooled == "true" then
  442.  
  443.       draw_text(2, 18, "mB Stored:", colors.lightGray, colors.black)
  444.       fluid_stored = coolQty
  445.       fluid_max = 10000000
  446.       fluid_stored_percent = math.floor((fluid_stored/fluid_max)*100)
  447.       draw_text(13, 18, fluid_stored_percent.."% ("..fluid_stored.." mB)", colors.white, colors.black)
  448.  
  449.     else
  450.  
  451.       draw_text(2, 18, "RF Stored:", colors.lightGray, colors.black)
  452.       energy_stored_percent = math.floor((energy_stored/10000000)*100)
  453.       local maxVal = 100
  454.       local minVal = energy_stored_percent
  455.       draw_text(13, 18, energy_stored_percent.."% ("..energy_stored.." RF)", colors.white, colors.black)
  456.       progress_bar(2, 19, monX-2, minVal, maxVal, colors.green, colors.gray)
  457.     end
  458.     ------------CONTROL RODS------------
  459.      
  460.       draw_text(2, 21, "Control Rods:", colors.lightGray, colors.black)
  461.       draw_text(16, 21, rodLevel.."%", colors.white, colors.black)
  462.       local maxVal = 100
  463.       local minVal = rodLevel
  464.       progress_bar(2, 22, monX-2, minVal, maxVal, colors.cyan, colors.gray)
  465. end
  466.  
Advertisement
Add Comment
Please, Sign In to add comment