Advertisement
Maro919

Standard Teleporter Core

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