Compare commits

...
4 Commits
Author SHA1 Message Date
paula 65865033c1 Vaguely import every tile 2026-11-21 10:33:28 -03:00
paula 04c9511b6c placeschematic command 2026-11-21 09:16:51 -03:00
paula c4eda256a1 docs 2026-11-21 09:10:49 -03:00
paula f36176a872 Skip blocks that don't exist on schematics 2026-11-21 09:10:43 -03:00
49 changed files with 158 additions and 24 deletions
+1 -1
View File
@@ -587,7 +587,7 @@ export interface SchematicApi {
// create's schematic and quill. resolves to where the file is. throws for a box of more than 1048576 blocks (2^20)
export(from: Position, to: Position): Promise<string>;
// 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
// every state and entity in it has to be registered. blocks that aren't are left out, with a warning
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,
+7 -1
View File
@@ -509,7 +509,13 @@ export function check_references(mods: LoadedMod[], engine_textures: Iterable<st
}
// cubi: entities, like dropped items, are the engine's
for (const { file, json } of mod.schematics) {
for (const { id } of json.palette) uses(file, id, "block");
// the server skips blocks that don't exist, so a schematic still loads without the mod they came from
for (const id of new Set(json.palette.map((entry) => entry.id))) {
check_namespace(file, id);
if (!block_owners.has(id)) {
warnings.push(`${file}: block ${id} doesn't exist, it will be left out`);
}
}
for (const { id } of json.entities) {
check_namespace(file, id);
if (!id.startsWith("cubi:") && !entity_owners.has(id)) {
+2 -1
View File
@@ -118,6 +118,7 @@ Commands can only be registered while the server phase runs.
| `/locatebiome <biome>` | Says where the nearest biome of that kind is, up to 6400 blocks away |
| `/tp <x> <y> <z>` | Teleports you there |
| `/fill <x1> <y1> <z1> <x2> <y2> <z2> <block>` | Fills the box between two corners with a block, `air` to clear it |
| `/placeschematic <id> <x> <y> <z>` | Places a registered schematic with its lowest corner there |
| `/fly` | Toggles flying: jump goes up, sneak goes down, sprint goes faster |
| `/creative` | Toggles placing blocks without using them up and instant breaking |
| `/tps` | Shows how the game loop keeps up |
@@ -125,7 +126,7 @@ Commands can only be registered while the server phase runs.
| `/skin [url] [slim\|normal]` | Changes your skin to a 64×64 PNG, or back to the default |
| `/help [command]` | Every command's usage, or one's |
`/fill` and `/exportschematic` suggest the block you're looking at for each corner.
`/fill`, `/exportschematic` and `/placeschematic` suggest the block you're looking at for each corner.
`/give`'s data is the stack's `data`, and with `fluid` it's the fluid's (see
[Fluids](../resources/fluids.md#containers)):
+8 -5
View File
@@ -36,8 +36,9 @@ Positions are from the box's lowest corner.
## Placing
A mod's `schematics/<name>.cbsm` files are its schematics `<mod id>:<name>`, so an exported file can be dropped in as it
is. `check-mods` checks that every block and entity in them exists. They're registered before any script runs, so a
server phase can place them right away. Players never download them.
is. `check-mods` checks that every block and entity in them exists. A missing block is only a warning: it's left out
when the schematic loads, so one made with blocks from another mod still works without it. They're registered before any
script runs, so a server phase can place them right away. Players never download them.
A script can also register one it made itself, by id in the server phase, then place it by that id:
@@ -50,9 +51,9 @@ ctx.schematics.place("copper_tools:hut", {
});
```
`register` checks it right away, so a schematic with a block, state or entity that isn't registered stops the mod from
loading, instead of failing when it's placed. It takes a `Schematic` (exported from `cubi/server`), the same shape as a
file's.
`register` checks it right away, so a schematic with a state or entity that isn't registered stops the mod from loading,
instead of failing when it's placed. Blocks that aren't registered are left out, with a warning. It takes a `Schematic`
(exported from `cubi/server`), the same shape as a file's.
`place` puts the whole schematic in at once, replacing what was there, air too:
@@ -72,6 +73,8 @@ file's.
It throws for an id nothing registered, or a placement that isn't one, before anything changes.
`/placeschematic id x y z` places one unturned, with its lowest corner at `x y z`.
## Reading
`get` gives a copy of a registered schematic, a file's or one a script registered, with its `id`, or `undefined` for an
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
+26 -2
View File
@@ -33,6 +33,9 @@ export interface TileType extends TileRules {
// each the bottom of a 1 by 2 opening in its outer wall, facing into the tile. tiles share their outer walls, so
// two tiles meeting at a doorway have it in the same place
doorways: Marker[];
// the doorways it can be attached to another tile by, all of them when left out. a tile that starts with a row of
// structure void, like lethal3's, is only entered there: that row is the wall of the tile it's entered from
entries?: Marker[];
// where a vent can go, facing the way the vent would
vent_spots: Marker[];
}
@@ -70,8 +73,21 @@ export const MAIN_TILE = "dangerous_business:main";
export const TILES: Record<string, TileRules> = {
[MAIN_TILE]: { weight: 0 },
"dangerous_business:2x1catwalk": { weight: 20 },
...looks("hallway", 25),
...looks("hallway_t", 15),
...looks("hallway_lt", 15),
...looks("hallway_rt", 15),
...looks("hallway_x", 15),
...looks("hallway_big_x", 3),
"dangerous_business:hallway_maze": { weight: 1 },
};
// a tile with three looks, name, name2 and name3, each as likely, together as likely as weight
function looks(name: string, weight: number): Record<string, TileRules> {
const rules = { weight: weight / 3 };
return Object.fromEntries(["", "2", "3"].map((look) => [`dangerous_business:${name}${look}`, rules]));
}
// how many times a plan that comes out with fewer rooms than it should is tried again
const PLAN_ATTEMPTS = 10;
@@ -80,11 +96,19 @@ export function tile_type(schematic: Schematic, rules: TileRules): TileType {
schematic.blocks
.filter((block) => schematic.palette[block.i].id === id)
.map(({ i, p: [x, y, z] }) => ({ x, y, z, facing: schematic.palette[i].s?.facing ?? 0 }));
const doorways = markers(DOORWAY);
// a doorway with nothing beside it along the wall is in the row of structure void
const blocks = new Set(schematic.blocks.map((block) => block.p.join()));
const entries = doorways.filter(({ x, y, z, facing }) => {
const [step_x, step_z] = facing % 2 === 0 ? [1, 0] : [0, 1];
return !blocks.has([x - step_x, y, z - step_z].join()) && !blocks.has([x + step_x, y, z + step_z].join());
});
return {
...rules,
id: schematic.id!,
size: schematic.size,
doorways: markers(DOORWAY),
doorways,
entries: entries.length ? entries : undefined,
vent_spots: markers(VENT_SPOT),
};
}
@@ -160,7 +184,7 @@ function try_plan(rng: { next(): number }, types: TileType[], config: InteriorPl
while (pool.size > 0) {
const type = pick(rng, pool);
pool.delete(type);
for (const own of shuffled(rng, type.doorways)) {
for (const own of shuffled(rng, type.entries ?? type.doorways)) {
// turned to face away from the tile it's attached to
const quarters = (doorway.facing + 2 - own.facing + 4) % 4;
const [x, z] = turned(own.x, own.z, quarters);
+21 -7
View File
@@ -2,7 +2,7 @@ import type { ClientContext, Color } from "cubi/client";
import type { ServerContext } from "cubi/server";
import { DOOR, FACILITY_DOOR } from "./shared.ts";
import { Alea } from "@paulaboks/rng";
import { MAIN_TILE, type Marker, type PlacedTile, plan_interior, tile_type, TILES } from "./interior.ts";
import { DOORWAY, MAIN_TILE, type Marker, type PlacedTile, plan_interior, tile_type, TILES } from "./interior.ts";
export { shared } from "./shared.ts";
@@ -148,7 +148,7 @@ const INTERIOR_ORIGIN = { x: 0, y: 60, z: 0 };
// how likely a doorway between two rooms is to get a door, otherwise it's left open
const DOOR_CHANCE = 0.8;
const INTERIOR_DOOR = "dangerous_business:factory_door";
const DOORWAY_WALL = "dangerous_business:sheet_wall";
const DOORWAY_WALL = "dangerous_business:concrete_brick_wall";
// how likely a vent spot is to get a vent, otherwise it's left empty
const VENT_CHANCE = 0.65;
const VENT = "dangerous_business:vent";
@@ -168,13 +168,12 @@ function generate_interior(ctx: ServerContext, config: InteriorGenerationConfig)
ctx.schematics.place(tile, { position, rotation });
}
for (const { x, y, z, facing, connected } of plan.doorways) {
if (!connected) {
ctx.world.set_block(x, y, z, DOORWAY_WALL);
ctx.world.set_block(x, y + 1, z, DOORWAY_WALL);
} else if (rng.next() < DOOR_CHANCE) {
const door = connected && rng.next() < DOOR_CHANCE;
if (!connected || door) wall_off_doorway(ctx, x, y, z, facing, door);
if (door) {
ctx.world.set_block(x, y, z, INTERIOR_DOOR, { facing, half: 0 });
ctx.world.set_block(x, y + 1, z, INTERIOR_DOOR, { facing, half: 1 });
} else {
} else if (connected) {
ctx.world.set_block(x, y, z, "cubi:air");
}
}
@@ -189,3 +188,18 @@ function generate_interior(ctx: ServerContext, config: InteriorGenerationConfig)
}
interior_tiles = plan.tiles;
}
// fills the opening a doorway is in, 3 wide and 4 tall like lethal3's or 1 by 2, leaving the doorway itself when a
// door goes there. only air and the doorway are filled, so the wall around a smaller opening stays as it is
function wall_off_doorway(ctx: ServerContext, x: number, y: number, z: number, facing: number, door: boolean) {
// along the wall
const [step_x, step_z] = facing % 2 === 0 ? [1, 0] : [0, 1];
for (let side = -1; side <= 1; side++) {
for (let up = 0; up < 4; up++) {
if (door && side === 0 && up < 2) continue;
const [at_x, at_y, at_z] = [x + side * step_x, y + up, z + side * step_z];
const id = ctx.world.get_block(at_x, at_y, at_z);
if (id === "cubi:air" || id === DOORWAY) ctx.world.set_block(at_x, at_y, at_z, DOORWAY_WALL);
}
}
}
+23
View File
@@ -1882,6 +1882,29 @@ export class GameServer {
.then(coordinates(["x1", "y1", "z1", "x2", "y2", "z2"], (context) => this.#export_schematic(context))),
);
// TODO: only let operators do this
register(
literal<ServerCommandSource>("placeschematic")
.describe("Places a schematic with its lowest corner at x y z")
.then(
argument<string, ServerCommandSource>("schematic", word())
.suggests((partial) =>
[...this.schematics.keys()].filter((id) => id.startsWith(partial)).map((text) => ({ text }))
)
.then(coordinates(["x", "y", "z"], (context) => {
const id = context.get<string>("schematic");
if (!this.schematics.has(id)) throw new CommandError(`There's no schematic ${id}`);
const [x, y, z] = ["x", "y", "z"].map((axis) => context.get<number>(axis));
try {
this.place_schematic(id, { position: { x, y, z } });
} catch (e) {
throw new CommandError(`Couldn't place it: ${(e as Error).message}`);
}
context.source.send_message(`Placed ${id} at ${x} ${y} ${z}`);
})),
),
);
// TODO: only let operators do this
register(
literal<ServerCommandSource>("fill")
+1 -1
View File
@@ -73,7 +73,7 @@ export async function start_game(
for (const mod of mods) {
for (const schematic of mod.data.schematics ?? []) {
try {
game.schematics.set(schematic.id!, prepare_schematic(game, schematic));
game.schematics.set(schematic.id!, prepare_schematic(game, schematic.id!, schematic));
} catch (e) {
throw new ModLoadError(mod.listing.id, `schematic ${schematic.id}: ${(e as Error).message}`);
}
+1 -1
View File
@@ -1064,7 +1064,7 @@ export class ModRuntime {
register: (id, schematic) => {
register.new_id("schematic", id, game().schematics.has(id));
try {
game().schematics.set(id, prepare_schematic(game(), schematic));
game().schematics.set(id, prepare_schematic(game(), id, schematic));
} catch (e) {
register.fail(`schematic ${id}: ${(e as Error).message}`);
}
+15 -4
View File
@@ -109,8 +109,9 @@ export interface RegisteredSchematic {
palette: { id: string; value: number }[];
}
// a copy of the schematic, checked once so placing it can't fail halfway. throws saying what's wrong with it
export function prepare_schematic(game: GameServer, json: Schematic): RegisteredSchematic {
// a copy of the schematic, checked once so placing it can't fail halfway. blocks that aren't registered are left out,
// with a warning. throws saying what's wrong with it
export function prepare_schematic(game: GameServer, id: string, json: Schematic): RegisteredSchematic {
const problems = schematic_problems(json);
if (problems.length > 0) throw new Error(problems.slice(0, 5).join(", "));
const schematic = structuredClone(json);
@@ -118,10 +119,15 @@ export function prepare_schematic(game: GameServer, json: Schematic): Registered
if (volume > MAX_SCHEMATIC_VOLUME) {
throw new Error(`a schematic holds up to ${MAX_SCHEMATIC_VOLUME} blocks, and this one is ${volume}`);
}
const palette = schematic.palette.map(({ id, s }) => {
// palette entries whose block isn't registered
const missing = new Set<number>();
const palette = schematic.palette.map(({ id, s }, i) => {
if (id === AIR_ID) return { id, value: AIR };
const info = EverythingRegistry.get<BlockRegistry>("blocks", id);
if (!info) throw new Error(`it has ${id}, which isn't a block`);
if (!info) {
missing.add(i);
return { id, value: AIR };
}
return { id, value: value_with_states(game.world.block_ids[id], info, s ?? {}) };
});
for (const { i, p } of schematic.blocks) {
@@ -135,6 +141,11 @@ export function prepare_schematic(game: GameServer, json: Schematic): Registered
for (const { id } of schematic.entities) {
if (!has_entity_type(id)) throw new Error(`it has ${id}, which isn't an entity`);
}
if (missing.size > 0) {
schematic.blocks = schematic.blocks.filter(({ i }) => !missing.has(i));
const ids = [...missing].map((i) => palette[i].id).join(", ");
console.warn(`Schematic ${id} has blocks that don't exist, leaving them out: ${ids}`);
}
return { schematic, palette };
}
+24
View File
@@ -121,3 +121,27 @@ Deno.test("vent spots are in the plan where their rooms put them, turned with th
assert(spots.length > 0);
assertEquals(result.vent_spots, spots);
});
Deno.test("a room with entries is only attached to another by them", () => {
const types: TileType[] = [
{ id: "test:main", size: [9, 6, 9], doorways: cross, vent_spots: [], weight: 0 },
{ id: "test:hallway", size: [9, 6, 9], doorways: cross, entries: [cross[0]], vent_spots: [], weight: 1 },
];
for (const seed of ["a", "b", "c"]) {
const result = plan_interior(new Alea(seed), types, {
main: "test:main",
origin: { x: 0, y: 0, z: 0 },
rooms: 20,
});
const connected = new Set(result.doorways.filter((d) => d.connected).map(({ x, y, z }) => `${x},${y},${z}`));
for (const { tile, position, rotation } of result.tiles.slice(1)) {
// the entry, the north doorway, where it is once the hallway is turned
let [x, z] = [cross[0].x, cross[0].z];
for (let i = 0; i < rotation / 90; i++) [x, z] = [-z, x];
assert(
connected.has(`${position.x + x},${position.y + cross[0].y},${position.z + z}`),
`${tile} at ${seed}`,
);
}
}
});
+29 -1
View File
@@ -123,10 +123,16 @@ export function server(ctx: ServerContext) {
entities: [{ p: [1.5, 1, 0.5], id: "bworld:chicken", data: { rotation: [-Math.PI / 2, 0] } }],
});
try {
ctx.schematics.register("testmod:broken", { size: [1, 1, 1], palette: [{ id: "testmod:nothing" }], blocks: [{ i: 0, p: [0, 0, 0] }], entities: [] });
ctx.schematics.register("testmod:broken", { size: [1, 1, 1], palette: [], blocks: [], entities: [{ p: [0.5, 0, 0.5], id: "testmod:nothing", data: {} }] });
} catch (e) {
ctx.storage.set("broken", (e as Error).message);
}
ctx.schematics.register("testmod:partial", {
size: [1, 2, 1],
palette: [{ id: "testmod:nothing" }, { id: "bworld:oak_planks" }],
blocks: [{ i: 0, p: [0, 0, 0] }, { i: 1, p: [0, 1, 0] }],
entities: [],
});
// the file's is registered before scripts run
try {
ctx.schematics.register("testmod:post", { size: [1, 1, 1], palette: [], blocks: [], entities: [] });
@@ -245,6 +251,28 @@ 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("blocks that aren't registered are left out of a schematic, and the rest is placed", async () => {
const t = await place_setup();
t.game.set_block(5, 100, 5, "bworld:stone");
t.schematics.place("testmod:partial", { position: { x: 5, y: 100, z: 5 } });
assertEquals(t.game.world.get_block_id(5, 100, 5), "bworld:stone");
assertEquals(t.game.world.get_block_id(5, 101, 5), "bworld:oak_planks");
});
Deno.test("/placeschematic places a registered schematic with its lowest corner at x y z", async () => {
const t = await place_setup();
t.join(1, "alice");
t.take(1);
t.send(1, { type: "chat", text: "/placeschematic testmod:post 3 100 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");
t.take(1);
t.send(1, { type: "chat", text: "/placeschematic testmod:nothing 3 100 4" });
const chat = t.take(1).filter((m: Msg) => m.type === "chat").map((m: Msg) => m.text as string);
assert(chat.some((text) => text.includes("no schematic testmod:nothing")), chat.join("\n"));
});
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")!;