(redirected from DetectingFire)


Robo Home | Changes | Preferences | AllPages

I had some spare time, so I put together some of the questions appearing in the forums. The list is not complete, and many questions are not answered yet. Please help it grow. -- Albert

The FAQ is organized in several sections:

Installing and using

I can't install Robocode. Please, help!

First make sure you have java installed on your computer. To check it, run the command: java -version It should execute and show a version 1.3 or higher. If not, go to Sun site and download the last version. After, make sure you have downloaded the correct file robocode-setup.jar. Execute the file by double-clicking on it, or if you want to do it manually, execute the following command: java -jar robocode-setup.jar

It will launch an installer that will install robocode. The installer creates the icons to execute Robocode, but you can execute it by running a ".bat" (or analogous linux) file in the /robocode directory.

I have downloaded a robot from the Web, but I don't know how to use it, because it doesn't appear anywhere. Could you help me, please?

You select "Robot --> Import Downloaded Robot" from the menu, then select the jar file of the robot that you want to import.

What do I have to do to see the source code of a robot?

You can do two things. If you open the robot ".jar" file with WinZip? (or a similar utility) you will find the souce code there (assuming, of course, the robot is open source). The other way is to open the Editor Window in Robocode, and use the command in the File menu to extract the source code.

Also, if you have the Java SDK then you can extract it with jar.

Can I play Robocode online?

Robocode is not an "on-line" 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 into the RobocodeRepository (or send them by email) join any of the existing competitions or organize one with your friends.

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.

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 faster.

Game physics

Can I fire bullets with power higher than 3 or lower than 1?

The answer is no and yes. You can't fire bullets with power greater than 3, but you can fire bullets with power as low as 0.1. If you call a firing function (ie. setFire) with a value greater than 3, Robocode will adjust it to 3, and if you call it with a power lower than 0.1 it will adjust it to 0.1.

How fast does a bullet travel?

A bullet travels at velocity between 11 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.

Which is the range of a bullet?

A bullet has no range. It keeps going till it hits a bot or a wall.

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 bullet every 16 ticks.

How much damage does a bullet do?

See next question.

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. 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 some energy (you get 3 * bullet power). When you hit an enemy bot, each bot takes 0.6 damage. When you hit a wall, the (Advanced) robot will take a damage of max(abs(velocity) * 0.5 - 1, 0).

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?

You called getXXX() - for example getVelocity() - function too many times a turn. The limit is 10000 getXXX() function calls per turn. To avoid disabling in such situations, store returned values in variables for future use. That will also be a bit faster. Added by Ph

How fast do I move?

You can move at a maximum speed of 8. You can modify (down) your maximum velocity by using setMaxVelocity?(...). Note that your bot will always accelerate to reach it's maximum velocity.

How fast do I accelerate?

You accelerate at 1 per tick, and you deccelerate at 2 per tick. For example, if you are moving at an speed of 8 and reverse your direction your velocities will be [6,4,2,0,1,2,3,4,5,6,7,8].

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)).

Which is the size of a bot?

The size of a bot is 36x36. It is modelled 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 happens?

Time in robocode is in discrete "ticks"; it isn't continuous as time should be. 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 Math.abs(velocity) * .5 - 1; (Never < 0), so by detecting (significant) wall-hits and adjusting the enemy drop accordingly (AdvancedRobots? take Math.abs(velocity) * .5 - 1; (Never < 0)), 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.

Can I know the heading of the enemy gun?

No. Also, you can't know the heading of the enemy radar.

How fast can I turn my Radar?

It turns 45 degrees per tick.

Can I specify the initial position of my bot?

No. The bots are randomly placed in the field at the beginning of each round.

Programing your robot

I'm using b=setFireBullet(1) to fire, and then I want to get the bullet coordinates. But when I try to print them using System.out.println(b.getX() + b.getY()) I get an error. What's wrong?

setFireBullet creates the Bullet object, but the bullet doesn't actually leave your bots gun until the next tick, so you can't do getX() or getY() on the bullet until then. If you change it to fireBullet() you should be ok, because the function won't return until the bullet is in the air. If fireBullet won't work for you, you'll have to devise another method of making sure that you don't do getX() and getY() on bullets until the turn after you fire. For example, you could store Bullets in a Vector, 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.

