Verdigris working !

This commit is contained in:
2026-10-22 15:51:40 -03:00
parent 6af6c52673
commit db0589d965
9 changed files with 542 additions and 104 deletions
+5 -1
View File
@@ -71,7 +71,11 @@ export function around_edge(x: number, y: number, z: number, face: Face, directi
// the wire cell a way out joins, undefined when it joins a block that isn't wire or nothing
export function joined_cell(world: BlockReader, x: number, y: number, z: number, face: Face, direction: Face) {
const way = joins_out(world, x, y, z, face, direction);
return cell_through(joins_out(world, x, y, z, face, direction), x, y, z, face, direction);
}
// the wire cell a way out that joins way leads to, undefined for a block that isn't wire or nothing
export function cell_through(way: WayOut | undefined, x: number, y: number, z: number, face: Face, direction: Face) {
if (way === "straight") return step(x, y, z, direction);
if (way === "edge") return around_edge(x, y, z, face, direction);
return undefined;
+13 -8
View File
@@ -1,41 +1,44 @@
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
// SPDX-License-Identifier: AGPL-3.0-or-later
// verdigris's components, see REDSTONE.md: the wire, which works out its net's power whenever something around it
// changes, sources, and the devices
// verdigris's components, see REDSTONE.md: the wire, which keeps its net's power through the net index, sources, and
// the devices
import type { BlockRef, Face, Position, ServerContext } from "bworld/server";
import { register_lever } from "./devices.ts";
import { arms_of, around_edge, can_cover, covers, face_states, faces_of, MAX_ARMS } from "./joins.ts";
import { type BlockReader, source_lookup, strong_sides } from "./signals.ts";
import { DIRECTIONS, FACES, OPPOSITE, step } from "./sides.ts";
import { type SignalWorld, tell_around_below, update_net, wire_at } from "./wires.ts";
import { NetIndex, type SignalWorld, tell_around_below, wire_at } from "./wires.ts";
// a wire's states that are its shape: the faces it covers and its arms. its power changing isn't
const SHAPE_STATES = [...FACES, ...Array.from({ length: MAX_ARMS }, (_, i) => `arm_${i}`)];
export function register_verdigris(ctx: ServerContext) {
const world = signal_world(ctx);
const nets = new NetIndex(world);
ctx.components.register_block("bworld:verdigris_wire", {
on_create(block) {
tell_around_edges(world, block);
update_net(world, block.x, block.y, block.z);
nets.created(block.x, block.y, block.z);
},
on_break(block) {
const wire = wire_at(world, block.x, block.y, block.z);
tell_around_edges(world, block);
if (wire && wire.power > 0) tell_around_below(world, wire);
if (wire && ctx.world.get_state(block.x, block.y, block.z, "power")) tell_around_below(world, wire);
nets.breaking(block.x, block.y, block.z);
},
on_neighbor_change(block, _params, neighbor) {
if (lose_face(ctx, world, block, neighbor)) return;
update_net(world, block.x, block.y, block.z);
nets.beside_changed(block.x, block.y, block.z, neighbor);
},
// it covered another face, or its rule joined or let go of something, which can join two nets or split one
// it lost a face, or its rule joined or let go of something, which can join two nets or split one
on_state_change(block, _params, before) {
if (nets.setting_power) return;
const changed = SHAPE_STATES.some((name) =>
ctx.world.get_state(block.x, block.y, block.z, name) !== before[name]
);
if (changed) update_net(world, block.x, block.y, block.z);
if (changed) nets.reshaped(block.x, block.y, block.z);
},
});
@@ -89,6 +92,8 @@ function lose_face(ctx: ServerContext, world: SignalWorld, block: BlockRef, neig
ctx.world.break_block(block.x, block.y, block.z);
return true;
}
// the wires it joins around the edges of the face it loses aren't beside it, and the engine doesn't tell them
tell_around_edges(world, block);
ctx.world.drop_item(block.x, block.y, block.z, { id: "bworld:verdigris", count: 1 });
const states = { ...face_states(left), ...arms_of(world, block.x, block.y, block.z, left) };
for (const [name, value] of Object.entries(states)) ctx.world.set_state(block.x, block.y, block.z, name, value);
+13
View File
@@ -32,6 +32,19 @@ export function step(x: number, y: number, z: number, face: Face): [number, numb
return [x + dx, y + dy, z + dz];
}
// a position as one number, so maps and sets of them don't make a string each: x and z in 21 bits and y in 8, further
// than chunk_key lets a world go
export function pack(x: number, y: number, z: number) {
return ((x + 2 ** 20) * 2 ** 21 + z + 2 ** 20) * 256 + y;
}
export function unpack(key: number): [number, number, number] {
const y = key % 256;
const xz = (key - y) / 256;
const z = xz % 2 ** 21;
return [(xz - z) / 2 ** 21 - 2 ** 20, y, z - 2 ** 20];
}
// which way a block points, from its states: the name of a facing state, read as FACES' values with 2 bits or 3, or
// a face and facing state for blocks on a floor, wall or ceiling, like a lever. face 0 is the floor, where it points
// up, 1 a wall, where it points the way facing says, and 2 the ceiling, where it points down
+277 -87
View File
@@ -3,12 +3,11 @@
// wire nets, see "Power" in REDSTONE.md: a wire cell and every wire cell it reaches through its joins are one net, and
// they all have the same power, the most any block gives into any of them. a cell's faces always join each other, so
// it's one piece of one net. walked again on every change for now. the net index that keeps walks between changes
// comes later
// it's one piece of one net. NetIndex keeps the nets between changes, see "The Net Index"
import type { Face } from "bworld/shared";
import { faces_of, joined_cell, joins_out, ways_out } from "./joins.ts";
import { cell_through, faces_of, joins_out, ways_out } from "./joins.ts";
import { type BlockReader, MAX_POWER, output, type SignalReader, solid_power } from "./signals.ts";
import { FACES, OPPOSITE, step } from "./sides.ts";
import { FACES, OPPOSITE, pack, step, unpack } from "./sides.ts";
export interface SignalWorld extends SignalReader {
// sets a wire's power without telling the blocks around it, see ServerWorld.set_state's update_neighbors
@@ -25,42 +24,13 @@ export interface Wire {
readonly faces: readonly Face[];
// the ways out of its cell it joins, each a face and a direction, see ways_out
readonly out: readonly [Face, Face][];
readonly power: number;
}
const key = (x: number, y: number, z: number) => `${x} ${y} ${z}`;
// the wire at x y z, undefined when there's none
export function wire_at(world: BlockReader, x: number, y: number, z: number): Wire | undefined {
const faces = faces_of(world, x, y, z);
if (faces.length === 0) return undefined;
return {
x,
y,
z,
faces,
out: ways_out(faces).filter((_, i) => world.get_state(x, y, z, `arm_${i}`) === 1),
power: world.get_state(x, y, z, "power") ?? 0,
};
}
// every wire in the net of the wire at x y z, nothing when there's no wire there
export function walk(world: BlockReader, x: number, y: number, z: number): Wire[] {
const seen = new Set([key(x, y, z)]);
const queue: [number, number, number][] = [[x, y, z]];
const wires: Wire[] = [];
while (queue.length > 0) {
const wire = wire_at(world, ...queue.pop()!);
if (!wire) continue;
wires.push(wire);
for (const [face, direction] of wire.out) {
const next = joined_cell(world, wire.x, wire.y, wire.z, face, direction);
if (!next || seen.has(key(...next))) continue;
seen.add(key(...next));
queue.push(next);
}
}
return wires;
return { x, y, z, faces, out: ways_out(faces).filter((_, i) => world.get_state(x, y, z, `arm_${i}`) === 1) };
}
// the blocks a wire's faces lie on
@@ -68,59 +38,6 @@ function below(wire: Wire) {
return wire.faces.map((face) => ({ face, at: step(wire.x, wire.y, wire.z, face) }));
}
// the power going into a wire: from each block it lies on, a source's output on that side or a solid block's power,
// and from the blocks that aren't wire it joins
export function wire_input(world: SignalReader, wire: Wire) {
let most = 0;
for (const { face, at } of below(wire)) {
const id = world.get_block(...at);
if (id !== undefined) {
most = Math.max(most, output(world, ...at, id, OPPOSITE[face]), solid_power(world, ...at, id));
}
if (most === MAX_POWER) return most;
}
for (const [face, direction] of wire.out) {
if (joins_out(world, wire.x, wire.y, wire.z, face, direction) !== "block") continue;
const [nx, ny, nz] = step(wire.x, wire.y, wire.z, direction);
most = Math.max(most, output(world, nx, ny, nz, world.get_block(nx, ny, nz)!, OPPOSITE[direction]));
if (most === MAX_POWER) return most;
}
return most;
}
// works out the power of the net the wire at x y z is in, and sets it on each of its wires that doesn't have it. then
// each block the net gives power to is told once: the blocks its wires join, the blocks they lie on and the blocks
// around those, which are powered through them
export function update_net(world: SignalWorld, x: number, y: number, z: number) {
const wires = walk(world, x, y, z);
let power = 0;
for (const wire of wires) {
power = Math.max(power, wire_input(world, wire));
if (power === MAX_POWER) break;
}
if (wires.every((wire) => wire.power === power)) return;
for (const wire of wires) {
if (wire.power !== power) world.set_power(wire.x, wire.y, wire.z, power);
}
const told = new Set(wires.map((wire) => key(wire.x, wire.y, wire.z)));
const tell = (at: [number, number, number], from: { x: number; y: number; z: number }) => {
if (told.has(key(...at))) return;
told.add(key(...at));
world.neighbor_changed(...at, from);
};
for (const wire of wires) {
for (const [face, direction] of wire.out) {
if (joins_out(world, wire.x, wire.y, wire.z, face, direction) === "block") {
tell(step(wire.x, wire.y, wire.z, direction), wire);
}
}
for (const { at } of below(wire)) {
tell(at, wire);
for (const face of FACES) tell(step(...at, face), { x: at[0], y: at[1], z: at[2] });
}
}
}
// the blocks around the blocks a wire lies on, told when it's gone with power, since nothing else tells them they're
// no longer powered through
export function tell_around_below(world: SignalWorld, wire: Wire) {
@@ -135,3 +52,276 @@ export function tell_around_below(world: SignalWorld, wire: Wire) {
}
}
}
// a wire cell in a net, with what its ways out joined when it was read: wire cells, packed, and the directions of
// blocks that aren't wire
interface Cell {
readonly wire: Wire;
readonly joins: readonly number[];
readonly blocks: readonly Face[];
}
interface Net {
// by packed position
readonly cells: Map<number, Cell>;
// the power going into each of its cells that gets some from outside it, see #input
readonly inputs: Map<number, number>;
// the most of inputs, which each of its cells has
power: number;
}
function most(inputs: Map<number, number>) {
let power = 0;
for (const input of inputs.values()) power = Math.max(power, input);
return power;
}
// the nets of the wire cells anything has touched, kept between changes so most changes cost a few reads instead of a
// walk, see "The Net Index" in REDSTONE.md. it's only in memory, and a cell it doesn't have is walked the first time
// anything touches it, so it can't hold anything the world doesn't. the wire's server hooks call created, breaking,
// reshaped and beside_changed
export class NetIndex {
readonly #world: SignalWorld;
// each cell's net, by packed position
readonly #nets = new Map<number, Net>();
// cells of nets that were forgotten, to walk again, see #settle
readonly #stale: number[] = [];
#setting = false;
constructor(world: SignalWorld) {
this.#world = world;
}
// whether it's setting wires' power, which changes no net's shape
get setting_power() {
return this.#setting;
}
// a wire was placed at x y z: it and the nets of the cells it joins are one net
created(x: number, y: number, z: number) {
this.#settle();
if (this.#nets.has(pack(x, y, z))) return;
const cell = this.#read(x, y, z);
if (cell) this.#link(x, y, z, cell, undefined);
}
// the wire at x y z is breaking: its net is forgotten, and what's left of it is walked again once it's gone, by the
// first cell of it that hears
breaking(x: number, y: number, z: number) {
const key = pack(x, y, z);
const net = this.#nets.get(key);
if (net) this.#forget(net, key);
}
// the wire at x y z covers other faces or joins other things. letting go of a cell can split its net, so the net is
// walked again. otherwise the nets of the cells it joins now are joined to it
reshaped(x: number, y: number, z: number) {
this.#settle();
const key = pack(x, y, z);
const net = this.#nets.get(key);
const cell = this.#read(x, y, z);
if (net && cell && net.cells.get(key)!.joins.every((joined) => cell.joins.includes(joined))) {
this.#link(x, y, z, cell, net);
return;
}
if (net) this.#forget(net);
else this.#stale.push(key);
this.#settle();
}
// the block at beside changed, which can change what the wire cell at x y z gets from outside, and so its net's
// power. one where a way out of it leads can change what that joins without changing its arm_ state, like a lever
// placed in the cell a join went around an edge through, so the cell is read again
beside_changed(x: number, y: number, z: number, beside: { x: number; y: number; z: number }) {
this.#settle();
const key = pack(x, y, z);
const net = this.#nets.get(key);
if (!net) {
this.#stale.push(key);
this.#settle();
return;
}
const cell = net.cells.get(key)!;
const changed = pack(beside.x, beside.y, beside.z);
if (cell.wire.out.some(([, direction]) => pack(...step(x, y, z, direction)) === changed)) {
this.reshaped(x, y, z);
return;
}
const input = this.#input(cell);
if (input === (net.inputs.get(key) ?? 0)) return;
if (input > 0) net.inputs.set(key, input);
else net.inputs.delete(key);
const power = most(net.inputs);
if (power === net.power) return;
net.power = power;
this.#set_power(net, [...net.cells.values()]);
}
// makes cell, at x y z, one net with the cells it joins, and works out its input again. was is its net before,
// undefined for a new wire. the cells that don't have the net's power get it, and nothing else is written
#link(x: number, y: number, z: number, cell: Cell, was: Net | undefined) {
const key = pack(x, y, z);
// a cell it joins that isn't in a net yet is walked first. if that walk got here, it saw to everything
for (const joined of cell.joins) if (!this.#nets.has(joined)) this.#stale.push(joined);
this.#settle();
if (this.#nets.get(key) !== was) return;
const nets = new Set<Net>();
if (was) nets.add(was);
for (const joined of cell.joins) {
const net = this.#nets.get(joined);
if (net) nets.add(net);
}
const input = this.#input(cell);
let power = input;
for (const net of nets) {
for (const [at, given] of net.inputs) if (at !== key) power = Math.max(power, given);
}
// found before the nets are joined, when it's still known which had which power
const changed: Cell[] = [];
for (const net of nets) {
if (net.power === power) continue;
for (const [at, other] of net.cells) if (at !== key) changed.push(other);
}
if (was ? was.power !== power : (this.#world.get_state(x, y, z, "power") ?? 0) !== power) changed.push(cell);
let net: Net = { cells: new Map(), inputs: new Map(), power };
for (const other of nets) if (other.cells.size > net.cells.size) net = other;
for (const other of nets) if (other !== net) this.#absorb(net, other);
net.cells.set(key, cell);
this.#nets.set(key, net);
if (input > 0) net.inputs.set(key, input);
else net.inputs.delete(key);
net.power = power;
this.#set_power(net, changed);
}
// takes net out of the index, and puts its cells but except in #stale to walk again
#forget(net: Net, except?: number) {
for (const key of net.cells.keys()) {
this.#nets.delete(key);
if (key !== except) this.#stale.push(key);
}
}
// puts from's cells in net
#absorb(net: Net, from: Net) {
for (const [key, cell] of from.cells) {
net.cells.set(key, cell);
this.#nets.set(key, net);
}
for (const [key, input] of from.inputs) net.inputs.set(key, input);
}
// walks from each cell in #stale that no walk has reached since it was put there, then gives the cells of the nets
// those walks found that don't have their net's power
#settle() {
if (this.#stale.length === 0) return;
const found = new Set<Net>();
while (this.#stale.length > 0) {
const key = this.#stale.pop()!;
if (!this.#nets.has(key)) this.#walk(key, found);
}
for (const net of found) {
this.#set_power(
net,
[...net.cells.values()].filter(({ wire }) =>
(this.#world.get_state(wire.x, wire.y, wire.z, "power") ?? 0) !== net.power
),
);
}
}
// indexes the net of the wire at start, walked from there, and adds it to found. a cell another net has takes that
// net along: one found by this settle is taken whole, and one from before is forgotten and its other cells walked
// again. a walk only reaches another net while two cells' arms don't agree yet, between one changing and the other
#walk(start: number, found: Set<Net>) {
const net: Net = { cells: new Map(), inputs: new Map(), power: 0 };
const seen = new Set([start]);
const queue = [start];
while (queue.length > 0) {
const key = queue.pop()!;
const other = this.#nets.get(key);
if (other === net) continue;
if (other && found.has(other)) {
this.#absorb(net, other);
found.delete(other);
continue;
}
if (other) this.#forget(other);
const cell = this.#read(...unpack(key));
if (!cell) continue;
net.cells.set(key, cell);
this.#nets.set(key, net);
const input = this.#input(cell);
if (input > 0) net.inputs.set(key, input);
for (const joined of cell.joins) {
if (seen.has(joined)) continue;
seen.add(joined);
queue.push(joined);
}
}
if (net.cells.size === 0) return;
net.power = most(net.inputs);
found.add(net);
}
// the cell at x y z as it is now, undefined when there's no wire
#read(x: number, y: number, z: number): Cell | undefined {
const wire = wire_at(this.#world, x, y, z);
if (!wire) return undefined;
const joins: number[] = [];
const blocks: Face[] = [];
for (const [face, direction] of wire.out) {
const way = joins_out(this.#world, x, y, z, face, direction);
if (way === "block") blocks.push(direction);
const next = cell_through(way, x, y, z, face, direction);
if (next) joins.push(pack(...next));
}
return { wire, joins, blocks };
}
// the power going into a cell from outside its net: from each block it lies on, a source's output on that side or a
// solid block's power, and from the blocks that aren't wire it joins
#input({ wire, blocks }: Cell) {
const world = this.#world;
let power = 0;
for (const { face, at } of below(wire)) {
const id = world.get_block(...at);
if (id !== undefined) {
power = Math.max(power, output(world, ...at, id, OPPOSITE[face]), solid_power(world, ...at, id));
}
if (power === MAX_POWER) return power;
}
for (const direction of blocks) {
const at = step(wire.x, wire.y, wire.z, direction);
const id = world.get_block(...at);
if (id !== undefined) power = Math.max(power, output(world, ...at, id, OPPOSITE[direction]));
if (power === MAX_POWER) return power;
}
return power;
}
// sets each of cells to its net's power, then tells each block that gives power to once: the blocks they join, the
// blocks they lie on and the blocks around those, which are powered through them
#set_power(net: Net, cells: readonly Cell[]) {
this.#setting = true;
try {
for (const { wire } of cells) this.#world.set_power(wire.x, wire.y, wire.z, net.power);
} finally {
this.#setting = false;
}
const told = new Set<number>();
const tell = (at: [number, number, number], from: { x: number; y: number; z: number }) => {
const key = pack(...at);
if (told.has(key) || this.#nets.get(key) === net) return;
told.add(key);
this.#world.neighbor_changed(...at, from);
};
for (const { wire, blocks } of cells) {
for (const direction of blocks) tell(step(wire.x, wire.y, wire.z, direction), wire);
for (const { at } of below(wire)) {
tell(at, wire);
for (const face of FACES) tell(step(...at, face), { x: at[0], y: at[1], z: at[2] });
}
}
}
}
Binary file not shown.

Before

Width:  |  Height:  |  Size: 97 B

After

Width:  |  Height:  |  Size: 192 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 97 B

After

Width:  |  Height:  |  Size: 174 B