diff options
-rw-r--r-- | src/game/g_utils.c | 12 |
1 files changed, 9 insertions, 3 deletions
diff --git a/src/game/g_utils.c b/src/game/g_utils.c index 2e08afdf..d4a9ef6a 100644 --- a/src/game/g_utils.c +++ b/src/game/g_utils.c @@ -771,14 +771,20 @@ gentity_t *G_ClosestEnt( vec3_t origin, gentity_t **entities, int numEntities ) { int i; float nd, d; - gentity_t *closestEnt = NULL; + gentity_t *closestEnt; - for( i = 0; i < numEntities; i++ ) + if( numEntities <= 0 ) + return NULL; + + closestEnt = entities[ 0 ]; + d = DistanceSquared( origin, closestEnt->s.origin ); + + for( i = 1; i < numEntities; i++ ) { gentity_t *ent = entities[ i ]; nd = DistanceSquared( origin, ent->s.origin ); - if( i == 0 || nd < d ) + if( nd < d ) { d = nd; closestEnt = ent; |