// SPDX-FileCopyrightText: 2026 Paula Wisnievski // SPDX-License-Identifier: AGPL-3.0-or-later // the blocks that make and use verdigris signals, see "Blocks" in REDSTONE.md import { assert, assertAlmostEquals, assertEquals } from "@std/assert"; import { bake_block, type BakedQuad, type ModelJson } from "$/common/block_models.ts"; import { block_shape } 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 { FACE_OFFSETS, FACES, OPPOSITE, step } from "$/mods/bworld/scripts/verdigris/sides.ts"; import { client_of, server_blocks, test_game } from "./helpers.ts"; import { AREA, type Builder, builder, type Pos } from "./verdigris.ts"; const TORCH = "bworld:verdigris_torch"; // 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: readonly 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]; } // the middle of a quad function middle(quad: BakedQuad) { return [0, 1, 2].map((axis) => (quad.positions[axis] + quad.positions[3 + axis] + quad.positions[6 + axis] + quad.positions[9 + axis]) / 4 ); } Deno.test("a torch stands up on a floor, and leans out of a wall from the bottom of the face it's on", async () => { await test_game("mods"); const block = EverythingRegistry.get("blocks", TORCH)!; const models = Object.fromEntries(EverythingRegistry.entries("models")); for (const facing of [0, 1, 2, 3, 4]) { const quads = bake_block( block, set_state_value(default_block_value(0, block), block, "facing", facing)!, models, ); // the ends of its stick, by the part of the texture they show: the middle two columns, from row v down 2 rows const end = (v: number) => middle( quads.find((quad) => [0, 1, 2, 3].every((k) => quad.uvs[k * 2] >= 7 && quad.uvs[k * 2] <= 9 && quad.uvs[k * 2 + 1] >= v && quad.uvs[k * 2 + 1] <= v + 2 ) )!, ); const [top, bottom] = [end(6), end(14)]; assert(top[1] > bottom[1], `${facing} points up`); const shape = block_shape(block, { facing, lit: 1 }); assertEquals(shape.length, 1); if (facing === 4) { assertAlmostEquals(top[0], bottom[0], 1e-6); assertAlmostEquals(top[2], bottom[2], 1e-6); assertEquals([shape[0][1], shape[0][0] + shape[0][3], shape[0][2] + shape[0][5]], [0, 1, 1]); continue; } const front = FACES[facing]; assert(toward(top, front) > toward(bottom, front) + 0.1, `${front} leans out`); assertAlmostEquals(toward(bottom, OPPOSITE[front]), 1, 1e-6, `${front} stands on the wall behind it`); assertAlmostEquals(Math.max(toward(shape[0], OPPOSITE[front]), toward(shape[0].slice(3), OPPOSITE[front])), 1); } }); // a stone block at x 100 z with a torch on top, a stone block over the torch, and a torch on top of that async function tower(x: number, z: number) { const b = await builder(); for (const y of [100, 102]) { b.t.game.set_block(x, y, z, "bworld:stone"); b.use(TORCH, x, y, z, "top"); } run(b, 10); return { ...b, lower: [x, 101, z] as Pos, upper: [x, 103, z] as Pos }; } function run(b: Builder, ticks: number) { for (let i = 0; i < ticks; i++) b.t.game.tick(); } const lit = (b: Builder, at: Pos) => b.states(...at).lit === 1; Deno.test("a torch goes out 2 ticks after the block it's on is powered, and lights 2 ticks after it isn't", async () => { const b = await tower(0, 0); // the lower torch powers the block the upper one is on assertEquals([lit(b, b.lower), lit(b, b.upper)], [true, false]); b.use("bworld:lever", 0, 100, 0, "east"); b.use(undefined, 1, 100, 0, "top"); run(b, 1); assertEquals([lit(b, b.lower), lit(b, b.upper)], [true, false]); run(b, 1); assertEquals([lit(b, b.lower), lit(b, b.upper)], [false, false]); run(b, 2); assertEquals([lit(b, b.lower), lit(b, b.upper)], [false, true]); b.use(undefined, 1, 100, 0, "top"); run(b, 2); assertEquals(lit(b, b.lower), true); run(b, 2); assertEquals(lit(b, b.upper), false); }); Deno.test("a lit torch powers wire beside it and the block above it, but not the block it's on", async () => { const b = await tower(0, 0); // wire beside the lower torch, on the side of the block it's on, and on top of a block over the upper torch b.t.game.set_block(1, 100, 0, "bworld:stone"); b.use("bworld:verdigris", 1, 100, 0, "top"); b.use("bworld:verdigris", 0, 100, 0, "north"); b.t.game.set_block(0, 104, 0, "bworld:stone"); b.use("bworld:verdigris", 0, 104, 0, "top"); const wires: Pos[] = [[1, 101, 0], [0, 100, -1], [0, 105, 0]]; assertEquals(wires.map((at) => b.power(...at)), [15, 0, 0]); // a lever powering the block the lower torch is on puts it out, which lights the upper one b.use("bworld:lever", 0, 100, 0, "west"); b.use(undefined, -1, 100, 0, "top"); run(b, 4); assertEquals([lit(b, b.lower), lit(b, b.upper)], [false, true]); assertEquals(wires.map((at) => b.power(...at)), [0, 15, 15]); }); Deno.test("a torch that powers the block it's on blinks, until it burns out for a while", async () => { const b = await builder(); // a torch on the side of a block, with a block over it that it powers. wire on that block's side and on top of the // block the torch is on powers that block back b.t.game.set_block(0, 100, 0, "bworld:stone"); b.t.game.set_block(1, 101, 0, "bworld:stone"); b.use("bworld:verdigris", 0, 100, 0, "top"); b.use("bworld:verdigris", 1, 101, 0, "west"); b.use(TORCH, 0, 100, 0, "east"); const torch: Pos = [1, 100, 0]; assertEquals(b.id(...torch), TORCH); const seen: boolean[] = []; for (let i = 0; i < 400; i++) { b.t.game.tick(); seen.push(lit(b, torch)); } const changes = seen.filter((on, i) => i > 0 && on !== seen[i - 1]).length; assert(seen.slice(0, 20).includes(true) && seen.slice(0, 20).includes(false), "it blinks"); // the longest it stayed out let longest = 0; let run_out = 0; for (const on of seen) { run_out = on ? 0 : run_out + 1; longest = Math.max(longest, run_out); } assert(longest > 100, `it burnt out, longest out ${longest}`); assert(seen.lastIndexOf(true) > seen.indexOf(false) + longest, "and lit again"); assert(changes < 400 / 4, "it blinked less than it would without burning out"); }); Deno.test("a torch goes on a floor or a wall but not under a ceiling, and breaks when what it's on goes", async () => { const b = await builder(); b.t.game.set_block(3, 101, 3, "bworld:stone"); // a player sees it the way the server places it const client = client_of(b.t.game, AREA); for (const face of ["top", "south", "bottom"] as const) { client.place(TORCH, ...step(3, 101, 3, face), 0, face, [0.5, 0.5, 0.5]); b.use(TORCH, 3, 101, 3, face); } assertEquals(client.blocks(), server_blocks(b.t.game, AREA)); assertEquals([b.states(3, 102, 3).facing, b.states(3, 101, 4).facing], [ FACES.indexOf("top"), FACES.indexOf("south"), ]); assertEquals(b.id(3, 100, 3), AIR_ID); b.t.game.set_block(3, 101, 3, AIR_ID); assertEquals([b.id(3, 102, 3), b.id(3, 101, 4)], [AIR_ID, AIR_ID]); const dropped = b.t.game.item_entities().filter((e) => e.item.type_id === TORCH).length; assertEquals(dropped, 2); });