Verdigris torch

This commit is contained in:
2026-10-22 16:31:09 -03:00
parent db0589d965
commit 20e6ac6fbb
12 changed files with 500 additions and 73 deletions
+34
View File
@@ -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": {}
}
}
}
+35
View File
@@ -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] }
}
}
]
}
}
@@ -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] }
}
}
]
}
}
+75 -4
View File
@@ -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<number, number[]>();
const burnt_until = new Map<number, number>();
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;
}
+2 -1
View File
@@ -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
+11 -2
View File
@@ -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);
+23
View File
@@ -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));
}
+3 -1
View File
@@ -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
+65
View File
@@ -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<BlockRegistry>("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<ReturnType<typeof builder>>;
+172
View File
@@ -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<BlockRegistry>("blocks", TORCH)!;
const models = Object.fromEntries(EverythingRegistry.entries<ModelJson>("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);
});
+5 -55
View File
@@ -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<BlockRegistry>("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<ReturnType<typeof builder>>;
// every wire cell these join joins them back
function joined_both_ways(b: Builder, wires: Pos[]) {
for (const at of wires) {
+37 -10
View File
@@ -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);