// SPDX-FileCopyrightText: 2026 Paula Wisnievski // SPDX-License-Identifier: AGPL-3.0-or-later // verdigris wire: 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, 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 { default_block_value, set_state_value } from "@/common/utils.ts"; import type { SavedChunk, WorldSave } from "@/server/game/saves.ts"; import { can_cover, ways_out } from "@/mods/bworld/scripts/verdigris/joins.ts"; import { DIRECTIONS, FACE_OFFSETS, FACES, OPPOSITE, step } from "@/mods/bworld/scripts/verdigris/sides.ts"; import { NetIndex } from "@/mods/bworld/scripts/verdigris/wires.ts"; import { client_of, server_blocks, test_game } from "./helpers.ts"; import { AREA, type Builder, builder, type Pos } from "./verdigris.ts"; // how far it stands off the face it lies on, in blocks const HEIGHT = 0.25 / 16; const WIRE = "bworld:verdigris_wire"; // every set of faces one wire cell can cover function coverable(): Face[][] { const sets: Face[][] = []; for (let set = 1; set < 1 << FACES.length; set++) { const faces = FACES.filter((_, i) => set & (1 << i)); if (can_cover(faces)) sets.push(faces); } return sets; } const covering = (faces: Face[]) => Object.fromEntries(faces.map((face) => [face, 1])); async function wire() { await test_game("mods"); const block = EverythingRegistry.get("blocks", WIRE)!; const models = Object.fromEntries(EverythingRegistry.entries("models")); const value = (states: Record) => { let value = default_block_value(0, block); for (const [name, state] of Object.entries(states)) value = set_state_value(value, block, name, state)!; return value; }; return { block, quads: (states: Record) => bake_block(block, value(states), models), }; } function corners(quad: BakedQuad) { return [0, 1, 2, 3].map((k) => quad.positions.slice(k * 3, k * 3 + 3)); } // how far toward face a point in the block is: 0 on the far side of the block, 1 on that face function toward(point: number[], face: Face) { const offset = FACE_OFFSETS[face]; return 0.5 + (point[0] - 0.5) * offset[0] + (point[1] - 0.5) * offset[1] + (point[2] - 0.5) * offset[2]; } function range(quad: BakedQuad, face: Face) { const reach = corners(quad).map((point) => toward(point, face)); return [Math.min(...reach), Math.max(...reach)]; } // lying on face of the cell: flat at the wire's height off it, facing away from it function lies_on(quad: BakedQuad, face: Face) { const [low, high] = range(quad, face); return quad.face === FACE_INDEX[OPPOSITE[face]] && Math.abs(low - (1 - HEIGHT)) < 1e-6 && Math.abs(high - (1 - HEIGHT)) < 1e-6; } // the quad lying on face from the cell's middle toward direction, to its edge or a little past. undefined if none function arm(quads: BakedQuad[], face: Face, direction: Face) { const found = quads.filter((quad) => lies_on(quad, face) && range(quad, direction)[0] > 0.5); assert(found.length <= 1); if (found[0]) { for (const across of DIRECTIONS[face].filter((d) => d !== direction && d !== OPPOSITE[direction])) { const [low, high] = range(found[0], across); assert(low > 0.4 && high < 0.6, `on ${face} toward ${direction}: no wider than the wire`); } } return found[0]; } // the part of its texture a point on a face facing that way would show on a whole block, like a face's default uv function block_uv(face: number, [x, y, z]: number[]): [number, number] { const uvs: [number, number][] = [ [x, z], // top [x, 1 - z], // bottom [x, 1 - y], // south [1 - x, 1 - y], // north [z, 1 - y], // west [1 - z, 1 - y], // east ]; return [uvs[face][0] * 16, uvs[face][1] * 16]; } Deno.test("a wire draws its middle on each face it covers, and its faces meet where they meet in the cell", async () => { const { quads } = await wire(); for (const face of FACES) { const alone = quads({ [face]: 1 }); assertEquals(alone.length, 1, `on ${face} it's only its middle`); assert(lies_on(alone[0], face), face); for (const direction of DIRECTIONS[face]) { // each runs to the edge between them, and no further const both = quads({ [face]: 1, [direction]: 1 }); assertEquals(both.length, 4, `${face} and ${direction}`); assertAlmostEquals(range(arm(both, face, direction)!, direction)[1], 1, 1e-6); assertAlmostEquals(range(arm(both, direction, face)!, face)[1], 1, 1e-6); } } }); Deno.test("each arm_ state draws the way out of the cell its faces give it, a little past the edge", async () => { const { quads } = await wire(); for (const faces of coverable()) { const without = quads(covering(faces)); ways_out(faces).forEach(([face, direction], i) => { const where = `${faces} arm_${i}`; const with_arm = quads({ ...covering(faces), [`arm_${i}`]: 1 }); assertEquals(with_arm.length, without.length + 1, where); assertEquals(arm(without, face, direction), undefined, where); assertAlmostEquals(range(arm(with_arm, face, direction)!, direction)[1], 1 + HEIGHT, 1e-6, where); }); } }); Deno.test("the wire's texture lies the way it would on a whole block's faces, so it lines up from wire to wire", async () => { const { quads } = await wire(); for (const faces of coverable()) { const arms = Object.fromEntries(ways_out(faces).map((_, i) => [`arm_${i}`, 1])); for (const quad of quads({ ...covering(faces), ...arms })) { corners(quad).forEach((point, k) => { const [u, v] = block_uv(quad.face, point); assertAlmostEquals(quad.uvs[k * 2], u, 1e-6, `${faces}, corner ${point}`); assertAlmostEquals(quad.uvs[k * 2 + 1], v, 1e-6, `${faces}, corner ${point}`); }); } } }); Deno.test("a cell covers faces that join each other, as many as its arm_ states fit", () => { assert(can_cover(["bottom"])); assert(can_cover(["bottom", "north"])); assert(can_cover(["bottom", "north", "east"])); assert(can_cover(FACES)); assert(!can_cover([])); // opposite faces have nothing joining them, and around the cell there are too many ways out assert(!can_cover(["bottom", "top"])); assert(!can_cover(["bottom", "north", "top"])); assert(!can_cover(["north", "east", "south", "west"])); }); Deno.test("a wire shows its powered texture while it has power, and can be pointed at", async () => { const { block, quads } = await wire(); const lit = (power: number) => new Set(quads({ bottom: 1, north: 1, power }).map((quad) => quad.texture)); assertEquals(lit(0), new Set(["bworld:verdigris_wire"])); assertEquals(lit(7), new Set(["bworld:verdigris_wire_on"])); // a plate around its middle on each face it covers for (const face of FACES) { const states = Object.fromEntries(block.states!.map((state) => [state.name, state.name === face ? 1 : 0])); const shape = block_shape(block, states); assertEquals(shape.length, 1, face); const box = shape[0]; const extent = (to: Face) => { const reach = [toward(box.slice(0, 3), to), toward(box.slice(3), to)]; return [Math.min(...reach), Math.max(...reach)]; }; assertEquals(extent(face).map((v) => Math.round(v * 16)), [15, 16], face); for (const across of DIRECTIONS[face]) assertEquals(extent(across).map((v) => Math.round(v * 16)), [3, 13]); } }); // every wire cell these join joins them back function joined_both_ways(b: Builder, wires: Pos[]) { for (const at of wires) { assertEquals(b.id(...at), WIRE, `${at}`); for (const other of b.joins(...at)) { const [x, y, z] = other.split(" ").map(Number); assert(b.joins(x, y, z).includes(at.join(" ")), `${other} doesn't join ${at} back`); } } } // the wire from a floor up a wall onto a ceiling: one on the floor at z 2, then the cell at z 1 on the floor and the // south face of a column at 0 100 0, one more up the column, the cell under the ceiling there on the column and the // ceiling, and one on the ceiling above z 2. a lever on the floor joins the first async function floor_wall_ceiling() { const b = await builder(); for (const [x, y, z] of [[0, 100, 0], [0, 101, 0], [0, 102, 0], [0, 103, 1], [0, 103, 2]]) { b.t.game.set_block(x, y, z, "bworld:stone"); } b.use("bworld:verdigris", 0, 99, 2, "top"); b.use("bworld:verdigris", 0, 99, 1, "top"); b.use("bworld:verdigris", 0, 100, 0, "south"); b.use("bworld:verdigris", 0, 101, 0, "south"); b.use("bworld:verdigris", 0, 102, 0, "south"); b.use("bworld:verdigris", 0, 103, 1, "bottom"); b.use("bworld:verdigris", 0, 103, 2, "bottom"); b.use("bworld:lever", 0, 99, 3, "top"); const wires: Pos[] = [[0, 100, 2], [0, 100, 1], [0, 101, 1], [0, 102, 1], [0, 102, 2]]; return { ...b, wires, lever: [0, 100, 3] as Pos }; } Deno.test("wire placed on another face of a cell with wire joins it there, up a wall and onto a ceiling", async () => { const b = await floor_wall_ceiling(); assertEquals(b.covers(0, 100, 1), ["north", "bottom"]); assertEquals(b.covers(0, 102, 1), ["north", "top"]); joined_both_ways(b, b.wires); assertEquals(b.joins(0, 100, 1).sort(), ["0 100 2", "0 101 1"]); assertEquals(b.joins(0, 102, 1).sort(), ["0 101 1", "0 102 2"]); // a cell can't cover a floor and a ceiling with nothing joining them b.t.game.set_block(-3, 101, -3, "bworld:stone"); b.use("bworld:verdigris", -3, 99, -3, "top"); b.use("bworld:verdigris", -3, 101, -3, "bottom"); assertEquals(b.covers(-3, 100, -3), ["bottom"]); }); Deno.test("a wire goes over the edge of a block to the wire on its next face, while nothing's in the way", async () => { const b = await builder(); // on top of a block, over its north edge, down its north face, and on along the floor 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"); assertEquals(b.covers(3, 100, 0), ["south", "bottom"]); joined_both_ways(b, [[3, 101, 1], [3, 100, 0], [3, 100, -1]]); assertEquals(b.joins(3, 101, 1), ["3 100 0"]); assertEquals(b.joins(3, 100, 0).sort(), ["3 100 -1", "3 101 1"]); // filling the cell the edge goes around lets go of it at both ends b.t.game.set_block(3, 101, 0, "bworld:stone"); 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"); b.t.game.set_block(0, 101, 0, "bworld:stone"); b.t.game.set_block(3, 100, 1, "bworld:stone"); b.use("bworld:verdigris", 0, 99, 1, "top"); b.use("bworld:verdigris", 0, 101, 0, "south"); b.use("bworld:verdigris", 3, 100, 1, "top"); const client = client_of(b.t.game, AREA); const place = (at: Pos, clicked: Pos, face: Face) => { client.place(WIRE, ...at, 0, face, [0.5, 0.5, 0.5]); b.use("bworld:verdigris", ...clicked, face); }; // beside one on the floor place([0, 100, 2], [0, 99, 2], "top"); assertEquals(client.blocks(), server_blocks(b.t.game, AREA)); // onto the wall of the cell with the floor wire, joining that and the wire up the wall place([0, 100, 1], [0, 100, 0], "south"); assertEquals(b.covers(0, 100, 1), ["north", "bottom"]); assertEquals(b.joins(0, 101, 1), ["0 100 1"]); assertEquals(client.blocks(), server_blocks(b.t.game, AREA)); // over an edge the wire it joins isn't beside it, so the server tells that one, and sends what it became b.t.game.tick(); b.t.take(1); place([3, 100, 0], [3, 100, 1], "north"); const differs = client.blocks().filter((line, i) => line !== server_blocks(b.t.game, AREA)[i]); assertEquals(differs.map((line) => line.split(" ").slice(0, 3).join(" ")), ["3 101 1"]); const sent = b.t.take(1).find((m) => m.type === "set_block" && m.x === 3 && m.y === 101 && m.z === 1); assertEquals(sent?.state, b.t.game.world.get_block_value(3, 101, 1) >>> 16); }); Deno.test("a lever's power goes all the way along a net, and the net goes off with it", async () => { const b = await floor_wall_ceiling(); assertEquals(b.wires.map((at) => b.power(...at)), [0, 0, 0, 0, 0]); b.use(undefined, ...b.lever, "top"); assertEquals(b.states(...b.lever).powered, 1); assertEquals(b.wires.map((at) => b.power(...at)), [15, 15, 15, 15, 15]); // the stone they lie on was powered by them, but that doesn't hold them on b.use(undefined, ...b.lever, "top"); assertEquals(b.wires.map((at) => b.power(...at)), [0, 0, 0, 0, 0]); }); Deno.test("a wire takes power from what it lies on, and two nets on one block don't power each other", async () => { const b = await builder(); // a lever on the side of a block powers it, and the wires on its top and underneath through it b.t.game.set_block(-3, 101, -3, "bworld:stone"); b.use("bworld:verdigris", -3, 101, -3, "top"); b.use("bworld:verdigris", -3, 101, -3, "bottom"); b.use("bworld:lever", -3, 101, -3, "east"); b.use(undefined, -2, 101, -3, "top"); assertEquals([b.power(-3, 102, -3), b.power(-3, 100, -3)], [15, 15]); // a wire on a verdigris block, and one joined to one b.t.game.set_block(3, 101, 3, "bworld:verdigris_block"); b.use("bworld:verdigris", 3, 101, 3, "top"); assertEquals(b.power(3, 102, 3), 15); b.t.game.set_block(3, 101, -4, "bworld:stone"); b.t.game.set_block(4, 102, -4, "bworld:verdigris_block"); b.use("bworld:verdigris", 3, 101, -4, "top"); b.use("bworld:verdigris", 3, 101, -4, "bottom"); assertEquals(b.power(3, 102, -4), 15); // the wire under that block isn't powered by the net on top of it, which only wire powers it with assertEquals(b.power(3, 100, -4), 0); b.t.game.set_block(4, 102, -4, AIR_ID); assertEquals(b.power(3, 102, -4), 0); }); // five wires in a row on the floor at z 5, from x -2 to 2, and a lever at the west end, switched on async function powered_row(save?: WorldSave) { const b = await builder(save); const wires: Pos[] = [-2, -1, 0, 1, 2].map((x) => [x, 100, 5]); if (!save) { for (const [x] of wires) b.use("bworld:verdigris", x, 99, 5, "top"); b.use("bworld:lever", -3, 99, 5, "top"); b.use(undefined, -3, 100, 5, "top"); } return { ...b, wires, lever: [-3, 100, 5] as Pos }; } Deno.test("breaking a wire in the middle of a net leaves the half cut off from the lever without power", async () => { const b = await powered_row(); assertEquals(b.wires.map((at) => b.power(...at)), [15, 15, 15, 15, 15]); b.t.game.set_block(0, 100, 5, AIR_ID); assertEquals([-2, -1, 1, 2].map((x) => b.power(x, 100, 5)), [15, 15, 0, 0]); }); Deno.test("each wire is sent once when its net's power changes, and not at all when a block beside it is placed", async () => { const b = await powered_row(); const sent_wires = () => b.t.take(1).filter((m) => m.type === "set_block" && m.id === WIRE).map((m) => `${m.x} ${m.z}`); b.t.game.tick(); b.t.take(1); b.use(undefined, ...b.lever, "top"); assertEquals(sent_wires().sort(), b.wires.map(([x, , z]) => `${x} ${z}`).sort()); b.use(undefined, ...b.lever, "top"); b.t.take(1); b.t.game.set_block(0, 100, 6, "bworld:stone"); b.t.game.set_block(1, 101, 5, "bworld:stone"); assertEquals(sent_wires(), []); assertEquals(b.wires.map((at) => b.power(...at)), [15, 15, 15, 15, 15]); }); Deno.test("a world saved and loaded has the same nets", async () => { const b = await powered_row(); const saved = b.t.game.save(); const again = await powered_row(saved); assertEquals(again.wires.map((at) => again.power(...at)), [15, 15, 15, 15, 15]); again.use(undefined, ...again.lever, "top"); 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 = (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("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(); 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 = () => b.t.game.item_entities().filter((e) => e.item.type_id === "bworld:verdigris").reduce( (n, e) => n + e.item.amount, 0, ); // on a floor and the wall beside it in one cell b.t.game.set_block(5, 100, 4, "bworld:stone"); b.use("bworld:verdigris", 5, 99, 5, "top"); b.use("bworld:verdigris", 5, 100, 4, "south"); assertEquals(b.covers(5, 100, 5), ["north", "bottom"]); b.t.game.set_block(5, 100, 4, AIR_ID); assertEquals(b.covers(5, 100, 5), ["bottom"]); assertEquals(verdigris(), 1); b.t.game.set_block(5, 99, 5, AIR_ID); assertEquals(b.id(5, 100, 5), AIR_ID); assertEquals(verdigris(), 2); // broken by hand, a cell drops what it covered b.t.game.set_block(-5, 100, 4, "bworld:stone"); b.use("bworld:verdigris", -5, 99, 5, "top"); b.use("bworld:verdigris", -5, 100, 4, "south"); b.t.game.destroy_block(-5, 100, 5); assertEquals(verdigris(), 4); }); Deno.test("a wire or lever only goes on something to attach to, and a lever breaks when it goes", async () => { const b = await builder(); // on a wall it points out of the wall, on a floor or ceiling it lies or hangs there b.t.game.set_block(5, 101, -5, "bworld:stone"); b.use("bworld:lever", 5, 101, -5, "south"); assertEquals([b.states(5, 101, -4).face, b.states(5, 101, -4).facing], [1, FACES.indexOf("south")]); b.use("bworld:lever", 5, 101, -5, "bottom"); assertEquals(b.states(5, 100, -5).face, 2); b.use("bworld:lever", 4, 99, -5, "top"); assertEquals(b.states(4, 100, -5).face, 0); b.t.game.set_block(5, 101, -5, AIR_ID); assertEquals([b.id(5, 101, -4), b.id(5, 100, -5)], [AIR_ID, AIR_ID]); assertEquals(b.id(4, 100, -5), "bworld:lever"); // a fence's top isn't something to lie on b.t.game.set_block(-5, 100, -5, "bworld:oak_fence"); b.use("bworld:verdigris", -5, 100, -5, "top"); assertEquals(b.id(-5, 101, -5), AIR_ID); });