Files
bworld/server/game/schematics.ts
T
2026-11-15 18:41:22 -03:00

96 lines
4.0 KiB
TypeScript

// 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
import type { GameServer } from "./game_server.ts";
import type { SavedEntity } from "./entity/entity.ts";
import type { Position } from "@/common/mod_api/shared.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";
// 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;
// the blocks between two corners, both of them in it, with their tile data, and the entities whose feet are in the
// box. positions in it are from the box's lowest corner. throws for a box that's too big
export function capture_schematic(game: GameServer, from: Position, to: Position): Schematic {
for (const corner of [from, to]) {
if (![corner?.x, corner?.y, corner?.z].every(Number.isFinite)) {
throw new Error("a schematic's corners need a number for each of x, y and z");
}
}
const [x0, x1] = [Math.floor(Math.min(from.x, to.x)), Math.floor(Math.max(from.x, to.x))];
const [y0, y1] = [Math.floor(Math.min(from.y, to.y)), Math.floor(Math.max(from.y, to.y))];
const [z0, z1] = [Math.floor(Math.min(from.z, to.z)), Math.floor(Math.max(from.z, to.z))];
const size: [number, number, number] = [x1 - x0 + 1, y1 - y0 + 1, z1 - z0 + 1];
const volume = size[0] * size[1] * size[2];
if (volume > MAX_SCHEMATIC_VOLUME) {
throw new Error(`a schematic holds up to ${MAX_SCHEMATIC_VOLUME} blocks, and that box has ${volume}`);
}
const world = game.world;
const palette: SchematicPaletteEntry[] = [];
// palette entries by block value, the id with its state bits
const entries = new Map<number, number>();
const blocks: SchematicBlock[] = [];
for (let y = y0; y <= y1; y++) {
for (let z = z0; z <= z1; z++) {
for (let x = x0; x <= x1; x++) {
const value = world.get_block_value(x, y, z);
let i = entries.get(value);
if (i === undefined) {
i = palette.length;
entries.set(value, i);
palette.push(palette_entry(value));
}
const block: SchematicBlock = { i, p: [x - x0, y - y0, z - z0] };
const tile = world.get_tile(x, y, z);
if (tile) {
const data = game.mods.export_block_data(x, y, z, tile.id);
if (data !== undefined) block.data = data;
if (tile.client_data) block.client_data = structuredClone(tile.client_data);
}
blocks.push(block);
}
}
}
// ones riding something are in what they ride's passengers, like in a save
const entities: SchematicEntity[] = [];
for (const entity of game.entities.all()) {
if (entity.vehicle || !entity.persistent) continue;
if (entity.x < x0 || entity.x >= x1 + 1 || entity.y < y0 || entity.y >= y1 + 1) continue;
if (entity.z < z0 || entity.z >= z1 + 1) continue;
const { id, uuid: _uuid, pos: _pos, ...data } = entity.save();
entities.push({
p: [entity.x - x0, entity.y - y0, entity.z - z0],
id,
data: without_passenger_uuids(data as SavedEntity),
});
}
return { size, palette, blocks, entities };
}
function palette_entry(value: number): SchematicPaletteEntry {
const nid = value & ID_MASK;
const block = nid === AIR ? undefined : EverythingRegistry.get_by_id<BlockRegistry>("blocks", nid);
if (!block) return { id: AIR_ID };
const states = block_states(value, block);
return Object.keys(states).length ? { id: block.id, s: states } : { id: block.id };
}
// each placed copy gets new ones
function without_passenger_uuids(saved: SavedEntity): Record<string, unknown> {
if (!saved.passengers) return saved;
return {
...saved,
passengers: saved.passengers.map(({ uuid: _uuid, ...passenger }) =>
without_passenger_uuids(passenger as SavedEntity)
),
};
}