117 lines
3.3 KiB
Markdown
117 lines
3.3 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 palette.palette[number].data is from BlockComponent.export_data
|
|
|
|
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": { "items": [] }
|
|
}
|
|
],
|
|
"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
|
|
|
|
```typescript
|
|
ctx.schematics.place(id, { position, dimension, position_pivot, rotation, mirror });
|
|
```
|
|
|
|
### 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
|
|
|
|
```typescript
|
|
ctx.schematics.export(dimension, from, to); // saves the schematic file to a temporary directory and prints the location on the chat for the player
|
|
```
|
|
|
|
And a `/exportschematic [x1] [y1] [z1] [x2] [y2] [z2]` command that calls ctx.schematics.export on the player's
|
|
dimension
|