Verdigris wire model

This commit is contained in:
2026-10-22 12:57:05 -03:00
parent b5777c84d3
commit 0d80e85dec
13 changed files with 1040 additions and 0 deletions
+586
View File
@@ -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
}
]
}
}
@@ -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"
}
}
}
]
}
}
@@ -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"
}
}
}
]
}
}
@@ -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"
}
}
}
]
}
}
@@ -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"
}
}
}
]
}
}
@@ -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"
}
}
}
]
}
}
@@ -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"
}
}
}
]
}
}
@@ -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"
}
}
}
]
}
}
+30
View File
@@ -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<Record<Face, readonly [number, number, number]>> = {
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<Record<Face, readonly [Face, Face, Face, Face]>> = {
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"],
};
Binary file not shown.

After

Width:  |  Height:  |  Size: 97 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 97 B

+149
View File
@@ -0,0 +1,149 @@
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
// SPDX-License-Identifier: AGPL-3.0-or-later
// verdigris wire, see REDSTONE.md: how it's drawn on every side it can lie on
import { assert, assertAlmostEquals, assertEquals } from "@std/assert";
import { bake_block, type BakedQuad, FACE_INDEX, type ModelJson } from "$/common/block_models.ts";
import { block_shape } from "$/common/block_shapes.ts";
import { type BlockRegistry, EverythingRegistry } from "$/common/everything_registry.ts";
import type { Face } from "$/common/mod_api/shared.ts";
import { default_block_value, set_state_value } from "$/common/utils.ts";
import { DIRECTIONS, FACE_OFFSETS, FACES } from "$/mods/bworld/scripts/verdigris/sides.ts";
import { test_game } from "./helpers.ts";
// how far it stands off the face it lies on, in blocks
const HEIGHT = 0.25 / 16;
const OPPOSITE: Record<Face, Face> = {
north: "south",
south: "north",
east: "west",
west: "east",
top: "bottom",
bottom: "top",
};
async function wire() {
await test_game("mods");
const block = EverythingRegistry.get<BlockRegistry>("blocks", "bworld:verdigris_wire")!;
const models = Object.fromEntries(EverythingRegistry.entries<ModelJson>("models"));
const value = (states: Record<string, number>) => {
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<string, number>) => 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]);
});
});
+138
View File
@@ -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<string, ModelElementJson[]> = {
// 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<ModelFace, ModelFace> = {
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<string, ModelElementJson[]> = { ...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<string, unknown>[] = [];
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`);