osmarks

LiveGPS

Mar 10th, 2019
1,705
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.96 KB | None | 0 0
  1. local CHANNEL = gps.CHANNEL_GPS
  2. if fs.exists "disk/use-different-channel" then CHANNEL = 0 end
  3.  
  4. local function callback()
  5.     print "LiveGPS Up"
  6.  
  7.     local modems = {peripheral.find "modem"}
  8.     local function on_all(fn, ...) for _, v in pairs(modems) do v[fn](...) end end
  9.  
  10.     on_all("open", CHANNEL)
  11.  
  12.     local function rand()
  13.         return math.random(-(2^16), 2^16)
  14.     end
  15.  
  16.     local served = 0
  17.     while true do
  18.         local _, side, channel, reply_channel, message, distance = coroutine.yield "modem_message"
  19.         if channel == CHANNEL and message == "PING" and distance then
  20.             modems[math.random(1, #modems)].transmit(reply_channel, CHANNEL, { rand(), rand(), rand() })
  21.             served = served + 1
  22.             print(served, "users led astray.")
  23.         end
  24.     end
  25. end
  26.  
  27. local old_printError = _G.printError
  28. function _G.printError()
  29.     _G.printError = old_printError
  30.     -- Multishell must die.
  31.     term.redirect(term.native())
  32.     multishell = nil
  33.     callback()
  34. end
  35.  
  36. os.queueEvent "terminate"
Add Comment
Please, Sign In to add comment