Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local cChannel = 15150
- local tArgs = {...}
- if #tArgs < 3 then
- printError("Usage: " .. shell.getRunningProgram() .. " <x> <y> <z>")
- return
- end
- local coords = {tonumber(tArgs[1]) or 0, tonumber(tArgs[2]) or 0, tonumber(tArgs[3]) or 0}
- local function openChannel(channel)
- for i,v in pairs(peripheral.getNames()) do
- if peripheral.getType(v) == "modem" then
- peripheral.call(v, "open", channel)
- end
- end
- end
- local function closeChannel(channel)
- for i,v in pairs(peripheral.getNames()) do
- if peripheral.getType(v) == "modem" then
- peripheral.call(v, "close", channel)
- end
- end
- end
- local function closeAll()
- for i,v in pairs(peripheral.getNames()) do
- if peripheral.getType(v) == "modem" then
- peripheral.call(v, "closeAll")
- end
- end
- end
- local function transmit(channel, reply, message)
- local modem = peripheral.find("modem")
- if modem then
- modem.transmit(channel, reply, message)
- end
- end
- local channels = {}
- openChannel(cChannel)
- local lastMessages = {}
- print("Hosting reverse GPS at (" .. coords[1] .. ", " .. coords[2] .. ", " .. coords[3] .. ")")
- local running = true
- while running do
- local success, err = pcall(function()
- local event, side, channel, reply, message, distance = os.pullEventRaw()
- if event == "modem_message" then
- if channel == cChannel and type(message) == "table" then
- if reply == 0 then
- if message.openChannel then
- openChannel(message.channel)
- table.insert(channels, message.channel)
- print("Opened channel " .. message.channel)
- elseif message.closeChannel then
- closeChannel(message.channel)
- local toRemove = {}
- for _,channel in ipairs(channels) do
- if channel == message.channel then
- table.insert(toRemove, 1, _)
- end
- end
- for _,i in ipairs(toRemove) do
- table.remove(channels, i)
- end
- print("Closed channel " .. message.channel)
- elseif message.closeAll then
- closeAll()
- channels = {}
- openChannel(cChannel)
- print("Closed all channels")
- elseif message.update then
- local file = fs.open(shell.getRunningProgram(), "w")
- file.write(message.source)
- file.close()
- os.reboot()
- elseif message.discard then
- local last = lastMessages[message.channel]
- if last then
- last = last[message.reply]
- if last and #last ~= 0 then
- table.remove(last, 1)
- end
- end
- elseif message.getDistance then
- local last = lastMessages[message.channel]
- if last then
- last = last[message.reply]
- if last and #last ~= 0 then
- table.insert(last[1], message.id)
- transmit(cChannel, 1, last[1])
- table.remove(last, 1)
- print("Sent gps data")
- end
- end
- elseif message.clear then
- lastMessages = {}
- print("Cleared messages")
- end
- end
- else
- if not lastMessages[channel] then
- lastMessages[channel] = {}
- end
- if not lastMessages[channel][reply] then
- lastMessages[channel][reply] = {}
- end
- table.insert(lastMessages[channel][reply], {coords, distance})
- end
- elseif event == "key" then
- if side == keys.q then
- os.pullEvent("char")
- running = false
- end
- elseif event == "peripheral" then
- if peripheral.getType(side) == "modem" then
- for _,channel in pairs(channels) do
- peripheral.call(side, "open", channel)
- end
- peripheral.call(side, "open", cChannel)
- end
- end
- end)
- if not success then
- print("Error: " .. err)
- end
- end
Add Comment
Please, Sign In to add comment