// 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 face of // its cell it can cover, pointing each way that face's directions go (DIRECTIONS in sides.ts). a way out of the cell // is an arm_ state whose meaning depends on which faces the cell covers (ways_out in joins.ts), so there's a part for // each of those for every set of faces a cell can cover, too many to keep right by hand. run it after changing // anything here, the directions or the ways out: // // 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 } from "@/common/block_models.ts"; import type { ModelFace } from "@/common/mod_data.ts"; import type { Face } from "@/common/mod_api/shared.ts"; import { can_cover, ways_out } from "@/mods/bworld/scripts/verdigris/joins.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 wire_face = { texture: "#wire" }; // the pieces of the wire on a cell's floor. 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: wire_face } }], // to the edge of its cell, where it meets the wire on the cell's face that way arm: [{ from: [A, 0, 0], to: [B, HEIGHT, A], faces: { top: wire_face } }], // a way out of its cell it joins: a little past the edge, so going around the edge of the block it lies on it meets // the wire on that block's next face, and straight on it meets the next cell's out: [{ from: [A, 0, -HEIGHT], to: [B, HEIGHT, A], faces: { top: wire_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", "out"]) 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 the wire covering face of its cell, pointing toward direction: turned // around the middle of the block so down goes to face and north to direction. clamped to the block, like model_shape function placed(elements: ModelElementJson[], face: Face, direction: Face) { const down = FACE_OFFSETS[face]; 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, face: Face, direction: Face) { const wanted = placed(PIECES[piece], face, 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 ${face} pointing ${direction}`); } const parts: Record[] = []; for (const covered of FACES) { parts.push({ when: `${covered}=1`, ...placement("center", covered, DIRECTIONS[covered][0]), shape: CENTER_SHAPE }); } // two faces that meet inside the cell join at their edge for (const covered of FACES) { for (const direction of DIRECTIONS[covered]) { parts.push({ when: `${covered}=1,${direction}=1`, ...placement("arm", covered, direction) }); } } // the ways out of the cell it joins, for every set of faces a cell can cover for (let set = 1; set < 1 << FACES.length; set++) { const faces = FACES.filter((_, i) => set & (1 << i)); if (!can_cover(faces)) continue; const covering = FACES.map((covered) => `${covered}=${faces.includes(covered) ? 1 : 0}`).join(","); ways_out(faces).forEach(([covered, direction], i) => { parts.push({ when: `${covering},arm_${i}=1`, ...placement("out", covered, 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`);