<commit_before>// File created: prior to 2005-12-01

package deewiant.perception;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.geom.Arc2D;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import java.util.Collections;

import robocode.Rules;
import robocode.util.Utils;

import deewiant.common.Enemy;
import deewiant.common.Global;
import deewiant.common.Tools;

public final class Perception {
	private static final Rectangle2D[] quadrants = new Rectangle2D.Double[9];
	private        final double        gunCoolingRate;

	private static final double        EXTRATURN = Math.PI / 16;

	private double
		radarHeading,
		prevRadarHeading,
		prevCW,
		prevCCW,
		prevToCW,
		prevToCCW,
		priorToCircle = -1;

	private boolean
		// True if going clockwise, false if going counterclockwise.
		//
		// The main purpose of clockwise is to prevent the spin direction
		// spontaneously changing when we switch between radar modes (corner arc
		// to edge arc, enemies arc to edge/corner arc, any arc to full circle,
		// etc.)
		clockwise = true,
		// somebody nearby that we haven't seen, woops!
		pleaseFullCircle = false,
		wasInArc = true;

	private final Arc2D
		// temp arc used here and there
		arc = new Arc2D.Double(),
		// the arc the radar last spun through
		radarArc = new Arc2D.Double();

	private static final double[]
		arcCentres = {
			// edges
			1 * Math.PI,
			3 * Math.PI / 2,
			0,
			1 * Math.PI / 2,

			// corners
			3 * Math.PI / 4,
			5 * Math.PI / 4,
			7 * Math.PI / 4,
			1 * Math.PI / 4,
		},
		arcExtents = {
			Math.PI,   Math.PI,   Math.PI,   Math.PI,
			Math.PI/2, Math.PI/2, Math.PI/2, Math.PI/2,
		};

	public Perception() {
		gunCoolingRate = Global.bot.getGunCoolingRate();

		final double
			cornerSize = Math.hypot(5 * Tools.BOT_WIDTH, 5 * Tools.BOT_HEIGHT),
			w = Global.mapWidth,
			h = Global.mapHeight;

		/* split battlefield into 9 quadrants:
		 *   one for each corner (5-8, clockwise from top)
		 *   one for each edge   (1-4, clockwise from top)
		 *   one for the middle  (0)
		 * (in the array in order of size)
		 * so (note origin O at *lower* left corner):
		 *
		 * +---+---------+---+
		 * |   |         |   |
		 * | 5 |    1    | 6 |
		 * |   |         |   |
		 * +---+---------+---+
		 * |   |         |   |
		 * |   |         |   |
		 * |   |         |   |
		 * |   |         |   |
		 * | 4 |    0    | 2 |
		 * |   |         |   |
		 * |   |         |   |
		 * |   |         |   |
		 * |   |         |   |
		 * +---+---------+---+
		 * |   |         |   |
		 * | 8 |    3    | 7 |
		 * |   |         |   |
		 * O---+---------+---+
		 */

		if (quadrants[0] == null) {
			quadrants[0] = new Rectangle2D.Double(    cornerSize,     cornerSize, w - 2*cornerSize, h - 2*cornerSize);
			quadrants[1] = new Rectangle2D.Double(    cornerSize, h - cornerSize, w - 2*cornerSize,       cornerSize);
			quadrants[2] = new Rectangle2D.Double(w - cornerSize,     cornerSize,       cornerSize, h - 2*cornerSize);
			quadrants[3] = new Rectangle2D.Double(    cornerSize,              0, w - 2*cornerSize,       cornerSize);
			quadrants[4] = new Rectangle2D.Double(             0,     cornerSize,       cornerSize, h - 2*cornerSize);
			quadrants[5] = new Rectangle2D.Double(             0, h - cornerSize,       cornerSize,       cornerSize);
			quadrants[6] = new Rectangle2D.Double(w - cornerSize, h - cornerSize,       cornerSize,       cornerSize);
			quadrants[7] = new Rectangle2D.Double(w - cornerSize,              0,       cornerSize,       cornerSize);
			quadrants[8] = new Rectangle2D.Double(             0,              0,       cornerSize,       cornerSize);
		}
	}

