Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Robocode.TrunkInt/HeuristicLab.Problems.Robocode/3.3/robocode/robots/sample/TrackFire.java @ 13019

Last change on this file since 13019 was 13019, checked in by gkronber, 9 years ago

#2069: added necessary robocode files to project to make sure that the robocode problem is self-contained (only a java installation is necessary)

File size: 2.1 KB
Line 
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 */
8package sample;
9
10
11import robocode.Robot;
12import robocode.ScannedRobotEvent;
13import robocode.WinEvent;
14import static robocode.util.Utils.normalRelativeAngleDegrees;
15
16import 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 */
27public 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}       
Note: See TracBrowser for help on using the repository browser.