User:Exauge/snippets
Contents
About
These are just a few code snippets to help you get a robot up and going nice and fast :)
Movement
Oscillator 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 larget bots (minis and megas). I'll start with The Bare Minimum *Note the bare minimum is not an oscillator movement*:
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 and oscillation:
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); setTurnRightRadians((Math.cos(e.getBearingRadians()))); } } }
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); setTurnRightRadians((Math.cos(e.getBearingRadians()))); } if (mxMv < 30) { setBack(75 * backDir); } } }
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 { static double eEner; public void onHitWall(HitWallEvent event){ backDir = backDir * -1; } public void onHitByBullet(HitByBulletEvent e) { randHit = Math.random() + .5; } 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); setTurnRightRadians((Math.cos(e.getBearingRadians()))); } if (mxMv < 30) { setBack(75 * backDir); } } }
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.
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.