Some more kinetic blocks

This commit is contained in:
2026-10-28 22:10:48 -03:00
parent fa0748a9c5
commit b8cccb86ff
35 changed files with 670 additions and 73 deletions
+44
View File
@@ -0,0 +1,44 @@
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
// SPDX-License-Identifier: AGPL-3.0-or-later
// how chain drives join into lines: each keeps where it is in its line (start, middle, end, or none on its own) and
// which way the line goes, and changes them as chain drives beside it come and go, like ChainDriveBlock.updateShape.
// no ctx, so placing and neighbor rules both use it
import type { Face } from "bworld/shared";
import { type Axis, chain_axis, type ChainPart, face_axis, positive } from "./connections.ts";
// a chain drive's states that matter for its line
export interface ChainState {
axis: Axis;
part: ChainPart;
// its line goes along the first of the axes that aren't its own, see chain_axis
along_first: boolean;
}
// a chain drive's line once the block toward face is other, undefined when that isn't a chain drive. one on its own
// starts a line with a chain drive beside it across its axis, and one in a line only joins more along that line, and
// is the end of it again when the one past it goes
export function chain_update(state: ChainState, face: Face, other: ChainState | undefined): ChainState {
const { axis } = state;
const toward = face_axis(face);
if (axis === toward) return state;
const facing_along_first = axis === "x" ? toward === "y" : toward === "x";
const ahead = positive(face);
if (!other) {
if (facing_along_first !== state.along_first || state.part === "none") return state;
if (state.part === "middle") return { ...state, part: ahead ? "end" : "start" };
if ((state.part === "start") === ahead) return { ...state, part: "none" };
return state;
}
if (other.axis === toward) return state;
if (other.part !== "none" && chain_axis(other.axis, other.along_first) !== toward) return state;
let { part, along_first } = state;
if (part === "none") {
part = ahead ? "start" : "end";
along_first = facing_along_first;
} else if (chain_axis(axis, along_first) !== toward) {
return state;
}
if ((part === "start") !== ahead) part = "middle";
return { axis, part, along_first };
}
+104 -4
View File
@@ -24,8 +24,27 @@ export interface KineticParams {
// 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
@@ -73,6 +92,12 @@ export interface KineticShape {
// 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
@@ -80,6 +105,8 @@ 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
@@ -95,7 +122,76 @@ export function kinetic_shape(params: KineticParams, state: (name: string) => nu
return { axis: FACE_AXIS[facing], shafts, facing };
}
const axis = AXES[state(params.axis ?? "axis") ?? 1];
return { axis, shafts: AXIS_FACES[axis], ...(params.cog && { cog: params.cog }) };
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<Kinetic, "x" | "y" | "z" | "source">): 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 {
@@ -117,13 +213,17 @@ function face_toward([dx, dy, dz]: readonly number[]): Face | undefined {
}
// 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, and the other way round for cogs, at the ratio of
// their sizes
// 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])) return 1;
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
+1 -4
View File
@@ -10,10 +10,7 @@ import { connected as connected_in, type Kinetic, neighbour_positions, type Pos,
import { type StressNode, type StressWorld, update_stress } from "./stress.ts";
// a kinetic block as the world keeps it
export interface KineticNode extends StressNode {
// the block next to it it gets its speed from. undefined for a still block, and for a generator turning it
readonly source?: Pos;
}
export type KineticNode = StressNode;
export interface KineticWorld extends StressWorld {
// undefined when what's there isn't kinetic