Ocnirp/Source Code

From Robowiki
< Ocnirp
Revision as of 11:15, 24 May 2009 by Nat (talk | contribs) (update the code to be more readable and with more comment)
Jump to navigation Jump to search
Ocnirp Sub-pages:
OcnirpVersion History - Source Code

package nat.nano;

import robocode.AdvancedRobot;
import robocode.HitWallEvent;
import robocode.ScannedRobotEvent;
import robocode.util.Utils;

/**
 * Ocnirp - a robot by Nat Pavasant
 * 
 * "Battle for the one-on-one nano-bot crown!"
 * 
 * @version 1.4
 * @author Nat Pavasant
 */
public final class Ocnirp extends AdvancedRobot {
	/**
	 * Size of pattern to match. 45 is the maximum value or robot will skip a
	 * turn in unacceptable rate. 30 is a standard value.
	 */
	private static final int PATTERN_DEPTH = 45;
	
	/**
	 * If Math.random() exceed this number, the robot will change the direction.
	 */
	private static final double CHANGE_INTERVAL = 0.9454623d;
	
	/**
	 * Preferred distance between this robot and enemy.
	 */
	private static final double PREFERRED_DISTANCE = 160d;
	
	/**
	 * Fire power, it used to be 2.5, but my crazy part of my brain told me to
	 * reduce this a bit.
	 */
	private static final double FIRE_POWER = 2.4433565d;
	
	/**
	 * Bullet velocity. It should be {@code 20 - 3 * FIRE_POWER}, but my crazy
	 * part of my brain told me to leave this 12.5
	 */
	private static final double BULLET_VELOCITY = 12.5d;
	
	/**
	 * Distance offset to turn
	 */
	private static final double DISTANCE_OFFSET = 0.38435d;
	
	/**
	 * Velocity trick multiplier, make this higher will make more change that
	 * robot will move at max velocity
	 */
	private static final double VELOCITY_MULTIPILER = 17.78456345d;
	
	/**
	 * Hard-coded bullet fight time make a code much smaller since my distance
	 * controller control all the distance.
	 */
	private static final int BFT = (int) (PREFERRED_DISTANCE / BULLET_VELOCITY + 2);
	
	/**
	 * A static variable to keep which direction it is moving.
	 */
	private static int dir = 100;
	
	/**
	 * Main robot method. For nanobot, this contains only radar control.
	 */
	public void run() {
		/*
		 * Performance Enhancing Bug! Have this cost 10 bytes and make me lose
		 * score.
		 */
		// setAdjustGunForRobotTurn(true);
		// setAdjustRadarForGunTurn(true);
		turnRadarRightRadians(Double.POSITIVE_INFINITY);
	}
	
	/**
	 * What to do when you see another robot.
	 * 
	 * Main robot operation method for almost all nano/micro/mini robots.
	 */
	public void onScannedRobot(ScannedRobotEvent e) {
		/*
		 * Variable order here is important. We can save ~3-4 bytes here. since
		 * all math operation must be done on registers. Java virtual machine
		 * has 4 register slot. First register will be taken by {@code this}
		 * object. Then each parameters will take the rest of registers. If
		 * there are any registers left, the variables defined in the method is
		 * filled the register until it full. Every type will take 1 registers
		 * except double, which take 2. If double is the fourth variable
		 * defined, it will overflow to the extra fifth register.
		 * 
		 * So, the most
		 * used variable should define first, which will be enemy absolute
		 * bearing in most cases. In this case, we define most used int before
		 * and make double overflow its register.
		 */
		int matchLen = PATTERN_DEPTH;	// PM Stuff
		double angle;					// enemy absolute bearing
		double dist;					// enemy distance
		int matchPos;					// PM Stuff
		
// ///////////////////////////////////////////////////////////////////////////////
// Radar
//
// Do infinity lock
//
		setTurnRadarLeftRadians(getRadarTurnRemaining());
//
// ///////////////////////////////////////////////////////////////////////////////

// ///////////////////////////////////////////////////////////////////////////////
// Movement (Perpendicular and Random with distance control)
//
// Turn perpendicular to an enemy, and add/deduct the angle a bit to
// control the distance
//
		setTurnRightRadians(
				Math.cos(angle = e.getBearingRadians())
				- Math.signum(
						getVelocity() * ((dist = e.getDistance()) - PREFERRED_DISTANCE)
				) * DISTANCE_OFFSET);
//
// Move ahead
//
		setAhead(dir);
//
// If random() exceed the change interval
//
		if (Math.random() > CHANGE_INTERVAL) {
//
// Reverse direction
//
			onHitWall(null);
//			
// Velocity trick
//
			setMaxVelocity(Math.random() * VELOCITY_MULTIPILER + 1);
//
// End if
//
		}
//
// ///////////////////////////////////////////////////////////////////////////////

// ///////////////////////////////////////////////////////////////////////////////
// Eric Simonton's Lateral Velocity Pattern Matching Gun
//
// Define counter
//
		int i;
//
// Append enemy log
//
		enemyHistory = String.valueOf(
				(char) /* Math.round */(/* 2 * */(e.getVelocity() * Math.sin(e
						.getHeadingRadians()
						- (angle += getHeadingRadians()))))).concat(
				enemyHistory);
//
// Match the history
//
		while ((matchPos = enemyHistory.indexOf(enemyHistory.substring(0,
				matchLen--), i = BFT)) < 0)
			;
//
// Get the firing angle
//
		do
			angle +=
				/* Math.asin */
				((short) enemyHistory.charAt(--matchPos) / dist
				/* 0.5 */);
		while (--i > 0);
//
// Turn the gun
//
		setTurnGunRightRadians(Utils.normalRelativeAngle(angle
				- getGunHeadingRadians()));
//
// Fire
//
		setFire(FIRE_POWER);
//
// ///////////////////////////////////////////////////////////////////////////////
	}
	
