[HELP] How to Calculate the 10 next Players and sort them by their distance
#1

Hi everyone i need help again,

Im stuck on this since 2 weeks and cant go on writting my gamemode until i got that.

I want to create a textdrawList with the nearest 10 Players from a specific player. The first closest should be listed first, the second closest should be listed as second etc... So How can 1. calculate the 10 closest Players and 2. sort the playerids from closest to farthest? How can I make the script to ignore offline Playerids? Pls help me! It would be good if you give me a code example. thank you!

CJ89
Reply
#2

Im sure someone can think of faster way in one loop, but this is one option:
pawn Код:
new pClosest[10],
  Float:num[3], Float:num2[3],
  Float:pDistance[MAX_PLAYERS] = { Float:0x7F800000, ... };
GetPlayerPos(playerid, num[0], num[1], num[2]);
for(new i; i < MAX_PLAYERS; i++)
{
  GetPlayerPos(i, num2[0], num2[1], num2[2]);
  pDistance[i] = floatpower(((num[0]-num2[0])*(num[0]-num2[0])+(num[1]-num2[1])*(num[1]-num2[1])+(num[2]-num2[2])*(num[2]-num2[2])), 0.5);
}
SortIDs(uDistance, pClosest);
pawn Код:
//by ♣ Joker ♠
#define SortIDs(%1,%2); \
  for(new c, l, v; c < sizeof %2; c++) { \
    for(l = 1, v = 0; l < sizeof %1; l++) \
      if(%1[l] < %1[v]) v = l; \
    %2[c] = v, %1[v] = (Float:0x7F800000); \
  }
Havent tested, but it should work. And if you want to skip offline players, foreach 0.3 compatible is the solution.
Reply
#3

thx.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)