Advertisement
Guest User

warpdrive.lua

a guest
Jan 2nd, 2016
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.30 KB | None | 0 0
  1. local component = require("component")
  2. local event = require("event")
  3.  
  4.  
  5. function ArrayLength(x)
  6.   local c = 0
  7.   for i, v in pairs(x) do
  8.     c = c + 1
  9.   end
  10.   return c
  11. end
  12.  
  13.  
  14. -- ---------- SCREEN class
  15.  
  16. Screen = {}
  17. Screen.__index = Screen
  18.  
  19. function Screen:SetMaxResolution()
  20.   w, h = component.gpu.maxResolution()
  21.   component.gpu.setResolution(w, h)
  22. end
  23.  
  24. function Screen:GetResolution()
  25.   return component.gpu.getResolution()
  26. end
  27.  
  28. function Screen:GetColorBitDepth()
  29.   return component.gpu.getDepth()
  30. end
  31.  
  32. function Screen:GetBackgroundColor()
  33.   return component.gpu.getBackground()
  34. end
  35.  
  36. function Screen:SetBackgroundColor(color, index)
  37.   if index == nil then
  38.     index = false
  39.   end
  40.  
  41.   return component.gpu.setBackground(color, index)
  42. end
  43.  
  44. function Screen:GetForegroundColor()
  45.   return component.gpu.getForeground()
  46. end
  47.  
  48. function Screen:SetForegroundColor(color, index)
  49.   if index == nil then
  50.     index = false
  51.   end
  52.  
  53.   return component.gpu.setForeground(color, index)
  54. end
  55.  
  56. function Screen:Clear(color, index)
  57.   if color == nil then
  58.     color = self.background_color;
  59.   end
  60.  
  61.   self:SetBackgroundColor(color, index)
  62. --  component.gpu.fill(1, 1, self.width, self.height, " ")
  63.   self.term.clear()
  64. end
  65.  
  66. function Screen:DrawStringAt(x, y, str, color)
  67.   if str == nil or str == "" then
  68.     return
  69.   end
  70.  
  71.   saved_color, saved_index = self:GetForegroundColor()
  72.  
  73.   if color == nil then
  74.     color = self.main_font_color
  75.   end
  76.   self:SetForegroundColor(color, false)
  77.   self.term.setCursor(x, y)
  78.   self.term.write(str)
  79.  
  80.   self:SetForegroundColor(saved_color, saved_index)
  81. end
  82.  
  83. function Screen:DrawString(str, color)
  84.   if color == nil then
  85.     color = self.main_font_color
  86.   end
  87.  
  88.   x, y = self.term.getCursor()
  89.   self:DrawStringAt(x, y, str, color)
  90. end
  91.  
  92. function Screen:Release()
  93.   self:SetBackgroundColor(self.saved_background_color, self.saved_background_index)
  94.   self:SetForegroundColor(self.saved_foreground_color, self.saved_foreground_index)
  95.  
  96. --  self:Clear(self.saved_background_color)
  97. end
  98.  
  99. function Screen:DrawSplash()
  100.   local splash = {
  101.     "  __      __                   ________        .__              ",
  102.     "/  \\    /  \\____ _____________\\______ \\_______|__|__  __ ____  ",
  103.     "\\   \\/\\/   |__  \\\\_  __ \\____ \\|    |  \\_  __ \\  \\  \\/ // __ \\ ",
  104.     " \\        / / __ \\|  | \\/  |_> >    `   \\  | \\/  |\\   /\\  ___/ ",
  105.     "  \\__/\\  / (____  /__|  |   __/_______  /__|  |__| \\_/  \\___  >",
  106.     "       \\/       \\/      |__|          \\/                    \\/ "
  107.   }
  108.  
  109.   if (self.height > 20) then
  110.     self.term.setCursor(0, 2)
  111.   end
  112.  
  113.   for i, str in pairs(splash) do
  114.     local left_padding = (screen.width - str:len()) / 2
  115.     print(string.rep(" ", left_padding) .. str)
  116.   end
  117. end
  118.  
  119. function Screen:DrawNoWarpDrive()
  120.   self:Clear()
  121.   self:DrawSplash()
  122.  
  123.   local x, y = self.term.getCursor()
  124.   local message = "Searching for WarpDrive Ship Controller"
  125.   self:DrawStringAt((self.width - message:len())/ 2 - 2, y + 2, message, self.error_font_color)
  126.  
  127.   for i = 0, 4 do
  128.     os.sleep(1)
  129.     self:DrawString(".", self.error_font_color)
  130.   end
  131. end
  132.  
  133. function Screen:DrawCoreError(msg)
  134.   self:Clear()
  135.   self:DrawSplash()
  136.  
  137.   local x, y = self.term.getCursor()
  138.   local message = "WARNING"
  139.  
  140.   self:DrawStringAt(
  141.     (self.width - message:len()) / 2,
  142.     y + 2,
  143.     message,
  144.     self.error_font_color
  145.   )
  146.  
  147.   message = "Warpdrive Core malfunction"
  148.   self:DrawStringAt(
  149.     (self.width - msg:len()) / 2,
  150.     y + 3,
  151.     message,
  152.     self.error_font_color
  153.   )
  154. end
  155.  
  156. function Screen:DrawPrompt(prompt, energy_gear)
  157.   self:Clear()
  158.   self:DrawSplash()
  159.   self:DrawCoreChargeGear(energy_gear)
  160.  
  161.   local x, y = self.term.getCursor()
  162.   self:DrawStringAt(
  163.     (self.width - prompt:len()) / 2,
  164.     y + 5,
  165.     prompt,
  166.     self.error_font_color
  167.   )
  168.  
  169.   self.term.setCursor(
  170.     (self.width - prompt:len()) / 2 - 1,
  171.     y + 6
  172.   )
  173.  
  174.   self.term.write(">");
  175.   self.term.setCursorBlink(true);
  176.   local str = self.term.read()
  177.   self.term.setCursorBlink(false)
  178.  
  179.   if (str ~= nil) then
  180.     str = str:sub(1, -2) -- remove tailing \n
  181.   end
  182.   return str
  183. end
  184.  
  185.  
  186. function Screen:DrawCoreChargeGear(value)
  187. --  self:DrawString("Energy: " .. energy .. " / " .. max_energy)
  188. --  self:DrawString(" " ..energy / max_energy .. "%")
  189.  
  190.   local gear_width = 6
  191.   if self.width < 160 then
  192.     gear_width = 3
  193.   end
  194.   local gear_vertical_padding = 5
  195.   local gear_right_padding = 3
  196.   local gear_height = self.height - 2 * gear_vertical_padding
  197.  
  198.   local energy_filled = value
  199.   local energy_empty = 1 - energy_filled
  200.  
  201.   local num_gear_colors = ArrayLength(self.core_charge_colors) - 1
  202.   local gear_color = self.core_charge_colors[ math.floor(energy_filled * num_gear_colors + 0.5) ]
  203.  
  204.   for i = 0, (gear_height - 1) do
  205.     local symbol = "░"
  206.     if (i / gear_height) >= energy_empty then
  207.       symbol = "▓"
  208.       color = gear_color
  209.     else
  210.       color = nil
  211.     end
  212.  
  213.     self:DrawStringAt(
  214.       self.width - gear_width - gear_right_padding,
  215.       gear_vertical_padding + i,
  216.       string.rep(symbol, gear_width),
  217.       color
  218.     )
  219.   end
  220.  
  221.   local energy_percentage = 100 * value
  222.   local gear_percentage = string.format("%.2f", energy_percentage)
  223.   if self.width < 160 then
  224.     gear_percentage = math.floor(energt_percentage + 0.5)
  225.   end
  226.   self:DrawStringAt(
  227.     self.width - gear_width / 2 - gear_right_padding - string.len(gear_percentage) / 2,
  228.     self.height - gear_vertical_padding + 1,
  229.     gear_percentage .. "%"
  230.   )
  231.  
  232.   self:DrawStringAt(
  233.     self.width - gear_width / 2 - gear_right_padding - 2,
  234.     self.height - gear_vertical_padding - gear_height - 2,
  235.     "CORE"
  236.   )
  237.   self:DrawStringAt(
  238.     self.width - gear_width / 2 - gear_right_padding - 4,
  239.     self.height - gear_vertical_padding - gear_height - 1,
  240.     "CHARGE"
  241.   )
  242. end
  243.  
  244. function Screen:DrawCrewWidget(crew)
  245.   local widget_left_padding = 5
  246.   local widget_players_left_padding = 3
  247.   local y = 30
  248.  
  249.   local header = "SHIP CREW"
  250.   if (crew['n'] > 0) then
  251.     header = header .. " (" .. crew['n'] .. ")"
  252.   end
  253.   self:DrawStringAt(
  254.     widget_left_padding,
  255.     y,
  256.     header
  257.   )
  258.  
  259.   for i, player in pairs(crew) do
  260.     if (i ~= "n") then
  261.       self:DrawStringAt(
  262.         widget_players_left_padding,
  263.         y + i,
  264.         player,
  265.         self.error_font_color
  266.       )
  267.     end
  268.   end
  269. end
  270.  
  271. function Screen:DrawMainMenu(ship_name, crew, core_energy)
  272.   self:Clear()
  273.   self:DrawSplash()
  274.  
  275.   local x, y = self.term.getCursor()
  276.   local welcome1 = "Welcime aboard < "
  277.   local welcome = "Welcome aboard < " .. ship_name .. " >, admiral"
  278.   self:DrawStringAt(
  279.     (self.width - welcome:len()) / 2,
  280.     y,
  281.     welcome1
  282.   )
  283.   self:DrawStringAt(
  284.     (self.width - welcome:len()) / 2 + welcome1:len(),
  285.     y,
  286.     ship_name,
  287.     self.error_font_color
  288.   )
  289.   self:DrawStringAt(
  290.     (self.width - welcome:len()) / 2 + welcome1:len() + ship_name:len(),
  291.     y,
  292.     " >, admiral"
  293.   )
  294.  
  295.   self:DrawCoreChargeGear(core_energy)
  296.   self:DrawCrewWidget(crew)
  297. end
  298.  
  299. function Screen.new(at_max_resolution)
  300.   local screen = {}
  301.   setmetatable(screen, Screen)
  302.  
  303.   if at_max_resolution then
  304.     screen:SetMaxResolution()
  305.   end
  306.  
  307.   screen.width, screen.height = screen:GetResolution()
  308.  
  309.   screen.colors = require("colors")
  310.   screen.term = require("term")
  311.  
  312.   screen.saved_background_color, screen.saved_background_index = screen:GetBackgroundColor()
  313.   screen.saved_foreground_color, screen.saved_foreground_index = screen:GetForegroundColor()
  314.  
  315.   screen.color_depth = screen:GetColorBitDepth()
  316.   if screen.color_depth == 1 then
  317.     os.exit()
  318.     screen.background_color = 0x000000
  319.     screen.main_font_color = 0xFFFFFF
  320.     screen.error_font_color = 0xFFFFFF
  321.   elseif screen.color_depth >= 8 then
  322.     screen.background_color = 0x3060A0
  323. screen.background_color=0;
  324.     screen.main_font_color = 0xFFFFFF
  325.     screen.error_font_color = 0xFFFF00
  326.     screen.core_charge_colors =
  327.     {
  328.       [0] = 0xFF0000,
  329.       [1] = 0xFFFF00,
  330.       [2] = 0x00FF00
  331.     }
  332.   end
  333.  
  334.   return screen
  335. end
  336.  
  337. -- ----------
  338.  
  339.  
  340.  
  341.  
  342.  
  343.  
  344. -- ---------- WARPDRIVE class
  345.  
  346. WarpDrive = {}
  347. WarpDrive.__index = WarpDrive
  348.  
  349. function WarpDrive:GetController()
  350.   if component.isAvailable("warpdriveShipController") then
  351.   --  return component.warpdriveShipController
  352.    
  353.     local ctrl = component.list("warpdriveShipController")()
  354.     if (ctrl ~= nil) then
  355.       return component.proxy(ctrl)
  356.     end
  357.   end
  358.   return nil
  359. end
  360.  
  361. function WarpDrive:IsNameSet()
  362.   return self:GetName() ~= "default"
  363. end
  364.  
  365. function WarpDrive:GetName()
  366.   local ctrl = self:GetController()
  367.   if (ctrl) then
  368.     return ctrl.coreFrequency()
  369.   end
  370.  
  371.   return nil
  372. end
  373.  
  374. function WarpDrive:SetName(name)
  375.   local ctrl = self:GetController()
  376.   if (ctrl) then
  377.     ctrl.coreFrequency(name)
  378.     if (ctrl.coreFrequency() == name) then
  379.         return true
  380.     end
  381.   end
  382.  
  383.   return false
  384. end
  385.  
  386. function WarpDrive:GetCrew()
  387.   local ctrl = self:GetController()
  388.   if (ctrl) then
  389.     local str, list = ctrl.getAttachedPlayers()
  390.     return list
  391.   end
  392.  
  393.   return nil
  394. end
  395.  
  396. function WarpDrive:Update()
  397.   local ctrl = self:GetController()
  398.  
  399.   self.controller_error = false
  400.   if (ctrl == nil) then
  401.     self.controller_error = true
  402.     return
  403.   end
  404.  
  405.   self.energy, self.max_energy = ctrl.energy()
  406.  
  407.   self.core_error = false
  408.   if self.energy == nil or self.max_energy == nil then
  409.     self.core_error = true
  410.   end
  411. end
  412.  
  413. function WarpDrive.new()
  414.   warpdrive = {}
  415.   setmetatable(warpdrive, WarpDrive)
  416.  
  417. --  for i, value in pairs(component.list()) do
  418. --    print(i .. ": " .. value)
  419. --  end
  420.  
  421.   local ctrl = warpdrive:GetController()
  422.   if ctrl == nil then
  423.     return nil
  424.   end
  425.  
  426.   warpdrive:Update()
  427.  
  428.   return warpdrive
  429. end
  430.  
  431. -- ----------
  432.  
  433.  
  434.  
  435. local shell = require("shell")
  436. local args = shell.parse(...)
  437.  
  438.  
  439. screen = Screen.new(true)
  440.  
  441. if args[1] ~= "nosplash" then
  442.   screen:Clear()
  443.   screen:DrawSplash()
  444. --  os.sleep(4)
  445. end
  446.  
  447. warpdrive = WarpDrive.new()
  448. while warpdrive == nil do
  449.   screen:Clear()
  450.   screen:DrawNoWarpDrive()
  451.  
  452.   warpdrive = WarpDrive.new()
  453. end
  454.  
  455.  
  456. -- ----------
  457.  
  458.  
  459.  
  460.  
  461. while true do
  462.   warpdrive:Update()
  463.  
  464.   if warpdrive.core_error then
  465.     screen:DrawCoreError("WarpDrive Core malfunction")
  466.   elseif warpdrive.controller_error then
  467.     screen:DrawCoreError("WarpDrive Ship Controller malfunction")
  468.   else
  469.  
  470.     if (not warpdrive:IsNameSet()) then
  471.       local new_name = screen:DrawPrompt("Enter ship name:", warpdrive.energy / warpdrive.max_energy)
  472.       if (new_name ~= nil) then
  473.         warpdrive:SetName(new_name)
  474.       end
  475.     else
  476.       screen:DrawMainMenu(warpdrive:GetName(), warpdrive:GetCrew(), warpdrive.energy / warpdrive.max_energy)
  477.     end
  478.  
  479.   end
  480.  
  481.   os.sleep(0.18)
  482. end
  483.  
  484. screen:Release()
  485.  
  486. --while true do
  487. --  local  _, _, x, y = event.pull("touch");
  488. --  print(x, y);
  489. --end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement