# Map file format
## Overview
Format is little endian. It has following structure:
|Section      |
|-------------|
|Header       |
|Chunks data  |
|Chunk headers|

Information about data types used in format:
|Name        |Description                                          |
|------------|-----------------------------------------------------|
|char string |Unterminated non-prefixed static size array of bytes |
|uint16      |Unsigned 16-bit integer                              |
|uint32      |Unsigned 32-bit integer                              |
|uint64      |Unsigned 64-bit integer                              |

## Header
File header consists of following fields:
|Name                       |Size (bytes) |Type        |Offset    |Remarks                                                |
|---------------------------|-------------|------------|----------|-------------------------------------------------------|
|File magic                 |7            |char string |0         |It always has value `polymap`                          |
|Format version number      |2            |uint16      |7         |For development version `0xFFFF` number should be used |
|Chunk count                |4            |uint32      |9         |                                                       |
|Chunk headers array offset |8            |uint64      |14        |Offset from beginning of file                          |

## Single chunk data entry
Immediately after header chunk data array is located. Each entry has following structure:
|Name              |Size (bytes)          |Type        |Offset    |Remarks                                        |
|------------------|----------------------|------------|----------|-----------------------------------------------|
|Compression mode  |1                     |uint8       |0         |See *Compression modes* for possible values    |
|Data size         |2                     |uint16      |1         |Size of following compressed data              |
|Data              |varying               |uint8 array |3         |Compressed chunk data of size specified earlier|

### Compression modes
- `0`: No compression. Every node is stored using it's 16-bit ID
- `1`: Efficient Huffman coding. Node count is stored first then node ID, however following encoding is used to
	decrease size: node count is stored in first 7 bits in byte. Last bit is used to denote if count spans to next byte,
	if this is a case then that bit is high (`1`), else it's low (`0`). Because there are only 7 bits available maximum
	value that can be stored is `127`. Situation repeats for next bytes if last bit was `1`. Same applies to node ID.
- `2`: Node grouping. Each node group is consits of following: X, Y, Z (uint8), width, height, depth (uint8), node ID (encoded
	like in *Efficient Huffman coding*)

Compression mode will be selected depending on which mode gives best result in particular case.

## Single chunk header
|Name        |Size (bytes)   |Type        |Offset            |Remarks                                                |
|------------|---------------|------------|------------------|-------------------------------------------------------|
|Chunk Index |4              |uint32      |0                 |Index is calculated in following way: `Z*32*32+Y*32+X` |
|Data offset |8              |uint64      |4                 |Offset of chunk data in *Chunk data* file section      |