Schematics editor wip
This commit is contained in:
@@ -0,0 +1,4 @@
|
|||||||
|
# Dev
|
||||||
|
|
||||||
|
Tools for making the game from inside it. It depends on no mod, so it works alongside any game, like
|
||||||
|
`MODS=dangerous_business,dev`.
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
{
|
||||||
|
"format_version": 1,
|
||||||
|
"block": {
|
||||||
|
"id": "dev:schematic_editor",
|
||||||
|
"textures": "cubi:missing",
|
||||||
|
"interactive": true,
|
||||||
|
"components": {
|
||||||
|
"dev:schematic_editor": {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
{
|
||||||
|
"format_version": 1,
|
||||||
|
"id": "dev",
|
||||||
|
"name": "Dev",
|
||||||
|
"description": "Tools for making the game in game, like editing schematics. Works with any mod.",
|
||||||
|
"version": "0.1.0",
|
||||||
|
"authors": [
|
||||||
|
"paula"
|
||||||
|
],
|
||||||
|
"game_version": ">=0.1.0",
|
||||||
|
"main": "scripts/main.ts"
|
||||||
|
}
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
|
||||||
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||||
|
|
||||||
|
// tools for making the game from inside it. it depends on no mod, so it can be added to any game
|
||||||
|
import type { ClientContext } from "cubi/client";
|
||||||
|
import type { ServerContext } from "cubi/server";
|
||||||
|
import { register_schematic_editor, register_schematic_editor_screen } from "./schematic_editor.ts";
|
||||||
|
|
||||||
|
export function server(ctx: ServerContext) {
|
||||||
|
register_schematic_editor(ctx);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function client(ctx: ClientContext) {
|
||||||
|
register_schematic_editor_screen(ctx);
|
||||||
|
}
|
||||||
@@ -0,0 +1,137 @@
|
|||||||
|
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
|
||||||
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||||
|
|
||||||
|
// a block for working on schematics in game. right clicking it opens a screen listing every registered schematic. the
|
||||||
|
// one picked is kept in its data, and can be placed above the block, cleared away again, or saved after changing it
|
||||||
|
import type { ClientContext } from "cubi/client";
|
||||||
|
import type { Player, Position, ServerContext } from "cubi/server";
|
||||||
|
|
||||||
|
const EDITOR = "dev:schematic_editor";
|
||||||
|
// the server tells a player to open the screen, and the client tells the server what they picked or pressed
|
||||||
|
const OPEN = "dev:schematic_editor_open";
|
||||||
|
const SELECT = "dev:schematic_editor_select";
|
||||||
|
const ACTION = "dev:schematic_editor_action";
|
||||||
|
|
||||||
|
interface OpenMessage {
|
||||||
|
ids: string[];
|
||||||
|
selected?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
type Action = "place" | "clear" | "save";
|
||||||
|
|
||||||
|
interface EditorData {
|
||||||
|
schematic?: string;
|
||||||
|
// the box the last placed schematic fills, both corners in it, for clear and save
|
||||||
|
placed?: { from: Position; to: Position };
|
||||||
|
}
|
||||||
|
|
||||||
|
export function register_schematic_editor(ctx: ServerContext) {
|
||||||
|
// the editor each player last opened, by player id, so what they do goes to that block
|
||||||
|
const opened = new Map<string, Position>();
|
||||||
|
const editor_of = (player: Player) => {
|
||||||
|
const at = opened.get(player.id);
|
||||||
|
const block = at && ctx.world.get_block_ref(at.x, at.y, at.z);
|
||||||
|
return block?.id === EDITOR ? block : undefined;
|
||||||
|
};
|
||||||
|
|
||||||
|
ctx.components.register_block(EDITOR, {
|
||||||
|
on_interact(block, _params, player) {
|
||||||
|
opened.set(player.id, { x: block.x, y: block.y, z: block.z });
|
||||||
|
const message: OpenMessage = { ids: ctx.schematics.ids(), selected: block.data?.schematic };
|
||||||
|
ctx.net.send(player, OPEN, message);
|
||||||
|
return true;
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
ctx.net.on<string>(SELECT, (player, schematic) => {
|
||||||
|
const block = editor_of(player);
|
||||||
|
if (!block || !ctx.schematics.ids().includes(schematic)) return;
|
||||||
|
block.data = { ...block.data, schematic };
|
||||||
|
});
|
||||||
|
|
||||||
|
ctx.net.on<Action>(ACTION, async (player, action) => {
|
||||||
|
const block = editor_of(player);
|
||||||
|
if (!block) return;
|
||||||
|
const data: EditorData = block.data ?? {};
|
||||||
|
if (action === "place") {
|
||||||
|
const schematic = data.schematic && ctx.schematics.get(data.schematic);
|
||||||
|
if (!schematic) return player.send_message("Pick a schematic first");
|
||||||
|
// a smaller one placed over a bigger one would leave the rest of it behind
|
||||||
|
if (data.placed) ctx.world.fill(data.placed.from, data.placed.to, "cubi:air");
|
||||||
|
const from = { x: block.x, y: block.y + 1, z: block.z };
|
||||||
|
const [sx, sy, sz] = schematic.size;
|
||||||
|
ctx.schematics.place(schematic.id!, { position: from });
|
||||||
|
block.data = {
|
||||||
|
...data,
|
||||||
|
placed: { from, to: { x: from.x + sx - 1, y: from.y + sy - 1, z: from.z + sz - 1 } },
|
||||||
|
};
|
||||||
|
} else if (action === "clear") {
|
||||||
|
if (!data.placed) return;
|
||||||
|
ctx.world.fill(data.placed.from, data.placed.to, "cubi:air");
|
||||||
|
block.data = { ...data, placed: undefined };
|
||||||
|
} else if (action === "save") {
|
||||||
|
if (!data.placed) return player.send_message("Place a schematic first");
|
||||||
|
try {
|
||||||
|
player.send_message(
|
||||||
|
`Saved ${data.schematic} to ${await ctx.schematics.export(data.placed.from, data.placed.to)}`,
|
||||||
|
);
|
||||||
|
} catch (e) {
|
||||||
|
player.send_message(`Couldn't save it: ${(e as Error).message}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// how many schematics fit on a page of the list, and the size of the buttons
|
||||||
|
const PAGE_SIZE = 10;
|
||||||
|
const BUTTON_WIDTH = 200;
|
||||||
|
const BUTTON_HEIGHT = 20;
|
||||||
|
|
||||||
|
export function register_schematic_editor_screen(ctx: ClientContext) {
|
||||||
|
ctx.net.on<OpenMessage>(OPEN, ({ ids, selected }) => {
|
||||||
|
const pages = Math.max(1, Math.ceil(ids.length / PAGE_SIZE));
|
||||||
|
let page = Math.floor(Math.max(0, ids.indexOf(selected ?? "")) / PAGE_SIZE);
|
||||||
|
ctx.ui.open({
|
||||||
|
on_render(g) {
|
||||||
|
const width = BUTTON_WIDTH + 20;
|
||||||
|
const height = (PAGE_SIZE + 2) * BUTTON_HEIGHT + 50;
|
||||||
|
const left = (g.width - width) / 2;
|
||||||
|
const top = (g.height - height) / 2;
|
||||||
|
const x = left + 10;
|
||||||
|
g.panel(left, top, width, height);
|
||||||
|
g.text(`Schematic: ${selected ?? "none"}`, x, top + 8);
|
||||||
|
|
||||||
|
const shown = ids.slice(page * PAGE_SIZE, (page + 1) * PAGE_SIZE);
|
||||||
|
shown.forEach((id, i) => {
|
||||||
|
const label = id === selected ? `> ${id} <` : id;
|
||||||
|
if (g.button(label, x, top + 22 + i * BUTTON_HEIGHT, BUTTON_WIDTH, BUTTON_HEIGHT)) {
|
||||||
|
selected = id;
|
||||||
|
ctx.net.send(SELECT, id);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
const pager = top + 26 + PAGE_SIZE * BUTTON_HEIGHT;
|
||||||
|
const arrow = 30;
|
||||||
|
if (page > 0 && g.button("<", x, pager, arrow, BUTTON_HEIGHT)) page--;
|
||||||
|
if (page < pages - 1 && g.button(">", x + BUTTON_WIDTH - arrow, pager, arrow, BUTTON_HEIGHT)) page++;
|
||||||
|
const label = `${page + 1} / ${pages}`;
|
||||||
|
g.text(
|
||||||
|
label,
|
||||||
|
x + (BUTTON_WIDTH - g.measure_text(label)) / 2,
|
||||||
|
pager + (BUTTON_HEIGHT - g.text_height) / 2,
|
||||||
|
);
|
||||||
|
|
||||||
|
const actions = pager + BUTTON_HEIGHT + 4;
|
||||||
|
const action_width = BUTTON_WIDTH / 4;
|
||||||
|
(["place", "clear", "save"] as const).forEach((action, i) => {
|
||||||
|
const name = action[0].toUpperCase() + action.slice(1);
|
||||||
|
if (g.button(name, x + i * action_width, actions, action_width, BUTTON_HEIGHT)) {
|
||||||
|
ctx.net.send(ACTION, action);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
// the preview isn't built yet
|
||||||
|
g.button("Preview", x + 3 * action_width, actions, action_width, BUTTON_HEIGHT);
|
||||||
|
},
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
@@ -0,0 +1,77 @@
|
|||||||
|
// 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");
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user