Advertisement
bobmarley12345

reactor_program_fixed

Mar 4th, 2022 (edited)
836
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 35.50 KB | None | 0 0
  1. ---@diagnostic disable: lowercase-global
  2. --  BigReactor Control
  3. --  by jaranvil aka jared314, and carrot :)
  4. --
  5. --  feel free to use and/or modify this code
  6. --
  7. -----------------------------------------------
  8. -- Reactor Control - Version History
  9. --
  10. --  Version 3.0 - March 04/03/2022
  11. --      fixed the infinite  parallel threads bug,
  12. --      because jaranvil though it was a great idea
  13. --      to call a parallel method, which calls itself :-)
  14. --      recursive parallel = bad
  15. -----------------------------------------------
  16. local version = 2.3
  17. -- is auto power enabled
  18. local auto_string = false
  19. -- auto on value
  20. local on = 0
  21. -- auto off value
  22. local off = 99
  23. -- is auto control rods enabled
  24. local auto_rods = false
  25. -- control rod auto value
  26. local auto_rf = 0
  27.  
  28. -- peripherals
  29. local reactor
  30. local mon
  31.  
  32. -- monitor size
  33. local monX
  34. local monY
  35.  
  36. term.clear()
  37. -------------------FORMATTING-------------------------------
  38. function cls()
  39.     mon.setBackgroundColor(colors.black)
  40.     mon.clear()
  41.     mon.setCursorPos(1, 1)
  42. end
  43.  
  44. -- display text on computer's terminal screen
  45. function gfxprint_term(x, y, text, text_color, bg_color)
  46.     term.setTextColor(text_color)
  47.     term.setBackgroundColor(bg_color)
  48.     term.setCursorPos(x, y)
  49.     write(text)
  50. end
  51.  
  52. -- display text text on monitor, "mon" peripheral
  53. function gfxprint(x, y, text, text_color, bg_color)
  54.     mon.setBackgroundColor(bg_color)
  55.     mon.setTextColor(text_color)
  56.     mon.setCursorPos(x, y)
  57.     mon.write(text)
  58. end
  59.  
  60. -- draw line on computer terminal
  61. function gfxline(x, y, length, color)
  62.     mon.setBackgroundColor(color)
  63.     mon.setCursorPos(x, y)
  64.     mon.write(string.rep(" ", length))
  65. end
  66.  
  67. -- draw line on computer terminal
  68. function gfxline_term(x, y, length, color)
  69.     term.setBackgroundColor(color)
  70.     term.setCursorPos(x, y)
  71.     term.write(string.rep(" ", length))
  72. end
  73.  
  74. -- draws two overlapping lines
  75. -- background line of bg_color
  76. -- main line of bar_color as a percentage of minVal/maxVal
  77. function gfx_progbar(x, y, length, minVal, maxVal, bar_color, bg_color)
  78.     gfxline(x, y, length, bg_color) -- backgoround bar
  79.     local barSize = math.floor((minVal / maxVal) * length)
  80.     gfxline(x, y, barSize, bar_color) -- progress so far
  81. end
  82.  
  83. -- same as above but on the computer terminal
  84. function gfx_progbar_term(x, y, length, minVal, maxVal, bar_color, bg_color)
  85.     gfxline_term(x, y, length, bg_color) -- backgoround bar
  86.     local barSize = math.floor((minVal / maxVal) * length)
  87.     gfxline_term(x, y, barSize, bar_color) -- progress so far
  88. end
  89.  
  90. -- create button on monitor
  91. function gfxbtn(x, y, length, text, txt_color, bg_color)
  92.     gfxline(x, y, length, bg_color)
  93.     gfxprint((x + 2), y, text, txt_color, bg_color)
  94. end
  95.  
  96. -- header and footer bars on monitor
  97. function drawMenuBar()
  98.     gfxline(1, 1, monX, colors.blue)
  99.     gfxprint(2, 1, "Power    Tools    Settings", colors.white, colors.blue)
  100.     gfxline(1, 19, monX, colors.blue)
  101.     gfxprint(2, 19, "     Reactor Control", colors.white, colors.blue)
  102. end
  103.  
  104. -- dropdown menu for power options
  105. function drawPowerMenu()
  106.     gfxline(1, 2, 9, colors.gray)
  107.     gfxline(1, 3, 9, colors.gray)
  108.     gfxline(1, 4, 9, colors.gray)
  109.     if active then
  110.         gfxprint(2, 2, "ON", colors.lightGray, colors.gray)
  111.         gfxprint(2, 3, "OFF", colors.white, colors.gray)
  112.     else
  113.         gfxprint(2, 2, "ON", colors.white, colors.gray)
  114.         gfxprint(2, 3, "OFF", colors.lightGray, colors.gray)
  115.     end
  116.     gfxprint(2, 4, "Auto", colors.white, colors.gray)
  117. end
  118.  
  119. -- dropbox menu for tools
  120. function drawToolMenu()
  121.     gfxline(10, 2, 14, colors.gray)
  122.     gfxline(10, 3, 14, colors.gray)
  123.     gfxline(10, 4, 14, colors.gray)
  124.     gfxline(10, 5, 14, colors.gray)
  125.     gfxprint(11, 2, "Control Rods", colors.white, colors.gray)
  126.     gfxprint(11, 3, "Efficiency", colors.white, colors.gray)
  127.     gfxprint(11, 4, "Fuel", colors.white, colors.gray)
  128.     gfxprint(11, 5, "Waste", colors.white, colors.gray)
  129. end
  130.  
  131. -- dropdown menu for settings
  132. function drawSettingsMenu()
  133.     gfxline(12, 2, 18, colors.gray)
  134.     gfxline(12, 3, 18, colors.gray)
  135.     gfxprint(13, 2, "Check for Updates", colors.white, colors.gray)
  136.     gfxprint(13, 3, "Reset peripherals", colors.white, colors.gray)
  137. end
  138.  
  139. -- basic popup screen with title bar and exit button
  140. function drawPopupScreen(y, title, height)
  141.     cls()
  142.     drawMenuBar()
  143.  
  144.     gfxline(4, y, 22, colors.blue)
  145.     gfxline(25, y, 1, colors.red)
  146.  
  147.     for counter = y + 1, height + y do
  148.         gfxline(4, counter, 22, colors.white)
  149.     end
  150.  
  151.     gfxprint(25, y, "X", colors.white, colors.red)
  152.     gfxprint(5, y, title, colors.white, colors.blue)
  153. end
  154.  
  155. -- write settings to config file
  156. function save_conf()
  157.     sw = fs.open("config.txt", "w")
  158.     sw.writeLine(version)
  159.     sw.writeLine(auto_string)
  160.     sw.writeLine(on)
  161.     sw.writeLine(off)
  162.     sw.writeLine(auto_rods)
  163.     sw.writeLine(auto_rf)
  164.     sw.close()
  165. end
  166.  
  167. -- read settings from file
  168. function load_conf()
  169.     sr = fs.open("config.txt", "r")
  170.     version = tonumber(sr.readLine())
  171.     auto_string = sr.readLine()
  172.     on = tonumber(sr.readLine())
  173.     off = tonumber(sr.readLine())
  174.     auto_rods = sr.readLine()
  175.     auto_rf = tonumber(sr.readLine())
  176.     sr.close()
  177. end
  178.  
  179. ------------------------END FORMATTING--------------------------
  180.  
  181. --
  182. function homepage()
  183.     while true do
  184.         cls()
  185.         drawMenuBar()
  186.         terminfo()
  187.  
  188.         energy_stored = reactor.getEnergyStored()
  189.  
  190.         --------POWER STAT--------------
  191.         gfxprint(2, 3, "Power:", colors.yellow, colors.black)
  192.         active = reactor.getActive()
  193.         if active then
  194.             gfxprint(10, 3, "ONLINE", colors.lime, colors.black)
  195.         else
  196.             gfxprint(10, 3, "OFFLINE", colors.red, colors.black)
  197.         end
  198.  
  199.         -----------FUEL---------------------
  200.         gfxprint(2, 5, "Fuel Level:", colors.yellow, colors.black)
  201.         local maxVal = reactor.getFuelAmountMax()
  202.         local minVal = reactor.getFuelAmount()
  203.         local percent = math.floor((minVal / maxVal) * 100)
  204.         gfxprint(15, 5, percent .. "%", colors.white, colors.black)
  205.  
  206.         if percent < 25 then
  207.             gfx_progbar(2, 6, monX - 2, minVal, maxVal, colors.red, colors.gray)
  208.         else
  209.             if percent < 50 then
  210.                 gfx_progbar(2, 6, monX - 2, minVal, maxVal, colors.orange, colors.gray)
  211.             else
  212.                 if percent < 75 then
  213.                     gfx_progbar(2, 6, monX - 2, minVal, maxVal, colors.yellow, colors.gray)
  214.                 else
  215.                     if percent <= 100 then
  216.                         gfx_progbar(2, 6, monX - 2, minVal, maxVal, colors.lime, colors.gray)
  217.                     end
  218.                 end
  219.             end
  220.         end
  221.  
  222.         -----------ROD HEAT---------------
  223.         gfxprint(2, 8, "Fuel Temp:", colors.yellow, colors.black)
  224.         local maxVal = 2000
  225.         local minVal = math.floor(reactor.getFuelTemperature())
  226.  
  227.         if minVal < 500 then
  228.             gfx_progbar(2, 9, monX - 2, minVal, maxVal, colors.lime, colors.gray)
  229.         else
  230.             if minVal < 1000 then
  231.                 gfx_progbar(2, 9, monX - 2, minVal, maxVal, colors.yellow, colors.gray)
  232.             else
  233.                 if minVal < 1500 then
  234.                     gfx_progbar(2, 9, monX - 2, minVal, maxVal, colors.orange, colors.gray)
  235.                 else
  236.                     if minVal < 2000 then
  237.                         gfx_progbar(2, 9, monX - 2, minVal, maxVal, colors.red, colors.gray)
  238.                     else
  239.                         if minVal >= 2000 then
  240.                             gfx_progbar(2, 9, monX - 2, 2000, maxVal, colors.red, colors.gray)
  241.                         end
  242.                     end
  243.                 end
  244.             end
  245.         end
  246.  
  247.         gfxprint(15, 8, math.floor(minVal) .. "/" .. maxVal, colors.white, colors.black)
  248.  
  249.         -----------CASING HEAT---------------
  250.         gfxprint(2, 11, "Casing Temp:", colors.yellow, colors.black)
  251.         local maxVal = 2000
  252.         local minVal = math.floor(reactor.getCasingTemperature())
  253.         if minVal < 500 then
  254.             gfx_progbar(2, 12, monX - 2, minVal, maxVal, colors.lime, colors.gray)
  255.         else
  256.             if minVal < 1000 then
  257.                 gfx_progbar(2, 12, monX - 2, minVal, maxVal, colors.yellow, colors.gray)
  258.             else
  259.                 if minVal < 1500 then
  260.                     gfx_progbar(2, 12, monX - 2, minVal, maxVal, colors.orange, colors.gray)
  261.                 else
  262.                     if minVal < 2000 then
  263.                         gfx_progbar(2, 12, monX - 2, minVal, maxVal, colors.red, colors.gray)
  264.                     else
  265.                         if minVal >= 2000 then
  266.                             gfx_progbar(2, 12, monX - 2, 2000, maxVal, colors.red, colors.gray)
  267.                         end
  268.                     end
  269.                 end
  270.             end
  271.         end
  272.         gfxprint(15, 11, math.floor(minVal) .. "/" .. maxVal, colors.white, colors.black)
  273.  
  274.         -------------OUTPUT-------------------
  275.         if reactor.isActivelyCooled() then
  276.  
  277.             gfxprint(2, 14, "mB/tick:", colors.yellow, colors.black)
  278.             mbt = math.floor(reactor.getHotFluidProducedLastTick())
  279.             gfxprint(13, 14, mbt .. " mB/t", colors.white, colors.black)
  280.  
  281.         else
  282.  
  283.             gfxprint(2, 14, "RF/tick:", colors.yellow, colors.black)
  284.             rft = math.floor(reactor.getEnergyProducedLastTick())
  285.             gfxprint(13, 14, rft .. " RF/T", colors.white, colors.black)
  286.  
  287.         end
  288.  
  289.         ------------STORAGE------------
  290.         if reactor.isActivelyCooled() then
  291.  
  292.             gfxprint(2, 15, "mB Stored:", colors.yellow, colors.black)
  293.             fluid_stored = reactor.getHotFluidAmount()
  294.             fluid_max = reactor.getHotFluidAmountMax()
  295.             fluid_stored_percent = math.floor((fluid_stored / fluid_max) * 100)
  296.             gfxprint(13, 15, fluid_stored_percent .. "% (" .. fluid_stored .. " mB)", colors.white, colors.black)
  297.  
  298.         else
  299.  
  300.             gfxprint(2, 15, "RF Stored:", colors.yellow, colors.black)
  301.             energy_stored_percent = math.floor((energy_stored / 10000000) * 100)
  302.             gfxprint(13, 15, energy_stored_percent .. "% (" .. energy_stored .. " RF)", colors.white, colors.black)
  303.  
  304.         end
  305.  
  306.         -------------AUTO CONTROL RODS-----------------------
  307.         auto_rods_bool = auto_rods == "true"
  308.         insertion_percent = reactor.getControlRodLevel(0)
  309.  
  310.         if reactor.isActivelyCooled() then
  311.             gfxprint(2, 16, "Control Rods:", colors.yellow, colors.black)
  312.             gfxprint(16, 16, insertion_percent .. "%", colors.white, colors.black)
  313.         else
  314.  
  315.             if auto_rods_bool then
  316.                 if active then
  317.                     if rft > auto_rf + 50 then
  318.                         reactor.setAllControlRodLevels(insertion_percent + 1)
  319.                     else
  320.                         if rft < auto_rf - 50 then
  321.                             reactor.setAllControlRodLevels(insertion_percent - 1)
  322.                         end
  323.                     end
  324.                 end
  325.  
  326.                 gfxprint(2, 16, "Control Rods:", colors.yellow, colors.black)
  327.                 gfxprint(16, 16, insertion_percent .. "%", colors.white, colors.black)
  328.                 gfxprint(21, 16, "(Auto)", colors.red, colors.black)
  329.  
  330.             else
  331.                 gfxprint(2, 16, "Control Rods:", colors.yellow, colors.black)
  332.                 gfxprint(16, 16, insertion_percent .. "%", colors.white, colors.black)
  333.             end
  334.         end
  335.  
  336.         -------------AUTO SHUTOFF--------------------------
  337.         if reactor.isActivelyCooled() then
  338.  
  339.             -- i dont know what I should do here
  340.  
  341.         else
  342.             auto = auto_string == "true"
  343.             if auto then
  344.                 if active then
  345.                     gfxprint(2, 17, "Auto off:", colors.yellow, colors.black)
  346.                     gfxprint(13, 17, off .. "% RF Stored", colors.white, colors.black)
  347.                     if energy_stored_percent >= off then
  348.                         reactor.setActive(false)
  349.                         stop_function = "recursive"
  350.                         return
  351.                     end
  352.                 else
  353.                     gfxprint(2, 17, "Auto on:", colors.yellow, colors.black)
  354.                     gfxprint(13, 17, on .. "% RF Stored", colors.white, colors.black)
  355.                     if energy_stored_percent <= on then
  356.                         reactor.setActive(true)
  357.                         stop_function = "recursive"
  358.                         return
  359.                     end
  360.                 end
  361.             else
  362.                 gfxprint(2, 17, "Auto power:", colors.yellow, colors.black)
  363.                 gfxprint(14, 17, "disabled", colors.red, colors.black)
  364.             end
  365.         end
  366.  
  367.         sleep(0.5)
  368.     end
  369. end
  370.  
  371. --------------MENU SCREENS--------------
  372.  
  373. -- auto power menu
  374. function auto_off()
  375.  
  376.     auto = auto_string == "true"
  377.     if auto then -- auto power enabled
  378.  
  379.         drawPopupScreen(3, "Auto Power", 11)
  380.         gfxprint(5, 5, "Enabled", colors.lime, colors.white)
  381.         gfxprint(15, 5, " disable ", colors.white, colors.black)
  382.  
  383.         gfxprint(5, 7, "ON when storage =", colors.gray, colors.white)
  384.         gfxprint(5, 8, " - ", colors.white, colors.black)
  385.         gfxprint(13, 8, on .. "% RF", colors.black, colors.white)
  386.         gfxprint(22, 8, " + ", colors.white, colors.black)
  387.  
  388.         gfxprint(5, 10, "OFF when storage =", colors.gray, colors.white)
  389.         gfxprint(5, 11, " - ", colors.white, colors.black)
  390.         gfxprint(13, 11, off .. "% RF", colors.black, colors.white)
  391.         gfxprint(22, 11, " + ", colors.white, colors.black)
  392.  
  393.         gfxprint(11, 13, " Save ", colors.white, colors.black)
  394.  
  395.         local event, side, xPos, yPos = os.pullEvent("monitor_touch")
  396.  
  397.         -- disable auto
  398.         if yPos == 5 then
  399.             if xPos >= 15 and xPos <= 21 then
  400.                 auto_string = "false"
  401.                 save_conf()
  402.                 auto_off()
  403.             else
  404.                 auto_off()
  405.             end
  406.         end
  407.  
  408.         -- increase/decrease auto on %
  409.         if yPos == 8 then
  410.             if xPos >= 5 and xPos <= 8 then
  411.                 previous_on = on
  412.                 on = on - 1
  413.             end
  414.             if xPos >= 22 and xPos <= 25 then
  415.                 previous_on = on
  416.                 on = on + 1
  417.             end
  418.         end
  419.  
  420.         -- increase/decrease auto off %
  421.         if yPos == 11 then
  422.             if xPos >= 5 and xPos <= 8 then
  423.                 previous_off = off
  424.                 off = off - 1
  425.             end
  426.             if xPos >= 22 and xPos <= 25 then
  427.                 previous_off = off
  428.                 off = off + 1
  429.             end
  430.         end
  431.  
  432.         if on < 0 then
  433.             on = 0
  434.         end
  435.         if off > 99 then
  436.             off = 99
  437.         end
  438.  
  439.         if on == off or on > off then
  440.             on = previous_on
  441.             off = previous_off
  442.             drawPopupScreen(5, "Error", 6)
  443.             gfxprint(5, 7, "Auto On value must be", colors.black, colors.white)
  444.             gfxprint(5, 8, "lower then auto off", colors.black, colors.white)
  445.             gfxprint(11, 10, "Okay", colors.white, colors.black)
  446.             local event, side, xPos, yPos = os.pullEvent("monitor_touch")
  447.  
  448.             auto_off()
  449.         end
  450.  
  451.         -- Okay button
  452.         if yPos == 13 and xPos >= 11 and xPos <= 17 then
  453.             save_conf()
  454.             run_app()
  455.         end
  456.  
  457.         -- Exit button
  458.         if yPos == 3 and xPos == 25 then
  459.             run_app()
  460.         end
  461.  
  462.         auto_off()
  463.     else
  464.         drawPopupScreen(3, "Auto Power", 5)
  465.         gfxprint(5, 5, "Disabled", colors.red, colors.white)
  466.         gfxprint(15, 5, " enable ", colors.white, colors.gray)
  467.         gfxprint(11, 7, "Okay", colors.white, colors.black)
  468.  
  469.         local event, side, xPos, yPos = os.pullEvent("monitor_touch")
  470.  
  471.         -- Okay button
  472.         if yPos == 7 and xPos >= 11 and xPos <= 17 then
  473.             run_app()
  474.         end
  475.  
  476.         if yPos == 5 then
  477.             if xPos >= 15 and xPos <= 21 then
  478.                 auto_string = "true"
  479.                 save_conf()
  480.                 auto_off()
  481.             else
  482.                 auto_off()
  483.             end
  484.         else
  485.             auto_off()
  486.         end
  487.     end
  488. end
  489.  
  490. -- efficiency menu
  491. function efficiency()
  492.     drawPopupScreen(3, "Efficiency", 12)
  493.     fuel_usage = reactor.getFuelConsumedLastTick()
  494.     rft = math.floor(reactor.getEnergyProducedLastTick())
  495.  
  496.     rfmb = rft / fuel_usage
  497.  
  498.     gfxprint(5, 5, "Fuel Consumption: ", colors.lime, colors.white)
  499.     gfxprint(5, 6, fuel_usage .. " mB/t", colors.black, colors.white)
  500.     gfxprint(5, 8, "Energy per mB: ", colors.lime, colors.white)
  501.     gfxprint(5, 9, rfmb .. " RF/mB", colors.black, colors.white)
  502.  
  503.     gfxprint(5, 11, "RF/tick:", colors.lime, colors.white)
  504.     gfxprint(5, 12, rft .. " RF/T", colors.black, colors.white)
  505.  
  506.     gfxprint(11, 14, " Okay ", colors.white, colors.black)
  507.  
  508.     local event, side, xPos, yPos = os.pullEvent("monitor_touch")
  509.  
  510.     -- Okay button
  511.     if yPos == 14 and xPos >= 11 and xPos <= 17 then
  512.         run_app()
  513.     end
  514.  
  515.     -- Exit button
  516.     if yPos == 3 and xPos == 25 then
  517.         run_app()
  518.     end
  519.  
  520.     efficiency()
  521. end
  522.  
  523. function fuel()
  524.     drawPopupScreen(3, "Fuel", 9)
  525.  
  526.     fuel_max = reactor.getFuelAmountMax()
  527.     fuel_level = reactor.getFuelAmount()
  528.     fuel_reactivity = math.floor(reactor.getFuelReactivity())
  529.  
  530.     gfxprint(5, 5, "Fuel Level: ", colors.lime, colors.white)
  531.     gfxprint(5, 6, fuel_level .. "/" .. fuel_max, colors.black, colors.white)
  532.  
  533.     gfxprint(5, 8, "Reactivity: ", colors.lime, colors.white)
  534.     gfxprint(5, 9, fuel_reactivity .. "%", colors.black, colors.white)
  535.  
  536.     gfxprint(11, 11, " Okay ", colors.white, colors.black)
  537.  
  538.     local event, side, xPos, yPos = os.pullEvent("monitor_touch")
  539.  
  540.     -- Okay button
  541.     if yPos == 11 and xPos >= 11 and xPos <= 17 then
  542.         run_app()
  543.     end
  544.  
  545.     -- Exit button
  546.     if yPos == 3 and xPos == 25 then
  547.         run_app()
  548.     end
  549.  
  550.     fuel()
  551. end
  552.  
  553. function waste()
  554.     drawPopupScreen(3, "Waste", 8)
  555.  
  556.     waste_amount = reactor.getWasteAmount()
  557.     gfxprint(5, 5, "Waste Amount: ", colors.lime, colors.white)
  558.     gfxprint(5, 6, waste_amount .. " mB", colors.black, colors.white)
  559.     gfxprint(8, 8, " Eject Waste ", colors.white, colors.red)
  560.     gfxprint(11, 10, " Close ", colors.white, colors.black)
  561.  
  562.     local event, side, xPos, yPos = os.pullEvent("monitor_touch")
  563.  
  564.     -- eject button
  565.     if yPos == 8 and xPos >= 8 and xPos <= 21 then
  566.         reactor.doEjectWaste()
  567.         drawPopupScreen(5, "Waste Eject", 5)
  568.         gfxprint(5, 7, "Waste Ejeceted.", colors.black, colors.white)
  569.         gfxprint(11, 9, " Close ", colors.white, colors.black)
  570.         local event, side, xPos, yPos = os.pullEvent("monitor_touch")
  571.         -- Okay button
  572.         if yPos == 7 and xPos >= 11 and xPos <= 17 then
  573.             run_app()
  574.         end
  575.  
  576.         -- Exit button
  577.         if yPos == 3 and xPos == 25 then
  578.             run_app()
  579.         end
  580.     end
  581.  
  582.     -- Okay button
  583.     if yPos == 10 and xPos >= 11 and xPos <= 17 then
  584.         run_app()
  585.     end
  586.  
  587.     -- Exit button
  588.     if yPos == 3 and xPos == 25 then
  589.         run_app()
  590.     end
  591.     waste()
  592. end
  593.  
  594. function set_auto_rf()
  595.     drawPopupScreen(5, "Auto Adjust", 11)
  596.     gfxprint(5, 7, "Try to maintain:", colors.black, colors.white)
  597.  
  598.     gfxprint(13, 9, " ^ ", colors.white, colors.gray)
  599.     gfxprint(10, 11, auto_rf .. " RF/t", colors.black, colors.white)
  600.     gfxprint(13, 13, " v ", colors.white, colors.gray)
  601.     gfxprint(11, 15, " Okay ", colors.white, colors.gray)
  602.  
  603.     local event, side, xPos, yPos = os.pullEvent("monitor_touch")
  604.  
  605.     -- increase button
  606.     if yPos == 9 then
  607.         auto_rf = auto_rf + 100
  608.         save_conf()
  609.         set_auto_rf()
  610.     end
  611.  
  612.     -- decrease button
  613.     if yPos == 13 then
  614.         auto_rf = auto_rf - 100
  615.         if auto_rf < 0 then
  616.             auto_rf = 0
  617.         end
  618.         save_conf()
  619.         set_auto_rf()
  620.     end
  621.  
  622.     if yPos == 15 then
  623.         control_rods()
  624.     end
  625.  
  626.     set_auto_rf()
  627. end
  628.  
  629. function control_rods()
  630.  
  631.     if reactor.isActivelyCooled() then
  632.  
  633.         drawPopupScreen(3, "Control Rods", 13)
  634.         insertion_percent = reactor.getControlRodLevel(0)
  635.  
  636.         gfxprint(5, 5, "Inserted: " .. insertion_percent .. "%", colors.black, colors.white)
  637.         gfx_progbar(5, 7, 20, insertion_percent, 100, colors.yellow, colors.gray)
  638.  
  639.         gfxprint(5, 9, " << ", colors.white, colors.black)
  640.         gfxprint(10, 9, " < ", colors.white, colors.black)
  641.         gfxprint(17, 9, " > ", colors.white, colors.black)
  642.         gfxprint(21, 9, " >> ", colors.white, colors.black)
  643.  
  644.         gfxprint(5, 11, "Auto:", colors.black, colors.white)
  645.         gfxprint(5, 13, "unavilable for", colors.red, colors.white)
  646.         gfxprint(5, 14, "active cooling", colors.red, colors.white)
  647.  
  648.         gfxprint(11, 16, " Close ", colors.white, colors.gray)
  649.  
  650.         local event, side, xPos, yPos = os.pullEvent("monitor_touch")
  651.  
  652.         if yPos == 9 and xPos >= 5 and xPos <= 15 then
  653.             reactor.setAllControlRodLevels(insertion_percent - 10)
  654.         end
  655.  
  656.         if yPos == 9 and xPos >= 10 and xPos <= 13 then
  657.             reactor.setAllControlRodLevels(insertion_percent - 1)
  658.         end
  659.  
  660.         if yPos == 9 and xPos >= 17 and xPos <= 20 then
  661.             reactor.setAllControlRodLevels(insertion_percent + 1)
  662.         end
  663.  
  664.         if yPos == 9 and xPos >= 21 and xPos <= 25 then
  665.             reactor.setAllControlRodLevels(insertion_percent + 10)
  666.         end
  667.  
  668.         ------Close button-------
  669.         if yPos == 16 and xPos >= 11 and xPos <= 17 then
  670.             run_app()
  671.         end
  672.  
  673.         ------Exit button------------
  674.         if yPos == 5 and xPos == 25 then
  675.             run_app()
  676.         end
  677.         control_rods()
  678.  
  679.     else
  680.  
  681.         drawPopupScreen(3, "Control Rods", 13)
  682.         insertion_percent = reactor.getControlRodLevel(0)
  683.  
  684.         gfxprint(5, 5, "Inserted: " .. insertion_percent .. "%", colors.black, colors.white)
  685.         gfx_progbar(5, 7, 20, insertion_percent, 100, colors.yellow, colors.gray)
  686.  
  687.         gfxprint(5, 9, " << ", colors.white, colors.black)
  688.         gfxprint(10, 9, " < ", colors.white, colors.black)
  689.         gfxprint(17, 9, " > ", colors.white, colors.black)
  690.         gfxprint(21, 9, " >> ", colors.white, colors.black)
  691.  
  692.         gfxprint(5, 11, "Auto:", colors.black, colors.white)
  693.         gfxprint(16, 11, " disable ", colors.white, colors.black)
  694.  
  695.         auto_rods_bool = auto_rods == "true"
  696.         if auto_rods_bool then
  697.  
  698.             gfxprint(5, 13, "RF/t: " .. auto_rf, colors.black, colors.white)
  699.             gfxprint(18, 13, " set ", colors.white, colors.black)
  700.         else
  701.             gfxprint(16, 11, " enable ", colors.white, colors.black)
  702.             gfxprint(5, 13, "disabled", colors.red, colors.white)
  703.         end
  704.  
  705.         gfxprint(11, 15, " Close ", colors.white, colors.gray)
  706.  
  707.         local event, side, xPos, yPos = os.pullEvent("monitor_touch")
  708.  
  709.         -----manual adjust buttons------------
  710.         if yPos == 9 and xPos >= 5 and xPos <= 15 then
  711.             reactor.setAllControlRodLevels(insertion_percent - 10)
  712.         end
  713.  
  714.         if yPos == 9 and xPos >= 10 and xPos <= 13 then
  715.             reactor.setAllControlRodLevels(insertion_percent - 1)
  716.         end
  717.  
  718.         if yPos == 9 and xPos >= 17 and xPos <= 20 then
  719.             reactor.setAllControlRodLevels(insertion_percent + 1)
  720.         end
  721.  
  722.         if yPos == 9 and xPos >= 21 and xPos <= 25 then
  723.             reactor.setAllControlRodLevels(insertion_percent + 10)
  724.         end
  725.  
  726.         ------auto buttons-----------------
  727.         if yPos == 11 and xPos >= 16 then
  728.             if auto_rods_bool then
  729.                 auto_rods = "false"
  730.                 save_conf()
  731.                 control_rods()
  732.             else
  733.                 auto_rods = "true"
  734.                 save_conf()
  735.                 control_rods()
  736.             end
  737.         end
  738.  
  739.         if yPos == 13 and xPos >= 18 then
  740.             set_auto_rf()
  741.         end
  742.  
  743.         ------Close button-------
  744.         if yPos == 15 and xPos >= 11 and xPos <= 17 then
  745.             run_app()
  746.         end
  747.  
  748.         ------Exit button------------
  749.         if yPos == 5 and xPos == 25 then
  750.             run_app()
  751.         end
  752.         control_rods()
  753.  
  754.     end
  755. end
  756.  
  757. -----------------------Settings--------------------------------
  758.  
  759. function rf_mode()
  760.     wait = read()
  761. end
  762.  
  763. function steam_mode()
  764.     wait = read()
  765. end
  766.  
  767. function install_update(program, pastebin)
  768.     cls()
  769.     gfxline(4, 5, 22, colors.blue)
  770.  
  771.     for counter = 6, 10 do
  772.         gfxline(4, counter, 22, colors.white)
  773.     end
  774.  
  775.     gfxprint(5, 5, "Updating...", colors.white, colors.blue)
  776.     gfxprint(5, 7, "Open computer", colors.black, colors.white)
  777.     gfxprint(5, 8, "terminal.", colors.black, colors.white)
  778.  
  779.     if fs.exists("install") then
  780.         fs.delete("install")
  781.     end
  782.     shell.run("pastebin get p4zeq7Ma install")
  783.     shell.run("install")
  784. end
  785.  
  786. function update()
  787.     drawPopupScreen(5, "Updates", 4)
  788.     gfxprint(5, 7, "Connecting to", colors.black, colors.white)
  789.     gfxprint(5, 8, "pastebin...", colors.black, colors.white)
  790.  
  791.     sleep(0.5)
  792.  
  793.     shell.run("pastebin get MkF2QQjH current_version.txt")
  794.     sr = fs.open("current_version.txt", "r")
  795.     current_version = tonumber(sr.readLine())
  796.     sr.close()
  797.     fs.delete("current_version.txt")
  798.     terminfo()
  799.  
  800.     if current_version > version then
  801.  
  802.         drawPopupScreen(5, "Updates", 7)
  803.         gfxprint(5, 7, "Update Available!", colors.black, colors.white)
  804.         gfxprint(11, 9, " Install ", colors.white, colors.black)
  805.         gfxprint(11, 11, " Ignore ", colors.white, colors.black)
  806.  
  807.         local event, side, xPos, yPos = os.pullEvent("monitor_touch")
  808.  
  809.         -- Instatll button
  810.         if yPos == 9 and xPos >= 11 and xPos <= 17 then
  811.             install_update()
  812.         end
  813.  
  814.         -- Exit button
  815.         if yPos == 5 and xPos == 25 then
  816.             stop_function = "updated"
  817.             return
  818.         end
  819.  
  820.         stop_function = "updated"
  821.         return
  822.  
  823.     else
  824.         drawPopupScreen(5, "Updates", 5)
  825.         gfxprint(5, 7, "You are up to date!", colors.black, colors.white)
  826.         gfxprint(11, 9, " Okay ", colors.white, colors.black)
  827.  
  828.         local event, side, xPos, yPos = os.pullEvent("monitor_touch")
  829.  
  830.         -- Okay button
  831.         if yPos == 9 and xPos >= 11 and xPos <= 17 then
  832.             stop_function = "updated"
  833.             return
  834.         end
  835.  
  836.         -- Exit button
  837.         if yPos == 5 and xPos == 25 then
  838.             stop_function = "updated"
  839.             return
  840.         end
  841.        
  842.         stop_function = "updated"
  843.         return
  844.     end
  845.  
  846. end
  847.  
  848. function reset_peripherals()
  849.     cls()
  850.     gfxline(4, 5, 22, colors.blue)
  851.  
  852.     for counter = 6, 10 do
  853.         gfxline(4, counter, 22, colors.white)
  854.     end
  855.  
  856.     gfxprint(5, 5, "Reset Peripherals", colors.white, colors.blue)
  857.     gfxprint(5, 7, "Open computer", colors.black, colors.white)
  858.     gfxprint(5, 8, "terminal.", colors.black, colors.white)
  859.     setup_wizard()
  860. end
  861.  
  862. -- stop running status screen if monitors was touched
  863. function stop()
  864.     while true do
  865.         local event, side, xPos, yPos = os.pullEvent("monitor_touch")
  866.         x = xPos
  867.         y = yPos
  868.         stop_function = "monitor_touch"
  869.         return
  870.     end
  871. end
  872.  
  873. function mon_touch()
  874.     -- when the monitor is touch on the homepage
  875.     if y == 1 then
  876.         if x < monX / 3 then
  877.             drawPowerMenu()
  878.             local event, side, xPos, yPos = os.pullEvent("monitor_touch")
  879.             if xPos < 9 then
  880.                 if yPos == 2 then
  881.                     reactor.setActive(true)
  882.                     timer = 0 -- reset anytime the reactor is turned on/off
  883.                     stop_function = "touched"
  884.                     return
  885.                 else
  886.                     if yPos == 3 then
  887.                         reactor.setActive(false)
  888.                         timer = 0 -- reset anytime the reactor is turned on/off
  889.                         stop_function = "touched"
  890.                         return
  891.                     else
  892.                         if yPos == 4 then
  893.                             auto_off()
  894.                         else
  895.                             stop_function = "touched"
  896.                             return
  897.                         end
  898.                     end
  899.                 end
  900.             else
  901.                 stop_function = "touched"
  902.                 return
  903.             end
  904.  
  905.         else
  906.             if x < 20 then
  907.                 drawToolMenu()
  908.                 local event, side, xPos, yPos = os.pullEvent("monitor_touch")
  909.                 if xPos < 25 and xPos > 10 then
  910.                     if yPos == 2 then
  911.                         control_rods()
  912.                     else
  913.                         if yPos == 3 then
  914.                             efficiency()
  915.                         else
  916.                             if yPos == 4 then
  917.                                 fuel()
  918.                             else
  919.                                 if yPos == 5 then
  920.                                     waste()
  921.                                 else
  922.                                     stop_function = "touched"
  923.                                     return
  924.                                 end
  925.                             end
  926.                         end
  927.                     end
  928.                 else
  929.                     stop_function = "touched"
  930.                     return
  931.                 end
  932.             else
  933.                 if x < monX then
  934.                     drawSettingsMenu()
  935.                     local event, side, xPos, yPos = os.pullEvent("monitor_touch")
  936.                     if xPos > 13 then
  937.                         if yPos == 2 then
  938.                             update()
  939.                             if (stop_function == "updated") then
  940.                                 return
  941.                             end
  942.                         else
  943.                             if yPos == 3 then
  944.                                 reset_peripherals()
  945.                             else
  946.                                 stop_function = "touched"
  947.                                 return
  948.                             end
  949.                         end
  950.                     else
  951.                         stop_function = "touched"
  952.                         return
  953.                     end
  954.                 end
  955.             end
  956.         end
  957.     else
  958.         stop_function = "touched"
  959.         return
  960.     end
  961. end
  962.  
  963. function terminfo()
  964.     term.clear()
  965.     gfxline_term(1, 1, 55, colors.blue)
  966.     gfxprint_term(13, 1, "BigReactor Controls", colors.white, colors.blue)
  967.     gfxline_term(1, 19, 55, colors.blue)
  968.     gfxprint_term(6, 19, "by jaranvil aka jared314, and carrot :)", colors.white, colors.blue)
  969.  
  970.     gfxprint_term(1, 3, "Current program:", colors.white, colors.black)
  971.     gfxprint_term(1, 4, "Reactor Control v3.0", colors.blue, colors.black)
  972.  
  973.     gfxprint_term(1, 6, "Please give me your feedback, suggestions,", colors.white, colors.black)
  974.     gfxprint_term(1, 7, "and errors!", colors.white, colors.black)
  975. end
  976.  
  977. -- run both homepage() and stop() until one returns
  978. function run_app()
  979.     while true do
  980.         cls()
  981.         parallel.waitForAny(homepage, stop)
  982.    
  983.         if stop_function == "terminal_screen" then
  984.             stop_function = "nothing"
  985.             setup_wizard()
  986.         elseif stop_function == "monitor_touch" then
  987.             stop_function = "nothing"
  988.             mon_touch()
  989.             if (stop_function == "touched") then
  990.                 stop_function = "nothing"
  991.             end
  992.         elseif stop_function == "recursive" then
  993.             stop_function = "nothing"
  994.         end
  995.     end
  996. end
  997.  
  998. -- test if the entered monitor and reactor can be wrapped
  999. function init_app()
  1000.     term.clear()
  1001.  
  1002.     gfxline_term(1, 1, 55, colors.blue)
  1003.     gfxprint_term(10, 1, "BigReactors Controls", colors.white, colors.blue)
  1004.  
  1005.     gfxline_term(1, 19, 55, colors.blue)
  1006.     gfxprint_term(3, 19, "by jaranvil aka jared314, and carrot :)", colors.white, colors.blue)
  1007.    
  1008.     gfxprint_term(1, 3, "Searching for a peripherals...", colors.white, colors.black)
  1009.     reactor = find_reactor()
  1010.     gfxprint_term(2, 5, "Connecting to reactor... ", colors.white, colors.black)
  1011.     while (reactor == nil) do
  1012.         gfxprint_term(1, 8, "Error:", colors.red, colors.black)
  1013.         gfxprint_term(1, 9, "Could not connect to reactor", colors.red, colors.black)
  1014.         gfxprint_term(1, 10, "Reactor must be connected with networking cable", colors.white, colors.black)
  1015.         gfxprint_term(1, 11, "and modems or the computer is directly beside", colors.white, colors.black)
  1016.         gfxprint_term(1, 12, "the reactors computer port.", colors.white, colors.black)
  1017.         gfxprint_term(1, 14, "Press Enter to continue...", colors.gray, colors.black)
  1018.         wait = read()
  1019.         reactor = find_reactor()
  1020.     end
  1021.  
  1022.     gfxprint_term(27, 5, "success", colors.lime, colors.black)
  1023.     sleep(0.5)
  1024.  
  1025.     gfxprint_term(2, 6, "Connecting to monitor...", colors.white, colors.black)
  1026.     sleep(0.5)
  1027.     mon = find_mon()
  1028.     while (mon == nil) do
  1029.         gfxprint_term(1, 7, "Error:", colors.red, colors.black)
  1030.         gfxprint_term(1, 8, "Could not connect to a monitor. Place a 3x3 advanced monitor", colors.red, colors.black)
  1031.         gfxprint_term(1, 11, "Press Enter to continue...", colors.gray, colors.black)
  1032.         wait = read()
  1033.         mon = find_mon()
  1034.     end
  1035.  
  1036.     monX, monY = mon.getSize()
  1037.     gfxprint_term(27, 6, "success", colors.lime, colors.black)
  1038.     sleep(0.5)
  1039.     gfxprint_term(2, 7, "saving configuration...", colors.white, colors.black)
  1040.  
  1041.     save_conf()
  1042.  
  1043.     sleep(0.1)
  1044.     gfxprint_term(1, 9, "Setup Complete!", colors.lime, colors.black)
  1045.     sleep(1)
  1046.  
  1047.     auto = auto_string == "true"
  1048.     run_app()
  1049. end
  1050. ----------------SETUP-------------------------------
  1051.  
  1052. function setup_wizard()
  1053.     term.clear()
  1054.  
  1055.     gfxprint_term(1, 1, "BigReactor Controls v" .. version, colors.lime, colors.black)
  1056.     gfxprint_term(1, 2, "Peripheral setup", colors.white, colors.black)
  1057.     gfxprint_term(1, 4, "Step 1:", colors.lime, colors.black)
  1058.     gfxprint_term(1, 5, "-Place 3x3 advanced monitors next to computer.", colors.white, colors.black)
  1059.     gfxprint_term(1, 7, "Step 2:", colors.lime, colors.black)
  1060.     gfxprint_term(1, 8, "-Place a wired modem on this computer and on the ", colors.white, colors.black)
  1061.     gfxprint_term(1, 9, " computer port of the reactor.", colors.white, colors.black)
  1062.     gfxprint_term(1, 10, "-connect modems with network cable.", colors.white, colors.black)
  1063.     gfxprint_term(1, 11, "-right click modems to activate.", colors.white, colors.black)
  1064.     gfxprint_term(1, 13, "Press Enter when ready...", colors.gray, colors.black)
  1065.  
  1066.     wait = read()
  1067.     init_app()
  1068. end
  1069.  
  1070. function find_reactor()
  1071.     local names = peripheral.getNames()
  1072.     local i, name
  1073.     for i, name in pairs(names) do
  1074.         if peripheral.getType(name) == "BigReactors-Reactor" then
  1075.             return peripheral.wrap(name)
  1076.         else
  1077.             -- return nil
  1078.         end
  1079.     end
  1080. end
  1081.  
  1082. function find_mon()
  1083.     local names = peripheral.getNames()
  1084.     local i, name
  1085.     for i, name in pairs(names) do
  1086.         if peripheral.getType(name) == "monitor" then
  1087.             test = name
  1088.             return peripheral.wrap(name)
  1089.         else
  1090.             -- return nil
  1091.         end
  1092.     end
  1093. end
  1094.  
  1095. function start()
  1096.     -- if configs exists, load values and test
  1097.     if fs.exists("config.txt") then
  1098.         load_conf()
  1099.         init_app()
  1100.     else
  1101.         setup_wizard()
  1102.     end
  1103. end
  1104.  
  1105. start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement