Difference between revisions of "NP/Source Code"

From Robowiki
< NP
Jump to navigation Jump to search
m
m (Using <syntaxhighlight>.)
 
(5 intermediate revisions by one other user not shown)
Line 6: Line 6:
 
}}
 
}}
  
Below is the code for more shrank prototype2. Codesize: 532 bytes. 217 bytes to play with.
+
== Code For NP 1.3 ==
 +
[[Code size]]: 745 bytes
  
<pre>
+
<syntaxhighlight>
 
package nat.micro;
 
package nat.micro;
  
Line 20: Line 21:
  
 
import java.awt.geom.Point2D;
 
import java.awt.geom.Point2D;
 +
import java.awt.geom.Rectangle2D;
  
 
/**
 
/**
Line 41: Line 43:
 
static final int BINS = 31; // 31 bins is fine for non-smoothed array.
 
static final int BINS = 31; // 31 bins is fine for non-smoothed array.
 
static final int MIDDLE_BIN = 15; // middle gin
 
static final int MIDDLE_BIN = 15; // middle gin
static final double MEA = 0.694498266 / MIDDLE_BIN; // hard-coded for 2.5 bullet power
+
static final double MEA = 0.694498266 / MIDDLE_BIN; // hard-coded for 2.5
static final double MAX_PASSED_DISTANCE = 12.5 / 2;
+
// bullet power
 
 
 
static double _enemyX; // enemy location
 
static double _enemyX; // enemy location
 
static double _enemyY; // enemy location
 
static double _enemyY; // enemy location
 
static int _enemyDirection = 1;
 
static int _enemyDirection = 1;
 +
 +
static double distance;
 
 
 
// Segment: Lateral Velocity
 
// Segment: Lateral Velocity
static int[][] _gunStats = new int[6][BINS];
+
// static int[][][] _gunStats = new int[6][4][BINS];
 +
static int[][][][] _gunStats = new int[6][4][2][BINS];
 +
// static int[][][][][] _gunStats = new int[6][4][2][3][BINS];
 
 
 
// Gun
 
// Gun
Line 58: Line 64:
 
 
 
// Distance controller constant
 
// Distance controller constant
static final double PREFERRED_DISTANCE = 150d;
+
static final double PREFERRED_DISTANCE = 180d;
static final double CLOSING_FACTOR = 7500d;
+
static final double CLOSING_FACTOR = 10000d;
 
 
static double _moveFactor = 20; // length to move, story the direction as
+
static double _moveFactor = 20; // length to move, store the direction as
 
// sign
 
// sign
 
static double _enemyEnergy; // keep track of enemy energy
 
static double _enemyEnergy; // keep track of enemy energy
 
static int _currentVelocity;
 
static int _currentVelocity;
 
static int _myLateralVelocity;
 
static int _myLateralVelocity;
static int[] _surfStats = new int[9];
+
static int _myLatVel, _distance;
 +
static int[][][] _surfStats = new int[4][5][9];
 
 
 
public void run() {
 
public void run() {
 
setAdjustRadarForGunTurn(true);
 
setAdjustRadarForGunTurn(true);
setAdjustGunForRobotTurn(true);
+
// setAdjustGunForRobotTurn(true); // not enough codesize
 
 
 
do {
 
do {
Line 80: Line 87:
 
Wave wave;
 
Wave wave;
 
double enemyAbsoluteBearing;
 
double enemyAbsoluteBearing;
double distance;
+
int mostVisited = MIDDLE_BIN, i = BINS;
 
 
 
// /////////////////////////////////////
 
// /////////////////////////////////////
 
// Wave (Velocity) Surfing movement
 
// Wave (Velocity) Surfing movement
// Base loosly on WaveSnake, by Kev
+
// Base loosely on WaveSnake, by Kev
 
//
 
//
 
 
 
// Turn Perpendicular with distance controller
 
// Turn Perpendicular with distance controller
setTurnRightRadians(Math.cos((enemyAbsoluteBearing = e
+
setTurnRightRadians(Math
.getBearingRadians())
+
.cos((enemyAbsoluteBearing = e.getBearingRadians())
- ((distance = e.getDistance()) - PREFERRED_DISTANCE)
+
- ((NP.distance = e.getDistance()) - PREFERRED_DISTANCE)
* _moveFactor / CLOSING_FACTOR));
+
* _moveFactor / CLOSING_FACTOR));
 
 
 
if (_enemyEnergy > (_enemyEnergy = e.getEnergy())) {
 
if (_enemyEnergy > (_enemyEnergy = e.getEnergy())) {
 
int testVel = 8;
 
int testVel = 8;
 
do {
 
do {
if (_surfStats[testVel] < _surfStats[_currentVelocity]) {
+
if (_surfStats[_distance][_myLatVel][testVel] < _surfStats[_distance][_myLatVel][_currentVelocity]) {
 
_currentVelocity = testVel;
 
_currentVelocity = testVel;
 
}
 
}
+
if (testVel == 5)
// Prevent robot to move at velocity 0 and 2
+
testVel = 3;
if (testVel == 6)
 
testVel = 3;
 
 
} while (testVel-- > 0);
 
} while (testVel-- > 0);
 
 
Line 116: Line 121:
 
// /////////////////////////////////////
 
// /////////////////////////////////////
 
// GuessFactor targeting gun
 
// GuessFactor targeting gun
// Base loosly on Falcon gun.
+
// Base loosely on Falcon gun.
 
//
 
//
 
addCustomEvent(wave = new Wave());
 
addCustomEvent(wave = new Wave());
Line 132: Line 137:
 
int direction;
 
int direction;
 
if ((direction = (int) Math.signum(enemyLatVel = e.getVelocity()
 
if ((direction = (int) Math.signum(enemyLatVel = e.getVelocity()
* Math.sin(e.getHeadingRadians() - enemyAbsoluteBearing))) != 0)
+
* Math.sin(e.getHeadingRadians() - (wave.wBearing = enemyAbsoluteBearing)))) != 0)
 
_enemyDirection = direction;
 
_enemyDirection = direction;
 
 
// Radar - add here due the codesize shrinking
+
// Segment: lateralVelocity and forwardWall
setTurnRadarRightRadians(Utils
+
// wave.stats = _gunStats[(Math.abs(enemyLatVel) >= 7.75) ? 4 : (int)
.normalRelativeAngle((wave.wBearing = enemyAbsoluteBearing)
+
// (1.4427 * Math
- getRadarHeadingRadians()) * 2);
+
// .log(Math.abs(enemyLatVel) + 1.75))][gunWallDistance(0.18247367367) ?
+
// (gunWallDistance(0.36494734735) ? (gunWallDistance(0.63865785787) ? 3
// Segment: lateralVelocity
+
// : 2) : 1) : 0];
 
wave.stats = _gunStats[(Math.abs(enemyLatVel) >= 7.75) ? 4 : (int) (1.4427 * Math
 
wave.stats = _gunStats[(Math.abs(enemyLatVel) >= 7.75) ? 4 : (int) (1.4427 * Math
.log(Math.abs(enemyLatVel) + 1.75))];
+
.log(Math.abs(enemyLatVel) + 1.75))][gunWallDistance(
+
0.18247367367, enemyAbsoluteBearing) ? (gunWallDistance(
int mostVisited = MIDDLE_BIN; // default at head-on
+
0.36494734735, enemyAbsoluteBearing) ? (gunWallDistance(
int i = BINS;
+
0.63865785787, enemyAbsoluteBearing) ? 3 : 2) : 1) : 0][gunWallDistance(
 +
-0.36494734735, enemyAbsoluteBearing) ? 0 : 1];
 
 
 
do {
 
do {
Line 155: Line 161:
 
setTurnGunRightRadians(Utils.normalRelativeAngle(enemyAbsoluteBearing
 
setTurnGunRightRadians(Utils.normalRelativeAngle(enemyAbsoluteBearing
 
- getGunHeadingRadians()
 
- getGunHeadingRadians()
+ (wave.wBearingDirection = MEA * _enemyDirection)
+
+ (wave.wBearingDirection = (MEA * _enemyDirection))
 
* (mostVisited - MIDDLE_BIN)));
 
* (mostVisited - MIDDLE_BIN)));
setFire(BULLET_POWER);
+
wave.weight = setFireBullet(BULLET_POWER) != null ? 4 : 1;
 
// /////////////////////////////////////////
 
// /////////////////////////////////////////
 +
 +
_distance = (int) (Math.min((distance + 50) / 200, 3));
 +
_myLatVel = (int) ((Math.abs(getVelocity()
 +
* Math.sin(e.getBearingRadians())) + 1) / 2);
 +
 +
setTurnRadarRightRadians(Utils
 +
.normalRelativeAngle(enemyAbsoluteBearing
 +
- getRadarHeadingRadians()) * 2);
 
}
 
}
 
 
Line 164: Line 178:
 
double wGunX;
 
double wGunX;
 
double wGunY;
 
double wGunY;
// Point2D.Double gunLocation;
 
 
double wBearing;
 
double wBearing;
 
double wBearingDirection;
 
double wBearingDirection;
 
double wDistance;
 
double wDistance;
+
int weight;
 
int[] stats;
 
int[] stats;
 
 
 
public boolean test() {
 
public boolean test() {
 
if (Math.abs((wDistance += BULLET_VELOCITY)
 
if (Math.abs((wDistance += BULLET_VELOCITY)
- Point2D.distance(wGunX, wGunY, _enemyX, _enemyY)) <= MAX_PASSED_DISTANCE) {
+
- Point2D.distance(wGunX, wGunY, _enemyX, _enemyY)) <= BULLET_VELOCITY / 2) {
 +
 
stats[(int) ((Utils.normalRelativeAngle(Math.atan2(_enemyX
 
stats[(int) ((Utils.normalRelativeAngle(Math.atan2(_enemyX
- wGunY, _enemyY - wGunX)
+
- wGunX, _enemyY - wGunY)
 
- wBearing)) / wBearingDirection)
 
- wBearing)) / wBearingDirection)
+ MIDDLE_BIN]++;
+
+ MIDDLE_BIN] += weight;
 
// removeCustomEvent(this);
 
// removeCustomEvent(this);
 
}
 
}
Line 190: Line 204:
 
 
 
public void onHitByBullet(HitByBulletEvent e) {
 
public void onHitByBullet(HitByBulletEvent e) {
_surfStats[_currentVelocity]++;
+
_surfStats[_distance][_myLatVel][_currentVelocity]++;
 
}
 
}
 
 
 
public void onHitWall(HitWallEvent e) {
 
public void onHitWall(HitWallEvent e) {
 
_moveFactor = -_moveFactor;
 
_moveFactor = -_moveFactor;
 +
}
 +
 +
// inject the absBearing here is good since it on the register, but inject
 +
// the distance isn't good since it out of register now.
 +
private static boolean gunWallDistance(double wallDistance, double ab) {
 +
double angle;
 +
return (new Rectangle2D.Double(18, 18, 764, 564))
 +
.contains(new Point2D.Double(_enemyX
 +
+ Math.sin(angle = ab
 +
+ (_enemyDirection * wallDistance)) * distance,
 +
_enemyY + Math.cos(angle) * distance));
 
}
 
}
 
}
 
}
</pre>
+
</syntaxhighlight>

Latest revision as of 09:29, 1 July 2010

NP Sub-pages:
NPVersion History - Source Code

Code For NP 1.3

Code size: 745 bytes

package nat.micro;

import robocode.AdvancedRobot;
import robocode.BulletHitEvent;
import robocode.Condition;
import robocode.HitByBulletEvent;
import robocode.HitWallEvent;
import robocode.ScannedRobotEvent;
import robocode.util.Utils;

import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;

/**
 * 
 * NP - Nat Pavasant
 * 
 * A micro-bot by Nat Pavasant; named after the author. This robot perform a
 * Wave Surfing-like movement called Velocity Surfing and fire using a very
 * simple GuessFactor targeting.
 * 
 * Gun base on Falcon, by PEZ and Kawigi; Movement base on WaveSnake, by Kev.
 */
