[Home]David Alves/LinearTargetingBot

Robo Home | David Alves | Changes | Preferences | AllPages

An example of what you can do by extending DrawingBot.

package davidalves.sample;
import robocode.*;
import java.awt.Color;
import java.awt.geom.Point2D;
import robocode.util.Utils;

public class LinearTargetingBot extends DrawingBot{
	
	public void run() {
		setAdjustRadarForGunTurn(true);
		do{
			turnRadarRightRadians(Double.POSITIVE_INFINITY);
		} while(true);
	}
	
	public void onScannedRobot(ScannedRobotEvent e) {
		double bulletPower = Math.min(3.0, getEnergy());
		Point2D.Double me = new Point2D.Double(getX(), getY());
		Point2D.Double enemy = project(me, getHeadingRadians() + e.getBearingRadians(), e.getDistance());
		
		
		double deltaTime = 1;
		double battleFieldHeight = getBattleFieldHeight(), battleFieldWidth = getBattleFieldWidth();
		
		Point2D.Double predicted = (Point2D.Double) enemy.clone();
		
		while(deltaTime * (20.0 - 3.0 * bulletPower) < me.distance(predicted)){		
			deltaTime++;
			predicted = project(predicted, e.getHeadingRadians(), e.getVelocity());
			
			predicted.x = Math.min(Math.max(18.0, predicted.x), battleFieldWidth - 18.0);	
			predicted.y = Math.min(Math.max(18.0, predicted.y), battleFieldHeight - 18.0);
		}
		drawLine(enemy, predicted, Color.RED);
		drawCircle(predicted, 15, Color.RED);
		drawLine(me, predicted, Color.GREEN);
		setTurnRadarRightRadians(Utils.normalRelativeAngle(angle(me, enemy) - getRadarHeadingRadians()));
		setTurnGunRightRadians(Utils.normalRelativeAngle(angle(me, predicted) - getGunHeadingRadians()));
		setFire(3);
		execute();
	}
	
	public static Point2D.Double project(Point2D.Double origin, double angle, double distance){
		return new Point2D.Double(origin.x + distance * Math.sin(angle), origin.y + distance * Math.cos(angle));
	}
	
	public static double angle(Point2D.Double origin, Point2D.Double destination){
		return Utils.normalAbsoluteAngle(Math.atan2(destination.x - origin.x, destination.y - origin.y));
	}
}

Robo Home | David Alves | Changes | Preferences | AllPages
Edit text of this page | View other revisions
Last edited November 11, 2006 10:06 EST by David Alves (diff)
Search: