Make cactus generare in deserts

This commit is contained in:
2026-10-10 19:41:51 -03:00
parent 20c5e97599
commit b03ad862e0
5 changed files with 102 additions and 9 deletions
+1 -1
View File
@@ -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 });
+9 -7
View File
@@ -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++) {
+44
View File
@@ -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)])}`);
});