Files
bworld/server/game/saves.ts
T
2026-09-27 23:45:07 -03:00

108 lines
3.8 KiB
TypeScript

// 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
// a bit per block, set where it isn't what world generation makes there, like a block a player placed
//
// 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
import { CHUNK_AREA, CHUNK_HEIGHT } from "$/common/constants.ts";
export const CHUNK_FORMAT = 1;
export const CHUNK_VOLUME = CHUNK_AREA * CHUNK_HEIGHT;
export interface SavedChunk {
x: number;
z: number;
data: Uint8Array<ArrayBuffer>;
}
// a world as the host reads it and the game saves it. the game only saves the chunks that changed since it last did
export interface WorldSave {
// level.json, without it the world is new
level?: string;
chunks: SavedChunk[];
}
export interface ChunkBlocks {
// an id and its state bits
palette: [string, number][];
// the palette index of each block
blocks: Uint16Array;
// CHUNK_VOLUME bits, see is_changed
changed: Uint8Array;
}
export function is_changed(chunk: ChunkBlocks, index: number) {
return (chunk.changed[index >> 3] & (1 << (index & 7))) !== 0;
}
export function encode_chunk(chunk: 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 size = 1 + 4 + ids.reduce((sum, id) => sum + 1 + id.length + 2, 0) +
CHUNK_VOLUME * (wide ? 2 : 1) + CHUNK_VOLUME / 8;
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;
});
for (let i = 0; i < CHUNK_VOLUME; i++) {
if (wide) {
view.setUint16(offset, chunk.blocks[i]);
offset += 2;
} else {
bytes[offset++] = chunk.blocks[i];
}
}
bytes.set(chunk.changed, offset);
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) 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 (bytes.length !== offset + CHUNK_VOLUME * (wide ? 2 : 1) + CHUNK_VOLUME / 8) {
throw new Error("Wrong size");
}
const blocks = new Uint16Array(CHUNK_VOLUME);
for (let i = 0; i < CHUNK_VOLUME; i++) {
if (wide) {
blocks[i] = view.getUint16(offset);
offset += 2;
} else {
blocks[i] = bytes[offset++];
}
if (blocks[i] >= palette_size) throw new Error("Bad palette index");
}
const changed = bytes.slice(offset, offset + CHUNK_VOLUME / 8);
return { palette, blocks, changed };
}