190 lines
8.6 KiB
TypeScript
190 lines
8.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, and chain drives join into lines
|
|
import type { BlockPlacement, Face, Id, SharedContext } from "cubi/shared";
|
|
import { chain_update, type ChainState } from "./kinetics/chains.ts";
|
|
import { COMPACTING, PRESSING, processing_type } from "./recipes/processing.ts";
|
|
import {
|
|
AXES,
|
|
type Axis,
|
|
chain_axis,
|
|
CHAIN_PARTS,
|
|
face_axis,
|
|
FACE_OFFSETS,
|
|
FACINGS,
|
|
kinetic_shape,
|
|
type KineticParams,
|
|
type KineticShape,
|
|
OPPOSITE,
|
|
} from "./kinetics/connections.ts";
|
|
|
|
export function setup(ctx: SharedContext) {
|
|
const lookup = { expand: ctx.tags.expand, item_exists: ctx.items.exists, fluid_exists: ctx.fluids.exists };
|
|
ctx.recipes.register_type(PRESSING.id, processing_type(PRESSING, lookup));
|
|
ctx.recipes.register_type(COMPACTING.id, processing_type(COMPACTING, lookup));
|
|
|
|
// 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 a facing faces away from the axle beside it, or toward the player, or away from them while
|
|
// sneaking, like DirectionalKineticBlock. one with its axle only in front faces the axle instead, like
|
|
// CreativeMotorBlock
|
|
const facing_for = (placement: BlockPlacement, shaft: KineticParams["shaft"]): Face => {
|
|
const side = axle_beside(placement.x, placement.y, placement.z);
|
|
if (side && !placement.sneaking) return shaft === "front" ? side : OPPOSITE[side];
|
|
return placement.sneaking ? placement.looking : OPPOSITE[placement.looking];
|
|
};
|
|
|
|
// 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
|
|
const axis_for = (placement: BlockPlacement): Axis => {
|
|
const side = axle_beside(placement.x, placement.y, placement.z);
|
|
if (side && !placement.sneaking) return face_axis(side);
|
|
return face_axis(side ? placement.face : placement.looking);
|
|
};
|
|
|
|
// a chain drive's line, undefined for a block that isn't one
|
|
const chain_of = (shape: KineticShape | undefined): ChainState | undefined =>
|
|
shape?.chain && {
|
|
axis: shape.axis,
|
|
part: shape.chain.part,
|
|
along_first: shape.chain.axis === chain_axis(shape.axis, true),
|
|
};
|
|
const chain_states = (params: KineticParams, chain: ChainState) => ({
|
|
[params.chain!.part]: CHAIN_PARTS.indexOf(chain.part),
|
|
[params.chain!.along_first]: chain.along_first ? 1 : 0,
|
|
});
|
|
|
|
// a block with an axis is placed as axis_for says, and a chain drive joins the lines beside it. a block with a
|
|
// facing faces as facing_for says. 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]) };
|
|
}
|
|
if (params.facing !== undefined) {
|
|
return { [params.facing]: FACINGS.indexOf(facing_for(placement, params.shaft)) };
|
|
}
|
|
const axis = axis_for(placement);
|
|
const states = { [params.axis ?? "axis"]: AXES.indexOf(axis) };
|
|
if (!params.chain) return states;
|
|
const { x, y, z } = placement;
|
|
const alone: ChainState = { axis, part: "none", along_first: false };
|
|
const chain = FACINGS.reduce(
|
|
(chain, side) => chain_update(chain, side, chain_of(beside(x, y, z, side))),
|
|
alone,
|
|
);
|
|
return { ...states, ...chain_states(params, chain) };
|
|
},
|
|
// a chain drive joins one placed beside it, and ends its line where one goes, like ChainDriveBlock.updateShape
|
|
on_neighbor_change(block, params, neighbor) {
|
|
if (!params.chain) return;
|
|
const own = chain_of(kinetic_at(block.x, block.y, block.z));
|
|
const side = FACINGS.find((side) => {
|
|
const [dx, dy, dz] = FACE_OFFSETS[side];
|
|
return block.x + dx === neighbor.x && block.y + dy === neighbor.y && block.z + dz === neighbor.z;
|
|
});
|
|
if (!own || !side) return;
|
|
const chain = chain_update(own, side, chain_of(kinetic_at(neighbor.x, neighbor.y, neighbor.z)));
|
|
if (chain === own) return;
|
|
for (const [name, value] of Object.entries(chain_states(params, chain))) {
|
|
ctx.world.set_state(block.x, block.y, block.z, name, value);
|
|
}
|
|
},
|
|
});
|
|
|
|
// a gearbox lies flat with its axles level, and side_item, the vertical gearbox, stands it on its side: across the
|
|
// axle level with it, or else across the way the player looks, like GearboxBlock and VerticalGearboxItem
|
|
ctx.components.register_block<{ side_item: Id }>("create:gearbox", {
|
|
state_for_placement(placement, params) {
|
|
if (placement.item !== params.side_item) return { axis: AXES.indexOf("y") };
|
|
const side = axle_beside(placement.x, placement.y, placement.z, FACINGS.slice(0, 4));
|
|
const across = side
|
|
? face_axis(side)
|
|
: placement.facing === "north" || placement.facing === "south"
|
|
? "z"
|
|
: "x";
|
|
return { axis: AXES.indexOf(across === "x" ? "z" : "x") };
|
|
},
|
|
});
|
|
|
|
// a wheel's paddles would cut into a wheel beside it, unless that one is on the same axis and beside it along that
|
|
// axis, like WaterWheelBlock.canSurvive
|
|
const wheel_at = (x: number, y: number, z: number) => {
|
|
const id = ctx.world.get_block(x, y, z);
|
|
return id !== undefined && ctx.blocks.exists(id) &&
|
|
ctx.blocks.components(id)["create:water_wheel"] !== undefined;
|
|
};
|
|
ctx.components.register_block("create:water_wheel", {
|
|
state_for_placement(placement) {
|
|
const { x, y, z } = placement;
|
|
const axis = face_axis(facing_for(placement, "both"));
|
|
const fits = FACINGS.every((side) => {
|
|
const [dx, dy, dz] = FACE_OFFSETS[side];
|
|
if (!wheel_at(x + dx, y + dy, z + dz)) return true;
|
|
return face_axis(side) === axis && beside(x, y, z, side)?.axis === axis;
|
|
});
|
|
if (!fits) return false;
|
|
},
|
|
});
|
|
|
|
// 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) };
|
|
},
|
|
});
|
|
}
|