Cog wheels
This commit is contained in:
@@ -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]);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user