Verdigris wire and lever

This commit is contained in:
2026-10-22 13:43:57 -03:00
parent 0d80e85dec
commit 6af6c52673
24 changed files with 3191 additions and 733 deletions
+99 -2
View File
@@ -2,17 +2,23 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
import { resolve, toFileUrl } from "@std/path";
import { EverythingRegistry } from "$/common/everything_registry.ts";
import { type BlockRegistry, EverythingRegistry } from "$/common/everything_registry.ts";
import { load_and_check, load_order } from "$/tools/check_mods.ts";
import { mod_data } from "$/common/mod_check.ts";
import type { ServerModSource } from "$/server/game/load_mods.ts";
import { GameServer } from "$/server/game/game_server.ts";
import { start_game } from "$/server/game/load_mods.ts";
import { apply_config } from "$/server/load_bmods.ts";
import { PROTOCOL_VERSION } from "$/common/protocol.ts";
import { AIR_ID, PROTOCOL_VERSION } from "$/common/protocol.ts";
import { decode_level_chunk } from "$/common/chunk_streaming.ts";
import { CHUNK_AREA, CHUNK_SIZE, ID_MASK } from "$/common/constants.ts";
import type { SavedChunk, SavedChunks, WorldSave } from "$/server/game/saves.ts";
import { BlockRules } from "$/common/block_rules.ts";
import { collision_shape_at, covers_face, is_solid } from "$/common/block_shapes.ts";
import { tag_api } from "$/common/tags.ts";
import { get_state_value, set_state_value } from "$/common/utils.ts";
import type { Face, SharedWorld } from "$/common/mod_api/shared.ts";
import { setup as setup_shared } from "$/mods/bworld/scripts/shared.ts";
// the repo's own mods/, read once: reading and checking every mod's files is most of starting a game in a test. only
// mods/ is kept, since tests change the temporary folders they make between games
@@ -184,3 +190,94 @@ export const container_items = (messages: Msg[], container: string) =>
// what the open menu's data last was among messages
export const menu_data = (messages: Msg[]) => messages.findLast((m) => m.type === "menu_data")?.data;
// a box of blocks, by the lowest and highest of each coordinate in it
export interface Area {
x: [number, number];
y: [number, number];
z: [number, number];
}
// every block in the area, as id and state bits
function blocks_in(area: Area, get: (x: number, y: number, z: number) => [string, number]) {
const out: string[] = [];
for (let x = area.x[0]; x <= area.x[1]; x++) {
for (let y = area.y[0]; y <= area.y[1]; y++) {
for (let z = area.z[0]; z <= area.z[1]; z++) out.push(`${x} ${y} ${z} ${get(x, y, z).join(" ")}`);
}
}
return out;
}
export function server_blocks(game: GameServer, area: Area) {
return blocks_in(area, (x, y, z) => [game.world.get_block_id(x, y, z), game.world.get_block_value(x, y, z) >>> 16]);
}
// a client: its copy of the area, and the bworld shared script's rules running on it the way client/mods.ts does
export function client_of(game: GameServer, area: Area) {
const copy = new Map<string, [string, number]>();
for (const line of server_blocks(game, area)) {
const [x, y, z, id, state] = line.split(" ");
copy.set(`${x} ${y} ${z}`, [id, Number(state)]);
}
const at = (x: number, y: number, z: number) => copy.get(`${x} ${y} ${z}`);
const info = (id: string) => EverythingRegistry.get<BlockRegistry>("blocks", id);
const rules = new BlockRules((_mod, _what, fn) => fn(), (x, y, z) => at(x, y, z)?.[0]);
const world: SharedWorld = {
get_block: (x, y, z) => at(x, y, z)?.[0],
get_state(x, y, z, name) {
const [id, state] = at(x, y, z) ?? [AIR_ID, 0];
const block = info(id);
return block?.states?.some((s) => s.name === name) ? get_state_value(state << 16, block, name) : undefined;
},
set_state(x, y, z, name, value) {
const [id, state] = at(x, y, z) ?? [AIR_ID, 0];
const block = info(id);
if (!block?.states?.some((s) => s.name === name)) return false;
const changed = set_state_value(state << 16, block, name, value)! >>> 16;
if (changed !== state) {
copy.set(`${x} ${y} ${z}`, [id, changed]);
rules.notify_neighbors(x, y, z);
}
return true;
},
is_face_sturdy(x, y, z, face: Face) {
const [id, state] = at(x, y, z) ?? [AIR_ID, 0];
const block = info(id);
return block !== undefined && covers_face(collision_shape_at(block, state << 16), face);
},
biome_at: () => undefined,
season_at: () => undefined,
};
setup_shared({
mod: { id: "bworld", version: "test" },
side: "client",
tags: tag_api(),
blocks: {
exists: (id) => info(id) !== undefined,
components: (id) => structuredClone(info(id)!.components ?? {}),
solid: (id) => id !== AIR_ID && is_solid(info(id)!),
},
world,
components: { register_block: (id, block_rules) => rules.register("bworld", id, block_rules) },
});
return {
// 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]);
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
interact(x: number, y: number, z: number, yaw: number, face: Face, hit: [number, number, number]) {
return rules.interact(info(at(x, y, z)![0])!, x, y, z, { face, hit, sneaking: false }, yaw);
},
break(x: number, y: number, z: number) {
copy.set(`${x} ${y} ${z}`, [AIR_ID, 0]);
rules.notify_neighbors(x, y, z);
},
blocks: () => blocks_in(area, (x, y, z) => at(x, y, z)!),
};
}
+11 -100
View File
@@ -4,107 +4,18 @@
// shared scripts' block rules run on clients too, so what a client shows the moment it places, breaks or right clicks
// a block is what the server ends up with
import { assertEquals } from "@std/assert";
import { BlockRules } from "$/common/block_rules.ts";
import { collision_shape_at, covers_face, is_solid } from "$/common/block_shapes.ts";
import { type BlockRegistry, EverythingRegistry } from "$/common/everything_registry.ts";
import { tag_api } from "$/common/tags.ts";
import { AIR_ID } from "$/common/protocol.ts";
import { get_state_value, set_state_value } from "$/common/utils.ts";
import type { Face, SharedWorld } from "$/common/mod_api/shared.ts";
import { setup as setup_shared } from "$/mods/bworld/scripts/shared.ts";
import { test_game } from "./helpers.ts";
import { get_state_value } from "$/common/utils.ts";
import type { Face } from "$/common/mod_api/shared.ts";
import { type Area, client_of, server_blocks, test_game } from "./helpers.ts";
type Game = Awaited<ReturnType<typeof test_game>>;
const AREA = { x: [-3, 3], y: [99, 101], z: [-3, 3] };
// every block in the area, as id and state bits
function blocks_in(get: (x: number, y: number, z: number) => [string, number]) {
const out: string[] = [];
for (let x = AREA.x[0]; x <= AREA.x[1]; x++) {
for (let y = AREA.y[0]; y <= AREA.y[1]; y++) {
for (let z = AREA.z[0]; z <= AREA.z[1]; z++) out.push(`${x} ${y} ${z} ${get(x, y, z).join(" ")}`);
}
}
return out;
}
const AREA: Area = { x: [-3, 3], y: [99, 101], z: [-3, 3] };
function state(t: Game, x: number, y: number, z: number, name: string) {
return get_state_value(t.game.world.get_block_value(x, y, z), t.game.world.get_block_info(x, y, z)!, name);
}
function server_blocks(t: Game) {
return blocks_in((x, y, z) => [t.game.world.get_block_id(x, y, z), t.game.world.get_block_value(x, y, z) >>> 16]);
}
// a client: its copy of the area, and the bworld shared script's rules running on it the way client/mods.ts does
function client_of(t: Game) {
const copy = new Map<string, [string, number]>();
for (const line of server_blocks(t)) {
const [x, y, z, id, state] = line.split(" ");
copy.set(`${x} ${y} ${z}`, [id, Number(state)]);
}
const at = (x: number, y: number, z: number) => copy.get(`${x} ${y} ${z}`);
const info = (id: string) => EverythingRegistry.get<BlockRegistry>("blocks", id);
const rules = new BlockRules((_mod, _what, fn) => fn(), (x, y, z) => at(x, y, z)?.[0]);
const world: SharedWorld = {
get_block: (x, y, z) => at(x, y, z)?.[0],
get_state(x, y, z, name) {
const [id, state] = at(x, y, z) ?? [AIR_ID, 0];
const block = info(id);
return block?.states?.some((s) => s.name === name) ? get_state_value(state << 16, block, name) : undefined;
},
set_state(x, y, z, name, value) {
const [id, state] = at(x, y, z) ?? [AIR_ID, 0];
const block = info(id);
if (!block?.states?.some((s) => s.name === name)) return false;
const changed = set_state_value(state << 16, block, name, value)! >>> 16;
if (changed !== state) {
copy.set(`${x} ${y} ${z}`, [id, changed]);
rules.notify_neighbors(x, y, z);
}
return true;
},
is_face_sturdy(x, y, z, face: Face) {
const [id, state] = at(x, y, z) ?? [AIR_ID, 0];
const block = info(id);
return block !== undefined && covers_face(collision_shape_at(block, state << 16), face);
},
biome_at: () => undefined,
season_at: () => undefined,
};
setup_shared({
mod: { id: "bworld", version: "test" },
side: "client",
tags: tag_api(),
blocks: {
exists: (id) => info(id) !== undefined,
components: (id) => structuredClone(info(id)!.components ?? {}),
solid: (id) => id !== AIR_ID && is_solid(info(id)!),
},
world,
components: { register_block: (id, block_rules) => rules.register("bworld", id, block_rules) },
});
return {
// 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]);
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
interact(x: number, y: number, z: number, yaw: number, face: Face, hit: [number, number, number]) {
return rules.interact(info(at(x, y, z)![0])!, x, y, z, { face, hit, sneaking: false }, yaw);
},
break(x: number, y: number, z: number) {
copy.set(`${x} ${y} ${z}`, [AIR_ID, 0]);
rules.notify_neighbors(x, y, z);
},
blocks: () => blocks_in((x, y, z) => at(x, y, z)!),
};
}
// alice on a stone floor at y 99, south of 0 100 0, holding id
async function setup(id: string, yaw = 0) {
const t = await test_game("mods");
@@ -128,11 +39,11 @@ Deno.test("a client places a fence joined to the fences and walls beside it, the
t.game.set_block(-1, 100, 0, "bworld:oak_fence");
t.game.set_block(0, 100, -1, "bworld:stone");
t.game.set_block(1, 100, 0, "bworld:oak_leaves");
const client = client_of(t);
const client = client_of(t.game, AREA);
client.place("bworld:oak_fence", 0, 100, 0, 0, "top", [0.5, 1, 0.5]);
use(0, 99, 0, "top", [0.5, 1, 0.5]);
assertEquals(client.blocks(), server_blocks(t));
assertEquals(client.blocks(), server_blocks(t.game, AREA));
// it joined the fence and the stone, and the fence joined it back
assertEquals(state(t, 0, 100, 0, "west"), 1);
assertEquals(state(t, 0, 100, 0, "north"), 1);
@@ -141,21 +52,21 @@ Deno.test("a client places a fence joined to the fences and walls beside it, the
// breaking it lets go of the one beside it, on both
client.break(0, 100, 0);
t.send(1, { type: "break_block", x: 0, y: 100, z: 0 });
assertEquals(client.blocks(), server_blocks(t));
assertEquals(client.blocks(), server_blocks(t.game, AREA));
assertEquals(state(t, -1, 100, 0, "east"), 0);
});
Deno.test("a client places a trapdoor turned and in the half the server puts it", async () => {
const { t, use } = await setup("bworld:oak_trapdoor", Math.PI / 2);
t.game.set_block(0, 100, 0, "bworld:stone");
const client = client_of(t);
const client = client_of(t.game, AREA);
// on the upper half of the stone's east side, and on top of the floor facing alice, who looks west
client.place("bworld:oak_trapdoor", 1, 100, 0, Math.PI / 2, "east", [1, 0.8, 0.5]);
use(0, 100, 0, "east", [1, 0.8, 0.5]);
client.place("bworld:oak_trapdoor", 0, 100, 2, Math.PI / 2, "top", [0.5, 1, 0.5]);
use(0, 99, 2, "top", [0.5, 1, 0.5]);
assertEquals(client.blocks(), server_blocks(t));
assertEquals(client.blocks(), server_blocks(t.game, AREA));
assertEquals(state(t, 1, 100, 0, "half"), 1);
assertEquals(state(t, 0, 100, 2, "facing"), 1);
});
@@ -163,12 +74,12 @@ Deno.test("a client places a trapdoor turned and in the half the server puts it"
Deno.test("a client opens and closes a trapdoor right away, the same as the server, without placing anything", async () => {
const { t, use } = await setup("bworld:oak_trapdoor");
t.game.set_block(0, 100, 0, "bworld:oak_trapdoor");
const client = client_of(t);
const client = client_of(t.game, AREA);
for (const open of [1, 0]) {
assertEquals(client.interact(0, 100, 0, 0, "south", [0.5, 0.5, 1]), true);
use(0, 100, 0, "south", [0.5, 0.5, 1]);
assertEquals(client.blocks(), server_blocks(t));
assertEquals(client.blocks(), server_blocks(t.game, AREA));
assertEquals(state(t, 0, 100, 0, "open"), open);
}
// alice still has all of hers
+373 -76
View File
@@ -1,30 +1,40 @@
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
// SPDX-License-Identifier: AGPL-3.0-or-later
// verdigris wire, see REDSTONE.md: how it's drawn on every side it can lie on
// verdigris wire, see REDSTONE.md: how it's drawn on the faces of its cell, how it joins, and its nets' power
import { assert, assertAlmostEquals, assertEquals } from "@std/assert";
import { bake_block, type BakedQuad, FACE_INDEX, 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 { default_block_value, set_state_value } from "$/common/utils.ts";
import { DIRECTIONS, FACE_OFFSETS, FACES } from "$/mods/bworld/scripts/verdigris/sides.ts";
import { test_game } from "./helpers.ts";
import { AIR_ID } from "$/common/protocol.ts";
import { block_states, default_block_value, set_state_value } from "$/common/utils.ts";
import type { 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 { DIRECTIONS, FACE_OFFSETS, FACES, OPPOSITE } from "$/mods/bworld/scripts/verdigris/sides.ts";
import { wire_at } from "$/mods/bworld/scripts/verdigris/wires.ts";
import { type Area, client_of, server_blocks, test_game } from "./helpers.ts";
// how far it stands off the face it lies on, in blocks
const HEIGHT = 0.25 / 16;
const OPPOSITE: Record<Face, Face> = {
north: "south",
south: "north",
east: "west",
west: "east",
top: "bottom",
bottom: "top",
};
const WIRE = "bworld:verdigris_wire";
// every set of faces one wire cell can cover
function coverable(): Face[][] {
const sets: Face[][] = [];
for (let set = 1; set < 1 << FACES.length; set++) {
const faces = FACES.filter((_, i) => set & (1 << i));
if (can_cover(faces)) sets.push(faces);
}
return sets;
}
const covering = (faces: Face[]) => Object.fromEntries(faces.map((face) => [face, 1]));
async function wire() {
await test_game("mods");
const block = EverythingRegistry.get<BlockRegistry>("blocks", "bworld:verdigris_wire")!;
const block = EverythingRegistry.get<BlockRegistry>("blocks", WIRE)!;
const models = Object.fromEntries(EverythingRegistry.entries<ModelJson>("models"));
const value = (states: Record<string, number>) => {
let value = default_block_value(0, block);
@@ -52,13 +62,26 @@ function range(quad: BakedQuad, face: Face) {
return [Math.min(...reach), Math.max(...reach)];
}
// lying on the block toward side: flat at the wire's height off it, facing away from it
function lies_on(quad: BakedQuad, side: Face) {
const [low, high] = range(quad, side);
return quad.face === FACE_INDEX[OPPOSITE[side]] && Math.abs(low - (1 - HEIGHT)) < 1e-6 &&
// lying on face of the cell: flat at the wire's height off it, facing away from it
function lies_on(quad: BakedQuad, face: Face) {
const [low, high] = range(quad, face);
return quad.face === FACE_INDEX[OPPOSITE[face]] && Math.abs(low - (1 - HEIGHT)) < 1e-6 &&
Math.abs(high - (1 - HEIGHT)) < 1e-6;
}
// the quad lying on face from the cell's middle toward direction, to its edge or a little past. undefined if none
function arm(quads: BakedQuad[], face: Face, direction: Face) {
const found = quads.filter((quad) => lies_on(quad, face) && range(quad, direction)[0] > 0.5);
assert(found.length <= 1);
if (found[0]) {
for (const across of DIRECTIONS[face].filter((d) => d !== direction && d !== OPPOSITE[direction])) {
const [low, high] = range(found[0], across);
assert(low > 0.4 && high < 0.6, `on ${face} toward ${direction}: no wider than the wire`);
}
}
return found[0];
}
// the part of its texture a point on a face facing that way would show on a whole block, like a face's default uv
function block_uv(face: number, [x, y, z]: number[]): [number, number] {
const uvs: [number, number][] = [
@@ -72,78 +95,352 @@ function block_uv(face: number, [x, y, z]: number[]): [number, number] {
return [uvs[face][0] * 16, uvs[face][1] * 16];
}
Deno.test("a wire on each side draws its middle there, and joins toward each of that side's directions", async () => {
Deno.test("a wire draws its middle on each face it covers, and its faces meet where they meet in the cell", async () => {
const { quads } = await wire();
FACES.forEach((side, s) => {
const alone = quads({ side: s });
assertEquals(alone.length, 1, `on ${side} it's only its middle`);
assert(lies_on(alone[0], side), side);
for (const face of FACES) {
const alone = quads({ [face]: 1 });
assertEquals(alone.length, 1, `on ${face} it's only its middle`);
assert(lies_on(alone[0], face), face);
for (const direction of DIRECTIONS[face]) {
// each runs to the edge between them, and no further
const both = quads({ [face]: 1, [direction]: 1 });
assertEquals(both.length, 4, `${face} and ${direction}`);
assertAlmostEquals(range(arm(both, face, direction)!, direction)[1], 1, 1e-6);
assertAlmostEquals(range(arm(both, direction, face)!, face)[1], 1, 1e-6);
}
}
});
DIRECTIONS[side].forEach((direction, i) => {
const where = `on ${side}, joining ${direction}`;
const [straight, edge, corner] = [1, 2, 3].map((join) =>
quads({ side: s, [`join_${i}`]: join }).filter((quad) =>
!lies_on(quad, side) || range(quad, direction)[1] > 0.6
)
);
// on the wire's side, from its middle out to the cell's edge that way
const arm = straight.find((quad) => lies_on(quad, side))!;
assert(arm, where);
assertEquals(straight.length, 1, where);
const [from, to] = range(arm, direction);
assert(from > 0.5 && Math.abs(to - 1) < 1e-6, `${where}: ${from} to ${to}`);
for (const across of DIRECTIONS[side].filter((d) => d !== direction && d !== OPPOSITE[direction])) {
const [low, high] = range(arm, across);
assert(low > 0.4 && high < 0.6, `${where}: no wider than the wire`);
}
// over an edge, a little past it to meet the wire around the corner
assertEquals(edge.length, 1, where);
assertAlmostEquals(range(edge[0], direction)[1], 1 + HEIGHT, 1e-6, where);
// into a corner, then up the face it runs into, all the way across the cell
assertEquals(corner.length, 2, where);
const bend = corner.find((quad) => !lies_on(quad, side))!;
assertEquals(bend.face, FACE_INDEX[OPPOSITE[direction]], where);
assertAlmostEquals(range(bend, direction)[0], 1 - HEIGHT, 1e-6, where);
assertEquals(range(bend, side).map((v) => Math.round(v * 1e6) / 1e6), [0, 1], where);
Deno.test("each arm_ state draws the way out of the cell its faces give it, a little past the edge", async () => {
const { quads } = await wire();
for (const faces of coverable()) {
const without = quads(covering(faces));
ways_out(faces).forEach(([face, direction], i) => {
const where = `${faces} arm_${i}`;
const with_arm = quads({ ...covering(faces), [`arm_${i}`]: 1 });
assertEquals(with_arm.length, without.length + 1, where);
assertEquals(arm(without, face, direction), undefined, where);
assertAlmostEquals(range(arm(with_arm, face, direction)!, direction)[1], 1 + HEIGHT, 1e-6, where);
});
});
}
});
Deno.test("the wire's texture lies the way it would on a whole block's faces, so it lines up from wire to wire", async () => {
const { quads } = await wire();
FACES.forEach((side, s) => {
for (let i = 0; i < 4; i++) {
for (const join of [1, 2, 3]) {
for (const quad of quads({ side: s, [`join_${i}`]: join })) {
corners(quad).forEach((point, k) => {
const [u, v] = block_uv(quad.face, point);
const where = `on ${side}, join_${i}=${join}, corner ${point}`;
assertAlmostEquals(quad.uvs[k * 2], u, 1e-6, where);
assertAlmostEquals(quad.uvs[k * 2 + 1], v, 1e-6, where);
});
}
}
for (const faces of coverable()) {
const arms = Object.fromEntries(ways_out(faces).map((_, i) => [`arm_${i}`, 1]));
for (const quad of quads({ ...covering(faces), ...arms })) {
corners(quad).forEach((point, k) => {
const [u, v] = block_uv(quad.face, point);
assertAlmostEquals(quad.uvs[k * 2], u, 1e-6, `${faces}, corner ${point}`);
assertAlmostEquals(quad.uvs[k * 2 + 1], v, 1e-6, `${faces}, corner ${point}`);
});
}
});
}
});
Deno.test("a cell covers faces that join each other, as many as its arm_ states fit", () => {
assert(can_cover(["bottom"]));
assert(can_cover(["bottom", "north"]));
assert(can_cover(["bottom", "north", "east"]));
assert(can_cover(FACES));
assert(!can_cover([]));
// opposite faces have nothing joining them, and around the cell there are too many ways out
assert(!can_cover(["bottom", "top"]));
assert(!can_cover(["bottom", "north", "top"]));
assert(!can_cover(["north", "east", "south", "west"]));
});
Deno.test("a wire shows its powered texture while it has power, and can be pointed at", async () => {
const { block, quads } = await wire();
assertEquals(new Set(quads({ join_0: 3 }).map((quad) => quad.texture)), new Set(["bworld:verdigris_wire"]));
assertEquals(
new Set(quads({ join_0: 3, power: 7 }).map((quad) => quad.texture)),
new Set(["bworld:verdigris_wire_on"]),
);
// a plate around its middle, on whichever side it lies
FACES.forEach((side, s) => {
const shape = block_shape(block, { side: s, join_0: 0, join_1: 0, join_2: 0, join_3: 0, power: 0 });
assertEquals(shape.length, 1, side);
const lit = (power: number) => new Set(quads({ bottom: 1, north: 1, power }).map((quad) => quad.texture));
assertEquals(lit(0), new Set(["bworld:verdigris_wire"]));
assertEquals(lit(7), new Set(["bworld:verdigris_wire_on"]));
// a plate around its middle on each face it covers
for (const face of FACES) {
const states = Object.fromEntries(block.states!.map((state) => [state.name, state.name === face ? 1 : 0]));
const shape = block_shape(block, states);
assertEquals(shape.length, 1, face);
const box = shape[0];
const extent = (face: Face) => {
const reach = [toward(box.slice(0, 3), face), toward(box.slice(3), face)];
const extent = (to: Face) => {
const reach = [toward(box.slice(0, 3), to), toward(box.slice(3), to)];
return [Math.min(...reach), Math.max(...reach)];
};
assertEquals(extent(side).map((v) => Math.round(v * 16)), [15, 16], side);
for (const across of DIRECTIONS[side]) assertEquals(extent(across).map((v) => Math.round(v * 16)), [3, 13]);
});
assertEquals(extent(face).map((v) => Math.round(v * 16)), [15, 16], face);
for (const across of DIRECTIONS[face]) assertEquals(extent(across).map((v) => Math.round(v * 16)), [3, 13]);
}
});
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 };
}
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) {
assertEquals(b.id(...at), WIRE, `${at}`);
for (const other of b.joins(...at)) {
const [x, y, z] = other.split(" ").map(Number);
assert(b.joins(x, y, z).includes(at.join(" ")), `${other} doesn't join ${at} back`);
}
}
}
// the wire from a floor up a wall onto a ceiling: one on the floor at z 2, then the cell at z 1 on the floor and the
// south face of a column at 0 100 0, one more up the column, the cell under the ceiling there on the column and the
// ceiling, and one on the ceiling above z 2. a lever on the floor joins the first
async function floor_wall_ceiling() {
const b = await builder();
for (const [x, y, z] of [[0, 100, 0], [0, 101, 0], [0, 102, 0], [0, 103, 1], [0, 103, 2]]) {
b.t.game.set_block(x, y, z, "bworld:stone");
}
b.use("bworld:verdigris", 0, 99, 2, "top");
b.use("bworld:verdigris", 0, 99, 1, "top");
b.use("bworld:verdigris", 0, 100, 0, "south");
b.use("bworld:verdigris", 0, 101, 0, "south");
b.use("bworld:verdigris", 0, 102, 0, "south");
b.use("bworld:verdigris", 0, 103, 1, "bottom");
b.use("bworld:verdigris", 0, 103, 2, "bottom");
b.use("bworld:lever", 0, 99, 3, "top");
const wires: Pos[] = [[0, 100, 2], [0, 100, 1], [0, 101, 1], [0, 102, 1], [0, 102, 2]];
return { ...b, wires, lever: [0, 100, 3] as Pos };
}
Deno.test("wire placed on another face of a cell with wire joins it there, up a wall and onto a ceiling", async () => {
const b = await floor_wall_ceiling();
assertEquals(b.covers(0, 100, 1), ["north", "bottom"]);
assertEquals(b.covers(0, 102, 1), ["north", "top"]);
joined_both_ways(b, b.wires);
assertEquals(b.joins(0, 100, 1).sort(), ["0 100 2", "0 101 1"]);
assertEquals(b.joins(0, 102, 1).sort(), ["0 101 1", "0 102 2"]);
// a cell can't cover a floor and a ceiling with nothing joining them
b.t.game.set_block(-3, 101, -3, "bworld:stone");
b.use("bworld:verdigris", -3, 99, -3, "top");
b.use("bworld:verdigris", -3, 101, -3, "bottom");
assertEquals(b.covers(-3, 100, -3), ["bottom"]);
});
Deno.test("a wire goes over the edge of a block to the wire on its next face, while nothing's in the way", async () => {
const b = await builder();
// on top of a block, over its north edge, down its north face, and on along the floor
b.t.game.set_block(3, 100, 1, "bworld:stone");
b.use("bworld:verdigris", 3, 100, 1, "top");
b.use("bworld:verdigris", 3, 100, 1, "north");
b.use("bworld:verdigris", 3, 99, 0, "top");
b.use("bworld:verdigris", 3, 99, -1, "top");
assertEquals(b.covers(3, 100, 0), ["south", "bottom"]);
joined_both_ways(b, [[3, 101, 1], [3, 100, 0], [3, 100, -1]]);
assertEquals(b.joins(3, 101, 1), ["3 100 0"]);
assertEquals(b.joins(3, 100, 0).sort(), ["3 100 -1", "3 101 1"]);
// filling the cell the edge goes around lets go of it at both ends
b.t.game.set_block(3, 101, 0, "bworld:stone");
assertEquals([b.joins(3, 101, 1), b.joins(3, 100, 0)], [[], ["3 100 -1"]]);
});
Deno.test("a player sees a wire join the moment they place it, the way the server joins it", async () => {
const b = await builder();
b.t.game.set_block(0, 100, 0, "bworld:stone");
b.t.game.set_block(0, 101, 0, "bworld:stone");
b.t.game.set_block(3, 100, 1, "bworld:stone");
b.use("bworld:verdigris", 0, 99, 1, "top");
b.use("bworld:verdigris", 0, 101, 0, "south");
b.use("bworld:verdigris", 3, 100, 1, "top");
const client = client_of(b.t.game, AREA);
const place = (at: Pos, clicked: Pos, face: Face) => {
client.place(WIRE, ...at, 0, face, [0.5, 0.5, 0.5]);
b.use("bworld:verdigris", ...clicked, face);
};
// beside one on the floor
place([0, 100, 2], [0, 99, 2], "top");
assertEquals(client.blocks(), server_blocks(b.t.game, AREA));
// onto the wall of the cell with the floor wire, joining that and the wire up the wall
place([0, 100, 1], [0, 100, 0], "south");
assertEquals(b.covers(0, 100, 1), ["north", "bottom"]);
assertEquals(b.joins(0, 101, 1), ["0 100 1"]);
assertEquals(client.blocks(), server_blocks(b.t.game, AREA));
// over an edge the wire it joins isn't beside it, so the server tells that one, and sends what it became
b.t.game.tick();
b.t.take(1);
place([3, 100, 0], [3, 100, 1], "north");
const differs = client.blocks().filter((line, i) => line !== server_blocks(b.t.game, AREA)[i]);
assertEquals(differs.map((line) => line.split(" ").slice(0, 3).join(" ")), ["3 101 1"]);
const sent = b.t.take(1).find((m) => m.type === "set_block" && m.x === 3 && m.y === 101 && m.z === 1);
assertEquals(sent?.state, b.t.game.world.get_block_value(3, 101, 1) >>> 16);
});
Deno.test("a lever's power goes all the way along a net, and the net goes off with it", async () => {
const b = await floor_wall_ceiling();
assertEquals(b.wires.map((at) => b.power(...at)), [0, 0, 0, 0, 0]);
b.use(undefined, ...b.lever, "top");
assertEquals(b.states(...b.lever).powered, 1);
assertEquals(b.wires.map((at) => b.power(...at)), [15, 15, 15, 15, 15]);
// the stone they lie on was powered by them, but that doesn't hold them on
b.use(undefined, ...b.lever, "top");
assertEquals(b.wires.map((at) => b.power(...at)), [0, 0, 0, 0, 0]);
});
Deno.test("a wire takes power from what it lies on, and two nets on one block don't power each other", async () => {
const b = await builder();
// a lever on the side of a block powers it, and the wires on its top and underneath through it
b.t.game.set_block(-3, 101, -3, "bworld:stone");
b.use("bworld:verdigris", -3, 101, -3, "top");
b.use("bworld:verdigris", -3, 101, -3, "bottom");
b.use("bworld:lever", -3, 101, -3, "east");
b.use(undefined, -2, 101, -3, "top");
assertEquals([b.power(-3, 102, -3), b.power(-3, 100, -3)], [15, 15]);
// a wire on a verdigris block, and one joined to one
b.t.game.set_block(3, 101, 3, "bworld:verdigris_block");
b.use("bworld:verdigris", 3, 101, 3, "top");
assertEquals(b.power(3, 102, 3), 15);
b.t.game.set_block(3, 101, -4, "bworld:stone");
b.t.game.set_block(4, 102, -4, "bworld:verdigris_block");
b.use("bworld:verdigris", 3, 101, -4, "top");
b.use("bworld:verdigris", 3, 101, -4, "bottom");
assertEquals(b.power(3, 102, -4), 15);
// the wire under that block isn't powered by the net on top of it, which only wire powers it with
assertEquals(b.power(3, 100, -4), 0);
b.t.game.set_block(4, 102, -4, AIR_ID);
assertEquals(b.power(3, 102, -4), 0);
});
// five wires in a row on the floor at z 5, from x -2 to 2, and a lever at the west end, switched on
async function powered_row(save?: WorldSave) {
const b = await builder(save);
const wires: Pos[] = [-2, -1, 0, 1, 2].map((x) => [x, 100, 5]);
if (!save) {
for (const [x] of wires) b.use("bworld:verdigris", x, 99, 5, "top");
b.use("bworld:lever", -3, 99, 5, "top");
b.use(undefined, -3, 100, 5, "top");
}
return { ...b, wires, lever: [-3, 100, 5] as Pos };
}
Deno.test("breaking a wire in the middle of a net leaves the half cut off from the lever without power", async () => {
const b = await powered_row();
assertEquals(b.wires.map((at) => b.power(...at)), [15, 15, 15, 15, 15]);
b.t.game.set_block(0, 100, 5, AIR_ID);
assertEquals([-2, -1, 1, 2].map((x) => b.power(x, 100, 5)), [15, 15, 0, 0]);
});
Deno.test("each wire is sent once when its net's power changes, and not at all when a block beside it is placed", async () => {
const b = await powered_row();
const sent_wires = () =>
b.t.take(1).filter((m) => m.type === "set_block" && m.id === WIRE).map((m) => `${m.x} ${m.z}`);
b.t.game.tick();
b.t.take(1);
b.use(undefined, ...b.lever, "top");
assertEquals(sent_wires().sort(), b.wires.map(([x, , z]) => `${x} ${z}`).sort());
b.use(undefined, ...b.lever, "top");
b.t.take(1);
b.t.game.set_block(0, 100, 6, "bworld:stone");
b.t.game.set_block(1, 101, 5, "bworld:stone");
assertEquals(sent_wires(), []);
assertEquals(b.wires.map((at) => b.power(...at)), [15, 15, 15, 15, 15]);
});
Deno.test("a world saved and loaded has the same nets", async () => {
const b = await powered_row();
const saved = b.t.game.save();
const again = await powered_row(saved);
assertEquals(again.wires.map((at) => again.power(...at)), [15, 15, 15, 15, 15]);
again.use(undefined, ...again.lever, "top");
assertEquals(again.wires.map((at) => again.power(...at)), [0, 0, 0, 0, 0]);
});
Deno.test("wire comes off a face whose block goes, and drops one verdigris for each face it covered", async () => {
const b = await builder();
const verdigris = () =>
b.t.game.item_entities().filter((e) => e.item.type_id === "bworld:verdigris").reduce(
(n, e) => n + e.item.amount,
0,
);
// on a floor and the wall beside it in one cell
b.t.game.set_block(5, 100, 4, "bworld:stone");
b.use("bworld:verdigris", 5, 99, 5, "top");
b.use("bworld:verdigris", 5, 100, 4, "south");
assertEquals(b.covers(5, 100, 5), ["north", "bottom"]);
b.t.game.set_block(5, 100, 4, AIR_ID);
assertEquals(b.covers(5, 100, 5), ["bottom"]);
assertEquals(verdigris(), 1);
b.t.game.set_block(5, 99, 5, AIR_ID);
assertEquals(b.id(5, 100, 5), AIR_ID);
assertEquals(verdigris(), 2);
// broken by hand, a cell drops what it covered
b.t.game.set_block(-5, 100, 4, "bworld:stone");
b.use("bworld:verdigris", -5, 99, 5, "top");
b.use("bworld:verdigris", -5, 100, 4, "south");
b.t.game.destroy_block(-5, 100, 5);
assertEquals(verdigris(), 4);
});
Deno.test("a wire or lever only goes on something to attach to, and a lever breaks when it goes", async () => {
const b = await builder();
// on a wall it points out of the wall, on a floor or ceiling it lies or hangs there
b.t.game.set_block(5, 101, -5, "bworld:stone");
b.use("bworld:lever", 5, 101, -5, "south");
assertEquals([b.states(5, 101, -4).face, b.states(5, 101, -4).facing], [1, FACES.indexOf("south")]);
b.use("bworld:lever", 5, 101, -5, "bottom");
assertEquals(b.states(5, 100, -5).face, 2);
b.use("bworld:lever", 4, 99, -5, "top");
assertEquals(b.states(4, 100, -5).face, 0);
b.t.game.set_block(5, 101, -5, AIR_ID);
assertEquals([b.id(5, 101, -4), b.id(5, 100, -5)], [AIR_ID, AIR_ID]);
assertEquals(b.id(4, 100, -5), "bworld:lever");
// a fence's top isn't something to lie on
b.t.game.set_block(-5, 100, -5, "bworld:oak_fence");
b.use("bworld:verdigris", -5, 100, -5, "top");
assertEquals(b.id(-5, 101, -5), AIR_ID);
});