Module/Radar/SpinningBestStartDirection

From Robowiki
< Module‎ | Radar
Revision as of 22:13, 26 March 2008 by Jab (talk | contribs) (Module Radar SpinningBestStartDirection)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search
package jab.radar;

import robocode.util.Utils;
import jab.Module;
import jab.Radar;


/**
 * Credits
 * SpinningBestStartDirection by jab. Code license: RWPCL
 * Calculate if the radar must turns clockwise or not.
 * The main idea is to start scanning the opposite quadrant (biggest area)
 * as quick as possible, and if it starts scanning that quadrant
 * scan in the direction of that quadrant that has more area.
 */
public class SpinningBestStartDirection extends Radar {

	public SpinningBestStartDirection (Module bot) {
		super(bot);
	}

	private double radarClockwise;
	
	public void scan() {
		// Get the start radar direction only when the battle begins
		if (radarClockwise==0) radarClockwise = getRadarDirection();
		bot.setTurnRadarRightRadians(Double.POSITIVE_INFINITY * radarClockwise);
	}

	
	private int getRadarDirection() {

		double width = bot.getBattleFieldWidth();
		double height = bot.getBattleFieldHeight();

		double radarHeadingRadians = bot.getRadarHeadingRadians();
		double to0 = Math.abs(Utils.normalRelativeAngle(radarHeadingRadians));
		double to90 = Math.abs(Utils.normalRelativeAngle(radarHeadingRadians
				- Math.PI / 2));
		double to180 = Math.abs(Utils.normalRelativeAngle(radarHeadingRadians
				- Math.PI));
		double to270 = Math.abs(Utils.normalRelativeAngle(radarHeadingRadians
				- 3 * Math.PI / 2));
		// 1 2
		// 3 4
		if (bot.getX() <= width / 2 && bot.getY() >= height / 2)
			return (to90 < to180) ? 1 : -1;
		if (bot.getX() >= width / 2 && bot.getY() >= height / 2)
			return (to180 < to270) ? 1 : -1;
		if (bot.getX() <= width / 2 && bot.getY() <= height / 2)
			return (to0 < to90) ? 1 : -1;
		return (to270 < to0) ? 1 : -1;
	}

}