2.2 Comments

Introduction

This section covers some selected topics related to coding for TORCS.

Performance

Because there are several robots in the simulation, we have to take care that our robot doesn't eat up too much performance. Look at the previous listing, I call there 3 times RtTrackSideTgAngleL. That's very bad, because there are very expensive utility functions, so you should keep the number of calls minimal. So whereever you can recycle an expensive computation, do it (that's a bit oversimplified, I know).
There are perhaps also some pieces in the code which doesn't need to be evaluated on every simulation step, so do it just every 10th or whatever is appropriate.

Boolean Expressions

If you need complicated boolean expressions, encapsulate them into a function or a method. This will make the code easier to understand, because the name states what we want to ask (remember isStuck()).

Portability

You have seen that TORCS runs on Windows, Linux/x86 and Linux/ppc. So you have to take care for portability. There are also some issues with Visual C++ 6.0.

For Loops

You can write (according to the standard) this for loops:

for (int i=0; i < 100; i++) { ... }
for (int i=7; i > 0; i--) { ... }

In fact the variable i should disappear after the for loop, but it doesn't in VC++. So to make VC++ happy, write

int i;
for (i=0; i < 100; i++) { ... }
for (i=7; i > 0; i--) { ... }

Constants in Classes

Implement constants like I do in the next section, or use #define. If you need integer constants you can also abuse enumerations for them. The following example won't work with VC++:

class bla {
static const int someconst = 7;
static const float someotherconst = 6.5;
};

Magic Numbers

What's also very bad on my bot till now are the numbers, which are spread all over the place. For now we should define them on a central location as constants. Constants are superior over numbers, because they have meaningful names and you need to change them just once. In a later chapter we will read some of them from an XML file.

Numerical Problems

To implement your robot you will certainly need floating point numbers. Keep in mind that these are not the same like the Real numbers from mathematics, they are discrete and there are gaps between any two numbers. If you are interested in the details search the web for "IEEE 754 floating point numbers".

Adding up Numbers

If you add up values of very different magnitude, e. g. the float 20.000001 and 0.000001 the sum will be still 20.000001 (and not 20.000002). The cause is the internal representation of floating point numbers, be aware of such problems. As example you can produce this way an endless while loop, when the guard waits for the sum to grow to a certain limit.

Special Numbers

The IEEE 754 floating point numbers have some special numbers to signal a special state or problems. The most interesting is the NaN (not a number) which results e. g. in the case when you divide 0.0 with 0.0. Once you have a NaN stored in a variable all computed results based on it are NaN as well. Sometimes you can avoid the danger with manipulating the expression.

Summary

  • You keep an eye on performance.
  • You try to write code which is easy to understand.
  • You try to write portable code.
  • Avoid meaningless numbers, use constants instead.
  • Be aware of floating point numbers.