Advertisement
MJRLegends

Turbine(Big Reactors) Management(Monitor Edition)

Dec 13th, 2015
474
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 14.14 KB | None | 0 0
  1. ----------- Made BY MJRLegends (Please dont claim as your own code) -----------
  2. version = "2.3.0"
  3.  
  4. mouseWidth = 0
  5. mouseHeight = 0
  6.  
  7. currentTurbine = 1
  8. setting = false
  9. control = false
  10. monitorFound = false
  11.  
  12. turbines = {}
  13. turbinesManagement = {}
  14. turbine = null
  15.  
  16. function draw_line(x, y, length, color)
  17.     monitor.setBackgroundColor(color)
  18.     monitor.setCursorPos(x,y)
  19.     monitor.write(string.rep(" ", length))
  20. end
  21.  
  22. function progress_bar(x, y, length, minVal, maxVal, bar_color, bg_color)
  23.     draw_line(x, y, length, bg_color) --backgoround bar
  24.     local barSize = math.floor((minVal/maxVal) * length)
  25.     draw_line(x, y, barSize, bar_color)     --progress so far
  26. end
  27.  
  28. function progress_bar_multi_line(x, y, length, minVal, maxVal, bar_color, bg_color, numberOFLine)
  29.     local barSize = math.floor((minVal/maxVal) * length)
  30.     y = y - 1
  31.     for i=1,numberOFLine,1 do
  32.         draw_line(x, y + i, length, bg_color) --background bar
  33.         draw_line(x, y + i, barSize, bar_color)     --progress so far
  34.     end
  35. end
  36.  
  37. function draw_text(x, y, text, text_color, bg_color)
  38.     monitor.setBackgroundColor(bg_color)
  39.     monitor.setTextColor(text_color)
  40.     monitor.setCursorPos(x,y)
  41.     monitor.write(text)
  42. end
  43.  
  44. function terminalOutput()
  45.     term.clear()
  46.     term.setBackgroundColor(colors.black)
  47.    
  48.     term.setTextColor(colors.blue)
  49.     term.setCursorPos(1,1)
  50.     term.write("MJRLegends Turbine Management (Monitor Edition)")
  51.    
  52.     term.setTextColor(colors.lime)
  53.     term.setCursorPos(1,2)
  54.     term.write("Version: " .. version)
  55.    
  56.     term.setTextColor(colors.red)
  57.     term.setCursorPos(1,3)
  58.     term.write("Note: Monitors are optional")
  59.    
  60.     term.setTextColor(colors.cyan)
  61.     term.setCursorPos(1,5)
  62.     term.write("Peripheral Connected:")
  63. end
  64.  
  65. function initPeripherals()
  66.     local perList = peripheral.getNames()
  67.     local yLevel = 6
  68.     local turbineNumber = 1
  69.    
  70.     turbines = {}
  71.     turbinesManagement = {}
  72.     monitorFound = false
  73.     term.setTextColor(colors.yellow)
  74.     for i=1,#perList,1 do
  75.         if peripheral.getType(perList[i]) == "monitor" and monitorFound == false then
  76.             monitor = peripheral.wrap(perList[i])
  77.             term.setCursorPos(1,yLevel)
  78.             term.write("Monitor Found!")
  79.             yLevel = yLevel + 1
  80.             monitorFound = true
  81.         end
  82.         if peripheral.getType(perList[i]) == "BigReactors-Turbine" then
  83.             table.insert(turbines, turbineNumber, perList[i])
  84.             table.insert(turbinesManagement, turbineNumber, true)
  85.             turbineNumber = turbineNumber +1
  86.         end
  87.     end
  88.     term.setCursorPos(1,yLevel)
  89.     term.write((turbineNumber - 1) .. " Turbine Found!")
  90.     turbine = peripheral.wrap(turbines[currentTurbine])
  91. end
  92.  
  93. function drawMainScreen()
  94.     displayW,displayH=monitor.getSize()
  95.     monitor.clear()
  96.     monitor.setTextColour(colours.blue)
  97.     monitor.setCursorPos(5,1)
  98.     monitor.write("MJRLegends Turbine Management")
  99.    
  100.     monitor.setCursorPos((displayW / 2)-2,2)
  101.     monitor.write("v"..version)
  102.    
  103.     monitor.setTextColour(colours.blue)
  104.     monitor.setCursorPos(0,3)
  105.     monitor.write(string.rep("-", displayW))
  106.     monitor.setTextColour(colours.white)
  107.    
  108.     numberOfActiveTurbines = 0
  109.     totalBufferedEnergy = 0
  110.     totalBufferedSteam = 0
  111.     totalBufferedWater = 0
  112.     for i=1,#turbines,1 do
  113.         tempTurbine = peripheral.wrap(turbines[i])
  114.         if tempTurbine.getActive() then
  115.             numberOfActiveTurbines = numberOfActiveTurbines + 1
  116.         end
  117.         totalBufferedEnergy = totalBufferedEnergy + tempTurbine.getEnergyStored()
  118.         totalBufferedSteam = totalBufferedSteam + tempTurbine.getInputAmount()
  119.         totalBufferedWater = totalBufferedWater + tempTurbine.getOutputAmount()
  120.     end
  121.    
  122.     monitor.setCursorPos(1,4)
  123.     monitor.write("Number of Online Turbines: " .. numberOfActiveTurbines .. "/" .. table.getn(turbines))
  124.    
  125.     monitor.setTextColour(colours.blue)
  126.     monitor.setCursorPos(0,5)
  127.     monitor.write(string.rep("-", displayW))
  128.     monitor.setTextColour(colours.white)
  129.    
  130.     monitor.setCursorPos(1,7)
  131.     monitor.setTextColour(colours.yellow)
  132.     monitor.write("Total Stored Energy: " .. totalBufferedEnergy .. " RF")
  133.     progress_bar_multi_line(2, 8, displayW-2, totalBufferedEnergy, (1000000 * table.getn(turbines)), colors.red, colors.gray, 2)
  134.     monitor.setBackgroundColor(colours.black)
  135.     monitor.setCursorPos(1,11)
  136.     monitor.write("Total Stored Steam: " .. totalBufferedSteam .. " mb")
  137.     progress_bar_multi_line(2, 12, displayW-2, totalBufferedSteam, (4000 * table.getn(turbines)), colors.lightGray, colors.gray, 2)
  138.     monitor.setBackgroundColor(colours.black)
  139.     monitor.setBackgroundColor(colours.black)
  140.     monitor.setCursorPos(1,15)
  141.     monitor.write("Total Stored Water: " .. totalBufferedWater .. " mb")
  142.     progress_bar_multi_line(2, 16, displayW-2, totalBufferedWater, (4000 * table.getn(turbines)), colors.blue, colors.gray, 2)
  143.     monitor.setBackgroundColor(colours.black)
  144.    
  145.     monitor.setTextColour(colours.white)
  146.     monitor.setBackgroundColor(colours.blue)
  147.     monitor.setCursorPos(1,displayH - 0)
  148.     monitor.write("                                        ")
  149.     monitor.setCursorPos(1,displayH - 1)
  150.     monitor.write("             Control Screen             ")
  151.     monitor.setCursorPos(1,displayH - 2)
  152.     monitor.write("                                        ")
  153.     monitor.setBackgroundColor(colours.black)
  154. end
  155.  
  156. function drawControlScreen()
  157.     displayW,displayH=monitor.getSize()
  158.     monitor.clear()
  159.     active = turbine.getActive()
  160.     energy_stored = turbine.getEnergyStored()
  161.  
  162.     monitor.clear()
  163.     monitor.setCursorPos(1,1)
  164.     monitor.setTextColour(colours.blue)
  165.     monitor.write("MJRLegends Turbine Management")
  166.     monitor.setTextColour(colours.white)
  167.     w,h=monitor.getSize()
  168.     -----------Turbine ON/OFF---------------------
  169.     monitor.setCursorPos(1,3)
  170.     monitor.write("Turbine: ")
  171.     if active then
  172.             monitor.setBackgroundColour(colours.blue)
  173.     else
  174.             monitor.setBackgroundColour(colours.grey)
  175.     end
  176.     monitor.setCursorPos(20,3)
  177.     monitor.write(" ON  ")
  178.  
  179.     if active then
  180.             monitor.setBackgroundColour(colours.grey)
  181.     else
  182.             monitor.setBackgroundColour(colours.blue)
  183.     end
  184.     monitor.setCursorPos(25,3)
  185.     monitor.write(" OFF ")
  186.     monitor.setBackgroundColour(colours.black) 
  187.    
  188.     -------------Multi Turbine Support Controls-------------------
  189.     monitor.setBackgroundColour(colours.blue)
  190.     monitor.setCursorPos(displayW - 8,1)
  191.     monitor.write("<")
  192.     monitor.setCursorPos(displayW - 5,1)
  193.     monitor.write(">")
  194.     monitor.setCursorPos(displayW - 3,1)
  195.     monitor.setBackgroundColour(colours.black)
  196.     monitor.setTextColour(colours.yellow)
  197.     monitor.write("".. currentTurbine)
  198.    
  199.     -----------Section Lines---------------------
  200.     displayW,displayH=monitor.getSize()
  201.    
  202.     monitor.setTextColour(colours.blue)
  203.     monitor.setCursorPos(0,2)
  204.     monitor.write(string.rep("-", displayW))
  205.     monitor.setTextColour(colours.white)
  206.    
  207.     monitor.setTextColour(colours.blue)
  208.     monitor.setCursorPos(0,4)
  209.     monitor.write(string.rep("-", displayW))
  210.     monitor.setTextColour(colours.white)
  211.    
  212.     monitor.setTextColour(colours.blue)
  213.     monitor.setCursorPos(0,displayH - 2)
  214.     monitor.write(string.rep("-", displayW))
  215.     monitor.setTextColour(colours.white)
  216.    
  217.     monitor.setTextColour(colours.blue)
  218.     monitor.setCursorPos(0,displayH - 0)
  219.     monitor.write(string.rep("-", displayW))
  220.     monitor.setTextColour(colours.white)
  221.    
  222.     -----------Bottom Bar Buttons---------------------
  223.     if setting then
  224.         monitor.setBackgroundColour(colours.grey)
  225.     else
  226.         monitor.setBackgroundColour(colours.blue)
  227.     end
  228.     monitor.setCursorPos(displayW - 10,displayH - 1)
  229.     monitor.write(" Settings ")
  230.     monitor.setBackgroundColour(colours.black)
  231.    
  232.     monitor.setBackgroundColour(colours.green)
  233.     monitor.setCursorPos(2,displayH - 1)
  234.     monitor.write(" Main Menu ")
  235. end
  236.  
  237. function drawDisplayScreen()
  238.     displayW,displayH=monitor.getSize()
  239.    
  240.     -----------Router speed---------------------
  241.     draw_text(2, 5, "Routor Speed: ", colors.yellow, colors.black)
  242.     local maxVal = 2000
  243.     local minVal = math.floor(turbine.getRotorSpeed())
  244.     draw_text(19, 5, minVal.." rpm", colors.white, colors.black)
  245.  
  246.     if minVal < 700 then
  247.     progress_bar(2, 6, displayW-2, minVal, maxVal, colors.lightBlue, colors.gray)
  248.     elseif minVal < 900 then      
  249.     progress_bar(2, 6, displayW-2, minVal, maxVal, colors.lime, colors.gray)
  250.     elseif minVal < 1700 then
  251.     progress_bar(2, 6, displayW-2, minVal, maxVal, colors.lightBlue, colors.gray)
  252.     elseif minVal < 1900 then
  253.     progress_bar(2, 6, displayW-2, minVal, maxVal, colors.lime, colors.gray)
  254.     elseif minVal < 2000 then
  255.     progress_bar(2, 6, displayW-2, minVal, maxVal, colors.yellow, colors.gray)
  256.     elseif minVal >= 2000 then
  257.     progress_bar(2, 6, displayW-2, minVal, maxVal, colors.red, colors.gray)
  258.     end
  259.  
  260.     -----------Steam Level---------------------
  261.     draw_text(2, 8, "Steam Amount: ", colors.yellow, colors.black)
  262.     local maxVal = 4000
  263.     local minVal = math.floor(turbine.getInputAmount())
  264.     draw_text(19, 8, minVal.." mB", colors.white, colors.black)
  265.     progress_bar(2, 9, displayW-2, minVal, maxVal, colors.lightGray, colors.gray)
  266.  
  267.  
  268.     -----------Water Level---------------------
  269.     draw_text(2, 11, "Water Amount: ", colors.yellow, colors.black)
  270.     local maxVal = 4000
  271.     local minVal = math.floor(turbine.getOutputAmount())
  272.     draw_text(19, 11, minVal.." mB", colors.white, colors.black)
  273.     progress_bar(2, 12, displayW-2, minVal, maxVal, colors.blue, colors.gray)
  274.  
  275.  
  276.     -------------OUTPUT-------------------
  277.     draw_text(2, 14, "RF/tick: ", colors.yellow, colors.black)
  278.     rft = math.floor(turbine.getEnergyProducedLastTick())
  279.     draw_text(19, 14, rft.." RF/T", colors.white, colors.black)
  280.  
  281.     -----------RF STORAGE---------------
  282.     draw_text(2, 15, "RF Stored:", colors.yellow, colors.black)
  283.     local maxVal = 1000000
  284.     local minVal = energy_stored
  285.     local percent = math.floor((energy_stored/maxVal)*100)
  286.     draw_text(19, 15, percent.."%", colors.white, colors.black)
  287.  
  288.     ------------FLOW RATE----------------
  289.     draw_text(2, 16, "Flow Rate: ", colors.yellow, colors.black)
  290.     flow_rate = turbine.getFluidFlowRateMax()
  291.     draw_text(19, 16, flow_rate.." mB/t", colors.white, colors.black)
  292.  
  293.     ------------COILS---------------------------
  294.     engaged = turbine.getInductorEngaged()
  295.     draw_text(2, 17, "Coils: ", colors.yellow, colors.black)
  296.  
  297.     if engaged then
  298.         draw_text(19, 17, "Engaged", colors.white, colors.black)
  299.     else
  300.         draw_text(19, 17, "Disengaged", colors.white, colors.black)
  301.     end
  302. end
  303.  
  304. function drawSettingScreen()
  305.     displayW,displayH=monitor.getSize()
  306.    
  307.     -------------Title-------------------
  308.     draw_text(16, 6, "Settings", colors.blue, colors.black)
  309.    
  310.     monitor.setTextColour(colours.blue)
  311.     monitor.setCursorPos(0,7)
  312.     monitor.write(string.rep("-", displayW))
  313.     monitor.setTextColour(colours.white)
  314.    
  315.     -----------Turbine Management Enable/Disable---------------------
  316.     monitor.setCursorPos(1,9)
  317.     monitor.write("Turbine Management: ")
  318.     if turbinesManagement[currentTurbine] == true then
  319.         monitor.setBackgroundColour(colours.blue)
  320.     else
  321.         monitor.setBackgroundColour(colours.grey)
  322.     end
  323.     monitor.setCursorPos(20,9)
  324.     monitor.write(" ON  ")
  325.  
  326.     if turbinesManagement[currentTurbine] == true then
  327.         monitor.setBackgroundColour(colours.grey)
  328.     else
  329.         monitor.setBackgroundColour(colours.blue)
  330.     end
  331.     monitor.setCursorPos(25,9)
  332.     monitor.write(" OFF ")
  333.     monitor.setBackgroundColour(colours.black)
  334. end
  335.  
  336. function management()
  337.     for i=1,#turbines,1 do
  338.         if turbinesManagement[i] == true then
  339.             tempTurbine = peripheral.wrap(turbines[i])
  340.             active = tempTurbine.getActive()
  341.             energy_stored = tempTurbine.getEnergyStored()
  342.             rotorSpeed = tempTurbine.getRotorSpeed()
  343.             local maxVal = 1000000
  344.             local minVal = energy_stored
  345.             local percent = math.floor((energy_stored/maxVal)*100)
  346.             if rotorSpeed < 1801 then
  347.                 if percent > 99 then
  348.                     tempTurbine.setActive(false)
  349.                 elseif percent < 75 then
  350.                     tempTurbine.setActive(true)
  351.                 end
  352.             else
  353.                 tempTurbine.setActive(false)
  354.             end
  355.         end
  356.     end
  357. end
  358.  
  359. function checkClickPosition()
  360.     displayW,displayH=monitor.getSize()
  361.     if control == false then
  362.         if mouseWidth > 0 and mouseWidth < displayW and mouseHeight > displayH - 3 and mouseHeight < displayH then
  363.             control = true
  364.         end
  365.     else
  366.         if mouseWidth > 20 and mouseWidth < 24 and mouseHeight == 3 then
  367.             turbine.setActive(true)
  368.         elseif mouseWidth > 24 and mouseWidth < 28 and mouseHeight == 3 then
  369.             turbine.setActive(false)
  370.         elseif mouseWidth > 1 and mouseWidth < (displayW - 26) and mouseHeight == (displayH - 1) then
  371.             control = false
  372.             setting = false
  373.         elseif mouseWidth > (displayW - 10) and mouseWidth < displayW and mouseHeight == (displayH - 1) then
  374.             if setting then
  375.                 setting = false
  376.             else
  377.                 setting = true
  378.             end
  379.         elseif mouseWidth > (displayW - 9) and mouseWidth < (displayW - 7) and mouseHeight == 1 then
  380.             if currentTurbine > 1 then
  381.                 currentTurbine = currentTurbine - 1
  382.                 turbine = peripheral.wrap(turbines[currentTurbine])
  383.             end
  384.         elseif mouseWidth > (displayW - 6) and mouseWidth < (displayW - 4) and mouseHeight == 1 then
  385.             if currentTurbine < table.getn(turbines) then
  386.                 currentTurbine = currentTurbine + 1
  387.                 turbine = peripheral.wrap(turbines[currentTurbine])
  388.             end
  389.         end
  390.         if setting then
  391.             if mouseWidth > 20 and mouseWidth < 24 and mouseHeight == 9 then
  392.                 turbinesManagement[currentTurbine] = true
  393.             elseif mouseWidth > 24 and mouseWidth < 28 and mouseHeight == 9 then
  394.                 turbinesManagement[currentTurbine] = false
  395.             end
  396.         end
  397.     end
  398.     sleep(0.5)
  399. end
  400.  
  401. function mainMenu()
  402.     terminalOutput()
  403.     initPeripherals()
  404.     if monitorFound == true then
  405.         monitor.setTextScale(1)
  406.     end
  407.     while true do
  408.         if monitorFound == true then
  409.             displayW,displayH=monitor.getSize()
  410.             if displayH == 26 and displayW == 39 then
  411.                 if control == true then
  412.                     drawControlScreen()
  413.                     if setting == true then
  414.                         drawSettingScreen()
  415.                     else
  416.                         drawDisplayScreen()
  417.                     end
  418.                 else
  419.                     drawMainScreen()
  420.                 end
  421.                 management()
  422.             else
  423.                 print("This program is built for a 4x4 monitor only!")
  424.                 return
  425.             end
  426.         else
  427.             management()
  428.         end
  429.         sleep(0.5)
  430.     end
  431. end
  432.  
  433. function events()
  434.     while true do
  435.         event,p1,p2,p3 = os.pullEvent()
  436.         if event=="monitor_touch" then
  437.             mouseWidth = p2 -- sets mouseWidth
  438.             mouseHeight = p3 -- and mouseHeight
  439.             checkClickPosition() -- this runs our function
  440.         elseif event=="peripheral" then
  441.             terminalOutput()
  442.             initPeripherals()
  443.         elseif event=="peripheral_detach" then
  444.             terminalOutput()
  445.             initPeripherals()
  446.         end
  447.     end
  448. end
  449.  
  450. parallel.waitForAny(mainMenu,events)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement