From 20e6ac6fbb37530c0f35cbdfd3e4288e3f156892 Mon Sep 17 00:00:00 2001 From: Paula Date: Thu, 22 Oct 2026 16:31:09 -0300 Subject: [PATCH] Verdigris torch --- mods/bworld/blocks/verdigris_torch.json | 34 ++++ mods/bworld/models/verdigris_torch.json | 35 ++++ mods/bworld/models/verdigris_wall_torch.json | 38 ++++ mods/bworld/scripts/verdigris/devices.ts | 79 ++++++++- mods/bworld/scripts/verdigris/server.ts | 3 +- mods/bworld/scripts/verdigris/shared.ts | 13 +- mods/bworld/scripts/verdigris/signals.ts | 23 +++ tests/helpers.ts | 4 +- tests/verdigris.ts | 65 +++++++ tests/verdigris_devices_test.ts | 172 +++++++++++++++++++ tests/verdigris_wire_test.ts | 60 +------ tools/verdigris_bench.ts | 47 +++-- 12 files changed, 500 insertions(+), 73 deletions(-) create mode 100644 mods/bworld/blocks/verdigris_torch.json create mode 100644 mods/bworld/models/verdigris_torch.json create mode 100644 mods/bworld/models/verdigris_wall_torch.json create mode 100644 tests/verdigris.ts create mode 100644 tests/verdigris_devices_test.ts diff --git a/mods/bworld/blocks/verdigris_torch.json b/mods/bworld/blocks/verdigris_torch.json new file mode 100644 index 0000000..925bd2e --- /dev/null +++ b/mods/bworld/blocks/verdigris_torch.json @@ -0,0 +1,34 @@ +{ + "format_version": 1, + "block": { + "id": "bworld:verdigris_torch", + "textures": { "torch": "bworld:verdigris_torch" }, + "model": "bworld:verdigris_torch", + "render_layer": "cutout", + "collision": false, + "shape": [[6, 0, 6, 10, 11, 10]], + "mining": { "toughness": 0 }, + "drops": "bworld:verdigris_torch", + "states": [ + { "name": "facing", "bits": 3, "default": 4 }, + { "name": "lit", "bits": 1, "default": 1 } + ], + "variants": { + "lit=0": { "textures": { "torch": "bworld:verdigris_torch_off" } }, + "facing=0": { "model": "bworld:verdigris_wall_torch", "shape": [[6, 3, 11, 10, 14, 16]] }, + "facing=1": { "model": "bworld:verdigris_wall_torch", "y": 90, "shape": [[6, 3, 11, 10, 14, 16]] }, + "facing=2": { "model": "bworld:verdigris_wall_torch", "y": 180, "shape": [[6, 3, 11, 10, 14, 16]] }, + "facing=3": { "model": "bworld:verdigris_wall_torch", "y": 270, "shape": [[6, 3, 11, 10, 14, 16]] } + }, + "components": { + "bworld:verdigris_source": { + "power": 15, + "when": "lit", + "facing": "facing", + "out": ["front", "sides"], + "strong": ["top"] + }, + "bworld:verdigris_torch": {} + } + } +} diff --git a/mods/bworld/models/verdigris_torch.json b/mods/bworld/models/verdigris_torch.json new file mode 100644 index 0000000..0674eeb --- /dev/null +++ b/mods/bworld/models/verdigris_torch.json @@ -0,0 +1,35 @@ +{ + "format_version": 1, + "model": { + "id": "bworld:verdigris_torch", + "elements": [ + { + "from": [7, 0, 7], + "to": [9, 10, 9], + "shade": false, + "faces": { + "top": { "texture": "#torch", "uv": [7, 6, 9, 8] }, + "bottom": { "texture": "#torch", "uv": [7, 14, 9, 16] } + } + }, + { + "from": [7, 0, 0], + "to": [9, 16, 16], + "shade": false, + "faces": { + "west": { "texture": "#torch", "uv": [0, 0, 16, 16] }, + "east": { "texture": "#torch", "uv": [0, 0, 16, 16] } + } + }, + { + "from": [0, 0, 7], + "to": [16, 16, 9], + "shade": false, + "faces": { + "north": { "texture": "#torch", "uv": [0, 0, 16, 16] }, + "south": { "texture": "#torch", "uv": [0, 0, 16, 16] } + } + } + ] + } +} diff --git a/mods/bworld/models/verdigris_wall_torch.json b/mods/bworld/models/verdigris_wall_torch.json new file mode 100644 index 0000000..b4743b7 --- /dev/null +++ b/mods/bworld/models/verdigris_wall_torch.json @@ -0,0 +1,38 @@ +{ + "format_version": 1, + "model": { + "id": "bworld:verdigris_wall_torch", + "elements": [ + { + "from": [7, 3, 15], + "to": [9, 13, 17], + "rotation": { "origin": [8, 3, 16], "axis": "x", "angle": -22.5 }, + "shade": false, + "faces": { + "top": { "texture": "#torch", "uv": [7, 6, 9, 8] }, + "bottom": { "texture": "#torch", "uv": [7, 14, 9, 16] } + } + }, + { + "from": [7, 3, 8], + "to": [9, 19, 24], + "rotation": { "origin": [8, 3, 16], "axis": "x", "angle": -22.5 }, + "shade": false, + "faces": { + "west": { "texture": "#torch", "uv": [0, 0, 16, 16] }, + "east": { "texture": "#torch", "uv": [0, 0, 16, 16] } + } + }, + { + "from": [0, 3, 15], + "to": [16, 19, 17], + "rotation": { "origin": [8, 3, 16], "axis": "x", "angle": -22.5 }, + "shade": false, + "faces": { + "north": { "texture": "#torch", "uv": [0, 0, 16, 16] }, + "south": { "texture": "#torch", "uv": [0, 0, 16, 16] } + } + } + ] + } +} diff --git a/mods/bworld/scripts/verdigris/devices.ts b/mods/bworld/scripts/verdigris/devices.ts index d1c1cb3..65aff64 100644 --- a/mods/bworld/scripts/verdigris/devices.ts +++ b/mods/bworld/scripts/verdigris/devices.ts @@ -4,11 +4,20 @@ // the blocks that make and use signals, see "Blocks" in REDSTONE.md. what they give out is their bworld:verdigris_source // params, this is what makes their states change import type { BlockRef, Face, Position, ServerContext } from "bworld/server"; -import { type FacingParam, front_of, OPPOSITE, step } from "./sides.ts"; +import { power_from, type SignalReader } from "./signals.ts"; +import { type FacingParam, front_of, OPPOSITE, pack, step } from "./sides.ts"; // a lever's face and facing states, see FacingParam const LEVER_FACING: FacingParam = { face: "face", facing: "facing" }; +// how many ticks a torch takes to go out or light after the block it's on changes +const TORCH_DELAY = 2; +// a torch that goes out more than TORCH_BURNOUT times in TORCH_BURNOUT_WINDOW ticks stays out for TORCH_BURNOUT_TICKS, +// so a fast torch clock can't cost the server much +const TORCH_BURNOUT = 8; +const TORCH_BURNOUT_WINDOW = 60; +const TORCH_BURNOUT_TICKS = 160; + // switched on and off by right clicking it, and it breaks when what it's on goes export function register_lever(ctx: ServerContext) { ctx.components.register_block("bworld:lever", { @@ -24,10 +33,72 @@ export function register_lever(ctx: ServerContext) { }); } +// out while the block it's on is powered, lit while it isn't, TORCH_DELAY ticks after that changes, and burnt out for a +// while when that's too often. which ticks it went out on and how long it's burnt out for are only kept in memory, so a +// restart forgets them +export function register_torch(ctx: ServerContext, world: SignalReader) { + const went_out = new Map(); + const burnt_until = new Map(); + const back = (block: Position) => + OPPOSITE[front_of("facing", (name) => ctx.world.get_state(block.x, block.y, block.z, name))]; + const powered = (block: Position) => power_from(world, block.x, block.y, block.z, back(block)) > 0; + // it's lit while powered or out while not, so it changes TORCH_DELAY ticks from now, unless it already will + const check = (block: Position) => { + const lit = ctx.world.get_state(block.x, block.y, block.z, "lit") === 1; + if (lit === powered(block) && !ctx.world.has_scheduled_tick(block.x, block.y, block.z)) { + ctx.world.schedule_tick(block.x, block.y, block.z, TORCH_DELAY); + } + }; + // the ticks the torch at key went out on lately, without the ones too long ago to count + const lately = (key: number, now: number) => + (went_out.get(key) ?? []).filter((tick) => tick > now - TORCH_BURNOUT_WINDOW); + + ctx.components.register_block("bworld:verdigris_torch", { + on_create: check, + on_break(block) { + const key = pack(block.x, block.y, block.z); + went_out.delete(key); + burnt_until.delete(key); + }, + on_neighbor_change(block, _params, neighbor) { + if (!breaks_without(ctx, block, back(block), neighbor)) check(block); + }, + on_scheduled_tick(block) { + const key = pack(block.x, block.y, block.z); + const now = ctx.system.current_tick; + const lit = ctx.world.get_state(block.x, block.y, block.z, "lit") === 1; + if (lit && powered(block)) { + ctx.world.set_state(block.x, block.y, block.z, "lit", 0); + const ticks = [...lately(key, now), now]; + went_out.set(key, ticks); + if (ticks.length > TORCH_BURNOUT) { + burnt_until.set(key, now + TORCH_BURNOUT_TICKS); + ctx.world.schedule_tick(block.x, block.y, block.z, TORCH_BURNOUT_TICKS); + } + } else if (!lit && !powered(block)) { + // a block has one tick waiting at most, so when going out set off a sooner one, the one that ends the + // burnout was dropped, and this one waits out the rest + const until = burnt_until.get(key) ?? now; + if (until > now) { + ctx.world.schedule_tick(block.x, block.y, block.z, until - now); + return; + } + burnt_until.delete(key); + const ticks = lately(key, now); + if (ticks.length > 0) went_out.set(key, ticks); + else went_out.delete(key); + ctx.world.set_state(block.x, block.y, block.z, "lit", 1); + } + }, + }); +} + // breaks a block that hangs from the block toward face when the neighbor that changed is that block and its face isn't -// something to attach to anymore +// something to attach to anymore. whether it broke function breaks_without(ctx: ServerContext, block: BlockRef, face: Face, neighbor: Position) { const [x, y, z] = step(block.x, block.y, block.z, face); - if (neighbor.x !== x || neighbor.y !== y || neighbor.z !== z) return; - if (!ctx.world.is_face_sturdy(x, y, z, OPPOSITE[face])) ctx.world.break_block(block.x, block.y, block.z); + if (neighbor.x !== x || neighbor.y !== y || neighbor.z !== z) return false; + if (ctx.world.is_face_sturdy(x, y, z, OPPOSITE[face])) return false; + ctx.world.break_block(block.x, block.y, block.z); + return true; } diff --git a/mods/bworld/scripts/verdigris/server.ts b/mods/bworld/scripts/verdigris/server.ts index 4007502..fa4930a 100644 --- a/mods/bworld/scripts/verdigris/server.ts +++ b/mods/bworld/scripts/verdigris/server.ts @@ -4,7 +4,7 @@ // 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 { register_lever, register_torch } 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"; @@ -63,6 +63,7 @@ export function register_verdigris(ctx: ServerContext) { }); register_lever(ctx); + register_torch(ctx, world); } // 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 diff --git a/mods/bworld/scripts/verdigris/shared.ts b/mods/bworld/scripts/verdigris/shared.ts index ffe8811..4825621 100644 --- a/mods/bworld/scripts/verdigris/shared.ts +++ b/mods/bworld/scripts/verdigris/shared.ts @@ -42,10 +42,19 @@ export function register_verdigris_rules(ctx: SharedContext) { return { face: 1, facing: FACES.indexOf(OPPOSITE[side]) }; }, }); + + // on a floor pointing up, or on a wall pointing out of it. not under a ceiling + ctx.components.register_block("bworld:verdigris_torch", { + state_for_placement(placement) { + const side = hangs_from(ctx, placement); + if (!side || side === "top") return false; + return { facing: FACES.indexOf(OPPOSITE[side]) }; + }, + }); } -// the face toward the block a wire or lever placed there hangs from: the one it's placed against, if that block's face -// is something to attach to +// the face toward the block a wire, lever or torch placed there hangs from: the one it's placed against, if that +// block's face is something to attach to function hangs_from(ctx: SharedContext, placement: BlockPlacement): Face | undefined { const side = OPPOSITE[placement.face]; const [x, y, z] = step(placement.x, placement.y, placement.z, side); diff --git a/mods/bworld/scripts/verdigris/signals.ts b/mods/bworld/scripts/verdigris/signals.ts index c31107a..5595bce 100644 --- a/mods/bworld/scripts/verdigris/signals.ts +++ b/mods/bworld/scripts/verdigris/signals.ts @@ -113,3 +113,26 @@ export function solid_power(world: SignalReader, x: number, y: number, z: number } return most; } + +// the power a solid block has for anything but wire: solid_power, and the power of the wire lying on it +export function block_power(world: SignalReader, x: number, y: number, z: number, id: Id) { + if (!world.solid(id)) return 0; + let most = solid_power(world, x, y, z, id); + for (const face of FACES) { + if (most === MAX_POWER) break; + const [nx, ny, nz] = step(x, y, z, face); + if (world.get_block(nx, ny, nz) === WIRE && world.get_state(nx, ny, nz, OPPOSITE[face]) === 1) { + most = Math.max(most, world.get_state(nx, ny, nz, "power") ?? 0); + } + } + return most; +} + +// the power going into the block at x y z from the one toward face: that one's output on that side, or its power if +// it's solid +export function power_from(world: SignalReader, x: number, y: number, z: number, face: Face) { + const [nx, ny, nz] = step(x, y, z, face); + const id = world.get_block(nx, ny, nz); + if (id === undefined) return 0; + return Math.max(output(world, nx, ny, nz, id, OPPOSITE[face]), block_power(world, nx, ny, nz, id)); +} diff --git a/tests/helpers.ts b/tests/helpers.ts index d9f0e87..b58aa63 100644 --- a/tests/helpers.ts +++ b/tests/helpers.ts @@ -267,7 +267,9 @@ export function client_of(game: GameServer, area: Area) { // what game_mode does when this client places held against face of x y z, at hit place(id: string, x: number, y: number, z: number, yaw: number, face: Face, hit: [number, number, number]) { const value = rules.placement_value(info(id)!, yaw, 0, { x, y, z, face, hit, sneaking: false }); - copy.set(`${x} ${y} ${z}`, [id, value! >>> 16]); + // its rules say it can't go there + if (value === undefined) return; + copy.set(`${x} ${y} ${z}`, [id, value >>> 16]); rules.notify_neighbors(x, y, z); }, // what game_mode does when this client right clicks face of x y z, at hit. whether the rules did something diff --git a/tests/verdigris.ts b/tests/verdigris.ts new file mode 100644 index 0000000..b3b0d21 --- /dev/null +++ b/tests/verdigris.ts @@ -0,0 +1,65 @@ +// SPDX-FileCopyrightText: 2026 Paula Wisnievski +// SPDX-License-Identifier: AGPL-3.0-or-later + +// what the verdigris tests build with: a player on a stone floor placing things, and the world read the way the +// verdigris scripts read it +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 } from "$/common/utils.ts"; +import type { WorldSave } from "$/server/game/saves.ts"; +import { joined_cell } from "$/mods/bworld/scripts/verdigris/joins.ts"; +import type { BlockReader } from "$/mods/bworld/scripts/verdigris/signals.ts"; +import { FACES } from "$/mods/bworld/scripts/verdigris/sides.ts"; +import { wire_at } from "$/mods/bworld/scripts/verdigris/wires.ts"; +import { type Area, test_game } from "./helpers.ts"; + +export type Pos = [number, number, number]; +export const AREA: Area = { x: [-6, 6], y: [99, 104], z: [-6, 6] }; + +// alice on a stone floor at y 99, with air above it to build in +export async function builder(save?: WorldSave) { + const t = await test_game("mods", save); + t.join(1, "alice"); + if (!save) { + for (let x = AREA.x[0]; x <= AREA.x[1]; x++) { + for (let z = AREA.z[0]; z <= AREA.z[1]; z++) { + t.game.set_block(x, 99, z, "bworld:stone"); + for (let y = 100; y <= AREA.y[1]; y++) t.game.set_block(x, y, z, AIR_ID); + } + } + } + t.send(1, { type: "move", x: 0.5, y: 100, z: 0.5, yaw: 0, pitch: 0 }); + const player = t.game.players()[0]; + // alice uses id, or whatever she's holding, on face of the block at x y z, from right by it + const use = (id: string | undefined, x: number, y: number, z: number, face: Face) => { + if (id) { + t.send(1, { type: "chat", text: `/give ${id}` }); + const slot = player.inventory.to_synced_data().findIndex((item) => item?.id === id); + t.send(1, { type: "select_slot", slot }); + } + 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] }); + }; + const states = (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)!); + const id = (x: number, y: number, z: number) => t.game.world.get_block_id(x, y, z); + const power = (...at: Pos) => states(...at).power; + // the world the way the wire's own code reads it + const reader: BlockReader = { + get_block: id, + get_state: (x, y, z, name) => states(x, y, z)[name], + source: (block) => + EverythingRegistry.get("blocks", block)?.components?.["bworld:verdigris_source"] as never, + }; + const covers = (...at: Pos) => FACES.filter((face) => states(...at)[face] === 1); + // the wire cells the wire at x y z joins + const joins = (...at: Pos) => + (wire_at(reader, ...at)?.out ?? []).flatMap(([face, direction]) => { + const cell = joined_cell(reader, ...at, face, direction); + return cell ? [cell.join(" ")] : []; + }); + return { t, use, states, id, power, covers, joins, reader }; +} + +export type Builder = Awaited>; diff --git a/tests/verdigris_devices_test.ts b/tests/verdigris_devices_test.ts new file mode 100644 index 0000000..32a468e --- /dev/null +++ b/tests/verdigris_devices_test.ts @@ -0,0 +1,172 @@ +// 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); +}); diff --git a/tests/verdigris_wire_test.ts b/tests/verdigris_wire_test.ts index 424c193..411cbfc 100644 --- a/tests/verdigris_wire_test.ts +++ b/tests/verdigris_wire_test.ts @@ -9,13 +9,13 @@ 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 { default_block_value, set_state_value } from "$/common/utils.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 { 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, wire_at } from "$/mods/bworld/scripts/verdigris/wires.ts"; -import { type Area, client_of, server_blocks, test_game } from "./helpers.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; @@ -172,56 +172,6 @@ Deno.test("a wire shows its powered texture while it has power, and can be point } }); -type Pos = [number, number, number]; -const AREA: Area = { x: [-6, 6], y: [99, 104], z: [-6, 6] }; - -// alice on a stone floor at y 99, with air above it to build in -async function builder(save?: WorldSave) { - const t = await test_game("mods", save); - t.join(1, "alice"); - if (!save) { - for (let x = AREA.x[0]; x <= AREA.x[1]; x++) { - for (let z = AREA.z[0]; z <= AREA.z[1]; z++) { - t.game.set_block(x, 99, z, "bworld:stone"); - for (let y = 100; y <= AREA.y[1]; y++) t.game.set_block(x, y, z, AIR_ID); - } - } - } - t.send(1, { type: "move", x: 0.5, y: 100, z: 0.5, yaw: 0, pitch: 0 }); - const player = t.game.players()[0]; - // alice uses id, or whatever she's holding, on face of the block at x y z, from right by it - const use = (id: string | undefined, x: number, y: number, z: number, face: Face) => { - if (id) { - t.send(1, { type: "chat", text: `/give ${id}` }); - const slot = player.inventory.to_synced_data().findIndex((item) => item?.id === id); - t.send(1, { type: "select_slot", slot }); - } - 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] }); - }; - const states = (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)!); - const id = (x: number, y: number, z: number) => t.game.world.get_block_id(x, y, z); - const power = (...at: Pos) => states(...at).power; - // the world the way the wire's own code reads it - const reader: BlockReader = { - get_block: id, - get_state: (x, y, z, name) => states(x, y, z)[name], - source: (block) => - EverythingRegistry.get("blocks", block)?.components?.["bworld:verdigris_source"] as never, - }; - const covers = (...at: Pos) => FACES.filter((face) => states(...at)[face] === 1); - // the wire cells the wire at x y z joins - const joins = (...at: Pos) => - (wire_at(reader, ...at)?.out ?? []).flatMap(([face, direction]) => { - const cell = joined_cell(reader, ...at, face, direction); - return cell ? [cell.join(" ")] : []; - }); - return { t, use, states, id, power, covers, joins, reader }; -} - -type Builder = Awaited>; - // every wire cell these join joins them back function joined_both_ways(b: Builder, wires: Pos[]) { for (const at of wires) { diff --git a/tools/verdigris_bench.ts b/tools/verdigris_bench.ts index d5345f5..9df10f6 100644 --- a/tools/verdigris_bench.ts +++ b/tools/verdigris_bench.ts @@ -1,13 +1,15 @@ // 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 +// how long verdigris 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 +// taken away beside it while it's powered, a wire broken out of its middle and put back, a lever switching a 64×64 +// grid, and a lever switching a tower of 50 torches. 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. the tower's 50 torches +// changing over 102 ticks took about 3 ms more than the ticks did with nothing switched. repeater clocks come with +// repeaters // deno run -A tools/verdigris_bench.ts import type { Face } from "../common/mod_api/shared.ts"; import { AIR_ID } from "../common/protocol.ts"; @@ -84,9 +86,9 @@ async function floor() { } 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 }; + const states = (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)!); + return { t, hold, use, lay, lever, measure, states }; } // 1000 cells in rows of 40 along x, every other z, each row joined to the next at alternating ends @@ -111,7 +113,7 @@ function zigzag(): Pos[] { 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"); + if (f.states(...cells.at(-1)!).power !== 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: ${ @@ -137,4 +139,29 @@ function zigzag(): Pos[] { ); console.log(` lever switching it: ${f.measure(10, () => f.use(...lever, "top"))}`); } + +{ + const f = await floor(); + // 50 torches, each on top of a block on top of the one below, and a lever on the side of the bottom block + const height = 50; + f.hold("bworld:verdigris_torch"); + for (let i = 0; i < height; i++) { + f.t.game.set_block(0, 100 + i * 2, 0, "bworld:stone"); + f.use(0, 100 + i * 2, 0, "top"); + } + f.hold("bworld:lever"); + f.use(0, 100, 0, "east"); + // each torch changes 2 ticks after the one under it + const ticks = height * 2 + 2; + const run = () => { + for (let i = 0; i < ticks; i++) f.t.game.tick(); + }; + run(); + const top = () => f.states(0, 101 + (height - 1) * 2, 0).lit; + const was = top(); + const idle = f.measure(4, run); + console.log(`a ${height} torch tower switched by a lever, over the ${ticks} ticks it takes:`); + console.log(` ${f.measure(10, () => (f.use(1, 100, 0, "top"), run()))}, against ${idle} with nothing switched`); + if (top() !== was) throw new Error("the top torch didn't change back"); +} Deno.exit(0);