51 lines
2.6 KiB
TypeScript
51 lines
2.6 KiB
TypeScript
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
|
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
// what decides how fast generators turn: water flowing past a water wheel, and the creative motor's speed as it's
|
|
// scrolled through. no ctx, so tests can call them directly
|
|
import type { Position } from "cubi/shared";
|
|
import { AXES, type Axis, face_axis, FACE_OFFSETS, FACINGS, type Pos } from "./connections.ts";
|
|
|
|
// which way water around a wheel on axis at at turns it: 1 for counterclockwise seen from the axis's positive end, -1
|
|
// for the other way, 0 when it's still or the flows cancel out. each of the 4 blocks around it across the axis with
|
|
// fluid flowing along a paddle's way round, more than at 60° to it, pushes it one way, like
|
|
// WaterWheelBlockEntity.determineAndApplyFlowScore. flow is which way the fluid at x y z flows, 0 0 0 for none
|
|
export function flow_score(axis: Axis, [x, y, z]: Pos, flow: (x: number, y: number, z: number) => Position): number {
|
|
const along = AXES.indexOf(axis);
|
|
let score = 0;
|
|
for (const face of FACINGS) {
|
|
if (face_axis(face) === axis) continue;
|
|
const offset = FACE_OFFSETS[face];
|
|
const { x: fx, y: fy, z: fz } = flow(x + offset[0], y + offset[1], z + offset[2]);
|
|
// only the part of it across the axis turns the wheel
|
|
const across = [fx, fy, fz].map((v, i) => i === along ? 0 : v);
|
|
const length = Math.hypot(...across);
|
|
if (length === 0) continue;
|
|
// the way a paddle there moves turning counterclockwise: the axis crossed with where it is
|
|
const unit = [0, 0, 0].map((_, i) => i === along ? 1 : 0);
|
|
const moving = [
|
|
unit[1] * offset[2] - unit[2] * offset[1],
|
|
unit[2] * offset[0] - unit[0] * offset[2],
|
|
unit[0] * offset[1] - unit[1] * offset[0],
|
|
];
|
|
const dot = (across[0] * moving[0] + across[1] * moving[1] + across[2] * moving[2]) / length;
|
|
if (Math.abs(dot) > 0.5) score += Math.sign(dot);
|
|
}
|
|
return Math.sign(score);
|
|
}
|
|
|
|
// the creative motor's speed one notch of the mouse wheel on from current, up when forward: by 1 near 0, and by 4, 16
|
|
// and 64 from 4, 32 and 128 rpm on, or always by 1 when fine. it never stops, it skips 0 to turn the other way, and it
|
|
// stays within max. like the 0.5 creative motor's step
|
|
export function motor_step(current: number, forward: boolean, fine: boolean, max: number): number {
|
|
let step = 1;
|
|
if (!fine) {
|
|
const magnitude = Math.abs(current) - (forward === current > 0 ? 0 : 1);
|
|
if (magnitude >= 4) step *= 4;
|
|
if (magnitude >= 32) step *= 4;
|
|
if (magnitude >= 128) step *= 4;
|
|
}
|
|
if (current + (forward ? step : -step) === 0) step += 1;
|
|
return Math.max(-max, Math.min(max, current + (forward ? step : -step)));
|
|
}
|