Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Robocode.TrunkInt/HeuristicLab.Problems.Robocode/3.3/robocode/robots/sample/Fire.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.0 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.HitByBulletEvent;
12import robocode.HitRobotEvent;
13import robocode.Robot;
14import robocode.ScannedRobotEvent;
15import static robocode.util.Utils.normalRelativeAngleDegrees;
16
17import java.awt.*;
18
19
20/**
21 * Fire - a sample robot by Mathew Nelson, and maintained.
22 * <p/>
23 * Sits still. Spins gun around. Moves when hit.
24 *
25 * @author Mathew A. Nelson (original)
26 * @author Flemming N. Larsen (contributor)
27 */
28public class Fire extends Robot {
29  int dist = 50; // distance to move when we're hit
30
31  /**
32   * run:  Fire's main run function
33   */
34  public void run() {
35    // Set colors
36    setBodyColor(Color.orange);
37    setGunColor(Color.orange);
38    setRadarColor(Color.red);
39    setScanColor(Color.red);
40    setBulletColor(Color.red);
41
42    // Spin the gun around slowly... forever
43    while (true) {
44      turnGunRight(5);
45    }
46  }
47
48  /**
49   * onScannedRobot:  Fire!
50   */
51  public void onScannedRobot(ScannedRobotEvent e) {
52    // If the other robot is close by, and we have plenty of life,
53    // fire hard!
54    if (e.getDistance() < 50 && getEnergy() > 50) {
55      fire(3);
56    } // otherwise, fire 1.
57    else {
58      fire(1);
59    }
60    // Call scan again, before we turn the gun
61    scan();
62  }
63
64  /**
65   * onHitByBullet:  Turn perpendicular to the bullet, and move a bit.
66   */
67  public void onHitByBullet(HitByBulletEvent e) {
68    turnRight(normalRelativeAngleDegrees(90 - (getHeading() - e.getHeading())));
69
70    ahead(dist);
71    dist *= -1;
72    scan();
73  }
74
75  /**
76   * onHitRobot:  Aim at it.  Fire Hard!
77   */
78  public void onHitRobot(HitRobotEvent e) {
79    double turnGunAmt = normalRelativeAngleDegrees(e.getBearing() + getHeading() - getGunHeading());
80
81    turnGunRight(turnGunAmt);
82    fire(3);
83  }
84}
Note: See TracBrowser for help on using the repository browser.