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

  1. #ifndef lint
  2. static    char    sccsid[] = "@(#)laser.c 1.1 92/05/28 SMI" ;
  3.     /* from laser.c 1.3 90/05/02 SMI */
  4. #endif
  5.  
  6. /*
  7.  * Copyright (c) 1986 by Sun Microsystems, Inc.
  8.  */
  9.  
  10. /*
  11.  * test all objects to see if they've been touched by the laser beam
  12.  */
  13.  
  14. #include <stdio.h>
  15. #include <math.h>
  16. #include "dstar.h"
  17. #include "object_types.h"
  18.  
  19. #define    LASER_THRESH2    (LASER_THRESH*LASER_THRESH)
  20.  
  21.  
  22. /*******
  23.  *
  24.  * test_object(object)
  25.  *
  26.  * check to see if the object lies along the laser beam, and how
  27.  * close.
  28.  *
  29.  * given P0 is my position, P1 is the object's position and V is the
  30.  * vector along the laser beam, then the point of closest approach, P, for
  31.  * the laser beam to P1 is
  32.  *
  33.  *    d = V * (P1-P0)            (* = dot-product)
  34.  *    P = dV
  35.  *
  36.  * Note that since V is normalized, d is the distance from P0 to P.
  37.  *
  38.  ******/
  39.  
  40.  
  41.  
  42. test_object(object)
  43.     Object    *object ;
  44. {
  45.     Pt3d    P, dP ;
  46.     float        d, dist ;
  47.  
  48.     dP.x = object->Posn.x - Me->Posn.x ;
  49.     dP.y = object->Posn.y - Me->Posn.y ;
  50.     dP.z = object->Posn.z - Me->Posn.z ;
  51.  
  52.     d = dP.x * Me->Pointing.x +
  53.         dP.y * Me->Pointing.y +
  54.         dP.z * Me->Pointing.z ;
  55.  
  56.     if (d < 0.0  ||  d > LASER_RANGE)
  57.       return ;
  58.  
  59.     P.x = Me->Posn.x + d*Me->Pointing.x ;
  60.     P.y = Me->Posn.y + d*Me->Pointing.y ;
  61.     P.z = Me->Posn.z + d*Me->Pointing.z ;
  62.  
  63.     dist = (P.x - object->Posn.x) * (P.x - object->Posn.x) +
  64.            (P.y - object->Posn.y) * (P.y - object->Posn.y) +
  65.            (P.z - object->Posn.z) * (P.z - object->Posn.z) ;
  66.  
  67.     if(dist > LASER_THRESH2)
  68.       return ;
  69.  
  70.  
  71.     /* GOT IT! */
  72.  
  73.  
  74.     net_blow_away(Me, object) ;
  75. }
  76.  
  77.  
  78.  
  79.  
  80. test_objects()
  81. {
  82.     int    i ;
  83.     Object    *object ;
  84.  
  85.     /* check enemy ships */
  86.  
  87.     object = &objects[0] ;
  88.     for(i=0; i<MAX_OBJECTS; ++i)
  89.     {
  90.       if(object != Me  &&
  91.          (object->class == OBJ_PLAYER  ||
  92.           object->class == OBJ_MISSILE)  &&
  93.          object->status == OBJ_ACTIVE )
  94.       {
  95.         test_object(object) ;
  96.       }
  97.       ++object ;
  98.     }
  99. }
  100.