Originally Posted by mekasha
My question is, is there any way to kind of sort the items based on a relative closeness to player position? I don't need to find "distance", I was just thinking something simple like ((player X coord - item X coord) or (item X coord - player X coord)) based on whichever is higher, then sorting based on the lower result.
|
Distance is pretty much exactly what you're trying to sort by.
If you have a table like
Lua Code:
{
{34132, 76.5, 63.5},
{34931, 21.7, 50.8},
{34133, 26.9, 31.9},
}
And you only want to sort by the X coordinate, you would get the player's coordinates and then sort the table..
Lua Code:
local coords = {
{34132, 76.5, 63.5},
{34931, 21.7, 50.8},
{34133, 26.9, 31.9},
}
local playerX, playerY = GetPlayerMapPosition('player')
table.sort(coords, function(a, b)
return (a[2] - playerX) < (b[2] - playerX)
end)
For actual distance you'd sort like this
Lua Code:
table.sort(coords, function(a, b)
return ((a[2] - playerX)^2 + (a[3] - playerY)^2) < ((b[2] - playerX)^2 + (b[3] - playerY)^2)
end)