Files
bworld/tests/chunk_loading_test.ts
T
2026-10-11 01:26:51 -03:00

47 lines
1.9 KiB
TypeScript

// with a worldgen pool, chunks players are sent are generated in its workers: a player that teleports somewhere new
// gets every chunk around them, the same as the game would make them, without the game generating them itself. see
// GameServer.use_worldgen_pool
import { assert, assertEquals } from "@std/assert";
import { is_within_view } from "$/common/chunk_streaming.ts";
import { game_with_pool, load_view } from "./chunk_loading.ts";
// the workers and their ports live until the test closes them
const options = { sanitizeOps: false, sanitizeResources: false };
Deno.test({
name: "a player teleported somewhere new gets every chunk in view from the pool, as the game would make them",
...options,
fn: async () => {
const t = await game_with_pool(2);
try {
t.join(1, "alice");
t.send(1, { type: "view_distance", distance: 5 });
const player = t.game.players()[0];
t.game.world.fallback_callers = new Map();
const fallbacks = t.game.world.fallbacks;
t.game.teleport(player, 10_000.5, 150, 3_000.5);
const generated = t.pool!.generated;
const { chunks } = await load_view(t, 1);
const [cx, cz] = [Math.floor(10_000 / 16), Math.floor(3_000 / 16)];
for (let x = cx - 6; x <= cx + 6; x++) {
for (let z = cz - 6; z <= cz + 6; z++) {
if (!is_within_view(cx, cz, player.chunks.view_distance, x, z)) continue;
const chunk = chunks.get(`${x},${z}`);
assert(chunk, `chunk ${x},${z} wasn't sent`);
assertEquals(chunk.blocks, t.game.world.generated_blocks(x, z), `chunk ${x},${z}`);
}
}
assert(t.pool!.generated - generated > chunks.size, "the pool made them, and their neighbors");
const on_thread = t.game.world.fallbacks - fallbacks;
console.log(
`generated on the game thread: ${on_thread}`,
Object.fromEntries(t.game.world.fallback_callers),
);
assert(on_thread < chunks.size / 4, `${on_thread} chunks generated on the game thread`);
} finally {
t.close();
}
},
});