Files
bworld/tests/create/helpers.ts
T
2026-10-15 13:07:09 -03:00

84 lines
3.9 KiB
TypeScript

// SPDX-FileCopyrightText: 2026 Paula Wisnievski
// SPDX-License-Identifier: AGPL-3.0-or-later
import { type BlockRegistry, EverythingRegistry } from "$/common/everything_registry.ts";
import { AIR_ID } from "$/common/protocol.ts";
import { get_state_value, set_state_value } from "$/common/utils.ts";
import type { Face } from "$/common/mod_api/shared.ts";
import type { ClientContext, Instances, ModelRef, Visual, VisualBlock } from "$/common/mod_api/client.ts";
import { setup as setup_client } from "$/mods/create/scripts/client.ts";
import { stone_floor, test_game } from "../helpers.ts";
// facing and axis states, in the order the importer numbers them
export const FACING = { north: 0, east: 1, south: 2, west: 3, up: 4, down: 5 };
export const AXIS = { x: 0, y: 1, z: 2 };
// alice standing south of 0 100 0 on a stone floor, looking north, with nothing in her hands. config changes create's
export async function setup(config: Record<string, unknown> = {}) {
const t = await test_game("mods", undefined, undefined, { create: config });
const { game } = t;
t.join(1, "alice");
stone_floor(game, 6);
for (let x = -6; x <= 6; x++) {
for (let z = -6; z <= 6; z++) for (let y = 100; y < 104; y++) game.set_block(x, y, z, AIR_ID);
}
const move = (sneaking = false, pitch = 0) =>
t.send(1, { type: "move", x: 0.5, y: 100, z: 2.5, yaw: 0, pitch, ...(sneaking && { sneaking }) });
move();
// a block with one state set, the way a player would have placed it
const place = (x: number, y: number, z: number, id: string, state?: string, value = 0) => {
const info = EverythingRegistry.get<BlockRegistry>("blocks", id)!;
game.set_block(x, y, z, id, undefined, state ? set_state_value(0, info, state, value)! >>> 16 : 0);
};
const speed = (x: number, y: number, z: number) => game.world.get_tile(x, y, z)?.client_data?.speed as number ?? 0;
const state = (x: number, y: number, z: number, name: string) =>
get_state_value(game.world.get_block_value(x, y, z), game.world.get_block_info(x, y, z)!, name);
const use = (x: number, y: number, z: number, face: Face = "south") =>
t.send(1, { type: "use_block", x, y, z, face, hit: [0.5, 0.5, 1] });
const ticks = (n: number) => {
for (let i = 0; i < n; i++) game.tick();
};
// the rpm a crank turns at
const crank =
(EverythingRegistry.get<BlockRegistry>("blocks", "create:hand_crank")!.components!["create:hand_crank"] as {
speed: number;
}).speed;
return { ...t, move, place, speed, state, use, ticks, crank };
}
// create's client script with only what its visuals need, and a block for one to draw that records what its instances
// do, by model ("own" for the block's own model): how rotating ones turn, and where transformed ones are moved
export async function visual_for(
id: string,
states: Record<string, number>,
data: Record<string, unknown>,
[x, y, z] = [0, 100, 0],
) {
await test_game("mods");
const visuals = new Map<string, (block: VisualBlock, instances: Instances) => Visual>();
setup_client({
blocks: {
components: (id: string) =>
structuredClone(EverythingRegistry.get<BlockRegistry>("blocks", id)!.components),
},
render: { register_visual: (id: string, create: never) => visuals.set(id, create) },
} as unknown as ClientContext);
const key = (model?: ModelRef) => model === undefined ? "own" : JSON.stringify(model);
const turns = new Map<string, [string, number, number]>();
const transforms = new Map<string, number[]>();
const instances = {
rotating: (model?: ModelRef) => ({
set_rotation: (axis: string, rpm: number, offset = 0) => turns.set(key(model), [axis, rpm, offset]),
delete() {},
}),
transformed: (model?: ModelRef) => ({
set_transform: (matrix: ArrayLike<number>) => transforms.set(key(model), Array.from(matrix)),
delete() {},
}),
} as unknown as Instances;
const block = { id, x, y, z, states, get_client_data: (field: string) => structuredClone(data[field]) as never };
const visual = visuals.get(id)!(block, instances);
return { visual, turns, transforms, data };
}