2.3 Define Multiple Robots

Overview

First ask yourself the question, why we want more than one driver in one module? It's because we can share the program code and data between the drivers of our module. If we write our driver general enough, we can load parameters for the different tracks and cars, so no code difference is necessary.

Car Definition

In chapter 1.1 we had chosen a cg-nascar-fwd as our car. When you edited bt.cpp, you might have recognized that in this file is no clue which car we want to use. The information about the cars is stored in the bt.xml file. So let's have a look at it.

The following three lines just define the name and the structure. You'll not need to change them.

<params name="bt" type="robotdef">
  <section name="Robots">
    <section name="index">

The next line

      <section name="1">

defines the index of the driver. Because we can have more than one driver per module, each one is uniquely identified by its index. You will receive it e. g. in your

static void drive(int index, tCarElt* car, tSituation *s)

function from TORCS, so you know for which car you need to drive. It has to match with the index you assign to modInfo[i].index in bt.cpp, but you will see that later. The next two lines contain the name and description of you driver, they have to match with the modInfo[i] entries (don't ask me why it has to be defined here and in the bt.cpp).

        <attstr name="name" val="bt 1"></attstr>
        <attstr name="desc" val=""></attstr>

The next three entries are your teams name, your name and the car to use.

        <attstr name="team" val="berniw"></attstr>
        <attstr name="author" val="Bernhard Wymann"></attstr>
        <attstr name="car name" val="cg-nascar-rwd"></attstr>

Now you can choose a racing number and define the color of the drivers entry in the leaders board (if you don't know how to enable the leaders board during the race, hit F1 for help).

        <attnum name="race number" val="61"></attnum>
        <attnum name="red" val="1.0"></attnum>
        <attnum name="green" val="0.0"></attnum>
        <attnum name="blue" val="1.0"></attnum>
      </section>
    </section>
  </section>
</params>

Adding Cars

We will now add two car entries to the file bt.xml (one module is allowed to handle up to ten robots). This will not yet work in the simulation, because we need to change also bt.cpp. You can simply copy the part between <section name="1"> and </section> two times and change the race numbers, the leaders board color if you like, the car you choose and of course the index. To follow the tutorial choose indices 0, 1 and 2 and the names "bt 1", "bt 2" and "bt 3". If you want other cars do

$ ls /usr/local/games/torcs/cars

to get a list of the installed cars. The file bt.xml should finally look similar to this:

<?xml version="1.0" encoding="UTF-8"?>
<!--
    file                 : bt.xml
    created              : Thu Dec 12 02:34:31 CET 2002
    copyright            : (C) 2002 Bernhard Wymann
-->

<!--    This program is free software; you can redistribute it and/or modify  -->
<!--    it under the terms of the GNU General Public License as published by  -->
<!--    the Free Software Foundation; either version 2 of the License, or     -->
<!--    (at your option) any later version.                                   -->

<!DOCTYPE params SYSTEM "../../libs/tgf/params.dtd">

<params name="bt" type="robotdef">
  <section name="Robots">
    <section name="index">

      <section name="0">
        <attstr name="name" val="bt 1"></attstr>
        <attstr name="desc" val=""></attstr>
        <attstr name="team" val="berniw"></attstr>
        <attstr name="author" val="Bernhard Wymann"></attstr>
        <attstr name="car name" val="cg-nascar-rwd"></attstr>
        <attnum name="race number" val="61"></attnum>
        <attnum name="red" val="1.0"></attnum>
        <attnum name="green" val="0.0"></attnum>
        <attnum name="blue" val="1.0"></attnum>
      </section>

      <section name="1">
        <attstr name="name" val="bt 2"></attstr>
        <attstr name="desc" val=""></attstr>
        <attstr name="team" val="berniw"></attstr>
        <attstr name="author" val="Bernhard Wymann"></attstr>
        <attstr name="car name" val="xj-220"></attstr>
        <attnum name="race number" val="62"></attnum>
        <attnum name="red" val="1.0"></attnum>
        <attnum name="green" val="0.0"></attnum>
        <attnum name="blue" val="1.0"></attnum>
      </section>

      <section name="2">
        <attstr name="name" val="bt 3"></attstr>
        <attstr name="desc" val=""></attstr>
        <attstr name="team" val="berniw"></attstr>
        <attstr name="author" val="Bernhard Wymann"></attstr>
        <attstr name="car name" val="155-DTM"></attstr>
        <attnum name="race number" val="63"></attnum>
        <attnum name="red" val="1.0"></attnum>
        <attnum name="green" val="0.0"></attnum>
        <attnum name="blue" val="1.0"></attnum>
      </section>

    </section>
  </section>
</params>

Summary

  • You know how to define cars in bt.xml.
  • You know you can define up to ten cars.
  • To follow the tutorial you have chosen the indices 0, 1 and 2.