Verdigris wire model
This commit is contained in:
@@ -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]);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user