// SPDX-FileCopyrightText: 2026 Paula Wisnievski // SPDX-License-Identifier: AGPL-3.0-or-later // what a world is saved as, apart from the files: level.json's text and each chunk's blocks. server/regions.ts puts // the chunks into region files, this is what's inside one chunk's entry before it's compressed, little-endian: // // u8 format, CHUNK_FORMAT // i16 the bottom of the lowest section, the dimension's min_y when it was saved // u32 palette size, then each entry: u8 id length, the id as utf-8, u16 state bits // the sections' blocks and biomes, bottom first, each block the index of its entry in the palette. see // write_column in common/section.ts, which level_chunk uses too // // blocks are saved by their string ids, so mods can come and go without a save's numbers meaning other blocks. every // chunk the game generates is saved whole, like minecraft's, so a change to world generation only shows in chunks never // generated before. a chunk loaded into a dimension with another height keeps the sections both have (see // ServerWorld's #from_saved). // formats 1, 2 and 3 are still read, with each column's biome all the way up. format 3 is this one from before biomes // had heights, see read_column's flat_biomes. they're big-endian and from when the world was 0 to 256 high: after the palette, the // index of every block from y 0 up, then z, then x, u8 each or u16 for more than 256 entries. format 2 then has a u8 // biome palette size, each biome as u8 length and utf-8, and a u8 biome index a column. format 1 has no biomes, but a // bit per block for whether world generation made it, from when only chunks players changed were saved import { CHUNK_AREA } from "@/common/constants.ts"; import { Reader, Writer } from "@/common/bytes.ts"; import { column_biomes, type ColumnBiomes, read_column, Section, SECTION_VOLUME, write_column, } from "@/common/section.ts"; import { SECTION_HEIGHT } from "@/common/world_shape.ts"; export const CHUNK_FORMAT = 4; // how many blocks a chunk of formats 1 and 2 has const FLAT_VOLUME = CHUNK_AREA * 256; export interface SavedChunk { x: number; z: number; data: Uint8Array; } // a world as the game saves it: level.json's text and the chunks that changed since it last saved export interface WorldSave { // level.json, without it the world is new level?: string; chunks: SavedChunk[]; } // a world for the game to load. its chunks are read when they're needed, there can be far too many to keep export interface WorldSource { level?: string; chunks: SavedChunks; } export interface SavedChunks { // where every saved chunk is list: [number, number][]; // a saved chunk's bytes, see encode_chunk. undefined when it can't be read, which generates it again read(chunk_x: number, chunk_z: number): Uint8Array | undefined; } export interface ChunkBlocks { // an id and its state bits palette: [string, number][]; // the bottom of the lowest section min_y: number; // the palette index of each block, a section at a time from min_y up sections: Section[]; // see ServerWorld.biome_at. format 1 chunks don't have them biomes?: ColumnBiomes; } export function encode_chunk(chunk: Required): Uint8Array { const writer = new Writer(); writer.u8(CHUNK_FORMAT); writer.i16(chunk.min_y); writer.u32(chunk.palette.length); for (const [id, state] of chunk.palette) { writer.string(id); writer.u16(state); } write_column(writer, chunk.sections, chunk.biomes); return writer.bytes().slice(); } // throws when the bytes aren't a chunk this can read export function decode_chunk(bytes: Uint8Array): ChunkBlocks { const format = bytes[0]; if (format < 1 || format > CHUNK_FORMAT) throw new Error(`Unknown chunk format ${format}`); // formats 1 and 2 are big-endian, and flat const flat = format <= 2; const reader = new Reader(bytes.subarray(1), !flat); const min_y = flat ? 0 : reader.i16(); if (min_y % SECTION_HEIGHT !== 0) throw new Error(`Bad min_y ${min_y}`); const palette: [string, number][] = []; const palette_size = reader.u32(); for (let i = 0; i < palette_size; i++) palette.push([reader.string(), reader.u16()]); const index = (value: number) => { if (value >= palette_size) throw new Error("Bad palette index"); return value; }; if (!flat) { const { sections, biomes } = read_column(reader, index, format === 3); if (!reader.done) throw new Error("Wrong size"); return { palette, min_y, sections, biomes }; } const blocks = new Uint32Array(FLAT_VOLUME); const wide = palette_size > 256; for (let i = 0; i < FLAT_VOLUME; i++) blocks[i] = index(wide ? reader.u16() : reader.u8()); const sections = Array.from( { length: FLAT_VOLUME / SECTION_VOLUME }, (_, i) => Section.of(blocks.subarray(i * SECTION_VOLUME, (i + 1) * SECTION_VOLUME)), ); let biomes: ColumnBiomes | undefined; if (format === 1) { reader.take(FLAT_VOLUME / 8); } else { const biome_palette: string[] = []; const biome_palette_size = reader.u8(); for (let i = 0; i < biome_palette_size; i++) biome_palette.push(reader.string()); const columns = [...reader.take(CHUNK_AREA)].map((i) => { if (i >= biome_palette_size) throw new Error("Bad biome palette index"); return biome_palette[i]; }); biomes = column_biomes(columns, sections.length); } if (!reader.done) throw new Error("Wrong size"); return { palette, min_y, sections, biomes }; }