Difference between revisions of "Ocnirp/Source Code"

From Robowiki
Jump to navigation Jump to search
m (update the code to be more readable and with more comment)
m (Using <syntaxhighlight>.)
 
(One intermediate revision by one other user not shown)
Line 6: Line 6:
 
}}
 
}}
 
----
 
----
<pre>
+
<syntaxhighlight>
 
package nat.nano;
 
package nat.nano;
  
Line 22: Line 22:
 
  * @author Nat Pavasant
 
  * @author Nat Pavasant
 
  */
 
  */
 +
public final class Ocnirp extends AdvancedRobot {
 +
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.61
 +
* @author Nat Pavasant
 +
*/
 +
@SuppressWarnings("unused")
 
public final class Ocnirp extends AdvancedRobot {
 
public final class Ocnirp extends AdvancedRobot {
 
/**
 
/**
Line 27: Line 44:
 
* turn in unacceptable rate. 30 is a standard value.
 
* turn in unacceptable rate. 30 is a standard value.
 
*/
 
*/
private static final int PATTERN_DEPTH = 45;
+
private static final int PATTERN_DEPTH = 30;
 
 
 
/**
 
/**
 
* If Math.random() exceed this number, the robot will change the direction.
 
* If Math.random() exceed this number, the robot will change the direction.
 
*/
 
*/
private static final double CHANGE_INTERVAL = 0.9454623d;
+
private static final double CHANGE_INTERVAL = 0.92;
 
 
 
/**
 
/**
 
* Preferred distance between this robot and enemy.
 
* Preferred distance between this robot and enemy.
 
*/
 
*/
private static final double PREFERRED_DISTANCE = 160d;
+
private static final double PREFERRED_DISTANCE = 180d;
 
 
 
/**
 
/**
Line 55: Line 72:
 
*/
 
*/
 
private static final double DISTANCE_OFFSET = 0.38435d;
 
private static final double DISTANCE_OFFSET = 0.38435d;
 +
 +
/**
 +
* Distance offset to turn
 +
*/
 +
private static final double DISTANCE_CONTROLLER = 237.5;
 
 
 
/**
 
/**
Line 81: Line 103:
 
* score.
 
* score.
 
*/
 
*/
// setAdjustGunForRobotTurn(true);
+
setAdjustGunForRobotTurn(true);
 
// setAdjustRadarForGunTurn(true);
 
// setAdjustRadarForGunTurn(true);
 
turnRadarRightRadians(Double.POSITIVE_INFINITY);
 
turnRadarRightRadians(Double.POSITIVE_INFINITY);
Line 116: Line 138:
 
//
 
//
 
// Do infinity lock
 
// Do infinity lock
//
 
 
setTurnRadarLeftRadians(getRadarTurnRemaining());
 
setTurnRadarLeftRadians(getRadarTurnRemaining());
//
 
 
// ///////////////////////////////////////////////////////////////////////////////
 
// ///////////////////////////////////////////////////////////////////////////////
  
Line 127: Line 147:
 
// control the distance
 
// control the distance
 
//
 
//
setTurnRightRadians(
+
// setTurnRightRadians(
Math.cos(angle = e.getBearingRadians())
+
// Math.cos(angle = e.getBearingRadians())
- Math.signum(
+
// - Math.signum(
getVelocity() * ((dist = e.getDistance()) - PREFERRED_DISTANCE)
+
// getVelocity() * ((dist = e.getDistance()) - PREFERRED_DISTANCE)
) * DISTANCE_OFFSET);
+
// ) * DISTANCE_OFFSET);
 +
// setTurnRightRadians(Math.cos(
 +
// (angle = e.getBearingRadians())
 +
// - ((dist = e.getDistance()) - PREFERRED_DISTANCE)
 +
// * getVelocity() / DISTANCE_CONTROLLER
 +
// ));
 +
// No distance controller
 +
setTurnRightRadians(Math.cos(angle = e.getBearingRadians()));
 
//
 
//
 
// Move ahead
 
// Move ahead
Line 142: Line 169:
 
//
 
//
 
// Reverse direction
 
// Reverse direction
 +
// Optimize to
 
//
 
//
 
onHitWall(null);
 
onHitWall(null);
 
//
 
//
 
// Velocity trick
 
// Velocity trick
//
+
//setMaxVelocity(Math.random() * VELOCITY_MULTIPILER + 1);
setMaxVelocity(Math.random() * VELOCITY_MULTIPILER + 1);
 
//
 
// End if
 
 
//
 
//
 
}
 
}
Line 165: Line 190:
 
//
 
//
 
enemyHistory = String.valueOf(
 
enemyHistory = String.valueOf(
(char) /* Math.round */(/* 2 * */(e.getVelocity() * Math.sin(e
+
(char) Math.round( 2 * (e.getVelocity() * Math.sin(e
 
.getHeadingRadians()
 
.getHeadingRadians()
 
- (angle += getHeadingRadians()))))).concat(
 
- (angle += getHeadingRadians()))))).concat(
Line 173: Line 198:
 
//
 
//
 
while ((matchPos = enemyHistory.indexOf(enemyHistory.substring(0,
 
while ((matchPos = enemyHistory.indexOf(enemyHistory.substring(0,
matchLen--), i = BFT)) < 0)
+
matchLen--), i = (int) (((dist = e.getDistance()) / 12.5D) - 1))) < 0)
 
;
 
;
 
//
 
//
Line 180: Line 205:
 
do
 
do
 
angle +=
 
angle +=
/* Math.asin */
+
Math.asin
 
((short) enemyHistory.charAt(--matchPos) / dist
 
((short) enemyHistory.charAt(--matchPos) / dist
/* 0.5 */);
+
  / 2d );
 
while (--i > 0);
 
while (--i > 0);
 
//
 
//
Line 192: Line 217:
 
// Fire
 
// Fire
 
//
 
//
 +
//if (dist < 100) setFire(3d);
 +
//else
 
setFire(FIRE_POWER);
 
setFire(FIRE_POWER);
 
//
 
//
Line 284: Line 311:
 
+ (char) 2 + (char) 1 + (char) 0;
 
+ (char) 2 + (char) 1 + (char) 0;
 
}
 
}
</pre>
+
 
