// SPDX-FileCopyrightText: 2026 Paula Wisnievski // SPDX-License-Identifier: AGPL-3.0-or-later // the server sends the world in whole chunks, like minecraft java: nearest first, in batches the client paces, and // only to players who can see them import { assert, assertEquals } from "@std/assert"; import { CHUNK_AREA } from "@/common/constants.ts"; import { decode_level_chunk, encode_level_chunk, is_within_view } from "@/common/chunk_streaming.ts"; import { PROTOCOL_VERSION } from "@/common/protocol.ts"; import { BIOME_CELL_HEIGHT, ChunkColumn, section_index, to_flat } from "@/common/section.ts"; import { WorldShape } from "@/common/world_shape.ts"; import { sent_block, test_game } from "./helpers.ts"; Deno.test("a chunk comes back as it was encoded, whatever its sections hold", () => { // as tall as a dimension can be made, sections are counted in the message const section = CHUNK_AREA * 16; const blocks = new Uint32Array(section * 24); // section 0 is all one block, 1 has 3 kinds, 2 has 16, 3 has 300 (more than a palette holds), the rest air blocks.fill(7, 0, section); for (let i = 0; i < section; i++) { blocks[section + i] = [1, 2 | (3 << 16), 5][i % 3]; blocks[2 * section + i] = i % 16; blocks[3 * section + i] = (i % 300) | ((i % 7) << 16); } const shape = new WorldShape({ min_y: -64, max_y: 320, sky_light: true }); // two biomes across, and a third in the cells from the 40th up const biomes = Array.from( { length: shape.volume / BIOME_CELL_HEIGHT }, (_, i) => i >= 40 * CHUNK_AREA ? "test:caves" : i % CHUNK_AREA < 100 ? "bworld:plains" : "bworld:desert", ); const column = ChunkColumn.from_flat(-3, 70000, shape, blocks, biomes); const decoded = decode_level_chunk(encode_level_chunk(column)); assertEquals(decoded.x, -3); assertEquals(decoded.z, 70000); assertEquals(to_flat(decoded.sections), blocks); assertEquals(decoded.sections.map((section) => section.blocks === null).indexOf(false), 1, "uniform ones stay so"); assertEquals(decoded.biomes, column.biomes); const back = new ChunkColumn(-3, 70000, shape, decoded.sections, decoded.biomes); assertEquals(back.biome_at(-48 + 3, 95, 70000 * 16 + 2), "bworld:plains"); assertEquals(back.biome_at(-48 + 3, 96, 70000 * 16 + 2), "test:caves"); // one biome, and the receiver's numbering const one = decode_level_chunk( encode_level_chunk(ChunkColumn.from_flat(0, 0, shape, blocks, new Array(CHUNK_AREA).fill("bworld:plains"))), (value) => value === 7 ? 70 : value, ); assertEquals(one.biomes.palette, ["bworld:plains"]); assertEquals(one.biomes.sections, new Array(24).fill(0)); assertEquals(one.sections[0].value, 70); assertEquals(one.sections[1].get(0), 1); }); Deno.test("the view is round, with a ring of chunks past it for meshing its edge", () => { assert(is_within_view(0, 0, 2, 0, 0)); assert(is_within_view(0, 0, 2, 3, 0), "the outer ring"); assert(!is_within_view(0, 0, 2, 3, 0, false)); assert(!is_within_view(0, 0, 2, 4, 0)); assert(is_within_view(0, 0, 2, 2, 2)); assert(!is_within_view(0, 0, 2, 3, 3), "corners are cut off"); assert(is_within_view(10, -10, 2, 12, -12)); }); // every chunk around cx, cz a view distance of 2 covers function view(cx: number, cz: number) { const chunks: string[] = []; for (let x = cx - 4; x <= cx + 4; x++) { for (let z = cz - 4; z <= cz + 4; z++) { if (is_within_view(cx, cz, 2, x, z)) chunks.push(`${x},${z}`); } } return chunks.sort(); } Deno.test("chunks come in batches, nearest first, as fast as the client says it can take them", async () => { const { game, take, send } = await test_game("mods"); game.on_connect(1); send(1, { type: "hello", name: "alice", protocol: PROTOCOL_VERSION }); send(1, { type: "ready", view_distance: 2 }); send(1, { type: "move", x: 8, y: 100, z: 8, yaw: 0, pitch: 0 }); const joined = take(1); assert(joined.some((m) => m.type === "set_center_chunk" && m.x === 0 && m.z === 0)); assert(!joined.some((m) => m.type === "level_chunk"), "chunks come with ticks"); // the first batch before the client has said anything: minecraft's 9 chunks game.tick(); let messages = take(1).filter((m) => m.type.includes("chunk")); assertEquals(messages[0].type, "chunk_batch_start"); assertEquals(messages.at(-1), { type: "chunk_batch_finished", size: 9 }); const first = messages.filter((m) => m.type === "level_chunk"); assertEquals(first.length, 9); assertEquals(`${first[0].x},${first[0].z}`, "0,0"); for (const chunk of first) assert(Math.abs(chunk.x) <= 1 && Math.abs(chunk.z) <= 1, "nearest first"); // nothing more until the client has it game.tick(); assertEquals(take(1).filter((m) => m.type === "level_chunk"), []); // then the rest, each chunk in view once const sent = first.map((m) => `${m.x},${m.z}`); for (let i = 0; i < 20; i++) { send(1, { type: "chunk_batch_received", chunks_per_tick: 4 }); game.tick(); messages = take(1).filter((m) => m.type === "level_chunk"); assert(messages.length <= 4); sent.push(...messages.map((m) => `${m.x},${m.z}`)); } assertEquals(sent.sort(), view(0, 0)); // walking off: a new center, the chunks out of view are forgotten and the new ones come send(1, { type: "move", x: 8 + 16 * 3, y: 100, z: 8, yaw: 0, pitch: 0 }); messages = take(1); assertEquals(messages.find((m) => m.type === "set_center_chunk"), { type: "set_center_chunk", x: 3, z: 0 }); const forgotten = messages.filter((m) => m.type === "forget_level_chunk").map((m) => `${m.x},${m.z}`); assertEquals(forgotten.sort(), view(0, 0).filter((key) => !view(3, 0).includes(key))); const more: string[] = []; for (let i = 0; i < 20; i++) { send(1, { type: "chunk_batch_received", chunks_per_tick: 64 }); game.tick(); more.push(...take(1).filter((m) => m.type === "level_chunk").map((m) => `${m.x},${m.z}`)); } assertEquals(more.sort(), view(3, 0).filter((key) => !view(0, 0).includes(key))); }); Deno.test("block changes only go to players who have the chunk, the others get them with it", async () => { const { game, take, send, join } = await test_game("mods"); const alice = join(1, "alice"); send(1, { type: "move", x: 8, y: 100, z: 8, yaw: 0, pitch: 0 }); const bob = join(2, "bob"); send(2, { type: "move", x: 8 + 16 * 100, y: 100, z: 8, yaw: 0, pitch: 0 }); game.tick(); take(1); take(2); game.set_block(3, 120, 5, "bworld:stone"); assert(take(1).some((m) => m.type === "set_block" && m.x === 3 && m.id === "bworld:stone")); assertEquals(take(2).filter((m) => m.type === "set_block"), [], "bob is too far away to have it"); // bob walks over and gets the chunk with the stone in it send(2, { type: "move", x: 8, y: 100, z: 8, yaw: 0, pitch: 0 }); send(2, { type: "chunk_batch_received", chunks_per_tick: 9 }); game.tick(); assertEquals(sent_block(take(2), bob, 3, 120, 5), ["bworld:stone", 0]); // the chunk the server sends is what it has, with the ids join listed assertEquals(alice.blocks[game.world.block_ids["bworld:stone"]], "bworld:stone"); const chunk = decode_level_chunk(game.world.level_chunk(0, 0)); const y = 120 - game.world.shape.min_y; assertEquals(chunk.sections[y >> 4].get(section_index(3, y, 5)), game.world.get_block_value(3, 120, 5)); }); Deno.test("/viewdistance caps every player's view distance, and raising it gives them what they asked for", async () => { const t = await test_game("mods"); t.join(1, "alice"); const player = t.game.players()[0]; t.send(1, { type: "view_distance", distance: 12 }); assertEquals(player.chunks.view_distance, 12); t.send(1, { type: "chat", text: "/viewdistance 4" }); assertEquals([t.game.view_distance, player.chunks.view_distance], [4, 4]); t.send(1, { type: "chat", text: "/viewdistance 32" }); assertEquals([t.game.view_distance, player.chunks.view_distance], [32, 12]); });