User:AW/help/skippedTurns

From Robowiki
Jump to navigation Jump to search

I am having a problem with my robot, in robocode 1.7.2.2 it would cause robocode to crash, but in 1.7.3.0 it just causes my robot to skip turns. here is the code:

package aw;

import robocode.*;
import robocode.util.*;
import aw.Mallorn.tree.*;
import java.util.*;

public class advanced extends AdvancedRobot {

	UberBucket gunTree = new UberBucket(24, 5);

	List<KNearestNeighborGFWave> KNearestNeighborGFWaves = new ArrayList<KNearestNeighborGFWave>();

	HashMap<double[], Double> hashMapOfPoints = new HashMap<double[], Double>();

	int direction = 1;
	double gunPower = 0.5;
	double enemyBearing = 0;
	double enemyHeading = 0;
	double enemyAbsBearing = 0;
	double enemyEnergy = 100;
	double oldEnemyEnergy = 100;
	double enemyDistance = 0;
	double enemyVelocity = 0;

	double enemyX;
	double enemyY;
	double closeX;
	double closeY;

	int count;
	int pointsAdded;

	public void run() {
		setAdjustRadarForGunTurn(true);
		setAdjustGunForRobotTurn(true);
		setAdjustRadarForRobotTurn(true);

		while (true) {
			if (getRadarTurnRemainingRadians() == 0) {
				setTurnRadarRightRadians(Double.POSITIVE_INFINITY);
			}

			execute();
		}

	}

	public void onScannedRobot(ScannedRobotEvent e) {
		out.println("onScannedRobot");
		updateWaves(KNearestNeighborGFWaves);
		count++;

		double radarTurn = (Utils.normalRelativeAngle(getHeadingRadians()
				+ e.getBearingRadians() - getRadarHeadingRadians()));
		if (radarTurn > 0)
			radarTurn += 0.3;
		else
			radarTurn -= 0.3;

		setTurnRadarRightRadians(radarTurn);

		enemyBearing = e.getBearingRadians();
		enemyHeading = e.getHeadingRadians();
		enemyAbsBearing = (getHeadingRadians() + e.getBearingRadians());
		enemyEnergy = e.getEnergy();
		enemyDistance = e.getDistance();
		enemyVelocity = e.getVelocity();
		enemyX = getX() + enemyDistance * Math.sin((enemyAbsBearing));
		enemyY = getY() + enemyDistance * Math.cos((enemyAbsBearing));

		// don't try to figure out the direction they're moving
		// they're not moving, just use the direction we had before
		if (enemyVelocity != 0) {
			if (Math.sin(enemyHeading - enemyAbsBearing) * enemyVelocity < 0)
				direction = -1;
			else
				direction = 1;
		}
		KNearestNeighborGFWave wave = new KNearestNeighborGFWave(getX(),
				getY(), enemyAbsBearing, 1.99, direction, getTime(), 5);
		wave.dataWhenFired[0] = Math.abs(enemyVelocity) / 8;
		wave.dataWhenFired[1] = enemyBearing / (Math.PI * 2);
		wave.dataWhenFired[2] = (enemyDistance / 1200);
		wave.dataWhenFired[3] = enemyHeading / (2 * Math.PI);
		wave.dataWhenFired[4] = Utils
				.normalAbsoluteAngle((enemyHeading - enemyAbsBearing)
						/ (2 * Math.PI));
		KNearestNeighborGFWaves.add(wave);

		double[] SearchPoint = new double[5];
		SearchPoint[0] = (Math.abs(enemyVelocity) / 8);
		SearchPoint[1] = (enemyBearing / (Math.PI * 2));
		SearchPoint[2] = (enemyDistance / 1200);
		SearchPoint[3] = (enemyHeading / (2 * Math.PI));
		SearchPoint[4] = (Utils
				.normalAbsoluteAngle((enemyHeading - enemyAbsBearing)) / (2 * Math.PI));

		double gunTurn = getHeadingRadians() + e.getBearingRadians()
				- getGunHeadingRadians();
		// out.println("( " + closeX + ", " + closeY + " )");
		if (getGunHeat() < 2 * getGunCoolingRate() && getEnergy() > 2) {
			double[][] nearestPoints = gunTree.getNNearestPoints(SearchPoint,
					20);
			 out.println("nearest points retrieved");
			
			double botWidth = 1 / Math.atan(18 / enemyDistance);
			final double EulersConstant = 2.7182818284;
			double[] angles = new double[20];
			for (int i = 0; i < 20; i++) {
				if (nearestPoints[i] != null)
					angles[i] = hashMapOfPoints.get(nearestPoints[i]);
				else {
					out.println("angles[" + i + "] was null");
					angles[i] = 0.0;// set to GF 0
				}
//				out.println(angles[i]);
			}
			 out.println("angles set");

			double[] scores = new double[20];
			int maxI = 0;
			double maxScore = 0;
			for (int i = 0; i < 20; i++) {
				for (double angle : angles) {
					double ux = (angle - angles[i]) * botWidth;
					scores[i] += Math.pow(EulersConstant, -0.5 * ux * ux);
				}
				if (scores[i] > maxScore) {
					maxScore = scores[i];
					maxI = i;
				}
			}
			 out.println("best angle found");


			gunTurn += direction * maxEscapeAngle(20 - 3 * 1.99) * angles[maxI];

			out.println("gunTurn set");
			// out.println(offsetAngle + "     " + direction * offsetAngle);
			// setTurnGunRightRadians(offsetAngle);
			// out.println(offsetAngle);
		}
		setTurnGunRightRadians((Utils.normalRelativeAngle(gunTurn)));

		out.println("setTurnGunRightRadians");
		setFire(1.99);
		out.println("setFire");
	}

	public void updateWaves(List<KNearestNeighborGFWave> WavesCollection) {
		out.println("update waves started");
		for (int i = 0; i < WavesCollection.size(); i++) {
			KNearestNeighborGFWave currentWave = (KNearestNeighborGFWave) WavesCollection
					.get(i);
			if (currentWave.getGF(enemyX, enemyY, getTime()) != -3) {
				double[] point = new double[5];
				point[0] = currentWave.getDataWhenFired(0);
				point[1] = currentWave.getDataWhenFired(1);
				point[2] = currentWave.getDataWhenFired(2);
				point[3] = currentWave.getDataWhenFired(3);
				point[4] = currentWave.getDataWhenFired(4);

				pointsAdded++;
				out.println(pointsAdded);

				gunTree.addPoint(point);
				hashMapOfPoints.put(point,
						currentWave.getGF(enemyX, enemyY, getTime()));
				WavesCollection.remove(i);
				i--;
			}
		}
		out.println("update waves ended");

	}

	private double maxEscapeAngle(double bulletSpeed) {
		return Math.atan(8.0 / bulletSpeed);
	}
	
	public void onSkippedTurn(SkippedTurnEvent e) {
		
	}

	// public void onPaint(Graphics2D g) {
	// int x, y;
	// g.fillRect((int)(closeX - 5), (int)(closeY - 5), 10, 10);
	// }

}


the robot prints out "setFire" and then starts skipping turns. So my question is basically this: Is stdout buffered so that println could be called but nothing printed until the method ends or am I doing something stupid in the above code? Or am I just crazy (this is not really a question, the answer is well known)?