Placing schematics
This commit is contained in:
+73
-32
@@ -47,10 +47,10 @@ import { collision_shape_at, type Shape } from "@/common/block_shapes.ts";
|
||||
import { physics } from "@/common/physics.ts";
|
||||
import { EXHAUSTION } from "./vitals.ts";
|
||||
import { entity_ref, ModRuntime, new_stack, run_guarded, type ServerCommandSource } from "./mod_runtime.ts";
|
||||
import type { LootContext, Position, UseTarget } from "@/common/mod_api/server.ts";
|
||||
import type { LootContext, Position, SchematicPlacement, UseTarget } from "@/common/mod_api/server.ts";
|
||||
import type { EntityJson } from "@/common/mod_data.ts";
|
||||
import { encode_schematic } from "@/common/schematic.ts";
|
||||
import { capture_schematic } from "./schematics.ts";
|
||||
import { capture_schematic, placed_schematic, type RegisteredSchematic } from "./schematics.ts";
|
||||
import {
|
||||
argument,
|
||||
choice,
|
||||
@@ -88,8 +88,8 @@ import { Mob } from "./entity/mob.ts";
|
||||
import { LivingEntity } from "./entity/living_entity.ts";
|
||||
import { EntityManager } from "./entity/entity_manager.ts";
|
||||
import { NaturalSpawner } from "./natural_spawner.ts";
|
||||
import { create_entity, entity_type_ids } from "./entity/entity_types.ts";
|
||||
import { damage_source, type DamageSource, type SavedEntity } from "./entity/entity.ts";
|
||||
import { create_entity, entity_type_ids, load_entity } from "./entity/entity_types.ts";
|
||||
import { damage_source, type DamageSource, type Entity, type SavedEntity } from "./entity/entity.ts";
|
||||
import { effect_type, effect_type_ids, EffectInstance } from "./entity/effects.ts";
|
||||
import { ATTRIBUTES } from "./entity/attributes.ts";
|
||||
|
||||
@@ -183,6 +183,8 @@ export class GameServer {
|
||||
#mods: GameMods;
|
||||
// ctx.containers, saved with the world
|
||||
containers = new Map<string, Container>();
|
||||
// by id, what ctx.schematics.register added for place
|
||||
readonly schematics = new Map<string, RegisteredSchematic>();
|
||||
// everything in the world that isn't a block or a player
|
||||
entities: EntityManager;
|
||||
mods: ModRuntime;
|
||||
@@ -693,34 +695,8 @@ export class GameServer {
|
||||
// replaces a block: the old one's on_break, then the new one's on_create, then the blocks beside it hear of it
|
||||
set_block(x: number, y: number, z: number, id: string, player?: ServerPlayer, state?: number) {
|
||||
this.mods.rules.change(() => {
|
||||
const old_id = this.world.get_block_id(x, y, z);
|
||||
if (this.#break_stages.size > 0) this.set_break_progress(x, y, z, undefined);
|
||||
if (old_id !== AIR_ID) {
|
||||
const components = this.mods.components_of(old_id).filter(({ component }) => component.on_break);
|
||||
if (components.length > 0) {
|
||||
const block = this.mods.block_ref(x, y, z, old_id);
|
||||
const player_api = player && this.mods.player(player);
|
||||
for (const { mod, id: component_id, component, params } of components) {
|
||||
run_guarded(
|
||||
mod,
|
||||
`${component_id} on_break`,
|
||||
() => component.on_break!(block, params, player_api),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
this.world.remove_tile(x, y, z);
|
||||
this.world.set_block(x, y, z, id, state);
|
||||
|
||||
if (id !== AIR_ID) {
|
||||
const block = this.mods.block_ref(x, y, z, id);
|
||||
for (const { mod, id: component_id, component, params } of this.mods.components_of(id)) {
|
||||
if (component.on_create) {
|
||||
run_guarded(mod, `${component_id} on_create`, () => component.on_create!(block, params));
|
||||
}
|
||||
}
|
||||
}
|
||||
this.#replace_block(x, y, z, id, state, player);
|
||||
this.#create_block(x, y, z, id);
|
||||
this.mods.rules.notify_neighbors(x, y, z);
|
||||
|
||||
// a block standing on this one that can't stand on what it is now breaks, like a crop on farmland that was
|
||||
@@ -734,6 +710,35 @@ export class GameServer {
|
||||
});
|
||||
}
|
||||
|
||||
// the old block's on_break, then the new one without a tile. nothing else hears of it yet
|
||||
#replace_block(x: number, y: number, z: number, id: string, state?: number, player?: ServerPlayer) {
|
||||
const old_id = this.world.get_block_id(x, y, z);
|
||||
if (this.#break_stages.size > 0) this.set_break_progress(x, y, z, undefined);
|
||||
if (old_id !== AIR_ID) {
|
||||
const components = this.mods.components_of(old_id).filter(({ component }) => component.on_break);
|
||||
if (components.length > 0) {
|
||||
const block = this.mods.block_ref(x, y, z, old_id);
|
||||
const player_api = player && this.mods.player(player);
|
||||
for (const { mod, id: component_id, component, params } of components) {
|
||||
run_guarded(mod, `${component_id} on_break`, () => component.on_break!(block, params, player_api));
|
||||
}
|
||||
}
|
||||
}
|
||||
this.world.remove_tile(x, y, z);
|
||||
this.world.set_block(x, y, z, id, state);
|
||||
}
|
||||
|
||||
// its components' on_create
|
||||
#create_block(x: number, y: number, z: number, id: string) {
|
||||
if (id === AIR_ID) return;
|
||||
const block = this.mods.block_ref(x, y, z, id);
|
||||
for (const { mod, id: component_id, component, params } of this.mods.components_of(id)) {
|
||||
if (component.on_create) {
|
||||
run_guarded(mod, `${component_id} on_create`, () => component.on_create!(block, params));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// the cracks machines are making on blocks, as the stage clients draw, by position. see set_break_progress
|
||||
#break_stages = new Map<string, number>();
|
||||
|
||||
@@ -924,6 +929,42 @@ export class GameServer {
|
||||
return this.#host.write_schematic(encode_schematic(capture_schematic(this, from, to)));
|
||||
}
|
||||
|
||||
// see ctx.schematics.place. the blocks all change before any of them hears of it, so a door's halves or a torch's
|
||||
// wall are both there by then
|
||||
place_schematic(id: string, placement: SchematicPlacement) {
|
||||
const registered = this.schematics.get(id);
|
||||
if (!registered) throw new Error(`there's no schematic ${id}`);
|
||||
const { blocks, entities } = placed_schematic(registered, placement);
|
||||
const placed = blocks.filter(({ y }) => this.world.shape.contains(y));
|
||||
this.mods.rules.change(() => {
|
||||
for (const { x, y, z, id, state } of placed) this.#replace_block(x, y, z, id, state);
|
||||
for (const { x, y, z, id, block } of placed) {
|
||||
if (id === AIR_ID) continue;
|
||||
// TODO: import_data, so blocks can make what their data points to, like a chest's container
|
||||
if (block.data !== undefined) this.set_tile_data(x, y, z, structuredClone(block.data));
|
||||
for (const [field, value] of Object.entries(block.client_data ?? {})) {
|
||||
this.set_tile_client_data(x, y, z, field, structuredClone(value));
|
||||
}
|
||||
if (block.data === undefined && block.client_data === undefined) this.#create_block(x, y, z, id);
|
||||
}
|
||||
});
|
||||
// each on its own, a big schematic tells more blocks than one change can
|
||||
for (const { x, y, z } of placed) this.mods.rules.notify_neighbors(x, y, z);
|
||||
|
||||
const seat = (vehicle: Entity) => {
|
||||
for (const passenger of vehicle.passengers) {
|
||||
vehicle.position_passenger(passenger);
|
||||
seat(passenger);
|
||||
}
|
||||
};
|
||||
for (const saved of entities) {
|
||||
const entity = load_entity(this.entities, saved);
|
||||
if (!entity) continue;
|
||||
seat(entity);
|
||||
this.entities.add_entity(entity);
|
||||
}
|
||||
}
|
||||
|
||||
// menus
|
||||
|
||||
// the id a new menu for this player gets, 1 to 100 and around, like minecraft's containerCounter
|
||||
|
||||
@@ -12,8 +12,10 @@ import {
|
||||
is_waterlogged,
|
||||
set_state_value,
|
||||
shared_items_api,
|
||||
value_with_states,
|
||||
} from "@/common/utils.ts";
|
||||
import { LootTables } from "./loot.ts";
|
||||
import { prepare_schematic } from "./schematics.ts";
|
||||
import { collision_shape_at, covers_face, raycast_blocks, type Shape } from "@/common/block_shapes.ts";
|
||||
import { AIR, CHUNK_SIZE, faces, ID_MASK } from "@/common/constants.ts";
|
||||
import { physics } from "@/common/physics.ts";
|
||||
@@ -769,15 +771,13 @@ export class ModRuntime {
|
||||
set_block: (x, y, z, id, states) => {
|
||||
const info = id === AIR_ID ? undefined : EverythingRegistry.get<BlockRegistry>("blocks", id);
|
||||
if (id !== AIR_ID && !info) throw new Error(`unknown block ${id}`);
|
||||
let value = info && game().world.default_values[game().world.block_ids[id]];
|
||||
for (const [name, state] of Object.entries(states ?? {})) {
|
||||
const bits = info?.states?.find((s) => s.name === name)?.bits;
|
||||
if (bits === undefined || !Number.isInteger(state) || state < 0 || state >= 2 ** bits) {
|
||||
throw new Error(`${id} has no state ${name} that fits ${state}`);
|
||||
}
|
||||
value = set_state_value(value!, info!, name, state)!;
|
||||
const state = info && states
|
||||
? value_with_states(game().world.block_ids[id], info, states) >>> 16
|
||||
: undefined;
|
||||
if (id === AIR_ID && states && Object.keys(states).length > 0) {
|
||||
throw new Error(`${id} has no state ${Object.keys(states)[0]}`);
|
||||
}
|
||||
game().set_block(x, y, z, id, undefined, states && value !== undefined ? value >>> 16 : undefined);
|
||||
game().set_block(x, y, z, id, undefined, state);
|
||||
return true;
|
||||
},
|
||||
break_block: (x, y, z, options = {}) => {
|
||||
@@ -1061,6 +1061,15 @@ export class ModRuntime {
|
||||
},
|
||||
schematics: {
|
||||
export: (from, to) => game().export_schematic(from, to),
|
||||
register: (id, schematic) => {
|
||||
register.new_id("schematic", id, game().schematics.has(id));
|
||||
try {
|
||||
game().schematics.set(id, prepare_schematic(game(), schematic));
|
||||
} catch (e) {
|
||||
register.fail(`schematic ${id}: ${(e as Error).message}`);
|
||||
}
|
||||
},
|
||||
place: (id, placement) => game().place_schematic(id, placement),
|
||||
},
|
||||
menus: {
|
||||
extend_player_menu: (build) => {
|
||||
|
||||
+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