Stress for create networks

This commit is contained in:
2026-10-28 21:28:36 -03:00
parent a70375c9ad
commit 507e9aab24
10 changed files with 276 additions and 46 deletions
+37 -24
View File
@@ -4,24 +4,18 @@
// 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. no ctx, the world is anything that keeps these, so tests can give their own
import { type Kinetic, neighbour_positions, speed_ratio } from "./connections.ts";
export type Pos = readonly [number, number, number];
// 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 interface KineticNode extends Kinetic {
// rpm around its axis, counterclockwise seen from the axis's positive end, like getTheoreticalSpeed
readonly speed: number;
export interface KineticNode extends StressNode {
// the block next to it it gets its speed from. undefined for a still block, and for a generator turning it
readonly source?: Pos;
// the generator its speed comes from, as "x y z", for telling a loop apart from another network meeting it
readonly network?: string;
// the speed it makes itself, 0 for anything that isn't a generator or one that's still, like getGeneratedSpeed
readonly generated: number;
}
export interface KineticWorld {
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;
@@ -33,7 +27,7 @@ export interface KineticWorld {
// 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) {
spread(world, at);
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.
@@ -45,14 +39,40 @@ export function handle_removed(world: KineticWorld, at: Pos) {
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,
};
restart(without, detach(without, node));
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
// 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;
@@ -80,14 +100,7 @@ const same = (a: Pos | undefined, b: Pos) => a !== undefined && a[0] === b[0] &&
const pos_of = (node: Kinetic): Pos => [node.x, node.y, node.z];
const network_of = (at: Pos) => at.join(" ");
// the kinetic blocks next to node that turn it or that it turns, like getConnectedNeighbours
function connected(world: KineticWorld, node: Kinetic): KineticNode[] {
return neighbour_positions(node).flatMap(([x, y, z]) => {
const neighbour = world.get(x, y, z);
const turns = neighbour && (speed_ratio(node, neighbour) !== 0 || speed_ratio(neighbour, node) !== 0);
return turns ? [neighbour] : [];
});
}
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
@@ -146,7 +159,7 @@ function detach(world: KineticWorld, node: Kinetic): Pos[] {
// 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) update_generated(world, at);
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