Pinkishu

geoid

Aug 7th, 2012
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.22 KB | None | 0 0
  1. rednet.open("top")
  2. clientIDs={}
  3. clientIDs[13]=true
  4. clientIDs[14]=true
  5. clientIDs[15]=true
  6.  
  7. local locFixes = {}
  8. local locs = {}
  9. local myLoc = {296,66,733}
  10.  
  11. function getLoc(id)
  12.     local loc = locs[id]
  13.     if loc == nil then return nil end
  14.     return {locType=loc[1],x=loc[2].x,y=loc[2].y,z=loc[2].z}
  15. end
  16.  
  17. function saveLocs()
  18.     local file = fs.open("/locs","w")
  19.     file.write(textutils.serialize(locs))
  20.     file.close()
  21. end
  22.  
  23. function loadLocs()
  24.     if not fs.exists("/locs") then return end
  25.  
  26.     local file = fs.open("/locs","r")
  27.     local s = file.readAll()
  28.     locs = textutils.unserialize(s)
  29.     file.close()
  30. end
  31.  
  32. loadLocs()
  33.  
  34. local function trilaterate( A, B, C )
  35.  
  36.     local a2b = B.position - A.position
  37.     local a2c = C.position - A.position
  38.        
  39.     if math.abs( a2b:normalize():dot( a2c:normalize() ) ) > 0.999 then
  40.         return nil
  41.     end
  42.    
  43.     local d = a2b:length()
  44.     local ex = a2b:normalize( )
  45.     local i = ex:dot( a2c )
  46.     local ey = (a2c - (ex * i)):normalize()
  47.     local j = ey:dot( a2c )
  48.     local ez = ex:cross( ey )
  49.  
  50.     local r1 = A.distance
  51.     local r2 = B.distance
  52.     local r3 = C.distance
  53.        
  54.     local x = (r1*r1 - r2*r2 + d*d) / (2*d)
  55.     local y = (r1*r1 - r3*r3 - x*x + (x-i)*(x-i) + j*j) / (2*j)
  56.        
  57.     local result = A.position + (ex * x) + (ey * y)
  58.  
  59.     local zSquared = r1*r1 - x*x - y*y
  60.     if zSquared > 0 then
  61.         local z = math.sqrt( zSquared )
  62.         local result1 = result + (ez * z)
  63.         local result2 = result - (ez * z)
  64.        
  65.         local rounded1, rounded2 = result1:round(), result2:round()
  66.         if rounded1.x ~= rounded2.x or rounded1.y ~= rounded2.y or rounded1.z ~= rounded2.z then
  67.             return result1, result2
  68.         else
  69.             return rounded1
  70.         end
  71.     end
  72.     return result:round()
  73.    
  74. end
  75.  
  76. local function narrow( p1, p2, fix )
  77.     local dist1 = math.abs( (p1 - fix.position):length() - fix.distance )
  78.     local dist2 = math.abs( (p2 - fix.position):length() - fix.distance )
  79.    
  80.     if math.abs(dist1 - dist2) < 0.05 then
  81.         return p1, p2
  82.     elseif dist1 < dist2 then
  83.         return p1:round()
  84.     else
  85.         return p2:round()
  86.     end
  87. end
  88.  
  89. function processID(tID)
  90.     print("PROC"..tID)
  91.   local p1,p2 = trilaterate(locFixes[tID][1],locFixes[tID][2],locFixes[tID][3])
  92.   print("TRILD")
  93.   if p2 then
  94.     prinT("NAR")
  95.     p1,p2 = narrow( p1,p2,locFixes[tID][4] )
  96.   end
  97.   print("OD")
  98.   if p1 then
  99.     if p2 then
  100.       if locs[tID][1] ~= "exact" then
  101.         locs[tID] = {"ambigous",p1,p2}
  102.         print("Ambi: " .. p1.x .. "/".. p1.y .. "/"..p1.z)
  103.         saveLocs()
  104.       end
  105.     else
  106.       locs[tID] = {"exact",p1}
  107.       print("Exacted: " .. p1.x .. "/".. p1.y .. "/"..p1.z)
  108.       saveLocs()
  109.     end
  110.   end
  111.   return p1,p2
  112. end
  113.  
  114. function insertFix(tID,dist,hostLoc)
  115.     print("INSFIX"..tID.."/"..hostLoc.x)
  116.   if locFixes[tID] then
  117.     table.insert(locFixes[tID],{sender=tID,position=hostLoc,distance=dist})
  118.     if #locFixes[tID] >= 4 then
  119.       processID(tID)
  120.       locFixes[tID] = nil
  121.     end
  122.   else
  123.     locFixes[tID] = {{sender=tID,position=hostLoc,distance=dist}}
  124.   end
  125. end
  126. function detect()
  127. while true do
  128.   id,msg,dist = rednet.receive()
  129.   if clientIDs[id] then
  130.     local tab = textutils.unserialize(msg)
  131.     insertFix(tab[1],tab[2],vector.new(tab[3],tab[4],tab[5]))    
  132.   else
  133.     insertFix(id,dist,vector.new(myLoc[1],myLoc[2],myLoc[3]))
  134.     --print(id.."=>"..msg)
  135.   end
  136. end
  137. end
Advertisement
Add Comment
Please, Sign In to add comment