can anyone explain to me how you get your radar to stay focused on a robot that you defined as your next 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. For example, the following code (on the scan event) locks the radar on the enemy, so you can have an scan per turn:

 setTurnRadarRightRadians(normalRelativeAngle(e.getBearingRadians?() + getHeadingRadians()-getRadarHeadingRadians?()) * 1.9);

why are there two functions for getBearing for example - one in rad & one in degrees? is there any performance gain if I use rad instead of degrees?

There is no advantage to use one or the other. Just use the ones you prefer. Usually, people start using degrees (just because they feel more comfortable) and later they swich 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 cick on the button on the right of the screen that shows the name of your robot to open the console.

How do I go to a certain point (x,y)?

driveTo(x,y) will drive to any point on the battlefield. Sorry it's so unreadable - it's taken from DuelistMicro so it's as small as possible to fit into a microbot.

 driveTo(double nextX, double nextY){
 double myX = getX();double myY = getY();
 setTurnRightRadians?(normalizedTurnAngle? =  Math.atan(Math.tan(turnAngle = ((Math.atan2(nextX - myX, nextY - myY) - setHeadingRadians()) + (7.0 *  Math.PI)) % (2.0 * Math.PI) -  Math.PI)));
 setAhead((normalizedTurnAngle? == turnAngle ? 1.0 : -1.0) * (java.awt.geom.Point2D.Double.distance(myX, myY, nextX, nextY)));}

You can find other methods of doing this on Movement/CodeSnippetGoTo.

How do I keep data from round to round?

The easier way is to make you variables in the bot class static. Note that it will save data between rounds, not between battles. To save between battles you will have to save to a file. The maximum allowed diskspace for files is 200k (as for robocode version 1.06). Just take a look into SavingDataHowto for additional information.

What is the difference between the setXXX (ie. setFire) and the XXX (ie. fire) methods?

Basically, the setXXX just notify Robocode to take some action at the end of the turn (ie. when execute() is called). The other kind of methods end the turn when you call them (ie. you could think of them as a setXXX followed by an inmediate execute()). Also, they will block your robot till the command is executed.

I recommend to use setXXX methods unless you have a good reason to choose the other alternatives.

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() or getVelocity() many times during a single tick. So if you are using them in a long loop, it will raise this error. Just assign the values you want to use to a variable and then use this variable in your loop (instead of the functions).

I'm trying to recognize an enemy/teammate from its name (using e.getName()) but the condition always fails. What's happening?

