Wiiplay123

Firewolf GPS Tracker

Feb 13th, 2014
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.95 KB | None | 0 0
  1.  
  2. local CHANNEL_GPS = 65534
  3.  
  4. local function trilaterate( A, B, C )
  5.     local a2b = B.position - A.position
  6.     local a2c = C.position - A.position
  7.        
  8.     if math.abs( a2b:normalize():dot( a2c:normalize() ) ) > 0.999 then
  9.         return nil
  10.     end
  11.    
  12.     local d = a2b:length()
  13.     local ex = a2b:normalize( )
  14.     local i = ex:dot( a2c )
  15.     local ey = (a2c - (ex * i)):normalize()
  16.     local j = ey:dot( a2c )
  17.     local ez = ex:cross( ey )
  18.  
  19.     local r1 = A.distance
  20.     local r2 = B.distance
  21.     local r3 = C.distance
  22.        
  23.     local x = (r1*r1 - r2*r2 + d*d) / (2*d)
  24.     local y = (r1*r1 - r3*r3 - x*x + (x-i)*(x-i) + j*j) / (2*j)
  25.        
  26.     local result = A.position + (ex * x) + (ey * y)
  27.  
  28.     local zSquared = r1*r1 - x*x - y*y
  29.     if zSquared > 0 then
  30.         local z = math.sqrt( zSquared )
  31.         local result1 = result + (ez * z)
  32.         local result2 = result - (ez * z)
  33.        
  34.         local rounded1, rounded2 = result1:round(), result2:round()
  35.         if rounded1.x ~= rounded2.x or rounded1.y ~= rounded2.y or rounded1.z ~= rounded2.z then
  36.             return rounded1, rounded2
  37.         else
  38.             return rounded1
  39.         end
  40.     end
  41.     return result:round()
  42.    
  43. end
  44.  
  45. local function narrow( p1, p2, fix )
  46.     local dist1 = math.abs( (p1 - fix.position):length() - fix.distance )
  47.     local dist2 = math.abs( (p2 - fix.position):length() - fix.distance )
  48.    
  49.     if math.abs(dist1 - dist2) < 0.05 then
  50.         return p1, p2
  51.     elseif dist1 < dist2 then
  52.         return p1:round()
  53.     else
  54.         return p2:round()
  55.     end
  56. end
  57.  
  58. x, y, z = gps.locate()
  59. if x == nil then
  60.     print("Not able to get GPS location.")
  61.     write("Computer X: ")
  62.     x = read()
  63.     write("Computer Y: ")
  64.     y = read()
  65.     write("Computer Z: ")
  66.     z = read()
  67. end
  68. if ({...})[1] ~= nil then
  69.     if ({...])[2] == "server" then
  70.         rednet.open(({...})[1])
  71.         while true do
  72.             local id2, msg = rednet.receive()
  73.             if type(msg) == "table" and msg[1] == "locatefirewolfserver" then
  74.                 rednet.broadcast("firewolf.broadcast.dns.list")
  75.                 while true do
  76.                     local id, msg, dist = rednet.receive(1)
  77.                     if msg == nil or os.clock() > time + 3 then
  78.                         break
  79.                     elseif msg == "firewolf-site:"..msg[2] then
  80.                         rednet.send(id2,{{x,y,z},dist})
  81.                         break
  82.                     end
  83.                 end
  84.             end
  85.         end
  86.         rednet.send(id2,{{x,y,z},dist})
  87.     else
  88.         print("Enter site address.")
  89.         local site = read()
  90.         rednet.open(({...})[1])
  91.         local time = os.clock()
  92.         local positions = {}
  93.         rednet.broadcast({"locatefirewolfserver",site})
  94.         while true do
  95.             local id, msg, dist = rednet.receive(1)
  96.             if msg == nil or os.clock() > time + 4 then
  97.                 break
  98.             elseif type(msg) == "table" then
  99.                 if msg[1] == "serverlocator" then
  100.                     table.insert(positions,{["position"] = msg[2],["distance"] = msg[3]})
  101.                 end
  102.             end
  103.         end
  104.         rednet.close(({...})[1])
  105.         local previous = trilaterate(positions[1],positions[2],positions[3])
  106.         for i = 4, #positions do
  107.             previous = narrow(previous,positions[i])
  108.         end
  109.         for i, v in pairs(previous) do
  110.             if (type(v) == "table") then
  111.                 for k, p in pairs(v) do
  112.                     print(p)
  113.                 end
  114.             else
  115.                 print(v)
  116.             end
  117.         end
  118.     end
  119. end
Advertisement
Add Comment
Please, Sign In to add comment