[Home]PluggableRobot/Math2

Robo Home | PluggableRobot | Changes | Preferences | AllPages

Difference (from prior minor revision) (major diff)

Removed: 10d9
import java.awt.Graphics2D;

Changed: 12c11
import java.util.ArrayList;
import java.util.Random;

Changed: 14c13
import robocode.*;
import robocode.util.Utils;

Changed: 17,18c16
* A pluggable listener and strategy architecture for a robot.
* http://robowiki.net/cgi-bin/robowiki?PluggableRobot
* Math utility class.

Changed: 21,28c19,20
public abstract class PluggableRobot extends AdvancedRobot {
private static boolean _battleInitialized = false;
private static Hud _hud;

private ListenerDelegate? _listenerDelegate;
private ArrayList<Component> _components;
private ArrayList<Hud.Painter> _painters;
private Point2D.Double _center;
public final class Math2 {
public static final double PI_OVER_2 = Math.PI / 2;

Changed: 30,79c22
/**
* Sets up the ListenerDelegate? and the Component and Painter lists.
*/
protected PluggableRobot() {
_listenerDelegate = new ListenerDelegate?();
_components = new ArrayList<Component>();
_painters = new ArrayList<Hud.Painter>();
}

/**
* Set up the robot, then continuously collect events and invoke components.
*/
@Override
public final void run() {
// Initialize battle (at start of first round only)
if(!_battleInitialized) {
_hud = new Hud(this);
initializeBattle();
_battleInitialized = true;
}

// Register custom event test() hook for event manager
addCustomEvent(new Condition("eventManager") {
public boolean test() {
PluggableRobot.this.handleEvents();
return false;
}
});

// Round is starting
initializeRound();

// Main loop
while(true) {
for(Component component : _components) {
component.go();
}

execute();
}
}

/**
* This method will be called at the beginning of a battle. Robots can override this method to
* initialize their properties. This is a good place to initialize static properties or set your
* tank color.
*/
protected void initializeBattle() {
// Default implementation does nothing
}
private static final Random random = new Random();

Changed: 81,94c24
/**
* This method will be called at the beginning of each round. Robots can override this method to
* initialize their properties. This is a good place to set up non-static properties and
* register listeners, painters and components.
*/
protected void initializeRound() {
// Default implementation does nothing
}

/**
* Called before events get processed each tick. The default implementation does nothing.
*/
public void onBeforeEventsProcessed?() {
// Do nothing
private Math2() {

Changed: 98c28
* Returns a Point2D.Double object representing the center of the battlefield.
* Returns a random int between the min and max arguments, inclusive.

Changed: 100,103c30,32
public Point2D.Double getCenter() {
if(_center == null) {
_center = new Point2D.Double(getBattleFieldWidth?() / 2, getBattleFieldHeight?() / 2);
}
public static int randomInteger(int min, int max) {
return random.nextInt(max - min + 1) + min;
}

Changed: 105,106c34,39
return _center;
}
/**
* Returns a random double value between 0.0 (inclusive) and 1.0 (exclusive).
*/
public static double randomDouble() {
return random.nextDouble();
}

Added: 107a41,47
/**
* If value is less than min, returns min. If value is greater than max, returns max. Otherwise,
* returns value.
*/
public static double limit(double min, double value, double max) {
return Math.max(min, Math.min(value, max));
}

Changed: 109,114c49,54
/**
* Registers the given EventListener?, which will cause it to receive notifications of the events
* indicated by the listener interfaces it implements.
*/
protected void registerListener(EventListener? listener) {
_listenerDelegate.register(listener);
/**
* Adds the X and Y components of the given Point2D.Double objects and returns a new
* Point2D.Double object with the result.
*/
public static Point2D.Double add(Point2D.Double point1, Point2D.Double point2) {
return new Point2D.Double(point1.x + point2.x, point1.y + point2.y);

Changed: 117,121c57,62
/**
* Reigsters the given Component, which will give it the opportunity to act each turn.
*/
protected void registerComponent(Component component) {
_components.add(component);
/**
* Subtracts the X and Y components of the second given Point2D.Double object from those of the
* first and returns a new Point2D.Double object with the result.
*/
public static Point2D.Double subtract(Point2D.Double point1, Point2D.Double point2) {
return new Point2D.Double(point1.x - point2.x, point1.y - point2.y);

Changed: 125c66,67
* Reigsters the given Painter, which will give it the opportunity to draw on the HUD each turn.
* Returns the absolute bearing in radians from the given origin point to the given target
* point.

Changed: 127,128c69,70
protected void registerPainter(Hud.Painter painter) {
_painters.add(painter);
public static double getAbsoluteTargetBearing?(Point2D.Double origin, Point2D.Double target) {
return Utils.normalAbsoluteAngle?(Math.atan2(target.x - origin.x, target.y - origin.y));

Changed: 132c74,75
* Hand out notifications to the Painters.
* Returns a Point2D.Double object indicating the relative position of an object at the given
* angle and distance from the origin.

Changed: 134,142c77,80
@Override
public final void onPaint(Graphics2D g) {
_hud.setContext(g); // Inject the graphics context into the Hud

for(Hud.Painter painter : _painters) {
painter.paint(_hud, getTime());
}

_hud.setContext(null); // Clear the injected graphics context
public static Point2D.Double getRelativePosition?(double angle, double distance) {
double dx = distance * Math.sin(angle);
double dy = distance * Math.cos(angle);
return new Point2D.Double(dx, dy);

Changed: 146c84,85
* Process all the events in the queue.
* Returns a Point2D.Double object indicating the position of an object at the given angle and
* distance from the given origin point.

Changed: 148,151c87,90
private void handleEvents() {
onBeforeEventsProcessed?();
_listenerDelegate.processEvents(getAllEvents?());
clearAllEvents?();
public static Point2D.Double getAbsolutePosition?(Point2D.Double origin, double angle, double distance) {
double x = origin.x + distance * Math.sin(angle);
double y = origin.y + distance * Math.cos(angle);
return new Point2D.Double(x, y);

Changed: 154,155c93,98
// Since we have our own event manager, we want to prevent overrides of the Robocode event
// methods, so we'll make them final.
/**
* Converts degrees to radians.
*/
public static double degToRad?(double degrees) {
return degrees * Math.PI / 180;
}

Changed: 157,203c100,105
@Override
public final void onCustomEvent(CustomEvent event) {
}

@Override
public final void onDeath(DeathEvent? event) {
}

@Override
public final void onSkippedTurn(SkippedTurnEvent? event) {
}

@Override
public final void onBulletHit?(BulletHitEvent? event) {
}

@Override
public final void onBulletHitBullet?(BulletHitBulletEvent? event) {
}

@Override
public final void onBulletMissed?(BulletMissedEvent? event) {
}

@Override
public final void onHitByBullet?(HitByBulletEvent? event) {
}

@Override
public final void onHitRobot?(HitRobotEvent? event) {
}

@Override
public final void onHitWall?(HitWallEvent? event) {
}

@Override
public final void onRobotDeath?(RobotDeathEvent? event) {
}

@Override
public final void onScannedRobot(ScannedRobotEvent event) {
}

@Override
public final void onWin(WinEvent? event) {
}
/**
* Converts radians to degrees.
*/
public static double radToDeg?(double radians) {
return radians * 180 / Math.PI;
}

Changed: 205c107
</nowiki></pre>
</nowiki></pre>

/*
 * PluggableRobot, by Robert J. Walker
 * Home page: http://robowiki.net/cgi-bin/robowiki?PluggableRobot
 * This software is made available under the RoboWiki Limited Public Code License (RWLPCL). The full
 * text of the license may be found at http://robowiki.net/cgi-bin/robowiki?RWLPCL.
 */
package rjw.pluggablerobot;

import java.awt.geom.Point2D;
import java.util.Random;

import robocode.util.Utils;

/**
 * Math utility class.
 * @author Robert J. Walker
 */
public final class Math2 {
	public static final double PI_OVER_2 = Math.PI / 2;

	private static final Random random = new Random();

	private Math2() {
	}

	/**
	 * Returns a random int between the min and max arguments, inclusive.
	 */
    public static int randomInteger(int min, int max) {
    	return random.nextInt(max - min + 1) + min;
    }

    /**
     * Returns a random double value between 0.0 (inclusive) and 1.0 (exclusive).
     */
    public static double randomDouble() {
    	return random.nextDouble();
    }

    /**
     * If value is less than min, returns min. If value is greater than max, returns max. Otherwise,
     * returns value.
     */
    public static double limit(double min, double value, double max) {
        return Math.max(min, Math.min(value, max));
    }

    /**
     * Adds the X and Y components of the given Point2D.Double objects and returns a new
     * Point2D.Double object with the result.
     */
    public static Point2D.Double add(Point2D.Double point1, Point2D.Double point2) {
		return new Point2D.Double(point1.x + point2.x, point1.y + point2.y);
	}

    /**
     * Subtracts the X and Y components of the second given Point2D.Double object from those of the
     * first and returns a new Point2D.Double object with the result.
     */
	public static Point2D.Double subtract(Point2D.Double point1, Point2D.Double point2) {
		return new Point2D.Double(point1.x - point2.x, point1.y - point2.y);
	}

	/**
	 * Returns the absolute bearing in radians from the given origin point to the given target
	 * point.
	 */
	public static double getAbsoluteTargetBearing(Point2D.Double origin, Point2D.Double target) {
		return Utils.normalAbsoluteAngle(Math.atan2(target.x - origin.x, target.y - origin.y));
	}

	/**
	 * Returns a Point2D.Double object indicating the relative position of an object at the given
	 * angle and distance from the origin.
	 */
	public static Point2D.Double getRelativePosition(double angle, double distance) {
		double dx = distance * Math.sin(angle);
		double dy = distance * Math.cos(angle);
		return new Point2D.Double(dx, dy);
	}

	/**
	 * Returns a Point2D.Double object indicating the position of an object at the given angle and
	 * distance from the given origin point.
	 */
	public static Point2D.Double getAbsolutePosition(Point2D.Double origin, double angle, double distance) {
		double x = origin.x + distance * Math.sin(angle);
		double y = origin.y + distance * Math.cos(angle);
		return new Point2D.Double(x, y);
	}

    /**
     * Converts degrees to radians.
     */
    public static double degToRad(double degrees) {
    	return degrees * Math.PI / 180;
    }

    /**
     * Converts radians to degrees.
     */
    public static double radToDeg(double radians) {
    	return radians * 180 / Math.PI;
    }
}

Robo Home | PluggableRobot | Changes | Preferences | AllPages
Edit text of this page | View other revisions
Last edited November 3, 2007 0:52 EST by RobertWalker (diff)
Search: