home *** CD-ROM | disk | FTP | other *** search
/ Czech Logic, Card & Gambling Games / Logické hry.iso / hry / Robocode / robocode-setup-1.0.7.jar / extract.jar / robots / sample / Tracker.java < prev    next >
Encoding:
Java Source  |  2005-02-18  |  4.1 KB  |  148 lines

  1. package sample;
  2. import robocode.*;
  3. /**
  4.  * Tracker - a sample robot by Mathew Nelson
  5.  * 
  6.  * Locks onto a robot, moves close, fires when close.
  7.  */
  8. public class Tracker extends Robot
  9. {
  10.     int count = 0;            // Keeps track of how long we've
  11.                             // been searching for our target
  12.     double gunTurnAmt;        // How much to turn our gun when searching
  13.     String trackName;        // Name of the robot we're currently tracking
  14.  
  15.     /**
  16.      * run: Tracker's main run function
  17.      */    
  18.     public void run() {
  19.         trackName = null;                    // Initialize to not tracking anyone
  20.         setAdjustGunForRobotTurn(true);        // Keep the gun still when we turn
  21.         gunTurnAmt = 10;                    // Initialize gunTurn to 10
  22.         while (true) {
  23.             // turn the Gun (looks for enemy)
  24.             turnGunRight(gunTurnAmt);
  25.             // Keep track of how long we've been looking
  26.             count++;
  27.             // If we've haven't seen our target for 2 turns, look left
  28.             if (count > 2)                    
  29.             {
  30.                 gunTurnAmt = -10;
  31.             }
  32.             // If we still haven't seen our target for 5 turns, look right
  33.             if (count > 5)
  34.                 gunTurnAmt = 10;
  35.             // If we *still* haven't seen our target after 10 turns, find another target
  36.             if (count > 11)
  37.                 trackName = null;
  38.         }
  39.     }
  40.     
  41.     /**
  42.      * onScannedRobot: Here's the good stuff
  43.      */    
  44.     public void onScannedRobot(ScannedRobotEvent e) {
  45.  
  46.         // If we have a target, and this isn't it, return immediately
  47.         //  so we can get more ScannedRobotEvents.
  48.         if (trackName != null && !e.getName().equals(trackName))
  49.             return;
  50.  
  51.         // If we don't have a target, well, now we do!
  52.         if (trackName == null) {
  53.             trackName = e.getName();
  54.             out.println("Tracking " + trackName);
  55.         }
  56.         // This is our target.  Reset count (see the run method)
  57.         count = 0;
  58.         // If our target is too far away, turn and move torward it.
  59.         if (e.getDistance() > 150)
  60.         {
  61.             gunTurnAmt = normalRelativeAngle(e.getBearing() + (getHeading() - getRadarHeading()));
  62.             
  63.             turnGunRight(gunTurnAmt);        // Try changing these to setTurnGunRight,
  64.             turnRight(e.getBearing());      // and see how much Tracker improves...
  65.                                             // (you'll have to make Tracker an AdvancedRobot)
  66.             ahead(e.getDistance() - 140);
  67.             return;
  68.         }
  69.  
  70.         // Our target is close.
  71.         gunTurnAmt = normalRelativeAngle(e.getBearing() + (getHeading() - getRadarHeading()));
  72.         turnGunRight(gunTurnAmt);
  73.         fire(3);
  74.         
  75.         // Our target is too close!  Back up.
  76.         if (e.getDistance() < 100)
  77.         {
  78.             if (e.getBearing() > -90 && e.getBearing() <= 90)
  79.                 back(40);
  80.             else
  81.                 ahead(40);
  82.         }
  83.         scan();
  84.  
  85.     }
  86.     
  87.     /**
  88.      * onHitRobot:  Set him as our new target
  89.      */    
  90.     public void onHitRobot(HitRobotEvent e) {
  91.         // Only print if he's not already our target.
  92.         if (trackName != null && !trackName.equals(e.getName()))
  93.         {
  94.             out.println("Tracking " + e.getName() + " due to collision");
  95.         }
  96.         // Set the target
  97.         trackName = e.getName();
  98.         // Back up a bit.
  99.         // Note:  We won't get scan events while we're doing this!
  100.         // An AdvancedRobot might use setBack(); execute();
  101.         gunTurnAmt = normalRelativeAngle(e.getBearing() + (getHeading() - getRadarHeading()));
  102.         turnGunRight(gunTurnAmt);
  103.         fire(3);
  104.         back(50);
  105.     }
  106.  
  107.     /**
  108.      * onWin:  Do a victory dance
  109.      */    
  110.     public void onWin(WinEvent e) {
  111.         for (int i = 0; i < 50; i++)
  112.         {
  113.             turnRight(30);
  114.             turnLeft(30);
  115.         }
  116.     }
  117.     
  118.     // normalAbsoluteAngle is not used in this robot,
  119.     // but is here for reference.
  120.     /**
  121.      * normalAbsoluteAngle:  returns angle such that 0 <= angle < 360
  122.      */    
  123.     public double normalAbsoluteAngle(double angle) {
  124.         if (angle >= 0 && angle < 360)
  125.             return angle;
  126.         double fixedAngle = angle;
  127.         while (fixedAngle < 0)
  128.             fixedAngle += 360;
  129.         while (fixedAngle >= 360)
  130.             fixedAngle -= 360;
  131.         return fixedAngle;
  132.     }
  133.     
  134.     /**
  135.      * normalRelativeAngle:  returns angle such that -180<angle<=180
  136.      */    
  137.     public double normalRelativeAngle(double angle) {
  138.         if (angle > -180 && angle <= 180)
  139.             return angle;
  140.         double fixedAngle = angle;
  141.         while (fixedAngle <= -180)
  142.             fixedAngle += 360;
  143.         while (fixedAngle > 180)
  144.             fixedAngle -= 360;
  145.         return fixedAngle;
  146.     }
  147.  
  148. }