Files
bworld/tests/verdigris_devices_test.ts
2026-11-04 21:51:26 -03:00

341 lines
14 KiB
TypeScript

// SPDX-FileCopyrightText: 2026 Paula Wisnievski
// SPDX-License-Identifier: AGPL-3.0-or-later
// the blocks that make and use verdigris signals
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 { ways_out } from "@/mods/bworld/scripts/verdigris/joins.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);
});
const DOOR = "bworld:oak_door";
const TRAPDOOR = "bworld:oak_trapdoor";
const open = (b: Builder, at: Pos) => b.states(...at).open === 1;
// an oak door at x 100 z, with its upper half above, placed by alice from the south
function door(b: Builder, x: number, z: number) {
b.use(DOOR, x, 99, z, "top", [x, 100, z + 2]);
assertEquals(b.id(x, 101, z), DOOR);
const halves: Pos[] = [[x, 100, z], [x, 101, z]];
return () => halves.map((at) => open(b, at));
}
Deno.test("a door opens while either of its halves is powered, both halves together", async () => {
const b = await builder();
const door_open = door(b, 0, 0);
// a lever on the floor beside the lower half
b.use("bworld:lever", 1, 99, 0, "top");
b.use(undefined, 1, 100, 0, "top");
assertEquals(door_open(), [true, true]);
b.use(undefined, 1, 100, 0, "top");
assertEquals(door_open(), [false, false]);
// a verdigris block beside the upper half
b.t.game.set_block(-1, 101, 0, "bworld:verdigris_block");
assertEquals(door_open(), [true, true]);
// and the lever, which keeps it open once the block's gone
b.use(undefined, 1, 100, 0, "top");
b.t.game.set_block(-1, 101, 0, AIR_ID);
assertEquals(door_open(), [true, true]);
b.use(undefined, 1, 100, 0, "top");
assertEquals(door_open(), [false, false]);
// placed with only its upper half beside power, it opens
b.t.game.set_block(4, 101, 0, "bworld:verdigris_block");
assertEquals(door(b, 3, 0)(), [true, true]);
});
Deno.test("a door opened or closed by hand stays that way until its power changes", async () => {
const b = await builder();
const door_open = door(b, 0, 0);
b.use("bworld:lever", 1, 99, 0, "top");
const switch_lever = () => b.use(undefined, 1, 100, 0, "top");
const click_door = () => b.use(undefined, 0, 100, 0, "south", [0, 100, 2]);
click_door();
switch_lever();
assertEquals(door_open(), [true, true]);
click_door();
assertEquals(door_open(), [false, false]);
// powering the other half too doesn't change whether it's powered
b.t.game.set_block(-1, 101, 0, "bworld:verdigris_block");
b.t.game.set_block(-1, 101, 0, AIR_ID);
assertEquals(door_open(), [false, false]);
switch_lever();
assertEquals(door_open(), [false, false]);
switch_lever();
assertEquals(door_open(), [true, true]);
});
Deno.test("wire beside a door joins it and opens it", async () => {
const b = await builder();
const door_open = door(b, 0, 0);
// from the door's lower half east along the floor to a lever
b.use("bworld:verdigris", 1, 99, 0, "top");
b.use("bworld:verdigris", 2, 99, 0, "top");
b.use("bworld:lever", 3, 99, 0, "top");
const west = ways_out(["bottom"]).findIndex(([, direction]) => direction === "west");
assertEquals(b.states(1, 100, 0)[`arm_${west}`], 1);
b.use(undefined, 3, 100, 0, "top");
assertEquals(door_open(), [true, true]);
b.use(undefined, 3, 100, 0, "top");
assertEquals(door_open(), [false, false]);
});
Deno.test("a trapdoor opens while it's powered, and opens straight away when it's placed beside power", async () => {
const b = await builder();
b.use(TRAPDOOR, 0, 99, 0, "top", [0, 100, 2]);
const trapdoor: Pos = [0, 100, 0];
assertEquals(b.id(...trapdoor), TRAPDOOR);
b.t.game.set_block(1, 100, 0, "bworld:verdigris_block");
assertEquals(open(b, trapdoor), true);
b.t.game.set_block(1, 100, 0, AIR_ID);
assertEquals(open(b, trapdoor), false);
b.t.game.set_block(4, 100, 0, "bworld:verdigris_block");
b.use(TRAPDOOR, 3, 99, 0, "top", [3, 100, 2]);
assertEquals(open(b, [3, 100, 0]), true);
});
const PULSE_REPEATER = "bworld:pulse_repeater";
// a pulse repeater at 0 100 0 placed by alice looking north, so it gives power north, with a lever behind it and wire on
// the floor in front of it. the wire's power after each of ticks ticks
async function pulse_repeater() {
const b = await builder();
b.use(PULSE_REPEATER, 0, 99, 0, "top", [0, 100, 2]);
assertEquals(b.states(0, 100, 0).facing, FACES.indexOf("north"));
b.use("bworld:lever", 0, 99, 1, "top", [0, 100, 3]);
b.use("bworld:verdigris", 0, 99, -1, "top", [0, 100, 2]);
const switch_lever = () => b.use(undefined, 0, 100, 1, "top", [0, 100, 3]);
const wire = (ticks: number) =>
Array.from({ length: ticks }, () => {
b.t.game.tick();
return b.power(0, 100, -1);
});
const scroll = (amount: number, at: Pos = [0, 100, 0]) =>
b.t.send(1, { type: "scroll_block", x: at[0], y: at[1], z: at[2], face: "top", hit: [0.5, 1, 0.5], amount });
const overlays = () => b.t.take(1).filter((m) => m.type === "chat" && m.overlay).map((m) => m.text);
return { ...b, switch_lever, wire, scroll, overlays };
}
Deno.test("a pulse repeater pulses out of its front its delay after what's behind it is powered, once each time", async () => {
const b = await pulse_repeater();
b.switch_lever();
// 2 ticks by default, then 2 ticks of power, and no more while the lever stays on
assertEquals(b.wire(10), [0, 15, 15, 0, 0, 0, 0, 0, 0, 0]);
b.switch_lever();
assertEquals(b.wire(4), [0, 0, 0, 0]);
b.switch_lever();
assertEquals(b.wire(4), [0, 15, 15, 0]);
});
Deno.test("scrolling the mouse wheel at a pulse repeater changes its delay, and shows it above the hotbar", async () => {
const b = await pulse_repeater();
b.overlays();
b.scroll(1);
assertEquals(b.overlays(), ["Delay: 3 ticks"]);
// a tick at a time up to a second, then seconds
b.scroll(17);
b.scroll(1);
assertEquals(b.overlays(), ["Delay: 1 second", "Delay: 2 seconds"]);
b.switch_lever();
const wire = b.wire(42);
assertEquals([wire.indexOf(15), wire.lastIndexOf(15)], [39, 40]);
// it goes no further than an hour, and no lower than 2 ticks
b.scroll(1000);
b.scroll(-1);
b.scroll(-1000);
assertEquals(b.overlays(), ["Delay: 60 minutes", "Delay: 59 minutes", "Delay: 2 ticks"]);
// blocks that aren't scrollable and ones out of reach don't hear it
b.scroll(1, [0, 99, 0]);
b.t.game.teleport(b.t.game.players()[0], 0.5, 100, 20.5);
b.scroll(1);
assertEquals(b.overlays(), []);
assertEquals(b.states(0, 100, 0).delay, 0);
});
Deno.test("right clicking a pulse repeater inverts it, so it's on and its pulse turns it off", async () => {
const b = await pulse_repeater();
b.use(undefined, 0, 100, 0, "top", [0, 100, 2]);
assertEquals(b.power(0, 100, -1), 15);
b.switch_lever();
assertEquals(b.wire(4), [15, 0, 0, 15]);
b.use(undefined, 0, 100, 0, "top", [0, 100, 2]);
assertEquals(b.power(0, 100, -1), 0);
});
Deno.test("a pulse repeater only goes on a floor, and breaks when the floor goes", async () => {
const b = await builder();
b.t.game.set_block(3, 101, 3, "bworld:stone");
b.use(PULSE_REPEATER, 3, 101, 3, "south", [3, 101, 6]);
assertEquals(b.id(3, 101, 4), AIR_ID);
b.use(PULSE_REPEATER, 3, 101, 3, "top", [3, 102, 6]);
assertEquals(b.id(3, 102, 3), PULSE_REPEATER);
b.t.game.set_block(3, 101, 3, AIR_ID);
assertEquals(b.id(3, 102, 3), AIR_ID);
});