107 lines
4.6 KiB
TypeScript
107 lines
4.6 KiB
TypeScript
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
|
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
// how create's blocks are placed, on the server and on every client: kinetic blocks line up with the axles they're
|
|
// placed against
|
|
import type { BlockPlacement, Face, SharedContext } from "bworld/shared";
|
|
import {
|
|
AXES,
|
|
type Axis,
|
|
face_axis,
|
|
FACE_OFFSETS,
|
|
FACINGS,
|
|
kinetic_shape,
|
|
type KineticParams,
|
|
type KineticShape,
|
|
OPPOSITE,
|
|
} from "./kinetics/connections.ts";
|
|
|
|
export function setup(ctx: SharedContext) {
|
|
// what turns at x y z, undefined when nothing does
|
|
const kinetic_at = (x: number, y: number, z: number): KineticShape | undefined => {
|
|
const id = ctx.world.get_block(x, y, z);
|
|
const params = id && ctx.blocks.exists(id)
|
|
? ctx.blocks.components(id)["create:kinetic"] as KineticParams | undefined
|
|
: undefined;
|
|
return params && kinetic_shape(params, (name) => ctx.world.get_state(x, y, z, name));
|
|
};
|
|
const beside = (x: number, y: number, z: number, side: Face) => {
|
|
const [dx, dy, dz] = FACE_OFFSETS[side];
|
|
return kinetic_at(x + dx, y + dy, z + dz);
|
|
};
|
|
|
|
// the side of x y z with an axle pointing at it, like getPreferredFacing, of the sides given. undefined when there's
|
|
// none, or when axles point at it along two axes, since it can't line up with both
|
|
const axle_beside = (x: number, y: number, z: number, sides = FACINGS): Face | undefined => {
|
|
let found: Face | undefined;
|
|
for (const side of sides) {
|
|
if (!beside(x, y, z, side)?.shafts.includes(OPPOSITE[side])) continue;
|
|
if (found && face_axis(found) !== face_axis(side)) return undefined;
|
|
found = side;
|
|
}
|
|
return found;
|
|
};
|
|
|
|
// a cog lines up with the small cog it's placed against, or the axle beside it, or else the face it's clicked onto,
|
|
// which is all sneaking looks at, like CogWheelBlock.getAxisForPlacement
|
|
const cog_axis = (placement: BlockPlacement): Axis => {
|
|
const { x, y, z, face } = placement;
|
|
if (placement.sneaking) return face_axis(face);
|
|
const against = beside(x, y, z, OPPOSITE[face]);
|
|
if (against?.cog === "small") return against.axis;
|
|
const side = axle_beside(x, y, z);
|
|
return face_axis(side ?? face);
|
|
};
|
|
// whether a cog on axis at x y z keeps clear of the cogs beside it: a large one's teeth would cut into any cog
|
|
// beside it across its axis, and a small one's into a large one, unless that one's axis points at it. like
|
|
// isValidCogwheelPosition
|
|
const cog_fits = (x: number, y: number, z: number, axis: Axis, cog: "small" | "large") =>
|
|
FACINGS.every((side) => {
|
|
const other = beside(x, y, z, side);
|
|
if (face_axis(side) === axis || !other?.cog || other.axis === face_axis(side)) return true;
|
|
return other.cog === "small" && cog === "small";
|
|
});
|
|
|
|
// a block with an axis lines up with the axle it's placed against, or sneaking, with the face it's clicked onto.
|
|
// otherwise it points the way the player looks, like RotatedPillarKineticBlock. a block with a facing faces away
|
|
// from the axle, or toward the player, or away from them while sneaking, like DirectionalKineticBlock. cogs are
|
|
// placed their own way, see cog_axis
|
|
ctx.components.register_block<KineticParams>("create:kinetic", {
|
|
state_for_placement(placement, params) {
|
|
if (params.cog) {
|
|
const axis = cog_axis(placement);
|
|
if (!cog_fits(placement.x, placement.y, placement.z, axis, params.cog)) return false;
|
|
return { [params.axis ?? "axis"]: AXES.indexOf(axis) };
|
|
}
|
|
// along an axle beside it, or else facing the player, like HorizontalKineticBlock and MechanicalPressBlock
|
|
if (params.facing !== undefined && params.horizontal) {
|
|
const side = axle_beside(placement.x, placement.y, placement.z, FACINGS.slice(0, 4));
|
|
return { [params.facing]: FACINGS.indexOf(side ?? OPPOSITE[placement.facing]) };
|
|
}
|
|
const side = axle_beside(placement.x, placement.y, placement.z);
|
|
if (params.facing !== undefined) {
|
|
const facing = side && !placement.sneaking
|
|
? OPPOSITE[side]
|
|
: placement.sneaking
|
|
? placement.looking
|
|
: OPPOSITE[placement.looking];
|
|
return { [params.facing]: FACINGS.indexOf(facing) };
|
|
}
|
|
const axis = side && !placement.sneaking
|
|
? face_axis(side)
|
|
: side
|
|
? face_axis(placement.face)
|
|
: face_axis(placement.looking);
|
|
return { [params.axis ?? "axis"]: AXES.indexOf(axis) };
|
|
},
|
|
});
|
|
|
|
// a crank faces away from what it's placed on, unless there's an axle beside it to turn, like HandCrankBlock
|
|
ctx.components.register_block("create:hand_crank", {
|
|
state_for_placement(placement) {
|
|
const side = axle_beside(placement.x, placement.y, placement.z);
|
|
return { facing: FACINGS.indexOf(side && !placement.sneaking ? OPPOSITE[side] : placement.face) };
|
|
},
|
|
});
|
|
}
|