/ HabrahabrTutorial + - / src - build.properties - build.xml
bin.dir = bin builds.dir = builds robocode.dir =; the path to the Robocode home directory, the default is C: \\ Robocode (I draw your attention that this is the java properties format and the slashes must be short-cut) robocode.jar = $ {robocode.dir} \\ libs \\ robocode.jar
<?xml version="1.0" encoding="UTF-8"?> <project name="HabrahabrTutorial" basedir="." default="release"> <property file="build.properties"/> <property name="robot.version" value="0.1"/> <property name="robot.package" value="ru.jdev.habrahabr"/> <property name="robot.path" value="ru/jdev/habrahabr"/> <property name="robot.name" value="HabrahabrTutorial"/> <path id="src.files"> <pathelement location="src"/> </path> <target name="init"> <mkdir dir="${bin.dir}"/> </target> <target name="compile" depends="init" description="Compiles source files"> <javac destdir="${bin.dir}" debug="on" debuglevel="lines,vars,source" optimize="yes" target="1.6"> <src refid="src.files"/> <classpath> <pathelement location="${robocode.jar}"/> </classpath> </javac> </target> <target name="clean" description="Deletes all previous build artifacts"> <delete dir="${bin.dir}"/> </target> <target name="release" depends="clean, compile"> <copy todir="${bin.dir}"> <fileset dir="src"/> </copy> <echo file="${bin.dir}/${robot.path}/${robot.name}.properties">robocode.version=1.7.3 robot.java.source.included=true robot.version=${robot.version} robot.author.name=Alexey jdev Zhidkov robot.classname=${robot.package}.${robot.name} robot.name=${robot.name} robot.description=Tutorial robot for habrahabr.ru </echo> <jar destfile="${builds.dir}\${robot.package}.${robot.name}_${robot.version}.jar" compress="true"> <fileset dir="${bin.dir}"/> </jar> <copy todir="${robocode.dir}\robots\"> <fileset file="${builds.dir}\${robot.package}.${robot.name}_${robot.version}.jar"/> </copy> <delete includeEmptyDirs="true"> <fileset dir="${bin.dir}" includes="**/*"/> </delete> </target> </project>
package ru.jdev.habrahabr; import robocode.AdvancedRobot; public class HabrahabrTutorial extends AdvancedRobot { @Override public void run() { while (true) { /** * , * */ execute(); } } }
package ru.jdev.habrahabr; import robocode.AdvancedRobot; import robocode.DeathEvent; import robocode.ScannedRobotEvent; import robocode.util.Utils; import java.awt.*; import java.awt.geom.Point2D; import static java.lang.Math.signum; import static java.lang.Math.toRadians; public class HabrahabrTutorial extends AdvancedRobot { /*+*/private static final double RADIANS_5 = toRadians(5); /*+*/private boolean isAlive = true; /*+*/private double enemyX = -1; /*+*/private double enemyY = -1; @Override public void run() { /*+*/setTurnRadarRightRadians(Double.POSITIVE_INFINITY); // /*~*/while (isAlive) { // true, /*+*/if (enemyX > -1) { // /*+*/final double radarTurn = getRadarTurn(); /*+*/setTurnRadarRightRadians(radarTurn); /*+*/} /** * , * */ execute(); } } /*+*/private double getRadarTurn() { // // : final double alphaToEnemy = angleTo(getX(), getY(), enemyX, enemyY); // , , (Utils, Robocode ): final double sign = (alphaToEnemy != getRadarHeadingRadians()) ? signum(Utils.normalRelativeAngle(alphaToEnemy - getRadarHeadingRadians())) : 1; // 5 return Utils.normalRelativeAngle(alphaToEnemy - getRadarHeadingRadians() + RADIANS_5 * sign); // , setTurnRadarRightRadians, // } @Override /*+*/public void onScannedRobot(ScannedRobotEvent event) { /** ScannedRobotEvent , , , * , ( -, ) */ // final double alphaToEnemy = getHeadingRadians() + event.getBearingRadians(); // enemyX = getX() + Math.sin(alphaToEnemy) * event.getDistance(); enemyY = getY() + Math.cos(alphaToEnemy) * event.getDistance(); } @Override /*+*/public void onDeath(DeathEvent event) { isAlive = false; } @Override /*+*/public void onPaint(Graphics2D g) { // , // , // Paint if (enemyX > -1) { g.setColor(Color.WHITE); g.drawRect((int) (enemyX - getWidth() / 2), (int) (enemyY - getHeight() / 2), (int) getWidth(), (int) getHeight()); } } /** * Robocode - 0 : * 90 - , 180 - , 270 - , 360 - . * <p/> * - . * , , , , */ /*+*/private static double angleTo(double baseX, double baseY, double x, double y) { double theta = Math.asin((y - baseY) / Point2D.distance(x, y, baseX, baseY)) - Math.PI / 2; if (x >= baseX && theta < 0) { theta = -theta; } return (theta %= Math.PI * 2) >= 0 ? theta : (theta + Math.PI * 2); } }
private double getDistance() { // return 200 - 400 * random(); } private double getBodyTurn() { // final double alphaToMe = angleTo(enemyX, enemyY, getX(), getY()); // ( , ) ... final double lateralDirection = signum((getVelocity() != 0 ? getVelocity() : 1) * Math.sin(Utils.normalRelativeAngle(getHeadingRadians() - alphaToMe))); // final double desiredHeading = Utils.normalAbsoluteAngle(alphaToMe + Math.PI / 2 * lateralDirection); // final double normalHeading = getVelocity() >= 0 ? getHeadingRadians() : Utils.normalAbsoluteAngle(getHeadingRadians() + Math.PI); // return Utils.normalRelativeAngle(desiredHeading - normalHeading); }
setTurnRadarRightRadians(radarTurn); /*+*/ final double bodyTurn = getBodyTurn(); /*+*/ setTurnRightRadians(bodyTurn); /*+*/ /*+*/ if (getDistanceRemaining() == 0) { /*+*/ final double distance = getDistance(); /*+*/ setAhead(distance); /*+*/ } }
private double getGunTurn() { // : , : return Utils.normalRelativeAngle(angleTo(getX(), getY(), enemyX, enemyY) - getGunHeadingRadians()); }
setAhead(distance); } /*+*/ final double gunTurn = getGunTurn(); /*+*/ setTurnGunRightRadians(gunTurn); /*+*/ setFire(2); }
Source: https://habr.com/ru/post/147947/