home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_disks / 300-399 / ff331.lzh / CRobots / sniper.r < prev    next >
Text File  |  1990-03-21  |  5KB  |  182 lines

  1. CRobots, Inc.
  2. T. Poindexter
  3. /* sniper */
  4. /* strategy: since a scan of the entire battlefield can be done in 90 */
  5. /* degrees from a corner, sniper can scan the field quickly. */
  6.  
  7. /* external variables, that can be used by any function */
  8. int corner;           /* current corner 0, 1, 2, or 2 */
  9. int c1x, c1y;         /* corner 1 x and y */
  10. int c2x, c2y;         /*   "    2 "  "  " */
  11. int c3x, c3y;         /*   "    3 "  "  " */
  12. int c4x, c4y;         /*   "    4 "  "  " */
  13. int s1, s2, s3, s4;   /* starting scan position for corner 1 - 4 */
  14. int sc;               /* current scan start */
  15. int d;                /* last damage check */
  16.  
  17.  
  18.  
  19. /* main */
  20. main()
  21. {
  22.   int closest;        /* check for targets in range */
  23.   int range;          /* range to target */
  24.   int dir;            /* scan direction */
  25.  
  26.   /* initialize the corner info */
  27.   /* x and y location of a corner, and starting scan degree */
  28.   c1x = 10;  c1y = 10;  s1 = 0;
  29.   c2x = 10;  c2y = 990; s2 = 270;
  30.   c3x = 990; c3y = 990; s3 = 180;
  31.   c4x = 990; c4y = 10;  s4 = 90;
  32.   closest = 9999;
  33.   new_corner();       /* start at a random corner */ 
  34.   d = damage();       /* get current damage */
  35.   dir = sc;           /* starting scan direction */ 
  36.  
  37.   while (1) {         /* loop is executed forever */
  38.  
  39.     while (dir < sc + 90) {  /* scan through 90 degree range */
  40.       range = scan(dir,1);   /* look at a direction */
  41.       if (range <= 700 && range > 0) { 
  42.         while (range > 0) {    /* keep firing while in range */
  43.           closest = range;     /* set closest flag */
  44.           cannon(dir,range);   /* fire! */
  45.           range = scan(dir,1); /* check target again */
  46.           if (d + 15 > damage())  /* sustained several hits, */ 
  47.             range = 0;            /* goto new corner */
  48.         }
  49.         dir -= 10;             /* back up scan, in case */
  50.       }
  51.  
  52.       dir += 2;                /* increment scan */
  53.       if (d != damage()) {     /* check for damage incurred */
  54.         new_corner();          /* we're hit, move now */
  55.         d = damage();
  56.         dir = sc;
  57.       }
  58.     }
  59.  
  60.     if (closest == 9999) {       /* check for any targets in range */
  61.       new_corner();             /* nothing, move to new corner */
  62.       d = damage();
  63.       dir = sc;
  64.     } else                      /* targets in range, resume */
  65.       dir = sc;
  66.     closest = 9999;
  67.   } 
  68.  
  69. }  /* end of main */
  70.  
  71.  
  72.  
  73. /* new corner function to move to a different corner */
  74. new_corner() {
  75.   int x, y;
  76.   int angle;
  77.   int new;
  78.  
  79.   new = rand(4);           /* pick a random corner */
  80.   if (new == corner)       /* but make it different than the */
  81.     corner = (new + 1) % 4;/* current corner */
  82.   else
  83.     corner = new;
  84.   if (corner == 0) {       /* set new x,y and scan start */
  85.     x = c1x; 
  86.     y = c1y; 
  87.     sc = s1;
  88.   } 
  89.   if (corner == 1) {
  90.     x = c2x; 
  91.     y = c2y; 
  92.     sc = s2;
  93.   }
  94.   if (corner == 2) {
  95.     x = c3x; 
  96.     y = c3y; 
  97.     sc = s3;
  98.   }
  99.   if (corner == 3) { 
  100.     x = c4x; 
  101.     y = c4y; 
  102.     sc = s4;
  103.   }
  104.  
  105.   /* find the heading we need to get to the desired corner */
  106.   angle = plot_course(x,y);
  107.  
  108.   /* start drive train, full speed */
  109.   drive(angle,100);
  110.  
  111.   /* keep traveling until we are within 100 meters */
  112.   /* speed is checked in case we run into wall, other robot */
  113.   /* not terribly great, since were are doing nothing while moving */
  114.  
  115.   while (distance(loc_x(),loc_y(),x,y) > 100 && speed() > 0)
  116.     ;
  117.  
  118.   /* cut speed, and creep the rest of the way */
  119.  
  120.   drive(angle,20);
  121.   while (distance(loc_x(),loc_y(),x,y) > 10 && speed() > 0)
  122.     ;
  123.  
  124.   /* stop drive, should coast in the rest of the way */
  125.   drive(angle,0); 
  126. }  /* end of new_corner */
  127.  
  128. /* classical pythagorean distance formula */
  129. distance(x1,y1,x2,y2)
  130. int x1;
  131. int y1;
  132. int x2;
  133. int y2;
  134. {
  135.   int x, y;
  136.  
  137.   x = x1 - x2;
  138.   y = y1 - y2;
  139.   d = sqrt((x*x) + (y*y));
  140.   return(d);
  141. }
  142.  
  143. /* plot course function, return degree heading to */
  144. /* reach destination x, y; uses atan() trig function */
  145. plot_course(xx,yy)
  146. int xx, yy;
  147. {
  148.   int d;
  149.   int x,y;
  150.   int scale;
  151.   int curx, cury;
  152.  
  153.   scale = 100000;  /* scale for trig functions */
  154.   curx = loc_x();  /* get current location */
  155.   cury = loc_y();
  156.   x = curx - xx;
  157.   y = cury - yy;
  158.  
  159.   /* atan only returns -90 to +90, so figure out how to use */
  160.   /* the atan() value */
  161.  
  162.   if (x == 0) {      /* x is zero, we either move due north or south */
  163.     if (yy > cury)
  164.       d = 90;        /* north */
  165.     else
  166.       d = 270;       /* south */
  167.   } else {
  168.     if (yy < cury) {
  169.       if (xx > curx)
  170.         d = 360 + atan((scale * y) / x);  /* south-east, quadrant 4 */
  171.       else
  172.         d = 180 + atan((scale * y) / x);  /* south-west, quadrant 3 */ 
  173.     } else {
  174.       if (xx > curx)
  175.         d = atan((scale * y) / x);        /* north-east, quadrant 1 */
  176.       else
  177.         d = 180 + atan((scale * y) / x);  /* north-west, quadrant 2 */
  178.     }
  179.   }
  180.   return (d);
  181. }
  182.