Seasons api
This commit is contained in:
@@ -0,0 +1,93 @@
|
||||
// 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 { copy_dir, test_game } from "./helpers.ts";
|
||||
|
||||
const SCRIPT = `
|
||||
import type { ServerContext } from "bworld/server";
|
||||
|
||||
export function setup(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 keep spring by leaving it out
|
||||
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: "*" }],
|
||||
scripts: { server: "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;
|
||||
const season_of = (biome: string) => biome.includes("ocean") ? "spring" : "winter";
|
||||
|
||||
// spawn is on dry land
|
||||
const biome = ctx.world.biome_at(0, 0);
|
||||
assertEquals(biome, game.world.biome_at(0, 0));
|
||||
assertEquals(season_of(biome), "winter");
|
||||
assertEquals(ctx.world.season_at(0.7, 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, -3000);
|
||||
assertEquals(far, game.world.biome_at(5000, -3000));
|
||||
assertEquals(ctx.world.season_at(5000, -3000), season_of(far));
|
||||
} finally {
|
||||
Deno.removeSync(dir, { recursive: true });
|
||||
}
|
||||
});
|
||||
@@ -67,6 +67,8 @@ function client_of(t: Game) {
|
||||
const block = info(id);
|
||||
return block !== undefined && covers_face(collision_shape_at(block, state << 16), face);
|
||||
},
|
||||
biome_at: () => undefined,
|
||||
season_at: () => undefined,
|
||||
};
|
||||
setup_shared({
|
||||
mod: { id: "bworld", version: "test" },
|
||||
|
||||
Reference in New Issue
Block a user