Verdigris working !
This commit is contained in:
+6
-3
@@ -105,9 +105,12 @@ export function get_state_value(value: number, block_info: BlockRegistry, name:
|
||||
|
||||
// every state of a block value by name, like { age: 7 }
|
||||
export function block_states(value: number, block_info: BlockRegistry): Record<string, number> {
|
||||
return Object.fromEntries(
|
||||
(block_info.states ?? []).map((s) => [s.name, get_state_value(value, block_info, s.name)!]),
|
||||
);
|
||||
if (!block_info.states) return {};
|
||||
if (!block_info.compiled_states) compile_block_states(block_info);
|
||||
const state_bits = value >>> STATE_SHIFT;
|
||||
const states: Record<string, number> = {};
|
||||
for (const s of block_info.compiled_states!) states[s.name] = (state_bits & s.mask) >>> s.shift;
|
||||
return states;
|
||||
}
|
||||
|
||||
export function set_state_value(value: number, block_info: BlockRegistry, name: string, new_value: number) {
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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 |
@@ -3,17 +3,18 @@
|
||||
|
||||
// verdigris wire, see REDSTONE.md: how it's drawn on the faces of its cell, how it joins, and its nets' power
|
||||
import { assert, assertAlmostEquals, assertEquals } from "@std/assert";
|
||||
import { Alea } from "@paulaboks/rng";
|
||||
import { bake_block, type BakedQuad, FACE_INDEX, type ModelJson } from "$/common/block_models.ts";
|
||||
import { block_shape } from "$/common/block_shapes.ts";
|
||||
import { block_shape, is_solid } from "$/common/block_shapes.ts";
|
||||
import { type BlockRegistry, EverythingRegistry } from "$/common/everything_registry.ts";
|
||||
import type { Face } from "$/common/mod_api/shared.ts";
|
||||
import { AIR_ID } from "$/common/protocol.ts";
|
||||
import { block_states, default_block_value, set_state_value } from "$/common/utils.ts";
|
||||
import type { WorldSave } from "$/server/game/saves.ts";
|
||||
import type { SavedChunk, WorldSave } from "$/server/game/saves.ts";
|
||||
import { can_cover, joined_cell, ways_out } from "$/mods/bworld/scripts/verdigris/joins.ts";
|
||||
import type { BlockReader } from "$/mods/bworld/scripts/verdigris/signals.ts";
|
||||
import { DIRECTIONS, FACE_OFFSETS, FACES, OPPOSITE } from "$/mods/bworld/scripts/verdigris/sides.ts";
|
||||
import { wire_at } from "$/mods/bworld/scripts/verdigris/wires.ts";
|
||||
import { DIRECTIONS, FACE_OFFSETS, FACES, OPPOSITE, step } from "$/mods/bworld/scripts/verdigris/sides.ts";
|
||||
import { NetIndex, wire_at } from "$/mods/bworld/scripts/verdigris/wires.ts";
|
||||
import { type Area, client_of, server_blocks, test_game } from "./helpers.ts";
|
||||
|
||||
// how far it stands off the face it lies on, in blocks
|
||||
@@ -216,7 +217,7 @@ async function builder(save?: WorldSave) {
|
||||
const cell = joined_cell(reader, ...at, face, direction);
|
||||
return cell ? [cell.join(" ")] : [];
|
||||
});
|
||||
return { t, use, states, id, power, covers, joins };
|
||||
return { t, use, states, id, power, covers, joins, reader };
|
||||
}
|
||||
|
||||
type Builder = Awaited<ReturnType<typeof builder>>;
|
||||
@@ -285,6 +286,30 @@ Deno.test("a wire goes over the edge of a block to the wire on its next face, wh
|
||||
assertEquals([b.joins(3, 101, 1), b.joins(3, 100, 0)], [[], ["3 100 -1"]]);
|
||||
});
|
||||
|
||||
Deno.test("a lever put where a wire went around an edge joins both ends instead, and taken away they join again", async () => {
|
||||
const b = await builder();
|
||||
// on top of a block, over its north edge and down its north face, then along the floor to a lever that's on
|
||||
b.t.game.set_block(3, 100, 1, "bworld:stone");
|
||||
b.use("bworld:verdigris", 3, 100, 1, "top");
|
||||
b.use("bworld:verdigris", 3, 100, 1, "north");
|
||||
b.use("bworld:verdigris", 3, 99, 0, "top");
|
||||
b.use("bworld:verdigris", 3, 99, -1, "top");
|
||||
b.use("bworld:lever", 3, 99, -2, "top");
|
||||
b.use(undefined, 3, 100, -2, "top");
|
||||
const wires: Pos[] = [[3, 101, 1], [3, 100, 0], [3, 100, -1]];
|
||||
assertEquals(wires.map((at) => b.power(...at)), [15, 15, 15]);
|
||||
// hanging from a ceiling in the cell the edge went through. the wires' arms don't change, they join the lever
|
||||
b.t.game.set_block(3, 102, 0, "bworld:stone");
|
||||
b.use("bworld:lever", 3, 102, 0, "bottom");
|
||||
assertEquals(wires.map((at) => b.power(...at)), [0, 15, 15]);
|
||||
b.use(undefined, 3, 101, 0, "top");
|
||||
assertEquals(b.power(3, 101, 1), 15);
|
||||
b.t.game.destroy_block(3, 101, 0);
|
||||
assertEquals(wires.map((at) => b.power(...at)), [15, 15, 15]);
|
||||
b.use(undefined, 3, 100, -2, "top");
|
||||
assertEquals(wires.map((at) => b.power(...at)), [0, 0, 0]);
|
||||
});
|
||||
|
||||
Deno.test("a player sees a wire join the moment they place it, the way the server joins it", async () => {
|
||||
const b = await builder();
|
||||
b.t.game.set_block(0, 100, 0, "bworld:stone");
|
||||
@@ -398,6 +423,64 @@ Deno.test("a world saved and loaded has the same nets", async () => {
|
||||
assertEquals(again.wires.map((at) => again.power(...at)), [0, 0, 0, 0, 0]);
|
||||
});
|
||||
|
||||
Deno.test("whatever is built, broken and switched, each wire has the power walking its net again gives it", async () => {
|
||||
let b = await builder();
|
||||
const rng = new Alea("verdigris nets");
|
||||
const pick = <T>(list: readonly T[]) => list[Math.floor(rng.next() * list.length)];
|
||||
const cell = (): Pos => [pick([-2, -1, 0, 1, 2]), pick([100, 101, 102]), pick([-2, -1, 0, 1, 2])];
|
||||
const cells: Pos[] = [];
|
||||
for (let x = -2; x <= 2; x++) for (let y = 100; y <= 102; y++) for (let z = -2; z <= 2; z++) cells.push([x, y, z]);
|
||||
const solid = (id: string) => id !== AIR_ID && is_solid(EverythingRegistry.get<BlockRegistry>("blocks", id)!);
|
||||
// placed in an empty cell or one with wire, against a solid block beside it
|
||||
const place = (id: string) => {
|
||||
const spots = cells.flatMap((at) =>
|
||||
[AIR_ID, WIRE].includes(b.id(...at))
|
||||
? FACES.filter((face) => solid(b.id(...step(...at, face)))).map((face) => [at, face] as const)
|
||||
: []
|
||||
);
|
||||
if (spots.length === 0) return;
|
||||
const [at, face] = pick(spots);
|
||||
b.use(id, ...step(...at, face), OPPOSITE[face]);
|
||||
};
|
||||
const switch_lever = () => {
|
||||
const levers = cells.filter((at) => b.id(...at) === "bworld:lever");
|
||||
if (levers.length > 0) b.use(undefined, ...pick(levers), "top");
|
||||
};
|
||||
const actions = [
|
||||
...Array(4).fill(() => place("bworld:verdigris")),
|
||||
() => place("bworld:lever"),
|
||||
switch_lever,
|
||||
switch_lever,
|
||||
() => b.t.game.set_block(...cell(), "bworld:stone"),
|
||||
() => b.t.game.set_block(...cell(), "bworld:stone"),
|
||||
() => b.t.game.set_block(...cell(), "bworld:verdigris_block"),
|
||||
() => b.t.game.set_block(...cell(), AIR_ID),
|
||||
() => b.t.game.destroy_block(...cell()),
|
||||
];
|
||||
// every chunk saved so far: a save only has the chunks that changed since the world was loaded
|
||||
const chunks = new Map<string, SavedChunk>();
|
||||
for (let step = 0; step < 400; step++) {
|
||||
// saved and loaded now and then, so some nets are only walked when something touches them
|
||||
if (step % 40 === 39) {
|
||||
const saved = b.t.game.save();
|
||||
for (const chunk of saved.chunks) chunks.set(`${chunk.x} ${chunk.z}`, chunk);
|
||||
b = await builder({ level: saved.level, chunks: [...chunks.values()] });
|
||||
}
|
||||
pick(actions)();
|
||||
// a new index walks each net from scratch, and says which wires don't have its power
|
||||
const wrong: string[] = [];
|
||||
const walked = new NetIndex({
|
||||
...b.reader,
|
||||
solid,
|
||||
set_power: (x, y, z, power) => wrong.push(`${x} ${y} ${z} has ${b.power(x, y, z)}, not ${power}`),
|
||||
neighbor_changed: () => {},
|
||||
});
|
||||
for (const [x, y, z] of cells) if (b.id(x, y, z) === WIRE) walked.beside_changed(x, y, z, { x, y, z });
|
||||
assertEquals(wrong, [], `after step ${step}`);
|
||||
}
|
||||
assert(cells.some((at) => b.id(...at) === WIRE && b.power(...at) > 0), "some wire was powered");
|
||||
});
|
||||
|
||||
Deno.test("wire comes off a face whose block goes, and drops one verdigris for each face it covered", async () => {
|
||||
const b = await builder();
|
||||
const verdigris = () =>
|
||||
|
||||
@@ -0,0 +1,140 @@
|
||||
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
// how long verdigris wire takes on a stone floor, see "Performance" in REDSTONE.md, and how many set_block messages a
|
||||
// player watching would get: placing a 1000 wire zigzag one wire at a time, a lever switching it, a block placed and
|
||||
// taken away beside it while it's powered, a wire broken out of its middle and put back, and a lever switching a 64×64
|
||||
// grid. with the net index a wire took 0.5 ms to place however big its net, a block beside the powered zigzag 0.1 ms
|
||||
// and no wire messages, and switching took 15 to 20 ms for the zigzag and 50 to 60 ms for the grid, about 15 µs a
|
||||
// wire. walking the net on every change, a wire took 30 ms to place in the zigzag and 400 ms in the grid, a block
|
||||
// beside 20 ms, and switching 35 to 45 ms and 135 ms. repeater clocks and torch towers come with repeaters and torches
|
||||
// deno run -A tools/verdigris_bench.ts
|
||||
import type { Face } from "../common/mod_api/shared.ts";
|
||||
import { AIR_ID } from "../common/protocol.ts";
|
||||
import { block_states } from "../common/utils.ts";
|
||||
import { stone_floor, test_game } from "../tests/helpers.ts";
|
||||
|
||||
type Pos = [number, number, number];
|
||||
|
||||
// alice on a stone floor at y 99 with nothing above it, placing what she holds on blocks from right by them
|
||||
async function floor() {
|
||||
const t = await test_game("mods");
|
||||
stone_floor(t.game, 34);
|
||||
for (let x = -34; x <= 34; x++) {
|
||||
for (let z = -34; z <= 34; z++) {
|
||||
for (let y = 100; y < 103; y++) {
|
||||
if (t.game.world.get_block_id(x, y, z) !== AIR_ID) t.game.set_block(x, y, z, AIR_ID);
|
||||
}
|
||||
}
|
||||
}
|
||||
// every block that changes, as if a player saw them all
|
||||
let changes: string[] = [];
|
||||
const on_block_change = t.game.world.on_block_change;
|
||||
t.game.world.on_block_change = (x, y, z, id, state) => {
|
||||
changes.push(id);
|
||||
on_block_change?.(x, y, z, id, state);
|
||||
};
|
||||
t.join(1, "alice");
|
||||
t.send(1, { type: "move", x: 0.5, y: 100, z: 0.5, yaw: 0, pitch: 0 });
|
||||
const player = t.game.players()[0];
|
||||
t.game.tick();
|
||||
t.take(1);
|
||||
const hold = (id: string) => {
|
||||
t.send(1, { type: "chat", text: `/give ${id} 64` });
|
||||
t.send(1, {
|
||||
type: "select_slot",
|
||||
slot: player.inventory.to_synced_data().findIndex((item) => item?.id === id),
|
||||
});
|
||||
};
|
||||
const use = (x: number, y: number, z: number, face: Face) => {
|
||||
t.game.teleport(player, x + 0.5, y + 1, z + 0.5);
|
||||
t.send(1, { type: "use_block", x, y, z, face, hit: [0.5, 0.5, 0.5] });
|
||||
};
|
||||
// wire on the floor under each of cells, in order, and how long that took
|
||||
const lay = (cells: Pos[]) => {
|
||||
let ms = 0;
|
||||
cells.forEach(([x, , z], i) => {
|
||||
if (i % 64 === 0) hold("bworld:verdigris");
|
||||
const start = performance.now();
|
||||
use(x, 99, z, "top");
|
||||
ms += performance.now() - start;
|
||||
});
|
||||
t.take(1);
|
||||
return ms;
|
||||
};
|
||||
// a lever on the floor at x z
|
||||
const lever = (x: number, z: number) => {
|
||||
hold("bworld:lever");
|
||||
use(x, 99, z, "top");
|
||||
t.take(1);
|
||||
return [x, 100, z] as Pos;
|
||||
};
|
||||
// runs change count times, and how long each took and the set_block messages each sent, all and wire's
|
||||
const measure = (count: number, change: (i: number) => void) => {
|
||||
let ms = 0;
|
||||
let sent = 0;
|
||||
let wires = 0;
|
||||
for (let i = 0; i < count; i++) {
|
||||
const start = performance.now();
|
||||
changes = [];
|
||||
change(i);
|
||||
ms += performance.now() - start;
|
||||
sent += changes.length;
|
||||
wires += changes.filter((id) => id === "bworld:verdigris_wire").length;
|
||||
}
|
||||
return `${(ms / count).toFixed(2)} ms, ${sent / count} set_block (${wires / count} wire)`;
|
||||
};
|
||||
const power = (x: number, y: number, z: number) =>
|
||||
block_states(t.game.world.get_block_value(x, y, z), t.game.world.get_block_info(x, y, z)!).power;
|
||||
return { t, use, lay, lever, measure, power };
|
||||
}
|
||||
|
||||
// 1000 cells in rows of 40 along x, every other z, each row joined to the next at alternating ends
|
||||
function zigzag(): Pos[] {
|
||||
const cells: Pos[] = [];
|
||||
for (let row = 0; cells.length < 1000; row++) {
|
||||
const z = -25 + row * 2;
|
||||
for (let i = 0; i < 40 && cells.length < 1000; i++) cells.push([row % 2 ? 19 - i : -20 + i, 100, z]);
|
||||
if (cells.length < 1000) cells.push([row % 2 ? -20 : 19, 100, z + 1]);
|
||||
}
|
||||
return cells;
|
||||
}
|
||||
|
||||
{
|
||||
const f = await floor();
|
||||
const cells = zigzag();
|
||||
const lever = f.lever(-21, -25);
|
||||
const placed = f.lay(cells);
|
||||
console.log(
|
||||
`placing ${cells.length} wires: ${(placed / cells.length).toFixed(2)} ms a wire, ${placed.toFixed(0)} ms`,
|
||||
);
|
||||
console.log(` lever switching them: ${f.measure(20, () => f.use(...lever, "top"))}`);
|
||||
f.use(...lever, "top");
|
||||
f.t.take(1);
|
||||
if (f.power(...cells.at(-1)!) !== 15) throw new Error("the zigzag isn't powered");
|
||||
// beside a wire on two rows
|
||||
console.log(
|
||||
` a block beside them placed and taken away: ${
|
||||
f.measure(20, (i) => f.t.game.set_block(0, 100, -24, i % 2 ? AIR_ID : "bworld:stone"))
|
||||
}`,
|
||||
);
|
||||
const middle = cells[cells.length / 2];
|
||||
console.log(
|
||||
` a wire in the middle broken and put back: ${
|
||||
f.measure(20, (i) => i % 2 ? f.use(middle[0], 99, middle[2], "top") : f.t.game.destroy_block(...middle))
|
||||
}`,
|
||||
);
|
||||
}
|
||||
|
||||
{
|
||||
const f = await floor();
|
||||
const cells: Pos[] = [];
|
||||
for (let z = -32; z < 32; z++) for (let x = -32; x < 32; x++) cells.push([x, 100, z]);
|
||||
const lever = f.lever(-33, -32);
|
||||
const placed = f.lay(cells);
|
||||
console.log(
|
||||
`placing a 64×64 grid: ${(placed / cells.length).toFixed(2)} ms a wire, ${(placed / 1000).toFixed(1)} s`,
|
||||
);
|
||||
console.log(` lever switching it: ${f.measure(10, () => f.use(...lever, "top"))}`);
|
||||
}
|
||||
Deno.exit(0);
|
||||
Reference in New Issue
Block a user