public class NP extends AdvancedRobot {
	// //////////////////////////////////////////
	// Falcon's Gun
	
	static final double BULLET_POWER = 2.5; // 2.5 is the best by the way =)
	static final double BULLET_VELOCITY = 12.5; // bullet velocity for 2.5-size
	// bullet
	
	static final int BINS = 31; // 31 bins is fine for non-smoothed array.
	static final int MIDDLE_BIN = 15; // middle gin
	static final double MEA = 0.694498266 / MIDDLE_BIN; // hard-coded for 2.5
	// bullet power
	
	static double _enemyX; // enemy location
	static double _enemyY; // enemy location
	static int _enemyDirection = 1;
	
	static double distance;
	
	// Segment: Lateral Velocity
	// static int[][][] _gunStats = new int[6][4][BINS];
	static int[][][][] _gunStats = new int[6][4][2][BINS];
	// static int[][][][][] _gunStats = new int[6][4][2][3][BINS];
	
	// Gun
	// //////////////////////////////////////////
	
	// //////////////////////////////////////////
	// Movement
	
	// Distance controller constant
	static final double PREFERRED_DISTANCE = 180d;
	static final double CLOSING_FACTOR = 10000d;
	
	static double _moveFactor = 20; // length to move, store the direction as
	// sign
	static double _enemyEnergy; // keep track of enemy energy
	static int _currentVelocity;
	static int _myLateralVelocity;
	static int _myLatVel, _distance;
	static int[][][] _surfStats = new int[4][5][9];
	
