Alakazard12

Reverse GPS

Dec 29th, 2015
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.55 KB | None | 0 0
  1. local cChannel = 15150
  2.  
  3. local tArgs = {...}
  4. if #tArgs < 3 then
  5.     printError("Usage: " .. shell.getRunningProgram() .. " <x> <y> <z>")
  6.     return
  7. end
  8.  
  9. local coords = {tonumber(tArgs[1]) or 0, tonumber(tArgs[2]) or 0, tonumber(tArgs[3]) or 0}
  10.  
  11. local function openChannel(channel)
  12.     for i,v in pairs(peripheral.getNames()) do
  13.         if peripheral.getType(v) == "modem" then
  14.             peripheral.call(v, "open", channel)
  15.         end
  16.     end
  17. end
  18.  
  19. local function closeChannel(channel)
  20.     for i,v in pairs(peripheral.getNames()) do
  21.         if peripheral.getType(v) == "modem" then
  22.             peripheral.call(v, "close", channel)
  23.         end
  24.     end
  25. end
  26.  
  27. local function closeAll()
  28.     for i,v in pairs(peripheral.getNames()) do
  29.         if peripheral.getType(v) == "modem" then
  30.             peripheral.call(v, "closeAll")
  31.         end
  32.     end
  33. end
  34.  
  35. local function transmit(channel, reply, message)
  36.     local modem = peripheral.find("modem")
  37.     if modem then
  38.         modem.transmit(channel, reply, message)
  39.     end
  40. end
  41.  
  42. local channels = {}
  43.  
  44. openChannel(cChannel)
  45.  
  46. local lastMessages = {}
  47.  
  48. print("Hosting reverse GPS at (" .. coords[1] .. ", " .. coords[2] .. ", " .. coords[3] .. ")")
  49.  
  50. local running = true
  51. while running do
  52.     local success, err = pcall(function()
  53.         local event, side, channel, reply, message, distance = os.pullEventRaw()
  54.         if event == "modem_message" then
  55.             if channel == cChannel and type(message) == "table" then
  56.                 if reply == 0 then
  57.                     if message.openChannel then
  58.                         openChannel(message.channel)
  59.                         table.insert(channels, message.channel)
  60.                         print("Opened channel " .. message.channel)
  61.                     elseif message.closeChannel then
  62.                         closeChannel(message.channel)
  63.                         local toRemove = {}
  64.                         for _,channel in ipairs(channels) do
  65.                             if channel == message.channel then
  66.                                 table.insert(toRemove, 1, _)
  67.                             end
  68.                         end
  69.                         for _,i in ipairs(toRemove) do
  70.                             table.remove(channels, i)
  71.                         end
  72.                         print("Closed channel " .. message.channel)
  73.                     elseif message.closeAll then
  74.                         closeAll()
  75.                         channels = {}
  76.                         openChannel(cChannel)
  77.                         print("Closed all channels")
  78.                     elseif message.update then
  79.                         local file = fs.open(shell.getRunningProgram(), "w")
  80.                         file.write(message.source)
  81.                         file.close()
  82.                         os.reboot()
  83.                     elseif message.discard then
  84.                         local last = lastMessages[message.channel]
  85.                         if last then
  86.                             last = last[message.reply]
  87.                             if last and #last ~= 0 then
  88.                                 table.remove(last, 1)
  89.                             end
  90.                         end
  91.                     elseif message.getDistance then
  92.                         local last = lastMessages[message.channel]
  93.                         if last then
  94.                             last = last[message.reply]
  95.                             if last and #last ~= 0 then
  96.                                 table.insert(last[1], message.id)
  97.                                 transmit(cChannel, 1, last[1])
  98.                                 table.remove(last, 1)
  99.                                 print("Sent gps data")
  100.                             end
  101.                         end
  102.                     elseif message.clear then
  103.                         lastMessages = {}
  104.                         print("Cleared messages")
  105.                     end
  106.                 end
  107.             else
  108.                 if not lastMessages[channel] then
  109.                     lastMessages[channel] = {}
  110.                 end
  111.                 if not lastMessages[channel][reply] then
  112.                     lastMessages[channel][reply] = {}
  113.                 end
  114.                 table.insert(lastMessages[channel][reply], {coords, distance})
  115.             end
  116.         elseif event == "key" then
  117.             if side == keys.q then
  118.                 os.pullEvent("char")
  119.                 running = false
  120.             end
  121.         elseif event == "peripheral" then
  122.             if peripheral.getType(side) == "modem" then
  123.                 for _,channel in pairs(channels) do
  124.                     peripheral.call(side, "open", channel)
  125.                 end
  126.                 peripheral.call(side, "open", cChannel)
  127.             end
  128.         end
  129.     end)
  130.  
  131.     if not success then
  132.         print("Error: " .. err)
  133.     end
  134. end
Add Comment
Please, Sign In to add comment