116 lines
4.1 KiB
TypeScript
116 lines
4.1 KiB
TypeScript
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
|
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
// the terrain is data: noises, density functions, noise settings and biome sources in mods' worldgen folders, see
|
|
// docs/modding/worldgen/terrain.md. tests/worldgen_golden_test.ts checks the base game's makes the same world
|
|
import { assert, assertEquals, assertRejects } from "@std/assert";
|
|
import { generate_raw_chunk } from "@/common/generation.ts";
|
|
import { CHUNK_AREA } from "@/common/constants.ts";
|
|
import { copy_dir, test_game } from "./helpers.ts";
|
|
|
|
// mods/bworld and a mod with files, in a temp folder
|
|
function with_mod(id: string, files: Record<string, unknown>, bworld = true) {
|
|
const dir = Deno.makeTempDirSync({ prefix: "bworld_terrain_" });
|
|
if (bworld) copy_dir("mods/bworld", `${dir}/bworld`);
|
|
const write = (path: string, json: unknown) => {
|
|
Deno.mkdirSync(`${dir}/${id}/${path.slice(0, path.lastIndexOf("/"))}`, { recursive: true });
|
|
Deno.writeTextFileSync(`${dir}/${id}/${path}`, JSON.stringify(json));
|
|
};
|
|
const dependencies = bworld ? [{ id: "bworld", version: "*" }] : [];
|
|
write("manifest.json", { format_version: 1, id, name: id, version: "1.0.0", dependencies });
|
|
for (const [path, json] of Object.entries(files)) write(path, json);
|
|
return dir;
|
|
}
|
|
|
|
const LAVENDER_BIOME = {
|
|
format_version: 1,
|
|
biome: {
|
|
id: "lavender:fields",
|
|
surface: { top: "bworld:sand", filler: "bworld:dirt", filler_depth: 3 },
|
|
},
|
|
};
|
|
|
|
Deno.test("a mod's biome generates where its rule, added to the base game's, says", async () => {
|
|
const dir = with_mod("lavender", {
|
|
"worldgen/biomes/fields.json": LAVENDER_BIOME,
|
|
"worldgen/biome_source/fields.json": {
|
|
format_version: 1,
|
|
biome_source: {
|
|
id: "lavender:fields",
|
|
extends: "bworld:overworld",
|
|
// all the land above the beaches that isn't mountains, before the climate decides
|
|
rules: [{
|
|
id: "lavender:fields",
|
|
before: "bworld:snowy_taiga",
|
|
when: { surface: { min_inclusive: 70, max_inclusive: 120 } },
|
|
biome: "lavender:fields",
|
|
}],
|
|
},
|
|
},
|
|
});
|
|
try {
|
|
const { game } = await test_game(dir);
|
|
const { block_ids, worldgen, default_values } = game.world;
|
|
let fields = 0;
|
|
for (let cx = -2; cx < 2; cx++) {
|
|
for (let cz = -2; cz < 2; cz++) {
|
|
const { column } = generate_raw_chunk(cx, cz, "lavender", block_ids, worldgen, default_values);
|
|
const { min_y, max_y } = game.world.shape;
|
|
for (let i = 0; i < CHUNK_AREA; i++) {
|
|
const [x, z] = [cx * 16 + i % 16, cz * 16 + Math.floor(i / 16)];
|
|
// its surface: the top block of the column is its sand
|
|
let y = max_y - 1;
|
|
while (y > min_y && column.get(x, y, z) === 0) y--;
|
|
if (column.biome_at(x, y, z) !== "lavender:fields") continue;
|
|
fields++;
|
|
assertEquals(column.get(x, y, z), block_ids["bworld:sand"]);
|
|
}
|
|
}
|
|
}
|
|
assert(fields > 100, `only ${fields} columns of lavender fields`);
|
|
assertEquals(
|
|
worldgen.generator.rules.findIndex((rule) => rule.id === "lavender:fields") + 1,
|
|
worldgen.generator.rules.findIndex((rule) => rule.id === "bworld:snowy_taiga"),
|
|
);
|
|
} finally {
|
|
Deno.removeSync(dir, { recursive: true });
|
|
}
|
|
});
|
|
|
|
Deno.test("a biome rule that looks at a climate value the noise settings don't have stops the server", async () => {
|
|
const dir = with_mod("lavender", {
|
|
"worldgen/biomes/fields.json": LAVENDER_BIOME,
|
|
"worldgen/biome_source/fields.json": {
|
|
format_version: 1,
|
|
biome_source: {
|
|
id: "lavender:fields",
|
|
extends: "bworld:overworld",
|
|
rules: [{
|
|
id: "lavender:fields",
|
|
after: "bworld:ocean",
|
|
when: { rainfall: { min_inclusive: 0 } },
|
|
biome: "lavender:fields",
|
|
}],
|
|
},
|
|
},
|
|
});
|
|
try {
|
|
await assertRejects(
|
|
() => test_game(dir),
|
|
Error,
|
|
"lavender: worldgen/biome_source/fields.json: rule lavender:fields looks at rainfall, which bworld:overworld doesn't have",
|
|
);
|
|
} finally {
|
|
Deno.removeSync(dir, { recursive: true });
|
|
}
|
|
});
|
|
|
|
Deno.test("without a dimension to play the server doesn't start", async () => {
|
|
const dir = with_mod("lonely", {}, false);
|
|
try {
|
|
await assertRejects(() => test_game(dir), Error, "no mod has a default dimension");
|
|
} finally {
|
|
Deno.removeSync(dir, { recursive: true });
|
|
}
|
|
});
|