Files
bworld/server/game/saves.ts
T
2026-10-12 15:09:22 -03:00

152 lines
5.9 KiB
TypeScript

// 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:
//
// u8 format, CHUNK_FORMAT
// u32 palette size, then each entry: u8 id length, the id as utf-8, u16 state bits
// the palette index of every block, in ServerWorld's order (y, then z, then x): u8 each for up to 256 entries,
// u16 each for more
// u8 biome palette size, then each entry: u8 id length, the id as utf-8
// the biome palette index of every column, u8 each, in the order of a layer's blocks
//
// numbers are big-endian. 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.
// format 1 had no biomes, and a bit per block after the blocks for whether world generation made it, from when only
// chunks players changed were saved. both are read
import { CHUNK_AREA, CHUNK_HEIGHT } from "$/common/constants.ts";
export const CHUNK_FORMAT = 2;
export const CHUNK_VOLUME = CHUNK_AREA * CHUNK_HEIGHT;
export interface SavedChunk {
x: number;
z: number;
data: Uint8Array<ArrayBuffer>;
}
// 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 palette index of each block
blocks: Uint16Array;
// each column's, see ServerWorld.biome_at. format 1 chunks don't have them
biomes?: string[];
}
export function encode_chunk(chunk: Required<ChunkBlocks>): Uint8Array<ArrayBuffer> {
const ids = chunk.palette.map(([id]) => new TextEncoder().encode(id));
if (ids.some((id) => id.length > 255)) throw new Error("A block id is too long to save");
const wide = chunk.palette.length > 256;
const biome_palette = [...new Set(chunk.biomes)];
if (chunk.biomes.length !== CHUNK_AREA || biome_palette.length > 255) throw new Error("Bad biomes");
const biome_ids = biome_palette.map((id) => new TextEncoder().encode(id));
if (biome_ids.some((id) => id.length > 255)) throw new Error("A biome id is too long to save");
const size = 1 + 4 + ids.reduce((sum, id) => sum + 1 + id.length + 2, 0) + CHUNK_VOLUME * (wide ? 2 : 1) +
1 + biome_ids.reduce((sum, id) => sum + 1 + id.length, 0) + CHUNK_AREA;
const bytes = new Uint8Array(size);
const view = new DataView(bytes.buffer);
let offset = 0;
view.setUint8(offset++, CHUNK_FORMAT);
view.setUint32(offset, chunk.palette.length);
offset += 4;
chunk.palette.forEach(([, state], i) => {
view.setUint8(offset++, ids[i].length);
bytes.set(ids[i], offset);
offset += ids[i].length;
view.setUint16(offset, state);
offset += 2;
});
if (wide) {
for (let i = 0; i < CHUNK_VOLUME; i++) {
view.setUint16(offset, chunk.blocks[i]);
offset += 2;
}
} else {
bytes.set(chunk.blocks, offset);
offset += CHUNK_VOLUME;
}
view.setUint8(offset++, biome_palette.length);
for (const id of biome_ids) {
view.setUint8(offset++, id.length);
bytes.set(id, offset);
offset += id.length;
}
for (const biome of chunk.biomes) bytes[offset++] = biome_palette.indexOf(biome);
return bytes;
}
// throws when the bytes aren't a chunk this can read
export function decode_chunk(bytes: Uint8Array): ChunkBlocks {
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
let offset = 0;
const format = view.getUint8(offset++);
if (format !== CHUNK_FORMAT && format !== 1) throw new Error(`Unknown chunk format ${format}`);
const palette_size = view.getUint32(offset);
offset += 4;
if (palette_size > CHUNK_VOLUME) throw new Error("Bad palette");
const palette: [string, number][] = [];
for (let i = 0; i < palette_size; i++) {
const length = view.getUint8(offset++);
const id = new TextDecoder().decode(bytes.subarray(offset, offset + length));
offset += length;
palette.push([id, view.getUint16(offset)]);
offset += 2;
}
const wide = palette_size > 256;
if (format === 1 && bytes.length !== offset + CHUNK_VOLUME * (wide ? 2 : 1) + CHUNK_VOLUME / 8) {
throw new Error("Wrong size");
}
if (bytes.length < offset + CHUNK_VOLUME * (wide ? 2 : 1)) throw new Error("Wrong size");
const blocks = new Uint16Array(CHUNK_VOLUME);
if (wide) {
for (let i = 0; i < CHUNK_VOLUME; i++) {
blocks[i] = view.getUint16(offset);
offset += 2;
}
} else {
blocks.set(bytes.subarray(offset, offset + CHUNK_VOLUME));
offset += CHUNK_VOLUME;
}
for (let i = 0; i < CHUNK_VOLUME; i++) {
if (blocks[i] >= palette_size) throw new Error("Bad palette index");
}
if (format === 1) return { palette, blocks };
const biome_palette: string[] = [];
const biome_palette_size = view.getUint8(offset++);
for (let i = 0; i < biome_palette_size; i++) {
const length = view.getUint8(offset++);
biome_palette.push(new TextDecoder().decode(bytes.subarray(offset, offset + length)));
offset += length;
}
if (bytes.length !== offset + CHUNK_AREA) throw new Error("Wrong size");
const biomes = [...bytes.subarray(offset)].map((index) => {
if (index >= biome_palette_size) throw new Error("Bad biome palette index");
return biome_palette[index];
});
return { palette, blocks, biomes };
}