ee_ingo

Turbine Control

Feb 21st, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 21.59 KB | None | 0 0
  1. -----BigReactor Control
  2. -----by jaranvil aka jared314 fix for myFTB by ee_ingo
  3.  
  4. -----feel free to use and/or mondify this code
  5. -----http://pastebin.com/RCPEHmxs
  6. -----------------------------------------------
  7.  
  8. version = 1
  9. term.clear()
  10. -------------------FORMATTING-------------------------------
  11. function clear()
  12.         mon.setBackgroundColor(colors.black)
  13.         mon.clear()
  14.         mon.setCursorPos(1,1)
  15. end
  16.  
  17. function draw_text_term(x, y, text, text_color, bg_color)
  18.         term.setTextColor(text_color)
  19.         term.setBackgroundColor(bg_color)
  20.         term.setCursorPos(x,y)
  21.         write(text)
  22. end
  23.  
  24. function draw_text(x, y, text, text_color, bg_color)
  25.         mon.setBackgroundColor(bg_color)
  26.         mon.setTextColor(text_color)
  27.         mon.setCursorPos(x,y)
  28.         mon.write(text)
  29. end
  30.  
  31. function draw_line(x, y, length, color)
  32.                 mon.setBackgroundColor(color)
  33.                 mon.setCursorPos(x,y)
  34.                 mon.write(string.rep(" ", length))
  35. end
  36.  
  37. function draw_line_term(x, y, length, color)
  38.                 term.setBackgroundColor(color)
  39.                 term.setCursorPos(x,y)
  40.                 term.write(string.rep(" ", length))
  41. end
  42.  
  43. function progress_bar(x, y, length, minVal, maxVal, bar_color, bg_color)
  44.         draw_line(x, y, length, bg_color) --backgoround bar
  45.         local barSize = math.floor((minVal/maxVal) * length)
  46.         draw_line(x, y, barSize, bar_color)     --progress so far
  47. end
  48.  
  49. function progress_bar_term(x, y, length, minVal, maxVal, bar_color, bg_color)
  50.         draw_line_term(x, y, length, bg_color) --backgoround bar
  51.         local barSize = math.floor((minVal/maxVal) * length)
  52.         draw_line_term(x, y, barSize, bar_color)        --progress so far
  53. end
  54.  
  55. function button(x, y, length, text, txt_color, bg_color)
  56.         draw_line(x, y, length, bg_color)
  57.         draw_text((x+2), y, text, txt_color, bg_color)
  58. end
  59.  
  60. function menu_bar()
  61.         draw_line(1, 1, monX, colors.blue)
  62.         draw_text(2, 1, "Power    Tools    Settings", colors.white, colors.blue)
  63.         draw_line(1, 19, monX, colors.blue)
  64.         draw_text(2, 19, "     Turbine Control", colors.white, colors.blue)
  65. end
  66.  
  67. function power_menu()
  68.         draw_line(1, 2, 9, colors.gray)
  69.         draw_line(1, 3, 9, colors.gray)
  70.         if active then
  71.                 draw_text(2, 2, "ON", colors.lightGray, colors.gray)
  72.                 draw_text(2, 3, "OFF", colors.white, colors.gray)
  73.         else
  74.                 draw_text(2, 2, "ON", colors.white, colors.gray)
  75.                 draw_text(2, 3, "OFF", colors.lightGray, colors.gray)
  76.         end
  77. end
  78.  
  79. function tools_menu()
  80.         draw_line(10, 2, 14, colors.gray)
  81.         draw_line(10, 3, 14, colors.gray)
  82.         draw_text(11, 2, "Flow Rate", colors.white, colors.gray)
  83.        
  84.        
  85. end
  86.  
  87. function settings_menu()
  88.         draw_line(12, 2, 18, colors.gray)
  89.         draw_line(12, 3, 18, colors.gray)
  90.         draw_text(13, 2, "Check for Updates", colors.white, colors.gray)
  91.         draw_text(13, 3, "Reset peripherals", colors.white, colors.gray)
  92. end
  93.  
  94. function popup_screen(y, title, height)
  95.         clear()
  96.         menu_bar()
  97.  
  98.         draw_line(4, y, 22, colors.blue)
  99.         draw_line(25, y, 1, colors.red)
  100.  
  101.         for counter = y+1, height+y do
  102.                 draw_line(4, counter, 22, colors.white)
  103.         end
  104.  
  105.         draw_text(25, y, "X", colors.white, colors.red)
  106.         draw_text(5, y, title, colors.white, colors.blue)
  107. end
  108.  
  109. function save_config()
  110.         sw = fs.open("turbine_config.txt", "w")        
  111.                 sw.writeLine(version)
  112.                 sw.writeLine(side)
  113.                 sw.writeLine(name)
  114.                 sw.writeLine(auto_string)
  115.                 sw.writeLine(on)
  116.                 sw.writeLine(off)
  117.         sw.close()
  118. end
  119.  
  120. function load_config()
  121.         sr = fs.open("turbine_config.txt", "r")
  122.                 version = tonumber(sr.readLine())
  123.                 side = sr.readLine()
  124.                 name = sr.readLine()
  125.                 auto_string = sr.readLine()
  126.                 on = tonumber(sr.readLine())
  127.                 off = tonumber(sr.readLine())
  128.         sr.close()
  129. end
  130.  
  131. --------------------------------------------------
  132.  
  133.  
  134.  
  135. function homepage()
  136.         while true do
  137.                 clear()
  138.                 menu_bar()
  139.                 terminal_screen()
  140.  
  141.                 active = turbine.getActive()
  142.                 energy_stored = turbine.getEnergyStored()
  143.                
  144.  
  145.                 --------POWER STAT--------------
  146.                 draw_text(2, 3, "Power: ", colors.yellow, colors.black)
  147.                
  148.                 if active then
  149.                         draw_text(10, 3, "ONLINE", colors.lime, colors.black)
  150.                 else
  151.                         draw_text(10, 3, "OFFLINE", colors.red, colors.black)
  152.                 end
  153.  
  154.  
  155.                 -----------Router speed---------------------
  156.                 draw_text(2, 5, "Router Speed: ", colors.yellow, colors.black)
  157.                 local maxVal = 2000
  158.                 local minVal = math.floor(turbine.getRotorSpeed())
  159.                 draw_text(19, 5, minVal.." rpm", colors.white, colors.black)
  160.  
  161.                 if minVal < 700 then
  162.                 progress_bar(2, 6, monX-2, minVal, maxVal, colors.lightBlue, colors.gray)
  163.                 else if minVal < 900 then      
  164.                 progress_bar(2, 6, monX-2, minVal, maxVal, colors.lime, colors.gray)
  165.                 else if minVal < 1700 then
  166.                 progress_bar(2, 6, monX-2, minVal, maxVal, colors.lightBlue, colors.gray)
  167.                 else if minVal < 1900 then
  168.                 progress_bar(2, 6, monX-2, minVal, maxVal, colors.lime, colors.gray)
  169.                 else if minVal < 2000 then
  170.                 progress_bar(2, 6, monX-2, minVal, maxVal, colors.yellow, colors.gray)
  171.                 else if minVal >= 2000 then
  172.                 progress_bar(2, 6, monX-2, minVal, maxVal, colors.red, colors.gray)
  173.                 end
  174.                 end
  175.                 end
  176.                 end
  177.                 end
  178.                 end
  179.  
  180.                 -----------Steam Level---------------------
  181.                 draw_text(2, 8, "Steam Amount: ", colors.yellow, colors.black)
  182.                 local maxVal = 4000
  183.                 local minVal = math.floor(turbine.getInputAmount())
  184.                 draw_text(19, 8, minVal.." mB", colors.white, colors.black)
  185.                 progress_bar(2, 9, monX-2, minVal, maxVal, colors.lightGray, colors.gray)
  186.                
  187.  
  188.                 -----------Water Level---------------------
  189.                 draw_text(2, 11, "Water Amount: ", colors.yellow, colors.black)
  190.                 local maxVal = 4000
  191.                 local minVal = math.floor(turbine.getOutputAmount())
  192.                 draw_text(19, 11, minVal.." mB", colors.white, colors.black)
  193.                 progress_bar(2, 12, monX-2, minVal, maxVal, colors.blue, colors.gray)
  194.        
  195.  
  196.                 -------------OUTPUT-------------------
  197.                 draw_text(2, 14, "RF/tick: ", colors.yellow, colors.black)
  198.                 rft = math.floor(turbine.getEnergyProducedLastTick())
  199.                 draw_text(19, 14, rft.." RF/T", colors.white, colors.black)
  200.  
  201.                 -----------RF STORAGE---------------
  202.                 draw_text(2, 15, "RF Stored:", colors.yellow, colors.black)
  203.                 local maxVal = 1000000
  204.                 local minVal = energy_stored
  205.                 local percent = math.floor((energy_stored/maxVal)*100)
  206.                 draw_text(19, 15, percent.."%", colors.white, colors.black)
  207.  
  208.                 ------------FLOW RATE----------------
  209.                 draw_text(2, 16, "Flow Rate: ", colors.yellow, colors.black)
  210.                 flow_rate = turbine.getFluidFlowRateMax()
  211.                 draw_text(19, 16, flow_rate.." mB/t", colors.white, colors.black)
  212.  
  213.  
  214.                
  215.                 sleep(0.5)
  216.         end
  217. end
  218.  
  219. --------------MENU SCREENS--------------
  220.  
  221.  
  222.  
  223. -------------------Tools----------------------
  224.  
  225. function flow_rate_menu()
  226.         popup_screen(3, "Flow Rate", 12)
  227.         flow_rate = turbine.getFluidFlowRateMax()
  228.         current_flow = turbine.getFluidFlowRate()
  229.  
  230.         draw_text(5, 5, "Steam consumption", colors.lime, colors.white)
  231.         draw_text(5, 6, "last tick:", colors.lime, colors.white)
  232.         draw_text(13, 8, current_flow.." mB", colors.black, colors.white)
  233.  
  234.         draw_text(5, 10, "Flow Rate Setting:", colors.lime, colors.white)
  235.         draw_text(8, 12, " < ", colors.white, colors.black)
  236.         draw_text(13, 12, flow_rate.." mB", colors.black, colors.white)
  237.         draw_text(21, 12, " > ", colors.white, colors.black)
  238.  
  239.         draw_text(13, 14, " Okay ", colors.white, colors.black)
  240.  
  241.         local event, side, xPos, yPos = os.pullEvent("monitor_touch")
  242.  
  243.         --decrease
  244.         if yPos == 12 and xPos >= 8 and xPos <= 11 then
  245.                 turbine.setFluidFlowRateMax(flow_rate-10)
  246.                 flow_rate_menu()
  247.         --increase
  248.         else if yPos == 12 and xPos >= 21 and xPos <= 24 then
  249.                 turbine.setFluidFlowRateMax(flow_rate+10)
  250.                 flow_rate_menu()
  251.         end
  252.         end
  253.  
  254.         --Okay button
  255.         if yPos == 14 and xPos >= 13 and xPos <= 19 then
  256.                 call_homepage()
  257.         end
  258.  
  259.         --Exit button
  260.         if yPos == 3 and xPos == 25 then
  261.                 call_homepage()
  262.         end
  263.         flow_rate_menu()
  264. end
  265.  
  266. -----------------------Settings--------------------------------
  267.  
  268.  
  269.  
  270. function install_update(program, pastebin)
  271.  
  272.  
  273. end
  274.  
  275. function update()
  276.         popup_screen(5, "Updates", 4)
  277.         draw_text(5, 7, "Connecting to", colors.black, colors.white)
  278.         draw_text(5, 8, "pastebin...", colors.black, colors.white)
  279.  
  280.         sleep(0.5)
  281.        
  282.         shell.run("pastebin get QP3qrzNu current_version.txt")
  283.         sr = fs.open("current_version.txt", "r")
  284.         current_version = tonumber(sr.readLine())
  285.         sr.close()
  286.         fs.delete("current_version.txt")
  287.         terminal_screen()
  288.  
  289.         if current_version > version then
  290.  
  291.                 popup_screen(5, "Updates", 7)
  292.                 draw_text(5, 7, "Update Available!", colors.black, colors.white)
  293.                 draw_text(11, 9, " Intall ", colors.white, colors.black)
  294.                 draw_text(11, 11, " Ignore ", colors.white, colors.black)
  295.  
  296.                 local event, side, xPos, yPos = os.pullEvent("monitor_touch")
  297.  
  298.                 --Instatll button
  299.                 if yPos == 9 and xPos >= 11 and xPos <= 17 then
  300.                         install_update("turbine_control", "5B8h94V4")
  301.                 end
  302.  
  303.                 --Exit button
  304.                 if yPos == 5 and xPos == 25 then
  305.                         call_homepage()
  306.                 end
  307.                 call_homepage()
  308.  
  309.         else
  310.                 popup_screen(5, "Updates", 5)
  311.                 draw_text(5, 7, "You are up to date!", colors.black, colors.white)
  312.                 draw_text(11, 9, " Okay ", colors.white, colors.black)
  313.  
  314.                 local event, side, xPos, yPos = os.pullEvent("monitor_touch")
  315.  
  316.                 --Okay button
  317.                 if yPos == 9 and xPos >= 11 and xPos <= 17 then
  318.                         call_homepage()
  319.                 end
  320.  
  321.                 --Exit button
  322.                 if yPos == 5 and xPos == 25 then
  323.                         call_homepage()
  324.                 end
  325.                 call_homepage()
  326.         end
  327.  
  328.        
  329.  
  330. end
  331.  
  332. function reset_peripherals()
  333.         clear()
  334.         draw_line(4, 5, 22, colors.blue)
  335.  
  336.         for counter = 6, 10 do
  337.                 draw_line(4, counter, 22, colors.white)
  338.         end
  339.  
  340.         draw_text(5, 5, "Reset Peripherals", colors.white, colors.blue)
  341.         draw_text(5, 7, "Open computer", colors.black, colors.white)
  342.         draw_text(5, 8, "terminal.", colors.black, colors.white)
  343.         setup_wizard()
  344.  
  345. end
  346.  
  347. --stop running status screen if monitors was touched
  348. function stop()
  349.         while true do
  350.                 local event, side, xPos, yPos = os.pullEvent("monitor_touch")
  351.                         x = xPos
  352.                         y = yPos
  353.                         stop_function = "monitor_touch"
  354.                 return
  355.         end    
  356. end
  357.  
  358. function mon_touch()
  359.         --when the monitor is touch on the homepage
  360.         if y == 1 then
  361.                         if x < monX/3 then
  362.                                 power_menu()
  363.                                 local event, side, xPos, yPos = os.pullEvent("monitor_touch")
  364.                                 if xPos < 9 then
  365.                                         if yPos == 2 then
  366.                                                 turbine.setActive(true)
  367.                                                 call_homepage()
  368.                                         else if yPos == 3 then
  369.                                                 turbine.setActive(false)
  370.                                                 call_homepage()
  371.                                         else
  372.                                                 call_homepage()
  373.                                         end
  374.                                         end
  375.                                 else
  376.                                         call_homepage()
  377.                                 end
  378.                                
  379.                         else if x < 20 then
  380.                                                                tools_menu()
  381.                                 local event, side, xPos, yPos = os.pullEvent("monitor_touch")
  382.                                 if xPos < 25 and xPos > 10 then
  383.                                         if yPos == 2 then
  384.                                                 flow_rate_menu()
  385.                                        
  386.                                         else if yPos == 4 then
  387.                                                
  388.                                         else if yPos == 5 then
  389.                                        
  390.                                         else
  391.                                                 call_homepage()
  392.                                         end
  393.                                         end
  394.                                         end
  395.                                 else
  396.                                         call_homepage()
  397.                                 end
  398.                         else if x < monX then
  399.                                 settings_menu()
  400.                                 local event, side, xPos, yPos = os.pullEvent("monitor_touch")
  401.                                 if xPos > 13 then
  402.                                         if yPos == 2 then
  403.                                                 update()
  404.                                         else if yPos == 3 then
  405.                                                 reset_peripherals()    
  406.                                         else
  407.                                                 call_homepage()
  408.                                         end
  409.                                         end
  410.                                 else
  411.                                         call_homepage()
  412.                                 end
  413.                         end
  414.                         end
  415.                         end
  416.                 else
  417.                         call_homepage()
  418.                 end
  419. end
  420.  
  421. function terminal_screen()
  422.         term.clear()
  423.         draw_line_term(1, 1, 55, colors.blue)
  424.         draw_text_term(13, 1, "BigReactor Controls", colors.white, colors.blue)
  425.         draw_line_term(1, 19, 55, colors.blue)
  426.         draw_text_term(1, 19, "by jaranvil aka jared314 fix for myFTB by ee_ingo", colors.white, colors.blue)
  427.  
  428.         draw_text_term(1, 3, "Current program:", colors.white, colors.black)
  429.         draw_text_term(1, 4, "Turbine Control v"..version, colors.blue, colors.black)
  430.  
  431.         draw_text_term(1, 9, "Please give me your feedback, suggestions,", colors.white, colors.black)
  432.         draw_text_term(1, 10, "and errors!", colors.white, colors.black)
  433.  
  434.         draw_text_term(1, 11, "reddit.com/r/br_controls", colors.blue, colors.black)
  435. end
  436.  
  437. --run both homepage() and stop() until one returns
  438. function call_homepage()
  439.         clear()
  440.         parallel.waitForAny(homepage, stop)
  441.  
  442.         if stop_function == "terminal_screen" then
  443.                 stop_function = "nothing"
  444.                 setup_wizard()
  445.         else if stop_function == "monitor_touch" then
  446.                 stop_function = "nothing"
  447.                 mon_touch()
  448.         end
  449.         end
  450. end
  451.  
  452. --try to wrap peripherals
  453. --catch any errors
  454.         function test_turbine_connection()
  455.                 turbine = peripheral.wrap(name)  --wrap reactor
  456.                 c = turbine.getConnected()
  457.             if unexpected_condition then error() end  
  458.         end
  459.  
  460.         function test_monitor_connection()
  461.                 mon = peripheral.wrap(side) --wrap mon
  462.                 monX, monY = mon.getSize() --get mon size ()
  463.             if unexpected_condition then error() end  
  464.         end
  465.  
  466. --test if the entered monitor and reactor can be wrapped
  467.         function test_configs()
  468.  
  469.                 term.clear()
  470.                 draw_line_term(1, 1, 55, colors.blue)
  471.                 draw_text_term(10, 1, "BigReactor Controls v"..version, colors.white, colors.blue)
  472.                 draw_line_term(1, 19, 55, colors.blue)
  473.                 draw_text_term(1, 19, "by jaranvil aka jared314 fix for myFTB by ee_ingo", colors.white, colors.blue)
  474.  
  475.                 draw_text_term(1, 3, "Wrapping peripherals...", colors.blue, colors.black)
  476.                 draw_text_term(2, 5, "wrap montior...", colors.white, colors.black)
  477.                 sleep(0.1)
  478.                 if pcall(test_monitor_connection) then
  479.                         draw_text_term(18, 5, "success", colors.lime, colors.black)
  480.                 else
  481.                         draw_text_term(1, 4, "Error:", colors.red, colors.black)
  482.                         draw_text_term(1, 8, "Could not connect to monitor on "..side.." side", colors.red, colors.black)
  483.                         draw_text_term(1, 9, "Valid sides are 'left', 'right', 'top', 'bottom' and 'back'", colors.white, colors.black)
  484.                         draw_text_term(1, 11, "Press Enter to continue...", colors.gray, colors.black)
  485.                         wait = read()
  486.                         setup_wizard()
  487.                 end
  488.                 sleep(0.1)
  489.                 draw_text_term(2, 6, "wrap turbine...", colors.white, colors.black)
  490.                 sleep(0.1)
  491.                 if pcall(test_turbine_connection) then
  492.                         draw_text_term(18, 6, "success", colors.lime, colors.black)
  493.                 else
  494.                         draw_text_term(1, 8, "Error:", colors.red, colors.black)
  495.                         draw_text_term(1, 9, "Could not connect to "..name, colors.red, colors.black)
  496.                         draw_text_term(1, 10, "Turbine must be connected with networking cable and wired modem", colors.white, colors.black)
  497.                         draw_text_term(1, 12, "Press Enter to continue...", colors.gray, colors.black)
  498.                         wait = read()
  499.                         setup_wizard()
  500.                 end
  501.                 sleep(0.1)
  502.                 draw_text_term(2, 8, "saving settings to file...", colors.white, colors.black)
  503.  
  504.                 save_config()
  505.  
  506.                 sleep(0.1)
  507.                 draw_text_term(1, 10, "Setup Complete!", colors.lime, colors.black)    
  508.                 sleep(3)
  509.  
  510.                 auto = auto_string == "true"
  511.                 call_homepage()
  512.  
  513. end
  514. ----------------SETUP-------------------------------
  515.  
  516. function setup_wizard()
  517.         term.clear()
  518.         draw_text_term(1, 1, "BigReactor Controls v"..version, colors.lime, colors.black)
  519.         draw_text_term(1, 2, "Peripheral setup wizard", colors.white, colors.black)
  520.         draw_text_term(1, 4, "Step 1:", colors.lime, colors.black)
  521.         draw_text_term(1, 5, "-Place 3x3 advanced monitors next to computer.", colors.white, colors.black)
  522.         draw_text_term(1, 7, "Step 2:", colors.lime, colors.black)
  523.         draw_text_term(1, 8, "-Place a wired modem on this computer and on the ", colors.white, colors.black)
  524.         draw_text_term(1, 9, " computer port of the turbine.", colors.white, colors.black)
  525.         draw_text_term(1, 10, "-connect modems with network cable.", colors.white, colors.black)
  526.         draw_text_term(1, 11, "-right click modems to activate.", colors.white, colors.black)
  527.         draw_text_term(1, 13, "Press Enter when ready...", colors.gray, colors.black)
  528.        
  529.         wait = read()
  530.        
  531.         term.clear()
  532.         draw_text_term(1, 1, "Peripheral Setup", colors.lime, colors.black)
  533.         draw_text_term(1, 3, "What side is your monitor on?", colors.yellow, colors.black)
  534.  
  535.         term.setTextColor(colors.white)
  536.         term.setCursorPos(1,4)
  537.         side = read()
  538.  
  539.         term.clear()
  540.         draw_text_term(1, 1, "Peripheral Setup", colors.lime, colors.black)
  541.         draw_text_term(1, 3, "What is the turbine's name?", colors.yellow, colors.black)
  542.         draw_text_term(1, 4, "type 'default' for  'BigReactors-Turbine_0'", colors.gray, colors.black)
  543.  
  544.         term.setTextColor(colors.white)
  545.         term.setCursorPos(1,5)
  546.         name = read()
  547.  
  548.         if name == "default" then name = "BigReactors-Turbine_0" end
  549.         auto_string = false
  550.         on = 0
  551.         off = 99
  552.  
  553.         test_configs()
  554. end
  555.  
  556. function start()
  557.         --if configs exists, load values and test
  558.         if fs.exists("turbine_config.txt") then
  559.                         load_config()
  560.  
  561.                         test_configs()
  562.         else
  563.                 setup_wizard()
  564.         end
  565. end
  566.  
  567. start()
Add Comment
Please, Sign In to add comment