Verdigris working !

This commit is contained in:
2026-10-22 15:51:40 -03:00
parent 6af6c52673
commit db0589d965
9 changed files with 542 additions and 104 deletions
+140
View File
@@ -0,0 +1,140 @@
// 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
// 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
// deno run -A tools/verdigris_bench.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 { stone_floor, test_game } from "../tests/helpers.ts";
type Pos = [number, number, number];
// alice on a stone floor at y 99 with nothing above it, placing what she holds on blocks from right by them
async function floor() {
const t = await test_game("mods");
stone_floor(t.game, 34);
for (let x = -34; x <= 34; x++) {
for (let z = -34; z <= 34; z++) {
for (let y = 100; y < 103; y++) {
if (t.game.world.get_block_id(x, y, z) !== AIR_ID) t.game.set_block(x, y, z, AIR_ID);
}
}
}
// every block that changes, as if a player saw them all
let changes: string[] = [];
const on_block_change = t.game.world.on_block_change;
t.game.world.on_block_change = (x, y, z, id, state) => {
changes.push(id);
on_block_change?.(x, y, z, id, state);
};
t.join(1, "alice");
t.send(1, { type: "move", x: 0.5, y: 100, z: 0.5, yaw: 0, pitch: 0 });
const player = t.game.players()[0];
t.game.tick();
t.take(1);
const hold = (id: string) => {
t.send(1, { type: "chat", text: `/give ${id} 64` });
t.send(1, {
type: "select_slot",
slot: player.inventory.to_synced_data().findIndex((item) => item?.id === id),
});
};
const use = (x: number, y: number, z: number, face: Face) => {
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] });
};
// wire on the floor under each of cells, in order, and how long that took
const lay = (cells: Pos[]) => {
let ms = 0;
cells.forEach(([x, , z], i) => {
if (i % 64 === 0) hold("bworld:verdigris");
const start = performance.now();
use(x, 99, z, "top");
ms += performance.now() - start;
});
t.take(1);
return ms;
};
// a lever on the floor at x z
const lever = (x: number, z: number) => {
hold("bworld:lever");
use(x, 99, z, "top");
t.take(1);
return [x, 100, z] as Pos;
};
// runs change count times, and how long each took and the set_block messages each sent, all and wire's
const measure = (count: number, change: (i: number) => void) => {
let ms = 0;
let sent = 0;
let wires = 0;
for (let i = 0; i < count; i++) {
const start = performance.now();
changes = [];
change(i);
ms += performance.now() - start;
sent += changes.length;
wires += changes.filter((id) => id === "bworld:verdigris_wire").length;
}
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 };
}
// 1000 cells in rows of 40 along x, every other z, each row joined to the next at alternating ends
function zigzag(): Pos[] {
const cells: Pos[] = [];
for (let row = 0; cells.length < 1000; row++) {
const z = -25 + row * 2;
for (let i = 0; i < 40 && cells.length < 1000; i++) cells.push([row % 2 ? 19 - i : -20 + i, 100, z]);
if (cells.length < 1000) cells.push([row % 2 ? -20 : 19, 100, z + 1]);
}
return cells;
}
{
const f = await floor();
const cells = zigzag();
const lever = f.lever(-21, -25);
const placed = f.lay(cells);
console.log(
`placing ${cells.length} wires: ${(placed / cells.length).toFixed(2)} ms a wire, ${placed.toFixed(0)} ms`,
);
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");
// beside a wire on two rows
console.log(
` a block beside them placed and taken away: ${
f.measure(20, (i) => f.t.game.set_block(0, 100, -24, i % 2 ? AIR_ID : "bworld:stone"))
}`,
);
const middle = cells[cells.length / 2];
console.log(
` a wire in the middle broken and put back: ${
f.measure(20, (i) => i % 2 ? f.use(middle[0], 99, middle[2], "top") : f.t.game.destroy_block(...middle))
}`,
);
}
{
const f = await floor();
const cells: Pos[] = [];
for (let z = -32; z < 32; z++) for (let x = -32; x < 32; x++) cells.push([x, 100, z]);
const lever = f.lever(-33, -32);
const placed = f.lay(cells);
console.log(
`placing a 64×64 grid: ${(placed / cells.length).toFixed(2)} ms a wire, ${(placed / 1000).toFixed(1)} s`,
);
console.log(` lever switching it: ${f.measure(10, () => f.use(...lever, "top"))}`);
}
Deno.exit(0);