Advertisement
Rochet2

Untitled

Aug 3rd, 2015
452
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.58 KB | None | 0 0
  1. -- Doesnt work properly from tests
  2.  
  3. -- Calculates optimal x,y,z coordinates for a diameter to hold most players
  4. -- returns nothing if no coords or random pic is as good
  5.  
  6. local function distsort(a, b)
  7.     return a[2] < b[2]
  8. end
  9.  
  10. local function GetPosition(players, diameter)
  11.     if #players <= 1 then
  12.         return
  13.     end
  14.    
  15.     -- Save everyone's distance to everyone to distances (its a 2d matrix)
  16.     -- Save the sum distance to everyone and the index to maxdists
  17.     local distances = {}
  18.     local maxdists = {}
  19.     for k1,v1 in ipairs(players) do
  20.         distances[k1] = {}
  21.         local distsum = 0
  22.  
  23.         for k2,v2 in ipairs(players) do
  24.             local curdist = v1:GetExactDistance2d(v2)
  25.             distances[k1][k2] = curdist
  26.             distsum = distsum + curdist
  27.         end
  28.  
  29.         maxdists[#maxdists+1] = {k1, distsum/2} -- divide by 2 since
  30.     end
  31.  
  32.     -- Need to throw away one by one until one remains so max count-1 times
  33.     for i = 1, #maxdists-1 do
  34.         -- Always sort first so maximum distance element is at top
  35.         table.sort(maxdists, distsort)
  36.        
  37.         -- if maximum distance is within the diameter allowed then stop
  38.         if maxdists[#maxdists][2] <= diameter then
  39.             break
  40.         end
  41.  
  42.         local maxelem = maxdists[#maxdists]
  43.         local elemdists = distances[maxelem[1]]
  44.         maxdists[#maxdists] = nil-- throw max element away
  45.        
  46.         -- decrease everyone's max distance by their distance from the element thrown away
  47.         for k,v in ipairs(maxdists) do
  48.             v[2] = v[2] - elemdists[v[1]]
  49.         end
  50.     end
  51.    
  52.     if #maxdists <= 1 then
  53.         return
  54.     end
  55.    
  56.     -- calculate average 2d coords
  57.     local x,y,z = 0,0,0
  58.     for k,v in ipairs(maxdists) do
  59.         local ox, oy, oz = players[v[1]]:GetLocation()
  60.         x = x+ox
  61.         y = y+oy
  62.         z = z+oz
  63.         print(v[1])
  64.     end
  65.     return x/#maxdists, y/#maxdists, z/#maxdists
  66. end
  67.  
  68. local Player = {}
  69. Player.__index = Player
  70.  
  71. function Player:GetLocation()
  72.     return self.x, self.y, self.z
  73. end
  74.  
  75. function Player:GetExactDistance2d(player)
  76.     local x = self.x - player.x
  77.     local y = self.y - player.y
  78.     return math.sqrt(x*x + y*y)
  79. end
  80.  
  81. function Player:new(x, y, z)
  82.     return setmetatable({x=x, y=y, z=z}, Player)
  83. end
  84.  
  85. local players = {
  86.     Player:new(6.4, 8, 6),
  87.     Player:new(4, 2, 7),
  88.     Player:new(3, 2, 3),
  89.     Player:new(18, 86, 100),
  90. }
  91.  
  92. local x,y,z = GetPosition(players, 1)
  93. print(x,y,z)
  94.  
  95. for k,v in ipairs(players) do
  96.     print(k, v:GetExactDistance2d({x=x, y=y, z=z}))
  97. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement