Advertisement
Guest User

vla_receiver.lua

a guest
Dec 11th, 2019
486
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.34 KB | None | 0 0
  1. local modem = peripheral.find("modem", function(n,o) return o.isWireless() end)
  2. local origin = nil
  3. local toid = 31415
  4. modem.open(toid)
  5.  
  6. local function printHeader(str, ny)
  7.     local w, h = term.getSize()
  8.     local x, y = term.getCursorPos()
  9.     term.setCursorPos(math.ceil(w/2) - math.floor(#str/2), ny or 1)
  10.     term.clearLine()
  11.     term.write(str)
  12.     term.setCursorPos(x, y)
  13. end
  14.  
  15. local function select_caster()
  16.     print("Scanning for casters")
  17.  
  18.     local pingmsg = "Cast_Ping"
  19.     local origins = {}
  20.     local timeout = os.startTimer(1)
  21.  
  22.     modem.transmit(0, 0, pingmsg)
  23.     while true do
  24.         local event, side, chan, reply, msg = os.pullEvent()
  25.         if event == "modem_message" and chan == toid and type(msg) == "table" and msg.origin and msg.senderChannel == 0 and msg.replyChannel == 0 and msg.dimension and type(msg.message) == "string" and msg.message == pingmsg then
  26.             origins[#origins+1] = msg.origin
  27.         elseif event == "timer" and side == timeout then break
  28.         end
  29.     end
  30.  
  31.     if #origins == 0 then return false
  32.     elseif #origins == 1 then return origins[1]
  33.     end
  34.  
  35.     local sel = 1
  36.     while true do
  37.         term.clear()
  38.         term.setCursorPos(1,1)
  39.         term.write("Select a caster")
  40.         for i = 1, #origins do
  41.             term.setCursorPos(1, i+2)
  42.             term.write(("%s%s%s"):format(i==sel and "[" or " ", origins[i], i==sel and "]" or " "))
  43.         end
  44.         local event, char = os.pullEvent("key")
  45.         if char == keys.up then sel = sel-1
  46.         elseif char == keys.down then sel = sel+1
  47.         elseif char == keys.enter then break
  48.         end
  49.         sel = math.max(1, math.min(#origins, sel))
  50.     end
  51.     return origins[sel]
  52. end
  53.  
  54. term.setTextColor(colors.yellow)
  55. if not origin then
  56.     origin = select_caster()
  57.     if not origin then
  58.         error("No casters found", 0)
  59.     end
  60. end
  61.  
  62. term.clear()
  63. term.setCursorPos(1, 3)
  64. while true do
  65.     printHeader("Connected to", 1)
  66.     printHeader(origin, 2)
  67.     local event, side, chan, reply, msg = os.pullEvent()
  68.     if event == "modem_message" then
  69.         if chan == toid and type(msg) == "table" and msg.origin == origin and msg.senderChannel and msg.replyChannel and msg.message and msg.dimension then
  70.             os.queueEvent("global_message", msg.senderChannel, msg.replyChannel, msg.message, msg.senderDistance)
  71.         end
  72.     elseif event == "key" then
  73.         if side == keys.q then sleep(0) return
  74.         elseif side == keys.c then
  75.             origin = select_caster() or origin
  76.             term.clear()
  77.             term.setCursorPos(1, 3)
  78.         end
  79.     end
  80. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement