MCPcopy Create free account
hub / github.com/JACoders/OpenJK / NPC_FindNearestEnemy

Function NPC_FindNearestEnemy

codeJK2/game/NPC_utils.cpp:1041–1087  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1039#define NEAR_DEFAULT_RADIUS 256
1040
1041int NPC_FindNearestEnemy( gentity_t *ent )
1042{
1043 gentity_t *radiusEnts[ MAX_RADIUS_ENTS ];
1044 vec3_t mins, maxs;
1045 int nearestEntID = -1;
1046 float nearestDist = (float)WORLD_SIZE*(float)WORLD_SIZE;
1047 float distance;
1048 int numEnts, numChecks = 0;
1049 int i;
1050
1051 //Setup the bbox to search in
1052 for ( i = 0; i < 3; i++ )
1053 {
1054 mins[i] = ent->currentOrigin[i] - NPCInfo->stats.visrange;
1055 maxs[i] = ent->currentOrigin[i] + NPCInfo->stats.visrange;
1056 }
1057
1058 //Get a number of entities in a given space
1059 numEnts = gi.EntitiesInBox( mins, maxs, radiusEnts, MAX_RADIUS_ENTS );
1060
1061 for ( i = 0; i < numEnts; i++ )
1062 {
1063 //Don't consider self
1064 if ( radiusEnts[i] == ent )
1065 continue;
1066
1067 //Must be valid
1068 if ( NPC_ValidEnemy( radiusEnts[i] ) == qfalse )
1069 continue;
1070
1071 numChecks++;
1072 //Must be visible
1073 if ( NPC_TargetVisible( radiusEnts[i] ) == qfalse )
1074 continue;
1075
1076 distance = DistanceSquared( ent->currentOrigin, radiusEnts[i]->currentOrigin );
1077
1078 //Found one closer to us
1079 if ( distance < nearestDist )
1080 {
1081 nearestEntID = radiusEnts[i]->s.number;
1082 nearestDist = distance;
1083 }
1084 }
1085
1086 return nearestEntID;
1087}
1088
1089/*
1090-------------------------

Callers 1

NPC_PickEnemyExtFunction · 0.70

Calls 3

DistanceSquaredFunction · 0.85
NPC_ValidEnemyFunction · 0.70
NPC_TargetVisibleFunction · 0.70

Tested by

no test coverage detected