Water wheel and creative motor
This commit is contained in:
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user