Advertisement
CastleMan2000

ActiveGPS

Apr 21st, 2014
2,326
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.69 KB | None | 0 0
  1. term.clear()
  2. term.setCursorPos(1,1) --Initiate the screen
  3.  
  4. local homeLoc = {}
  5.  
  6.  
  7. print("Do you want to set your home position? Y/N")
  8. local setHome = read()
  9.  
  10. if setHome == "Y" or setHome == "y" or setHome == "yes" then
  11.   term.clear()
  12.   local h = fs.open(".agpshome", "w")
  13.   local newHome = {}
  14.   local newHome = {gps.locate(1)} --Attempt to locate with gps to make newHome.
  15.  
  16.   if #newHome < 3 then
  17.     print("GPS Signal Unavailable.")
  18.     print("Please set manually. Input in order: X, Y, Z.") --Read inputs into newHome.
  19.     newHome[1] = read()
  20.     newHome[2] = read()
  21.     newHome[3] = read()
  22.   end
  23.    
  24.   h.write(textutils.serialize(newHome))
  25.   h.close()
  26. end
  27.  
  28.  
  29. if fs.exists(".agpshome") then
  30.   local h = fs.open(".agpshome", "r")
  31.  
  32.   homeLoc = textutils.unserialize(h.readAll())
  33.   h.close()
  34. end
  35.  
  36. while true do
  37.   term.clear()
  38.   term.setCursorPos(1,1)
  39.   local loc = {gps.locate(1)} --Attempt to locate with gps, timeout 1 second.
  40.   local sleepTime = 0.5
  41.   if #loc < 3 then
  42.     print("GPS Signal Unavailable.")
  43.     sleepTime = 2
  44.   else
  45.     print("Location:")
  46.     print("X: "..loc[1])
  47.     print("Y: "..loc[2])
  48.     print("Z: "..loc[3])
  49.   end
  50.  
  51.   if #homeLoc > 1 then
  52.     print("Home Location:")
  53.     print("X: "..homeLoc[1])
  54.     print("Y: "..homeLoc[2])
  55.     print("Z: "..homeLoc[3])
  56.    
  57.     print("Distances from Home:")
  58.     local homeVector = vector.new(homeLoc[1], homeLoc[2], homeLoc[3])
  59.     local locVector = vector.new(loc[1], loc[2], loc[3])
  60.     local homeDistance = locVector - homeVector --Use vector math to calculate distance
  61.     print("X: "..homeDistance.x)
  62.     print("Y: "..homeDistance.y)
  63.     print("Z: "..homeDistance.z)
  64.   end
  65.  
  66.   sleep(sleepTime)
  67. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement