PDA

View Full Version : [CSGO] Player distances in real time


intellild
12-12-2021, 01:41
https://github.com/intellild/sm_extension/tree/master/player_distance
This extension computes player distance in real time and providers a simple rule extension for plugins depends on player distances to each other, for example,
hide teammates (https://forums.alliedmods.net/showthread.php?p=2765747#post2765747)

This extension make use of SIMD features of the CPU to get best performance for computing player distances.
(Thanks to compilers, we don't need to write SIMD code by hand in most cases)

On my machine (3700x), the compute for 64 player is around 0.2ms(200000ns). So we can compute player distances every game frame.

kiceqs
12-13-2021, 11:25
float dist[MAXPLAYERS + 1][MAXPLAYERS + 1];

void Distance1()
{
Profiler prof = new Profiler();
prof.Start();
for (int i = 1; i <= MaxClients; ++i) {
if (!IsClientInGame(i)) {
continue;
}

float pos[3];
GetClientEyePosition(i, pos);

for (int j = 1; j <= MaxClients; ++j) {
if (i == j || !IsClientInGame(j)) {
continue;
}

float target[3];
GetClientEyePosition(j, target);
dist[i][j] = GetVectorDistance(pos, target);
}
}
prof.Stop();
PrintToServer("dist1 = %f", prof.Time);
}

void Distance2(bool squared = false)
{
Profiler prof = new Profiler();
prof.Start();

bool validClient[MAXPLAYERS + 1];
float pos[MAXPLAYERS + 1][3];
for (int i = 1; i <= MaxClients; ++i) {
if (!IsClientInGame(i)) {
continue;
}

validClient[i] = true;
GetClientEyePosition(i, pos[i]);
}

for (int i = 1; i <= MaxClients; ++i) {
if (!validClient[i]) {
continue;
}

for (int j = 1; j <= MaxClients; ++j) {
if (i == j || !validClient[j]) {
continue;
}

dist[i][j] = GetVectorDistance(pos[i], pos[j], squared);
}
}
prof.Stop();
PrintToServer("dist2 = %f (%s)", prof.Time, squared ? "squared" : " ");
}


On my laptop: i7 10750H, win10, with srcds and csgo running, (and a lot of other junk), here is what i got, with 48 players in game

dist1 = 0.000151
dist2 = 0.000059 ( )
dist2 = 0.000054 (squared)

The profiler uses QueryPerformanceCounter, thus the result should be enough in this case.

I mean sourcemod JIT is not that bad, and the major overhead is expected to be brand prediction missing and cache missing, not the calculation itself.

Total float operation is 48*48*(3+3) = 13824 (let multiplication and addition be the same), which is not that much at all for a morden cpu.