Difference between revisions of "User:Exauge/snippets"

From Robowiki
Jump to navigation Jump to search
m (small edit)
Line 2: Line 2:
 
These are just a few code snippets to help you get a robot up and going nice and fast :)
 
These are just a few code snippets to help you get a robot up and going nice and fast :)
 
== Movement ==
 
== Movement ==
=== Oscillator Movement ===
+
=== Oscillator / Random Movement ===
 
This is my strong (at least many small bots have a hard time hitting it), but small and easy to implement [[Oscillator Movement]]. It tries to stay away from walls, but isn't perfect so it can still collide sometimes. It works fairly well in smaller bots (nanos and micros) but a stronger movement will probably be needed for largest bots (minis and megas). My robot [[GateKeeper]] uses an extremely lightweight version of this movement. You can see it from its source.
 
This is my strong (at least many small bots have a hard time hitting it), but small and easy to implement [[Oscillator Movement]]. It tries to stay away from walls, but isn't perfect so it can still collide sometimes. It works fairly well in smaller bots (nanos and micros) but a stronger movement will probably be needed for largest bots (minis and megas). My robot [[GateKeeper]] uses an extremely lightweight version of this movement. You can see it from its source.
I'll start with '''The Bare Minimum''':
+
I'll start with '''The Bare Minimum''' as a [[Random Movement]] but you can use it as an [[Oscillator Movement]] as well:
 
<pre>
 
<pre>
 
public class YOURROBOTNAME extends AdvancedRobot {
 
public class YOURROBOTNAME extends AdvancedRobot {
Line 12: Line 12:
 
public void onScannedRobot(ScannedRobotEvent e) {
 
public void onScannedRobot(ScannedRobotEvent e) {
 
if(eEner > (eEner = e.getEnergy())) {
 
if(eEner > (eEner = e.getEnergy())) {
setTurnRight(Math.random()*180);
+
setTurnRight(Math.random()*360-180); // Delete this line if you use oscillation
 
setAhead(((Math.random()*700)-(350))*1.2);
 
setAhead(((Math.random()*700)-(350))*1.2);
 
}
 
}
 +
//setTurnRightRadians(Math.cos(absB = e.getBearingRadians())); // Uncomment this line for oscillation
 
}
 
}
 
}
 
}
Line 28: Line 29:
 
if(eEner > (eEner = e.getEnergy())) {
 
if(eEner > (eEner = e.getEnergy())) {
 
setMaxVelocity(16*Math.random()+5);
 
setMaxVelocity(16*Math.random()+5);
setTurnRight(Math.random()*180);
+
setTurnRight(Math.random()*360-180); // Delete this line if you use oscillation
 
setAhead(((Math.random()*700)-(350))*1.2);
 
setAhead(((Math.random()*700)-(350))*1.2);
 
}
 
}
 +
//setTurnRightRadians(Math.cos(absB = e.getBearingRadians())); // Uncomment this line for oscillation
 
}
 
}
 
}
 
}
Line 39: Line 41:
  
 
static double eEner;
 
static double eEner;
 +
int backDir = 1;
  
 
public void onHitWall(HitWallEvent event){
 
public void onHitWall(HitWallEvent event){
Line 48: Line 51:
 
if(eEner > (eEner = e.getEnergy())) {
 
if(eEner > (eEner = e.getEnergy())) {
 
setMaxVelocity(16*Math.random()+5);
 
setMaxVelocity(16*Math.random()+5);
setTurnRight(Math.random()*180);
+
setTurnRight(Math.random()*360-180); // Delete this line if you use oscillation
 
setAhead(((Math.random()*700)-(350))*1.2);
 
setAhead(((Math.random()*700)-(350))*1.2);
 
}
 
}
Line 54: Line 57:
 
setBack(75 * backDir);
 
setBack(75 * backDir);
 
}
 
}
 +
//setTurnRightRadians(Math.cos(absB = e.getBearingRadians())); // Uncomment this line for oscillation
 
}
 
}
 
}
 
}
Line 61: Line 65:
 
public class YOURROBOTNAME extends AdvancedRobot {
 
public class YOURROBOTNAME extends AdvancedRobot {
  
 +
int moveNeg = 1;
 +
double randInt = 1;
 +
double randHit = 1;
 
static double eEner;
 
static double eEner;
  
Line 68: Line 75:
  
 
public void onHitByBullet(HitByBulletEvent e) {
 
public void onHitByBullet(HitByBulletEvent e) {
randHit = Math.random() + .5;
+
moveNeg = moveNeg * -1;
 +
randInt = Math.random() + .5;
 +
randHit = randInt * randInt * moveNeg;
 
}
 
}
  
Line 77: Line 86:
 
if(eEner > (eEner = e.getEnergy())) {
 
if(eEner > (eEner = e.getEnergy())) {
 
setMaxVelocity(16*Math.random()+5);
 
setMaxVelocity(16*Math.random()+5);
setTurnRight(Math.random()*180);
+
setTurnRight(Math.random()*360-180); // Delete this line if you use oscillation
 
setAhead(((Math.random()*mvAvg)-(mvAvg*.5))*.8*randHit);
 
setAhead(((Math.random()*mvAvg)-(mvAvg*.5))*.8*randHit);
 
}
 
}
if (mxMv < 30) {
+
if (mxMv < 45) {
 
setBack(75 * backDir);
 
setBack(75 * backDir);
 
}
 
}
 +
//setTurnRightRadians(Math.cos(absB = e.getBearingRadians())); // Uncomment this line for oscillation
 
}
 
}
 
}
 
}

Revision as of 22:14, 6 May 2010

About

These are just a few code snippets to help you get a robot up and going nice and fast :)

Movement

Oscillator / Random Movement

This is my strong (at least many small bots have a hard time hitting it), but small and easy to implement Oscillator Movement. It tries to stay away from walls, but isn't perfect so it can still collide sometimes. It works fairly well in smaller bots (nanos and micros) but a stronger movement will probably be needed for largest bots (minis and megas). My robot GateKeeper uses an extremely lightweight version of this movement. You can see it from its source. I'll start with The Bare Minimum as a Random Movement but you can use it as an Oscillator Movement as well:

public class YOURROBOTNAME extends AdvancedRobot {

static double eEner;

public void onScannedRobot(ScannedRobotEvent e) {
	if(eEner > (eEner = e.getEnergy())) {
		setTurnRight(Math.random()*360-180); // Delete this line if you use oscillation
		setAhead(((Math.random()*700)-(350))*1.2);
	}
		//setTurnRightRadians(Math.cos(absB = e.getBearingRadians())); // Uncomment this line for oscillation
}
}

Next let's add a few more advanced features like changing velocity:

public class YOURROBOTNAME extends AdvancedRobot {

static double eEner;
double backDir = 1;

public void onScannedRobot(ScannedRobotEvent e) {
	if(eEner > (eEner = e.getEnergy())) {
		setMaxVelocity(16*Math.random()+5);
		setTurnRight(Math.random()*360-180); // Delete this line if you use oscillation
		setAhead(((Math.random()*700)-(350))*1.2);
	}
		//setTurnRightRadians(Math.cos(absB = e.getBearingRadians())); // Uncomment this line for oscillation
}
}

Unfortunately, something like the above two will probably bump into walls a lot so lets add a little wall avoidance:

public class YOURROBOTNAME extends AdvancedRobot {

static double eEner;
int backDir = 1;

public void onHitWall(HitWallEvent event){
	backDir = backDir * -1;
}

public void onScannedRobot(ScannedRobotEvent e) {
	double mxMv = Math.min(Math.min((getBattleFieldWidth()-getX()), (getBattleFieldHeight()-getY())), Math.min(getX(), getY()));
	if(eEner > (eEner = e.getEnergy())) {
		setMaxVelocity(16*Math.random()+5);
		setTurnRight(Math.random()*360-180); // Delete this line if you use oscillation
		setAhead(((Math.random()*700)-(350))*1.2);
	}
	if (mxMv < 30) {
		setBack(75 * backDir);
	}
		//setTurnRightRadians(Math.cos(absB = e.getBearingRadians())); // Uncomment this line for oscillation
}
}

Now for the entire thing with changed move amount on bullet hit.. Any of these can be used but obviously the fewer features the smaller the code size.

public class YOURROBOTNAME extends AdvancedRobot {

int moveNeg = 1;
double randInt = 1;
double randHit = 1;
static double eEner;

public void onHitWall(HitWallEvent event){
	backDir = backDir * -1;
}

public void onHitByBullet(HitByBulletEvent e) {
	moveNeg = moveNeg * -1;
	randInt = Math.random() + .5;
	randHit = randInt * randInt * moveNeg;
}

public void onScannedRobot(ScannedRobotEvent e) {
	double mxMv = Math.min(Math.min((getBattleFieldWidth()-getX()), (getBattleFieldHeight()-getY())), Math.min(getX(), getY()));
	double mvAmount = Math.max(getBattleFieldWidth(), getBattleFieldHeight());
	double mvAvg = (mxMv+mvAmount)/2;
	if(eEner > (eEner = e.getEnergy())) {
		setMaxVelocity(16*Math.random()+5);
		setTurnRight(Math.random()*360-180); // Delete this line if you use oscillation
		setAhead(((Math.random()*mvAvg)-(mvAvg*.5))*.8*randHit);
	}
	if (mxMv < 45) {
		setBack(75 * backDir);
	}
		//setTurnRightRadians(Math.cos(absB = e.getBearingRadians())); // Uncomment this line for oscillation
}
}

Targeting

Pattern Matching Gun

This is a pattern matching gun which is a slightly altered version of the one on Robar's Blackwidow. Again, it is a small pattern matcher intended for small bots. A more advanced code will probably work better with larger bots. If you look at the source code of almost every nano and micro, their pattern matcher will look almost exactly like this. That is because the lighter version of the pattern matchers were all for the most part based on FunkyChicken's pattern matcher.

public class YOURROBOTNAME extends AdvancedRobot {

static final double bPow = 2.2; // Bullet Power - change it to whatever you like.
static final double bVel = 20-3*bPow;
static final int patDep = 30;
static String eLog = "00000000000000000000000000888888";

public void onScannedRobot(ScannedRobotEvent e) {
	int i;
	double absB;
	int mLen = patDep;
	int indX;
	setTurnRightRadians((Math.cos(absB = e.getBearingRadians())));
	eLog = String.valueOf( (char)Math.round(e.getVelocity() * Math.sin(e.getHeadingRadians() - ( absB+=getHeadingRadians() )))).concat(eLog);
	while((indX = eLog.indexOf(eLog.substring(0, mLen--), (i = (int)((e.getDistance())/bVel)))) < 0);
	do{
		absB += Math.asin(((byte)eLog.charAt(indX--))/e.getDistance());
	}
	while(--i > 0);
	setTurnGunRightRadians(Utils.normalRelativeAngle(absB-getGunHeadingRadians()));
	setFire(bPow);
	}
}

Linear Targeting

Linear targeting is just a step ahead of Head-On Targeting. Most robots don't just stay in one point - they move. The idea behind linear targeting is that you can easily predict exactly where the enemy will be if they continue to move in a strait line at the same velocity. It is fairly effective against some robots with basic movement but usually isn't used outside the nanobot class and it is less-commonly used in the microbot class. The major advantage of Linear Targeting is that is has a small code size and it is better than Head-On Targeting. Think of linear targeting as a triangle with one vertex being your robot, another being the enemy robot, and the third being the point that the bullet will hit them. The angle at your robot's vertex will be the angle that your radar would need to turn if it were going to hit the enemy.

Lin targ.jpg

So how can that angle be found? It's quite simple.

public class YOURROBOTNAME extends AdvancedRobot {
	
	double bullPow = 2.4; // bullet power - change as you wish
	double bullVel = 20 - bullPow * 3; // bullet velocity
	
	public void onScannedRobot(ScannedRobotEvent e) {
		double absB = getHeadingRadians() + e.getBearingRadians(); // absolute bearing
		double redAng = e.getHeading() - getHeading(); // this is the measure of the vertex by the enemy's robot
		double blueAng = (180 - Math.abs(redAng) * (e.getVelocity() / bullVel)); // this is the measure of the vertex by your robot
		setTurnGunRightRadians(Utils.normalRelativeAngle(absB - getGunHeadingRadians() + blueAng)); // how much to turn the gun
		setFire(bullPow); // FIRE!
	}
}

Basically, this tells your robot to fire at the spot where the enemy will be when your bullet hits if it continues to move at the same heading and velocity. More thorough explanation / better code for it can be found in the Linear Targeting article.

Radar

Infinity Radar Lock

The infinity lock is very easy to implement and has a very small code size which is why I chose it for my first robot. For other radar locks see radar. **NOTE TO MAKE THE PATTERN MATCHER OR RANDOM MOVEMENT ABOVE WORK PROPERLY, YOU WILL NEED A RADAR LOCK OF SOME TYPE**

It's really quite simple to use. In your public void run, insert setTurnRadarRight(Double.POSITIVE_INFINITY); so that it will look something like this:

public void run() {
	setTurnRadarRight(Double.POSITIVE_INFINITY); // Radar Lock
}

Then at the end of your onscannedrobot, insert setTurnRadarLeft(getRadarTurnRemaining()); so that it will look something like this:

public void onScannedRobot(ScannedRobotEvent e) {
	// Your movement code... bla bla
	// Your gun code... bla bla
	setTurnRadarLeft(getRadarTurnRemaining());
}

And there you have it. You have just implemented a radar lock.