package bayen; import robocode.*; import java.awt.geom.Point2D; import robocode.util.Utils; /** * UbaNano - a robot by Bayen */ public class UbaNano extends AdvancedRobot { static double thenEnergy; static double nowEnergy; static double FORWARD = 1; /** * CrazyTracker2's run method */ public void run() { thenEnergy=100; nowEnergy=100; setAdjustGunForRobotTurn(true);//allow gun and radar setAdjustRadarForGunTurn(true);//free movement while (true) { turnRadarRight(360);//scan for enemies } } /** * onScannedRobot: We have a target. Go get it. */ public void onScannedRobot(ScannedRobotEvent e) { thenEnergy = nowEnergy; nowEnergy = e.getEnergy(); setTurnRight(e.getBearing() + 90); if(thenEnergy>nowEnergy){ FORWARD = -FORWARD; setAhead(10*FORWARD); } //here I get my enemy's statistics so that I can use //linear targeting double bulletPower = 0.1; double myX = getX(); double myY = getY(); double absoluteBearing = getHeadingRadians() + e.getBearingRadians(); double enemyX = getX() + e.getDistance() * Math.sin(absoluteBearing); double enemyY = getY() + e.getDistance() * Math.cos(absoluteBearing); double enemyHeading = getRadarHeadingRadians() + 3.141592653589793; double enemyVelocity = 20 - 3*(thenEnergy-nowEnergy); //now I do my calculations for linear targeting so that //I know where my enemy will be once my bullet gets there double deltaTime = 0; double battleFieldHeight= getBattleFieldHeight(),battleFieldWidth= getBattleFieldWidth(); double predictedX = enemyX, predictedY = enemyY; while((++deltaTime)*(20.0 - 3.0*bulletPower) <Point2D.Double.distance(myX, myY, predictedX, predictedY)){ predictedX += Math.sin(enemyHeading) * enemyVelocity; predictedY += Math.cos(enemyHeading) * enemyVelocity; if( predictedX < 18.0 || predictedY < 18.0 || predictedX > battleFieldWidth - 18.0 || predictedY > battleFieldHeight - 18.0){ predictedX = Math.min(Math.max(18.0, predictedX), battleFieldWidth - 18.0); predictedY = Math.min(Math.max(18.0, predictedY), battleFieldHeight - 18.0); bulletPower=0; break;}} double theta = Utils.normalAbsoluteAngle(Math.atan2(predictedX - getX(), predictedY - getY())); //now I can turn my gun... setTurnRadarRightRadians (Utils.normalRelativeAngle(absoluteBearing - getRadarHeadingRadians())); setTurnGunRightRadians(Utils.normalRelativeAngle(theta - getGunHeadingRadians())); //...and fire! if(nowEnergy < thenEnergy) fire(bulletPower); } public void onBulletHitBullet(BulletHitBulletEvent e){ out.println("WORKED"); } }
Have you read BulletShielding? -- Tango