	public void run() {
		setAdjustRadarForGunTurn(true);
//		setAdjustGunForRobotTurn(true); // not enough codesize
		
		do {
			turnRadarRightRadians(1);
		} while (true);
	}
	
	public void onScannedRobot(ScannedRobotEvent e) {
		Wave wave;
		double enemyAbsoluteBearing;
		int mostVisited = MIDDLE_BIN, i = BINS;
		
		// /////////////////////////////////////
		// Wave (Velocity) Surfing movement
		// Base loosely on WaveSnake, by Kev
		//
		
		// Turn Perpendicular with distance controller
		setTurnRightRadians(Math
				.cos((enemyAbsoluteBearing = e.getBearingRadians())
						- ((NP.distance = e.getDistance()) - PREFERRED_DISTANCE)
						* _moveFactor / CLOSING_FACTOR));
		
		if (_enemyEnergy > (_enemyEnergy = e.getEnergy())) {
			int testVel = 8;
			do {
				if (_surfStats[_distance][_myLatVel][testVel] < _surfStats[_distance][_myLatVel][_currentVelocity]) {
					_currentVelocity = testVel;
				}
				 if (testVel == 5)
				 testVel = 3;
			} while (testVel-- > 0);
			
			if (_currentVelocity < 4) {
				onHitWall(null);
			}
		}
		
		setMaxVelocity(2 * (_currentVelocity - 4));
		setAhead(_moveFactor);
		// /////////////////////////////////////
		
		// /////////////////////////////////////
		// GuessFactor targeting gun
		// Base loosely on Falcon gun.
		//
		addCustomEvent(wave = new Wave());
		
		_enemyX = (wave.wGunX = getX())
				+ Math.sin(enemyAbsoluteBearing += getHeadingRadians())
				* distance;
		_enemyY = (wave.wGunY = getY()) + Math.cos(enemyAbsoluteBearing)
				* distance;
		
		double enemyLatVel;
		
		// check for enemy movement direction, preserve old direction if current
		// velocity is 0
		int direction;
		if ((direction = (int) Math.signum(enemyLatVel = e.getVelocity()
				* Math.sin(e.getHeadingRadians() - (wave.wBearing = enemyAbsoluteBearing)))) != 0)
			_enemyDirection = direction;
		
		// Segment: lateralVelocity and forwardWall
		// wave.stats = _gunStats[(Math.abs(enemyLatVel) >= 7.75) ? 4 : (int)
		// (1.4427 * Math
		// .log(Math.abs(enemyLatVel) + 1.75))][gunWallDistance(0.18247367367) ?
		// (gunWallDistance(0.36494734735) ? (gunWallDistance(0.63865785787) ? 3
		// : 2) : 1) : 0];
		wave.stats = _gunStats[(Math.abs(enemyLatVel) >= 7.75) ? 4 : (int) (1.4427 * Math
				.log(Math.abs(enemyLatVel) + 1.75))][gunWallDistance(
				0.18247367367, enemyAbsoluteBearing) ? (gunWallDistance(
				0.36494734735, enemyAbsoluteBearing) ? (gunWallDistance(
				0.63865785787, enemyAbsoluteBearing) ? 3 : 2) : 1) : 0][gunWallDistance(
				-0.36494734735, enemyAbsoluteBearing) ? 0 : 1];
		
		do {
			i--;
			if (wave.stats[i] > wave.stats[mostVisited])
				mostVisited = i;
		} while (i > 0);
		
		setTurnGunRightRadians(Utils.normalRelativeAngle(enemyAbsoluteBearing
				- getGunHeadingRadians()
				+ (wave.wBearingDirection = (MEA * _enemyDirection))
				* (mostVisited - MIDDLE_BIN)));
		wave.weight = setFireBullet(BULLET_POWER) != null ? 4 : 1;
		// /////////////////////////////////////////
		
		_distance = (int) (Math.min((distance + 50) / 200, 3));
		_myLatVel = (int) ((Math.abs(getVelocity()
				* Math.sin(e.getBearingRadians())) + 1) / 2);

		setTurnRadarRightRadians(Utils
				.normalRelativeAngle(enemyAbsoluteBearing
						- getRadarHeadingRadians()) * 2);
	}
	