You can`t use an expression like if (e.getName()==testname) to check for equality. You have to use the String.equals() method to test it: if (e.getName().equals(testname)) will work.

(Scarpia adds: Actually, the way the Java VM handles strings, it will sometimes work with the == operator, since identical String objects are 'merged' in the memory (or so I've been told), but more often, it won't work. Just always use .equals() on strings and you'll be safe)

{Alberto? adds: As a matter of fact that happens only for String literals. If you declare, String x = "Hello", and later in the code you use println("Hello"), both are the same object.)

I want to reverse my direction when my movement is about to finish. I use something like if (getGetDistanceRemaining()<minimum) but the bot behaves in a strange way.

The getGetDistanceRemaining method (and in general all methods returning remaining movements) can return a positive or a negative value, depending on the sign you used when you called setAhead(). Use if (Math.abs(getGetDistanceRemaining())<minimum) instead.

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.

How can I avoid my gun/radar turning when my bot turns?

You can use setAdjustGunForRobotTurn, setAdjustRadarForGunTurn and setAdjustRadarForRobotTurn? methods to control this.

Where can I find good example bots (with source code)?

All over the place - well, not all are example bots. The [SnippetBot Tutorial] is a good, pure example-type robot to look at, and some of the best robots started there (including SandboxDT, according to Paul Evans). The SampleBots are also a good place to figure out how to do basic things, like finding the X/Y coordinates of your opponent, the angle to your opponent, turning perpendicular to your opponent, saving data between rounds and battles, etc. Also, several more advanced robots are open-source - like Loki's bots (if you speak German or whatever he writes his comments in (Kuuran notes: that looks like Dutch to me.Loki adds: it is Dutch! So, does this mean my bots are still obfuscated?)), MogBot, all of iiley's robots, the original Duelist, and many others. If you want to look at a robot with a lot of ideas translated into code, SpareParts is an open-source bot with a lot of strategies to choose from.

This wiki hosts quite a few complete source bots, some of which compete rather well:

Making calculations

My trigonometric calculations get very strange results. What is wrong?

Normal trigonometry is counter-clockwise (the angle increases to the left) but in Robocode it is clock-wise(the angle increases to the right). It means that you can not directly apply the normal trigonometric formulas to Robocode, but have to convert. Basically, it means to use sin where you should use cos and cos where you should use sin. See the RobocodeTrigTutorial for further information.

How do I find the angle between two coordinates?

The easier way is to use the built in java functions. Lets say that you want to calculate the absolute angle from your bot at (x0,y0) to the enemy bot at (x1,y1), then just use:


How do I normalize an angle to a value between 0 and 360?

In degrees, you can use (angle%360+360)%360, or just angle%360 if you're sure you're starting out with a positive angle. In radians, you can do the same thing (except with 2*PI instead of 360), or you can make Robocode do it - robocode.util.Utils.normalAbsoluteAngle?(angle)

How do I normalize an angle to a value between -180 and +180?

 The following functions are widely used to normalize relative angles. First is in degrees, second in radians.

 private double normalRelativeAngle(double angle) { return ((angle + 7*180) % (2*180)) - 180; }
 private double normalRelativeAngleRadians?(double angle) { return ((angle + 7*Math.PI) % (2*Math.PI)) - Math.PI; }

Again, you can also make Robocode do it for you - robocode.util.Utils.normalRelativeAngle(angle) - if you are using Radians.

Why should I want to normalize an angle between -180 and +180?

If you want to find the smallest amount to turn your gun to a certain angle, or find the smallest amount to turn to go a certain direction, you shouldn't have to go more than 180 degrees in either direction.

Is there an easy way to calculate the distance between 2 points?

Rather than reinventing the wheel you could use the Java function java.awt.geom.Point2D.distance.

 import java.awt.geom.*;
 double distance = Point2D.distance(x0, y0, x1, y1);

It is worth pointing out that if you're doing it merely to compare distances then Point2D.distanceSq is a little faster as it doesn't have to calculate a square root.


Which are the main Robocode competitions?

There is no official Robocode competition right now, but there are several leagues out there (1v1, melee and team). You will have to sign in your bot to enter them, after uploading it to the RobocodeRepository. Just take a look into the Leagues page for a complete list.

How can I implement my own competition?

The easier way is to use [Robo league] to run your competitions. It will automate them for you, and it's also useful to test your bots at home. You could also develop your own programs to implement the competition, using the Robocode.control API ... but it's not easy, so better leave it for later.

Where can I find more information?

This Wiki is right now the most complete knowledge database about Robocode. Also, it points you to (almost) all known resources about Robocode. Browse it to get what you want. If it's not here, just ask for it. And if you find something new, please add it to it.

currentTick++; out.println("currentTick = " + currentTick + " || energy = " + getEnergy());

currentTick being an int that starts off at -1 (meaning that it is set to zero on the first tick of the game. I found that at tick 452, everybody's energy dropped by 0.1 and then dropped by 0.1 every tick after that until every robot was dead on tick 1451. I guess I could do some more tests, but I was hoping somebody here would have the answer: are there any factors that push back the energy drop on tick 452? Such as firing bullets, hitting walls, hitting robots with bullets, etc. -- Avi?

Hitting another robot with a bullet will delay the inactivity timer. I believe firing shots that miss and hitting walls won't delay it. In theory, two bots could keep a match going for a loooong time by firing .1 power bullets at each other once every hundred ticks or so. In practice, I've never seen a match last longer than 2000 ticks. --David Alves

I think that firing shots that miss do delay it but not as much as hitting. Also, the higher the energy lost, the more it is delayed. However, it appears as though the newest version changed how the timer worked. -- Kinsen

Robo Home | Changes | Preferences | AllPages
Edit text of this page | View other revisions
Last edited August 15, 2007 8:42 EST by Daniel (diff)