Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local CHANNEL_GPS = 65534
- local function trilaterate( A, B, C )
- local a2b = B.position - A.position
- local a2c = C.position - A.position
- if math.abs( a2b:normalize():dot( a2c:normalize() ) ) > 0.999 then
- return nil
- end
- local d = a2b:length()
- local ex = a2b:normalize( )
- local i = ex:dot( a2c )
- local ey = (a2c - (ex * i)):normalize()
- local j = ey:dot( a2c )
- local ez = ex:cross( ey )
- local r1 = A.distance
- local r2 = B.distance
- local r3 = C.distance
- local x = (r1*r1 - r2*r2 + d*d) / (2*d)
- local y = (r1*r1 - r3*r3 - x*x + (x-i)*(x-i) + j*j) / (2*j)
- local result = A.position + (ex * x) + (ey * y)
- local zSquared = r1*r1 - x*x - y*y
- if zSquared > 0 then
- local z = math.sqrt( zSquared )
- local result1 = result + (ez * z)
- local result2 = result - (ez * z)
- local rounded1, rounded2 = result1:round(), result2:round()
- if rounded1.x ~= rounded2.x or rounded1.y ~= rounded2.y or rounded1.z ~= rounded2.z then
- return rounded1, rounded2
- else
- return rounded1
- end
- end
- return result:round()
- end
- local function narrow( p1, p2, fix )
- local dist1 = math.abs( (p1 - fix.position):length() - fix.distance )
- local dist2 = math.abs( (p2 - fix.position):length() - fix.distance )
- if math.abs(dist1 - dist2) < 0.05 then
- return p1, p2
- elseif dist1 < dist2 then
- return p1:round()
- else
- return p2:round()
- end
- end
- x, y, z = gps.locate()
- if x == nil then
- print("Not able to get GPS location.")
- write("Computer X: ")
- x = read()
- write("Computer Y: ")
- y = read()
- write("Computer Z: ")
- z = read()
- end
- if ({...})[1] ~= nil then
- if ({...])[2] == "server" then
- rednet.open(({...})[1])
- while true do
- local id2, msg = rednet.receive()
- if type(msg) == "table" and msg[1] == "locatefirewolfserver" then
- rednet.broadcast("firewolf.broadcast.dns.list")
- while true do
- local id, msg, dist = rednet.receive(1)
- if msg == nil or os.clock() > time + 3 then
- break
- elseif msg == "firewolf-site:"..msg[2] then
- rednet.send(id2,{{x,y,z},dist})
- break
- end
- end
- end
- end
- rednet.send(id2,{{x,y,z},dist})
- else
- print("Enter site address.")
- local site = read()
- rednet.open(({...})[1])
- local time = os.clock()
- local positions = {}
- rednet.broadcast({"locatefirewolfserver",site})
- while true do
- local id, msg, dist = rednet.receive(1)
- if msg == nil or os.clock() > time + 4 then
- break
- elseif type(msg) == "table" then
- if msg[1] == "serverlocator" then
- table.insert(positions,{["position"] = msg[2],["distance"] = msg[3]})
- end
- end
- end
- rednet.close(({...})[1])
- local previous = trilaterate(positions[1],positions[2],positions[3])
- for i = 4, #positions do
- previous = narrow(previous,positions[i])
- end
- for i, v in pairs(previous) do
- if (type(v) == "table") then
- for k, p in pairs(v) do
- print(p)
- end
- else
- print(v)
- end
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment