3D biomes
This commit is contained in:
@@ -0,0 +1,156 @@
|
||||
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
// biomes have heights: one per block across and every 4 blocks up, chosen by biome rules that can look at "y" and
|
||||
// "depth", see docs/modding/worldgen/biomes.md. a mod adds an underground biome to the base game's with one rule
|
||||
import { assert, assertEquals } from "@std/assert";
|
||||
import type { ServerContext } from "@/common/mod_api/server.ts";
|
||||
import { validate_noise_settings } from "@/common/worldgen/data.ts";
|
||||
import { client_level } from "./client_level.ts";
|
||||
import { copy_dir, test_game } from "./helpers.ts";
|
||||
|
||||
// mods/bworld and a mod whose biome is everywhere more than 16 blocks under the ground. its scripts keep what a feature
|
||||
// saw of chunk 0, 0's biomes at 8, 8, and the server's context. the feature also sets the cell 9, 201, 9 to it
|
||||
function deep_mod() {
|
||||
const dir = Deno.makeTempDirSync({ prefix: "bworld_deep_" });
|
||||
copy_dir("mods/bworld", `${dir}/bworld`);
|
||||
const write = (path: string, content: unknown) => {
|
||||
Deno.mkdirSync(`${dir}/deep/${path.slice(0, path.lastIndexOf("/"))}`, { recursive: true });
|
||||
Deno.writeTextFileSync(`${dir}/deep/${path}`, typeof content === "string" ? content : JSON.stringify(content));
|
||||
};
|
||||
write("manifest.json", {
|
||||
format_version: 1,
|
||||
id: "deep",
|
||||
name: "Deep",
|
||||
version: "1.0.0",
|
||||
dependencies: [{ id: "bworld", version: "*" }],
|
||||
scripts: { worldgen: "scripts/worldgen.ts", server: "scripts/server.ts" },
|
||||
});
|
||||
write("worldgen/biomes/caves.json", {
|
||||
format_version: 1,
|
||||
biome: {
|
||||
id: "deep:caves",
|
||||
surface: { top: "bworld:stone", filler: "bworld:stone", filler_depth: 1 },
|
||||
season: "winter",
|
||||
},
|
||||
});
|
||||
write("worldgen/biome_source/caves.json", {
|
||||
format_version: 1,
|
||||
biome_source: {
|
||||
id: "deep:caves",
|
||||
extends: "bworld:overworld",
|
||||
// before every rule of the base game's, which all hold all the way down
|
||||
rules: [{
|
||||
id: "deep:caves",
|
||||
before: "bworld:river_bed_frozen",
|
||||
when: { depth: { min_exclusive: 16 } },
|
||||
biome: "deep:caves",
|
||||
}],
|
||||
},
|
||||
});
|
||||
write(
|
||||
"scripts/worldgen.ts",
|
||||
`import type { WorldgenContext } from "cubi/worldgen";
|
||||
export function setup(gen: WorldgenContext) {
|
||||
gen.register_feature("deep:look", (chunk) => {
|
||||
if (chunk.x !== 0 || chunk.z !== 0) return;
|
||||
const ground = chunk.height_at(8, 8);
|
||||
(globalThis as any).deep_seen = [chunk.biome_at(8, ground, 8), chunk.biome_at(8, ground - 40, 8)];
|
||||
chunk.set_biome(9, 201, 9, "deep:caves");
|
||||
});
|
||||
}`,
|
||||
);
|
||||
write(
|
||||
"scripts/server.ts",
|
||||
`import type { ServerContext } from "cubi/server";
|
||||
export function setup(ctx: ServerContext) {
|
||||
(globalThis as any).deep_ctx = ctx;
|
||||
}`,
|
||||
);
|
||||
return dir;
|
||||
}
|
||||
|
||||
const seen = () => (globalThis as unknown as { deep_seen: string[] }).deep_seen;
|
||||
const server_ctx = () => (globalThis as unknown as { deep_ctx: ServerContext }).deep_ctx;
|
||||
|
||||
Deno.test("a rule with depth puts a biome under the ground, and the ground keeps its own and its blocks", async () => {
|
||||
const dir = deep_mod();
|
||||
try {
|
||||
const { game } = await test_game(dir);
|
||||
const world = game.world;
|
||||
const ground = world.surface_height(8, 8);
|
||||
const surface = world.biome_at(8, ground, 8);
|
||||
assert(surface !== "deep:caves", "the ground's biome is the base game's");
|
||||
assertEquals(world.biome_at(8, ground - 40, 8), "deep:caves");
|
||||
assertEquals(world.biome_at(8, world.shape.min_y - 100, 8), "deep:caves", "below the world, its bottom's");
|
||||
// features see the same, and set the 4 blocks tall cell a y is in
|
||||
assertEquals(seen(), [surface, "deep:caves"]);
|
||||
assertEquals([199, 200, 203, 204].map((y) => world.biome_at(9, y, 9) === "deep:caves"), [
|
||||
false,
|
||||
true,
|
||||
true,
|
||||
false,
|
||||
]);
|
||||
|
||||
// the blocks are what the base game makes without it
|
||||
const blocks = [[0, 0], [2, -3]].map(([x, z]) => world.generated_blocks(x, z).to_flat());
|
||||
const plain = (await test_game("mods")).game.world;
|
||||
assertEquals(blocks, [[0, 0], [2, -3]].map(([x, z]) => plain.generated_blocks(x, z).to_flat()));
|
||||
} finally {
|
||||
Deno.removeSync(dir, { recursive: true });
|
||||
}
|
||||
});
|
||||
|
||||
Deno.test("biomes under the ground are saved, sent to clients, and where entities stand", async () => {
|
||||
const dir = deep_mod();
|
||||
try {
|
||||
const { game, join, send, take } = await test_game(dir);
|
||||
const world = game.world;
|
||||
const ground = world.surface_height(8, 8);
|
||||
const deep = ground - 40;
|
||||
|
||||
// a client reads them from the chunk it's sent
|
||||
const { level } = client_level(world.shape);
|
||||
level.load_chunk(world.level_chunk(0, 0));
|
||||
assertEquals(level.biome_at(8, deep, 8), "deep:caves");
|
||||
assertEquals(level.biome_at(8, ground, 8), world.biome_at(8, ground, 8));
|
||||
level.dispose();
|
||||
|
||||
// scripts read the biome and season at a block, and an entity's at its feet
|
||||
const ctx = server_ctx();
|
||||
assertEquals([ctx.world.biome_at(8.5, deep, 8.5), ctx.world.season_at(8.5, deep, 8.5)], [
|
||||
"deep:caves",
|
||||
"winter",
|
||||
]);
|
||||
const chicken = ctx.world.spawn_entity("bworld:chicken", 8.5, deep, 8.5);
|
||||
assertEquals([chicken.biome, chicken.season], ["deep:caves", "winter"]);
|
||||
|
||||
// /locatebiome finds it under the ground, and says how deep
|
||||
join(1, "alice");
|
||||
send(1, { type: "move", x: 8.5, y: ground + 1, z: 8.5, yaw: 0, pitch: 0 });
|
||||
take(1);
|
||||
send(1, { type: "chat", text: "/locatebiome deep:caves" });
|
||||
const answer = take(1).filter((m) => m.type === "chat").map((m) => m.text).join("\n");
|
||||
const [x, y, z] = answer.match(/at (-?\d+) (-?\d+) (-?\d+)/)!.slice(1).map(Number);
|
||||
assertEquals(world.locate_biome("deep:caves", x, z), y);
|
||||
assert(y < world.locate_biome(world.biome_at(x, 1000, z), x, z)! - 16, `${answer}, not under the ground`);
|
||||
|
||||
// and they come back with the chunk
|
||||
world.set_block(8, ground + 1, 8, "bworld:stone");
|
||||
const loaded = (await test_game(dir, game.save())).game.world;
|
||||
assertEquals(loaded.biome_at(8, deep, 8), "deep:caves");
|
||||
assertEquals(loaded.biome_at(8, ground, 8), world.biome_at(8, ground, 8));
|
||||
} finally {
|
||||
Deno.removeSync(dir, { recursive: true });
|
||||
}
|
||||
});
|
||||
|
||||
Deno.test("a generator's climate values can't be named like what biome rules look at", async () => {
|
||||
const json =
|
||||
JSON.parse(await Deno.readTextFile("mods/bworld/worldgen/noise_settings/overworld.json")).noise_settings;
|
||||
assertEquals(validate_noise_settings(json), []);
|
||||
for (const name of ["y", "depth"]) {
|
||||
const problems = validate_noise_settings({ ...json, climate: { ...json.climate, [name]: 0 } });
|
||||
assert(problems.some((problem) => problem.includes("climate")), `${name}: ${problems}`);
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user