From d129834d37592d8ae95a7e4f3a2e3bfc47c11851 Mon Sep 17 00:00:00 2001 From: Ben Millwood Date: Fri, 15 Apr 2011 18:53:55 +0000 Subject: * Refactor G_ClosestEnt to avoid (spurious) unused variable warning --- src/game/g_utils.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) (limited to 'src/game') 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; -- cgit