130 lines
4.4 KiB
TypeScript
130 lines
4.4 KiB
TypeScript
// 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) and the hand crank that turns it
|
|
import type { BlockRef, Id, ServerContext } from "bworld/server";
|
|
import { along, kinetic_shape, type KineticParams } from "./kinetics/connections.ts";
|
|
import { handle_added, handle_removed, type KineticWorld, type Pos, update_generated } from "./kinetics/spreading.ts";
|
|
|
|
interface Config {
|
|
max_speed: number;
|
|
crank_hunger: number;
|
|
}
|
|
|
|
// a kinetic block's tile data, see KineticNode. its speed is its "speed" client data, which its visual turns it by
|
|
interface KineticData {
|
|
source?: Pos;
|
|
network?: string;
|
|
}
|
|
|
|
// the create:hand_crank component's params: the rpm it turns at
|
|
interface CrankParams {
|
|
speed: number;
|
|
}
|
|
|
|
// how long a crank keeps turning after each click, in ticks. holding use clicks every 4
|
|
const CRANK_USE_TICKS = 10;
|
|
|
|
interface CrankData extends KineticData {
|
|
// ticks left turning
|
|
in_use?: number;
|
|
// turned the other way, by sneaking
|
|
backwards?: boolean;
|
|
}
|
|
|
|
export function setup(ctx: ServerContext) {
|
|
const config: Config = ctx.config;
|
|
const world = kinetic_world(ctx, config);
|
|
|
|
ctx.components.register_block<KineticParams>("create:kinetic", {
|
|
on_create(block) {
|
|
handle_added(world, [block.x, block.y, block.z]);
|
|
},
|
|
on_break(block) {
|
|
handle_removed(world, [block.x, block.y, block.z]);
|
|
},
|
|
});
|
|
|
|
// 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", {
|
|
on_interact(block, params, player) {
|
|
const data: CrankData = block.data ?? {};
|
|
const backwards = player.is_sneaking();
|
|
const changed = !data.in_use || backwards !== (data.backwards ?? false);
|
|
block.data = { ...data, in_use: CRANK_USE_TICKS, backwards };
|
|
block.set_ticking(true);
|
|
if (changed) update_generated(world, [block.x, block.y, block.z]);
|
|
player.add_exhaustion(params.speed * config.crank_hunger);
|
|
return true;
|
|
},
|
|
on_tick(block) {
|
|
const data: CrankData = block.data ?? {};
|
|
const in_use = Math.max(0, (data.in_use ?? 0) - 1);
|
|
block.data = { ...data, in_use };
|
|
if (in_use > 0) return;
|
|
block.set_ticking(false);
|
|
update_generated(world, [block.x, block.y, block.z]);
|
|
},
|
|
});
|
|
}
|
|
|
|
// the world as rotation spreading sees it: blocks with create:kinetic, their speed in client data and where it 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_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,
|
|
};
|
|
params.set(id, found);
|
|
}
|
|
return found;
|
|
};
|
|
const block_at = (at: Pos): BlockRef => {
|
|
const block = ctx.world.get_block_ref(...at);
|
|
if (!block) throw new Error(`there's no kinetic block at ${at.join(" ")}`);
|
|
return block;
|
|
};
|
|
|
|
return {
|
|
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);
|
|
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 generated = crank && data.in_use && shape.facing
|
|
? along(shape.facing, data.backwards ? -crank.speed : crank.speed)
|
|
: 0;
|
|
return {
|
|
x,
|
|
y,
|
|
z,
|
|
...shape,
|
|
speed: block.get_client_data<number>("speed") ?? 0,
|
|
source: data.source,
|
|
network: data.network,
|
|
generated,
|
|
};
|
|
},
|
|
set(at, speed, source, network) {
|
|
const block = block_at(at);
|
|
block.set_client_data("speed", speed === 0 ? undefined : speed);
|
|
const { source: _, network: __, ...rest }: KineticData = block.data ?? {};
|
|
block.data = { ...rest, ...(source && { source }), ...(network !== undefined && { network }) };
|
|
},
|
|
break(at) {
|
|
ctx.world.break_block(...at);
|
|
},
|
|
max_speed: config.max_speed,
|
|
};
|
|
}
|