// 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, CHUNK_HEIGHT, CHUNK_SIZE } 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 { sent_block, test_game } from "./helpers.ts"; Deno.test("a chunk comes back as it was encoded, whatever its sections hold", () => { const blocks = new Uint32Array(CHUNK_AREA * CHUNK_HEIGHT); const section = CHUNK_AREA * 16; // 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 biomes = Array.from({ length: CHUNK_AREA }, (_, i) => i < 100 ? "bworld:plains" : "bworld:desert"); const decoded = decode_level_chunk(encode_level_chunk({ x: -3, z: 70000, blocks, biomes })); assertEquals(decoded.x, -3); assertEquals(decoded.z, 70000); assertEquals(decoded.blocks, blocks); assertEquals(decoded.biomes, biomes); // one biome, and the receiver's numbering const one = decode_level_chunk( encode_level_chunk({ x: 0, z: 0, blocks, biomes: biomes.map(() => "bworld:plains") }), (value) => value === 7 ? 70 : value, ); assertEquals(one.biomes, biomes.map(() => "bworld:plains")); assertEquals(one.blocks[0], 70); assertEquals(one.blocks[section], 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)); assertEquals(chunk.blocks[120 * CHUNK_AREA + 5 * CHUNK_SIZE + 3], 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]); });