Where does the flag land?

Where does the flag land?

The Revive Tanki server has never seen its maps. For mines and falling flags, a stack of grayscale images is enough.

The Revive Tanki server does not simulate physics. It knows neither the geometry of the maps nor their collisions, because in the original game every client computes the movement of its own tank and only reports where it currently is. For almost everything, that is enough. For two things it is not: if you drop a mine in the middle of a jump, you expect it on the ground and not in the air. And if you get shot off a roof while carrying the flag, it should be waiting below. In both cases the server has to know where the ground lies beneath a point, without ever having seen the map.

A tank loses the flag in the middle of a jump. It shows up where the ground was beneath it at that moment.

The answer is an old tool with a small extension: height maps, several of them stacked on top of each other.

The scan

The data is created in the Unity editor, once per map. A tool lays a grid over the entire collision geometry and, from far above, sends a ray straight down at every grid point. The important part is that the ray does not stop at the first hit. Physics.RaycastAll returns every surface it passes on the way: the roof, the floor below it, the ground.

The hits are sorted from top to bottom and filtered. Only the actual level geometry counts, meaning mesh colliders, and two surfaces less than ten centimeters apart are treated as one. What remains for each grid point is a short list of heights.

More than one layer

A classic height map stores exactly one value per point. For terrain that is enough, for a map with bridges, tunnels and accessible roofs it is not, because tanks stand above one another there. So instead of keeping only the first entry of each ray's list, all of them are kept: the topmost hit goes into layer 0, the second into layer 1, the third into layer 2, and so on.

Each layer is stored as its own grayscale image. Black is the lowest point of the layer, white the highest, and the two matching heights sit in a JSON file next to it. In the newer maps a pixel is magenta when the ray did not hit a surface that deep, and the lookup skips it.

On the left the file of Rio's top layer, on the right one area as a top-down view and in layers 0 to 2

On the left, the top layer of Rio exactly as it sits on the server: 2621 by 2621 pixels for an edge length of about 1.3 kilometers, so two pixels per meter. On the right, a 100 meter area, once as a top-down view from the game and then in the first three layers (brightened for display). Layer 0 looks like an aerial photo. Layer 1 only shows what lies underneath something else: the floors under the platforms, the ground under the roofs. In layer 2 almost nothing is left.

It matters what a layer is not: a storey. The number only counts which surface from the top it is at that one spot. The same ground belongs to layer 0 under the open sky, to layer 1 under a roof and to layer 2 under two ceilings.

The lookup

At runtime, all that is left of this is a loop. The server converts the tank's position into pixel coordinates and walks through the layers from top to bottom. The first layer whose surface is not above the tank is the ground it stands on or will fall onto. Slightly simplified:

C#
foreach (HeightMapLayer layer in heightMap.Layers)
{
    if (TryCalculateLayerHeight(tank.Position, heightMap, layer, out float layerHeight))
    {
        if (layerHeight - 1 < tank.Position.Y)
            return tank.Position with { Y = layerHeight - 0.1f };
    }
}

return null;

The one meter of tolerance absorbs the fact that the reported position is not exactly on the surface and that the stored height is only an approximation.

Section through a building with three floors and three tanks, with the check of every layer below

The image is a real section through the building from the video, every bar a sample, its color the layer. Tank A stands on the roof, where the very first layer fits. For B, layer 0 is above it and gets skipped, layer 1 is its ground. C is all the way down and needs three attempts. The counting from the top is visible here too: the ground floor changes color depending on how much lies above it.

What depends on it

Two places in the server use the lookup.

  • Mines. A mine is created at the position of the tank that drops it, projected onto the ground. If the lookup finds no ground, for example because the tank has fallen out of the map, the mine explodes right away instead of hanging in the void forever.
  • Flags. When a flag carrier dies or drops the flag, it lands on the ground beneath them. If there is none, it returns straight to its base.

A tank drives away from a mine it has just placed on the road

Without height maps, all that remains in both cases is the last reported position of the tank, and a flag lost in mid-air hangs there out of reach. That fallback still exists as an option, in case a server should not load the images.

Where it gets imprecise

The method is coarse, and it is worth knowing its limits.

  • Eight bits per height. Only one color channel is read, so there are 256 steps between the lowest and the highest point of a layer. On Rio the top layer spans from the lowest point of the terrain to the top of the high-rises, 246 meters, which makes a single step almost a meter tall. The lower layers cover less height and are more precise at around 40 centimeters.
  • A grid of half a meter. Anything narrower than the distance between two rays can slip through. A ray from above does not see vertical walls at all.

For what depends on it, that is precise enough. Nobody notices a mine that sits slightly too low, but everybody notices a flag twenty meters up in the air.

What I take away

The server does not need to know the map to know where its ground lies. The actual work happens once in the editor, and at runtime it only looks up a pixel. That a single height map is not enough for this was the actual insight, and stacking several of them was the smallest extension that solved the problem.

The game is available on the project's website.