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