Advertisement
Maro919

tp.lua

Nov 12th, 2024 (edited)
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.02 KB | Gaming | 0 0
  1. local list = require("/usr/lib/list")
  2. local display = {}
  3. local destinations = {}
  4. local idMap = {}
  5. local nameMap = {}
  6. local names = {}
  7.  
  8. local name
  9.  
  10. local scale = 1
  11.  
  12. function display:init(parent)
  13.     local w, h = parent.getSize()
  14.     parent.setCursorPos(1, 1)
  15.     parent.setTextColor(colors.yellow)
  16.     parent.setBackgroundColor(colors.blue)
  17.     parent.clearLine()
  18.     parent.write(name)
  19.     parent.setTextColor(colors.white)
  20.     parent.setBackgroundColor(colors.black)
  21.     self.displayList = list:create(parent, 1, 2, w, h - 1)
  22.     self.displayList:render()
  23. end
  24.  
  25. function display:draw()
  26.     self.displayList:draw()
  27. end
  28.  
  29. function display:update(names)
  30.     self.displayList:edit(names)
  31.     self.displayList:render()
  32. end
  33.  
  34. local function pulseRedstone()
  35.     rs.setOutput("bottom", true)
  36.     os.sleep(0)
  37.     rs.setOutput("bottom", false)
  38.     os.sleep(0)
  39. end
  40.  
  41. local function convertDestinations()
  42.     idMap = {}
  43.     nameMap = {}
  44.     names = {}
  45.     for k, v in pairs(destinations) do
  46.         table.insert(idMap, k)
  47.         table.insert(nameMap, v)
  48.         table.insert(names, k .. " - " .. v)
  49.     end
  50. end
  51.  
  52. local function handleInput()
  53.     --[[ Trigger for refresh timer ]]--
  54.     local timer = os.startTimer(0)
  55.     while true do
  56.         local event, p1, p2, p3 = os.pullEvent()
  57.         if event == "mouse_click" then
  58.             local index = display.displayList:click(p2, p3)
  59.             if index and rednet.lookup("tp", nameMap[index]) then
  60.                 --[[ To be improved ]]--
  61.                 pulseRedstone()
  62.                 pulseRedstone()
  63.                 rednet.send(idMap[index], nil, "tp")
  64.             end
  65.         elseif event == "monitor_touch" then
  66.             os.queueEvent("mouse_click", 1, p2, p3)
  67.         elseif event == "mouse_scroll" then
  68.             display.displayList:scroll(p1)
  69.         elseif event == "rednet_message" then
  70.             local sid, msg, prot = p1, p2, p3
  71.             if prot == "tp" then
  72.                 --[[ Future hook for better tp ]]--
  73.                 sleep(2)
  74.                 pulseRedstone()
  75.                 pulseRedstone()
  76.             elseif prot == "dns" then
  77.                 if type(msg) == "table" and msg.sType == "lookup response" and msg.sProtocol == "tp" and type(msg.sHostname) == "string" then
  78.                     destinations[sid] = msg.sHostname
  79.                     convertDestinations()
  80.                     display:update(names)
  81.                 end
  82.             end
  83.         elseif event == "timer" then
  84.             if p1 == timer then
  85.                 --[[ Periodically check available teleporters ]]--
  86.                 rednet.broadcast({
  87.                     sType = "lookup",
  88.                     sProtocol = "tp",
  89.                 }, "dns")
  90.                 --timer = os.startTimer(120)
  91.                 timer = nil
  92.             end
  93.         end
  94.         display:draw()
  95.     end
  96. end
  97.  
  98. local function main()
  99.     local modem = peripheral.find("modem", function(name, modem)
  100.         rednet.open(name)
  101.         return modem
  102.     end)
  103.     if not modem then
  104.         error("Modem not found.", 3)
  105.     end
  106.  
  107.     local port = peripheral.find("ae2:spatial_io_port")
  108.  
  109.     if port then
  110.         name = settings.get("tp.name")
  111.         if not name or type(name) ~= "string" then
  112.             error("Teleport name not configured.", 3)
  113.         end
  114.         rednet.host("tp", name)
  115.  
  116.         --[[ Announce presence ]]--
  117.         rednet.broadcast({
  118.             sType = "lookup response",
  119.             sProtocol = "tp",
  120.             sHostname = name,
  121.         }, "dns")
  122.     else
  123.         name = "Teleport Remote"
  124.     end
  125.  
  126.     local monitor = peripheral.find("monitor")
  127.     if monitor then
  128.         monitor.setTextScale(scale)
  129.         display:init(monitor)
  130.     else
  131.         display:init(term.current())
  132.     end
  133.  
  134.     --destinations[os.getComputerID()] = name
  135.     convertDestinations()
  136.     display:update(names)
  137.     -- TODO return cell to system if stuck in input
  138.  
  139.     --[[ Future hook for secure connection daemon ]]--
  140.     parallel.waitForAny(handleInput)
  141. end
  142.  
  143. main()
  144.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement