Advertisement
killer64

Untitled

Jan 2nd, 2014
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.23 KB | None | 0 0
  1. local function trilaterate( A, B, C )
  2. local a2b = B.position - A.position
  3. local a2c = C.position - A.position
  4. if math.abs( a2b:normalize():dot( a2c:normalize() ) ) > 0.999 then
  5. return nil
  6. end
  7. local d = a2b:length()
  8. local ex = a2b:normalize( )
  9. local i = ex:dot( a2c )
  10. local ey = (a2c - (ex * i)):normalize()
  11. local j = ey:dot( a2c )
  12. local ez = ex:cross( ey )
  13. local r1 = A.distance
  14. local r2 = B.distance
  15. local r3 = C.distance
  16. local x = (r1*r1 - r2*r2 + d*d) / (2*d)
  17. local y = (r1*r1 - r3*r3 - x*x + (x-i)*(x-i) + j*j) / (2*j)
  18. local result = A.position + (ex * x) + (ey * y)
  19. local zSquared = r1*r1 - x*x - y*y
  20. if zSquared > 0 then
  21. local z = math.sqrt( zSquared )
  22. local result1 = result + (ez * z)
  23. local result2 = result - (ez * z)
  24.  
  25. local rounded1, rounded2 = result1:round(), result2:round()
  26. if rounded1.x ~= rounded2.x or rounded1.y ~= rounded2.y or rounded1.z ~= rounded2.z then
  27. return rounded1, rounded2
  28. else
  29. return rounded1
  30. end
  31. end
  32. return result:round()
  33. end
  34.  
  35. local function narrow( p1, p2, fix )
  36. local dist1 = math.abs( (p1 - fix.position):length() - fix.distance )
  37. local dist2 = math.abs( (p2 - fix.position):length() - fix.distance )
  38. if math.abs(dist1 - dist2) < 0.05 then
  39. return p1, p2
  40. elseif dist1 < dist2 then
  41. return p1:round()
  42. else
  43. return p2:round()
  44. end
  45. end
  46.  
  47. local mlocations={
  48. ["modem_517"]={25,69,-139},
  49. ["modem_518"]={29,73,-135},
  50. ["modem_519"]={25,69,-131},
  51. ["modem_520"]={21,73,-135},
  52. }
  53. for k,v in pairs(mlocations) do
  54. peripheral.call(k,"open",824)
  55. end
  56. local rt={}
  57. local pos1={}
  58. local pos2={}
  59. while true do
  60. local p={os.pullEvent("modem_message")}
  61. if p[3]==824 then
  62. rt[p[4]]=rt[p[4]] or {}
  63. local tFixes=rt[p[4]]
  64. local tFix={position=vector.new(unpack(mlocations[p[2]])),distance=p[6]}
  65. table.insert(tFixes,tFix)
  66. if #tFixes >= 3 then
  67. if not pos1[p[4]] then
  68. pos1[p[4]],pos2[p[4]]=trilaterate(tFixes[1],tFixes[2],tFixes[#tFixes])
  69. else
  70. pos1[p[4]],pos2[p[4]]=narrow(pos1[p[4]],pos2[p[4]],tFixes[#tFixes])
  71. end
  72. end
  73. if pos1[p[4]] and not pos2[p[4]] then
  74. print("Position for "..p[4].." is "..pos1[p[4]].x..","..pos1[p[4]].y..","..pos1[p[4]].z)
  75. pos1[p[4]]=nil
  76. pos2[p[4]]=nil
  77. rt[p[4]]=nil
  78. end
  79. end
  80. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement