Files
bworld/tests/wood_test.ts
T
2026-11-04 21:51:26 -03:00

101 lines
4.1 KiB
TypeScript

// SPDX-FileCopyrightText: 2026 Paula Wisnievski
// SPDX-License-Identifier: AGPL-3.0-or-later
import { assert, assertEquals, assertRejects } from "@std/assert";
import { CHUNK_AREA, CHUNK_SIZE } from "@/common/constants.ts";
import { Container, ItemStack } from "@/common/inventory.ts";
import { CRAFTING_RESULT_SLOT } from "@/common/protocol.ts";
import { generate_raw_chunk } from "@/common/generation.ts";
import { get_biome } from "@/common/worldgen/biomes.ts";
import { update_crafting_result } from "@/server/game/crafting.ts";
import { recipe_lookup } from "@/common/mod_loader.ts";
import { copy_dir, test_game } from "./helpers.ts";
Deno.test("recipes that take planks take any kind, even mixed, but nothing else", async () => {
const { game } = await test_game("mods");
const craft = (top: string, bottom: string) => {
const grid = new Container(10);
grid.set_item(0, new ItemStack(top, 1));
grid.set_item(3, new ItemStack(bottom, 1));
update_crafting_result(grid, game.recipes.shaped);
return grid.get_item(CRAFTING_RESULT_SLOT);
};
assertEquals(craft("bworld:oak_planks", "bworld:oak_planks")?.type_id, "bworld:stick");
assertEquals(craft("bworld:birch_planks", "bworld:cherry_planks")?.type_id, "bworld:stick");
assertEquals(craft("bworld:birch_planks", "bworld:stone"), undefined);
// and each log makes its own planks
const grid = new Container(10);
grid.set_item(4, new ItemStack("bworld:dark_oak_log", 1));
update_crafting_result(grid, game.recipes.shaped);
assertEquals(grid.get_item(CRAFTING_RESULT_SLOT)?.type_id, "bworld:dark_oak_planks");
// every log burns and smelts
const recipes = recipe_lookup(() => game.recipes);
for (const log of ["bworld:oak_log", "bworld:spruce_log", "bworld:cherry_log"]) {
assert(recipes.find("bworld:fuel", log), log);
assert(recipes.find("bworld:smelting", log), log);
}
});
Deno.test("trees grow the wood of their kind and biome", async () => {
const { game } = await test_game("mods");
const ids = game.world.block_ids;
const woods: Record<string, string> = {
"bworld:oak_log": "oak",
"bworld:spruce_log": "spruce",
"bworld:birch_log": "birch",
"bworld:jungle_log": "jungle",
"bworld:acacia_log": "acacia",
"bworld:dark_oak_log": "dark_oak",
};
const wood_of = new Map(Object.entries(woods).map(([id, wood]) => [ids[id], wood]));
const ground = new Set([ids["bworld:grass_block"], ids["bworld:dirt"], ids["bworld:snow_block"]]);
const leaves_of = new Map(
Object.entries(woods).map(([id, wood]) => [ids[id.replace("log", "leaves")], wood]),
);
const found = new Set<string>();
const found_leaves = new Set<string>();
for (let cx = -8; cx < 8; cx++) {
for (let cz = -8; cz < 8; cz++) {
const { blocks, biomes } = generate_raw_chunk(cx, cz, "wood-test", ids, game.world.worldgen);
blocks.forEach((block, i) => {
const leaves = leaves_of.get(block);
if (leaves) found_leaves.add(leaves);
const wood = wood_of.get(block);
// the bottom of a trunk
if (!wood || !ground.has(blocks[i - CHUNK_AREA])) return;
const column = i % CHUNK_AREA;
const biome = biomes[column];
const info = get_biome(biome)!;
const allowed = info.trees!.kinds.map((kind) =>
info.trees!.wood[kind] ?? (kind === "big_oak" ? "oak" : kind)
);
assert(
allowed.includes(wood as never),
`${wood} in ${biome} at ${cx * CHUNK_SIZE}, ${cz * CHUNK_SIZE}`,
);
found.add(wood);
});
}
}
assert(found.size >= 3, `only found ${[...found]}`);
// each tree's leaves are its own wood's
assertEquals([...found_leaves].sort(), [...found].sort());
});
Deno.test("a biome with a kind of tree bworld's trees don't have stops the server", async () => {
const dir = Deno.makeTempDirSync({ prefix: "bworld_trees_" });
copy_dir("mods/bworld", `${dir}/bworld`);
const path = `${dir}/bworld/worldgen/biomes/forest.json`;
const json = JSON.parse(Deno.readTextFileSync(path));
json.biome.trees.kinds.push("palm");
Deno.writeTextFileSync(path, JSON.stringify(json));
try {
await assertRejects(() => test_game(dir), Error, "biome bworld:forest: trees.kinds: palm isn't one of oak");
} finally {
Deno.removeSync(dir, { recursive: true });
}
});