71 lines
3.2 KiB
TypeScript
71 lines
3.2 KiB
TypeScript
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
|
||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||
|
||
// stress, like create's KineticNetwork: a network's capacity is what its generators make, each one's capacity × its
|
||
// rpm, and its stress what its blocks take, each one's impact × its rpm. when the stress is more than the capacity the
|
||
// network is overstressed, and everything in it acts and is drawn still. a network is the blocks that get their speed
|
||
// from one generator, found by walking from it when something in it changes, never per tick. every block's tile data
|
||
// is kept whether its chunk is loaded or not, so the walk sees all of it and nothing needs counting apart. no ctx
|
||
import { connected, type Kinetic, type Pos } from "./connections.ts";
|
||
|
||
export interface StressNode extends Kinetic {
|
||
// rpm around its axis, counterclockwise seen from the axis's positive end, the speed it would turn at if it weren't
|
||
// overstressed, like getTheoreticalSpeed
|
||
readonly speed: number;
|
||
// the generator its speed comes from, as "x y z", which is also how a loop is told 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;
|
||
// what it takes per rpm, like BlockStressValues.getImpact
|
||
readonly impact: number;
|
||
// what it makes per rpm while it makes a speed, like BlockStressValues.getCapacity
|
||
readonly capacity: number;
|
||
readonly overstressed: boolean;
|
||
}
|
||
|
||
export interface StressWorld {
|
||
get(x: number, y: number, z: number): StressNode | undefined;
|
||
set_overstressed(at: Pos, overstressed: boolean): void;
|
||
}
|
||
|
||
// works out again whether each of networks is overstressed, and marks each of its blocks. blocks in changed that
|
||
// aren't in a network any more aren't overstressed either
|
||
export function update_stress(world: StressWorld, networks: Iterable<string>, changed: Iterable<Pos>) {
|
||
for (const network of networks) {
|
||
const members = members_of(world, network);
|
||
let capacity = 0;
|
||
let stress = 0;
|
||
for (const node of members) {
|
||
if (node.generated !== 0) capacity += node.capacity * Math.abs(node.generated);
|
||
stress += node.impact * Math.abs(node.speed);
|
||
}
|
||
const overstressed = stress > capacity;
|
||
for (const node of members) {
|
||
if (node.overstressed !== overstressed) world.set_overstressed([node.x, node.y, node.z], overstressed);
|
||
}
|
||
}
|
||
for (const at of changed) {
|
||
const node = world.get(...at);
|
||
if (node?.overstressed && node.network === undefined) world.set_overstressed(at, false);
|
||
}
|
||
}
|
||
|
||
// the blocks in a network, walking out from its generator through the blocks that turn with it
|
||
function members_of(world: StressWorld, network: string): StressNode[] {
|
||
const [x, y, z] = network.split(" ").map(Number);
|
||
const root = world.get(x, y, z);
|
||
if (root?.network !== network) return [];
|
||
const found = new Set([network]);
|
||
const members = [root];
|
||
for (let i = 0; i < members.length; i++) {
|
||
for (const neighbour of connected((x, y, z) => world.get(x, y, z), members[i])) {
|
||
const key = `${neighbour.x} ${neighbour.y} ${neighbour.z}`;
|
||
if (neighbour.network !== network || found.has(key)) continue;
|
||
found.add(key);
|
||
members.push(neighbour);
|
||
}
|
||
}
|
||
return members;
|
||
}
|