Placing schematics
This commit is contained in:
+135
-3
@@ -1,15 +1,24 @@
|
||||
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
// exporting a box of the world as a schematic, see common/schematic.ts and ctx.schematics
|
||||
// exporting a box of the world as a schematic, and where a registered one's blocks and entities go when it's placed.
|
||||
// see common/schematic.ts and ctx.schematics
|
||||
import type { GameServer } from "./game_server.ts";
|
||||
import type { SavedEntity } from "./entity/entity.ts";
|
||||
import { has_entity_type } from "./entity/entity_types.ts";
|
||||
import type { Position } from "@/common/mod_api/shared.ts";
|
||||
import type { SchematicPlacement } from "@/common/mod_api/server.ts";
|
||||
import { AIR, ID_MASK } from "@/common/constants.ts";
|
||||
import { type BlockRegistry, EverythingRegistry } from "@/common/everything_registry.ts";
|
||||
import { AIR_ID } from "@/common/protocol.ts";
|
||||
import type { Schematic, SchematicBlock, SchematicEntity, SchematicPaletteEntry } from "@/common/schematic.ts";
|
||||
import { block_states } from "@/common/utils.ts";
|
||||
import {
|
||||
type Schematic,
|
||||
schematic_problems,
|
||||
type SchematicBlock,
|
||||
type SchematicEntity,
|
||||
type SchematicPaletteEntry,
|
||||
} from "@/common/schematic.ts";
|
||||
import { block_states, get_state_value, set_state_value, value_with_states, wrap_angle } from "@/common/utils.ts";
|
||||
|
||||
// the most blocks one schematic can hold, 2^20, so an export can't hold up the game for long
|
||||
export const MAX_SCHEMATIC_VOLUME = 1 << 20;
|
||||
@@ -93,3 +102,126 @@ function without_passenger_uuids(saved: SavedEntity): Record<string, unknown> {
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
// a schematic ctx.schematics.register checked, with its palette as this world's block values
|
||||
export interface RegisteredSchematic {
|
||||
schematic: Schematic;
|
||||
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 {
|
||||
const problems = schematic_problems(json);
|
||||
if (problems.length > 0) throw new Error(problems.slice(0, 5).join(", "));
|
||||
const schematic = structuredClone(json);
|
||||
const volume = schematic.size[0] * schematic.size[1] * schematic.size[2];
|
||||
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 }) => {
|
||||
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`);
|
||||
return { id, value: value_with_states(game.world.block_ids[id], info, s ?? {}) };
|
||||
});
|
||||
for (const { i, p } of schematic.blocks) {
|
||||
if (i >= palette.length) {
|
||||
throw new Error(`the block at ${p.join(" ")} is palette entry ${i}, there are ${palette.length}`);
|
||||
}
|
||||
if (p.some((v, axis) => v >= schematic.size[axis])) {
|
||||
throw new Error(`the block at ${p.join(" ")} is outside its size`);
|
||||
}
|
||||
}
|
||||
for (const { id } of schematic.entities) {
|
||||
if (!has_entity_type(id)) throw new Error(`it has ${id}, which isn't an entity`);
|
||||
}
|
||||
return { schematic, palette };
|
||||
}
|
||||
|
||||
// a block a placement puts in the world, with its state bits turned
|
||||
export interface PlacedBlock {
|
||||
x: number;
|
||||
y: number;
|
||||
z: number;
|
||||
id: string;
|
||||
state: number;
|
||||
block: SchematicBlock;
|
||||
}
|
||||
|
||||
// how a placement turns the schematic: mirrored first, then turned clockwise this many quarters
|
||||
interface Turn {
|
||||
quarters: number;
|
||||
mirror?: "x" | "z";
|
||||
}
|
||||
|
||||
// where a schematic's blocks and its entities go for a placement, see SchematicPlacement and STRUCTURES.md. the
|
||||
// entities as saved entities, ready to load. throws for a placement that isn't one
|
||||
export function placed_schematic(
|
||||
{ schematic, palette }: RegisteredSchematic,
|
||||
placement: SchematicPlacement,
|
||||
): { blocks: PlacedBlock[]; entities: SavedEntity[] } {
|
||||
const [ox, oy, oz] = whole_position(placement?.position, "position");
|
||||
const [px, , pz] = placement.position_pivot === undefined
|
||||
? [0, 0, 0]
|
||||
: whole_position(placement.position_pivot, "position_pivot");
|
||||
const { rotation = 0, mirror } = placement;
|
||||
if (![0, 90, 180, 270].includes(rotation)) throw new Error(`rotation is 0, 90, 180 or 270, not ${rotation}`);
|
||||
if (mirror !== undefined && mirror !== "x" && mirror !== "z") {
|
||||
throw new Error(`mirror is "x", "z" or left out, not ${mirror}`);
|
||||
}
|
||||
const turn: Turn = { quarters: rotation / 90, mirror };
|
||||
|
||||
const states = palette.map(({ value }) => turned_value(value, turn) >>> 16);
|
||||
const blocks = schematic.blocks.map((block) => {
|
||||
const [x, y, z] = block.p;
|
||||
const [tx, tz] = turned_xz(x - px, z - pz, turn);
|
||||
return { x: ox + px + tx, y: oy + y, z: oz + pz + tz, id: palette[block.i].id, state: states[block.i], block };
|
||||
});
|
||||
// they turn around the middle of the pivot's block, like the blocks do
|
||||
const entities = schematic.entities.map(({ p: [x, y, z], id, data }) => {
|
||||
const [tx, tz] = turned_xz(x - px - 0.5, z - pz - 0.5, turn);
|
||||
const saved = turned_entity({ ...structuredClone(data), id } as SavedEntity, turn);
|
||||
saved.pos = [ox + px + 0.5 + tx, oy + y, oz + pz + 0.5 + tz];
|
||||
return saved;
|
||||
});
|
||||
return { blocks, entities };
|
||||
}
|
||||
|
||||
function whole_position(position: Position | undefined, name: string): [number, number, number] {
|
||||
const xyz = [position?.x, position?.y, position?.z];
|
||||
if (!xyz.every(Number.isFinite)) throw new Error(`${name} needs a number for each of x, y and z`);
|
||||
return xyz.map((v) => Math.floor(v!)) as [number, number, number];
|
||||
}
|
||||
|
||||
function turned_xz(x: number, z: number, turn: Turn): [number, number] {
|
||||
if (turn.mirror === "x") x = -x;
|
||||
if (turn.mirror === "z") z = -z;
|
||||
for (let i = 0; i < turn.quarters; i++) [x, z] = [-z, x];
|
||||
return [x, z];
|
||||
}
|
||||
|
||||
// only a state named facing turns: 0 north, 1 east, 2 south, 3 west. up and down (4 and 5) stay
|
||||
function turned_value(value: number, turn: Turn): number {
|
||||
const nid = value & ID_MASK;
|
||||
const info = nid === AIR ? undefined : EverythingRegistry.get_by_id<BlockRegistry>("blocks", nid);
|
||||
const bits = info?.states?.find((s) => s.name === "facing")?.bits;
|
||||
if (!info || bits === undefined) return value;
|
||||
let facing = get_state_value(value, info, "facing")!;
|
||||
if (facing >= 4) return value;
|
||||
if (turn.mirror === "x" && facing % 2 === 1) facing = 4 - facing;
|
||||
if (turn.mirror === "z" && facing % 2 === 0) facing = 2 - facing;
|
||||
facing = (facing + turn.quarters) % 4;
|
||||
return facing < 2 ** bits ? set_state_value(value, info, "facing", facing)! : value;
|
||||
}
|
||||
|
||||
// with its yaw and its passengers' turned. yaw 0 looks north, -π/2 east
|
||||
function turned_entity(saved: SavedEntity, turn: Turn): SavedEntity {
|
||||
if (Array.isArray(saved.rotation) && Number.isFinite(saved.rotation[0])) {
|
||||
let yaw = saved.rotation[0];
|
||||
if (turn.mirror === "x") yaw = -yaw;
|
||||
if (turn.mirror === "z") yaw = Math.PI - yaw;
|
||||
saved.rotation = [wrap_angle(yaw - turn.quarters * Math.PI / 2), saved.rotation[1]];
|
||||
}
|
||||
if (Array.isArray(saved.passengers)) saved.passengers = saved.passengers.map((p) => turned_entity(p, turn));
|
||||
return saved;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user