Loading schematics

This commit is contained in:
2026-11-15 19:54:08 -03:00
parent 6bb2c87373
commit 0564275f58
15 changed files with 142 additions and 17 deletions
+25 -1
View File
@@ -3,7 +3,7 @@
// exporting a box of the world as a schematic file, with /exportschematic, and placing registered schematics
import { assert, assertAlmostEquals, assertEquals, assertStringIncludes, assertThrows } from "@std/assert";
import { decode_schematic, type Schematic } from "@/common/schematic.ts";
import { decode_schematic, encode_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 { get_state_value, set_state_value } from "@/common/utils.ts";
@@ -127,6 +127,12 @@ export function server(ctx: ServerContext) {
} catch (e) {
ctx.storage.set("broken", (e as Error).message);
}
// the file's is registered before scripts run
try {
ctx.schematics.register("testmod:post", { size: [1, 1, 1], palette: [], blocks: [], entities: [] });
} catch (e) {
ctx.storage.set("post", (e as Error).message);
}
(globalThis as Record<string, unknown>).placing = ctx.schematics;
}
`;
@@ -147,6 +153,16 @@ async function place_setup() {
}),
);
Deno.writeTextFileSync(`${dir}/testmod/scripts/main.ts`, PLACE_SCRIPT);
Deno.mkdirSync(`${dir}/testmod/schematics`);
Deno.writeFileSync(
`${dir}/testmod/schematics/post.cbsm`,
encode_schematic({
size: [1, 2, 1],
palette: [{ id: "bworld:oak_log" }],
blocks: [{ i: 0, p: [0, 0, 0] }, { i: 0, p: [0, 1, 0] }],
entities: [],
}),
);
const t = await test_game(dir);
Deno.removeSync(dir, { recursive: true });
stone_floor(t.game);
@@ -228,3 +244,11 @@ Deno.test("a schematic is checked when it's registered, and a placement before a
);
assertEquals(t.game.world.get_block_id(0, 100, 0), "cubi:air");
});
Deno.test("a mod's schematics/<name>.cbsm files are registered as <mod id>:<name> before its scripts run", async () => {
const t = await place_setup();
assertStringIncludes(String(t.game.mods.storage.testmod?.post), "registered twice");
t.schematics.place("testmod:post", { position: { x: 3, y: 100, z: 4 } });
assertEquals(t.game.world.get_block_id(3, 100, 4), "bworld:oak_log");
assertEquals(t.game.world.get_block_id(3, 101, 4), "bworld:oak_log");
});