From 0d80e85dec2606c49936399cfb654f2669b18c89 Mon Sep 17 00:00:00 2001 From: Paula Date: Thu, 22 Oct 2026 12:57:05 -0300 Subject: [PATCH] Verdigris wire model --- mods/bworld/blocks/verdigris_wire.json | 586 ++++++++++++++++++ mods/bworld/models/verdigris_wire_arm.json | 17 + .../models/verdigris_wire_arm_east.json | 17 + mods/bworld/models/verdigris_wire_center.json | 17 + mods/bworld/models/verdigris_wire_corner.json | 26 + .../models/verdigris_wire_corner_east.json | 26 + mods/bworld/models/verdigris_wire_edge.json | 17 + .../models/verdigris_wire_edge_east.json | 17 + mods/bworld/scripts/verdigris/sides.ts | 30 + mods/bworld/textures/verdigris_wire.png | Bin 0 -> 97 bytes mods/bworld/textures/verdigris_wire_on.png | Bin 0 -> 97 bytes tests/verdigris_wire_test.ts | 149 +++++ tools/verdigris_wire_model.ts | 138 +++++ 13 files changed, 1040 insertions(+) create mode 100644 mods/bworld/blocks/verdigris_wire.json create mode 100644 mods/bworld/models/verdigris_wire_arm.json create mode 100644 mods/bworld/models/verdigris_wire_arm_east.json create mode 100644 mods/bworld/models/verdigris_wire_center.json create mode 100644 mods/bworld/models/verdigris_wire_corner.json create mode 100644 mods/bworld/models/verdigris_wire_corner_east.json create mode 100644 mods/bworld/models/verdigris_wire_edge.json create mode 100644 mods/bworld/models/verdigris_wire_edge_east.json create mode 100644 mods/bworld/scripts/verdigris/sides.ts create mode 100644 mods/bworld/textures/verdigris_wire.png create mode 100644 mods/bworld/textures/verdigris_wire_on.png create mode 100644 tests/verdigris_wire_test.ts create mode 100644 tools/verdigris_wire_model.ts diff --git a/mods/bworld/blocks/verdigris_wire.json b/mods/bworld/blocks/verdigris_wire.json new file mode 100644 index 0000000..5f2693b --- /dev/null +++ b/mods/bworld/blocks/verdigris_wire.json @@ -0,0 +1,586 @@ +{ + "format_version": 1, + "block": { + "id": "bworld:verdigris_wire", + "textures": { + "wire": "bworld:verdigris_wire_on" + }, + "render_layer": "cutout", + "collision": false, + "mining": { + "toughness": 0 + }, + "item": false, + "states": [ + { + "name": "side", + "bits": 3, + "default": 5 + }, + { + "name": "join_0", + "bits": 2, + "default": 0 + }, + { + "name": "join_1", + "bits": 2, + "default": 0 + }, + { + "name": "join_2", + "bits": 2, + "default": 0 + }, + { + "name": "join_3", + "bits": 2, + "default": 0 + }, + { + "name": "power", + "bits": 4, + "default": 0 + } + ], + "variants": { + "power=0": { + "textures": { + "wire": "bworld:verdigris_wire" + } + } + }, + "parts": [ + { + "when": "side=0", + "model": "bworld:verdigris_wire_center", + "x": 90, + "y": 180, + "uvlock": true, + "shape": [ + [3, 0, 3, 13, 1, 13] + ] + }, + { + "when": "side=0,join_0=1", + "model": "bworld:verdigris_wire_arm", + "x": 90, + "y": 180, + "uvlock": true + }, + { + "when": "side=0,join_0=2", + "model": "bworld:verdigris_wire_edge", + "x": 90, + "y": 180, + "uvlock": true + }, + { + "when": "side=0,join_0=3", + "model": "bworld:verdigris_wire_corner", + "x": 90, + "y": 180, + "uvlock": true + }, + { + "when": "side=0,join_1=1", + "model": "bworld:verdigris_wire_arm_east", + "x": 90, + "y": 180, + "uvlock": true + }, + { + "when": "side=0,join_1=2", + "model": "bworld:verdigris_wire_edge_east", + "x": 90, + "y": 180, + "uvlock": true + }, + { + "when": "side=0,join_1=3", + "model": "bworld:verdigris_wire_corner_east", + "x": 90, + "y": 180, + "uvlock": true + }, + { + "when": "side=0,join_2=1", + "model": "bworld:verdigris_wire_arm", + "x": 270, + "uvlock": true + }, + { + "when": "side=0,join_2=2", + "model": "bworld:verdigris_wire_edge", + "x": 270, + "uvlock": true + }, + { + "when": "side=0,join_2=3", + "model": "bworld:verdigris_wire_corner", + "x": 270, + "uvlock": true + }, + { + "when": "side=0,join_3=1", + "model": "bworld:verdigris_wire_arm_east", + "x": 270, + "uvlock": true + }, + { + "when": "side=0,join_3=2", + "model": "bworld:verdigris_wire_edge_east", + "x": 270, + "uvlock": true + }, + { + "when": "side=0,join_3=3", + "model": "bworld:verdigris_wire_corner_east", + "x": 270, + "uvlock": true + }, + { + "when": "side=1", + "model": "bworld:verdigris_wire_center", + "x": 90, + "y": 270, + "uvlock": true, + "shape": [ + [3, 0, 3, 13, 1, 13] + ] + }, + { + "when": "side=1,join_0=1", + "model": "bworld:verdigris_wire_arm", + "x": 90, + "y": 270, + "uvlock": true + }, + { + "when": "side=1,join_0=2", + "model": "bworld:verdigris_wire_edge", + "x": 90, + "y": 270, + "uvlock": true + }, + { + "when": "side=1,join_0=3", + "model": "bworld:verdigris_wire_corner", + "x": 90, + "y": 270, + "uvlock": true + }, + { + "when": "side=1,join_1=1", + "model": "bworld:verdigris_wire_arm_east", + "x": 90, + "y": 270, + "uvlock": true + }, + { + "when": "side=1,join_1=2", + "model": "bworld:verdigris_wire_edge_east", + "x": 90, + "y": 270, + "uvlock": true + }, + { + "when": "side=1,join_1=3", + "model": "bworld:verdigris_wire_corner_east", + "x": 90, + "y": 270, + "uvlock": true + }, + { + "when": "side=1,join_2=1", + "model": "bworld:verdigris_wire_arm", + "x": 270, + "y": 90, + "uvlock": true + }, + { + "when": "side=1,join_2=2", + "model": "bworld:verdigris_wire_edge", + "x": 270, + "y": 90, + "uvlock": true + }, + { + "when": "side=1,join_2=3", + "model": "bworld:verdigris_wire_corner", + "x": 270, + "y": 90, + "uvlock": true + }, + { + "when": "side=1,join_3=1", + "model": "bworld:verdigris_wire_arm_east", + "x": 270, + "y": 90, + "uvlock": true + }, + { + "when": "side=1,join_3=2", + "model": "bworld:verdigris_wire_edge_east", + "x": 270, + "y": 90, + "uvlock": true + }, + { + "when": "side=1,join_3=3", + "model": "bworld:verdigris_wire_corner_east", + "x": 270, + "y": 90, + "uvlock": true + }, + { + "when": "side=2", + "model": "bworld:verdigris_wire_center", + "x": 90, + "uvlock": true, + "shape": [ + [3, 0, 3, 13, 1, 13] + ] + }, + { + "when": "side=2,join_0=1", + "model": "bworld:verdigris_wire_arm", + "x": 90, + "uvlock": true + }, + { + "when": "side=2,join_0=2", + "model": "bworld:verdigris_wire_edge", + "x": 90, + "uvlock": true + }, + { + "when": "side=2,join_0=3", + "model": "bworld:verdigris_wire_corner", + "x": 90, + "uvlock": true + }, + { + "when": "side=2,join_1=1", + "model": "bworld:verdigris_wire_arm_east", + "x": 90, + "uvlock": true + }, + { + "when": "side=2,join_1=2", + "model": "bworld:verdigris_wire_edge_east", + "x": 90, + "uvlock": true + }, + { + "when": "side=2,join_1=3", + "model": "bworld:verdigris_wire_corner_east", + "x": 90, + "uvlock": true + }, + { + "when": "side=2,join_2=1", + "model": "bworld:verdigris_wire_arm", + "x": 270, + "y": 180, + "uvlock": true + }, + { + "when": "side=2,join_2=2", + "model": "bworld:verdigris_wire_edge", + "x": 270, + "y": 180, + "uvlock": true + }, + { + "when": "side=2,join_2=3", + "model": "bworld:verdigris_wire_corner", + "x": 270, + "y": 180, + "uvlock": true + }, + { + "when": "side=2,join_3=1", + "model": "bworld:verdigris_wire_arm_east", + "x": 270, + "y": 180, + "uvlock": true + }, + { + "when": "side=2,join_3=2", + "model": "bworld:verdigris_wire_edge_east", + "x": 270, + "y": 180, + "uvlock": true + }, + { + "when": "side=2,join_3=3", + "model": "bworld:verdigris_wire_corner_east", + "x": 270, + "y": 180, + "uvlock": true + }, + { + "when": "side=3", + "model": "bworld:verdigris_wire_center", + "x": 90, + "y": 90, + "uvlock": true, + "shape": [ + [3, 0, 3, 13, 1, 13] + ] + }, + { + "when": "side=3,join_0=1", + "model": "bworld:verdigris_wire_arm", + "x": 90, + "y": 90, + "uvlock": true + }, + { + "when": "side=3,join_0=2", + "model": "bworld:verdigris_wire_edge", + "x": 90, + "y": 90, + "uvlock": true + }, + { + "when": "side=3,join_0=3", + "model": "bworld:verdigris_wire_corner", + "x": 90, + "y": 90, + "uvlock": true + }, + { + "when": "side=3,join_1=1", + "model": "bworld:verdigris_wire_arm_east", + "x": 90, + "y": 90, + "uvlock": true + }, + { + "when": "side=3,join_1=2", + "model": "bworld:verdigris_wire_edge_east", + "x": 90, + "y": 90, + "uvlock": true + }, + { + "when": "side=3,join_1=3", + "model": "bworld:verdigris_wire_corner_east", + "x": 90, + "y": 90, + "uvlock": true + }, + { + "when": "side=3,join_2=1", + "model": "bworld:verdigris_wire_arm", + "x": 270, + "y": 270, + "uvlock": true + }, + { + "when": "side=3,join_2=2", + "model": "bworld:verdigris_wire_edge", + "x": 270, + "y": 270, + "uvlock": true + }, + { + "when": "side=3,join_2=3", + "model": "bworld:verdigris_wire_corner", + "x": 270, + "y": 270, + "uvlock": true + }, + { + "when": "side=3,join_3=1", + "model": "bworld:verdigris_wire_arm_east", + "x": 270, + "y": 270, + "uvlock": true + }, + { + "when": "side=3,join_3=2", + "model": "bworld:verdigris_wire_edge_east", + "x": 270, + "y": 270, + "uvlock": true + }, + { + "when": "side=3,join_3=3", + "model": "bworld:verdigris_wire_corner_east", + "x": 270, + "y": 270, + "uvlock": true + }, + { + "when": "side=4", + "model": "bworld:verdigris_wire_center", + "x": 180, + "uvlock": true, + "shape": [ + [3, 0, 3, 13, 1, 13] + ] + }, + { + "when": "side=4,join_0=1", + "model": "bworld:verdigris_wire_arm", + "x": 180, + "uvlock": true + }, + { + "when": "side=4,join_0=2", + "model": "bworld:verdigris_wire_edge", + "x": 180, + "uvlock": true + }, + { + "when": "side=4,join_0=3", + "model": "bworld:verdigris_wire_corner", + "x": 180, + "uvlock": true + }, + { + "when": "side=4,join_1=1", + "model": "bworld:verdigris_wire_arm", + "x": 180, + "y": 270, + "uvlock": true + }, + { + "when": "side=4,join_1=2", + "model": "bworld:verdigris_wire_edge", + "x": 180, + "y": 270, + "uvlock": true + }, + { + "when": "side=4,join_1=3", + "model": "bworld:verdigris_wire_corner", + "x": 180, + "y": 270, + "uvlock": true + }, + { + "when": "side=4,join_2=1", + "model": "bworld:verdigris_wire_arm", + "x": 180, + "y": 180, + "uvlock": true + }, + { + "when": "side=4,join_2=2", + "model": "bworld:verdigris_wire_edge", + "x": 180, + "y": 180, + "uvlock": true + }, + { + "when": "side=4,join_2=3", + "model": "bworld:verdigris_wire_corner", + "x": 180, + "y": 180, + "uvlock": true + }, + { + "when": "side=4,join_3=1", + "model": "bworld:verdigris_wire_arm", + "x": 180, + "y": 90, + "uvlock": true + }, + { + "when": "side=4,join_3=2", + "model": "bworld:verdigris_wire_edge", + "x": 180, + "y": 90, + "uvlock": true + }, + { + "when": "side=4,join_3=3", + "model": "bworld:verdigris_wire_corner", + "x": 180, + "y": 90, + "uvlock": true + }, + { + "when": "side=5", + "model": "bworld:verdigris_wire_center", + "uvlock": true, + "shape": [ + [3, 0, 3, 13, 1, 13] + ] + }, + { + "when": "side=5,join_0=1", + "model": "bworld:verdigris_wire_arm", + "uvlock": true + }, + { + "when": "side=5,join_0=2", + "model": "bworld:verdigris_wire_edge", + "uvlock": true + }, + { + "when": "side=5,join_0=3", + "model": "bworld:verdigris_wire_corner", + "uvlock": true + }, + { + "when": "side=5,join_1=1", + "model": "bworld:verdigris_wire_arm", + "y": 90, + "uvlock": true + }, + { + "when": "side=5,join_1=2", + "model": "bworld:verdigris_wire_edge", + "y": 90, + "uvlock": true + }, + { + "when": "side=5,join_1=3", + "model": "bworld:verdigris_wire_corner", + "y": 90, + "uvlock": true + }, + { + "when": "side=5,join_2=1", + "model": "bworld:verdigris_wire_arm", + "y": 180, + "uvlock": true + }, + { + "when": "side=5,join_2=2", + "model": "bworld:verdigris_wire_edge", + "y": 180, + "uvlock": true + }, + { + "when": "side=5,join_2=3", + "model": "bworld:verdigris_wire_corner", + "y": 180, + "uvlock": true + }, + { + "when": "side=5,join_3=1", + "model": "bworld:verdigris_wire_arm", + "y": 270, + "uvlock": true + }, + { + "when": "side=5,join_3=2", + "model": "bworld:verdigris_wire_edge", + "y": 270, + "uvlock": true + }, + { + "when": "side=5,join_3=3", + "model": "bworld:verdigris_wire_corner", + "y": 270, + "uvlock": true + } + ] + } +} diff --git a/mods/bworld/models/verdigris_wire_arm.json b/mods/bworld/models/verdigris_wire_arm.json new file mode 100644 index 0000000..10837f3 --- /dev/null +++ b/mods/bworld/models/verdigris_wire_arm.json @@ -0,0 +1,17 @@ +{ + "format_version": 1, + "model": { + "id": "bworld:verdigris_wire_arm", + "elements": [ + { + "from": [7, 0, 0], + "to": [9, 0.25, 7], + "faces": { + "top": { + "texture": "#wire" + } + } + } + ] + } +} diff --git a/mods/bworld/models/verdigris_wire_arm_east.json b/mods/bworld/models/verdigris_wire_arm_east.json new file mode 100644 index 0000000..e4d1264 --- /dev/null +++ b/mods/bworld/models/verdigris_wire_arm_east.json @@ -0,0 +1,17 @@ +{ + "format_version": 1, + "model": { + "id": "bworld:verdigris_wire_arm_east", + "elements": [ + { + "from": [9, 0, 7], + "to": [16, 0.25, 9], + "faces": { + "top": { + "texture": "#wire" + } + } + } + ] + } +} diff --git a/mods/bworld/models/verdigris_wire_center.json b/mods/bworld/models/verdigris_wire_center.json new file mode 100644 index 0000000..76b1810 --- /dev/null +++ b/mods/bworld/models/verdigris_wire_center.json @@ -0,0 +1,17 @@ +{ + "format_version": 1, + "model": { + "id": "bworld:verdigris_wire_center", + "elements": [ + { + "from": [7, 0, 7], + "to": [9, 0.25, 9], + "faces": { + "top": { + "texture": "#wire" + } + } + } + ] + } +} diff --git a/mods/bworld/models/verdigris_wire_corner.json b/mods/bworld/models/verdigris_wire_corner.json new file mode 100644 index 0000000..000235d --- /dev/null +++ b/mods/bworld/models/verdigris_wire_corner.json @@ -0,0 +1,26 @@ +{ + "format_version": 1, + "model": { + "id": "bworld:verdigris_wire_corner", + "elements": [ + { + "from": [7, 0, 0], + "to": [9, 0.25, 7], + "faces": { + "top": { + "texture": "#wire" + } + } + }, + { + "from": [7, 0, 0], + "to": [9, 16, 0.25], + "faces": { + "south": { + "texture": "#wire" + } + } + } + ] + } +} diff --git a/mods/bworld/models/verdigris_wire_corner_east.json b/mods/bworld/models/verdigris_wire_corner_east.json new file mode 100644 index 0000000..95e52f4 --- /dev/null +++ b/mods/bworld/models/verdigris_wire_corner_east.json @@ -0,0 +1,26 @@ +{ + "format_version": 1, + "model": { + "id": "bworld:verdigris_wire_corner_east", + "elements": [ + { + "from": [9, 0, 7], + "to": [16, 0.25, 9], + "faces": { + "top": { + "texture": "#wire" + } + } + }, + { + "from": [15.75, 0, 7], + "to": [16, 16, 9], + "faces": { + "west": { + "texture": "#wire" + } + } + } + ] + } +} diff --git a/mods/bworld/models/verdigris_wire_edge.json b/mods/bworld/models/verdigris_wire_edge.json new file mode 100644 index 0000000..59bbc7f --- /dev/null +++ b/mods/bworld/models/verdigris_wire_edge.json @@ -0,0 +1,17 @@ +{ + "format_version": 1, + "model": { + "id": "bworld:verdigris_wire_edge", + "elements": [ + { + "from": [7, 0, -0.25], + "to": [9, 0.25, 7], + "faces": { + "top": { + "texture": "#wire" + } + } + } + ] + } +} diff --git a/mods/bworld/models/verdigris_wire_edge_east.json b/mods/bworld/models/verdigris_wire_edge_east.json new file mode 100644 index 0000000..fb25ff7 --- /dev/null +++ b/mods/bworld/models/verdigris_wire_edge_east.json @@ -0,0 +1,17 @@ +{ + "format_version": 1, + "model": { + "id": "bworld:verdigris_wire_edge_east", + "elements": [ + { + "from": [9, 0, 7], + "to": [16.25, 0.25, 9], + "faces": { + "top": { + "texture": "#wire" + } + } + } + ] + } +} diff --git a/mods/bworld/scripts/verdigris/sides.ts b/mods/bworld/scripts/verdigris/sides.ts new file mode 100644 index 0000000..4414bd1 --- /dev/null +++ b/mods/bworld/scripts/verdigris/sides.ts @@ -0,0 +1,30 @@ +// SPDX-FileCopyrightText: 2026 Paula Wisnievski +// SPDX-License-Identifier: AGPL-3.0-or-later + +// faces, and the directions along them a wire lying on one runs, see REDSTONE.md +import type { Face } from "bworld/shared"; + +// the six faces in the order of a 3 bit facing state's values: a 2 bit facing's four, then top and bottom +export const FACES: readonly Face[] = ["north", "east", "south", "west", "top", "bottom"]; + +// one block toward each face +export const FACE_OFFSETS: Readonly> = { + north: [0, 0, -1], + east: [1, 0, 0], + south: [0, 0, 1], + west: [-1, 0, 0], + top: [0, 1, 0], + bottom: [0, -1, 0], +}; + +// the 4 directions along a wire lying on the block toward each face, as join_0 to join_3 number them. they're the same +// on every face: a floor wire's north, east, south and west, turned the way the floor turns onto that face, so a wire +// is drawn the same few pieces turned by its side. on a wall 0 is always down and 2 up +export const DIRECTIONS: Readonly> = { + north: ["bottom", "west", "top", "east"], + east: ["bottom", "north", "top", "south"], + south: ["bottom", "east", "top", "west"], + west: ["bottom", "south", "top", "north"], + top: ["south", "east", "north", "west"], + bottom: ["north", "east", "south", "west"], +}; diff --git a/mods/bworld/textures/verdigris_wire.png b/mods/bworld/textures/verdigris_wire.png new file mode 100644 index 0000000000000000000000000000000000000000..4be1d251e173d0cc7a23d6fdf8af587674f62f93 GIT binary patch literal 97 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!61|;P_|4#%`8lEnWAr*6y6AtkIb2jul;BtVW uDd7r(mc)S*4hpj}-Z42Pm?-jcGBeC = { + north: "south", + south: "north", + east: "west", + west: "east", + top: "bottom", + bottom: "top", +}; + +async function wire() { + await test_game("mods"); + const block = EverythingRegistry.get("blocks", "bworld:verdigris_wire")!; + const models = Object.fromEntries(EverythingRegistry.entries("models")); + const value = (states: Record) => { + let value = default_block_value(0, block); + for (const [name, state] of Object.entries(states)) value = set_state_value(value, block, name, state)!; + return value; + }; + return { + block, + quads: (states: Record) => bake_block(block, value(states), models), + }; +} + +function corners(quad: BakedQuad) { + return [0, 1, 2, 3].map((k) => quad.positions.slice(k * 3, k * 3 + 3)); +} + +// how far toward face a point in the block is: 0 on the far side of the block, 1 on that face +function toward(point: number[], face: Face) { + const offset = FACE_OFFSETS[face]; + return 0.5 + (point[0] - 0.5) * offset[0] + (point[1] - 0.5) * offset[1] + (point[2] - 0.5) * offset[2]; +} + +function range(quad: BakedQuad, face: Face) { + const reach = corners(quad).map((point) => toward(point, face)); + return [Math.min(...reach), Math.max(...reach)]; +} + +// lying on the block toward side: flat at the wire's height off it, facing away from it +function lies_on(quad: BakedQuad, side: Face) { + const [low, high] = range(quad, side); + return quad.face === FACE_INDEX[OPPOSITE[side]] && Math.abs(low - (1 - HEIGHT)) < 1e-6 && + Math.abs(high - (1 - HEIGHT)) < 1e-6; +} + +// the part of its texture a point on a face facing that way would show on a whole block, like a face's default uv +function block_uv(face: number, [x, y, z]: number[]): [number, number] { + const uvs: [number, number][] = [ + [x, z], // top + [x, 1 - z], // bottom + [x, 1 - y], // south + [1 - x, 1 - y], // north + [z, 1 - y], // west + [1 - z, 1 - y], // east + ]; + return [uvs[face][0] * 16, uvs[face][1] * 16]; +} + +Deno.test("a wire on each side draws its middle there, and joins toward each of that side's directions", async () => { + const { quads } = await wire(); + FACES.forEach((side, s) => { + const alone = quads({ side: s }); + assertEquals(alone.length, 1, `on ${side} it's only its middle`); + assert(lies_on(alone[0], side), side); + + DIRECTIONS[side].forEach((direction, i) => { + const where = `on ${side}, joining ${direction}`; + const [straight, edge, corner] = [1, 2, 3].map((join) => + quads({ side: s, [`join_${i}`]: join }).filter((quad) => + !lies_on(quad, side) || range(quad, direction)[1] > 0.6 + ) + ); + // on the wire's side, from its middle out to the cell's edge that way + const arm = straight.find((quad) => lies_on(quad, side))!; + assert(arm, where); + assertEquals(straight.length, 1, where); + const [from, to] = range(arm, direction); + assert(from > 0.5 && Math.abs(to - 1) < 1e-6, `${where}: ${from} to ${to}`); + for (const across of DIRECTIONS[side].filter((d) => d !== direction && d !== OPPOSITE[direction])) { + const [low, high] = range(arm, across); + assert(low > 0.4 && high < 0.6, `${where}: no wider than the wire`); + } + // over an edge, a little past it to meet the wire around the corner + assertEquals(edge.length, 1, where); + assertAlmostEquals(range(edge[0], direction)[1], 1 + HEIGHT, 1e-6, where); + // into a corner, then up the face it runs into, all the way across the cell + assertEquals(corner.length, 2, where); + const bend = corner.find((quad) => !lies_on(quad, side))!; + assertEquals(bend.face, FACE_INDEX[OPPOSITE[direction]], where); + assertAlmostEquals(range(bend, direction)[0], 1 - HEIGHT, 1e-6, where); + assertEquals(range(bend, side).map((v) => Math.round(v * 1e6) / 1e6), [0, 1], where); + }); + }); +}); + +Deno.test("the wire's texture lies the way it would on a whole block's faces, so it lines up from wire to wire", async () => { + const { quads } = await wire(); + FACES.forEach((side, s) => { + for (let i = 0; i < 4; i++) { + for (const join of [1, 2, 3]) { + for (const quad of quads({ side: s, [`join_${i}`]: join })) { + corners(quad).forEach((point, k) => { + const [u, v] = block_uv(quad.face, point); + const where = `on ${side}, join_${i}=${join}, corner ${point}`; + assertAlmostEquals(quad.uvs[k * 2], u, 1e-6, where); + assertAlmostEquals(quad.uvs[k * 2 + 1], v, 1e-6, where); + }); + } + } + } + }); +}); + +Deno.test("a wire shows its powered texture while it has power, and can be pointed at", async () => { + const { block, quads } = await wire(); + assertEquals(new Set(quads({ join_0: 3 }).map((quad) => quad.texture)), new Set(["bworld:verdigris_wire"])); + assertEquals( + new Set(quads({ join_0: 3, power: 7 }).map((quad) => quad.texture)), + new Set(["bworld:verdigris_wire_on"]), + ); + // a plate around its middle, on whichever side it lies + FACES.forEach((side, s) => { + const shape = block_shape(block, { side: s, join_0: 0, join_1: 0, join_2: 0, join_3: 0, power: 0 }); + assertEquals(shape.length, 1, side); + const box = shape[0]; + const extent = (face: Face) => { + const reach = [toward(box.slice(0, 3), face), toward(box.slice(3), face)]; + return [Math.min(...reach), Math.max(...reach)]; + }; + assertEquals(extent(side).map((v) => Math.round(v * 16)), [15, 16], side); + for (const across of DIRECTIONS[side]) assertEquals(extent(across).map((v) => Math.round(v * 16)), [3, 13]); + }); +}); diff --git a/tools/verdigris_wire_model.ts b/tools/verdigris_wire_model.ts new file mode 100644 index 0000000..801bdb4 --- /dev/null +++ b/tools/verdigris_wire_model.ts @@ -0,0 +1,138 @@ +// 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`);