Files
bworld/tests/schematic_test.ts
T
2026-11-15 18:41:22 -03:00

102 lines
4.5 KiB
TypeScript

// SPDX-FileCopyrightText: 2026 Paula Wisnievski
// SPDX-License-Identifier: AGPL-3.0-or-later
// exporting a box of the world as a schematic file, with /exportschematic
import { assert, assertEquals } from "@std/assert";
import { decode_schematic, type Schematic } from "@/common/schematic.ts";
import { create_entity } from "@/server/game/entity/entity_types.ts";
import { new_stack } from "@/server/game/mod_runtime.ts";
import { set_state_value } from "@/common/utils.ts";
import { stone_floor, test_game } from "./helpers.ts";
// deno-lint-ignore no-explicit-any
type Msg = any;
async function setup() {
const t = await test_game("mods");
t.join(1, "alice");
stone_floor(t.game);
t.send(1, { type: "move", x: 0.5, y: 100, z: 0.5, yaw: 0, pitch: 0 });
t.take(1);
// runs the command and waits for the host to write the file. the schematic, or undefined and what the player was
// told
const export_schematic = async (args: string) => {
t.send(1, { type: "chat", text: `/exportschematic ${args}` });
await new Promise((resolve) => setTimeout(resolve, 0));
const chat = t.take(1).filter((m: Msg) => m.type === "chat").map((m: Msg) => m.text as string);
const path = chat.map((text) => text.match(/^Saved the schematic to (.+)$/)?.[1]).find((path) => path);
const bytes = path ? t.schematics.get(path) : undefined;
return { schematic: bytes && decode_schematic(bytes), chat };
};
return { ...t, export_schematic };
}
// the palette entry of the block at p, from the box's lowest corner
function block_at(schematic: Schematic, p: [number, number, number]) {
const block = schematic.blocks.find((block) => block.p.every((v, i) => v === p[i]));
assert(block, `nothing at ${p}`);
return { ...schematic.palette[block.i], data: block.data };
}
Deno.test("a schematic has every block in the box with its states, air too, from the lowest corner", async () => {
const t = await setup();
t.game.set_block(2, 100, 1, "bworld:oak_planks");
t.game.set_block(1, 101, 1, "bworld:furnace");
const furnace_value = t.game.world.get_block_value(1, 101, 1);
const facing_west = set_state_value(furnace_value, t.game.world.get_block_info(1, 101, 1)!, "facing", 3)!;
t.game.set_block_state(1, 101, 1, facing_west >>> 16);
// the corners in any order
const { schematic } = await t.export_schematic("2 101 1 1 100 2");
assert(schematic);
assertEquals(schematic.size, [2, 2, 2]);
assertEquals(schematic.blocks.length, 8);
assertEquals(block_at(schematic, [1, 0, 0]).id, "bworld:oak_planks");
const furnace = block_at(schematic, [0, 1, 0]);
assertEquals(furnace.id, "bworld:furnace");
assertEquals(furnace.s?.facing, 3);
assertEquals(block_at(schematic, [0, 0, 1]).id, "cubi:air");
// blocks with the same states share their entry
assertEquals(new Set(schematic.palette.map((entry) => JSON.stringify(entry))).size, schematic.palette.length);
});
Deno.test("a chest's items are written into its data in place of its container's id", async () => {
const t = await setup();
t.game.set_block(1, 100, 1, "bworld:chest");
const id = (t.game.world.get_tile(1, 100, 1)?.mod_data as { container: string }).container;
t.game.containers.get(id)!.set_item(2, new_stack({ id: "bworld:coal", count: 5 }));
const { schematic } = await t.export_schematic("1 100 1 1 100 1");
const items = (block_at(schematic!, [0, 0, 0]).data as { container: unknown[] }).container;
assertEquals(items[2], { id: "bworld:coal", count: 5 });
assertEquals(items[0], null);
// exporting doesn't change the chest
assertEquals(t.game.containers.get(id)!.get_item(2)?.amount, 5);
});
Deno.test("a schematic has the entities whose feet are in the box, without their uuids", async () => {
const t = await setup();
const spawn = (x: number, z: number) => {
const chicken = create_entity(t.game.entities, "bworld:chicken")!;
chicken.set_position(x, 100, z);
t.game.entities.add_entity(chicken);
};
spawn(1.5, 1.25);
spawn(5.5, 1.5);
const { schematic } = await t.export_schematic("1 100 1 2 101 2");
assertEquals(schematic!.entities.length, 1);
const [chicken] = schematic!.entities;
assertEquals(chicken.id, "bworld:chicken");
assertEquals(chicken.p, [0.5, 0, 0.25]);
assertEquals(chicken.data.uuid, undefined);
});
Deno.test("a box with too many blocks isn't exported", async () => {
const t = await setup();
const { schematic, chat } = await t.export_schematic("0 0 0 1000 200 1000");
assertEquals(schematic, undefined);
assert(chat.some((text) => text.startsWith("Couldn't export it")), chat.join("\n"));
assertEquals(t.schematics.size, 0);
});