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.Robot; |
---|
12 | import robocode.ScannedRobotEvent; |
---|
13 | import robocode.WinEvent; |
---|
14 | import static robocode.util.Utils.normalRelativeAngleDegrees; |
---|
15 | |
---|
16 | import java.awt.*; |
---|
17 | |
---|
18 | |
---|
19 | /** |
---|
20 | * TrackFire - a sample robot by Mathew Nelson. |
---|
21 | * <p/> |
---|
22 | * Sits still. Tracks and fires at the nearest robot it sees. |
---|
23 | * |
---|
24 | * @author Mathew A. Nelson (original) |
---|
25 | * @author Flemming N. Larsen (contributor) |
---|
26 | */ |
---|
27 | public class TrackFire extends Robot { |
---|
28 | |
---|
29 | /** |
---|
30 | * TrackFire's run method |
---|
31 | */ |
---|
32 | public void run() { |
---|
33 | // Set colors |
---|
34 | setBodyColor(Color.pink); |
---|
35 | setGunColor(Color.pink); |
---|
36 | setRadarColor(Color.pink); |
---|
37 | setScanColor(Color.pink); |
---|
38 | setBulletColor(Color.pink); |
---|
39 | |
---|
40 | // Loop forever |
---|
41 | while (true) { |
---|
42 | turnGunRight(10); // Scans automatically |
---|
43 | } |
---|
44 | } |
---|
45 | |
---|
46 | /** |
---|
47 | * onScannedRobot: We have a target. Go get it. |
---|
48 | */ |
---|
49 | public void onScannedRobot(ScannedRobotEvent e) { |
---|
50 | // Calculate exact location of the robot |
---|
51 | double absoluteBearing = getHeading() + e.getBearing(); |
---|
52 | double bearingFromGun = normalRelativeAngleDegrees(absoluteBearing - getGunHeading()); |
---|
53 | |
---|
54 | // If it's close enough, fire! |
---|
55 | if (Math.abs(bearingFromGun) <= 3) { |
---|
56 | turnGunRight(bearingFromGun); |
---|
57 | // We check gun heat here, because calling fire() |
---|
58 | // uses a turn, which could cause us to lose track |
---|
59 | // of the other robot. |
---|
60 | if (getGunHeat() == 0) { |
---|
61 | fire(Math.min(3 - Math.abs(bearingFromGun), getEnergy() - .1)); |
---|
62 | } |
---|
63 | } // otherwise just set the gun to turn. |
---|
64 | // Note: This will have no effect until we call scan() |
---|
65 | else { |
---|
66 | turnGunRight(bearingFromGun); |
---|
67 | } |
---|
68 | // Generates another scan event if we see a robot. |
---|
69 | // We only need to call this if the gun (and therefore radar) |
---|
70 | // are not turning. Otherwise, scan is called automatically. |
---|
71 | if (bearingFromGun == 0) { |
---|
72 | scan(); |
---|
73 | } |
---|
74 | } |
---|
75 | |
---|
76 | public void onWin(WinEvent e) { |
---|
77 | // Victory dance |
---|
78 | turnRight(36000); |
---|
79 | } |
---|
80 | } |
---|