# .ter Format

## Overview

Terrain files (*.ter) represent a single terrain to be used by a world. This file format is generated by ZeroEditor and used during the munge process to create the tern chunk within *.lvl files.

## Structure

Name | Size (bytes) | Description
--- | --- | ---
Terrain Header | 2820/2821 (v21/22) | Header storing general information about the terrain.
Height Block | size * size * 2 | Height grid data.
Color Block | size * size * 4 | RGBA colors for the terrain grid.
Color Block | size * size * 4 | RGBA colors for the terrain grid.
Texture Block | size * size * 16 | Texture layer opacity for the terrain grid.
? | size * size / 2 | Maybe Water/Foliage.
? | size * size / 2 | Probably Water.
? | Variable | Not sure what this contains but it seems that this block is not required.

## Terrain Header

The Terrain Header is always 2821 bytes in length.

Data Type | Size (bytes) | Description
--- | --- | ---
`byte [4]` | 4 | Terrain Header Marker (TERR).
`long int` | 4 | .TER file version number (21 for SWBF, 22 for SWBFII).
`short int [4]` | 8 | Terrain extents. Example: -8, -8, 8, 8 for a 16x16 map.
`long int` | 4 | ? (ie. 164)
`float [16]` | 64 | Tile-range for each texture layer. _0.031 (1/32): the texture spans 32 meters. This is stored as 1/X of the value in ZeroEditor.
`byte [16]` | 16 | Mapping for each texture-layer. 0 is normal.
`byte [64]` | 64 | Unknown. Possibly 16 floats for texture-layer rotations.
`float` | 4 | Map height multiplier.
`float` | 4 | Grid-scale. Distance between points.
`long int` | 4 | ? (1)
`long int` | 4 | Full map size stored in the .TER file. The same or bigger than the map extents.
`long int` | 4 | ? (2)
`byte` | 1 | ? (0F) Only exists in SWBF II (version 22) terrain.
`TextureLayer [16]` | 1024 | Texture layers (see below).
`WaterLayer [16]` | 1086 | Water layers (see below).
`byte [524]` | 524 | Unknown.

## Terrain Blocks

The Terrain Header is 2820 (SWBF) or 2821 (SWBFII) bytes in length.

Name | Data Type | Size
--- | --- | ---
Height | `signed short` | Full map size * Full map size * 2
|  | Height value for every point on the grid. This value will be multiplied with the map scale multiplier.
Color | `byte [4]` | Full map size * Full map size * 4
|  | Color values for every point on the grid. 4 bytes (from 0 to 255) corresponding to the RGBA channels.
Color 2 | `byte [4]` | Full map size * Full map size * 4
|  | Color values for every point on the grid. 4 bytes (from 0 to 255) corresponding to the RGBA channels.
Texture | `byte [16]` | Full map size * Full map size * 16
|  | One byte (0-255) for each texture layer indicating the transparency of the corresponding texture layer.
Water | `byte` | Full map size * Full map size / 2
|  | Structure unknown.
Foliage | `byte` | Full map size * Full map size / 2
|  | Structure unknown.
Unknwon | `byte` | Variable
|  | Structure unknown.

## Terrain Structs

### TextureLayer

Data Type | Size (bytes) | Description
--- | --- | ---
`char [32]` | 32 | Diffuse texture name.
`char [32]` | 32 | Detail texture name.

### WaterLayer

Data Type | Size (bytes) | Description
--- | --- | ---
`float [2]` | 8 | Water height value (twice).
`byte [8]` | 8 | Unknown, always zero.
`float [2]` | 8 | UV animation velocity.
`float [2]` | 8 | UV animation repeat.
`byte [4]` | 4 | RGBA color values.
`char [32]` | 32 | Water texture name.

### End-of-file Block

Can be empty

Data Type | Size (bytes) | Description
--- | --- | ---
`int` | 4 | Length of the block.
`int` | 4 | Number of items. Can be zero, in which case there are no further fields and this block ends.
`int` | 4 | Number of (sub-)items?