package ar.tests; import static java.lang.Math.*; import static ar.tests.Util.*; import java.awt.Color; import java.awt.geom.*; import robocode.*; public class SuperMyFirstRobot extends AdvancedRobot { private static final double WALL_STICK = 250.0; private Rectangle2D.Double fieldRectangle; private double angle; private int direction = 1; private double lastEnemyHeading; public void run() { setColors(null, null, Color.RED, Color.YELLOW, Color.RED); setAdjustGunForRobotTurn(true); setAdjustRadarForRobotTurn(true); setAdjustRadarForGunTurn(true); setTurnRadarRightRadians(Double.POSITIVE_INFINITY); fieldRectangle = new Rectangle2D.Double(ROBOT_CENTER, ROBOT_CENTER / 2, getBattleFieldWidth() - ROBOT_CENTER, getBattleFieldHeight() - ROBOT_CENTER); do { direction = 1; scan(); setBackAsFront(angle); for (int i = 0; i < 18; i++) { doNothing(); } direction = -1; scan(); setBackAsFront(angle); for (int i = 0; i < 18; i++) { doNothing(); } } while (true); } public void onScannedRobot(ScannedRobotEvent e) { double absoluteBearing = getHeadingRadians() + e.getBearingRadians(); // Radar lock. if (getOthers() == 1) { setTurnRadarLeftRadians(getRadarTurnRemainingRadians()); } // Perpendicular movement. angle = normalRelativeAngle(wallSmoothing(new Point2D.Double(getX(), getY()), absoluteBearing + PI / 2, direction)); // Circular targeting. double relativeX = e.getDistance() * Math.sin(absoluteBearing); double relativeY = e.getDistance() * Math.cos(absoluteBearing); double db = 0; double ww = lastEnemyHeading; // enemy's starting heading do { db += getBulletSpeed(3); double dx = e.getVelocity() * Math.sin(ww); double dy = e.getVelocity() * Math.cos(ww); ww += e.getHeadingRadians() - lastEnemyHeading; relativeX += dx; relativeY += dy; } while (db < Util.distance(0, 0, relativeX, relativeY)); lastEnemyHeading = e.getHeadingRadians(); setTurnGunRightRadians(Math.asin(Math.sin(Math.atan2(relativeX, relativeY) - getGunHeadingRadians()))); setFire(3); } private double wallSmoothing(Point2D.Double botLocation, double angle, int orientation) { while (!fieldRectangle.contains(project(botLocation, angle, WALL_STICK))) { angle += orientation * 0.1; } return angle; } private void setBackAsFront(double goAngle) { double angle = normalRelativeAngle(goAngle - getHeadingRadians()); if (abs(angle) > (PI / 2)) { if (angle < 0) { setTurnRightRadians(PI + angle); } else { setTurnLeftRadians(PI - angle); } setBack(100); } else { if (angle < 0) { setTurnLeftRadians(-angle); } else { setTurnRightRadians(angle); } setAhead(100); } } }