Talon/source

From Robowiki
< Talon
Revision as of 18:00, 30 January 2011 by Chase-san (talk | contribs) (Thanks Rednaxela for the suggestion)
Jump to navigation Jump to search
package cs;
import robocode.util.Utils;
import robocode.*;
import java.util.*;
import java.awt.*;

/**
 * Talon - a robot by Chase
 * Nano Melee bot
 *
 * The idea is to avoid driving toward enemy robots,
 * nearer ones are more dangerous to drive towards
 */
public final class Talon extends AdvancedRobot {
	private static double[][] map = new double[10][2];
	private static int n = 0;

	public void run() {
		double angle, gn, bd, md;
		
		setAllColors(Color.red);
		
		setAdjustGunForRobotTurn(true);
		setAdjustRadarForGunTurn(true);
		
		while(true) {
			angle = 6.3;
			gn = bd = 2000;
			while(angle > 0) {
				angle -= 0.1;
				int t = getOthers();
				try {
					while(true) {
						double[] v = map[--t];
						if(v[0] < gn) {
							gn = v[0];
							setTurnGunRightRadians(Utils.normalRelativeAngle(v[1] - getGunHeadingRadians()));
						}
						
						md = Math.abs(Utils.normalRelativeAngle(v[1]-angle)) + (120/v[0]);
						
						if(md < bd) {
							bd = md;
							setTurnRightRadians(Utils.normalRelativeAngle(angle - getHeadingRadians()));
							setAhead(20);
						}
					}
				} catch(Exception e) {}
			}

			setFire(gn);
			
			turnRadarRightRadians(1);
		}
	}

	public void onScannedRobot(ScannedRobotEvent e) {
		try {
			map[--n] = new double[]{e.getDistance(),getHeadingRadians() + e.getBearingRadians()};
		} catch(Exception ex) {
			n = getOthers();
		}
	}
}