78 lines
3.8 KiB
TypeScript
78 lines
3.8 KiB
TypeScript
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
|
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
// the dev mod's schematic editor block: right clicking it lists the schematics, and the one picked is kept in its data
|
|
import { assert, assertEquals, assertMatch } from "@std/assert";
|
|
import { decode_schematic } from "@/common/schematic.ts";
|
|
import { copy_dir, test_game } from "./helpers.ts";
|
|
|
|
const EDITOR = "dev:schematic_editor";
|
|
|
|
// alice in a game with the dangerous_business schematics, next to an editor at 0 100 0
|
|
async function setup() {
|
|
const dir = Deno.makeTempDirSync({ prefix: "bworld_dev_" });
|
|
copy_dir("mods/dangerous_business", `${dir}/dangerous_business`);
|
|
copy_dir("mods/dev", `${dir}/dev`);
|
|
const t = await test_game(dir);
|
|
Deno.removeSync(dir, { recursive: true });
|
|
t.join(1, "alice");
|
|
t.send(1, { type: "move", x: 0.5, y: 100, z: 2.5, yaw: 0, pitch: 0 });
|
|
t.game.set_block(0, 100, 0, EDITOR);
|
|
t.take(1);
|
|
return t;
|
|
}
|
|
|
|
Deno.test("the schematic editor lists every schematic and keeps the one a player picks", async () => {
|
|
const t = await setup();
|
|
t.send(1, { type: "use_block", x: 0, y: 100, z: 0, face: "top" });
|
|
const opened = t.take(1).find((m) => m.type === "mod" && m.channel === "dev:schematic_editor_open");
|
|
const ids = [...t.game.schematics.keys()];
|
|
assertEquals(opened?.data, { ids }, "nothing is picked yet");
|
|
|
|
const data = () => t.game.world.get_tile(0, 100, 0)?.mod_data as { schematic?: string } | undefined;
|
|
t.send(1, { type: "mod", channel: "dev:schematic_editor_select", data: ids[1] });
|
|
assertEquals(data()?.schematic, ids[1]);
|
|
// one that isn't registered is ignored
|
|
t.send(1, { type: "mod", channel: "dev:schematic_editor_select", data: "dev:nothing" });
|
|
assertEquals(data()?.schematic, ids[1]);
|
|
|
|
t.send(1, { type: "use_block", x: 0, y: 100, z: 0, face: "top" });
|
|
const reopened = t.take(1).find((m) => m.type === "mod" && m.channel === "dev:schematic_editor_open");
|
|
assertEquals(reopened?.data, { ids, selected: ids[1] });
|
|
});
|
|
|
|
Deno.test("the schematic editor places its schematic above itself, saves it with changes, and clears it", async () => {
|
|
const t = await setup();
|
|
const action = (data: string) => t.send(1, { type: "mod", channel: "dev:schematic_editor_action", data });
|
|
const chat = () => t.take(1).filter((m) => m.type === "chat").map((m) => m.text as string);
|
|
// the smallest, so placing it is quick
|
|
const volume = (id: string) => t.game.schematics.get(id)!.schematic.size.reduce((a, b) => a * b);
|
|
const id = [...t.game.schematics.keys()].sort((a, b) => volume(a) - volume(b))[0];
|
|
const { schematic, palette } = t.game.schematics.get(id)!;
|
|
const solid = schematic.blocks.find((block) => palette[block.i].id !== "cubi:air")!;
|
|
const [x, y, z] = [solid.p[0], 101 + solid.p[1], solid.p[2]];
|
|
|
|
t.send(1, { type: "use_block", x: 0, y: 100, z: 0, face: "top" });
|
|
action("place");
|
|
assert(chat().some((text) => text.includes("Pick a schematic")), "nothing to place yet");
|
|
t.send(1, { type: "mod", channel: "dev:schematic_editor_select", data: id });
|
|
action("place");
|
|
assertEquals(t.game.world.get_block_id(x, y, z), palette[solid.i].id);
|
|
|
|
// what's saved is the box as it is now
|
|
t.game.set_block(x, y, z, "cubi:air");
|
|
action("save");
|
|
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
const path = chat().map((text) => text.match(/^Saved .+ to (.+)$/)?.[1]).find((path) => path);
|
|
assertMatch(path ?? "", /\.cbsm$/);
|
|
const saved = decode_schematic(t.schematics.get(path!)!);
|
|
assertEquals(saved.size, schematic.size);
|
|
const at = saved.blocks.find((block) => block.p.join() === solid.p.join())!;
|
|
assertEquals(saved.palette[at.i].id, "cubi:air");
|
|
|
|
t.game.set_block(x, y, z, palette[solid.i].id);
|
|
action("clear");
|
|
assertEquals(t.game.world.get_block_id(x, y, z), "cubi:air");
|
|
assertEquals(t.game.world.get_block_id(0, 100, 0), EDITOR, "the editor stays");
|
|
});
|