 +
</syntaxhighlight>

Latest revision as of 09:30, 1 July 2010

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 {
	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.61
 * @author Nat Pavasant
 */
@SuppressWarnings("unused")
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 = 30;
	
	/**
	 * If Math.random() exceed this number, the robot will change the direction.
	 */
	private static final double CHANGE_INTERVAL = 0.92;
	
	/**
	 * Preferred distance between this robot and enemy.
	 */
	private static final double PREFERRED_DISTANCE = 180d;
	
	/**
	 * 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;
	
	/**
	 * Distance offset to turn
	 */
	private static final double DISTANCE_CONTROLLER = 237.5;
	
	/**
	 * 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);
//		setTurnRightRadians(Math.cos(
//						(angle = e.getBearingRadians())
//						- ((dist = e.getDistance()) - PREFERRED_DISTANCE)
//						* getVelocity() / DISTANCE_CONTROLLER
//		));
		// No distance controller
		setTurnRightRadians(Math.cos(angle = e.getBearingRadians()));
//
// Move ahead
//
		setAhead(dir);
//
// If random() exceed the change interval
//
		if (Math.random() > CHANGE_INTERVAL) {
//
// Reverse direction
// Optimize to
//
			onHitWall(null);
//			
// Velocity trick
			//setMaxVelocity(Math.random() * VELOCITY_MULTIPILER + 1);
//
		}
//
// ///////////////////////////////////////////////////////////////////////////////

// ///////////////////////////////////////////////////////////////////////////////
// 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 = (int) (((dist = e.getDistance()) / 12.5D) - 1))) < 0)
			;
//
// Get the firing angle
//
		do
			angle +=
				Math.asin
				((short) enemyHistory.charAt(--matchPos) / dist
				  / 2d );
		while (--i > 0);
//
// Turn the gun
//
		setTurnGunRightRadians(Utils.normalRelativeAngle(angle
				- getGunHeadingRadians()));
//
// Fire
//
		//if (dist < 100) setFire(3d);
		//else
		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;
}