Cog wheels

This commit is contained in:
2026-10-15 12:01:46 -03:00
parent a0596ed0ef
commit 74c315bdfc
14 changed files with 331 additions and 88 deletions
+43 -14
View File
@@ -19,32 +19,60 @@ const HANDLE_TURNS: Record<Face, ModelRef> = {
bottom: { model: HANDLE, x: 90 },
};
// how a model made along y is turned to an axis, the way the kinetic blocks' variants turn theirs
const AXIS_TURNS: Record<Axis, { x?: number; y?: number }> = { x: { x: 90, y: 90 }, y: {}, z: { x: 90, y: 180 } };
export function setup(ctx: ClientContext) {
const shape_of = (block: VisualBlock): KineticShape => {
const params = ctx.blocks.components(block.id)["create:kinetic"] as KineticParams;
return kinetic_shape(params, (name) => block.states[name]);
};
const shaft = (block: VisualBlock, instances: Instances) => turning(block, instances, shape_of(block)).visual;
ctx.render.register_visual("create:shaft", shaft, { replaces_model: true });
const plain = (block: VisualBlock, instances: Instances): Visual => ({
update: turning(block, instances, shape_of(block)),
});
ctx.render.register_visual("create:shaft", plain, { replaces_model: true });
ctx.render.register_visual("create:cogwheel", plain, { replaces_model: true });
// a large cog's teeth are half as far apart as a small one's, so on the blocks where small cogs aren't offset it's
// offset by half of one of its own teeth. its shaft is apart from it and offset like any other, so it lines up with
// the shafts next to it. like BracketedKineticBlockEntityVisual.LargeCogVisual
ctx.render.register_visual("create:large_cogwheel", (block, instances) => {
const shape = shape_of(block);
const turns = AXIS_TURNS[shape.axis];
const cog = turning(
block,
instances,
shape,
{ model: "create:block_large_cogwheel_shaftless", ...turns },
11.25,
);
const shaft = turning(block, instances, shape, { model: "create:block_cogwheel_shaft", ...turns });
return {
update() {
cog();
shaft();
},
};
}, { replaces_model: true });
// the half shaft turns with the speed like any kinetic block, and the handle catches up with it and slows down
// with it, like HandCrankVisual's independent angle
ctx.render.register_visual("create:hand_crank", (block, instances) => {
const shape = shape_of(block);
const { visual, speed } = turning(block, instances, shape);
const turn = turning(block, instances, shape);
const handle = instances.rotating(HANDLE_TURNS[shape.facing!]);
// in degrees, and degrees a tick
let angle = 0;
let velocity = 0;
let last: number | undefined;
return {
...visual,
update: turn,
on_frame(time) {
const ticks = last === undefined ? 0 : time - last;
last = time;
// a quarter of the way to the shaft's speed each tick
velocity += (speed() * 0.3 - velocity) * Math.min(1, ticks / 4);
velocity += (speed_of(block) * 0.3 - velocity) * Math.min(1, ticks / 4);
angle = (angle + velocity * ticks) % 360;
handle.set_rotation(shape.axis, 0, angle);
},
@@ -52,16 +80,17 @@ export function setup(ctx: ClientContext) {
}, { replaces_model: true });
}
// the block's own model turning around its axis at its speed. offset so cogs mesh with the ones beside them, like
// KineticBlockEntityVisual.rotationOffset: turned by half a tooth on every other block
function turning(block: VisualBlock, instances: Instances, shape: KineticShape) {
const speed = () => block.get_client_data<number>("speed") ?? 0;
const instance = instances.rotating();
const offset = should_offset(shape.axis, block) ? 22.5 : 0;
const turn = () => instance.set_rotation(shape.axis, speed(), offset);
const speed_of = (block: VisualBlock) => block.get_client_data<number>("speed") ?? 0;
// a model turning around the block's axis at its speed, the block's own when not given. returns what turns it again
// when the speed changes. on every other block it's offset by half a small cog's tooth, so cogs mesh with the ones
// beside them, and on the rest by offset, like KineticBlockEntityVisual.rotationOffset
function turning(block: VisualBlock, instances: Instances, shape: KineticShape, model?: ModelRef, offset = 0) {
const instance = instances.rotating(model);
const angle = should_offset(shape.axis, block) ? 22.5 : offset;
const turn = () => instance.set_rotation(shape.axis, speed_of(block), angle);
turn();
const visual: Visual = { update: turn };
return { visual, speed };
return turn;
}
// whether the coordinates across the axis add up to an even number, like KineticBlockEntityVisual.shouldOffset
+43 -9
View File
@@ -18,11 +18,15 @@ export interface KineticParams {
// 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";
// 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";
}
// 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"];
const AXES: readonly Axis[] = ["x", "y", "z"];
// a 2 bit axis state's values
export const AXES: readonly Axis[] = ["x", "y", "z"];
export const FACE_OFFSETS: Readonly<Record<Face, readonly [number, number, number]>> = {
west: [-1, 0, 0],
@@ -65,6 +69,7 @@ export interface KineticShape {
shafts: readonly Face[];
// the way it faces, for a block with a facing
facing?: Face;
cog?: "small" | "large";
}
// a kinetic block where it is
@@ -87,7 +92,7 @@ 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] };
return { axis, shafts: AXIS_FACES[axis], ...(params.cog && { cog: params.cog }) };
}
export function face_axis(face: Face): Axis {
@@ -101,23 +106,52 @@ export function along(face: Face, speed: number): number {
}
// the face of from that to is beside, undefined when they aren't beside each other
function face_toward(from: Kinetic, to: Kinetic): Face | undefined {
const [dx, dy, dz] = [to.x - from.x, to.y - from.y, to.z - from.z];
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: 1 for axles that meet. like
// RotationPropagator.getRotationSpeedModifier
// 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
export function speed_ratio(from: Kinetic, to: Kinetic): number {
const face = face_toward(from, to);
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;
// 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;
}
// where the blocks it could turn are, like getPotentialNeighbourLocations
// 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][] {
return Object.values(FACE_OFFSETS).map(([dx, dy, dz]) => [block.x + dx, block.y + dy, block.z + dz]);
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]);
}
+45 -13
View File
@@ -3,44 +3,76 @@
// 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 { Face, SharedContext } from "bworld/shared";
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";
const AXES = ["x", "y", "z"];
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. 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): Face | undefined => {
let found: Face | undefined;
for (const side of FACINGS) {
const [dx, dy, dz] = FACE_OFFSETS[side];
const [nx, ny, nz] = [x + dx, y + dy, z + dz];
const id = ctx.world.get_block(nx, ny, nz);
const params = id && ctx.blocks.exists(id)
? ctx.blocks.components(id)["create:kinetic"] as KineticParams | undefined
: undefined;
if (!params) continue;
const shape = kinetic_shape(params, (name) => ctx.world.get_state(nx, ny, nz, name));
if (!shape.shafts.includes(OPPOSITE[side])) continue;
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
// 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) };
}
const side = axle_beside(placement.x, placement.y, placement.z);
if (params.facing !== undefined) {
const facing = side && !placement.sneaking