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
+88
View File
@@ -11,6 +11,9 @@ import { load_bmods } from "$/server/load_bmods.ts";
import { start_host } from "$/server/host.ts";
import { copy_dir } from "./helpers.ts";
import { PROTOCOL_VERSION } from "$/common/protocol.ts";
import { decode_level_chunk, type LevelChunk } from "$/common/chunk_streaming.ts";
import { decode_chunk, encode_chunk } from "$/server/game/saves.ts";
import { list_chunks, read_chunk, write_chunks } from "$/server/regions.ts";
const ENGINE_TEXTURES = ["engine:missing", ...Array.from({ length: 9 }, (_, i) => `engine:break_${i}`)];
@@ -324,6 +327,91 @@ Deno.test({
},
});
// joins a host as alice with view distance 4, until she has every chunk in view
async function join_host(port: number): Promise<LevelChunk[]> {
const socket = new WebSocket(`ws://localhost:${port}/ws`);
socket.binaryType = "arraybuffer";
const chunks: LevelChunk[] = [];
await new Promise<void>((resolve, reject) => {
const timer = setTimeout(() => reject(new Error(`only ${chunks.length} chunks came`)), 60_000);
socket.onmessage = (event) => {
if (typeof event.data !== "string") {
chunks.push(decode_level_chunk(new Uint8Array(event.data)));
return;
}
const message = JSON.parse(event.data);
if (message.type === "welcome") socket.send(JSON.stringify({ type: "ready", view_distance: 4 }));
if (message.type === "chunk_batch_finished") {
socket.send(JSON.stringify({ type: "chunk_batch_received", chunks_per_tick: 64 }));
if (chunks.length >= 49) {
clearTimeout(timer);
resolve();
}
}
};
socket.onopen = () => socket.send(JSON.stringify({ type: "hello", name: "alice", protocol: PROTOCOL_VERSION }));
});
const closed = new Promise((resolve) => socket.onclose = resolve);
socket.close();
await closed;
return chunks;
}
Deno.test({
name: "a real host saves every chunk it generates, and players get them from the save after it starts again",
sanitizeOps: false,
sanitizeResources: false,
fn: async () => {
const { sources, folder } = await packed_mods();
const build = Deno.makeTempDirSync({ prefix: "bworld_build_" });
Deno.mkdirSync(`${build}/assets/textures`, { recursive: true });
Deno.writeTextFileSync(
`${build}/assets/textures/index.json`,
JSON.stringify(ENGINE_TEXTURES.slice(1).map((id) => id.split(":")[1])),
);
const run = async () => {
const host = await start_host({
build_dir: build,
server_mods_dir: folder,
world_dir: `${build}/world`,
seed: "saving-seed",
worldgen_workers: 2,
on_fatal: (message) => {
throw new Error(message);
},
});
const server = Deno.serve({ port: 0, onListen: () => {} }, host.handle);
try {
return await join_host(server.addr.port);
} finally {
await server.shutdown();
await host.shutdown();
}
};
try {
const first = await run();
const region = `${build}/world/region`;
const saved = await list_chunks(region);
for (const chunk of first) assert(saved.some(([x, z]) => x === chunk.x && z === chunk.z));
// a block at the top of every saved chunk, which generation never puts there
await write_chunks(
region,
await Promise.all(saved.map(async ([x, z]) => {
const chunk = decode_chunk((await read_chunk(region, x, z))!);
chunk.blocks[255 * 256] = chunk.palette.push(["bworld:stone", 0]) - 1;
return { x, z, data: encode_chunk(chunk as Required<typeof chunk>) };
})),
);
const second = await run();
assert(second.length >= 49);
for (const chunk of second) assert(chunk.blocks[255 * 256] !== 0, `${chunk.x},${chunk.z} is from the save`);
} finally {
for (const dir of [sources, folder, build]) Deno.removeSync(dir, { recursive: true });
}
},
});
// write_bmod without anything optional still makes a file read_bmod takes
Deno.test("a mod with only data packs and reads", () => {
const bmod = {