// SPDX-FileCopyrightText: 2026 Paula Wisnievski // SPDX-License-Identifier: AGPL-3.0-or-later // what turns, and what turns what: a kinetic block's axis and the ends of it with an axle, and how fast one block turns // the one next to it, like create's IRotate and RotationPropagator.getRotationSpeedModifier. no ctx, so placement // rules, the server and visuals all read blocks the same way import type { Face } from "cubi/shared"; export type Axis = "x" | "y" | "z"; export type Pos = readonly [number, number, number]; // the create:kinetic component's params, from a block's json export interface KineticParams { // the 2 bit state that says its axis, x, y or z, like a shaft's axis?: string; // or the 3 bit state that says which way it faces, north, east, south, west, up or down, its axis going through // that face, like a crank's facing?: string; // which ends of its axis have an axle that turns the block there: both, or for one with a facing only the end it // faces ("front") or the other ("back"). both when not given shaft?: "both" | "front" | "back"; // its facing is only north, east, south or west, a 2 bit state, like HorizontalKineticBlock's horizontal?: boolean; // a cogwheel: a small one turns the small ones beside it on the same axis, a large one turns small ones on the same // axis at its corners, and large ones on other axes across its edges, like create's ICogWheel cog?: "small" | "large"; // the 1 bit states that say whether there's an axle at the positive end of its axis and at the negative end, like an // encased cog's top_shaft and bottom_shaft. an axle at both when not given shaft_states?: [string, string]; // a gearbox: an axle on each of the 4 sides around its axis instead of along it, see axis_modifier gearbox?: boolean; // a clutch or a gearshift: while the 1 bit state powered names is 1, the half of its axle its speed doesn't come from // is cut off from it, or turns the other way, see axis_modifier split?: "clutch" | "gearshift"; powered?: string; // a chain drive, which turns the chain drives in a line with it across its axis, like ChainDriveBlock. part and // along_first name its states for where it is in the line (CHAIN_PARTS) and which way the line goes (chain_axis). // power names an adjustable chain gearshift's 4 bit state with the power going into it, which makes the rest of // the line turn faster, see chain_ratio chain?: { part: string; along_first: string; power?: string }; } // a chain drive's part state's values, in the order the importer numbers them: the last of a line, one between two // others, one on its own, and the first export const CHAIN_PARTS = ["end", "middle", "none", "start"] as const; export type ChainPart = typeof CHAIN_PARTS[number]; // a 3 bit facing state's values, in the order the importer numbers java's facing export const FACINGS: readonly Face[] = ["north", "east", "south", "west", "top", "bottom"]; // a 2 bit axis state's values export const AXES: readonly Axis[] = ["x", "y", "z"]; export const FACE_OFFSETS: Readonly> = { west: [-1, 0, 0], east: [1, 0, 0], bottom: [0, -1, 0], top: [0, 1, 0], north: [0, 0, -1], south: [0, 0, 1], }; export const OPPOSITE: Readonly> = { west: "east", east: "west", bottom: "top", top: "bottom", north: "south", south: "north", }; const FACE_AXIS: Readonly> = { west: "x", east: "x", bottom: "y", top: "y", north: "z", south: "z", }; // the faces at the positive and negative ends of an axis const AXIS_FACES: Readonly> = { x: ["east", "west"], y: ["top", "bottom"], z: ["south", "north"], }; // how a block turns export interface KineticShape { axis: Axis; // the faces with an axle shafts: readonly Face[]; // the way it faces, for a block with a facing facing?: Face; cog?: "small" | "large"; // a gearbox, clutch or gearshift, whose axles turn depending on where its speed comes from, see axis_modifier halves?: "gearbox" | "clutch" | "gearshift"; powered?: boolean; // a chain drive's line: the axis it goes along, where in it this one is, and how much slower this one turns than // the rest, see chain_ratio chain?: { axis: Axis; part: ChainPart; ratio: number }; } // a kinetic block where it is export interface Kinetic extends KineticShape { readonly x: number; readonly y: number; readonly z: number; // the block next to it it gets its speed from. undefined for a still block, and for a generator turning it readonly source?: Pos; } // a block's shape from its params and its states, read by name export function kinetic_shape(params: KineticParams, state: (name: string) => number | undefined): KineticShape { if (params.facing !== undefined) { const facing = FACINGS[state(params.facing) ?? 0]; const shaft = params.shaft ?? "both"; const shafts = shaft === "front" ? [facing] : shaft === "back" ? [OPPOSITE[facing]] : [facing, OPPOSITE[facing]]; return { axis: FACE_AXIS[facing], shafts, facing }; } const axis = AXES[state(params.axis ?? "axis") ?? 1]; const ends = params.shaft_states; const shafts = params.gearbox ? FACINGS.filter((face) => FACE_AXIS[face] !== axis) : ends ? AXIS_FACES[axis].filter((_, i) => state(ends[i]) === 1) : AXIS_FACES[axis]; const halves = params.gearbox ? "gearbox" : params.split; const chain = params.chain; return { axis, shafts, ...(params.cog && { cog: params.cog }), ...(halves && { halves }), ...(params.powered && { powered: state(params.powered) === 1 }), ...(chain && { chain: { axis: chain_axis(axis, state(chain.along_first) === 1), part: CHAIN_PARTS[state(chain.part) ?? 2], ratio: chain_ratio(chain.power ? state(chain.power) ?? 0 : 0), }, }), }; } // the axis a line of chain drives on axis goes along: the first of the other two, or the second, like // ChainDriveBlock.getConnectionAxis export function chain_axis(axis: Axis, along_first: boolean): Axis { return along_first ? (axis === "x" ? "y" : "x") : (axis === "z" ? "y" : "z"); } // how much faster an adjustable chain gearshift with power going into it turns the rest of its line than it turns // itself, like ChainGearshiftBlockEntity.getModifierForSignal export function chain_ratio(power: number): number { return power === 0 ? 1 : 1 + (power + 1) / 16; } // how much faster the axle at face turns than the block, like RotationPropagator.getAxisModifier. a gearbox's axle // toward where its speed comes from turns with it and the one across from that the other way, and of the 4 others // the ones on the same side of it as that one turn the other way. a powered clutch's axle away from its speed is cut // off, and a powered gearshift's turns the other way. source is the side its speed comes from, undefined when it // isn't turned export function axis_modifier(shape: KineticShape, source: Face | undefined, face: Face): number { if (!shape.halves || !source) return 1; if (shape.halves === "gearbox") { if (FACE_AXIS[face] === FACE_AXIS[source]) return face === source ? 1 : -1; return positive(face) === positive(source) ? -1 : 1; } if (face === source || !shape.powered) return 1; return shape.halves === "clutch" ? 0 : -1; } // the side of a block its speed comes from, undefined when it isn't turned or it makes its own export function source_face(block: Pick): Face | undefined { return block.source && face_toward([block.source[0] - block.x, block.source[1] - block.y, block.source[2] - block.z]); } // whether face is at the positive end of its axis: east, top or south export function positive(face: Face): boolean { return AXIS_FACES[FACE_AXIS[face]][0] === face; } // whether a chain drive turns the one beside it toward face: they're on the same line, and it's on the side of this // one the line goes on, like ChainDriveBlock.areBlocksConnected function chained(from: KineticShape, to: KineticShape, face: Face): boolean { if (!from.chain || !to.chain || from.chain.axis !== to.chain.axis || FACE_AXIS[face] !== from.chain.axis) { return false; } const part = from.chain.part; return part === "middle" || part === (positive(face) ? "start" : "end"); } export function face_axis(face: Face): Axis { return FACE_AXIS[face]; } // a speed turning counterclockwise seen from face, as rpm around face's axis, which is counterclockwise seen from its // positive end. like KineticBlockEntity.convertToDirection export function along(face: Face, speed: number): number { return AXIS_FACES[FACE_AXIS[face]][0] === face ? speed : -speed; } // the face of from that to is beside, undefined when they aren't beside each other function face_toward([dx, dy, dz]: readonly number[]): Face | undefined { return (Object.keys(FACE_OFFSETS) as Face[]).find((face) => { const [ox, oy, oz] = FACE_OFFSETS[face]; return ox === dx && oy === dy && oz === dz; }); } // how much faster to turns than from when from turns it, 0 when from doesn't turn it, like // RotationPropagator.getRotationSpeedModifier: 1 for axles that meet unless one's halves turn differently, the // chain drives' ratio along a line of them, and the other way round for cogs, at the ratio of their sizes export function speed_ratio(from: Kinetic, to: Kinetic): number { const diff = [to.x - from.x, to.y - from.y, to.z - from.z]; const along = (axis: Axis) => diff[AXES.indexOf(axis)]; const face = face_toward(diff); if (face && from.shafts.includes(face) && to.shafts.includes(OPPOSITE[face])) { const into = axis_modifier(to, source_face(to), OPPOSITE[face]); return into === 0 ? 0 : axis_modifier(from, source_face(from), face) / into; } if (face && chained(from, to, face)) return from.chain!.ratio / to.chain!.ratio; // large cogs on different axes meet across an edge, off along both their axes. whether they turn the same way // around their own axes depends on which side of each other they're on if (from.cog === "large" && to.cog === "large") { if (from.axis === to.axis || along(from.axis) === 0 || along(to.axis) === 0) return 0; if (AXES.some((axis) => axis !== from.axis && axis !== to.axis && along(axis) !== 0)) return 0; return along(from.axis) > 0 !== along(to.axis) > 0 ? -1 : 1; } // a large cog meets a small one on the same axis at one of its corners, level with it along the axis const at_corner = from.axis === to.axis && along(from.axis) === 0 && AXES.every((axis) => axis === from.axis || Math.abs(along(axis)) === 1); if (from.cog === "large" && to.cog === "small" && at_corner) return -2; if (from.cog === "small" && to.cog === "large" && at_corner) return -0.5; // small cogs side by side on the same axis if (from.cog === "small" && to.cog === "small" && face && face_axis(face) !== from.axis && from.axis === to.axis) { return -1; } return 0; } // the offsets of the 12 blocks that share an edge with one const EDGES = [-1, 0, 1].flatMap((dx) => [-1, 0, 1].flatMap((dy) => [-1, 0, 1].map((dz) => [dx, dy, dz]))) .filter((offset) => offset.filter((d) => d !== 0).length === 2); // where the blocks it could turn are, like getPotentialNeighbourLocations: the 6 beside it, and for cogs the ones // across their edges, in their own plane for small ones export function neighbour_positions(block: Kinetic): [number, number, number][] { const offsets: readonly (readonly number[])[] = [ ...Object.values(FACE_OFFSETS), ...block.cog === "large" ? EDGES : [], ...block.cog === "small" ? EDGES.filter((offset) => offset[AXES.indexOf(block.axis)] === 0) : [], ]; return offsets.map(([dx, dy, dz]) => [block.x + dx, block.y + dy, block.z + dz]); } // the kinetic blocks next to node that turn it or that it turns, like getConnectedNeighbours. get is undefined where // what's there isn't kinetic export function connected( get: (x: number, y: number, z: number) => T | undefined, node: Kinetic, ): T[] { return neighbour_positions(node).flatMap(([x, y, z]) => { const neighbour = get(x, y, z); const turns = neighbour && (speed_ratio(node, neighbour) !== 0 || speed_ratio(neighbour, node) !== 0); return turns ? [neighbour] : []; }); } // how fast a block acts and is drawn turning, from its client data: its speed, or still while its network is // overstressed, like KineticBlockEntity.getSpeed export function actual_speed(block: { get_client_data(field: string): T | undefined }): number { return block.get_client_data("overstressed") ? 0 : block.get_client_data("speed") ?? 0; }