diff --git a/mods/bworld/manifest.json b/mods/bworld/manifest.json index 51a140a..58ba9da 100644 --- a/mods/bworld/manifest.json +++ b/mods/bworld/manifest.json @@ -12,6 +12,7 @@ "scripts": { "server": "scripts/server.ts", "client": "scripts/client.ts", - "shared": "scripts/shared.ts" + "shared": "scripts/shared.ts", + "worldgen": "scripts/worldgen.ts" } } diff --git a/mods/bworld/scripts/worldgen.ts b/mods/bworld/scripts/worldgen.ts new file mode 100644 index 0000000..ac1901d --- /dev/null +++ b/mods/bworld/scripts/worldgen.ts @@ -0,0 +1,46 @@ +// the base game's features, on top of the engine's terrain. runs in chunk workers and must give the same world every +// time: only chunk.rng and the noise helpers, never Math.random or the time +import type { FeatureChunk, WorldgenContext } from "bworld/worldgen"; + +export function setup(gen: WorldgenContext) { + gen.register_feature("bworld:cactus", cactus); +} + +// how tall a cactus grows, and how likely each height is +const CACTUS_HEIGHTS = [ + { height: 1, chance: 0.65 }, + { height: 2, chance: 0.25 }, + { height: 3, chance: 0.1 }, +]; + +// 2 or 3 tries a chunk, each a cactus where it lands on desert sand with room above +function cactus(chunk: FeatureChunk) { + const tries = chunk.rng.next() < 0.5 ? 2 : 3; + for (let i = 0; i < tries; i++) { + // every try rolls the same numbers whether or not it places, so one try doesn't move the next + const x = chunk.x * 16 + Math.floor(chunk.rng.next() * 16); + const z = chunk.z * 16 + Math.floor(chunk.rng.next() * 16); + const height = pick_height(chunk.rng.next()); + if (chunk.biome_at(x, z) !== "bworld:desert") { + continue; + } + const ground = chunk.height_at(x, z); + if (chunk.get_block(x, ground, z) !== "bworld:sand") { + continue; + } + for (let y = ground + 1; y <= ground + height; y++) { + if (chunk.get_block(x, y, z) !== "bworld:air") { + break; + } + chunk.set_block(x, y, z, "bworld:cactus"); + } + } +} + +function pick_height(roll: number) { + for (const { height, chance } of CACTUS_HEIGHTS) { + if (roll < chance) return height; + roll -= chance; + } + return CACTUS_HEIGHTS.at(-1)!.height; +} diff --git a/tests/bmod_test.ts b/tests/bmod_test.ts index bf2d712..6343197 100644 --- a/tests/bmod_test.ts +++ b/tests/bmod_test.ts @@ -43,7 +43,7 @@ Deno.test("packing a mod keeps its data, scripts, textures and credits, and give const bworld = read_bmod(Deno.readFileSync(`${folder}/bworld.bmod`), "bworld.bmod"); assertEquals(bworld.data.blocks.length, [...Deno.readDirSync("mods/bworld/blocks")].length); - assertEquals(Object.keys(bworld.scripts), ["server", "client", "shared"]); + assertEquals(Object.keys(bworld.scripts), ["server", "client", "worldgen", "shared"]); Deno.removeSync(sources, { recursive: true }); Deno.removeSync(folder, { recursive: true }); diff --git a/tests/mod_loading_test.ts b/tests/mod_loading_test.ts index 13201b8..65a4fe3 100644 --- a/tests/mod_loading_test.ts +++ b/tests/mod_loading_test.ts @@ -127,12 +127,14 @@ Deno.test("the template's worldgen feature places boulders, the same way every t const dir = mods_with_template(); const { game } = await test_game(dir, undefined, "boulder-seed"); const worldgen = game.world.worldgen!; - assertEquals(worldgen.features.map((f) => f.id), ["copper_tools:boulders"]); + // after the base game's, in load order + assertEquals(worldgen.features.map((f) => f.id), ["bworld:cactus", "copper_tools:boulders"]); - // compare against generation without mods: boulders are extra stone on top of the surface + // compare against generation without the template: boulders are extra stone on top of the surface + const without = { ...worldgen, features: worldgen.features.filter((f) => f.id !== "copper_tools:boulders") }; let boulders = 0; for (let cx = 0; cx < 20; cx++) { - const plain = generate_raw_chunk(cx, 0, "boulder-seed", game.world.block_ids); + const plain = generate_raw_chunk(cx, 0, "boulder-seed", game.world.block_ids, without); const modded = generate_raw_chunk(cx, 0, "boulder-seed", game.world.block_ids, worldgen); const differences = plain.blocks.filter((b, i) => b !== modded.blocks[i]).length; if (differences > 0) boulders++; @@ -142,10 +144,10 @@ Deno.test("the template's worldgen feature places boulders, the same way every t // loading the script again (like a client does) gives the same terrain const again = await load_worldgen( - [{ - mod: "copper_tools", - url: new URL(`file://${Deno.realPathSync(dir)}/copper_tools/scripts/worldgen.ts`).href, - }], + ["bworld", "copper_tools"].map((mod) => ({ + mod, + url: new URL(`file://${Deno.realPathSync(dir)}/${mod}/scripts/worldgen.ts`).href, + })), [], ); for (let cx = 0; cx < 20; cx++) { diff --git a/tests/worldgen_features_test.ts b/tests/worldgen_features_test.ts new file mode 100644 index 0000000..717c2be --- /dev/null +++ b/tests/worldgen_features_test.ts @@ -0,0 +1,44 @@ +// the base game's worldgen features, see mods/bworld/scripts/worldgen.ts +import { assert, assertEquals } from "@std/assert"; +import { CHUNK_AREA, CHUNK_HEIGHT, CHUNK_SIZE, ID_MASK } from "$/common/constants.ts"; +import { generate_raw_chunk } from "$/common/generation.ts"; +import { test_game } from "./helpers.ts"; + +// a desert on this seed, found with estimate_biome +const SEED = "cactus-seed"; +const DESERT_CHUNK = { x: -256, z: -128 }; + +Deno.test("cacti grow 1 to 3 tall on desert sand, 2 or 3 tries a chunk", async () => { + const { game } = await test_game("mods", undefined, SEED); + const ids = game.world.block_ids; + const cactus = ids["bworld:cactus"]; + const sand = ids["bworld:sand"]; + const heights: number[] = []; + for (let dx = -3; dx <= 3; dx++) { + for (let dz = -3; dz <= 3; dz++) { + const chunk = generate_raw_chunk(DESERT_CHUNK.x + dx, DESERT_CHUNK.z + dz, SEED, ids, game.world.worldgen); + const at = (x: number, y: number, z: number) => chunk.blocks[y * CHUNK_AREA + z * CHUNK_SIZE + x] & ID_MASK; + let in_chunk = 0; + for (let x = 0; x < CHUNK_SIZE; x++) { + for (let z = 0; z < CHUNK_SIZE; z++) { + for (let y = 1; y < CHUNK_HEIGHT; y++) { + // the bottom of each cactus + if (at(x, y, z) !== cactus || at(x, y - 1, z) === cactus) continue; + assertEquals(at(x, y - 1, z), sand, "on sand"); + assertEquals(chunk.biomes[z * CHUNK_SIZE + x], "bworld:desert"); + let height = 0; + while (at(x, y + height, z) === cactus) height++; + heights.push(height); + in_chunk++; + } + } + } + assert(in_chunk <= 3, `${in_chunk} cacti in one chunk`); + } + } + // 49 chunks, mostly desert + assert(heights.length >= 40, `only ${heights.length} cacti`); + assert(heights.every((h) => h >= 1 && h <= 3)); + const share = (h: number) => heights.filter((x) => x === h).length / heights.length; + assert(share(1) > 0.4 && share(3) < 0.3, `heights ${JSON.stringify([share(1), share(2), share(3)])}`); +});