// SPDX-FileCopyrightText: 2026 Paula Wisnievski // SPDX-License-Identifier: AGPL-3.0-or-later // dimensions: the world is generated by the default dimension's noise settings, exactly one mod has one, and players // spawn on ground that has collision import { assert, assertEquals, assertRejects } from "@std/assert"; import { CHUNK_AREA } from "@/common/constants.ts"; import { decode_level_chunk } from "@/common/chunk_streaming.ts"; import { ItemStack } from "@/common/inventory.ts"; import type { DimensionJson } from "@/common/worldgen/data.ts"; import { load_and_check } from "@/tools/check_mods.ts"; import { copy_dir, test_game } from "./helpers.ts"; // flat's ground: glass up to y 69, only east of x 16. west of it is open sea up to y 59 const GROUND_TOP = 69; const SEA_TOP = 59; const GROUND_FROM_X = 16; // mods/bworld, and flat when flat is true: a generator of its own and a dimension using it. which mods say their // dimension is the default is up to the test, and so are the flat dimension's shape, what its ground is made of and // its generator's cell height function mods(options: { flat: boolean; bworld_default: boolean; flat_default?: boolean; shape?: Pick; ground?: string; surface_rule?: unknown; cell_height?: number; }) { const dir = Deno.makeTempDirSync({ prefix: "bworld_dimensions_" }); copy_dir("mods/bworld", `${dir}/bworld`); const bworld_dimension = `${dir}/bworld/worldgen/dimension/overworld.json`; const json = JSON.parse(Deno.readTextFileSync(bworld_dimension)); json.dimension.default = options.bworld_default; Deno.writeTextFileSync(bworld_dimension, JSON.stringify(json)); if (!options.flat) return dir; const write = (path: string, json: unknown) => { Deno.mkdirSync(`${dir}/flat/${path.split("/").slice(0, -1).join("/")}`, { recursive: true }); Deno.writeTextFileSync(`${dir}/flat/${path}`, JSON.stringify(json)); }; write("manifest.json", { format_version: 1, id: "flat", name: "Flat", version: "1.0.0", dependencies: [{ id: "bworld", version: "*" }], }); write("worldgen/biome_source/flat.json", { format_version: 1, biome_source: { id: "flat:flat", rules: [{ id: "flat:beach", biome: "bworld:beach" }] }, }); // solid where both the y and the x parts are above 0 const below = (top: number) => ({ type: "sub", argument1: top + 0.5, argument2: { type: "y" } }); write("worldgen/noise_settings/flat.json", { format_version: 1, noise_settings: { id: "flat:flat", sea_level: SEA_TOP + 1, default_block: options.ground ?? "bworld:glass", cell_width: 1, terrain: { density: { type: "min", argument1: below(GROUND_TOP), argument2: { type: "sub", argument1: { type: "x" }, argument2: GROUND_FROM_X - 0.5 }, }, cell_height: options.cell_height ?? 1, }, caves: { density: 1, cell_height: options.cell_height ?? 1 }, preliminary_surface: GROUND_TOP + 1, climate: {}, steep: 100, biome_source: "flat:flat", fluids: [{ block: "bworld:water", below: SEA_TOP + 1, where: "open_sky" }], surface_rule: options.surface_rule ?? { type: "block", result_state: { Name: options.ground ?? "bworld:glass" } }, }, }); write("worldgen/dimension/flat.json", { format_version: 1, dimension: { id: "flat:flat", generator: "flat:flat", default: options.flat_default ?? true, ...options.shape }, }); return dir; } Deno.test("the world is generated by the default dimension's noise settings, from any mod", async () => { const dir = mods({ flat: true, bworld_default: false }); const { game } = await test_game(dir); Deno.removeSync(dir, { recursive: true }); const world = game.world; for (const z of [-20, 0, 37]) { assertEquals(world.get_block_id(40, GROUND_TOP, z), "bworld:glass"); assertEquals(world.get_block_id(40, GROUND_TOP + 1, z), "cubi:air"); assertEquals(world.get_block_id(-40, SEA_TOP, z), "bworld:water"); assertEquals(world.get_block_id(-40, SEA_TOP + 1, z), "cubi:air"); } }); Deno.test("a surface rule's density condition holds where its density function is in range", async () => { const block = (id: string) => ({ type: "block", result_state: { Name: id } }); const dir = mods({ flat: true, bworld_default: false, // glass where x is from 30 to 50, and below y 60 too surface_rule: { type: "sequence", sequence: [ { type: "condition", if_true: { type: "density", function: { type: "x" }, min_inclusive: 30, max_exclusive: 50 }, then_run: block("bworld:glass"), }, { type: "condition", if_true: { type: "density", function: { type: "y" }, max_exclusive: 60 }, then_run: block("bworld:glass"), }, block("bworld:stone"), ], }, }); const { game } = await test_game(dir); Deno.removeSync(dir, { recursive: true }); const at = (x: number, y: number) => game.world.get_block_id(x, y, 5); assertEquals([29, 30, 49, 50].map((x) => at(x, GROUND_TOP)), [ "bworld:stone", "bworld:glass", "bworld:glass", "bworld:stone", ]); assertEquals([at(60, 60), at(60, 59)], ["bworld:stone", "bworld:glass"]); }); Deno.test("players spawn on ground with collision, not on the water in the middle of the world", async () => { const dir = mods({ flat: true, bworld_default: false }); const { game, join } = await test_game(dir); Deno.removeSync(dir, { recursive: true }); const { x, y, z } = join(1, "alice").spawn; assert(x >= GROUND_FROM_X, `spawned at x ${x}, over the water`); assertEquals(y, GROUND_TOP + 1); assert(game.world.get_block_info(Math.floor(x), y - 1, Math.floor(z))?.has_collision); }); Deno.test("the server doesn't start without a default dimension, or with two", async () => { const none = mods({ flat: true, bworld_default: false, flat_default: false }); await assertRejects(() => test_game(none), Error, "no mod has a default dimension"); Deno.removeSync(none, { recursive: true }); const two = mods({ flat: true, bworld_default: true }); await assertRejects( () => test_game(two), Error, "only one dimension can be the default, and these all are: bworld:overworld (bworld), flat:flat (flat)", ); Deno.removeSync(two, { recursive: true }); }); // flat's ground is from x 16 on, x 40 is in chunk 2 at 8 across const GROUND_X = 40; const TALL = { min_y: -64, max_y: 320 }; Deno.test("a dimension's blocks go from its min_y to max_y - 1, and are sent and saved that way", async () => { const dir = mods({ flat: true, bworld_default: false, shape: TALL }); const { game, join } = await test_game(dir); const world = game.world; const alice = join(1, "alice"); assertEquals(alice.shape, { ...TALL, sky_light: true }); assertEquals(alice.spawn.y, GROUND_TOP + 1, "on the ground, looking down from the top"); assertEquals(world.get_block_id(GROUND_X, -64, 0), "bworld:glass", "generated down to the bottom"); for (const y of [-65, -64, 319, 320]) game.set_block(GROUND_X, y, 0, "bworld:stone"); assertEquals(world.get_block_id(GROUND_X, -65, 0), "cubi:air"); assertEquals(world.get_block_id(GROUND_X, 320, 0), "cubi:air"); // what clients get: every block from the bottom up const chunk = decode_level_chunk(world.level_chunk(2, 0)); assertEquals(chunk.sections.length, 384 / 16); assertEquals(chunk.sections[0].get(8), world.get_block_value(GROUND_X, -64, 0)); assertEquals(chunk.sections[23].get(15 * CHUNK_AREA + 8), world.get_block_value(GROUND_X, 319, 0)); // a column of ground: glass up to 69 and air above, all one block in every section but the one with the ground's // top in it const generated = world.generated_blocks(2, 0); assertEquals(generated.sections.map((section) => section.blocks !== null).indexOf(true), (64 + 64) / 16); assertEquals(generated.sections.filter((section) => section.blocks !== null).length, 1); const loaded = await test_game(dir, game.save()); Deno.removeSync(dir, { recursive: true }); for (const y of [-64, 319]) assertEquals(loaded.game.world.get_block_id(GROUND_X, y, 0), "bworld:stone"); }); Deno.test("a chunk saved at another height keeps the sections both heights have", async () => { const tall = mods({ flat: true, bworld_default: false, shape: TALL }); const { game } = await test_game(tall); for (const y of [-64, 0, 255, 319]) game.set_block(GROUND_X, y, 0, "bworld:stone"); const saved = game.save(); const short = mods({ flat: true, bworld_default: false }); const { game: shorter } = await test_game(short, saved); for (const y of [0, 255]) assertEquals(shorter.world.get_block_id(GROUND_X, y, 0), "bworld:stone"); assertEquals(shorter.world.get_block_id(GROUND_X, 254, 0), "cubi:air"); shorter.set_block(GROUND_X, 1, 0, "bworld:stone"); // and back, where what the shorter one didn't keep is air, like a dimension that grew const { game: again } = await test_game(tall, shorter.save()); for (const dir of [tall, short]) Deno.removeSync(dir, { recursive: true }); for (const y of [0, 1, 255]) assertEquals(again.world.get_block_id(GROUND_X, y, 0), "bworld:stone"); for (const y of [-64, 319]) assertEquals(again.world.get_block_id(GROUND_X, y, 0), "cubi:air"); }); Deno.test("under a dimension's ground it's dark, above its top it's open sky, and things fall out of its bottom", async () => { const dir = mods({ flat: true, bworld_default: false, shape: TALL, ground: "bworld:stone" }); const { game } = await test_game(dir); Deno.removeSync(dir, { recursive: true }); const world = game.world; assertEquals(world.light_at(GROUND_X, -60, 0), { sky: 0, block: 0 }); assertEquals(world.light_at(GROUND_X, 319, 0), { sky: 15, block: 0 }); assertEquals(world.light_at(GROUND_X, 400, 0), { sky: 15, block: 0 }); // the sea side has nothing under the water, items sink out of the world 64 blocks under its bottom const stays = game.entities.spawn_item(-40, -64 - 60, 0, new ItemStack("bworld:stone")); const falls = game.entities.spawn_item(-40, -64 - 70, 0, new ItemStack("bworld:stone")); stays.tick(); falls.tick(); assert(!stays.removed); assert(falls.removed); }); Deno.test("in a dimension without sky light only blocks give light", async () => { const dir = mods({ flat: true, bworld_default: false, shape: { sky_light: false } }); const { game, join } = await test_game(dir); Deno.removeSync(dir, { recursive: true }); assertEquals(join(1, "alice").shape, { min_y: 0, max_y: 256, sky_light: false }); const world = game.world; assertEquals(world.light_at(GROUND_X, GROUND_TOP + 5, 0), { sky: 0, block: 0 }, "in the open"); assertEquals(world.light_at(GROUND_X, 300, 0), { sky: 0, block: 0 }, "above the world"); game.set_block(GROUND_X, GROUND_TOP + 1, 0, "bworld:light"); assertEquals(world.light_at(GROUND_X, GROUND_TOP + 2, 0).sky, 0); assert(world.light_at(GROUND_X, GROUND_TOP + 2, 0).block > 0, "lit by the light block"); }); Deno.test("a dimension's min_y and max_y have to be whole sections, and fit its generator's cells", async () => { const errors = async (options: Parameters[0]) => { const dir = mods(options); const report = (await load_and_check(dir)).find((mod) => mod.id === "flat")!.report; Deno.removeSync(dir, { recursive: true }); return report.errors.filter((error) => error.includes("worldgen/dimension/flat.json")); }; assertEquals(await errors({ flat: true, bworld_default: false, shape: TALL }), []); for (const shape of [{ min_y: -8 }, { max_y: 0 }, { min_y: -4096 }, { max_y: 4096 }]) { assertEquals((await errors({ flat: true, bworld_default: false, shape })).length, 1, JSON.stringify(shape)); } // sections of 16, but the generator samples every 32 blocks const shape = { min_y: -48, max_y: 320 }; assert((await errors({ flat: true, bworld_default: false, shape, cell_height: 32 }))[0].includes("cell_height 32")); const dir = mods({ flat: true, bworld_default: false, shape, cell_height: 32 }); await assertRejects(() => test_game(dir), Error, "flat.json: can't be generated by flat:flat"); Deno.removeSync(dir, { recursive: true }); });