Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local component = require("component")
- local event = require("event")
- function ArrayLength(x)
- local c = 0
- for i, v in pairs(x) do
- c = c + 1
- end
- return c
- end
- -- ---------- SCREEN class
- Screen = {}
- Screen.__index = Screen
- function Screen:SetMaxResolution()
- w, h = component.gpu.maxResolution()
- component.gpu.setResolution(w, h)
- end
- function Screen:GetResolution()
- return component.gpu.getResolution()
- end
- function Screen:GetColorBitDepth()
- return component.gpu.getDepth()
- end
- function Screen:GetBackgroundColor()
- return component.gpu.getBackground()
- end
- function Screen:SetBackgroundColor(color, index)
- if index == nil then
- index = false
- end
- return component.gpu.setBackground(color, index)
- end
- function Screen:GetForegroundColor()
- return component.gpu.getForeground()
- end
- function Screen:SetForegroundColor(color, index)
- if index == nil then
- index = false
- end
- return component.gpu.setForeground(color, index)
- end
- function Screen:Clear(color, index)
- if color == nil then
- color = self.background_color;
- end
- self:SetBackgroundColor(color, index)
- -- component.gpu.fill(1, 1, self.width, self.height, " ")
- self.term.clear()
- end
- function Screen:DrawStringAt(x, y, str, color)
- if str == nil or str == "" then
- return
- end
- saved_color, saved_index = self:GetForegroundColor()
- if color == nil then
- color = self.main_font_color
- end
- self:SetForegroundColor(color, false)
- self.term.setCursor(x, y)
- self.term.write(str)
- self:SetForegroundColor(saved_color, saved_index)
- end
- function Screen:DrawString(str, color)
- if color == nil then
- color = self.main_font_color
- end
- x, y = self.term.getCursor()
- self:DrawStringAt(x, y, str, color)
- end
- function Screen:Release()
- self:SetBackgroundColor(self.saved_background_color, self.saved_background_index)
- self:SetForegroundColor(self.saved_foreground_color, self.saved_foreground_index)
- -- self:Clear(self.saved_background_color)
- end
- function Screen:DrawSplash()
- local splash = {
- " __ __ ________ .__ ",
- "/ \\ / \\____ _____________\\______ \\_______|__|__ __ ____ ",
- "\\ \\/\\/ |__ \\\\_ __ \\____ \\| | \\_ __ \\ \\ \\/ // __ \\ ",
- " \\ / / __ \\| | \\/ |_> > ` \\ | \\/ |\\ /\\ ___/ ",
- " \\__/\\ / (____ /__| | __/_______ /__| |__| \\_/ \\___ >",
- " \\/ \\/ |__| \\/ \\/ "
- }
- if (self.height > 20) then
- self.term.setCursor(0, 2)
- end
- for i, str in pairs(splash) do
- local left_padding = (screen.width - str:len()) / 2
- print(string.rep(" ", left_padding) .. str)
- end
- end
- function Screen:DrawNoWarpDrive()
- self:Clear()
- self:DrawSplash()
- local x, y = self.term.getCursor()
- local message = "Searching for WarpDrive Ship Controller"
- self:DrawStringAt((self.width - message:len())/ 2 - 2, y + 2, message, self.error_font_color)
- for i = 0, 4 do
- os.sleep(1)
- self:DrawString(".", self.error_font_color)
- end
- end
- function Screen:DrawCoreError(msg)
- self:Clear()
- self:DrawSplash()
- local x, y = self.term.getCursor()
- local message = "WARNING"
- self:DrawStringAt(
- (self.width - message:len()) / 2,
- y + 2,
- message,
- self.error_font_color
- )
- message = "Warpdrive Core malfunction"
- self:DrawStringAt(
- (self.width - msg:len()) / 2,
- y + 3,
- message,
- self.error_font_color
- )
- end
- function Screen:DrawPrompt(prompt, energy_gear)
- self:Clear()
- self:DrawSplash()
- self:DrawCoreChargeGear(energy_gear)
- local x, y = self.term.getCursor()
- self:DrawStringAt(
- (self.width - prompt:len()) / 2,
- y + 5,
- prompt,
- self.error_font_color
- )
- self.term.setCursor(
- (self.width - prompt:len()) / 2 - 1,
- y + 6
- )
- self.term.write(">");
- self.term.setCursorBlink(true);
- local str = self.term.read()
- self.term.setCursorBlink(false)
- if (str ~= nil) then
- str = str:sub(1, -2) -- remove tailing \n
- end
- return str
- end
- function Screen:DrawCoreChargeGear(value)
- -- self:DrawString("Energy: " .. energy .. " / " .. max_energy)
- -- self:DrawString(" " ..energy / max_energy .. "%")
- local gear_width = 6
- if self.width < 160 then
- gear_width = 3
- end
- local gear_vertical_padding = 5
- local gear_right_padding = 3
- local gear_height = self.height - 2 * gear_vertical_padding
- local energy_filled = value
- local energy_empty = 1 - energy_filled
- local num_gear_colors = ArrayLength(self.core_charge_colors) - 1
- local gear_color = self.core_charge_colors[ math.floor(energy_filled * num_gear_colors + 0.5) ]
- for i = 0, (gear_height - 1) do
- local symbol = "░"
- if (i / gear_height) >= energy_empty then
- symbol = "▓"
- color = gear_color
- else
- color = nil
- end
- self:DrawStringAt(
- self.width - gear_width - gear_right_padding,
- gear_vertical_padding + i,
- string.rep(symbol, gear_width),
- color
- )
- end
- local energy_percentage = 100 * value
- local gear_percentage = string.format("%.2f", energy_percentage)
- if self.width < 160 then
- gear_percentage = math.floor(energt_percentage + 0.5)
- end
- self:DrawStringAt(
- self.width - gear_width / 2 - gear_right_padding - string.len(gear_percentage) / 2,
- self.height - gear_vertical_padding + 1,
- gear_percentage .. "%"
- )
- self:DrawStringAt(
- self.width - gear_width / 2 - gear_right_padding - 2,
- self.height - gear_vertical_padding - gear_height - 2,
- "CORE"
- )
- self:DrawStringAt(
- self.width - gear_width / 2 - gear_right_padding - 4,
- self.height - gear_vertical_padding - gear_height - 1,
- "CHARGE"
- )
- end
- function Screen:DrawCrewWidget(crew)
- local widget_left_padding = 5
- local widget_players_left_padding = 3
- local y = 30
- local header = "SHIP CREW"
- if (crew['n'] > 0) then
- header = header .. " (" .. crew['n'] .. ")"
- end
- self:DrawStringAt(
- widget_left_padding,
- y,
- header
- )
- for i, player in pairs(crew) do
- if (i ~= "n") then
- self:DrawStringAt(
- widget_players_left_padding,
- y + i,
- player,
- self.error_font_color
- )
- end
- end
- end
- function Screen:DrawMainMenu(ship_name, crew, core_energy)
- self:Clear()
- self:DrawSplash()
- local x, y = self.term.getCursor()
- local welcome1 = "Welcime aboard < "
- local welcome = "Welcome aboard < " .. ship_name .. " >, admiral"
- self:DrawStringAt(
- (self.width - welcome:len()) / 2,
- y,
- welcome1
- )
- self:DrawStringAt(
- (self.width - welcome:len()) / 2 + welcome1:len(),
- y,
- ship_name,
- self.error_font_color
- )
- self:DrawStringAt(
- (self.width - welcome:len()) / 2 + welcome1:len() + ship_name:len(),
- y,
- " >, admiral"
- )
- self:DrawCoreChargeGear(core_energy)
- self:DrawCrewWidget(crew)
- end
- function Screen.new(at_max_resolution)
- local screen = {}
- setmetatable(screen, Screen)
- if at_max_resolution then
- screen:SetMaxResolution()
- end
- screen.width, screen.height = screen:GetResolution()
- screen.colors = require("colors")
- screen.term = require("term")
- screen.saved_background_color, screen.saved_background_index = screen:GetBackgroundColor()
- screen.saved_foreground_color, screen.saved_foreground_index = screen:GetForegroundColor()
- screen.color_depth = screen:GetColorBitDepth()
- if screen.color_depth == 1 then
- os.exit()
- screen.background_color = 0x000000
- screen.main_font_color = 0xFFFFFF
- screen.error_font_color = 0xFFFFFF
- elseif screen.color_depth >= 8 then
- screen.background_color = 0x3060A0
- screen.background_color=0;
- screen.main_font_color = 0xFFFFFF
- screen.error_font_color = 0xFFFF00
- screen.core_charge_colors =
- {
- [0] = 0xFF0000,
- [1] = 0xFFFF00,
- [2] = 0x00FF00
- }
- end
- return screen
- end
- -- ----------
- -- ---------- WARPDRIVE class
- WarpDrive = {}
- WarpDrive.__index = WarpDrive
- function WarpDrive:GetController()
- if component.isAvailable("warpdriveShipController") then
- -- return component.warpdriveShipController
- local ctrl = component.list("warpdriveShipController")()
- if (ctrl ~= nil) then
- return component.proxy(ctrl)
- end
- end
- return nil
- end
- function WarpDrive:IsNameSet()
- return self:GetName() ~= "default"
- end
- function WarpDrive:GetName()
- local ctrl = self:GetController()
- if (ctrl) then
- return ctrl.coreFrequency()
- end
- return nil
- end
- function WarpDrive:SetName(name)
- local ctrl = self:GetController()
- if (ctrl) then
- ctrl.coreFrequency(name)
- if (ctrl.coreFrequency() == name) then
- return true
- end
- end
- return false
- end
- function WarpDrive:GetCrew()
- local ctrl = self:GetController()
- if (ctrl) then
- local str, list = ctrl.getAttachedPlayers()
- return list
- end
- return nil
- end
- function WarpDrive:Update()
- local ctrl = self:GetController()
- self.controller_error = false
- if (ctrl == nil) then
- self.controller_error = true
- return
- end
- self.energy, self.max_energy = ctrl.energy()
- self.core_error = false
- if self.energy == nil or self.max_energy == nil then
- self.core_error = true
- end
- end
- function WarpDrive.new()
- warpdrive = {}
- setmetatable(warpdrive, WarpDrive)
- -- for i, value in pairs(component.list()) do
- -- print(i .. ": " .. value)
- -- end
- local ctrl = warpdrive:GetController()
- if ctrl == nil then
- return nil
- end
- warpdrive:Update()
- return warpdrive
- end
- -- ----------
- local shell = require("shell")
- local args = shell.parse(...)
- screen = Screen.new(true)
- if args[1] ~= "nosplash" then
- screen:Clear()
- screen:DrawSplash()
- -- os.sleep(4)
- end
- warpdrive = WarpDrive.new()
- while warpdrive == nil do
- screen:Clear()
- screen:DrawNoWarpDrive()
- warpdrive = WarpDrive.new()
- end
- -- ----------
- while true do
- warpdrive:Update()
- if warpdrive.core_error then
- screen:DrawCoreError("WarpDrive Core malfunction")
- elseif warpdrive.controller_error then
- screen:DrawCoreError("WarpDrive Ship Controller malfunction")
- else
- if (not warpdrive:IsNameSet()) then
- local new_name = screen:DrawPrompt("Enter ship name:", warpdrive.energy / warpdrive.max_energy)
- if (new_name ~= nil) then
- warpdrive:SetName(new_name)
- end
- else
- screen:DrawMainMenu(warpdrive:GetName(), warpdrive:GetCrew(), warpdrive.energy / warpdrive.max_energy)
- end
- end
- os.sleep(0.18)
- end
- screen:Release()
- --while true do
- -- local _, _, x, y = event.pull("touch");
- -- print(x, y);
- --end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement