Files
bworld/STRUCTURES.md
T
2026-11-15 19:54:08 -03:00

130 lines
4.2 KiB
Markdown

# Structures
## Schematics
Schematics are pre-defined block patterns to be placed somewhere in the world
### Schematic format
Schematics are json data, encoded and decoded with MessagePack (@std/msgpack), they are saved in .cbsm files
On the schematic the objects have `i`, `s` and `p` keys, these correspond to `index`, `state` and `position`, they are
shorten to save on bytes on msgpack
The data on blocks[number].data is from BlockComponent.export_data. A block's client data, like the item a cutting board
holds, goes in blocks[number].client_data. `id` is left out of exported schematics, it's set when one is registered
Example:
```json
{
"format_version": 1,
"schematic": {
"id": "bworld:my_schematic",
"size": [2, 2, 1],
"palette": [
{ "id": "bworld:stone" },
{ "id": "bworld:dirt" },
{
"id": "bworld:furnace",
"s": { "facing": 1 }
}
],
"blocks": [
{
"i": 0,
"p": [0, 0, 0]
},
{
"i": 1,
"p": [1, 0, 0]
},
{
"i": 2,
"p": [1, 1, 0],
"data": { "progress": 0, "container": [{ "id": "bworld:coal", "count": 5 }, null, null] }
}
],
"entities": [
{
"p": [0.5, 1.0, 0.5],
"id": "bworld:chicken",
"data": {}
}
]
}
}
```
Schematics will come from the schematics directory inside mods or through ctx.schematics.register() function
## Block data serialization
BlockComponent will have new methods:
```typescript
// the block's data as it's kept outside the world, with its containers' items written in
export_data?(block: BlockRef, params: P): unknown;
// the other way, for a block placed from a structure: creates its containers from those items
import_data?(block: BlockRef, params: P, data: unknown): void;
```
Blocks without the hooks just copy data unchanged, which is fine for plain fields.
### Placing schematics
Built without `import_data`, see `docs/modding/misc/schematics.md`:
```typescript
ctx.schematics.register(id, schematic);
ctx.schematics.place(id, { position, position_pivot, rotation, mirror });
```
Mods' `schematics/<name>.cbsm` files are registered as `<mod id>:<name>` before scripts run. They're decoded when the
mod is checked and packed into its `data.json` like its other server only data, so players don't get them. There's no
`dimension` yet, like export. Blocks with data in the schematic get it as it is and skip `on_create`, the rest get their
`on_create`, then every placed block tells its neighbors.
### Rotation and mirroring
- `rotation` is `0`, `90`, `180` or `270` degrees clockwise seen from above, the same as the `"y"` of a block variant
- `mirror` is `"x"` or `"z"`, the axis that gets flipped, or left out for none
- the mirror is applied first, then the rotation, both around `position_pivot`
Positions, relative to the pivot:
- mirror `"x"`: `x` becomes `-x`
- mirror `"z"`: `z` becomes `-z`
- each 90 degrees: `[x, z]` becomes `[-z, x]`
The only state that changes is one named `facing`, the standard one. Its values are always `0` north, `1` east, `2`
south, `3` west, then `4` up and `5` down for a 3 bit one (see `FACINGS` in `common/block_rules.ts` and the importer's
`ORDERS`):
- mirror `"x"` swaps east and west (`1` and `3`)
- mirror `"z"` swaps north and south (`0` and `2`)
- each 90 degrees adds 1 to a value under 4, wrapping around (`(facing + 1) % 4`)
- up and down never change
Every other state (`axis`, door hinges, stair shapes) is copied as it is, so those blocks can come out wrong in a turned
or mirrored schematic. A hook for them can come later if it's needed.
Entities get their position turned the same way as blocks, and their yaw with it (yaw 0 looks north, east is `-π/2`):
- mirror `"x"`: `yaw` becomes `-yaw`
- mirror `"z"`: `yaw` becomes `π - yaw`
- each 90 degrees: `yaw` becomes `yaw - π/2`
### Saving schematics
Built, see `docs/modding/misc/schematics.md`:
```typescript
// writes a .cbsm file to the system's temporary directory and resolves to its path
ctx.schematics.export(from, to);
```
It has no dimension argument yet, since a server only has one world. `/exportschematic x1 y1 z1 x2 y2 z2` calls it and
tells the player the path in chat. The game worker can't write files, so the host writes it (`write_schematic` in
`server/game/host_protocol.ts`).