// SPDX-FileCopyrightText: 2026 Paula Wisnievski // SPDX-License-Identifier: AGPL-3.0-or-later // features are code or data, and use each other: data features with types and modifiers scripts register, scripts // placing data features. they run by step, then mod, data before code. see docs/modding/worldgen/index.md import { assert, assertEquals, assertRejects } from "@std/assert"; import { CHUNK_AREA, CHUNK_HEIGHT, CHUNK_SIZE } from "$/common/constants.ts"; import { generate_raw_chunk } from "$/common/generation.ts"; import { copy_dir, test_game } from "./helpers.ts"; const SCRIPT = ` import type { WorldgenContext } from "bworld/worldgen"; export function setup(gen: WorldgenContext) { // a type for data features: a pillar of a block gen.register_feature_type<{ block: string; height: number }>("testmod:pillar", (chunk, config, origin) => { for (let i = 0; i < config.height; i++) chunk.set_block(origin.x, origin.y + i, origin.z, config.block); return true; }); // a modifier for them: the middle of the chunk gen.register_placement_modifier("testmod:middle", (chunk, _config, positions) => positions.map((p) => ({ x: chunk.x * 16 + 8, y: p.y, z: chunk.z * 16 + 8 })) ); // the same step as the data pillar, so after it: a mod's data features go first gen.register_feature("testmod:count_glass", (chunk) => { let glass = 0; for (let y = 0; y < 256; y++) if (chunk.get_block(chunk.x * 16 + 8, y, chunk.z * 16 + 8) === "bworld:glass") glass++; (globalThis as any).glass_seen = glass; }, { step: "raw_generation" }); // code placing a data feature where it likes gen.register_feature("testmod:place_one", (chunk) => { chunk.place_feature("testmod:glass_pillar", chunk.x * 16 + 2, 200, chunk.z * 16 + 2); }); } `; // mods/bworld and a test mod with files, in a temp folder function with_mod(files: Record, script = SCRIPT) { const dir = Deno.makeTempDirSync({ prefix: "bworld_features_" }); copy_dir("mods/bworld", `${dir}/bworld`); const write = (path: string, content: string) => { Deno.mkdirSync(`${dir}/testmod/${path.slice(0, path.lastIndexOf("/"))}`, { recursive: true }); Deno.writeTextFileSync(`${dir}/testmod/${path}`, content); }; write( "manifest.json", JSON.stringify({ format_version: 1, id: "testmod", name: "Test", version: "1.0.0", dependencies: [{ id: "bworld", version: "*" }], scripts: { worldgen: "scripts/worldgen.ts" }, }), ); write("scripts/worldgen.ts", script); for (const [path, json] of Object.entries(files)) write(path, JSON.stringify({ format_version: 1, feature: json })); return dir; } const GLASS_PILLAR = { id: "testmod:glass_pillar", type: "testmod:pillar", config: { block: "bworld:glass", height: 3 }, placement: [{ type: "testmod:middle" }, { type: "minecraft:heightmap", heightmap: "WORLD_SURFACE_WG" }], step: "raw_generation", }; // built in: a few two block columns of cobblestone on the ground const COBBLE = { id: "testmod:cobble_columns", type: "minecraft:block_column", config: { layers: [{ height: 2, provider: "bworld:cobblestone" }] }, placement: [ { type: "minecraft:count", count: 4 }, { type: "minecraft:in_square" }, { type: "minecraft:heightmap", heightmap: "OCEAN_FLOOR_WG" }, ], step: "underground_decoration", }; Deno.test("data features use types and modifiers scripts register, scripts place data features, all by step", async () => { const dir = with_mod({ "worldgen/features/glass_pillar.json": GLASS_PILLAR, "worldgen/features/cobble_columns.json": COBBLE, }); try { const { game } = await test_game(dir); const { block_ids, worldgen, default_values } = game.world; assertEquals(worldgen.features.map((f) => f.id), [ // raw_generation: the mod's data feature, then its code "testmod:glass_pillar", "testmod:count_glass", "testmod:cobble_columns", "bworld:trees", // the last step, for features that don't say: bworld's, then testmod's "bworld:cactus", "testmod:place_one", ]); const at = (blocks: Uint32Array, x: number, y: number, z: number) => blocks[y * CHUNK_AREA + z * CHUNK_SIZE + x]; const glass = block_ids["bworld:glass"]; const cobble = block_ids["bworld:cobblestone"]; for (const [cx, cz] of [[0, 0], [3, -2]]) { const { blocks } = generate_raw_chunk(cx, cz, game.world.seed, block_ids, worldgen, default_values); // the pillar in the middle, which the code feature after it in its step saw let pillar = 0; for (let y = 0; y < CHUNK_HEIGHT; y++) if (at(blocks, 8, y, 8) === glass) pillar++; assertEquals(pillar, 3); assertEquals((globalThis as unknown as { glass_seen: number }).glass_seen, 3); // the one place_one put at 2, 200, 2 assertEquals([200, 201, 202].map((y) => at(blocks, 2, y, 2)), [glass, glass, glass]); // up to 4 columns of 2 cobblestone, standing on the ground let columns = 0; for (let i = 0; i < blocks.length; i++) { if (blocks[i] !== cobble || blocks[i - CHUNK_AREA] === cobble) continue; columns++; assertEquals(blocks[i + CHUNK_AREA], cobble, "two tall"); } assert(columns >= 1 && columns <= 4, `${columns} columns`); } } finally { Deno.removeSync(dir, { recursive: true }); } }); Deno.test("a data feature whose type no script registers stops the server", async () => { const dir = with_mod({ "worldgen/features/broken.json": { ...GLASS_PILLAR, id: "testmod:broken", type: "testmod:nothing" }, }); try { await assertRejects( () => test_game(dir), Error, "type testmod:nothing isn't built in, and no worldgen script registered it", ); } finally { Deno.removeSync(dir, { recursive: true }); } }); Deno.test("a feature can't be both code and data", async () => { const clash = with_mod( { "worldgen/features/glass_pillar.json": GLASS_PILLAR }, `import type { WorldgenContext } from "bworld/worldgen"; export function setup(gen: WorldgenContext) { gen.register_feature("testmod:glass_pillar", () => {}); }`, ); try { await assertRejects( () => test_game(clash), Error, "feature testmod:glass_pillar is registered twice, as code or data", ); } finally { Deno.removeSync(clash, { recursive: true }); } });