Placing schematics
This commit is contained in:
@@ -18,8 +18,10 @@ import type {
|
||||
} from "./shared.ts";
|
||||
import type { ArgumentType, CommandNode } from "../commands.ts";
|
||||
import type { LootTableJson } from "../loot.ts";
|
||||
import type { Schematic } from "../schematic.ts";
|
||||
|
||||
export type { ArgumentType, CommandContext, CommandNode, Suggestion } from "../commands.ts";
|
||||
export type { Schematic, SchematicBlock, SchematicEntity, SchematicPaletteEntry } from "../schematic.ts";
|
||||
|
||||
export type {
|
||||
EventSignal,
|
||||
@@ -577,6 +579,25 @@ export interface SchematicApi {
|
||||
// the entities whose feet are in that box, written to a .cbsm file the host keeps in a temporary directory, like
|
||||
// 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
|
||||
register(id: Id, schematic: Schematic): void;
|
||||
// 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
|
||||
// placement that isn't one, before changing anything
|
||||
place(id: Id, placement: SchematicPlacement): void;
|
||||
}
|
||||
|
||||
export interface SchematicPlacement {
|
||||
// where the schematic's lowest corner goes, before it's turned
|
||||
position: Position;
|
||||
// the block it turns and mirrors around, from its lowest corner. 0 0 0 by default
|
||||
position_pivot?: Position;
|
||||
// clockwise seen from above. turns positions, a state named facing and entities' yaw, see STRUCTURES.md
|
||||
rotation?: 0 | 90 | 180 | 270;
|
||||
// the axis that gets flipped, before it's turned
|
||||
mirror?: "x" | "z";
|
||||
}
|
||||
|
||||
export interface ItemApi {
|
||||
|
||||
@@ -4,6 +4,8 @@
|
||||
// schematics: blocks and entities from a box of the world, kept to be placed again somewhere else. a .cbsm file is
|
||||
// one encoded with msgpack. the short keys (i, s, p) are there to keep files small, see STRUCTURES.md
|
||||
import { decode, encode } from "@std/msgpack";
|
||||
import * as z from "zod";
|
||||
import { find_problems, id, json_object, type Problems, vec3 } from "./schema.ts";
|
||||
|
||||
export const SCHEMATIC_EXTENSION = ".cbsm";
|
||||
|
||||
@@ -62,3 +64,23 @@ export function decode_schematic(bytes: Uint8Array): Schematic {
|
||||
}
|
||||
return file.schematic;
|
||||
}
|
||||
|
||||
const int = z.int();
|
||||
const block_position = z.tuple([int.min(0), int.min(0), int.min(0)], "must be [x, y, z], whole numbers from 0");
|
||||
const schematic_schema = z.object({
|
||||
id: z.string().optional(),
|
||||
size: z.tuple([int.min(1), int.min(1), int.min(1)], "must be [x, y, z], whole numbers from 1"),
|
||||
palette: z.array(z.object({ id, s: z.record(z.string(), int.min(0)).optional() })),
|
||||
blocks: z.array(z.object({
|
||||
i: int.min(0),
|
||||
p: block_position,
|
||||
data: z.unknown().optional(),
|
||||
client_data: json_object.optional(),
|
||||
})),
|
||||
entities: z.array(z.object({ p: vec3, id, data: json_object })),
|
||||
});
|
||||
|
||||
// what's wrong with the shape of a schematic, without looking at whether its blocks and entities exist
|
||||
export function schematic_problems(json: unknown): Problems {
|
||||
return find_problems(schematic_schema, json);
|
||||
}
|
||||
|
||||
@@ -189,6 +189,20 @@ export function default_block_value(nid: number, block_info: BlockRegistry | und
|
||||
return value;
|
||||
}
|
||||
|
||||
// a block's value with the states named set over its defaults, like { facing: 1 }. throws for a state it doesn't have
|
||||
// or a value that doesn't fit in it
|
||||
export function value_with_states(nid: number, block_info: BlockRegistry, states: Record<string, number>): number {
|
||||
let value = default_block_value(nid, block_info);
|
||||
for (const [name, state] of Object.entries(states)) {
|
||||
const bits = block_info.states?.find((s) => s.name === name)?.bits;
|
||||
if (bits === undefined || !Number.isInteger(state) || state < 0 || state >= 2 ** bits) {
|
||||
throw new Error(`${block_info.id} has no state ${name} that fits ${state}`);
|
||||
}
|
||||
value = set_state_value(value, block_info, name, state)!;
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
export function block_value(nid: number, state: number): number {
|
||||
return ((state << STATE_SHIFT) | nid) >>> 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user