Difference between revisions of "Robocode/FAQ"
< Robocode
Jump to navigation
Jump to search
(Adding more questions) |
|||
(40 intermediate revisions by 14 users not shown) | |||
Line 1: | Line 1: | ||
Frequently asked questions about Robocode. | Frequently asked questions about Robocode. | ||
− | == Installing and using == | + | == [[Robocode/Download|Installing]] and [[Robocode/Getting Started|using]] == |
+ | ; '''I can't install Robocode.''' | ||
+ | : Please see [[Robocode_Download_And_Install|these instructions]]. | ||
− | ; I | + | ; '''I get this error when trying to start Robocode: "'JAVA' is not recognized as an internal or external command, operable or batch file".''' |
− | : | + | : This is caused by an unknown path to your Java installation. Please follow [[Robocode/System_Requirements#PATH_must_be_set|these instructions]]. |
− | ; I | + | ; '''Can I play Robocode online?''' |
− | + | : Robocode is not an "online" game, so you can't, for example, share a battle with your friends in real time over the Internet. But you can upload your bots to places such as GitHub or Dropbox, and join any of the existing competitions such as [https://literumble.appspot.com/ RoboRumble] (enter [[RoboRumble/Enter The Competition|here]]), or organize one with your friends. | |
− | ; What | + | ;'''What is the difference between a set method like setAhead() and ahead() without the set-prefix?''' |
− | : | + | : The difference between a method like <code>ahead()</code>, and the method <code>setAhead()</code> is that set-methods are only available with the AdvancedRobot. The difference is that you can call multiple setters like e.g. <code>setAhead()</code>, <code>setTurnGunLeft()</code>, and <code>setFire()</code> in the same turn. These set commands will first take effect, i.e. execute, when you call the <code>execute()</code> method explicitly in a turn. Methods that are not setters like e.g. <code>ahead()</code>, <code>turnGunLeft()</code>, and <code>fire()</code> will execute independently and take one to many turns to complete, as one command will need to execute and complete before the next command is being executed. Setters are called in the same turn when <code>execute()</code> is called. So the method <code>ahead()</code> is actually the same as <code>setAhead()</code> + <code>execute()</code>, and in that order. The set methods can be seen as "fire and forget" methods that are executed immediately, whereas the methods like <code>ahead()</code> must first complete its movement, turning, firing etc. before the next command is being run. Hence those commands need to wait for the other commands to complete first before the method itself will be executed. |
− | ; | + | ; '''How do I get started?''' |
− | : Robocode | + | : See [[Robocode/Getting Started|Getting Started]] and [[Robocode/My First Robot|My First Robot]]. |
− | ; | + | ; '''What can robots do?''' |
− | : | + | : Many things. They can [[Movement|move]], turn, scan robots with their [[Radar]], aim their gun and [[Targeting|fire bullets]], and get information about the world around them. See the [http://robocode.sourceforge.net/docs/robocode/ Robocode API]. |
− | ; When I test my bots, Robocode is slow. Is there a way to execute the battles faster? | + | ; '''Should I download robots from the Internet?''' |
+ | : Yes. More robots makes your Robocode experience more fun. See [[Robocode/Downloading Robots|Downloading Robots]]. | ||
+ | |||
+ | ; '''I have downloaded a robot from the Web, but I don't know how to use it, because it doesn't appear anywhere.''' | ||
+ | : By default, robots are read from the <code>/robocode/robots</code> directory. You can select "Robot -> Import Downloaded Robot" to copy a robot JAR to this directory from another location. Also, you can configure Robocode to read robots from additional locations using the Properties dialog. | ||
+ | |||
+ | ; '''I have seen that many bots are packaged into ".jar" files. How do I package my bot?''' | ||
+ | : Select, "Robot -> Package robot for upload" from the menu, then enter your robot's details when prompted. See [[Robocode/Package Robot|Package Robot]]. | ||
+ | |||
+ | ; '''What do I have to do to see the source code of a robot?''' | ||
+ | : You can do two things. The first option is to open the Editor Window in Robocode and use the command in the File menu. The second option is to open the robot ".jar" file with a zip utility and find the source code there (assuming, of course, the robot is open source). See [[Robocode/Learning from Robots|Learning from Robots]]. | ||
+ | |||
+ | ;'''When I test my bots, Robocode is slow. Is there a way to execute the battles faster?''' | ||
: When you are testing your robot, you want to execute many battles in a short time. Minimize the Robocode main screen to make it execute the battles at full speed. | : When you are testing your robot, you want to execute many battles in a short time. Minimize the Robocode main screen to make it execute the battles at full speed. | ||
− | == [[Game physics]] == | + | ;'''What is with the star "*" next to my robot's name?''' |
+ | : The asterisk to the right of a robot's name indicates that the robot is a development version, not a [[Robocode/Package Robot|packaged version]]. | ||
+ | |||
+ | == [[Robocode/Game Physics|Game physics]] == | ||
+ | ; '''What are ticks, turns, and frames?''' | ||
+ | : A tick refers to one unit, which is also called a turn in Robocode. During one turn, you may perform one action as a Robot, or multiple (independent) actions as an AdvancedRobot. A frame is a unit of drawing to the Robocode client interface. If you are processing turns slowly, you will get one frame per tick / turn. However, if you up the turns per second beyond your computer's ability to render the frames, you will miss some frames of animation. This won't affect the robots' behavior, unless you foolishly added code in your <code>onPaint(Graphics2D)</code> method that alters your bots behavior. In that case, your bot will behave differently depending on whether or not the Paint button has been enabled, and if the framerate can keep up with the turnrate. | ||
+ | |||
+ | ; '''Can I fire bullets with power higher than 3.0 or lower than 1.0?''' | ||
+ | :No and yes. You can't fire bullets with power greater than 3.0, but you can fire bullets with power as low as 0.1. If you call a firing function (i.e. <code>setFire()</code>) with a value greater than 3.0, Robocode will adjust it to 3.0, and if you call it with a power lower than 0.1 (except 0.0 which will not fire) it will adjust it to 0.1. Additionally, you can fire bullets with power less than 0.1 under one condition: when your robot has less than 0.1 energy left, in which case a bullet is fired with however much energy your robot had left. | ||
− | + | : See [http://robocode.sourceforge.net/docs/robocode/robocode/Rules.html#MAX_BULLET_POWER <code>Rules.MAX_BULLET_POWER</code>] and [http://robocode.sourceforge.net/docs/robocode/robocode/Rules.html#MIN_BULLET_POWER <code>Rules.MIN_BULLET_POWER</code>]. | |
− | : | ||
− | ; How fast does a bullet travel? | + | ; '''How fast does a bullet travel?''' |
− | :A bullet travels at a speed between 11.0 and 19.7 depending on the power. The more powerful the bullet, the slower. The formula to calculate it is <code>velocity = 20 - (3 * power)</code>. | + | : A bullet travels at a speed between 11.0 and 19.7 depending on the power. The more powerful the bullet, the slower. The formula to calculate it is <code>velocity = 20 - (3 * power)</code>. |
− | + | : See [http://robocode.sourceforge.net/docs/robocode/robocode/Rules.html#getBulletSpeed(double) <code>Rules.getBulletSpeed()</code>]. | |
− | : | ||
− | ; | + | ; '''Does the robot velocity get added to the bullet velocity on firing?''' |
+ | : No, bullet velocity is not affected by robot velocity. It's kind of like the speed-of-light thing. | ||
+ | |||
+ | ; '''Are bullets fired from the end of the barrel, or the center of the robot?''' | ||
+ | : They start at the center of the robot on the tick that you call <code>fire()/setFire()/setFireBullet()</code>. | ||
+ | |||
+ | ; '''What is the range of a bullet?''' | ||
: A bullet has no range. It keeps going until it hits a robot or a wall. | : A bullet has no range. It keeps going until it hits a robot or a wall. | ||
− | ; | + | ; '''What is the size of a bullet?''' |
− | : | + | : A bullet has no size. Robocode treats them as line segments from one tick to the next. |
− | ; How much damage does a bullet do? | + | ; '''I want to fire a bullet every turn, but I can't. Why?''' |
− | + | : Every time you fire, the gun generates some heat. You must wait till it is cool again to fire. If you give a fire order when your gun is hot, it will do nothing. The heat generated by a shot is <code>1 + (firepower / 5)</code>. The gun cools down at a default rate of 0.1 per turn (note that you can change this parameter when you run the battle, but nobody usually does). It means you can fire a 3.0 power bullet every 16 ticks. | |
+ | |||
+ | : See [http://robocode.sourceforge.net/docs/robocode/robocode/Rules.html#getGunHeat(double) <code>Rules.getGunHeat()</code>] and [http://robocode.sourceforge.net/docs/robocode/robocode/Robot.html#getGunCoolingRate() <code>Robot.getGunCoolingRate()</code>]. | ||
+ | |||
+ | ; '''How much damage does a bullet do? How do I gain or lose energy?''' | ||
: You lose energy every time you hit a wall, you are hit by an enemy bullet, ram an enemy, or you fire your gun. The amount of energy you lose by being hit is <code>4 * bullet power + 2 * max(bullet power - 1 , 0)</code>. So the maximum amount is 16.0. When you fire, you spend a quantity of energy equal to the power of the bullet fired. When one of your bullets hits an enemy, you collect back <code>3 * bullet power</code> energy. When you hit an enemy bot, each bot takes 0.6 damage. If an [[AdvancedRobot]] (but not a [[Robot]] or [[JuniorRobot]]) hits a wall, it will take <code>max(abs(velocity) * 0.5 - 1, 0)</code> damage. | : You lose energy every time you hit a wall, you are hit by an enemy bullet, ram an enemy, or you fire your gun. The amount of energy you lose by being hit is <code>4 * bullet power + 2 * max(bullet power - 1 , 0)</code>. So the maximum amount is 16.0. When you fire, you spend a quantity of energy equal to the power of the bullet fired. When one of your bullets hits an enemy, you collect back <code>3 * bullet power</code> energy. When you hit an enemy bot, each bot takes 0.6 damage. If an [[AdvancedRobot]] (but not a [[Robot]] or [[JuniorRobot]]) hits a wall, it will take <code>max(abs(velocity) * 0.5 - 1, 0)</code> damage. | ||
− | ; Some times I get disabled. What happens? | + | : See [http://robocode.sourceforge.net/docs/robocode/robocode/Rules.html#getBulletDamage(double) <code>Rules.getBulletDamage()</code>] and [http://robocode.sourceforge.net/docs/robocode/robocode/Rules.html#getBulletHitBonus(double) <code>Rules.getBulletHitBonus()</code>] and [http://robocode.sourceforge.net/docs/robocode/robocode/Rules.html#getWallHitDamage(double) <code>Rules.getWallHitDamage()</code>]. |
+ | |||
+ | ; '''Some times I get disabled. What happens?''' | ||
: You can't kill yourself, so when your energy drops to zero because you hit a wall or you fire, your bot gets disabled. It will not be able to move nor fire. If you are lucky enough and one of your bullets in the air hits an enemy, you will get some energy back and recover from disabled status. | : You can't kill yourself, so when your energy drops to zero because you hit a wall or you fire, your bot gets disabled. It will not be able to move nor fire. If you are lucky enough and one of your bullets in the air hits an enemy, you will get some energy back and recover from disabled status. | ||
− | ; I get disabled, but I my energy > 0. Why? | + | ; '''I get disabled, but I my energy > 0. Why?''' |
− | : You may have called a getXXX() - | + | : There are a few possible causes. You may have called a getXXX() function - such as getVelocity() - too many times a turn. The limit is 10000 getXXX() function calls per turn. To avoid disabling in such situations, either store returned values in variables for future use or use a [[RobotStatus]] object obtained from [[StatusEvent]]. Another case in which you can get disabled is throwing an exception, which may disable your bot, even if you catch the exception. Also, if your bot gets stuck in an infinite (or very long) loop and skips many turns, it may also get disabled. |
− | ; How fast do I move? | + | ; '''How fast do I move?''' |
− | : You can move at a maximum speed of 8.0 units/tick. You can modify (down) your maximum velocity by using <code>setMaxVelocity(...)</code>. Note that your bot will always accelerate to reach | + | : You can move at a maximum speed of 8.0 units/tick. You can modify (down) your maximum velocity by using <code>setMaxVelocity(...)</code>. Note that your bot will always accelerate to reach its maximum velocity. |
− | ; How fast do I accelerate? | + | : See [http://robocode.sourceforge.net/docs/robocode/robocode/Rules.html#MAX_VELOCITY <code>Rules.MAX_VELOCITY</code>]. |
+ | |||
+ | ; '''How fast do I accelerate?''' | ||
: You accelerate at 1 unit/tick, and you decelerate at 2 units/tick. For example, if you are moving at an speed of 8.0 and reverse your direction your velocities will be [6.0, 4.0, 2.0, 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0]. | : You accelerate at 1 unit/tick, and you decelerate at 2 units/tick. For example, if you are moving at an speed of 8.0 and reverse your direction your velocities will be [6.0, 4.0, 2.0, 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0]. | ||
− | + | : See [http://robocode.sourceforge.net/docs/robocode/robocode/Rules.html#ACCELERATION <code>Rules.ACCELERATION</code>] and [http://robocode.sourceforge.net/docs/robocode/robocode/Rules.html#DECELERATION <code>Rules.DECELERATION </code>]. | |
− | : | ||
− | ; | + | ; '''How fast do I turn?''' |
− | : The | + | : The faster you go, the slower you turn. The formula to calculate it in degrees is <code>10 - 0.75 * abs(velocity)</code>. The maximum turn rate is 10 degrees/turn. |
− | ; It seems that Robocode doesn't follow standard physics. If my velocity is 0 and I accelerate (acceleration = 1) my final velocity is 1, but it should be 0.5. What happened? | + | : See [http://robocode.sourceforge.net/docs/robocode/robocode/Rules.html#getTurnRate(double) <code>Rules.getTurnRate()</code>] and [http://robocode.sourceforge.net/docs/robocode/robocode/Rules.html#MAX_TURN_RATE <code>Rules.MAX_TURN_RATE</code>]. |
+ | |||
+ | ; '''What is the size of a bot?''' | ||
+ | : The size of a bot is 36x36. Note, this is slightly smaller than the image of the bot. It is modeled as a non rotating square, so it's always the same regardless of its heading. | ||
+ | |||
+ | ; '''It seems that Robocode doesn't follow standard physics. If my velocity is 0 and I accelerate (acceleration = 1) my final velocity is 1, but it should be 0.5. What happened?''' | ||
: Time in Robocode, rather than being continuous, is in discrete "ticks". First acceleration is calculated, then velocity, and then position. So if you are stopped at a position 0 and you accelerate 1, your velocity next turn will be 1 and your position also 1. | : Time in Robocode, rather than being continuous, is in discrete "ticks". First acceleration is calculated, then velocity, and then position. So if you are stopped at a position 0 and you accelerate 1, your velocity next turn will be 1 and your position also 1. | ||
− | ; How can I detect when an enemy has fired? | + | ; '''How can I detect when an enemy has fired?''' |
− | : There is no direct way to detect when an enemy fired, but you can deduce it by monitoring the enemy energy drop. A drop between 0.1 and 3 usually means that it fired a bullet (there can be other reasons, such as a low energy bullet hit or a wall hit). Wall hits are (more or less) detectable as well. A deceleration > 2 means the bot hit a wall (or another bot). A deceleration <= 2 may be simple a bot hitting the brakes, or hitting a wall at velocity = 2, but since hitting a wall at that speed won't cause any damage, you can ignore that. AdvancedRobots take <code>abs(velocity) / 2 - 1 (Never < 0)</code> damage when hitting a wall, so by detecting (significant) wall-hits and adjusting the enemy drop accordingly, wall hits can be filtered out most of the time. This method fails when the enemy hits another robot. | + | : There is no direct way to detect when an enemy fired, but you can deduce it by monitoring the enemy [[energy drop]]. A drop between 0.1 and 3 usually means that it fired a bullet (there can be other reasons, such as a low energy bullet hit or a wall hit). Wall hits are (more or less) detectable as well. A deceleration > 2 means the bot hit a wall (or another bot). A deceleration <= 2 may be simple a bot hitting the brakes, or hitting a wall at velocity = 2, but since hitting a wall at that speed won't cause any damage, you can ignore that. AdvancedRobots take <code>abs(velocity) / 2 - 1 (Never < 0)</code> damage when hitting a wall, so by detecting (significant) wall-hits and adjusting the enemy drop accordingly, wall hits can be filtered out most of the time. This method fails when the enemy hits another robot. |
− | ; How can I detect the position and heading of an enemy bullet? | + | ; '''How can I detect the position and heading of an enemy bullet?''' |
: You can't. There is no way to know it, directly or indirectly. But of course, you can always [[GuessFactor|guess]]... | : You can't. There is no way to know it, directly or indirectly. But of course, you can always [[GuessFactor|guess]]... | ||
− | ; How fast can I turn my gun? | + | ; '''How fast can I turn my gun?''' |
: The gun turns at 20 degrees per tick. | : The gun turns at 20 degrees per tick. | ||
− | ; How fast can I turn my radar? | + | : See [http://robocode.sourceforge.net/docs/robocode/robocode/Rules.html#GUN_TURN_RATE <code>Rules.GUN_TURN_RATE</code>]. |
+ | |||
+ | ; '''How fast can I turn my radar?''' | ||
: It turns 45 degrees per tick. | : It turns 45 degrees per tick. | ||
− | ; Can I know the heading of the enemy gun/radar? | + | : See [http://robocode.sourceforge.net/docs/robocode/robocode/Rules.html#RADAR_TURN_RATE <code>Rules.RADAR_TURN_RATE</code>]. |
+ | |||
+ | ; '''Can I know the heading of the enemy gun/radar?''' | ||
: No. | : No. | ||
− | ; Can I specify the initial position of my bot? | + | ; '''Can I specify the initial position of my bot?''' |
: No. The bots are randomly placed in the field at the beginning of each round. | : No. The bots are randomly placed in the field at the beginning of each round. | ||
+ | |||
+ | ; '''How can I find more information about Robocode's internals?''' | ||
+ | : The true documentation is the source code. [https://github.com/robo-code/robocode Take a look]. | ||
+ | |||
+ | : Tips for navigating through the Robocode codebase: | ||
+ | :* Robocode internally makes use of [http://picocontainer.com/ picocontainer] to provide [[wikipedia:Dependency Injection|dependency injection]]. | ||
+ | :* The API methods are implemented in the [https://github.com/robo-code/robocode/blob/master/robocode.api/src/main/java/robocode/AdvancedRobot.java <code>AdvancedRobot</code>] and [https://github.com/robo-code/robocode/blob/master/robocode.api/src/main/java/robocode/Robot.java <code>Robot</code>] classes. They are wrappers around the [http://robocode.sourceforge.net/docs/robocode/robocode/robotinterfaces/peer/package-summary.html robot peer], which calls into the Robocode core. You can trace the flow of execution from there. | ||
+ | :* You can find a class' source file given its name through the [https://github.com/blog/793-introducing-the-file-finder GitHub File Finder], by pressing <code>t</code> anywhere in GitHub. | ||
+ | :* GitHub's [https://help.github.com/articles/searching-code/ repository code search] is also helpful. | ||
+ | :* Robocode's [https://github.com/robo-code/robocode/blob/master/versions.md version history] provides useful details about how the program evolved. | ||
+ | |||
+ | == Programming your robot == | ||
+ | ; '''What is the difference between the setXXX() (e.g. <code>setFire()</code>) and the XXX() (e.g. <code>fire()</code>) methods?''' | ||
+ | : Basically, the setXXX() methods just notify Robocode to take some action at the end of the turn. The XXX()-type methods end the turn when you call them, and they block your robot's thread until the command finishes. Unless you have a good reason, you should almost always use the setXXX() version when writing [[AdvancedRobot|AdvancedRobots]]. | ||
+ | |||
+ | ; '''How can I avoid my gun/radar turning when my bot turns?''' | ||
+ | : You can use <code>setAdjustGunForRobotTurn()</code>, <code> setAdjustRadarForGunTurn()</code>, and <code>setAdjustRadarForRobotTurn()</code> methods to control this. If you call <code>setAdjustGunForRobotTurn()</code> and <code> setAdjustRadarForGunTurn()</code>, you don't need to call <code>setAdjustRadarForRobotTurn()</code>. | ||
+ | |||
+ | ; '''Why are there two functions for <code>getBearing()</code> for example - one in radians and one in degrees? Is there any performance gain if I use radians instead of degrees?''' | ||
+ | : There is no real advantage to using one or the other. Just use the one you prefer. Often, people start using degrees (just because they feel more comfortable with them) and later they switch to radians (because calculations are easier since you can use the built-in Java trigonometric functions). Just remember to use always radians or always degrees; mixing them up is not a good idea. | ||
+ | |||
+ | ; '''I need to trace my bots actions and variables. I saw that everybody uses <code>out.println("...")</code>, but where is that printed?''' | ||
+ | : It prints to the [[Robocode/Robot Console|robot console]]. When you execute the battle, just click on the button on the right of the screen that shows the name of your robot to open its [[Robocode/Robot Console|console]]. | ||
+ | |||
+ | ; '''How do you get your radar to stay focused on a robot that you have defined as your target?''' | ||
+ | : You just turn the radar the other way around when you scan the bot. You lock your radar by not turning it 45 degrees, but only the arc needed to stay focused. See the [[Radar]] page for some example code. | ||
+ | |||
+ | ; '''How can I know how many enemies are in the battle field?''' | ||
+ | : You can use the <code>getOthers()</code> method to know how many live enemies are in the battlefield. | ||
+ | |||
+ | ; '''I'm trying to recognize an enemy/teammate from its name (using <code>e.getName()</code>) but the condition always fails. What's happening?''' | ||
+ | : Because of Java's funky way of interpreting references to Strings (not to mention a lack of operator overloading), you can't use an expression like <code>if (e.getName() == testname)</code> to check for equality. You have to use the <code>String.equals()</code> method, as in <code>if (e.getName().equals(testname))</code>. | ||
+ | |||
+ | ; '''How do I keep data from round to round and battle to battle?''' | ||
+ | : The easiest way is to save data between rounds of a battle is to make the variables in the bot class static. Because Robocode uses a separate classloader for every robot, the variables will not conflict even when you have more than one copy of a robot in a battle. Note that this will save data between ''rounds'', not between ''battles''. To save between battles you will have to save to a file. The maximum allowed disk space for files is 200k. Look at the [[:Category:Robocode API|Robocode API]] for more details. | ||
+ | |||
+ | ; '''I get the following message when I run my bot, and I don't know how to solve it.''' | ||
+ | <pre> | ||
+ | SYSTEM: You have made 10000 calls to getXX methods without calling execute() | ||
+ | SYSTEM: Robot disabled: Too many calls to getXX methods. | ||
+ | </pre> | ||
+ | : Robocode prevents you from calling functions like <code>getX()</code> or <code>getVelocity()</code> too many times during a single tick. So if you are using them in a long loop, it will raise this error. Actually, 95% of the time, this error is a symptom of an infinite loop in your bot. If you know you have a long-but-finite loop and you get this error, either just assign the values you want to use to a variable or use a [[RobotStatus]] object obtained from [[StatusEvent]]. | ||
+ | |||
+ | ; '''I'm using <code>bulletObject = setFireBullet(power)</code> to fire, and then I want to get the bullet coordinates. But when I try to print them using <code>System.out.println(bulletObject.getX() + bulletObject.getY())</code> I get an error. What's wrong?''' | ||
+ | : <code>setFireBullet()</code> creates a Bullet object, but the bullet doesn't actually leave your bots gun until the next tick, so you can't do <code>getX()</code> or <code>getY()</code> on the bullet until then. If you change it to <code>fireBullet()</code> you should be OK, because the function won't return until the bullet is in the air. If <code>fireBullet()</code> won't work for you, you'll have to devise another method of making sure that you don't do <code>getX()</code> and <code>getY()</code> on bullets until the turn after you fire. For example, you could store Bullets in an ArrayList, and print out their coordinates before you fire in your main loop, so that a given bullet will be added to the vector on one turn, but won't be accessed until the next turn when your main loop starts over. Alternatively, your bot can attempt to predict/simulate it's own location on the next tick, to know where the bullet will be created. | ||
+ | |||
+ | ; '''I want to reverse my direction when my movement is about to finish. I use something like <code>if (getDistanceRemaining() < minimum)</code>, but the bot behaves in a strange way.''' | ||
+ | : The <code>getDistanceRemaining()</code> method (and in general all methods returning remaining movements of the body, gun, or radar) can return a positive or a negative value, depending on the direction of your movement. Use <code>if (Math.abs(getGetDistanceRemaining()) < minimum)</code> instead. | ||
+ | |||
+ | [[File:AbsoluteRelativeAngles.png|thumb|300px|Absolute and relative angles in Robocode]] | ||
+ | ; '''What are the [http://robocode.sourceforge.net/docs/robocode/robocode/util/Utils.html <code>robocode.Utils</code> angle normalization methods]?''' | ||
+ | : In Robocode, relative angles (-180° to 180°) are commonly used as inputs to <code>setTurnGunRight()</code> and other such robot control methods. Normalizing them is often used as an optimization – for instance, there is no point of turning the gun 270° if you can turn it -90° instead; that saves 9 ticks. The method [http://robocode.sourceforge.net/docs/robocode/robocode/util/Utils.html#normalRelativeAngleDegrees(double) <code>Utils.normalRelativeAngleDegrees()</code>] is used to normalize relative angles. Similarly, absolute angles (0° to 360°) can be normalized with [http://robocode.sourceforge.net/docs/robocode/robocode/util/Utils.html#normalAbsoluteAngleDegrees(double) <code>Utils.normalAbsoluteAngleDegrees()</code>]. | ||
+ | |||
+ | ; '''My trigonometric calculations get very strange results. What is wrong?''' | ||
+ | : Normal trigonometry is counterclockwise (the angle increases to the left) but in Robocode it is clockwise (the angle increases to the right). It means that you cannot directly apply the normal trigonometric formulas to Robocode. Essentially, it means you must use <code>Math.sin()</code> where you would normally use <code>Math.cos()</code>, and <code>Math.cos()</code> where you would normally use <code>Math.sin()</code>. | ||
+ | |||
+ | ; '''How do I find the absolute angle between two coordinates?''' | ||
+ | : <syntaxhighlight lang="java">double angle = Math.atan2(x2 - x1, y2 - y1);</syntaxhighlight> | ||
+ | |||
+ | ; '''How do I calculate the distance between two coordinates?''' | ||
+ | : With this code (make sure to import <code>java.awt.geom.Point2D</code>): | ||
+ | : <syntaxhighlight lang="java">double distance = Point2D.distance(x1, y1, x2, y2);</syntaxhighlight> | ||
+ | : Alternatively, if you're doing this only to compare distances, <code>Point2D.distanceSq()</code> is a little faster as skips the square root calculation. | ||
+ | |||
+ | ; '''How to I drive to a certain point?''' | ||
+ | : See the [[GoTo]] page for code examples. | ||
+ | |||
+ | ; '''How do I do [some other thing that sounds incredibly basic]?''' | ||
+ | : Every one of the top bots has a general utility class somewhere in its code. Take a look. | ||
+ | |||
+ | == See also == | ||
+ | |||
+ | {{RobocodeDocsList}} | ||
+ | |||
+ | [[Category:Robocode Documentation]] |
Latest revision as of 10:40, 25 October 2020
Frequently asked questions about Robocode.
Contents
Installing and using
- I can't install Robocode.
- Please see these instructions.
- I get this error when trying to start Robocode: "'JAVA' is not recognized as an internal or external command, operable or batch file".
- This is caused by an unknown path to your Java installation. Please follow these instructions.
- Can I play Robocode online?
- Robocode is not an "online" game, so you can't, for example, share a battle with your friends in real time over the Internet. But you can upload your bots to places such as GitHub or Dropbox, and join any of the existing competitions such as RoboRumble (enter here), or organize one with your friends.
- What is the difference between a set method like setAhead() and ahead() without the set-prefix?
- The difference between a method like
ahead()
, and the methodsetAhead()
is that set-methods are only available with the AdvancedRobot. The difference is that you can call multiple setters like e.g.setAhead()
,setTurnGunLeft()
, andsetFire()
in the same turn. These set commands will first take effect, i.e. execute, when you call theexecute()
method explicitly in a turn. Methods that are not setters like e.g.ahead()
,turnGunLeft()
, andfire()
will execute independently and take one to many turns to complete, as one command will need to execute and complete before the next command is being executed. Setters are called in the same turn whenexecute()
is called. So the methodahead()
is actually the same assetAhead()
+execute()
, and in that order. The set methods can be seen as "fire and forget" methods that are executed immediately, whereas the methods likeahead()
must first complete its movement, turning, firing etc. before the next command is being run. Hence those commands need to wait for the other commands to complete first before the method itself will be executed.
- How do I get started?
- See Getting Started and My First Robot.
- What can robots do?
- Many things. They can move, turn, scan robots with their Radar, aim their gun and fire bullets, and get information about the world around them. See the Robocode API.
- Should I download robots from the Internet?
- Yes. More robots makes your Robocode experience more fun. See Downloading Robots.
- I have downloaded a robot from the Web, but I don't know how to use it, because it doesn't appear anywhere.
- By default, robots are read from the
/robocode/robots
directory. You can select "Robot -> Import Downloaded Robot" to copy a robot JAR to this directory from another location. Also, you can configure Robocode to read robots from additional locations using the Properties dialog.
- I have seen that many bots are packaged into ".jar" files. How do I package my bot?
- Select, "Robot -> Package robot for upload" from the menu, then enter your robot's details when prompted. See Package Robot.
- What do I have to do to see the source code of a robot?
- You can do two things. The first option is to open the Editor Window in Robocode and use the command in the File menu. The second option is to open the robot ".jar" file with a zip utility and find the source code there (assuming, of course, the robot is open source). See Learning from Robots.
- When I test my bots, Robocode is slow. Is there a way to execute the battles faster?
- When you are testing your robot, you want to execute many battles in a short time. Minimize the Robocode main screen to make it execute the battles at full speed.
- What is with the star "*" next to my robot's name?
- The asterisk to the right of a robot's name indicates that the robot is a development version, not a packaged version.
Game physics
- What are ticks, turns, and frames?
- A tick refers to one unit, which is also called a turn in Robocode. During one turn, you may perform one action as a Robot, or multiple (independent) actions as an AdvancedRobot. A frame is a unit of drawing to the Robocode client interface. If you are processing turns slowly, you will get one frame per tick / turn. However, if you up the turns per second beyond your computer's ability to render the frames, you will miss some frames of animation. This won't affect the robots' behavior, unless you foolishly added code in your
onPaint(Graphics2D)
method that alters your bots behavior. In that case, your bot will behave differently depending on whether or not the Paint button has been enabled, and if the framerate can keep up with the turnrate.
- Can I fire bullets with power higher than 3.0 or lower than 1.0?
- No and yes. You can't fire bullets with power greater than 3.0, but you can fire bullets with power as low as 0.1. If you call a firing function (i.e.
setFire()
) with a value greater than 3.0, Robocode will adjust it to 3.0, and if you call it with a power lower than 0.1 (except 0.0 which will not fire) it will adjust it to 0.1. Additionally, you can fire bullets with power less than 0.1 under one condition: when your robot has less than 0.1 energy left, in which case a bullet is fired with however much energy your robot had left.
- How fast does a bullet travel?
- A bullet travels at a speed between 11.0 and 19.7 depending on the power. The more powerful the bullet, the slower. The formula to calculate it is
velocity = 20 - (3 * power)
.
- Does the robot velocity get added to the bullet velocity on firing?
- No, bullet velocity is not affected by robot velocity. It's kind of like the speed-of-light thing.
- Are bullets fired from the end of the barrel, or the center of the robot?
- They start at the center of the robot on the tick that you call
fire()/setFire()/setFireBullet()
.
- What is the range of a bullet?
- A bullet has no range. It keeps going until it hits a robot or a wall.
- What is the size of a bullet?
- A bullet has no size. Robocode treats them as line segments from one tick to the next.
- I want to fire a bullet every turn, but I can't. Why?
- Every time you fire, the gun generates some heat. You must wait till it is cool again to fire. If you give a fire order when your gun is hot, it will do nothing. The heat generated by a shot is
1 + (firepower / 5)
. The gun cools down at a default rate of 0.1 per turn (note that you can change this parameter when you run the battle, but nobody usually does). It means you can fire a 3.0 power bullet every 16 ticks.
- How much damage does a bullet do? How do I gain or lose energy?
- You lose energy every time you hit a wall, you are hit by an enemy bullet, ram an enemy, or you fire your gun. The amount of energy you lose by being hit is
4 * bullet power + 2 * max(bullet power - 1 , 0)
. So the maximum amount is 16.0. When you fire, you spend a quantity of energy equal to the power of the bullet fired. When one of your bullets hits an enemy, you collect back3 * bullet power
energy. When you hit an enemy bot, each bot takes 0.6 damage. If an AdvancedRobot (but not a Robot or JuniorRobot) hits a wall, it will takemax(abs(velocity) * 0.5 - 1, 0)
damage.
- Some times I get disabled. What happens?
- You can't kill yourself, so when your energy drops to zero because you hit a wall or you fire, your bot gets disabled. It will not be able to move nor fire. If you are lucky enough and one of your bullets in the air hits an enemy, you will get some energy back and recover from disabled status.
- I get disabled, but I my energy > 0. Why?
- There are a few possible causes. You may have called a getXXX() function - such as getVelocity() - too many times a turn. The limit is 10000 getXXX() function calls per turn. To avoid disabling in such situations, either store returned values in variables for future use or use a RobotStatus object obtained from StatusEvent. Another case in which you can get disabled is throwing an exception, which may disable your bot, even if you catch the exception. Also, if your bot gets stuck in an infinite (or very long) loop and skips many turns, it may also get disabled.
- How fast do I move?
- You can move at a maximum speed of 8.0 units/tick. You can modify (down) your maximum velocity by using
setMaxVelocity(...)
. Note that your bot will always accelerate to reach its maximum velocity.
- See
Rules.MAX_VELOCITY
.
- How fast do I accelerate?
- You accelerate at 1 unit/tick, and you decelerate at 2 units/tick. For example, if you are moving at an speed of 8.0 and reverse your direction your velocities will be [6.0, 4.0, 2.0, 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0].
- See
Rules.ACCELERATION
andRules.DECELERATION
.
- How fast do I turn?
- The faster you go, the slower you turn. The formula to calculate it in degrees is
10 - 0.75 * abs(velocity)
. The maximum turn rate is 10 degrees/turn.
- See
Rules.getTurnRate()
andRules.MAX_TURN_RATE
.
- What is the size of a bot?
- The size of a bot is 36x36. Note, this is slightly smaller than the image of the bot. It is modeled as a non rotating square, so it's always the same regardless of its heading.
- It seems that Robocode doesn't follow standard physics. If my velocity is 0 and I accelerate (acceleration = 1) my final velocity is 1, but it should be 0.5. What happened?
- Time in Robocode, rather than being continuous, is in discrete "ticks". First acceleration is calculated, then velocity, and then position. So if you are stopped at a position 0 and you accelerate 1, your velocity next turn will be 1 and your position also 1.
- How can I detect when an enemy has fired?
- There is no direct way to detect when an enemy fired, but you can deduce it by monitoring the enemy energy drop. A drop between 0.1 and 3 usually means that it fired a bullet (there can be other reasons, such as a low energy bullet hit or a wall hit). Wall hits are (more or less) detectable as well. A deceleration > 2 means the bot hit a wall (or another bot). A deceleration <= 2 may be simple a bot hitting the brakes, or hitting a wall at velocity = 2, but since hitting a wall at that speed won't cause any damage, you can ignore that. AdvancedRobots take
abs(velocity) / 2 - 1 (Never < 0)
damage when hitting a wall, so by detecting (significant) wall-hits and adjusting the enemy drop accordingly, wall hits can be filtered out most of the time. This method fails when the enemy hits another robot.
- How can I detect the position and heading of an enemy bullet?
- You can't. There is no way to know it, directly or indirectly. But of course, you can always guess...
- How fast can I turn my gun?
- The gun turns at 20 degrees per tick.
- See
Rules.GUN_TURN_RATE
.
- How fast can I turn my radar?
- It turns 45 degrees per tick.
- Can I know the heading of the enemy gun/radar?
- No.
- Can I specify the initial position of my bot?
- No. The bots are randomly placed in the field at the beginning of each round.
- How can I find more information about Robocode's internals?
- The true documentation is the source code. Take a look.
- Tips for navigating through the Robocode codebase:
- Robocode internally makes use of picocontainer to provide dependency injection.
- The API methods are implemented in the
AdvancedRobot
andRobot
classes. They are wrappers around the robot peer, which calls into the Robocode core. You can trace the flow of execution from there. - You can find a class' source file given its name through the GitHub File Finder, by pressing
t
anywhere in GitHub. - GitHub's repository code search is also helpful.
- Robocode's version history provides useful details about how the program evolved.
Programming your robot
- What is the difference between the setXXX() (e.g.
setFire()
) and the XXX() (e.g.fire()
) methods? - Basically, the setXXX() methods just notify Robocode to take some action at the end of the turn. The XXX()-type methods end the turn when you call them, and they block your robot's thread until the command finishes. Unless you have a good reason, you should almost always use the setXXX() version when writing AdvancedRobots.
- How can I avoid my gun/radar turning when my bot turns?
- You can use
setAdjustGunForRobotTurn()
,setAdjustRadarForGunTurn()
, andsetAdjustRadarForRobotTurn()
methods to control this. If you callsetAdjustGunForRobotTurn()
andsetAdjustRadarForGunTurn()
, you don't need to callsetAdjustRadarForRobotTurn()
.
- Why are there two functions for
getBearing()
for example - one in radians and one in degrees? Is there any performance gain if I use radians instead of degrees? - There is no real advantage to using one or the other. Just use the one you prefer. Often, people start using degrees (just because they feel more comfortable with them) and later they switch to radians (because calculations are easier since you can use the built-in Java trigonometric functions). Just remember to use always radians or always degrees; mixing them up is not a good idea.
- I need to trace my bots actions and variables. I saw that everybody uses
out.println("...")
, but where is that printed? - It prints to the robot console. When you execute the battle, just click on the button on the right of the screen that shows the name of your robot to open its console.
- How do you get your radar to stay focused on a robot that you have defined as your target?
- You just turn the radar the other way around when you scan the bot. You lock your radar by not turning it 45 degrees, but only the arc needed to stay focused. See the Radar page for some example code.
- How can I know how many enemies are in the battle field?
- You can use the
getOthers()
method to know how many live enemies are in the battlefield.
- I'm trying to recognize an enemy/teammate from its name (using
e.getName()
) but the condition always fails. What's happening? - Because of Java's funky way of interpreting references to Strings (not to mention a lack of operator overloading), you can't use an expression like
if (e.getName() == testname)
to check for equality. You have to use theString.equals()
method, as inif (e.getName().equals(testname))
.
- How do I keep data from round to round and battle to battle?
- The easiest way is to save data between rounds of a battle is to make the variables in the bot class static. Because Robocode uses a separate classloader for every robot, the variables will not conflict even when you have more than one copy of a robot in a battle. Note that this will save data between rounds, not between battles. To save between battles you will have to save to a file. The maximum allowed disk space for files is 200k. Look at the Robocode API for more details.
- I get the following message when I run my bot, and I don't know how to solve it.
SYSTEM: You have made 10000 calls to getXX methods without calling execute() SYSTEM: Robot disabled: Too many calls to getXX methods.
- Robocode prevents you from calling functions like
getX()
orgetVelocity()
too many times during a single tick. So if you are using them in a long loop, it will raise this error. Actually, 95% of the time, this error is a symptom of an infinite loop in your bot. If you know you have a long-but-finite loop and you get this error, either just assign the values you want to use to a variable or use a RobotStatus object obtained from StatusEvent.
- I'm using
bulletObject = setFireBullet(power)
to fire, and then I want to get the bullet coordinates. But when I try to print them usingSystem.out.println(bulletObject.getX() + bulletObject.getY())
I get an error. What's wrong? setFireBullet()
creates a Bullet object, but the bullet doesn't actually leave your bots gun until the next tick, so you can't dogetX()
orgetY()
on the bullet until then. If you change it tofireBullet()
you should be OK, because the function won't return until the bullet is in the air. IffireBullet()
won't work for you, you'll have to devise another method of making sure that you don't dogetX()
andgetY()
on bullets until the turn after you fire. For example, you could store Bullets in an ArrayList, and print out their coordinates before you fire in your main loop, so that a given bullet will be added to the vector on one turn, but won't be accessed until the next turn when your main loop starts over. Alternatively, your bot can attempt to predict/simulate it's own location on the next tick, to know where the bullet will be created.
- I want to reverse my direction when my movement is about to finish. I use something like
if (getDistanceRemaining() < minimum)
, but the bot behaves in a strange way. - The
getDistanceRemaining()
method (and in general all methods returning remaining movements of the body, gun, or radar) can return a positive or a negative value, depending on the direction of your movement. Useif (Math.abs(getGetDistanceRemaining()) < minimum)
instead.
- What are the
robocode.Utils
angle normalization methods? - In Robocode, relative angles (-180° to 180°) are commonly used as inputs to
setTurnGunRight()
and other such robot control methods. Normalizing them is often used as an optimization – for instance, there is no point of turning the gun 270° if you can turn it -90° instead; that saves 9 ticks. The methodUtils.normalRelativeAngleDegrees()
is used to normalize relative angles. Similarly, absolute angles (0° to 360°) can be normalized withUtils.normalAbsoluteAngleDegrees()
.
- My trigonometric calculations get very strange results. What is wrong?
- Normal trigonometry is counterclockwise (the angle increases to the left) but in Robocode it is clockwise (the angle increases to the right). It means that you cannot directly apply the normal trigonometric formulas to Robocode. Essentially, it means you must use
Math.sin()
where you would normally useMath.cos()
, andMath.cos()
where you would normally useMath.sin()
.
- How do I find the absolute angle between two coordinates?
double angle = Math.atan2(x2 - x1, y2 - y1);
- How do I calculate the distance between two coordinates?
- With this code (make sure to import
java.awt.geom.Point2D
): double distance = Point2D.distance(x1, y1, x2, y2);
- Alternatively, if you're doing this only to compare distances,
Point2D.distanceSq()
is a little faster as skips the square root calculation.
- How to I drive to a certain point?
- See the GoTo page for code examples.
- How do I do [some other thing that sounds incredibly basic]?
- Every one of the top bots has a general utility class somewhere in its code. Take a look.
See also
Robocode API
Beginner Guides
- Welcome to Robocode
- System requirements
- Download and install
- Getting started
- Frequently asked questions
- My First Robot tutorial
- Game physics
- The anatomy of a robot
- Scoring in Robocode
- Using the robot console
- Downloading other robots
- Learning from other robots
- Package your robot
- Articles about Robocode
- Starting Robocode from the command line
- Graphical debugging
External Editors
- Using Eclipse with Robocode
- Creating a project in Eclipse
- Creating a robot in Eclipse
- Running your robot from Eclipse
- Debugging your robot with Eclipse
- Using NetBeans with Robocode
- Using Gradle with Robocode