Advertisement
glitchdetector

Latias' Base Manager

Dec 1st, 2016
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.25 KB | None | 0 0
  1. -- Menu.lua, a Latias interface --
  2.  
  3. local component = require("component")
  4. local event = require("event")
  5.  
  6. local w = 50
  7. local h = 16
  8. local gpu = component.gpu
  9. local color = {black=0x000000, white=0xFFFFFF}
  10. local keys = {up=200, down=208, ok=28, back=14, stop=211, ctrl=29, shift=42, left=203, right=205}
  11.  
  12. local menu = {
  13.     {"power","Power Menu","Power Storage Statistics",
  14.         function() end,
  15.         function() drawPower() end,
  16.         function() deadEnd() end -- On Listen
  17.     },
  18.     {"reactor","Reactor Overview","Reactor Overview & Management",
  19.         function() end,
  20.         function()
  21.             local reactors = components({"br_reactor"})
  22.             local count = 0
  23.             for c,_ in pairs(reactors) do
  24.                 local br = component.proxy(c)
  25.                 if br ~= nil then
  26.                     if br.getFuelAmount() ~= nil then -- check if reactor is fake
  27.                         local y = 4 + 4 * count
  28.                         local fuelCurrent = math.floor(br.getFuelAmount())
  29.                         local fuelMax = math.floor(br.getFuelAmountMax())
  30.                         local fuelWaste = math.floor(br.getWasteAmount())
  31.                         local powerCurrent = math.floor(br.getEnergyStored())
  32.                         local powerMax = math.floor(10000000)
  33.                         local tempFuel = math.floor(br.getFuelTemperature())
  34.                         local tempCase = math.floor(br.getCasingTemperature())
  35.                         drawLine(y)
  36.                         if br.getActive() then drawCenteredText(y, "Online - "..tempFuel.."C - "..tempCase.."C") else drawCenteredText(y, "Offline") end
  37.                        
  38.                         drawLine(y+1)
  39.                         drawCenteredText(y+1,powerCurrent.."rF / "..powerMax.."rF")                  
  40.                         drawLine(y+2)
  41.                         drawCenteredText(y+2,"Fuel "..fuelCurrent.."mb | "..fuelWaste.."mb Waste")
  42.  
  43.                         count = count + 1
  44.                     end
  45.                 end
  46.             end
  47.             if count == 0 then
  48.                 drawLine(4)
  49.                 drawCenteredText(4,"No reactors installed")
  50.             end
  51.         end,
  52.         function() leftRight() end
  53.     },
  54.     {"defense","Defense System","Home Security Defense System",
  55.         function() end,
  56.         function()
  57.             drawLine(4)
  58.             drawCenteredText(4,"No turrets mounted")
  59.         end,
  60.         function() deadEnd() end
  61.     },
  62.     {"farm","Farm Manager","Agricultural Management",
  63.         function() end,
  64.         function()
  65.             drawLine(4)
  66.             drawCenteredText(4,"Farm sockets currently not in use")
  67.         end,
  68.         function() deadEnd() end
  69.     },
  70.     {"quarry","Quarry Settings","Excavator Management System",
  71.         function() end,
  72.         function()
  73.             drawLine(4)
  74.             drawText(4,"No excavation site defined")
  75.         end,
  76.         function() deadEnd() end
  77.     },
  78.     {"internet","Internet Browser","Basic Internet Browser",
  79.         function()
  80.             url = "http://ask.hiof.no/~vegardbe/"
  81.         end,
  82.         function()
  83.             drawLine(4)
  84.             drawCenteredText(4,"503 - Service Unavailable")
  85.             drawLine(5)
  86.             drawText(5,"URL: "..url)
  87.         end,
  88.         function() deadEnd() end
  89.     }
  90. }
  91.  
  92. local screen = "menu"
  93. local screenData = {"menu","Menu","Menu",function() sel = 1 end,function() drawMenu() end,function() listen() end}
  94. local menuScreenData = screenData
  95. local selscreen = "menu"
  96. local seltitle = "Menu"
  97.  
  98. local title = "Menu"
  99.  
  100. local sel = 1
  101.  
  102. local stop = false
  103.  
  104. local menuSize = 0
  105. for k,v in pairs(menu) do
  106.     menuSize = menuSize + 1
  107. end
  108.  
  109. gpu.setResolution(w, h)
  110.  
  111. table_whitelist = function(t, side, filter)
  112.     local newTable = {}
  113.     for k,v in pairs(t) do
  114.         for _,f in pairs(filter) do
  115.             if side == "v" and v == f then
  116.                 newTable[k] = v
  117.             elseif side == "k" and k == f then
  118.                 newTable[k] = v            
  119.             end
  120.         end
  121.     end
  122.     return newTable
  123. end
  124.  
  125. components = function(t)
  126.     return table_whitelist(component.list(), "v", t)
  127. end
  128.  
  129. listen = function()
  130.     local id, _, type, key = event.pull(10, "key_down")
  131.     if (id == "key_down") then
  132.         if (key == keys.up) then
  133.             sel = sel - 1
  134.         elseif (key == keys.down) then
  135.             sel = sel + 1
  136.         elseif (key == keys.stop) then
  137.             beep(1)
  138.             stop = true
  139.         elseif (key == keys.ok) then
  140.             beep(2,true)
  141.             setScreen(menu[sel])
  142.         elseif (key == keys.back) then
  143.             beep(2)
  144.             setScreen(menuScreenData)
  145.         else
  146.             listen()
  147.         end
  148.     else
  149.     end
  150.  
  151.     if (sel < 1) then
  152.         sel = sel + menuSize
  153.     end
  154.     if (sel > menuSize) then
  155.         sel = sel - menuSize
  156.     end
  157. end
  158.  
  159. deadEnd = function()
  160.     invertColor(false)
  161.     drawCenteredText(h-1,"Press BACKSPACE to go back")
  162.     local id, _, type, key = event.pull(10, "key_down")
  163.     if (id == "key_down") then
  164.         if (key == keys.back) then
  165.             beep(2)
  166.             setScreen(menuScreenData)
  167.         else
  168.             deadEnd()
  169.         end
  170.     else
  171.     end    
  172. end
  173.  
  174. leftRight = function()
  175.     invertColor(false)
  176.     drawCenteredText(h-2,"Fuel Rod Controls")
  177.     drawCenteredText(h-1,"[<- LOWER] [BACKSPACE] [RISE ->]")
  178.     local id, _, type, key = event.pull(10, "key_down")
  179.     if (id == "key_down") then
  180.         if (key == keys.back) then
  181.             beep(2)
  182.             setScreen(menuScreenData)
  183.         elseif (key == keys.left) then
  184.            
  185.         elseif (key == keys.right) then
  186.            
  187.         else
  188.             leftRight()
  189.         end
  190.     else
  191.     end    
  192. end
  193.  
  194. beep = function(n,d)
  195.     for i=1, n do
  196.         if d then
  197.             freq = 200 + (1000 / n) * i
  198.         else
  199.             freq = 1800 - (1000 / n) * i          
  200.         end
  201.         component.computer.beep(freq, 0.1)
  202.     end
  203. end
  204.  
  205. clear = function()
  206.     for i=1, h do
  207.         drawClear(i)
  208.     end
  209. end
  210.  
  211. invertColor = function(b)
  212.     if (b) then
  213.         setColor(color.white, color.black)
  214.     else
  215.         setColor(color.black, color.white)
  216.     end
  217. end
  218.  
  219. setColor = function(bg, fg)
  220.     gpu.setBackground(bg)
  221.     gpu.setForeground(fg)
  222. end
  223.  
  224. drawLine = function(y)
  225.     setColor(color.white, color.black)
  226.     gpu.fill(1,y,w,1," ")  
  227. end
  228.  
  229. drawClear = function(y)
  230.     setColor(color.black, color.white)
  231.     gpu.fill(1,y,w,1," ")  
  232. end
  233.  
  234. drawCenteredText = function(y,text)
  235.     local center = w / 2
  236.     local start = center - string.len(text) / 2
  237.     gpu.set(start,y,text)
  238. end
  239.  
  240. drawText = function(y,text)
  241.     gpu.set(2,y,text)
  242. end
  243.  
  244. --local screenData = {"menu","Menu","Menu",function() sel = 0 end,function() drawMenu() end}
  245. setScreen = function(sD)
  246.     title = sD[3]
  247.     screen = sD[1]
  248.     sD[4]() -- Run the screen function
  249.     screenData = sD
  250. end
  251.  
  252. drawBasis = function()
  253.     drawLine(1)
  254.     drawCenteredText(1,"Ratiasu's Base System")
  255.     drawLine(2)
  256.     drawCenteredText(2,title)
  257.    
  258.     drawLine(h)
  259.     drawText(h, "version 2.3 - "..screen.." "..gpu.address)
  260. end
  261.  
  262. -- draw menu
  263. drawMenu = function()
  264.     local c = 1
  265.     for k,v in pairs(menu) do
  266.         if v ~= nil then
  267.             local selected = (sel == c)
  268.             invertColor(selected)
  269.             drawCenteredText(2 + 2 * c, v[2])
  270.             if selected then
  271.                 selscreen = v[1]
  272.                 seltitle = v[3]
  273.             end
  274.             c = c + 1
  275.         end
  276.     end
  277. end
  278.  
  279. jToRf = function(n)
  280.     return n * ((1/3200) * 1280)
  281. end
  282.  
  283. table_merge = function(a, b)
  284.     for k,v in pairs(b) do
  285.         table.insert(a, v)
  286.     end
  287.     return a
  288. end
  289.  
  290. drawPower = function()
  291.     local count = 0
  292.     local treshold = 25
  293.     local list = components({"basic_energy_cube", "advanced_energy_cube", "elite_energy_cube", "ultimate_energy_cube"})
  294.     for c,_ in pairs(list) do
  295.         local y = 4 + count * 3
  296.         local ec = component.proxy(c)
  297.         if ec ~= nil then
  298.             local powerCurrent = math.floor(jToRf(ec.getEnergy()))
  299.             local powerMax = math.floor(jToRf(ec.getMaxEnergy()))
  300.             local powerOutput = math.floor(jToRf(ec.getOutput()))
  301.             local powerNeeded = math.floor(jToRf(ec.getEnergyNeeded()))
  302.  
  303.             local percent = math.ceil((100 / powerMax) * powerCurrent)
  304.  
  305.             drawLine(y)
  306.             if percent < treshold then
  307.                 drawCenteredText(y,"CRITICAL POWER LEVEL")
  308.                 beep(2)
  309.             else
  310.                 drawCenteredText(y,percent.."%")
  311.             end
  312.            
  313.             drawLine(y+1)
  314.             drawCenteredText(y+1,powerCurrent.."RF / "..powerMax.."RF".."")
  315.             count = count + 1
  316.         end
  317.     end
  318.     if count == 0 then
  319.         drawLine(4)
  320.         drawCenteredText(4,"No power storage device found")        
  321.     end
  322. end
  323.  
  324. -- Main Loop!! :ooo
  325.  
  326. while (true) do
  327.     beep(1,true)
  328.     clear()
  329.    
  330.     drawBasis()
  331.    
  332.     -- Run the idle function for the screen
  333.     screenData[5]()
  334.  
  335.     -- Run the wait function for the screen
  336.     screenData[6]()
  337.    
  338.     -- If you can stop the script, do so
  339.     if stop then
  340.         beep(3)
  341.         clear()
  342.     break
  343.     end
  344. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement