ShicKla

Auspex Rings System

Dec 8th, 2021 (edited)
1,044
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --[[
  2. Auspex Rings System
  3. v1.0.1
  4. Created By: Augur ShicKla
  5.  
  6. System Requirements:
  7. Tier 2 GPU
  8. Tier 2 Screen
  9. ]]--
  10.  
  11. -- Each admin must be encased in quotes and separated by commas
  12. local AdminList = {"exampleName","ShicKla"}
  13.  
  14. local unicode = require("unicode")
  15. local event = require("event")
  16. local computer = require("computer")
  17. local component = require("component")
  18. local term = require("term")
  19. local gpu = component.gpu
  20. local screen = component.screen
  21. local MainLoop = true
  22. local status, err = nil
  23. local Events = {}
  24.  
  25. -- Checking System Requirements are Met --------------------------------------------
  26. if gpu.maxResolution() < 80 then
  27.   io.stderr:write("Tier 2 GPU and Screen Required")
  28.   os.exit(1)
  29. end
  30. if not component.isAvailable("transportrings") then
  31.   io.stderr:write("No Transport Rings Connected.")
  32.   os.exit(1)
  33. end
  34. -- End of Checking System Requirements ---------------------------------------------
  35.  
  36. local OriginalResolution = {gpu.getResolution()}
  37. local OrignalColorDepth = gpu.getDepth()
  38.  
  39. while MainLoop do
  40.   status, err = pcall(function()
  41.     local rings = component.transportrings
  42.     local ActiveButtons = {}
  43.     local DestinationButtons = {}
  44.     local AvailableRings = {}
  45.     local TransportInterlock = false
  46.     local User = ""
  47.     local resultMessages = {
  48.     OK = "OK",
  49.     BUSY = "BUSY",
  50.     BUSY_TARGET = "Target Rings Platform is Busy",
  51.     OBSTRUCTED = "Rings Platform is Obstructed",
  52.     OBSTRUCTED_TARGET = "Target Rings Platform is Obstructed",
  53.     NO_SUCH_ADDRESS = "NO_SUCH_ADDRESS"}
  54.  
  55. -- Button Object -------------------------------------------------------------------
  56.     local Button = {}
  57.     Button.__index = Button
  58.     function Button.new(xPos, yPos, width, height, label, func, border)
  59.       local self = setmetatable({}, Button)
  60.       if xPos < 1 or xPos > term.window.width then xPos = 1 end
  61.       if yPos < 1 or yPos > term.window.height then yPos = 1 end
  62.       if (width-2) < unicode.len(label) then width = unicode.len(label)+2 end
  63.       if height < 3 then height = 3 end
  64.       if border == nil then
  65.         self.border = true
  66.       else
  67.         self.border = border
  68.       end
  69.       self.xPos = xPos
  70.       self.yPos = yPos
  71.       self.width = width
  72.       self.height = height
  73.       self.label = label
  74.       self.func = func
  75.       self.visible = false
  76.       self.disabled = false
  77.       return self
  78.     end
  79.  
  80.     function Button.display(self, x, y)
  81.       table.insert(ActiveButtons, 1, self)
  82.       if (self.width-2) < unicode.len(self.label) then self.width = unicode.len(self.label)+2 end
  83.       if x ~= nil and y ~= nil then
  84.         self.xPos = x
  85.         self.yPos = y
  86.       end
  87.       if self.border then
  88.         gpu.fill(self.xPos+1, self.yPos, self.width-2, 1, "─")
  89.         gpu.fill(self.xPos+1, self.yPos+self.height-1, self.width-2, 1, "─")
  90.         gpu.fill(self.xPos, self.yPos+1, 1, self.height-2, "│")
  91.         gpu.fill(self.xPos+self.width-1, self.yPos+1, 1, self.height-2, "│")
  92.         gpu.set(self.xPos, self.yPos, "┌")
  93.         gpu.set(self.xPos+self.width-1, self.yPos, "┐")
  94.         gpu.set(self.xPos, self.yPos+self.height-1, "└")
  95.         gpu.set(self.xPos+self.width-1, self.yPos+self.height-1, "┘")
  96.       end
  97.       -- gpu.set(self.xPos+1, self.yPos+1, self.label)
  98.       gpu.set(self.xPos+(math.ceil((self.width)/2)-math.ceil(unicode.len(self.label)/2)), self.yPos+1, self.label)
  99.       self.visible = true
  100.     end
  101.  
  102.     function Button.hide(self)
  103.       self.visible = false
  104.       for i,v in ipairs(ActiveButtons) do
  105.         if v == self then table.remove(ActiveButtons, i) end
  106.       end
  107.       if self.border then
  108.         gpu.fill(self.xPos, self.yPos, self.width, self.height, " ")
  109.       else
  110.         gpu.fill(self.xPos+1, self.yPos+1, self.width-2, 1, " ")
  111.       end
  112.     end
  113.  
  114.     function Button.disable(self, bool)
  115.       if bool == nil then
  116.         self.disabled = false
  117.       else
  118.         self.disabled = bool
  119.       end
  120.       if self.disabled then gpu.setForeground(0x3C3C3C) end
  121.       if self.visible then self:display() end
  122.       gpu.setForeground(0xFFFFFF)
  123.     end
  124.  
  125.     function Button.touch(self, x, y)
  126.       local wasTouched = false
  127.       if self.visible and not self.disabled then  
  128.         if self.border then
  129.           if x >= self.xPos and x <= (self.xPos+self.width-1) and y >= self.yPos and y <= (self.yPos+self.height-1) then wasTouched = true end
  130.         else
  131.           if x >= self.xPos+1 and x <= (self.xPos+self.width-2) and y >= self.yPos+1 and y <= (self.yPos+self.height-2) then wasTouched = true end
  132.         end
  133.       end
  134.       if wasTouched then
  135.         gpu.setBackground(0x3C3C3C)
  136.         gpu.fill(self.xPos+1, self.yPos+1, self.width-2, self.height-2, " ")
  137.         gpu.set(self.xPos+(math.ceil((self.width)/2)-math.ceil(unicode.len(self.label)/2)), self.yPos+1, self.label)
  138.         gpu.setBackground(0x000000)
  139.         local success, msg = pcall(self.func)
  140.         if self.visible then
  141.           gpu.fill(self.xPos+1, self.yPos+1, self.width-2, self.height-2, " ")
  142.           gpu.set(self.xPos+(math.ceil((self.width)/2)-math.ceil(unicode.len(self.label)/2)), self.yPos+1, self.label)
  143.         end
  144.         if not success then
  145.           HadNoError = false
  146.           ErrorMessage = debug.traceback(msg)
  147.         end
  148.       end
  149.       return wasTouched
  150.     end
  151. -- End of Button Object ------------------------------------------------------------
  152.  
  153. -- Event Handlers ------------------------------------------------------------------
  154.    
  155.  
  156.     Events.transportrings_teleport_start = event.listen("transportrings_teleport_start", function(_, address, caller, initiating)
  157.       TransportInterlock = true
  158.       for i,v in ipairs(DestinationButtons) do
  159.         v:disable(true)
  160.       end
  161.     end)
  162.  
  163.     Events.transportrings_teleport_finished = event.listen("transportrings_teleport_finished", function(_, address, caller, initiating)
  164.       TransportInterlock = false
  165.       for i,v in ipairs(DestinationButtons) do
  166.         v:disable(false)
  167.       end
  168.     end)
  169.  
  170.     Events.touch = event.listen("touch", function(_, screenAddress, x, y, button, playerName)
  171.       if button == 0 then
  172.         for i,v in ipairs(ActiveButtons) do
  173.           if v:touch(x,y) then break end
  174.         end
  175.       end
  176.     end)
  177.  
  178.     Events.key_down = event.listen("key_down", function(_, keyboardAddress, chr, code, playerName)
  179.       User = playerName
  180.     end)
  181.  
  182.     Events.interrupted = event.listen("interrupted", function()
  183.       local shouldExit = false
  184.       for i,v in ipairs(AdminList) do
  185.         if v == User then shouldExit = true end
  186.       end
  187.       if shouldExit then MainLoop = false end
  188.     end)
  189. -- End of Event Handlers -----------------------------------------------------------
  190.  
  191. -- Main Procedures -----------------------------------------------------------------
  192.     local ringName = rings.getName()
  193.     if ringName == "NOT_IN_GRID" then error("Transport Rings Missing Address and Name", 0) end
  194.     if ringName == "" then ringName = tostring("Ring Platform "..rings.getAddress()) end
  195.     gpu.setResolution(36,18)
  196.     gpu.setDepth(4)
  197.     term.clear()
  198.     screen.setTouchModeInverted(true)
  199.     AvailableRings = rings.getAvailableRings()
  200.     gpu.setBackground(0x4B4B4B)
  201.     gpu.fill(1,1,36,3," ")
  202.     gpu.set(19-math.ceil(unicode.len(ringName)/2), 2, ringName)
  203.     gpu.setBackground(0x000000)
  204.  
  205.     local function updateButtons()
  206.       local buttonCount = 1
  207.       for k,v in pairs(AvailableRings) do
  208.         if v == "" then v = "Ring Platform "..tostring(k) end
  209.         DestinationButtons[buttonCount] = Button.new(1, (buttonCount-1)*3+4, 36, 0, v, function()
  210.           local result = rings.attemptTransportTo(k)
  211.           if result ~= "OK" then
  212.             computer.beep()
  213.             local msgString = resultMessages[result]
  214.             gpu.setBackground(0xFF0000)
  215.             gpu.fill(1,1,36,3," ")
  216.             gpu.set(19-math.ceil(unicode.len(msgString)/2), 2, msgString)
  217.             gpu.setBackground(0x000000)
  218.             os.sleep(3)
  219.             gpu.setBackground(0x4B4B4B)
  220.             gpu.fill(1,1,36,3," ")
  221.             gpu.set(19-math.ceil(unicode.len(ringName)/2), 2, ringName)
  222.             gpu.setBackground(0x000000)
  223.           end
  224.         end)
  225.         buttonCount = buttonCount + 1
  226.       end
  227.  
  228.       for i,v in ipairs(DestinationButtons) do
  229.         v:display()
  230.       end
  231.     end
  232.  
  233.     updateButtons()
  234.  
  235.     while MainLoop do
  236.       os.sleep(0.05)
  237.     end
  238. -- End Main Procedures -------------------------------------------------------------
  239.   end)
  240. -- Closing Procedures --------------------------------------------------------------
  241.   for k,v in pairs(Events) do
  242.     event.cancel(v)
  243.   end
  244.   screen.setTouchModeInverted(false)
  245.   gpu.setResolution(table.unpack(OriginalResolution))
  246.   gpu.setDepth(OrignalColorDepth)
  247.   term.clear()
  248. end
  249.  
  250.  
  251.  
  252.  
  253.  
  254.  
  255.  
  256.  
RAW Paste Data