// SPDX-FileCopyrightText: 2026 Paula Wisnievski // SPDX-License-Identifier: AGPL-3.0-or-later // writes the verdigris wire's look: its pieces as models, and the block's parts, which put each piece on every side a // wire can lie on, pointing each way that side's directions go (DIRECTIONS in sides.ts). that's a part per side, // direction and kind of join, too many to keep right by hand. run it after changing anything here or the directions: // // deno run -A tools/verdigris_wire_model.ts // // the parts lock their textures to the world (uvlock), so every piece shows the part of the texture under it, the // texture's line running north to south or its line running east to west, and it lines up from one wire to the next import { model_shape } from "$/common/block_shapes.ts"; import type { ModelElementJson, ModelFace } from "$/common/block_models.ts"; import type { Face } from "$/common/mod_api/shared.ts"; import { DIRECTIONS, FACE_OFFSETS, FACES } from "$/mods/bworld/scripts/verdigris/sides.ts"; const MODELS = "mods/bworld/models"; const BLOCK = "mods/bworld/blocks/verdigris_wire.json"; // how wide the wire is and how far it stands off the face it lies on, in pixels. the texture's lines are as wide const WIDTH = 2; const HEIGHT = 0.25; const [A, B] = [8 - WIDTH / 2, 8 + WIDTH / 2]; // what's outlined when the wire is pointed at: a plate around its middle, since the pieces are too thin to aim at const CENTER_SHAPE = [[3, 0, 3, 13, 1, 13]]; const face = { texture: "#wire" }; // a floor wire's pieces. the ones that point a way point north. their faces take their texture from where they are, // so they have no uv const PIECES: Record = { // always there, so the arms meet center: [{ from: [A, 0, A], to: [B, HEIGHT, B], faces: { top: face } }], // join 1, straight on: to the edge of its cell arm: [{ from: [A, 0, 0], to: [B, HEIGHT, A], faces: { top: face } }], // join 2, over an edge: a little past it, to meet the wire around the corner on the block's other face edge: [{ from: [A, 0, -HEIGHT], to: [B, HEIGHT, A], faces: { top: face } }], // join 3, into a corner: then up the face it runs into, to the wire on that face in the cell above corner: [ { from: [A, 0, 0], to: [B, HEIGHT, A], faces: { top: face } }, { from: [A, 0, 0], to: [B, 16, HEIGHT], faces: { south: face } }, ], }; // the faces a quarter turn clockwise seen from above takes each face to const TURNED_EAST: Record = { north: "east", east: "south", south: "west", west: "north", top: "top", bottom: "bottom", }; // a piece turned to point east. a part turns by x then y, which can't point a piece on a wall sideways, so the ones // that point a way have a copy that already does function pointing_east(elements: ModelElementJson[]): ModelElementJson[] { return elements.map(({ from, to, faces }) => ({ from: [16 - to[2], from[1], from[0]], to: [16 - from[2], to[1], to[0]], faces: Object.fromEntries(Object.entries(faces).map(([side, f]) => [TURNED_EAST[side as ModelFace], f])), })); } const models: Record = { ...PIECES }; for (const name of ["arm", "edge", "corner"]) models[`${name}_east`] = pointing_east(PIECES[name]); type Vec = readonly [number, number, number]; const cross = ( a: Vec, b: Vec, ): Vec => [a[1] * b[2] - a[2] * b[1], a[2] * b[0] - a[0] * b[2], a[0] * b[1] - a[1] * b[0]]; // boxes in the 0 to 1 space shapes are in, rounded and in order, to tell two shapes are the same function shape_key(boxes: readonly (readonly number[])[]) { return JSON.stringify(boxes.map((box) => box.map((v) => Math.round(v * 1e6) / 1e6)).sort()); } // the boxes a floor piece pointing north has on a wire lying on the block toward side, pointing toward direction: turned // around the middle of the block so down goes to side and north to direction. clamped to the block, like model_shape function placed(elements: ModelElementJson[], side: Face, direction: Face) { const down = FACE_OFFSETS[side]; const north = FACE_OFFSETS[direction]; const east = cross(down, north); const turn = ([x, y, z]: readonly number[]) => [0, 1, 2].map((i) => 8 + (x - 8) * east[i] - (y - 8) * down[i] - (z - 8) * north[i]); return shape_key(elements.map(({ from, to }) => { const [a, b] = [turn(from), turn(to)]; const clamp = (v: number) => Math.min(Math.max(v / 16, 0), 1); return [...a.map((v, i) => clamp(Math.min(v, b[i]))), ...a.map((v, i) => clamp(Math.max(v, b[i])))]; })); } // the model and turns that put a piece there, the piece pointing north if it can, then its copy pointing east function placement(piece: string, side: Face, direction: Face) { const wanted = placed(PIECES[piece], side, direction); for (const name of piece === "center" ? [piece] : [piece, `${piece}_east`]) { for (const x of [0, 90, 180, 270]) { for (const y of [0, 90, 180, 270]) { if (shape_key(model_shape({ id: name, elements: models[name] }, y, x)) !== wanted) continue; return { model: `bworld:verdigris_wire_${name}`, ...(x && { x }), ...(y && { y }), uvlock: true }; } } } throw new Error(`no turn puts the ${piece} on the ${side} pointing ${direction}`); } const JOINS = [[1, "arm"], [2, "edge"], [3, "corner"]] as const; const parts: Record[] = []; FACES.forEach((side, s) => { parts.push({ when: `side=${s}`, ...placement("center", side, DIRECTIONS[side][0]), shape: CENTER_SHAPE }); DIRECTIONS[side].forEach((direction, i) => { for (const [join, piece] of JOINS) { parts.push({ when: `side=${s},join_${i}=${join}`, ...placement(piece, side, direction) }); } }); }); const written: string[] = []; const write = (path: string, json: unknown) => { // lists of numbers on one line, like the models written by hand const text = JSON.stringify(json, null, "\t").replace( /\[\s*(-?[\d.]+(?:,\s*-?[\d.]+)*)\s*\]/g, (_, numbers: string) => `[${numbers.split(/,\s*/).join(", ")}]`, ); Deno.writeTextFileSync(path, text + "\n"); written.push(path); }; for (const [name, elements] of Object.entries(models)) { write(`${MODELS}/verdigris_wire_${name}.json`, { format_version: 1, model: { id: `bworld:verdigris_wire_${name}`, elements }, }); } const block = JSON.parse(Deno.readTextFileSync(BLOCK)); block.block.parts = parts; write(BLOCK, block); await new Deno.Command("deno", { args: ["fmt", "--quiet", ...written] }).output(); console.log(`wrote ${written.length - 1} models and ${parts.length} parts`);