package myrobots; import robocode.*; import java.awt.*; /** * SuperTracker - a Super Sample Robot by CrazyBassoonist based on the robot Tracker by Mathew Nelson and maintained by Flemming N. Larsen * <p/> * Locks onto a robot, moves close, fires when close. */ public class SuperTracker extends AdvancedRobot { int moveDirection=1;//which way to move /** * run: Tracker's main run function */ public void run() { setAdjustRadarForRobotTurn(true);//keep the radar still while we turn setBodyColor(new Color(128, 128, 50)); setGunColor(new Color(50, 50, 20)); setRadarColor(new Color(200, 200, 70)); setScanColor(Color.white); setBulletColor(Color.blue); setAdjustGunForRobotTurn(true); // Keep the gun still when we turn turnRadarRightRadians(Double.POSITIVE_INFINITY);//keep turning radar right } /** * onScannedRobot: Here's the good stuff */ public void onScannedRobot(ScannedRobotEvent e) { double absBearing=e.getBearingRadians()+getHeadingRadians();//自分を基準とする見つけた敵の相対角度+自分のボディの向きを足すと敵の絶対角度 enemies absolute bearing double latVel=e.getVelocity() * Math.sin(e.getHeadingRadians() -absBearing);//enemies later velocity敵の移動速度*sin(敵のボディの向き-敵の絶対角度) double gunTurnAmt;//amount to turn our gun setTurnRadarLeftRadians(getRadarTurnRemainingRadians());//lock on the radar if(Math.random()>.9){ setMaxVelocity((12*Math.random())+12);//ランダムでスピード決める } if (e.getDistance() > 150) {//距離が150以上なら gunTurnAmt = robocode.util.Utils.normalRelativeAngle(absBearing- getGunHeadingRadians()+latVel/22);//amount to turn our gun, lead just a little bit setTurnGunRightRadians(gunTurnAmt); //turn our gun setTurnRightRadians(robocode.util.Utils.normalRelativeAngle(absBearing-getHeadingRadians()+latVel/getVelocity()));//drive towards the enemies predicted future location setAhead((e.getDistance() - 140)*moveDirection);//前出る setFire(3);//撃つ } else{//十分近いなら gunTurnAmt = robocode.util.Utils.normalRelativeAngle(absBearing- getGunHeadingRadians()+latVel/15);//amount to turn our gun, lead just a little bit setTurnGunRightRadians(gunTurnAmt);//turn our gun setTurnLeft(-90-e.getBearing()); //敵に垂直に向く setAhead((e.getDistance() - 140)*moveDirection);//前出る setFire(3);//撃つ } } public void onHitWall(HitWallEvent e){ moveDirection=-moveDirection;//向き反転 } /** * onWin: Do a victory dance */ public void onWin(WinEvent e) { for (int i = 0; i < 50; i++) { turnRight(30); turnLeft(30); } } }