Files
bworld/mods/create/scripts/processing/press.ts
T
2026-10-15 13:07:09 -03:00

34 lines
1.5 KiB
TypeScript

// SPDX-FileCopyrightText: 2026 Paula Wisnievski
// SPDX-License-Identifier: AGPL-3.0-or-later
// the mechanical press's cycle, like create's PressingBehaviour: the head comes down, presses at the bottom halfway
// through, and goes back up. it counts CYCLE steps, more of them a tick the faster the press turns. no ctx, the server
// runs the cycle and visuals draw it from the same numbers
export const CYCLE = 240;
// where it presses, at the bottom
export const PRESS_AT = CYCLE / 2;
// how often a still press looks for something to press, in ticks
export const SCAN_TICKS = 10;
// a pressing press's "press" client data, what its visual moves the head by: the step of the cycle it was at when this
// was set, and how many steps a tick it's going
export interface PressShown {
steps: number;
rate: number;
}
// steps of the cycle a tick at a speed in rpm, like getRunningTickSpeed: 1 almost still, up to 60 at 512 rpm
export function cycle_rate(speed: number): number {
if (speed === 0) return 0;
return Math.floor(1 + 59 * Math.min(1, Math.abs(speed) / 512));
}
// how far down the head is that far through the cycle, 0 up to 1 at the bottom, like getRenderedHeadOffset: it
// speeds up on the way down, stays down a moment after pressing, and goes up at a steady speed
export function head_offset(steps: number): number {
const clamp = (value: number) => Math.max(0, Math.min(1, value));
if (steps < CYCLE * 2 / 3) return clamp((steps / CYCLE * 2) ** 3);
return clamp((CYCLE - steps) / CYCLE * 3);
}