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