// 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 import type { BlockRef, EntityRef, 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"; import { PRESSING, processing_for, processing_recipes, type ProcessingRecipe, roll_results, } from "./recipes/processing.ts"; import { CYCLE, cycle_rate, PRESS_AT, type PressShown, SCAN_TICKS } from "./processing/press.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; } interface PressData extends KineticData { // steps through the cycle while it's pressing, see processing/press.ts pressing?: number; // ticks until it looks for something to press again scan?: number; } export function setup(ctx: ServerContext) { const config: Config = ctx.config; const world = kinetic_world(ctx, config); // checked now, so a broken recipe stops the server rather than a press later const pressing = processing_recipes(ctx.recipes.list(PRESSING.id), PRESSING, { expand: (choice) => ctx.tags.expand(choice), item_exists: (id) => ctx.items.exists(id), }); ctx.components.register_block("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("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]); }, }); register_press(ctx, pressing); } // presses items lying on the ground under it while it turns, like create's MechanicalPressBlockEntity pressing in the // world: it looks for one it has a recipe for every SCAN_TICKS, then comes down on it, faster the faster it turns. a // lone item turns into what it's pressed into, and from a stack one at a time is, landing beside the rest. it only // ticks while it turns function register_press(ctx: ServerContext, recipes: ProcessingRecipe[]) { // the items lying in the block under it, a bit in from its sides when looking for one, like create's const items_under = (block: BlockRef, inset: number) => ctx.world.entities_in( { x: block.x + inset, y: block.y - 1 + inset, z: block.z + inset }, { x: block.x + 1 - inset, y: block.y - inset, z: block.z + 1 - inset }, "bworld:item", ).filter((item) => item.on_ground && processing_for(recipes, item.get_item()!.id)); const press = (item: EntityRef) => { const stack = item.get_item()!; const results = roll_results(processing_for(recipes, stack.id)!); const { x, y, z } = item.position; if (stack.count === 1) { const [first, ...rest] = results; item.set_item(first); for (const extra of rest) { const { x: vx, y: vy, z: vz } = item.velocity; ctx.world.spawn_item(x, y, z, extra).push(vx, vy, vz); } return; } const scatter = () => (Math.random() - 0.5) * 2; for (const result of results) ctx.world.spawn_item(x, y, z, result).push(scatter(), scatter(), scatter()); item.set_item({ ...stack, count: stack.count - 1 }); }; ctx.components.register_block("create:mechanical_press", { on_tick(block) { const data: PressData = block.data ?? {}; const rate = cycle_rate(block.get_client_data("speed") ?? 0); if (data.pressing === undefined) { // a block that starts turning ticks again, see kinetic_world if (rate === 0) { block.set_ticking(false); return; } const scan = (data.scan ?? 1) - 1; const found = scan <= 0 && items_under(block, 0.125).length > 0; block.data = { ...data, scan: scan <= 0 ? SCAN_TICKS : scan, ...(found && { pressing: 0 }) }; if (found) block.set_client_data("press", { steps: 0, rate } satisfies PressShown); return; } const steps = data.pressing; if (steps === PRESS_AT && rate > 0) { const [item] = items_under(block, 0); if (item) press(item); } // done, and it looks again straight away, for the rest of a stack if (steps > CYCLE) { const { pressing: _, scan: __, ...rest } = data; block.data = rest; block.set_client_data("press", undefined); return; } // it stops at the bottom to press on the next tick, however fast it's going const next = steps < PRESS_AT && steps + rate >= PRESS_AT ? PRESS_AT : steps + rate; block.data = { ...data, pressing: next }; if (block.get_client_data("press")?.rate !== rate) { block.set_client_data("press", { steps: next, rate } satisfies PressShown); } if (rate === 0) block.set_ticking(false); }, }); } // 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(); 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("speed") ?? 0, source: data.source, network: data.network, generated, }; }, set(at, speed, source, network) { const block = block_at(at); // 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); 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, }; }