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
+12 -6
View File
@@ -2,10 +2,18 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
// how create's moving blocks are drawn: kinetic blocks turn by the speed the server puts in their "speed" client data,
// as rotating instances, so a block turning steadily costs nothing per frame
// as rotating instances, so a block turning steadily costs nothing per frame. they're still while their network is
// overstressed
import type { ClientContext, Instances, ModelRef, Visual, VisualBlock } from "bworld/client";
import type { Face } from "bworld/shared";
import { type Axis, FACINGS, kinetic_shape, type KineticParams, type KineticShape } from "./kinetics/connections.ts";
import {
actual_speed,
type Axis,
FACINGS,
kinetic_shape,
type KineticParams,
type KineticShape,
} from "./kinetics/connections.ts";
import { head_offset, type PressShown } from "./processing/press.ts";
// the crank's handle model has its hub on its south side and its grip reaching north, so it's turned to put north
@@ -104,7 +112,7 @@ export function setup(ctx: ClientContext) {
const ticks = last === undefined ? 0 : time - last;
last = time;
// a quarter of the way to the shaft's speed each tick
velocity += (speed_of(block) * 0.3 - velocity) * Math.min(1, ticks / 4);
velocity += (actual_speed(block) * 0.3 - velocity) * Math.min(1, ticks / 4);
angle = (angle + velocity * ticks) % 360;
handle.set_rotation(shape.axis, 0, angle);
},
@@ -112,15 +120,13 @@ export function setup(ctx: ClientContext) {
}, { replaces_model: true });
}
const speed_of = (block: VisualBlock) => block.get_client_data<number>("speed") ?? 0;
// a model turning around the block's axis at its speed, the block's own when not given. returns what turns it again
// when the speed changes. on every other block it's offset by half a small cog's tooth, so cogs mesh with the ones
// beside them, and on the rest by offset, like KineticBlockEntityVisual.rotationOffset
function turning(block: VisualBlock, instances: Instances, shape: KineticShape, model?: ModelRef, offset = 0) {
const instance = instances.rotating(model);
const angle = should_offset(shape.axis, block) ? 22.5 : offset;
const turn = () => instance.set_rotation(shape.axis, speed_of(block), angle);
const turn = () => instance.set_rotation(shape.axis, actual_speed(block), angle);
turn();
return turn;
}
@@ -7,6 +7,7 @@
import type { Face } from "bworld/shared";
export type Axis = "x" | "y" | "z";
export type Pos = readonly [number, number, number];
// the create:kinetic component's params, from a block's json
export interface KineticParams {
@@ -157,3 +158,22 @@ export function neighbour_positions(block: Kinetic): [number, number, number][]
];
return offsets.map(([dx, dy, dz]) => [block.x + dx, block.y + dy, block.z + dz]);
}
// the kinetic blocks next to node that turn it or that it turns, like getConnectedNeighbours. get is undefined where
// what's there isn't kinetic
export function connected<T extends Kinetic>(
get: (x: number, y: number, z: number) => T | undefined,
node: Kinetic,
): T[] {
return neighbour_positions(node).flatMap(([x, y, z]) => {
const neighbour = get(x, y, z);
const turns = neighbour && (speed_ratio(node, neighbour) !== 0 || speed_ratio(neighbour, node) !== 0);
return turns ? [neighbour] : [];
});
}
// how fast a block acts and is drawn turning, from its client data: its speed, or still while its network is
// overstressed, like KineticBlockEntity.getSpeed
export function actual_speed(block: { get_client_data<T>(field: string): T | undefined }): number {
return block.get_client_data<boolean>("overstressed") ? 0 : block.get_client_data<number>("speed") ?? 0;
}
+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
+70
View File
@@ -0,0 +1,70 @@
// 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;
}
+19 -6
View File
@@ -4,8 +4,8 @@
// create's blocks that do something: so far what turns (create:kinetic), the hand crank that turns it, and the
// mechanical press
import type { BlockRef, EntityRef, Id, ServerContext } from "bworld/server";
import { along, kinetic_shape, type KineticParams } from "./kinetics/connections.ts";
import { handle_added, handle_removed, type KineticWorld, type Pos, update_generated } from "./kinetics/spreading.ts";
import { actual_speed, along, kinetic_shape, type KineticParams, type Pos } from "./kinetics/connections.ts";
import { handle_added, handle_removed, type KineticWorld, update_generated } from "./kinetics/spreading.ts";
import {
PRESSING,
processing_for,
@@ -18,9 +18,13 @@ import { CYCLE, cycle_rate, PRESS_AT, type PressShown, SCAN_TICKS } from "./proc
interface Config {
max_speed: number;
crank_hunger: number;
// by block id, what each block takes per rpm and what each generator makes per rpm, like create's CStress. 0 for a
// block that isn't there
stress: { impact: Record<Id, number>; capacity: Record<Id, number> };
}
// a kinetic block's tile data, see KineticNode. its speed is its "speed" client data, which its visual turns it by
// a kinetic block's tile data, see KineticNode. its speed is its "speed" client data, and whether its network is
// overstressed its "overstressed" client data, which its visual turns it by
interface KineticData {
source?: Pos;
network?: string;
@@ -127,7 +131,7 @@ function register_press(ctx: ServerContext, recipes: ProcessingRecipe[]) {
ctx.components.register_block("create:mechanical_press", {
on_tick(block) {
const data: PressData = block.data ?? {};
const rate = cycle_rate(block.get_client_data<number>("speed") ?? 0);
const rate = cycle_rate(actual_speed(block));
if (data.pressing === undefined) {
// a block that starts turning ticks again, see kinetic_world
if (rate === 0) {
@@ -163,8 +167,8 @@ function register_press(ctx: ServerContext, recipes: ProcessingRecipe[]) {
});
}
// the world as rotation spreading sees it: blocks with create:kinetic, their speed in client data and where it comes
// from in tile data
// the world as rotation spreading sees it: blocks with create:kinetic, their speed and whether they're overstressed in
// client data, and where their speed comes from in tile data
function kinetic_world(ctx: ServerContext, config: Config): KineticWorld {
// components never change once loaded, so each block's are looked up once
const params = new Map<Id, { kinetic?: KineticParams; crank?: CrankParams }>();
@@ -207,6 +211,9 @@ function kinetic_world(ctx: ServerContext, config: Config): KineticWorld {
source: data.source,
network: data.network,
generated,
impact: config.stress.impact[block.id] ?? 0,
capacity: config.stress.capacity[block.id] ?? 0,
overstressed: block.get_client_data<boolean>("overstressed") ?? false,
};
},
set(at, speed, source, network) {
@@ -220,6 +227,12 @@ function kinetic_world(ctx: ServerContext, config: Config): KineticWorld {
break(at) {
ctx.world.break_block(...at);
},
set_overstressed(at, overstressed) {
const block = block_at(at);
// like a block that starts turning, see set
if (!overstressed && block.get_client_data("speed")) block.set_ticking(true);
block.set_client_data("overstressed", overstressed || undefined);
},
max_speed: config.max_speed,
};
}