Advertisement
LDDestroier

GPS for turtles

May 8th, 2015
364
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.89 KB | None | 0 0
  1. local function printUsage()
  2.     print( "Usages:" )
  3.     print( "gps host" )
  4.     print( "gps host <x> <y> <z>" )
  5.     print( "gps locate" )
  6. end
  7.  
  8. local tArgs = { ... }
  9. if #tArgs < 1 then
  10.     printUsage()
  11.     return
  12. end
  13.  
  14. local function readNumber()
  15.     local num = nil
  16.     while num == nil do
  17.         num = tonumber(read())
  18.         if not num then
  19.             write( "Not a number. Try again: " )
  20.         end
  21.     end
  22.     return math.floor( num + 0.5 )
  23. end
  24.  
  25. local function open()
  26.     local bOpen, sFreeSide = false, nil
  27.     for n,sSide in pairs(rs.getSides()) do 
  28.         if peripheral.getType( sSide ) == "modem" then
  29.             sFreeSide = sSide
  30.             if rednet.isOpen( sSide ) then
  31.                 bOpen = true
  32.                 break
  33.             end
  34.         end
  35.     end
  36.    
  37.     if not bOpen then
  38.         if sFreeSide then
  39.             print( "No modem active. Opening "..sFreeSide.." modem" )
  40.             rednet.open( sFreeSide )
  41.             return true
  42.         else
  43.             print( "No modem attached" )
  44.             return false
  45.         end
  46.     end
  47.     return true
  48. end
  49.    
  50. local sCommand = tArgs[1]
  51. if sCommand == "locate" then
  52.     if open() then
  53.         gps.locate( 2, true )
  54.     end
  55.    
  56. elseif sCommand == "host" then
  57.     if open() then
  58.         local x,y,z
  59.         if #tArgs >= 4 then
  60.             x = tonumber(tArgs[2])
  61.             y = tonumber(tArgs[3])
  62.             z = tonumber(tArgs[4])
  63.             if x == nil or y == nil or z == nil then
  64.                 printUsage()
  65.                 return
  66.             end
  67.             print( "Position is "..x..","..y..","..z )
  68.         else
  69.             x,y,z = gps.locate( 2, true )
  70.             if x == nil then
  71.                 print( "Run \"gps host <x> <y> <z>\" to set position manually" )
  72.                 return
  73.             end
  74.         end
  75.    
  76.         print( "Serving GPS requests" )
  77.    
  78.         local nServed = 0
  79.         while true do
  80.             sender,message,distance = rednet.receive()
  81.             if message == "PING" then
  82.                 rednet.send(sender, textutils.serialize({x,y,z}))
  83.                
  84.                 nServed = nServed + 1
  85.                 if nServed > 1 then
  86.                     local x,y = term.getCursorPos()
  87.                     term.setCursorPos(1,y-1)
  88.                 end
  89.                 print( nServed.." GPS Requests served" )
  90.             end
  91.         end
  92.     end
  93.    
  94. else
  95.     printUsage()
  96.     return
  97. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement