Files
bworld/tests/verdigris.ts
T
2026-10-22 16:31:09 -03:00

66 lines
3.0 KiB
TypeScript

// 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>>;