# Legacy:New drivelines

While the description so far is exactly what is supported by the track exporter, the actual concept implemented in STK allows more complicated structure. This is done by maintaining two data structures: one containing the list of all quads, and one describing how the quads are connected with each other, the so called quad graph. The main difference between the two data structures is that one quad in the list of all quads can be used several times in the graph. The graph consists of nodes - as a default, each quad corresponds to a graph node with the same number - this is exactly what the track exporter assumes. But this is not necessary: A part of the track might not be used for a particular race (think of a city track where several different race tracks can be defined), or some quads might be used more than once (e.g. a figure 8 race track: the quad in the 'intersection' would be used twice)

The graph defines for each graph node which quad it belongs to, and which are the possible successors. Usually each quad will have exactly one successor, but in case of a shortcut or branch of the track it might have more than one.

Given the quad definitions from the picture, the following graphs could be defined:

### Simple Loop

A graph with nodes 1 to 29, corresponding to the quads 1 to 29. The order is then

```1 → 2 → 3 → ... → 29 → 1.
``` This defines a simple loop, driven in clockwise direction. The shortcut is not allowed (for the AI, there is no restriction where the players are allowed to drive).

### Reverse Loop

A graph with nodes 1 to 29, corresponding to the quads 1 to 29 and the graph:

```1 → 29 → 28 → ... → 2 → 1.
```

This gives a similar simple loop as the one above, but it is driven in reverse (ie. counter-clockwise).

### Simple Loop with Shortcut

For this we need a graph with 34 nodes, corresponding to the 34 quads. The main loop will be defined as in example 1, but additionally we add:

``` 15 → 30 → 31 ... → 34 → 23
```

This allows the AI to pick any of the two ways (the criteria for which way is picked are not defined yet - atm it's a random selection).

XXXXXXXXX The numbers indicate the number of the quads. This part does not define in which order the quads should be driven, or if the shortcut is to be used or not, it only defines which part of the track can be used. YYYYYYYYYYYYY

### A Loop Inside of the Track

The driveline structures also allows us to define a loop, i.e. forcing the AI to drive a circle before continuing, see picture:

(note that this is most likely an absurd example only - for a track like this to be fair the player karts would have to do the same loop, which means there must be a mean to indicate this to the player, which we currently don't have).

To do this, we need additional graph nodes for the sections that are driven twice. So graph nodes 1 to 34 corresponds to the quads 1 to 34, and then additional graph nodes 35 to 43 which are mapped to the quads 15 to 23 (again). So the quads 15 to 23 have each two graph nodes associated with them, but each graph node still has exactly one quad. Then we can define the following graph:

```1 → 2 → ... 14 → 15 → 16 → ... → 23 → 34 → 33 → ... → 30 → ...
```

This defines the first part of the loop: driving backwards on the shortcut to graph node 30 (which is quad 30). Then we use quad 15 for the second time, so we use the additional graph nodes here:

``` ... 30 → 35  → 36  → ... → 43  → 24 → 25 → ... → 29 → 1
```

In [] are the corresponding quad numbers for the graph nodes 35 to 43. By having the additional graph node layers the AI will simply follow the graph till the end (so there are no loops in the graph, except for starting a new lap), but the mappings of graph nodes to quads force the AI to drive a loop.

## How to create drivelines with the track exporter

The track exporter is able to create quad and graph data structures from specific meshes in the track. While it can't handle all theoretical cases (e.g. a looped track as the last example above is not possible, to do this you have to manually edit the graph file), it can handle all kinds of shortcuts and alternative ways.

### Creating a Shortcut or Alternative Way

If you want to add a shortcut (or alternative way), use a similar structure as for the main driveline, but give the game logic property 'type' the value 'driveline' (as opposed to main-driveline)

The shortcut is added in the picture using again two vertices to indicate where the start point is. The export script will then automatically export the additional quads, and create the graph from the closest quad of the main driveline to the beginning of the shortcut, and from the end of the shortcut to the closest quad.

## How to convert old drivelines to the new format

The old driveline format used two meshes - one for the left side, and one for the right side. To convert these to the new format:

1. For each of the two drivelines remove the edge that crosses the starting line. So instead of having a loop you have two lines, each line having a starting point and an ending point indicating the direction in which the karts are driving (see picture above).
2. Combine the two meshes into a single mesh.
3. Create a new edge connecting the two starting points.
4. Create a new edge connecting the two ending points.
5. Select the two starting vertices, extrude them (vertices only), to indicate where the first quad to drive on is.
6. Add the game logic property 'type', and set it to 'main-driveline'

Now you should be ready to export the track.

## The file format

While it is not at all necessary to know or understand the file format used, here a short description which should help in debugging any problems, or modeling more complex graphs than the default one created by the exporter script.

The created .quad file is plain ASCII, e.g.:

```<?xml version="1.0"?>
...
```

A quad is defined by specifying four coordinates. This can be done either by using four 3d coordinates (as in the first quad line), or by referring to a point of an already defined quad: the latter case uses p0="Q:P" which means: use point P of quad Q as point 0 of the current quad.

Similarly the graph files are plain ASCII, too:

```<?xml version="1.0"?>
<graph>