// 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 { client 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 = {}) { 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("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 overstressed = (x: number, y: number, z: number) => game.world.get_tile(x, y, z)?.client_data?.overstressed === true; 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("blocks", "create:hand_crank")!.components!["create:hand_crank"] as { speed: number; }).speed; return { ...t, move, place, speed, overstressed, state, use, ticks, crank }; } // create's client phase 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. and the // items and fluid boxes it draws now, by item and fluid id export async function visual_for( id: string, states: Record, data: Record, [x, y, z] = [0, 100, 0], ) { await test_game("mods"); const visuals = new Map Visual>(); setup_client({ blocks: { components: (id: string) => structuredClone(EverythingRegistry.get("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(); const transforms = new Map(); const items = new Set<{ id: string; transform?: number[] }>(); const fluids = new Set<{ id: string; box?: [number[], 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) => transforms.set(key(model), Array.from(matrix)), delete() {}, }), item: (stack: { id: string }) => { const item: { id: string; transform?: number[] } = { id: stack.id }; items.add(item); return { set_transform: (matrix: ArrayLike) => item.transform = Array.from(matrix), delete: () => items.delete(item), }; }, fluid: (fluid: { id: string }) => { const box: { id: string; box?: [number[], number[]] } = { id: fluid.id }; fluids.add(box); return { set_box: (from: number[], to: number[]) => box.box = [from, to], delete: () => fluids.delete(box), }; }, } 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, items, fluids, data }; }