Mechanical press !

This commit is contained in:
2026-10-15 13:07:09 -03:00
parent b43e8fe8b8
commit c7a2392591
15 changed files with 403 additions and 86 deletions
+93 -5
View File
@@ -1,12 +1,19 @@
// 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. and its
// recipe types' checks
import type { BlockRef, Id, ServerContext } from "bworld/server";
// 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_recipes } from "./recipes/processing.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;
@@ -34,12 +41,19 @@ interface CrankData extends KineticData {
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
processing_recipes(ctx.recipes.list(PRESSING.id), PRESSING, {
const pressing = processing_recipes(ctx.recipes.list(PRESSING.id), PRESSING, {
expand: (choice) => ctx.tags.expand(choice),
item_exists: (id) => ctx.items.exists(id),
});
@@ -75,6 +89,78 @@ export function setup(ctx: ServerContext) {
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<number>("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<PressShown>("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
@@ -125,6 +211,8 @@ function kinetic_world(ctx: ServerContext, config: Config): KineticWorld {
},
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 }) };