	static class Wave extends Condition {
		double wGunX;
		double wGunY;
		double wBearing;
		double wBearingDirection;
		double wDistance;
		int weight;
		int[] stats;
		
		public boolean test() {
			if (Math.abs((wDistance += BULLET_VELOCITY)
					- Point2D.distance(wGunX, wGunY, _enemyX, _enemyY)) <= BULLET_VELOCITY / 2) {
				
				stats[(int) ((Utils.normalRelativeAngle(Math.atan2(_enemyX
						- wGunX, _enemyY - wGunY)
						- wBearing)) / wBearingDirection)
						+ MIDDLE_BIN] += weight;
				// removeCustomEvent(this);
			}
			
			return false;
		}
	}
	
	public void onBulletHit(BulletHitEvent e) {
		_enemyEnergy -= 12.5;
	}
	
	public void onHitByBullet(HitByBulletEvent e) {
		_surfStats[_distance][_myLatVel][_currentVelocity]++;
	}
	
	public void onHitWall(HitWallEvent e) {
		_moveFactor = -_moveFactor;
	}
	
	// inject the absBearing here is good since it on the register, but inject
	// the distance isn't good since it out of register now.
	private static boolean gunWallDistance(double wallDistance, double ab) {
		double angle;
		return (new Rectangle2D.Double(18, 18, 764, 564))
				.contains(new Point2D.Double(_enemyX
						+ Math.sin(angle = ab
								+ (_enemyDirection * wallDistance)) * distance,
						_enemyY + Math.cos(angle) * distance));
	}
}