| 1039 | #define NEAR_DEFAULT_RADIUS 256 |
| 1040 | |
| 1041 | int 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 | ------------------------- |
no test coverage detected