Compare commits

...
2 Commits
Author SHA1 Message Date
paula 6c38166325 ctx.schematics.get 2026-11-17 22:14:32 -03:00
paula c0c728c066 Its almost generation time 2026-11-17 22:11:59 -03:00
7 changed files with 77 additions and 6 deletions
+4
View File
@@ -589,6 +589,10 @@ export interface SchematicApi {
// adds a schematic for place, by an id in this mod's namespace, while the server phase runs. it's checked here:
// every block, state and entity in it has to be registered
register(id: Id, schematic: Schematic): void;
// a copy of a registered schematic, a file's or one a script registered, with its id, like neoforge's
// StructureTemplateManager.get. undefined for an id nothing registered. for reading what's in it before placing it,
// like where a room's doorways are
get(id: Id): Schematic | undefined;
// puts a registered schematic in the world, all of it at once, replacing what was there, air too. replaced blocks
// get their on_break. blocks it has data for get that data as it is, the rest their on_create, then everything
// placed tells the blocks beside it. its entities come with new uuids. throws for an id nothing registered, or a
+14 -1
View File
@@ -4,7 +4,7 @@ A schematic is a box of the world saved to a file: its blocks with their states
placed again somewhere else. Schematic files end in `.cbsm` and are [MessagePack](https://msgpack.org), see
`common/schematic.ts` for their format.
Exporting, registering and placing are built. Blocks making what their data points to when they're placed
Exporting, registering, reading and placing are built. Blocks making what their data points to when they're placed
(`import_data`) is planned in `STRUCTURES.md`.
## Exporting
@@ -72,6 +72,19 @@ file's.
It throws for an id nothing registered, or a placement that isn't one, before anything changes.
## Reading
`get` gives a copy of a registered schematic, a file's or one a script registered, with its `id`, or `undefined` for an
id nothing registered. It's for looking at what's in one before placing it, like where a room's doorways are when
fitting rooms together:
```ts
const room = ctx.schematics.get("copper_tools:room")!;
const doorways = room.blocks.filter((block) => room.palette[block.i].id === "copper_tools:doorway");
```
Positions in it are from its lowest corner, before it's turned. Changing the copy doesn't change what `place` puts.
Until `import_data` is built, a block's exported data goes back in as it is, so a chest placed from a schematic has its
items' list in its data and no container.
Binary file not shown.
Binary file not shown.
+40 -5
View File
@@ -1,6 +1,7 @@
import type { ClientContext } from "cubi/client";
import type { ServerContext } from "cubi/server";
import type { ClientContext, Color } from "cubi/client";
import type { Position, ServerContext } from "cubi/server";
import { DOOR, FACILITY_DOOR } from "./shared.ts";
import { Alea } from "@paulaboks/rng";
export { shared } from "./shared.ts";
@@ -13,7 +14,23 @@ export function server(ctx: ServerContext) {
register_commands(ctx);
}
export function client(_ctx: ClientContext) {
export function client(ctx: ClientContext) {
register_hud(ctx);
}
// a small 5x5 ring in the middle of the screen to aim with, its corners and middle left empty, in gui pixels
function register_hud(ctx: ClientContext) {
const color: Color = [1, 1, 1, 1];
ctx.hud.register("dangerous_business:crosshair", {
on_render(g) {
const x = Math.floor(g.width / 2) - 2;
const y = Math.floor(g.height / 2) - 2;
g.rect(x + 1, y, 3, 1, color);
g.rect(x + 1, y + 4, 3, 1, color);
g.rect(x, y + 1, 1, 3, color);
g.rect(x + 4, y + 1, 1, 3, color);
},
});
}
// doors are two blocks, a lower and an upper half, see register_door in shared.ts. one is only placed with air above
@@ -126,7 +143,13 @@ function register_commands(ctx: ServerContext) {
);
}
const interior_tiles = [];
interface Tile {
tile: string;
position: Position;
rotation: number;
}
let interior_tiles: Tile[] = [];
function clear_map(ctx: ServerContext) {
// theres no need to clear the surface but we do need to clear each tile
@@ -139,5 +162,17 @@ function place_surface(ctx: ServerContext) {
);
}
function generate_interior(ctx: ServerContext) {
interface InteriorGenerationConfig {
size_multipler: number;
}
function plan_interior(rng: Alea, config: InteriorGenerationConfig): Tile[] {
const tiles: Tile[] = [];
return tiles;
}
function generate_interior(ctx: ServerContext, config: InteriorGenerationConfig) {
interior_tiles = plan_interior(new Alea(), config);
// generate main
// generate other rooms
}
+4
View File
@@ -1069,6 +1069,10 @@ export class ModRuntime {
register.fail(`schematic ${id}: ${(e as Error).message}`);
}
},
get: (id) => {
const registered = game().schematics.get(id);
return registered && { ...structuredClone(registered.schematic), id };
},
place: (id, placement) => game().place_schematic(id, placement),
},
menus: {
+15
View File
@@ -245,6 +245,21 @@ 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("get gives a copy of a registered schematic with its id, a file's or a script's", async () => {
const t = await place_setup();
const hut = t.schematics.get("testmod:hut")!;
assertEquals(hut.id, "testmod:hut");
assertEquals(hut.size, [2, 2, 1]);
assertEquals(hut.palette[hut.blocks.find((block) => block.p.join() === "0,1,0")!.i].id, "bworld:furnace");
assertEquals(t.schematics.get("testmod:post")?.id, "testmod:post");
assertEquals(t.schematics.get("testmod:nothing"), undefined);
// changing the copy doesn't change what's placed
hut.palette[0].id = "bworld:stone";
t.schematics.place("testmod:hut", { position: { x: 10, y: 100, z: 20 } });
assertEquals(t.game.world.get_block_id(10, 100, 20), "bworld:oak_planks");
});
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");