	/**
	 * When hit wall, reverse direction
	 */
	public void onHitWall(HitWallEvent e) {
		dir = -dir;
	}
	
	/**
	 * Enemy history string. Preload to prevent StringIndexOutOfBoundException.
	 */
	private static String enemyHistory = "" + (char) 0 + (char) 0 + (char) 0
			+ (char) 0 + (char) 0 + (char) 0 + (char) 0 + (char) 0 + (char) 0
			+ (char) 0 + (char) 0 + (char) 0 + (char) 0 + (char) 0 + (char) 0
			+ (char) 0 + (char) 0 + (char) 0 + (char) 0 + (char) 0 + (char) 0
			+ (char) 0 + (char) 0 + (char) 0 + (char) 0 + (char) 0 + (char) 0
			+ (char) 0 + (char) 0 + (char) 0 + (char) 0 + (char) 0 + (char) 0
			+ (char) 0 + (char) 0 + (char) 0 + (char) 0 + (char) 0 + (char) 0
			+ (char) 0 + (char) 0 + (char) 0 + (char) 0 + (char) 0 + (char) 0
			+ (char) 0 + (char) 0 + (char) 0 + (char) 0 + (char) 0 + (char) 0
			+ (char) 0 + (char) 0 + (char) 0 + (char) 0 + (char) 0 + (char) 0
			+ (char) 0 + (char) 0 + (char) 0 + (char) 0 + (char) 0 + (char) 0
			+ (char) 0 + (char) 0 + (char) 0 + (char) 0 + (char) 0 + (char) 0
			+ (char) 0 + (char) 0 + (char) 0 + (char) 0 + (char) 0 + (char) 0
			+ (char) 0 + (char) 0 + (char) 0 + (char) 0 + (char) 0 + (char) 0
			+ (char) 0 + (char) 0 + (char) 1 + (char) 0 + (char) 0 + (char) 0
			+ (char) 0 + (char) 0 + (char) 0 + (char) 0 + (char) 0 + (char) 0
			+ (char) 0 + (char) 0 + (char) 0 + (char) 0 + (char) 0 + (char) 0
			+ (char) 0 + (char) 0 + (char) 0 + (char) 0 + (char) 0 + (char) 0
			+ (char) 0 + (char) 0 + (char) 0 + (char) 0 + (char) 0 + (char) 0
			+ (char) 0 + (char) 0 + (char) 0 + (char) 0 + (char) 0 + (char) 0
			+ (char) 0 + (char) 0 + (char) 0 + (char) 0 + (char) 0 + (char) 0
			+ (char) 0 + (char) 0 + (char) 0 + (char) 0 + (char) 0 + (char) 0
			+ (char) 0 + (char) 0 + (char) 0 + (char) 0 + (char) 0 + (char) 0
			+ (char) 0 + (char) 0 + (char) 0 + (char) 0 + (char) 0 + (char) 0
			+ (char) 0 + (char) 0 + (char) 0 + (char) 0 + (char) 0 + (char) 0
			+ (char) 0 + (char) 0 + (char) 0 + (char) 0 + (char) 0 + (char) 0
			+ (char) 0 + (char) 0 + (char) 0 + (char) 0 + (char) 0 + (char) 0
			+ (char) 0 + (char) 0 + (char) 0 + (char) 0 + (char) 0 + (char) 0
			+ (char) 0 + (char) 0 + (char) 2 + (char) 0 + (char) 0 + (char) 0
			+ (char) 0 + (char) 0 + (char) 0 + (char) 0 + (char) 0 + (char) 0
			+ (char) 0 + (char) 0 + (char) 0 + (char) 0 + (char) 0 + (char) 0
			+ (char) 0 + (char) 0 + (char) 0 + (char) 0 + (char) 0 + (char) 0
			+ (char) 0 + (char) 0 + (char) 0 + (char) 0 + (char) 0 + (char) 0
			+ (char) 0 + (char) 0 + (char) 0 + (char) 0 + (char) 0 + (char) 0
			+ (char) 0 + (char) 0 + (char) 0 + (char) 0 + (char) 0 + (char) 0
			+ (char) 0 + (char) 0 + (char) 0 + (char) 0 + (char) 0 + (char) 0
			+ (char) 0 + (char) 0 + (char) 0 + (char) 0 + (char) 0 + (char) 0
			+ (char) 0 + (char) 0 + (char) 0 + (char) 0 + (char) 0 + (char) 0
			+ (char) 0 + (char) 0 + (char) 0 + (char) 0 + (char) 0 + (char) 0
			+ (char) 0 + (char) 0 + (char) 0 + (char) 0 + (char) 0 + (char) 0
			+ (char) 0 + (char) 0 + (char) 0 + (char) 0 + (char) 0 + (char) 0
			+ (char) 0 + (char) 0 + (char) 0 + (char) 0 + (char) 0 + (char) 0
			+ (char) 0 + (char) 0 + (char) -1 + (char) 0 + (char) 0 + (char) 0
			+ (char) 0 + (char) 0 + (char) 0 + (char) 0 + (char) 0 + (char) 0
			+ (char) 0 + (char) 0 + (char) 0 + (char) 0 + (char) 0 + (char) 0
			+ (char) 0 + (char) 0 + (char) 0 + (char) 0 + (char) 0 + (char) 0
			+ (char) 0 + (char) 0 + (char) 0 + (char) 0 + (char) 0 + (char) 0
			+ (char) 0 + (char) 0 + (char) 0 + (char) 0 + (char) 0 + (char) 0
			+ (char) 0 + (char) 0 + (char) 0 + (char) 0 + (char) 0 + (char) 0
			+ (char) 0 + (char) 0 + (char) 0 + (char) 0 + (char) 0 + (char) 0
			+ (char) 0 + (char) 0 + (char) 0 + (char) 0 + (char) 0 + (char) 0
			+ (char) 0 + (char) 0 + (char) 0 + (char) 0 + (char) 0 + (char) 0
			+ (char) 0 + (char) 0 + (char) 0 + (char) 0 + (char) 0 + (char) 0
			+ (char) 0 + (char) 0 + (char) 0 + (char) 0 + (char) 0 + (char) 0
			+ (char) 0 + (char) 0 + (char) 0 + (char) 0 + (char) 0 + (char) 0
			+ (char) 0 + (char) 0 + (char) 0 + (char) 0 + (char) 0 + (char) 0
			+ (char) 0 + (char) 0 + (char) 0 + (char) 0 + (char) -2 + (char) -4
			+ (char) -6 + (char) -8 + (char) -8 + (char) -8 + (char) -8
			+ (char) -8 + (char) -8 + (char) -8 + (char) -8 + (char) -8
			+ (char) -8 + (char) -8 + (char) -8 + (char) -8 + (char) -8
			+ (char) -8 + (char) -8 + (char) -8 + (char) -8 + (char) -8
			+ (char) -8 + (char) -8 + (char) -8 + (char) -8 + (char) -8
			+ (char) -8 + (char) -8 + (char) -8 + (char) -8 + (char) -8
			+ (char) -8 + (char) -8 + (char) -8 + (char) -8 + (char) -8
			+ (char) -8 + (char) -8 + (char) -8 + (char) -8 + (char) -8
			+ (char) -8 + (char) -7 + (char) -6 + (char) -5 + (char) -4
			+ (char) -3 + (char) -2 + (char) -1 + (char) 0 + (char) 2
			+ (char) 4 + (char) 6 + (char) 8 + (char) 8 + (char) 8 + (char) 8
			+ (char) 8 + (char) 8 + (char) 8 + (char) 8 + (char) 8 + (char) 8
			+ (char) 8 + (char) 8 + (char) 8 + (char) 8 + (char) 8 + (char) 8
			+ (char) 8 + (char) 8 + (char) 8 + (char) 8 + (char) 8 + (char) 8
			+ (char) 8 + (char) 8 + (char) 8 + (char) 8 + (char) 8 + (char) 8
			+ (char) 8 + (char) 8 + (char) 8 + (char) 8 + (char) 8 + (char) 8
			+ (char) 8 + (char) 8 + (char) 8 + (char) 8 + (char) 8 + (char) 8
			+ (char) 8 + (char) 7 + (char) 6 + (char) 5 + (char) 4 + (char) 3
			+ (char) 2 + (char) 1 + (char) 0;
}