Advertisement
Guest User

gps

a guest
Dec 8th, 2016
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.50 KB | None | 0 0
  1.  
  2. local function printUsage()
  3.     print( "Usages:" )
  4.     print( "gps host" )
  5.     print( "gps host <x> <y> <z>" )
  6.     print( "gps locate" )
  7. end
  8.  
  9. local tArgs = { ... }
  10. if #tArgs < 1 then
  11.     printUsage()
  12.     return
  13. end
  14.    
  15.  local sCommand = tArgs[1]
  16. if sCommand == "locate" then
  17.     -- "gps locate"
  18.     -- Just locate this computer (this will print the results)
  19.     gps.locate( 2, true )
  20.    
  21. elseif sCommand == "host" then
  22.     -- "gps host"
  23.     -- Act as a GPS host
  24.     if pocket then
  25.         print( "GPS Hosts must be stationary" )
  26.         return
  27.     end
  28.  
  29.     -- Find a modem
  30.     local sModemSide = nil
  31.     for n,sSide in ipairs( rs.getSides() ) do
  32.         if peripheral.getType( sSide ) == "modem" and peripheral.call( sSide, "isWireless" ) then  
  33.             sModemSide = sSide
  34.             break
  35.         end
  36.     end
  37.  
  38.     if sModemSide == nil then
  39.         print( "No wireless modems found. 1 required." )
  40.         return
  41.     end
  42.  
  43.     -- Determine position
  44.     local x,y,z
  45.     if #tArgs >= 4 then
  46.         -- Position is manually specified
  47.         x = tonumber(tArgs[2])
  48.         y = tonumber(tArgs[3])
  49.         z = tonumber(tArgs[4])
  50.         if x == nil or y == nil or z == nil then
  51.             printUsage()
  52.             return
  53.         end
  54.         print( "Position is "..x..","..y..","..z )
  55.     else
  56.         -- Position is to be determined using locate       
  57.         x,y,z = gps.locate( 2, true )
  58.         if x == nil then
  59.             print( "Run \"gps host <x> <y> <z>\" to set position manually" )
  60.             if bCloseChannel then
  61.                 print( "Closing GPS channel" )
  62.                 modem.close( gps.CHANNEL_GPS )
  63.             end
  64.             return
  65.         end
  66.     end
  67.    
  68.     -- Open a channel
  69.     local modem = peripheral.wrap( sModemSide )
  70.     print( "Opening channel on modem "..sModemSide )
  71.     modem.open( gps.CHANNEL_GPS )
  72.  
  73.     -- Serve requests indefinately
  74.     local nServed = 0
  75.     while true do
  76.         local e, p1, p2, p3, p4, p5 = os.pullEvent( "modem_message" )
  77.         if e == "modem_message" then
  78.             -- We received a message from a modem
  79.             local sSide, sChannel, sReplyChannel, sMessage, nDistance = p1, p2, p3, p4, p5
  80.             if sSide == sModemSide and sChannel == gps.CHANNEL_GPS and sMessage == "PING" and nDistance then
  81.                 -- We received a ping message on the GPS channel, send a response
  82.                 modem.transmit( sReplyChannel, gps.CHANNEL_GPS, { x, y, z } )
  83.            
  84.                 -- Print the number of requests handled
  85.                 nServed = nServed + 1
  86.                 if nServed > 1 then
  87.                     local x,y = term.getCursorPos()
  88.                     term.setCursorPos(1,y-1)
  89.                 end
  90.                 print( nServed.." GPS requests served" )
  91.             end
  92.         end
  93.     end
  94.  
  95.     -- Close the channel
  96.     print( "Closing channel" )
  97.     modem.close( gps.CHANNEL_GPS )
  98.  
  99. else
  100.     -- "gps somethingelse"
  101.     -- Error
  102.     printUsage()
  103.    
  104. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement