Files
bworld/mods/bworld/scripts/verdigris/server.ts
T
2026-10-22 13:43:57 -03:00

122 lines
5.6 KiB
TypeScript

// 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
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";
// 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);
ctx.components.register_block("bworld:verdigris_wire", {
on_create(block) {
tell_around_edges(world, block);
update_net(world, 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);
},
on_neighbor_change(block, _params, neighbor) {
if (lose_face(ctx, world, block, neighbor)) return;
update_net(world, block.x, block.y, block.z);
},
// it covered another face, or its rule joined or let go of something, which can join two nets or split one
on_state_change(block, _params, before) {
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);
},
});
// the engine tells the blocks beside a source when it changes. these tell the blocks around the solid blocks its
// strong sides power, which nothing else would. on_state_change tells them for the sides it had and has, in case it
// turned
ctx.components.register_block("bworld:verdigris_source", {
on_create: (block) => tell_through(world, block, strong_sides(world, block.x, block.y, block.z, block.id)),
on_break: (block) => tell_through(world, block, strong_sides(world, block.x, block.y, block.z, block.id)),
on_state_change(block, _params, before) {
const was: BlockReader = {
...world,
get_state: (x, y, z, name) =>
x === block.x && y === block.y && z === block.z ? before[name] : world.get_state(x, y, z, name),
};
const sides = new Set([
...strong_sides(was, block.x, block.y, block.z, block.id),
...strong_sides(world, block.x, block.y, block.z, block.id),
]);
tell_through(world, block, [...sides]);
},
});
register_lever(ctx);
}
// the wires on the next faces of the blocks a wire lies on, which it joins or could join around their edges. they aren't
// among the six blocks beside it the engine tells
function tell_around_edges(world: SignalWorld, block: BlockRef) {
const faces = faces_of(world, block.x, block.y, block.z);
for (const face of faces) {
for (const direction of DIRECTIONS[face]) {
if (faces.includes(direction)) continue;
const [x, y, z] = around_edge(block.x, block.y, block.z, face, direction);
if (covers(world, x, y, z, OPPOSITE[direction])) world.neighbor_changed(x, y, z, block);
}
}
}
// the face of a wire that lay on the block that changed, when that block's face isn't something to lie on anymore: the
// wire comes off it and drops it, or breaks when what's left can't be one cell. whether it did
function lose_face(ctx: ServerContext, world: SignalWorld, block: BlockRef, neighbor: Position) {
const faces = faces_of(world, block.x, block.y, block.z);
const face = faces.find((face) => {
const [x, y, z] = step(block.x, block.y, block.z, face);
return x === neighbor.x && y === neighbor.y && z === neighbor.z;
});
if (!face || ctx.world.is_face_sturdy(neighbor.x, neighbor.y, neighbor.z, OPPOSITE[face])) return false;
const left = faces.filter((other) => other !== face);
if (!can_cover(left)) {
ctx.world.break_block(block.x, block.y, block.z);
return true;
}
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);
return true;
}
// the blocks around each solid block toward those faces of the block, powered through it
function tell_through(world: SignalWorld, block: BlockRef, faces: readonly Face[]) {
for (const face of faces) {
const [sx, sy, sz] = step(block.x, block.y, block.z, face);
const id = world.get_block(sx, sy, sz);
if (id === undefined || !world.solid(id)) continue;
for (const around of FACES) {
if (around === OPPOSITE[face]) continue;
world.neighbor_changed(...step(sx, sy, sz, around), { x: sx, y: sy, z: sz });
}
}
}
function signal_world(ctx: ServerContext): SignalWorld {
return {
// a chunk that was never generated holds nothing anyone built, and reading it would generate it
get_block: (x, y, z) => ctx.world.is_loaded(x, z) ? ctx.world.get_block(x, y, z) : undefined,
get_state: (x, y, z, name) => ctx.world.get_state(x, y, z, name),
source: source_lookup(ctx.blocks),
solid: (id) => ctx.blocks.solid(id),
set_power: (x, y, z, power) => ctx.world.set_state(x, y, z, "power", power, { update_neighbors: false }),
neighbor_changed: (x, y, z, from) => ctx.world.neighbor_changed(x, y, z, from),
};
}