Chiddix

Untitled

Nov 23rd, 2012
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.98 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 turtle then
  58. print( "Turtles cannot act as GPS hosts." )
  59. return
  60. end
  61.  
  62. if open() then
  63. local x,y,z
  64. if #tArgs >= 4 then
  65. x = tonumber(tArgs[2])
  66. y = tonumber(tArgs[3])
  67. z = tonumber(tArgs[4])
  68. if x == nil or y == nil or z == nil then
  69. printUsage()
  70. return
  71. end
  72. print( "Position is "..x..","..y..","..z )
  73. else
  74. x,y,z = gps.locate( 2, true )
  75. if x == nil then
  76. print( "Run \"gps host <x> <y> <z>\" to set position manually" )
  77. return
  78. end
  79. end
  80.  
  81. print( "Serving GPS requests" )
  82.  
  83. local nServed = 0
  84. while true do
  85. sender,message,distance = rednet.receive()
  86. if message == "PING" then
  87. rednet.send(sender, textutils.serialize({x,y,z}))
  88.  
  89. nServed = nServed + 1
  90. if nServed > 1 then
  91. local x,y = term.getCursorPos()
  92. term.setCursorPos(1,y-1)
  93. end
  94. print( nServed.." GPS Requests served" )
  95. end
  96. end
  97. end
  98.  
  99. else
  100. printUsage()
  101. return
  102. end
Add Comment
Please, Sign In to add comment