User:Exauge/snippets

From Robowiki
Jump to navigation Jump to search

About

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

Random Movement

This is my strong (at least many small bots have a hard time hitting it), but small and easy to implement random 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 larget bots (minis and megas). I'll start with The Bare Minimum:

public class YOURROBOTNAME extends AdvancedRobot {

static double eEner;

public void onScannedRobot(ScannedRobotEvent e) {
	if(eEner > (eEner = e.getEnergy())) {
		setTurnRight((Math.random()*360)-180);
		setAhead(((Math.random()*700)-(350))*1.2);
	}
}
}

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);
		setAhead(((Math.random()*700)-(350))*1.2);
	}
}
}

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;

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);
		setAhead(((Math.random()*700)-(350))*1.2);
	}
	if (mxMv < 30) {
		setBack(75 * backDir);
	}
}
}

Now for the entire thing. Any of these can be used but obviously the fewer features the smaller the code size.

public class YOURROBOTNAME extends AdvancedRobot {

static double eEner;

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()));
	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);
		setAhead(((Math.random()*mvAvg)-(mvAvg*.5))*.8*randHit);
	}
	if (mxMv < 30) {
		setBack(75 * backDir);
	}
}
}

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);
	}
}

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.