User:Exauge/snippets
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, 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).
public class YOURROBOTNAME extends AdvancedRobot { static double eEner; public void onScannedRobot(ScannedRobotEvent e) { double mxMv = Math.min(Math.min((getBattleFieldWidth()-getX()), (getBattleFieldHeight()-getY())), Math.min(getX(), getY())); double moveAmount = Math.max(getBattleFieldWidth(), getBattleFieldHeight()); if(eEner > (eEner = e.getEnergy())) { setMaxVelocity(16*Math.random()+5); setTurnRight((Math.random()*360)-180); setAhead(((Math.random()*moveAmount)-(moveAmount*.5))*1.2*mxMv); } }
Pattern Matching Gun
This is my 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.