home *** CD-ROM | disk | FTP | other *** search
- #ifndef lint
- static char sccsid[] = "@(#)radar.c 1.1 92/05/28 SMI" ;
- /* from radar.c 1.3 90/05/02 SMI */
- #endif
-
- /*
- * Copyright (c) 1986 by Sun Microsystems, Inc.
- */
-
- /*
- * this is the radar function.
- *
- * If target is set and target is out of range, clear target.
- *
- * If target is clear go through the list of objects, select the
- * closest object within view.
- * If that object is closer than AUTO_RANGE, it is new target
- *
- */
-
-
-
-
- #include <stdio.h>
- #include <math.h>
- #include "parameters.h"
- #include "dstar.h"
- #include "object_types.h"
-
-
-
-
- extern int debug_level ;
-
- #define RADAR_RANGE2 (RADAR_RANGE*RADAR_RANGE)
- #define RADAR_TAN2 (RADAR_TAN*RADAR_TAN)
-
-
- do_radar()
- {
- int i,j ;
- Pt3d v1, v2 ;
- float d ;
- register Object *obj2 ;
- Object *closest = NULL ;
- Mat3d xform ;
- float mindist ;
- float dist2, tan2 ;
-
- if(Me->target != NULL)
- {
- /* no longer in the game? */
- if( (Me->target->class != OBJ_PLAYER &&
- Me->target->class != OBJ_MISSILE) ||
- Me->target->status != OBJ_ACTIVE )
- Me->target = NULL ;
- else
- {
- /* out of range? */
- v1.x = Me->target->Posn.x - Me->Posn.x ;
- v1.y = Me->target->Posn.y - Me->Posn.y ;
- v1.z = Me->target->Posn.z - Me->Posn.z ;
- dist2 = v1.x*v1.x + v1.y*v1.y + v1.z*v1.z ;
- if(dist2 > RADAR_RANGE2)
- Me->target = NULL ;
-
- else
- {
- /* transform into ship coordinates */
- v2.z = v1.x*Me->Forward.x +
- v1.y*Me->Forward.y +
- v1.z*Me->Forward.z ;
-
- /* behind us? */
- if(v2.z < 0.0)
- Me->target = NULL ;
- else
- {
- /* not within narrow view? */
- v2.x = v1.x*Me->Right.x +
- v1.y*Me->Right.y +
- v1.z*Me->Right.z ;
- v2.y = v1.x*Me->Up.x +
- v1.y*Me->Up.y +
- v1.z*Me->Up.z ;
-
- tan2 = (v2.x*v2.x + v2.y*v2.y)/(v2.z*v2.z) ;
- if(tan2 > RADAR_TAN2)
- Me->target = NULL ;
- }
- }
- }
- }
-
-
- if(Me->target == NULL)
- {
- /* search for any object within range */
-
- obj2 = &objects[0] ;
- mindist = RADAR_RANGE2 ;
- for(i=0; i<MAX_OBJECTS; ++i)
- {
- if(obj2 != Me &&
- (obj2->class == OBJ_PLAYER ||
- obj2->class == OBJ_MISSILE) &&
- obj2->status == OBJ_ACTIVE)
- {
- v1.x = obj2->Posn.x - Me->Posn.x ;
- v1.y = obj2->Posn.y - Me->Posn.y ;
- v1.z = obj2->Posn.z - Me->Posn.z ;
- dist2 = v1.x*v1.x + v1.y*v1.y + v1.z*v1.z ;
- if(dist2 < mindist)
- {
- v2.z = v1.x*Me->Forward.x +
- v1.y*Me->Forward.y +
- v1.z*Me->Forward.z ;
- if(v2.z > 0.0)
- {
- v2.x = v1.x*Me->Right.x +
- v1.y*Me->Right.y +
- v1.z*Me->Right.z ;
- v2.y = v1.x*Me->Up.x +
- v1.y*Me->Up.y +
- v1.z*Me->Up.z ;
- tan2 = (v2.x*v2.x + v2.y*v2.y)/(v2.z*v2.z) ;
- if(tan2 < RADAR_TAN2)
- {
- closest = obj2 ;
- mindist = dist2 ;
- }
- }
- }
- }
- ++obj2 ;
- }
-
- if(closest != NULL)
- Me->target = closest ;
- }
- }
-