asobi_terrain (asobi v0.35.4)
View SourcePure 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
-type format() :: #{tile_size := pos_integer(), chunk_width := pos_integer(), chunk_height := pos_integer()}.
-type tile() :: {X :: non_neg_integer(), Y :: non_neg_integer(), TileId :: non_neg_integer(), Flags :: non_neg_integer(), Elevation :: non_neg_integer()}.
-type tile_map() :: #{{non_neg_integer(), non_neg_integer()} => {non_neg_integer(), non_neg_integer(), non_neg_integer()}}.
Functions
-spec chunk_byte_size(format()) -> pos_integer().
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.
-spec default_format() -> format().
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.