Files
bworld/mods/bworld/scripts/verdigris/sides.ts
T
2026-10-22 15:51:40 -03:00

82 lines
3.4 KiB
TypeScript

// 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],
};
export const OPPOSITE: Readonly<Record<Face, Face>> = {
north: "south",
east: "west",
south: "north",
west: "east",
top: "bottom",
bottom: "top",
};
// the block toward face from x y z
export function step(x: number, y: number, z: number, face: Face): [number, number, number] {
const [dx, dy, dz] = FACE_OFFSETS[face];
return [x + dx, y + dy, z + dz];
}
// a position as one number, so maps and sets of them don't make a string each: x and z in 21 bits and y in 8, further
// than chunk_key lets a world go
export function pack(x: number, y: number, z: number) {
return ((x + 2 ** 20) * 2 ** 21 + z + 2 ** 20) * 256 + y;
}
export function unpack(key: number): [number, number, number] {
const y = key % 256;
const xz = (key - y) / 256;
const z = xz % 2 ** 21;
return [(xz - z) / 2 ** 21 - 2 ** 20, y, z - 2 ** 20];
}
// which way a block points, from its states: the name of a facing state, read as FACES' values with 2 bits or 3, or
// a face and facing state for blocks on a floor, wall or ceiling, like a lever. face 0 is the floor, where it points
// up, 1 a wall, where it points the way facing says, and 2 the ceiling, where it points down
export type FacingParam = string | { face: string; facing: string };
export function front_of(facing: FacingParam, state: (name: string) => number | undefined): Face {
if (typeof facing === "string") return FACES[state(facing) ?? 0];
const on = state(facing.face) ?? 0;
return on === 0 ? "top" : on === 2 ? "bottom" : FACES[state(facing.facing) ?? 0];
}
// a block's sides as components name them: faces, or front, back and sides, the four around the way it points
export type SideName = Face | "front" | "back" | "sides";
export function named_sides(names: readonly SideName[], front: Face | undefined): Face[] {
return names.flatMap((name): readonly Face[] => {
if (name === "front") return front ? [front] : [];
if (name === "back") return front ? [OPPOSITE[front]] : [];
if (name === "sides") return front ? FACES.filter((face) => face !== front && face !== OPPOSITE[front]) : [];
return [name];
});
}
// the 4 directions along a wire on each face of its cell, in the order its arm_ states take them (see ways_out in
// joins.ts). they're the same on every face: a floor'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 face. 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"],
};