Files
bworld/tests/terrain_data_test.ts
T

111 lines
3.9 KiB
TypeScript

// 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 { CHUNK_AREA } from "$/common/constants.ts";
import { generate_raw_chunk } from "$/common/generation.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 { blocks, biomes } = generate_raw_chunk(cx, cz, "lavender", block_ids, worldgen, default_values);
biomes.forEach((biome, column) => {
if (biome !== "lavender:fields") return;
fields++;
// its surface: the top block of the column is its sand
let y = 255;
while (y > 0 && blocks[y * CHUNK_AREA + column] === 0) y--;
assertEquals(blocks[y * CHUNK_AREA + column], 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 generator the server doesn't start", async () => {
const dir = with_mod("lonely", {}, false);
try {
await assertRejects(() => test_game(dir), Error, "there's no generator bworld:overworld");
} finally {
Deno.removeSync(dir, { recursive: true });
}
});