home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / alt / sources / 2611 / radar.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-11-23  |  2.8 KB  |  142 lines

  1. #ifndef lint
  2. static    char    sccsid[] = "@(#)radar.c 1.1 92/05/28 SMI" ;
  3.     /* from radar.c 1.3 90/05/02 SMI */
  4. #endif
  5.  
  6. /*
  7.  * Copyright (c) 1986 by Sun Microsystems, Inc.
  8.  */
  9.  
  10. /*
  11.  * this is the radar function.
  12.  *
  13.  * If target is set and target is out of range, clear target.
  14.  *
  15.  * If target is clear go through the list of objects, select the
  16.  * closest object within view.
  17.  * If that object is closer than AUTO_RANGE, it is new target
  18.  *
  19.  */
  20.  
  21.  
  22.  
  23.  
  24. #include <stdio.h>
  25. #include <math.h>
  26. #include "parameters.h"
  27. #include "dstar.h"
  28. #include "object_types.h"
  29.  
  30.  
  31.  
  32.  
  33. extern    int    debug_level ;
  34.  
  35. #define    RADAR_RANGE2    (RADAR_RANGE*RADAR_RANGE)
  36. #define    RADAR_TAN2    (RADAR_TAN*RADAR_TAN)
  37.  
  38.  
  39. do_radar()
  40. {
  41.     int    i,j ;
  42.     Pt3d    v1, v2 ;
  43.     float    d ;
  44. register Object    *obj2 ;
  45.     Object    *closest = NULL ;
  46.     Mat3d    xform ;
  47.     float    mindist ;
  48.     float    dist2, tan2 ;
  49.  
  50.     if(Me->target != NULL)
  51.     {
  52.       /* no longer in the game? */
  53.       if( (Me->target->class != OBJ_PLAYER  &&
  54.            Me->target->class != OBJ_MISSILE)  ||
  55.           Me->target->status != OBJ_ACTIVE )
  56.         Me->target = NULL ;
  57.       else
  58.       {
  59.         /* out of range? */
  60.         v1.x = Me->target->Posn.x - Me->Posn.x ;
  61.         v1.y = Me->target->Posn.y - Me->Posn.y ;
  62.         v1.z = Me->target->Posn.z - Me->Posn.z ;
  63.         dist2 = v1.x*v1.x + v1.y*v1.y + v1.z*v1.z ;
  64.         if(dist2 > RADAR_RANGE2)
  65.           Me->target = NULL ;
  66.  
  67.         else
  68.         {
  69.           /* transform into ship coordinates */
  70.           v2.z = v1.x*Me->Forward.x +
  71.              v1.y*Me->Forward.y +
  72.              v1.z*Me->Forward.z ;
  73.  
  74.           /* behind us? */
  75.           if(v2.z < 0.0)
  76.         Me->target = NULL ;
  77.           else
  78.           {
  79.         /* not within narrow view? */
  80.         v2.x = v1.x*Me->Right.x +
  81.                v1.y*Me->Right.y +
  82.                v1.z*Me->Right.z ;
  83.         v2.y = v1.x*Me->Up.x +
  84.                v1.y*Me->Up.y +
  85.                v1.z*Me->Up.z ;
  86.  
  87.         tan2 = (v2.x*v2.x + v2.y*v2.y)/(v2.z*v2.z) ;
  88.         if(tan2 > RADAR_TAN2)
  89.           Me->target = NULL ;
  90.           }
  91.         }
  92.       }
  93.     }
  94.  
  95.  
  96.     if(Me->target == NULL)
  97.     {
  98.       /* search for any object within range */
  99.  
  100.       obj2 = &objects[0] ;
  101.       mindist =  RADAR_RANGE2 ;
  102.       for(i=0; i<MAX_OBJECTS; ++i)
  103.       {
  104.         if(obj2 != Me &&
  105.            (obj2->class == OBJ_PLAYER  ||
  106.             obj2->class == OBJ_MISSILE)  &&
  107.            obj2->status == OBJ_ACTIVE)
  108.         {
  109.           v1.x = obj2->Posn.x - Me->Posn.x ;
  110.           v1.y = obj2->Posn.y - Me->Posn.y ;
  111.           v1.z = obj2->Posn.z - Me->Posn.z ;
  112.           dist2 = v1.x*v1.x + v1.y*v1.y + v1.z*v1.z ;
  113.           if(dist2 < mindist)
  114.           {
  115.         v2.z = v1.x*Me->Forward.x +
  116.                v1.y*Me->Forward.y +
  117.                v1.z*Me->Forward.z ;
  118.         if(v2.z > 0.0)
  119.         {
  120.           v2.x = v1.x*Me->Right.x +
  121.              v1.y*Me->Right.y +
  122.              v1.z*Me->Right.z ;
  123.           v2.y = v1.x*Me->Up.x +
  124.              v1.y*Me->Up.y +
  125.              v1.z*Me->Up.z ;
  126.           tan2 = (v2.x*v2.x + v2.y*v2.y)/(v2.z*v2.z) ;
  127.           if(tan2 < RADAR_TAN2)
  128.           {
  129.             closest = obj2 ;
  130.             mindist = dist2 ;
  131.           }
  132.         }
  133.           }
  134.         }
  135.       ++obj2 ;
  136.       }
  137.  
  138.       if(closest != NULL)
  139.         Me->target = closest ;
  140.     }
  141. }
  142.