Files
bworld/mods/create/scripts/kinetics/spreading.ts
T
2026-10-28 22:10:48 -03:00

187 lines
8.3 KiB
TypeScript

// SPDX-FileCopyrightText: 2026 Paula Wisnievski
// SPDX-License-Identifier: AGPL-3.0-or-later
// rotation spreading through what's connected, like create's RotationPropagator: a block that's placed takes the speed
// of what turns it, a generator's speed spreads to everything it turns, the faster of two takes the other's blocks
// over, and a block that would turn two ways at once breaks. each walk is a queue, not recursion, and runs only when
// something changes, and then so does stress for the networks it changed. no ctx, the world is anything that keeps
// these, so tests can give their own
import { connected as connected_in, type Kinetic, neighbour_positions, type Pos, speed_ratio } from "./connections.ts";
import { type StressNode, type StressWorld, update_stress } from "./stress.ts";
// a kinetic block as the world keeps it
export type KineticNode = StressNode;
export interface KineticWorld extends StressWorld {
// undefined when what's there isn't kinetic
get(x: number, y: number, z: number): KineticNode | undefined;
set(at: Pos, speed: number, source: Pos | undefined, network: string | undefined): void;
// breaks it and drops it, like a shaft turned two ways at once. its on_break runs handle_removed
break(at: Pos): void;
// the fastest anything can turn, like the maxRotationSpeed config
readonly max_speed: number;
}
// a block was placed: it takes the speed of what turns it, and passes it on, like RotationPropagator.handleAdded
export function handle_added(world: KineticWorld, at: Pos) {
with_stress(world, undefined, (world) => spread(world, at));
}
// a block is going: what it turned stops, then what's still turning around that spreads back in, like handleRemoved.
// on_break runs while it's still there, so the walks don't see it
export function handle_removed(world: KineticWorld, at: Pos) {
const node = world.get(...at);
if (!node || node.speed === 0) return;
const without: KineticWorld = {
get: (x, y, z) => x === at[0] && y === at[1] && z === at[2] ? undefined : world.get(x, y, z),
set: (...args) => world.set(...args),
break: (where) => world.break(where),
set_overstressed: (...args) => world.set_overstressed(...args),
max_speed: world.max_speed,
};
with_stress(without, node.network, (world) => restart(world, detach(world, node)));
}
// a generator's speed changed, like a crank being turned or let go. like GeneratingKineticBlockEntity's
// updateGeneratedRotation. it changes the network's capacity even when its speed stays the same
export function update_generated(world: KineticWorld, at: Pos) {
with_stress(world, world.get(...at)?.network, (world) => generate(world, at));
}
// runs change on world, keeping track of the networks it changes, then works out their stress again. network is the
// one the block that changed is in, whose stress changes even if nothing else in it does
function with_stress(world: KineticWorld, network: string | undefined, change: (world: KineticWorld) => void) {
const networks = new Set<string>(network === undefined ? [] : [network]);
const changed: Pos[] = [];
change({
get: (x, y, z) => world.get(x, y, z),
set(where, speed, source, network) {
const before = world.get(...where)?.network;
if (before !== undefined) networks.add(before);
if (network !== undefined) networks.add(network);
changed.push(where);
world.set(where, speed, source, network);
},
break: (where) => world.break(where),
set_overstressed: (...args) => world.set_overstressed(...args),
max_speed: world.max_speed,
});
update_stress(world, networks, changed);
}
function generate(world: KineticWorld, at: Pos) {
const node = world.get(...at);
if (!node || node.generated === node.speed) return;
const speed = node.generated;
if (speed === 0) {
// something else turns it, so it goes on turning with that
if (node.source) return;
world.set(at, 0, undefined, undefined);
restart(world, detach(world, node));
return;
}
if (node.source && node.speed !== 0 && Math.abs(node.speed) >= Math.abs(speed)) {
// something as fast or faster turns it: it goes along, unless it's turning the other way
if (Math.sign(node.speed) !== Math.sign(speed)) world.break(at);
return;
}
// it turns everything it's connected to, at its own speed. what it turned before stops first, so its old speed
// doesn't meet its new one
world.set(at, speed, undefined, network_of(at));
const stopped = node.speed !== 0 ? detach(world, node) : [];
spread(world, at);
restart(world, stopped);
}
const same = (a: Pos | undefined, b: Pos) => a !== undefined && a[0] === b[0] && a[1] === b[1] && a[2] === b[2];
const pos_of = (node: Kinetic): Pos => [node.x, node.y, node.z];
const network_of = (at: Pos) => at.join(" ");
const connected = (world: KineticWorld, node: Kinetic) => connected_in((x, y, z) => world.get(x, y, z), node);
// spreads the speed at start to what it's connected to, and on through whatever that changes, like
// propagateNewSource
function spread(world: KineticWorld, start: Pos) {
const queue: Pos[] = [start];
for (let i = 0; i < queue.length; i++) {
const at = queue[i];
const current = world.get(...at);
if (!current) continue;
for (const neighbour of connected(world, current)) {
const new_speed = current.speed * speed_ratio(current, neighbour);
const opposite_speed = neighbour.speed * speed_ratio(neighbour, current);
if (new_speed === 0 && opposite_speed === 0) continue;
const against = new_speed !== 0 && neighbour.speed !== 0 &&
Math.sign(new_speed) !== Math.sign(neighbour.speed);
const too_fast = Math.abs(new_speed) > world.max_speed || Math.abs(opposite_speed) > world.max_speed;
if (against || too_fast) {
world.break(at);
break;
}
// the neighbour is faster, so it turns this one, which passes that on from here
if (Math.abs(opposite_speed) > Math.abs(current.speed)) {
world.set(at, opposite_speed, pos_of(neighbour), neighbour.network);
queue.push(at);
break;
}
// this one is at least as fast, so it takes over everything the neighbour turns. unless the same generator
// turns both, which is a loop: fine while the speeds agree, and this breaks when they don't
const loop = current.network === undefined || current.network === neighbour.network;
if (Math.abs(new_speed) >= Math.abs(neighbour.speed) && loop) {
if (Math.abs(new_speed) > Math.abs(neighbour.speed) * (1 + 1 / 65536)) {
world.break(at);
break;
}
continue;
}
if (Math.abs(new_speed) < Math.abs(neighbour.speed) && Math.abs(neighbour.speed - new_speed) <= 1e-4) {
continue;
}
world.set(pos_of(neighbour), new_speed, at, current.network);
queue.push(pos_of(neighbour));
}
}
}
// the blocks node turned stop, like the removed or stopped block's part of RotationPropagator.handleRemoved. returns
// the generators among them, see restart
function detach(world: KineticWorld, node: Kinetic): Pos[] {
const at = pos_of(node);
return neighbour_positions(node).flatMap((where) =>
same(world.get(...where)?.source, at) ? clear(world, where) : []
);
}
// generators that stopped with what turned them start again with their own speed, like reActivateSource. that's a
// tick later in create, so here it's after the walk that stopped them, when what's turning around them has settled
function restart(world: KineticWorld, generators: Pos[]) {
for (const at of generators) generate(world, at);
}
// stops start and everything that got its speed through it, then lets what's still turning at the edges spread back
// in, like propagateMissingSource. returns the generators in it
function clear(world: KineticWorld, start: Pos): Pos[] {
const missing = world.get(...start)?.source;
const edges: Pos[] = [];
const generators: Pos[] = [];
const frontier: Pos[] = [start];
for (let i = 0; i < frontier.length; i++) {
const at = frontier[i];
const current = world.get(...at);
if (!current) continue;
world.set(at, 0, undefined, undefined);
if (current.generated !== 0) generators.push(at);
for (const neighbour of connected(world, current)) {
const where = pos_of(neighbour);
if (same(missing, where) || !neighbour.source) continue;
if (same(neighbour.source, at)) frontier.push(where);
else edges.push(where);
}
}
// an edge can turn out to be part of what stopped, found from another side later
const edge = edges.find((at) => world.get(...at)?.source !== undefined);
if (edge) spread(world, edge);
return generators;
}