Files
bworld/tests/worldgen_features_test.ts
T
2026-11-07 12:13:43 -03:00

51 lines
2.1 KiB
TypeScript

// SPDX-FileCopyrightText: 2026 Paula Wisnievski
// SPDX-License-Identifier: AGPL-3.0-or-later
// the base game's worldgen features, see mods/bworld/scripts/worldgen.ts
import { assert, assertEquals } from "@std/assert";
import { 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 { min_y, max_y } = game.world.shape;
const x0 = chunk.column.x * CHUNK_SIZE;
const z0 = chunk.column.z * CHUNK_SIZE;
const at = (x: number, y: number, z: number) => chunk.column.get(x0 + x, y, z0 + z) & 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 = min_y + 1; y < max_y; 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.column.biome_at(x0 + x, y - 1, z0 + z), "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)])}`);
});