asobi_terrain (asobi v0.35.4)

View Source

Pure functional terrain chunk encoding and compression.

Helpers for asobi_terrain_provider implementations that want a compact binary chunk format. A chunk is a grid of tiles; each tile is {TileId, Flags, Elevation}. The default format packs 4 bytes per tile (2B tile id, 1B flags, 1B elevation), so a 64x64 chunk is 16KB raw before compress_chunk/1 (zlib).

A provider typically builds a tile_map/0, then encodes and compresses:

Tiles = #{{0, 0} => {Grass, 0, 0}, {1, 0} => {Water, 0, 3}},
Payload = asobi_terrain:compress_chunk(asobi_terrain:encode_chunk(Tiles)),

Using these helpers is optional: a provider may return any binary its client knows how to decode. The client mirrors decompress_chunk/1 then decode_chunk/1 to read tiles back.

Summary

Functions

Raw (uncompressed) byte size of a chunk in the given format.

zlib-compress a raw chunk binary for storage and transport.

Decode a raw chunk binary back to a tile list. Inverse of encode_chunk/1.

Inverse of compress_chunk/1. Clients call this before decode_chunk/1.

The default chunk format: 4 bytes per tile, 64x64 tiles per chunk.

Encode a tile list (or map) to a raw chunk binary in the default format.

Types

format()

-type format() ::
          #{tile_size := pos_integer(), chunk_width := pos_integer(), chunk_height := pos_integer()}.

tile()

-type tile() ::
          {X :: non_neg_integer(),
           Y :: non_neg_integer(),
           TileId :: non_neg_integer(),
           Flags :: non_neg_integer(),
           Elevation :: non_neg_integer()}.

tile_map()

-type tile_map() ::
          #{{non_neg_integer(), non_neg_integer()} =>
                {non_neg_integer(), non_neg_integer(), non_neg_integer()}}.

Functions

chunk_byte_size/1

-spec chunk_byte_size(format()) -> pos_integer().

Raw (uncompressed) byte size of a chunk in the given format.

compress_chunk(Bin)

-spec compress_chunk(binary()) -> binary().

zlib-compress a raw chunk binary for storage and transport.

decode_chunk(Bin)

-spec decode_chunk(binary()) -> [tile()].

Decode a raw chunk binary back to a tile list. Inverse of encode_chunk/1.

decode_chunk/2

-spec decode_chunk(binary(), format()) -> [tile()].

decompress_chunk(Bin)

-spec decompress_chunk(binary()) -> binary().

Inverse of compress_chunk/1. Clients call this before decode_chunk/1.

default_format()

-spec default_format() -> format().

The default chunk format: 4 bytes per tile, 64x64 tiles per chunk.

encode_chunk(Tiles)

-spec encode_chunk([tile()]) -> binary().

Encode a tile list (or map) to a raw chunk binary in the default format.

encode_chunk/2

-spec encode_chunk([tile()] | tile_map(), format()) -> binary().