Advertisement
Tankman101

Reactor Controller v2

Apr 25th, 2017
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.27 KB | None | 0 0
  1. --[[
  2. Reactor Controller v2.1
  3.  
  4. Author: Tankman
  5.  
  6. May contain ants
  7. ]]
  8.  
  9.  
  10.  
  11. -- Load Libraries
  12. local component = require("component")
  13. local sides = require("sides")
  14. local event = require("event")
  15. local term = require("term")
  16. local tt = require("tankTools")
  17.  
  18. -- Hook Components
  19. local screen = component.screen
  20. local gpu = component.gpu
  21. local reactor = component.proxy("73d35576-e02c-474c-9b4d-841c9df8df10")
  22. local inv = component.proxy("1cbde4bf-9ce2-43d2-9548-aad062b1344e")
  23. local rs = component.proxy("91e053e1-0d60-471c-aec9-7207bd714be5")
  24. local tank = component.proxy("68c5c9d1-d0fc-4bbc-8419-98a51edbf0ca")
  25.  
  26. -- User Settings
  27. local cSide = sides.south -- Sides are a bit picky
  28. local rSide = sides.east
  29. local iSide = sides.west
  30.  
  31. -- Internal Variables
  32. local sidebarState = "Gauges"
  33. local sidebarGUI = false
  34. local check = true
  35. local charging = false
  36. local depleted = 0
  37. local overide = false
  38. local overheat = false
  39. local radiation = false
  40. local fuelTimer = 0
  41. local sw, sh = gpu.getResolution()
  42.  
  43. -- Functional Variables
  44. local active = reactor.getReactorEUOutput()
  45. local heat = reactor.getHeat()
  46. local maxHeat = reactor.getMaxHeat()
  47. local coolent = tank.getFluidInTank(cSide)
  48. local coolTank = coolent[1].amount
  49. local coolTankMax = coolent[1].capacity
  50. local hotTank = coolent[2].amount
  51. local hotTankMax = coolent[2].capacity
  52. local fuel = {}
  53.  
  54. -- Colors
  55. local fg = gpu.getForeground()
  56. local bg = gpu.getBackground()
  57. local colors = tt.colors()
  58.  
  59.  
  60. -- GUI Elements
  61. local controlButton = tt.buttonCreate("Start", 47, 5, 11, 3, colors.lime, colors.black)
  62. local exitButton = tt.buttonCreate("Exit", 47, 11, 11, 3, colors.blue, colors.white)
  63. local gaugesButton = tt.buttonCreate("Gauges", 2, 2, 12, 1, colors.red, colors.white)
  64. local fuelButton = tt.buttonCreate(" Fuel ", 14, 2, 10, 1, colors.blue, colors.white)
  65.  
  66. local heatBar = tt.barCreate(62, 6, 2, 18, colors.silver, colors.orange)
  67. local coolentBar = tt.barCreate(69, 6, 2, 18, colors.silver, colors.cyan)
  68. local hotCoolentBar = tt.barCreate(76, 6, 2, 18, colors.silver, colors.pink)
  69.  
  70. function exit()
  71.   rs.setOutput(rSide, 0)
  72.   check = false
  73.   event.ignore('touch',touch)
  74.   term.clear()
  75.   screen.setTouchModeInverted(false)
  76.   gpu.setBackground(bg)
  77.   gpu.setForeground(fg)
  78.   os.exit()
  79. end
  80.  
  81. function fuelCheck()
  82.   local count = 0
  83.   for i = 1, 54 do
  84.     local temp = inv.getStackInSlot(iSide, i)
  85.     if temp ~= nil then
  86.       if string.find(temp.label, "Fuel") then
  87.         count = count + 1
  88.         fuel[count] = temp.label
  89.       end
  90.     end
  91.   end
  92.   gpu.setBackground(colors.blue)
  93.   gpu.setForeground(colors.white)
  94.   if count == 0 and fuel[1] ~= nil then
  95.     fuel = {}
  96.   end
  97.   if fuel[1] ~= nil then
  98.     if tt.fuzzyCount(fuel, "Depleted") > 0 then
  99.       gpu.fill(4, 10, 22, 1, " ")
  100.       gpu.set(4, 10, "Fuel Summary: Depleted")
  101.       rs.setOutput(rSide, 0)
  102.       controlButton.text = "Start"
  103.       controlButton:disable(true)
  104.     else
  105.       gpu.fill(4, 10, 22, 1, " ")
  106.       gpu.set(4, 10, "Fuel Summary: Fissile")
  107.       controlButton:disable(false)
  108.     end
  109.   else
  110.     gpu.fill(4, 10, 22, 1, " ")
  111.     gpu.set(4, 10, "Fuel Summary: Empty")
  112.     rs.setOutput(rSide, 0)
  113.     controlButton.text = "Start"
  114.     controlButton:disable(true)
  115.   end
  116.   gpu.setBackground(bg)
  117.   gpu.setForeground(fg)
  118. end
  119.  
  120. function checkReactor()
  121.   heat = reactor.getHeat()
  122.   active = reactor.getReactorEUOutput()
  123.   local heatPercentage = tt.percent(heat, maxHeat)
  124.   gpu.setBackground(colors.blue)
  125.   gpu.setForeground(colors.white)
  126.   if active == 0 then
  127.     gpu.fill(4, 7, 15, 1, " ")
  128.     gpu.set(4, 7, "Reactor Offline")
  129.   else
  130.     gpu.fill(4, 7, 15, 1, " ")
  131.     gpu.set(4, 7, "Reactor Online")
  132.   end
  133.   gpu.fill(4, 8, 24, 1, " ")
  134.   gpu.set(4, 8, "Reactor Temp: " .. heat)
  135.   gpu.set(4, 9, "Reactor Max Temp: " .. maxHeat)
  136.   if active == 0 and charging == false and overide == false and overheat == false then
  137.     if fuelTimer == 0 then
  138.       fuelCheck()
  139.       fuelTimer = 20
  140.     else
  141.       fuelTimer = fuelTimer - 1
  142.     end
  143.   end
  144.   gpu.setBackground(bg)
  145.   gpu.setForeground(fg)
  146.   if heatPercentage >= 70 then
  147.     radiation = true
  148.   else
  149.     radiation = false
  150.   end
  151.   if sidebarState == "Gauges" then
  152.     if heatPercentage >= 75 then
  153.       overheat = true
  154.       overide = false
  155.       rs.setOutput(rSide, 0)
  156.       controlButton.text = "Start"
  157.       controlButton:disable(true)
  158.     else
  159.       if overheat == true and heatPercentage == 0 then
  160.         overheat = false
  161.         controlButton:disable(false)
  162.       end
  163.     end
  164.     if heatPercentage <= 25 then
  165.       heatBar.fColor = colors.lightBlue
  166.       heatBar:draw(heatPercentage)
  167.     else
  168.       if heatPercentage == 42 then
  169.         heatBar.fColor = colors.green
  170.         heatBar:draw(heatPercentage)
  171.       else
  172.         if heatPercentage <= 50 then
  173.           heatBar.fColor = colors.yellow
  174.           heatBar:draw(heatPercentage)
  175.         else
  176.           if heatPercentage <= 75 then
  177.             heatBar.fColor = colors.orange
  178.             heatBar:draw(heatPercentage)
  179.           else
  180.             if heatPercentage <= 100 then
  181.               heatBar.fColor = colors.red
  182.               heatBar:draw(heatPercentage)
  183.             end
  184.           end
  185.         end
  186.       end
  187.     end
  188.   end
  189. end
  190.  
  191. function sidebar()
  192.   if sidebarState == "Gauges" then
  193.     if sidebarGUI == false then
  194.       tt.label(59, 4, 20, " Gauges ", colors.white, colors.white)
  195.       gpu.setBackground(colors.blue)
  196.       gpu.setForeground(colors.white)
  197.       gpu.fill(59, 5, 20, 20, " ")
  198.       tt.vText(60, 7, "HEAT", colors.white)
  199.       tt.vText(67, 7, "COOLENT", colors.white)
  200.       tt.vText(74, 7, "HOT COOLENT", colors.white)
  201.       tt.vLine(65, 6, 18, colors.white)
  202.       tt.vLine(72, 6, 18, colors.white)
  203.       gpu.setBackground(bg)
  204.       gpu.setForeground(fg)
  205.       sidebarGUI = true
  206.     end
  207.     coolent = tank.getFluidInTank(cSide)
  208.     coolTank = coolent[1].amount
  209.     hotTank = coolent[2].amount
  210.     coolentBar:draw(tt.percent(coolTank, coolTankMax))
  211.     hotCoolentBar:draw(tt.percent(hotTank, hotTankMax))
  212.   else if sidebarState == "Fuel" then
  213.     if sidebarGUI == false then
  214.       tt.label(59, 4, 20, " Fuel ", colors.white, colors.white)
  215.       gpu.setBackground(colors.blue)
  216.       gpu.setForeground(colors.white)
  217.       gpu.fill(59, 5, 20, 20, " ")
  218.       gpu.set(60, 6, "Fuel U: " .. tt.itemCount(fuel, "Fuel Rod (Uranium)"))
  219.       gpu.set(60, 7, "Dual U: " .. tt.itemCount(fuel, "Dual Fuel Rod (Uranium)"))
  220.       gpu.set(60, 8, "Quad U: " .. tt.itemCount(fuel, "Quad Fuel Rod (Uranium)"))
  221.       gpu.set(60, 9, "Fuel MOX: " .. tt.itemCount(fuel, "Fuel Rod (MOX)"))
  222.       gpu.set(60, 10, "Dual MOX: " .. tt.itemCount(fuel, "Dual Fuel Rod (MOX)"))
  223.       gpu.set(60, 11, "Quad MOX: " .. tt.itemCount(fuel, "Quad Fuel Rod (MOX)"))
  224.       gpu.setBackground(bg)
  225.       gpu.setForeground(fg)
  226.       sidebarGUI = true
  227.     end
  228.   end
  229.   end
  230. end
  231.  
  232. function touch(name, address, x, y, button, player)
  233.   if controlButton:press(x, y) then
  234.     active = reactor.getReactorEUOutput()
  235.     if active == 0 then
  236.       overide = false
  237.       controlButton.tColor = colors.white
  238.       controlButton.bColor = colors.red
  239.       controlButton.text = "Stop"
  240.       rs.setOutput(rSide, 15)
  241.     else
  242.       overide = true
  243.       controlButton.tColor = colors.black
  244.       controlButton.bColor = colors.lime
  245.       controlButton.text = "Start"
  246.       rs.setOutput(rSide, 0)
  247.     end
  248.     controlButton:draw()
  249.   else if exitButton:press(x, y) then
  250.     exit()
  251.   else if gaugesButton:press(x, y) and sidebarState ~= "Gauges" then
  252.     sidebarState = "Gauges"
  253.     sidebarGUI = false
  254.     gaugesButton.bColor = colors.red
  255.     fuelButton.bColor = colors.blue
  256.     gaugesButton:draw()
  257.     fuelButton:draw()
  258.   else if fuelButton:press(x, y) and sidebarState ~= "Fuel" then
  259.     fuelCheck()
  260.     sidebarState = "Fuel"
  261.     sidebarGUI = false
  262.     gaugesButton.bColor = colors.blue
  263.     fuelButton.bColor = colors.red
  264.     gaugesButton:draw()
  265.     fuelButton:draw()
  266.   end
  267.   end
  268.   end
  269.   end
  270. end
  271.  
  272. function drawNotifications()
  273.   gpu.setBackground(colors.blue)
  274.   gpu.setForeground(colors.white)
  275.   local center
  276.   gpu.fill(3, 16, 54, 9, " ")
  277.   if overheat == true then
  278.     center = tt.textCenter(3, 16, 54, 9, "Reactor Nearing Meltdown, Console locked")
  279.     gpu.set(center.x, center.y, "Reactor Nearing Meltdown, Console locked")
  280.   else
  281.     if radiation == true then
  282.       center = tt.textCenter(3, 16, 54, 9, "Danger: Radiation Leak")
  283.       gpu.set(center.x, center.y, "Danger: Radiation Leak")
  284.     else
  285.       if depleted > 0 or fuel[1] == nil then
  286.         center = tt.textCenter(3, 16, 54, 9, "Please Replace Fuel")
  287.         gpu.set(center.x, center.y, "Please Replace Fuel")
  288.       else
  289.         if charging == true then
  290.           center = tt.textCenter(3, 16, 54, 9, "Reactor Idle, Batteries Full")
  291.           gpu.set(center.x, center.y, "Reactor Idle, Batteries Full")
  292.         else
  293.           if overide == true then
  294.             center = tt.textCenter(3, 16, 54, 9, "Manual Shutdown Engaged")
  295.             gpu.set(center.x, center.y, "Manual Shutdown Engaged")
  296.           else
  297.             center = tt.textCenter(3, 16, 54, 9, "Everything Nominal")
  298.             gpu.set(center.x, center.y, "Everything Nominal")
  299.           end
  300.         end
  301.       end
  302.     end
  303.   end
  304.   gpu.setBackground(bg)
  305.   gpu.setForeground(fg)
  306. end
  307.  
  308. function drawGUI()
  309.   gpu.setBackground(bg)
  310.   gpu.fill(1, 1, sw, sh, " ")
  311.   tt.frameDraw(1, 1, sw, sh, colors.white)
  312.   tt.frameDraw(1, 1, sw, 3, colors.white)
  313.   tt.label(2, 1, sw - 2, "[ Reactor Control ]", colors.white, colors.white)
  314.   gpu.set(1, 3, "╠")
  315.   gpu.set(sw, 3, "╣")
  316.   tt.frameDraw(3, 4, 43, 11, colors.white)
  317.   tt.label(4, 4, 41, " Status ", colors.white, colors.white)
  318.   tt.label(3, 15, 55, " Notifications ", colors.white, colors.white)
  319.   tt.hLine(47, 9, 11, colors.white)
  320.   gpu.setBackground(colors.blue)
  321.   gpu.fill(2, 2, sw - 2, 1, " ") -- Menu Bar
  322.   gpu.fill(4, 5, 41, 9, " ") -- Status Background
  323.   gpu.fill(3, 16, 55, 9, " ") -- Notifications Area
  324.   gpu.setBackground(bg)
  325.   controlButton:draw()
  326.   exitButton:draw()
  327.   gaugesButton:draw()
  328.   fuelButton:draw()
  329. end
  330.  
  331. function run()
  332.   screen.setTouchModeInverted(true)
  333.   rs.setOutput(rSide, 0)
  334.   drawGUI()
  335.   event.listen("touch", touch)
  336.   fuelCheck()
  337.   while check do
  338.     checkReactor()
  339.     sidebar()
  340.     drawNotifications()
  341.     os.sleep(0.25)
  342.   end
  343. end
  344.  
  345. run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement