Files
2026-11-14 12:18:41 -03:00

99 lines
3.5 KiB
TypeScript

// SPDX-FileCopyrightText: 2026 Paula Wisnievski
// SPDX-License-Identifier: AGPL-3.0-or-later
// blocks, entities, players and the world tell scripts their biome's season, see docs/modding/worldgen/biomes.md
import { assertEquals } from "@std/assert";
import type { ServerContext } from "@/common/mod_api/server.ts";
import { get_biome } from "@/common/worldgen/biomes.ts";
import { copy_dir, test_game } from "./helpers.ts";
const SCRIPT = `
import type { ServerContext } from "cubi/server";
export function server(ctx: ServerContext) {
(globalThis as any).season_test = { ctx, created: [] };
ctx.components.register_block("testmod:thermometer", {
on_create(block) {
(globalThis as any).season_test.created.push({ biome: block.biome, season: block.season });
},
});
}
`;
// bworld's biomes, with every one but the oceans in winter. oceans follow the calendar, spring on day 1
function season_mods() {
const dir = Deno.makeTempDirSync({ prefix: "bworld_seasons_" });
copy_dir("mods/bworld", `${dir}/bworld`);
const biomes = `${dir}/bworld/worldgen/biomes`;
for (const { name } of Deno.readDirSync(biomes)) {
if (name.includes("ocean")) continue;
const json = JSON.parse(Deno.readTextFileSync(`${biomes}/${name}`));
json.biome.season = "winter";
Deno.writeTextFileSync(`${biomes}/${name}`, JSON.stringify(json));
}
const mod = `${dir}/testmod`;
Deno.mkdirSync(`${mod}/blocks`, { recursive: true });
Deno.mkdirSync(`${mod}/scripts`);
Deno.writeTextFileSync(
`${mod}/manifest.json`,
JSON.stringify({
format_version: 1,
id: "testmod",
name: "Test",
version: "1.0.0",
dependencies: [{ id: "bworld", version: "*" }],
main: "scripts/server.ts",
}),
);
Deno.writeTextFileSync(
`${mod}/blocks/thermometer.json`,
JSON.stringify({
format_version: 1,
block: {
id: "testmod:thermometer",
textures: "bworld:stone",
mining: { toughness: 1 },
components: { "testmod:thermometer": {} },
},
}),
);
Deno.writeTextFileSync(`${mod}/scripts/server.ts`, SCRIPT);
return dir;
}
Deno.test("blocks, entities, players and the world know their biome's season", async () => {
const dir = season_mods();
try {
const { game, join, send } = await test_game(dir);
const { ctx, created } = (globalThis as unknown as {
season_test: { ctx: ServerContext; created: { biome: string; season: string }[] };
}).season_test;
// it's day 1, so oceans without their own season are in spring
const season_of = (biome: string) => get_biome(biome)?.season ?? "spring";
// spawn is on dry land
const biome = ctx.world.biome_at(0, 100, 0);
assertEquals(biome, game.world.biome_at(0, 100, 0));
assertEquals(season_of(biome), "winter");
assertEquals(ctx.world.season_at(0.7, 100.5, 0.2), "winter");
game.set_block(0, 100, 0, "testmod:thermometer");
assertEquals(created, [{ biome, season: season_of(biome) }]);
const chicken = ctx.world.spawn_entity("bworld:chicken", 0.5, 100, 0.5);
assertEquals([chicken.biome, chicken.season], [biome, season_of(biome)]);
join(1, "alice");
send(1, { type: "move", x: 0.5, y: 100, z: 0.5, yaw: 0, pitch: 0 });
const alice = ctx.players.by_name("alice")!;
assertEquals([alice.biome, alice.season], [biome, season_of(biome)]);
// somewhere far off, in a chunk nobody loaded, it's what generation makes there
const far = ctx.world.biome_at(5000, 64, -3000);
assertEquals(far, game.world.biome_at(5000, 64, -3000));
assertEquals(ctx.world.season_at(5000, 64, -3000), season_of(far));
} finally {
Deno.removeSync(dir, { recursive: true });
}
});