	// for when we've been shot at from a
	public void hiddenDudeAt(final double bearing) {
		if (getQuadrant() != 0) {
			pleaseFullCircle = true;
			// spin toward bearing as fast as possible
			clockwise = bearing >= 0;
		}
	}

	public void perceive() {
		radarHeading = Global.bot.getRadarHeadingRadians();

		// if just started, spin 360 degrees once
		final long now = Global.bot.getTime();
		if (now < 8)
			Global.bot.setTurnRadarRightRadians(Double.POSITIVE_INFINITY);
		else {
			// check for long-lost dudes
			// straight from RobotPeer
			final double prevAngle =
				Utils.normalAbsoluteAngle(prevRadarHeading - Math.PI/2);
			final double diff =
				Utils.normalRelativeAngle(radarHeading - prevRadarHeading);

			radarArc.setArc(
				Global.me.getX() - Rules.RADAR_SCAN_RADIUS,
				Global.me.getY() - Rules.RADAR_SCAN_RADIUS,
				2 * Rules.RADAR_SCAN_RADIUS,
				2 * Rules.RADAR_SCAN_RADIUS,
				Tools.toDeg(prevAngle),
				Tools.toDeg(diff),
				Arc2D.PIE);

			for (final Enemy dude : Global.dudes)
			if (!dude.positionUnknown && !dude.justSeen &&
				(
					radarArc.contains(dude.boundingBox) ||

					dude.distance > Rules.RADAR_SCAN_RADIUS
					// More accurate than the above but if he's that far it's
					// usually only one or two turns before he's really out of sight
					// anyway, so just use the quicker check
					//
					// !dude.boundingBox.intersectsLine(
					// 	new Line2D.Double(
					// 		Global.me,
					// 		Tools.projectVector(
					// 			Global.me,
					// 			Tools.currentAbsBearing(dude, Global.me),
					// 			Rules.RADAR_SCAN_RADIUS)))
				)
			)<commit_msg>Comment: positionUnknown checking isn't quite optimal.<commit_after>// File created: prior to 2005-12-01

package deewiant.perception;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.geom.Arc2D;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import java.util.Collections;

import robocode.Rules;
import robocode.util.Utils;

import deewiant.common.Enemy;
import deewiant.common.Global;
import deewiant.common.Tools;

public final class Perception {
	private static final Rectangle2D[] quadrants = new Rectangle2D.Double[9];
	private        final double        gunCoolingRate;

	private static final double        EXTRATURN = Math.PI / 16;

	private double
		radarHeading,
		prevRadarHeading,
		prevCW,
		prevCCW,
		prevToCW,
		prevToCCW,
		priorToCircle = -1;

	private boolean
		// True if going clockwise, false if going counterclockwise.
		//
		// The main purpose of clockwise is to prevent the spin direction
		// spontaneously changing when we switch between radar modes (corner arc
		// to edge arc, enemies arc to edge/corner arc, any arc to full circle,
		// etc.)
		clockwise = true,
		// somebody nearby that we haven't seen, woops!
		pleaseFullCircle = false,
		wasInArc = true;

	private final Arc2D
		// temp arc used here and there
		arc = new Arc2D.Double(),
		// the arc the radar last spun through
		radarArc = new Arc2D.Double();

	private static final double[]
		arcCentres = {
			// edges
			1 * Math.PI,
			3 * Math.PI / 2,
			0,
			1 * Math.PI / 2,

			// corners
			3 * Math.PI / 4,
			5 * Math.PI / 4,
			7 * Math.PI / 4,
			1 * Math.PI / 4,
		},
		arcExtents = {
			Math.PI,   Math.PI,   Math.PI,   Math.PI,
			Math.PI/2, Math.PI/2, Math.PI/2, Math.PI/2,
		};

	public Perception() {
		gunCoolingRate = Global.bot.getGunCoolingRate();

		final double
			cornerSize = Math.hypot(5 * Tools.BOT_WIDTH, 5 * Tools.BOT_HEIGHT),
			w = Global.mapWidth,
			h = Global.mapHeight;

		/* split battlefield into 9 quadrants:
		 *   one for each corner (5-8, clockwise from top)
		 *   one for each edge   (1-4, clockwise from top)
		 *   one for the middle  (0)
		 * (in the array in order of size)
		 * so (note origin O at *lower* left corner):
		 *
		 * +---+---------+---+
		 * |   |         |   |
		 * | 5 |    1    | 6 |
		 * |   |         |   |
		 * +---+---------+---+
		 * |   |         |   |
		 * |   |         |   |
		 * |   |         |   |
		 * |   |         |   |
		 * | 4 |    0    | 2 |
		 * |   |         |   |
		 * |   |         |   |
		 * |   |         |   |
		 * |   |         |   |
		 * +---+---------+---+
		 * |   |         |   |
		 * | 8 |    3    | 7 |
		 * |   |         |   |
		 * O---+---------+---+
		 */

		if (quadrants[0] == null) {
			quadrants[0] = new Rectangle2D.Double(    cornerSize,     cornerSize, w - 2*cornerSize, h - 2*cornerSize);
			quadrants[1] = new Rectangle2D.Double(    cornerSize, h - cornerSize, w - 2*cornerSize,       cornerSize);
			quadrants[2] = new Rectangle2D.Double(w - cornerSize,     cornerSize,       cornerSize, h - 2*cornerSize);
			quadrants[3] = new Rectangle2D.Double(    cornerSize,              0, w - 2*cornerSize,       cornerSize);
			quadrants[4] = new Rectangle2D.Double(             0,     cornerSize,       cornerSize, h - 2*cornerSize);
			quadrants[5] = new Rectangle2D.Double(             0, h - cornerSize,       cornerSize,       cornerSize);
			quadrants[6] = new Rectangle2D.Double(w - cornerSize, h - cornerSize,       cornerSize,       cornerSize);
			quadrants[7] = new Rectangle2D.Double(w - cornerSize,              0,       cornerSize,       cornerSize);
			quadrants[8] = new Rectangle2D.Double(             0,              0,       cornerSize,       cornerSize);
		}
	}

	// for when we've been shot at from a
	public void hiddenDudeAt(final double bearing) {
		if (getQuadrant() != 0) {
			pleaseFullCircle = true;
			// spin toward bearing as fast as possible
			clockwise = bearing >= 0;
		}
	}

	public void perceive() {
		radarHeading = Global.bot.getRadarHeadingRadians();

		// if just started, spin 360 degrees once
		final long now = Global.bot.getTime();
		if (now < 8)
			Global.bot.setTurnRadarRightRadians(Double.POSITIVE_INFINITY);
		else {
			// check for long-lost dudes
			// straight from RobotPeer
			final double prevAngle =
				Utils.normalAbsoluteAngle(prevRadarHeading - Math.PI/2);
			final double diff =
				Utils.normalRelativeAngle(radarHeading - prevRadarHeading);

			radarArc.setArc(
				Global.me.getX() - Rules.RADAR_SCAN_RADIUS,
				Global.me.getY() - Rules.RADAR_SCAN_RADIUS,
				2 * Rules.RADAR_SCAN_RADIUS,
				2 * Rules.RADAR_SCAN_RADIUS,
				Tools.toDeg(prevAngle),
				Tools.toDeg(diff),
				Arc2D.PIE);

			for (final Enemy dude : Global.dudes)
			if (!dude.positionUnknown && !dude.justSeen &&
				(
					// This isn't quite optimal: if an enemy is orbiting really
					// close by, this might be true even though we know that the
					// enemy is at most one radar sweep ahead.
					radarArc.contains(dude.boundingBox) ||

					dude.distance > Rules.RADAR_SCAN_RADIUS
					// More accurate than the above but if he's that far it's
					// usually only one or two turns before he's really out of sight
					// anyway, so just use the quicker check
					//
					// !dude.boundingBox.intersectsLine(
					// 	new Line2D.Double(
					// 		Global.me,
					// 		Tools.projectVector(
					// 			Global.me,
					// 			Tools.currentAbsBearing(dude, Global.me),
					// 			Rules.RADAR_SCAN_RADIUS)))
				)
			)
{'fraction_non_alphanumeric': 0.1267123287671233, 'fraction_numerical': 0.01814142910033321, 'mean_word_length': 2.1318840579710145, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7372828', 'n_tokens_mistral': 4383, 'n_tokens_neox': 3667, 'n_words': 935}