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
+77 -12
View File
@@ -9,6 +9,8 @@ import type { Face } from "bworld/shared";
import {
actual_speed,
type Axis,
axis_modifier,
face_axis,
FACINGS,
kinetic_shape,
type KineticParams,
@@ -43,17 +45,73 @@ export function setup(ctx: ClientContext) {
};
const plain = (block: VisualBlock, instances: Instances): Visual => ({
update: turning(block, instances, shape_of(block)),
update: turning(block, instances, shape_of(block).axis),
});
ctx.render.register_visual("create:shaft", plain, { replaces_model: true });
ctx.render.register_visual("create:cogwheel", plain, { replaces_model: true });
// the casing is the block's model, and the shaft through it turns, like the encased shafts' and chain drives'
// ShaftRenderer
const encased_shaft = (block: VisualBlock, instances: Instances): Visual => {
const { axis } = shape_of(block);
return { update: turning(block, instances, axis, { model: "create:block_shaft", ...AXIS_TURNS[axis] }) };
};
for (
const id of [
"andesite_encased_shaft",
"brass_encased_shaft",
"encased_chain_drive",
"adjustable_chain_gearshift",
]
) {
ctx.render.register_visual(`create:${id}`, encased_shaft);
}
// a half shaft out of each side with an axle, turning as axis_modifier says from the side its speed comes from,
// which the server puts in its "source" client data, like GearboxVisual and SplitShaftVisual
const halves = (block: VisualBlock, instances: Instances): Visual => {
const shape = shape_of(block);
const turns = shape.shafts.map((face) =>
turning(
block,
instances,
face_axis(face),
half_shaft(face),
0,
() => axis_modifier(shape, block.get_client_data<Face>("source"), face),
)
);
return { update: () => turns.forEach((turn) => turn()) };
};
for (const id of ["gearbox", "clutch", "gearshift"]) ctx.render.register_visual(`create:${id}`, halves);
// the cog turns in its casing, offset like a large cog's when it's one, and a half shaft out of each end with an
// axle, like EncasedCogVisual
const encased_cog = (model: string, offset: number) => (block: VisualBlock, instances: Instances): Visual => {
const { axis, shafts } = shape_of(block);
const turns = [
turning(block, instances, axis, { model, ...AXIS_TURNS[axis] }, offset),
...shafts.map((face) => turning(block, instances, axis, half_shaft(face))),
];
return { update: () => turns.forEach((turn) => turn()) };
};
for (const casing of ["andesite", "brass"]) {
ctx.render.register_visual(
`create:${casing}_encased_cogwheel`,
encased_cog("create:block_cogwheel_shaftless", 0),
);
ctx.render.register_visual(
`create:${casing}_encased_large_cogwheel`,
encased_cog("create:block_large_cogwheel_shaftless", 11.25),
);
}
// the whole wheel turns, paddles and all, like WaterWheelVisual
ctx.render.register_visual("create:water_wheel", plain, { replaces_model: true });
// its casing is its block's model, and the half shaft out of its front turns, like CreativeMotorRenderer
ctx.render.register_visual("create:creative_motor", (block, instances) => {
const shape = shape_of(block);
return { update: turning(block, instances, shape, half_shaft(shape.facing!)) };
return { update: turning(block, instances, shape.axis, half_shaft(shape.facing!)) };
});
// 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
@@ -65,11 +123,11 @@ export function setup(ctx: ClientContext) {
const cog = turning(
block,
instances,
shape,
shape.axis,
{ model: "create:block_large_cogwheel_shaftless", ...turns },
11.25,
);
const shaft = turning(block, instances, shape, { model: "create:block_cogwheel_shaft", ...turns });
const shaft = turning(block, instances, shape.axis, { model: "create:block_cogwheel_shaft", ...turns });
return {
update() {
cog();
@@ -82,7 +140,7 @@ export function setup(ctx: ClientContext) {
// up through the cycle from where the server last said it was, like PressVisual
ctx.render.register_visual("create:mechanical_press", (block, instances) => {
const shape = shape_of(block);
const turn = turning(block, instances, shape, { model: "create:block_shaft", ...AXIS_TURNS[shape.axis] });
const turn = turning(block, instances, shape.axis, { model: "create:block_shaft", ...AXIS_TURNS[shape.axis] });
const head = instances.transformed({
model: "create:block_mechanical_press_head",
y: FACINGS.indexOf(shape.facing!) * 90,
@@ -113,7 +171,7 @@ export function setup(ctx: ClientContext) {
// with it, like HandCrankVisual's independent angle
ctx.render.register_visual("create:hand_crank", (block, instances) => {
const shape = shape_of(block);
const turn = turning(block, instances, shape);
const turn = turning(block, instances, shape.axis);
const handle = instances.rotating(HANDLE_TURNS[shape.facing!]);
// in degrees, and degrees a tick
let angle = 0;
@@ -133,13 +191,20 @@ export function setup(ctx: ClientContext) {
}, { replaces_model: true });
}
// 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) {
// a model turning around axis at the block's speed times modifier, the block's own model 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,
axis: Axis,
model?: ModelRef,
offset = 0,
modifier = () => 1,
) {
const instance = instances.rotating(model);
const angle = should_offset(shape.axis, block) ? 22.5 : offset;
const turn = () => instance.set_rotation(shape.axis, actual_speed(block), angle);
const angle = should_offset(axis, block) ? 22.5 : offset;
const turn = () => instance.set_rotation(axis, actual_speed(block) * modifier(), angle);
turn();
return turn;
}
+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
+51 -1
View File
@@ -4,7 +4,19 @@
// create's blocks that do something: so far what turns (create:kinetic), the generators that turn it (the hand crank,
// the creative motor and the water wheel), and the mechanical press
import type { BlockRef, EntityRef, Id, ServerContext } from "bworld/server";
import { actual_speed, along, kinetic_shape, type KineticParams, type Pos } from "./kinetics/connections.ts";
import {
actual_speed,
along,
face_axis,
FACE_OFFSETS,
FACINGS,
kinetic_shape,
type KineticParams,
OPPOSITE,
type Pos,
positive,
source_face,
} from "./kinetics/connections.ts";
import { flow_score, motor_step } from "./kinetics/generators.ts";
import { handle_added, handle_removed, type KineticWorld, update_generated } from "./kinetics/spreading.ts";
import {
@@ -81,6 +93,39 @@ export function setup(ctx: ServerContext) {
on_break(block) {
handle_removed(world, [block.x, block.y, block.z]);
},
// states that change how it turns things, like a clutch being powered or a chain drive joining a line, take it
// out and put it back, like Create's detachKinetics and attachKinetics on a change that isn't
// areStatesKineticallyEquivalent
on_state_change(block, params, before) {
const now = kinetic_shape(params, (name) => ctx.world.get_state(block.x, block.y, block.z, name));
if (JSON.stringify(now) === JSON.stringify(kinetic_shape(params, (name) => before[name]))) return;
handle_removed(world, [block.x, block.y, block.z]);
handle_added(world, [block.x, block.y, block.z]);
},
});
// a shaft or cog right clicked with a casing becomes the encased one, by the casing's id, turned the same way. an
// encased cog gets an axle out of its casing toward each axle beside it. the casing isn't used up. like
// EncasableBlock.tryEncase and handleEncasing
ctx.components.register_block<Record<Id, Id>>("create:encasable", {
on_interact(block, params, player) {
const into = params[player.held_item?.id ?? ""];
const node = world.get(block.x, block.y, block.z);
if (!into || !node) return false;
const own = ctx.blocks.components(block.id)["create:kinetic"] as KineticParams;
const encased = ctx.blocks.components(into)["create:kinetic"] as KineticParams;
const states = {
[encased.axis ?? "axis"]: ctx.world.get_state(block.x, block.y, block.z, own.axis ?? "axis")!,
};
for (const side of FACINGS) {
if (!encased.shaft_states || face_axis(side) !== node.axis) continue;
const [dx, dy, dz] = FACE_OFFSETS[side];
const axle = world.get(block.x + dx, block.y + dy, block.z + dz)?.shafts.includes(OPPOSITE[side]);
states[encased.shaft_states[positive(side) ? 0 : 1]] = axle ? 1 : 0;
}
ctx.world.set_block(block.x, block.y, block.z, into, states);
return true;
},
});
// turns while it's being clicked, a little longer each click, and the other way while sneaking, like create's
@@ -285,6 +330,11 @@ function kinetic_world(ctx: ServerContext, config: Config): KineticWorld {
// machines stop ticking while they're still, like a press, and start again once they turn
if (speed !== 0 && !block.get_client_data("speed")) block.set_ticking(true);
block.set_client_data("speed", speed === 0 ? undefined : speed);
// the side its speed comes from decides how a gearbox's or clutch's axles turn, which its visual shows
const { gearbox, split } = params_of(block.id).kinetic!;
if (gearbox || split) {
block.set_client_data("source", source_face({ x: at[0], y: at[1], z: at[2], source }));
}
const { source: _, network: __, ...rest }: KineticData = block.data ?? {};
block.data = { ...rest, ...(source && { source }), ...(network !== undefined && { network }) };
},
+65 -10
View File
@@ -2,11 +2,14 @@
// 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
import type { BlockPlacement, Face, SharedContext } from "bworld/shared";
// placed against, and chain drives join into lines
import type { BlockPlacement, Face, Id, SharedContext } from "bworld/shared";
import { chain_update, type ChainState } from "./kinetics/chains.ts";
import {
AXES,
type Axis,
chain_axis,
CHAIN_PARTS,
face_axis,
FACE_OFFSETS,
FACINGS,
@@ -72,8 +75,27 @@ export function setup(ctx: SharedContext) {
};
// 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 as
// facing_for says. cogs are placed their own way, see cog_axis
// 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) {
@@ -89,13 +111,46 @@ export function setup(ctx: SharedContext) {
if (params.facing !== undefined) {
return { [params.facing]: FACINGS.indexOf(facing_for(placement, params.shaft)) };
}
const side = axle_beside(placement.x, placement.y, placement.z);
const axis = side && !placement.sneaking
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)
: side
? face_axis(placement.face)
: face_axis(placement.looking);
return { [params.axis ?? "axis"]: AXES.indexOf(axis) };
: placement.facing === "north" || placement.facing === "south"
? "z"
: "x";
return { axis: AXES.indexOf(across === "x" ? "z" : "x") };
},
});