Save all generated chunks instead of only chunks that were changed

This commit is contained in:
2026-10-11 02:02:01 -03:00
parent 44a39bf8ec
commit 5a59a63862
17 changed files with 644 additions and 314 deletions
+67 -26
View File
@@ -5,13 +5,17 @@
// 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
// 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
// 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 = 1;
export const CHUNK_FORMAT = 2;
export const CHUNK_VOLUME = CHUNK_AREA * CHUNK_HEIGHT;
export interface SavedChunk {
@@ -20,32 +24,45 @@ export interface SavedChunk {
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
// 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;
// CHUNK_VOLUME bits, see is_changed
changed: Uint8Array;
// each column's, see ServerWorld.biome_at. format 1 chunks don't have them
biomes?: string[];
}
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> {
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 size = 1 + 4 + ids.reduce((sum, id) => sum + 1 + id.length + 2, 0) +
CHUNK_VOLUME * (wide ? 2 : 1) + CHUNK_VOLUME / 8;
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;
@@ -59,15 +76,22 @@ export function encode_chunk(chunk: ChunkBlocks): Uint8Array<ArrayBuffer> {
view.setUint16(offset, state);
offset += 2;
});
for (let i = 0; i < CHUNK_VOLUME; i++) {
if (wide) {
if (wide) {
for (let i = 0; i < CHUNK_VOLUME; i++) {
view.setUint16(offset, chunk.blocks[i]);
offset += 2;
} else {
bytes[offset++] = chunk.blocks[i];
}
} else {
bytes.set(chunk.blocks, offset);
offset += CHUNK_VOLUME;
}
bytes.set(chunk.changed, offset);
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;
}
@@ -76,7 +100,7 @@ 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}`);
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");
@@ -89,19 +113,36 @@ export function decode_chunk(bytes: Uint8Array): ChunkBlocks {
offset += 2;
}
const wide = palette_size > 256;
if (bytes.length !== offset + CHUNK_VOLUME * (wide ? 2 : 1) + CHUNK_VOLUME / 8) {
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);
for (let i = 0; i < CHUNK_VOLUME; i++) {
if (wide) {
if (wide) {
for (let i = 0; i < CHUNK_VOLUME; i++) {
blocks[i] = view.getUint16(offset);
offset += 2;
} else {
blocks[i] = bytes[offset++];
}
} 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");
}
const changed = bytes.slice(offset, offset + CHUNK_VOLUME / 8);
return { palette, blocks, changed };
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 };
}