Water wheel and creative motor

This commit is contained in:
2026-10-28 21:40:39 -03:00
parent 507e9aab24
commit 872db88066
9 changed files with 325 additions and 35 deletions
+22 -9
View File
@@ -13,20 +13,25 @@ import {
kinetic_shape,
type KineticParams,
type KineticShape,
OPPOSITE,
} from "./kinetics/connections.ts";
import { head_offset, type PressShown } from "./processing/press.ts";
// a model reaching north turned to reach each way
const facing_turns = (model: string): Record<Face, ModelRef> => ({
north: model,
east: { model, y: 90 },
south: { model, y: 180 },
west: { model, y: 270 },
top: { model, x: 270 },
bottom: { model, x: 90 },
});
// the crank's handle model has its hub on its south side and its grip reaching north, so it's turned to put north
// where the crank faces
const HANDLE = "create:block_hand_crank_handle";
const HANDLE_TURNS: Record<Face, ModelRef> = {
north: HANDLE,
east: { model: HANDLE, y: 90 },
south: { model: HANDLE, y: 180 },
west: { model: HANDLE, y: 270 },
top: { model: HANDLE, x: 270 },
bottom: { model: HANDLE, x: 90 },
};
const HANDLE_TURNS = facing_turns("create:block_hand_crank_handle");
// a half shaft reaching south, from the middle of the block to its side, turned to reach where a block faces
const HALF_SHAFT_TURNS = facing_turns("create:block_shaft_half");
const half_shaft = (facing: Face) => HALF_SHAFT_TURNS[OPPOSITE[facing]];
// 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 } };
@@ -42,6 +47,14 @@ export function setup(ctx: ClientContext) {
});
ctx.render.register_visual("create:shaft", plain, { replaces_model: true });
ctx.render.register_visual("create:cogwheel", plain, { replaces_model: true });
// 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!)) };
});
// 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
@@ -0,0 +1,50 @@
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
// SPDX-License-Identifier: AGPL-3.0-or-later
// what decides how fast generators turn: water flowing past a water wheel, and the creative motor's speed as it's
// scrolled through. no ctx, so tests can call them directly
import type { Position } from "bworld/shared";
import { AXES, type Axis, face_axis, FACE_OFFSETS, FACINGS, type Pos } from "./connections.ts";
// which way water around a wheel on axis at at turns it: 1 for counterclockwise seen from the axis's positive end, -1
// for the other way, 0 when it's still or the flows cancel out. each of the 4 blocks around it across the axis with
// fluid flowing along a paddle's way round, more than at 60° to it, pushes it one way, like
// WaterWheelBlockEntity.determineAndApplyFlowScore. flow is which way the fluid at x y z flows, 0 0 0 for none
export function flow_score(axis: Axis, [x, y, z]: Pos, flow: (x: number, y: number, z: number) => Position): number {
const along = AXES.indexOf(axis);
let score = 0;
for (const face of FACINGS) {
if (face_axis(face) === axis) continue;
const offset = FACE_OFFSETS[face];
const { x: fx, y: fy, z: fz } = flow(x + offset[0], y + offset[1], z + offset[2]);
// only the part of it across the axis turns the wheel
const across = [fx, fy, fz].map((v, i) => i === along ? 0 : v);
const length = Math.hypot(...across);
if (length === 0) continue;
// the way a paddle there moves turning counterclockwise: the axis crossed with where it is
const unit = [0, 0, 0].map((_, i) => i === along ? 1 : 0);
const moving = [
unit[1] * offset[2] - unit[2] * offset[1],
unit[2] * offset[0] - unit[0] * offset[2],
unit[0] * offset[1] - unit[1] * offset[0],
];
const dot = (across[0] * moving[0] + across[1] * moving[1] + across[2] * moving[2]) / length;
if (Math.abs(dot) > 0.5) score += Math.sign(dot);
}
return Math.sign(score);
}
// the creative motor's speed one notch of the mouse wheel on from current, up when forward: by 1 near 0, and by 4, 16
// and 64 from 4, 32 and 128 rpm on, or always by 1 when fine. it never stops, it skips 0 to turn the other way, and it
// stays within max. like the 0.5 creative motor's step
export function motor_step(current: number, forward: boolean, fine: boolean, max: number): number {
let step = 1;
if (!fine) {
const magnitude = Math.abs(current) - (forward === current > 0 ? 0 : 1);
if (magnitude >= 4) step *= 4;
if (magnitude >= 32) step *= 4;
if (magnitude >= 128) step *= 4;
}
if (current + (forward ? step : -step) === 0) step += 1;
return Math.max(-max, Math.min(max, current + (forward ? step : -step)));
}
+74 -10
View File
@@ -1,10 +1,11 @@
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
// SPDX-License-Identifier: AGPL-3.0-or-later
// create's blocks that do something: so far what turns (create:kinetic), the hand crank that turns it, and the
// mechanical press
// 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 { flow_score, motor_step } from "./kinetics/generators.ts";
import { handle_added, handle_removed, type KineticWorld, update_generated } from "./kinetics/spreading.ts";
import {
PRESSING,
@@ -30,8 +31,9 @@ interface KineticData {
network?: string;
}
// the create:hand_crank component's params: the rpm it turns at
interface CrankParams {
// a generator's params, for create:hand_crank, create:creative_motor and create:water_wheel: the rpm it turns at, or
// the motor's to start with
interface GeneratorParams {
speed: number;
}
@@ -45,6 +47,16 @@ interface CrankData extends KineticData {
backwards?: boolean;
}
interface MotorData extends KineticData {
// rpm, counterclockwise seen from its shaft's end. its params' speed until it's scrolled
speed?: number;
}
interface WheelData extends KineticData {
// which way the water turns it, see flow_score
flow?: number;
}
interface PressData extends KineticData {
// steps through the cycle while it's pressing, see processing/press.ts
pressing?: number;
@@ -73,7 +85,7 @@ export function setup(ctx: ServerContext) {
// turns while it's being clicked, a little longer each click, and the other way while sneaking, like create's
// HandCrankBlock and HandCrankBlockEntity. turning it makes the player hungry
ctx.components.register_block<CrankParams>("create:hand_crank", {
ctx.components.register_block<GeneratorParams>("create:hand_crank", {
on_interact(block, params, player) {
const data: CrankData = block.data ?? {};
const backwards = player.is_sneaking();
@@ -94,6 +106,48 @@ export function setup(ctx: ServerContext) {
},
});
// turns at any speed, set by scrolling the mouse wheel at it, finer while sneaking, like create's
// CreativeMotorBlockEntity with the 0.5 step
ctx.components.register_block<GeneratorParams>("create:creative_motor", {
on_create(block) {
update_generated(world, [block.x, block.y, block.z]);
},
on_scroll(block, params, player, scroll) {
const data: MotorData = block.data ?? {};
let speed = data.speed ?? params.speed;
for (let i = 0; i < Math.abs(scroll.amount); i++) {
speed = motor_step(speed, scroll.amount > 0, player.is_sneaking(), config.max_speed);
}
player.send_message(`Speed: ${speed} rpm`, { overlay: true });
if (speed === (data.speed ?? params.speed)) return;
block.data = { ...data, speed };
update_generated(world, [block.x, block.y, block.z]);
},
});
// turned by fluid flowing past its sides, see flow_score. it looks again when something beside it changes, a tick
// later once that's settled, and every second, since water can change which way it flows without that, like
// WaterWheelBlock and WaterWheelBlockEntity's lazy tick
const update_flow = (block: BlockRef) => {
const { axis } = world.get(block.x, block.y, block.z)!;
const flow = flow_score(axis, [block.x, block.y, block.z], (x, y, z) => ctx.world.fluid_flow(x, y, z));
const data: WheelData = block.data ?? {};
// kept even when it's 0, so the wheel has data and ticks
if (data.flow === flow) return;
block.data = { ...data, flow };
update_generated(world, [block.x, block.y, block.z]);
};
ctx.components.register_block("create:water_wheel", {
on_create: update_flow,
on_neighbor_change(block) {
if (!ctx.world.has_scheduled_tick(block.x, block.y, block.z)) {
ctx.world.schedule_tick(block.x, block.y, block.z, 1);
}
},
on_scheduled_tick: update_flow,
on_second: update_flow,
});
register_press(ctx, pressing);
}
@@ -171,14 +225,19 @@ function register_press(ctx: ServerContext, recipes: ProcessingRecipe[]) {
// client data, and where their speed comes from in tile data
function kinetic_world(ctx: ServerContext, config: Config): KineticWorld {
// components never change once loaded, so each block's are looked up once
const params = new Map<Id, { kinetic?: KineticParams; crank?: CrankParams }>();
const params = new Map<
Id,
{ kinetic?: KineticParams; crank?: GeneratorParams; motor?: GeneratorParams; wheel?: GeneratorParams }
>();
const params_of = (id: Id) => {
let found = params.get(id);
if (!found) {
const components = ctx.blocks.components(id);
found = {
kinetic: components["create:kinetic"] as KineticParams | undefined,
crank: components["create:hand_crank"] as CrankParams | undefined,
crank: components["create:hand_crank"] as GeneratorParams | undefined,
motor: components["create:creative_motor"] as GeneratorParams | undefined,
wheel: components["create:water_wheel"] as GeneratorParams | undefined,
};
params.set(id, found);
}
@@ -194,13 +253,18 @@ function kinetic_world(ctx: ServerContext, config: Config): KineticWorld {
get(x, y, z) {
const block = ctx.world.get_block_ref(x, y, z);
if (!block) return undefined;
const { kinetic, crank } = params_of(block.id);
const { kinetic, crank, motor, wheel } = params_of(block.id);
if (!kinetic) return undefined;
const shape = kinetic_shape(kinetic, (name) => ctx.world.get_state(x, y, z, name));
const data: CrankData = block.data ?? {};
// a crank turns counterclockwise seen from its handle, or the other way while sneaking
const data: CrankData & MotorData & WheelData = block.data ?? {};
// a crank turns counterclockwise seen from its handle, or the other way while sneaking, and a motor seen
// from its shaft
const generated = crank && data.in_use && shape.facing
? along(shape.facing, data.backwards ? -crank.speed : crank.speed)
: motor && shape.facing
? along(shape.facing, data.speed ?? motor.speed)
: wheel
? (data.flow ?? 0) * wheel.speed
: 0;
return {
x,
+33 -10
View File
@@ -62,10 +62,18 @@ export function setup(ctx: SharedContext) {
return other.cog === "small" && cog === "small";
});
// a block with a facing faces away from the axle beside it, or toward the player, or away from them while
// sneaking, like DirectionalKineticBlock. one with its axle only in front faces the axle instead, like
// CreativeMotorBlock
const facing_for = (placement: BlockPlacement, shaft: KineticParams["shaft"]): Face => {
const side = axle_beside(placement.x, placement.y, placement.z);
if (side && !placement.sneaking) return shaft === "front" ? side : OPPOSITE[side];
return placement.sneaking ? placement.looking : OPPOSITE[placement.looking];
};
// 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. cogs are
// placed their own way, see cog_axis
// 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
ctx.components.register_block<KineticParams>("create:kinetic", {
state_for_placement(placement, params) {
if (params.cog) {
@@ -78,15 +86,10 @@ export function setup(ctx: SharedContext) {
const side = axle_beside(placement.x, placement.y, placement.z, FACINGS.slice(0, 4));
return { [params.facing]: FACINGS.indexOf(side ?? OPPOSITE[placement.facing]) };
}
const side = axle_beside(placement.x, placement.y, placement.z);
if (params.facing !== undefined) {
const facing = side && !placement.sneaking
? OPPOSITE[side]
: placement.sneaking
? placement.looking
: OPPOSITE[placement.looking];
return { [params.facing]: FACINGS.indexOf(facing) };
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
? face_axis(side)
: side
@@ -96,6 +99,26 @@ export function setup(ctx: SharedContext) {
},
});
// a wheel's paddles would cut into a wheel beside it, unless that one is on the same axis and beside it along that
// axis, like WaterWheelBlock.canSurvive
const wheel_at = (x: number, y: number, z: number) => {
const id = ctx.world.get_block(x, y, z);
return id !== undefined && ctx.blocks.exists(id) &&
ctx.blocks.components(id)["create:water_wheel"] !== undefined;
};
ctx.components.register_block("create:water_wheel", {
state_for_placement(placement) {
const { x, y, z } = placement;
const axis = face_axis(facing_for(placement, "both"));
const fits = FACINGS.every((side) => {
const [dx, dy, dz] = FACE_OFFSETS[side];
if (!wheel_at(x + dx, y + dy, z + dz)) return true;
return face_axis(side) === axis && beside(x, y, z, side)?.axis === axis;
});
if (!fits) return false;
},
});
// a crank faces away from what it's placed on, unless there's an axle beside it to turn, like HandCrankBlock
ctx.components.register_block("create:hand_crank", {
state_for_placement(placement) {