234 lines
9.6 KiB
TypeScript
234 lines
9.6 KiB
TypeScript
// a block's shape, like minecraft's VoxelShape: the boxes it's made of. it's what the player points at and what the
|
|
// cracks are drawn on when it breaks (minecraft's getShape), and what entities bump into when it has collision
|
|
// (getCollisionShape). blocks get theirs from the boxes of their model, or set "shape" like minecraft's Block.box
|
|
import {
|
|
block_parts,
|
|
element_rotation,
|
|
find_model,
|
|
looks_by_state,
|
|
type ModelJson,
|
|
states_of,
|
|
} from "./block_models.ts";
|
|
import { type BlockRegistry, EverythingRegistry } from "./everything_registry.ts";
|
|
import { STATE_SHIFT } from "./constants.ts";
|
|
|
|
// min x y z then max x y z, in block space (0 to 1)
|
|
export type Box = readonly [number, number, number, number, number, number];
|
|
export type Shape = readonly Box[];
|
|
|
|
export const FULL_SHAPE: Shape = [[0, 0, 0, 1, 1, 1]];
|
|
export const EMPTY_SHAPE: Shape = [];
|
|
|
|
// thinner than this on some axis is a flat plane, like the cross model's, not a box
|
|
const FLAT = 1e-4;
|
|
|
|
// the boxes of a model's elements, turned like the model is. elements turned by their own rotation take the box
|
|
// around them. flat planes (plants) aren't boxes, so a model made only of planes gets one box around all of them
|
|
export function model_shape(model: ModelJson, y_rotation = 0): Shape {
|
|
const boxes: Box[] = [];
|
|
let bounds: Box | undefined;
|
|
for (const element of model.elements) {
|
|
const rotate = element_rotation(element);
|
|
const min = [Infinity, Infinity, Infinity];
|
|
const max = [-Infinity, -Infinity, -Infinity];
|
|
for (let corner = 0; corner < 8; corner++) {
|
|
const point = rotate(
|
|
[0, 1, 2].map((axis) => ((corner >> axis) & 1 ? element.to[axis] : element.from[axis]) / 16),
|
|
);
|
|
for (let axis = 0; axis < 3; axis++) {
|
|
min[axis] = Math.min(min[axis], point[axis]);
|
|
max[axis] = Math.max(max[axis], point[axis]);
|
|
}
|
|
}
|
|
const box = clamp([...min, ...max]);
|
|
bounds = bounds ? clamp(bounds.map((v, i) => (i < 3 ? Math.min : Math.max)(v, box[i]))) : box;
|
|
if ([0, 1, 2].every((axis) => box[axis + 3] - box[axis] > FLAT)) boxes.push(box);
|
|
}
|
|
if (boxes.length === 0 && bounds) boxes.push(bounds);
|
|
return boxes.map((box) => turn(box, y_rotation));
|
|
}
|
|
|
|
// boxes in pixels, like a block's "shape" ([x1, y1, z1, x2, y2, z2], 0 to 16), turned like the model is. a collision
|
|
// shape can go up to 24 pixels high, like minecraft's fences, so nothing jumps over them
|
|
export function pixel_shape(boxes: number[][], y_rotation = 0, top = 1): Shape {
|
|
return boxes.map((box) => turn(clamp(box.map((v) => v / 16), top), y_rotation));
|
|
}
|
|
|
|
// how high a collision shape can go, in blocks
|
|
export const COLLISION_TOP = 1.5;
|
|
|
|
// what the block is at a value with state bits: each part's (or the variant's) "shape" when it sets one, otherwise
|
|
// its model's boxes
|
|
export function block_shape(block: BlockRegistry, states?: Record<string, number>): Shape {
|
|
const models = Object.fromEntries(EverythingRegistry.entries<ModelJson>("models"));
|
|
return block_parts(block, states).flatMap((part) => {
|
|
if (part.shape) return pixel_shape(part.shape, part.y);
|
|
const model = find_model(part.model, models);
|
|
return model ? model_shape(model, part.y) : FULL_SHAPE;
|
|
});
|
|
}
|
|
|
|
// what entities bump into, like getCollisionShape: each part's "collision_shape" when it sets one, otherwise its shape
|
|
export function block_collision_shape(block: BlockRegistry, states?: Record<string, number>): Shape {
|
|
if (!block.has_collision) return EMPTY_SHAPE;
|
|
const parts = block_parts(block, states);
|
|
if (parts.every((part) => !part.collision_shape)) return block_shape(block, states);
|
|
const models = Object.fromEntries(EverythingRegistry.entries<ModelJson>("models"));
|
|
return parts.flatMap((part) => {
|
|
if (part.collision_shape) return pixel_shape(part.collision_shape, part.y, COLLISION_TOP);
|
|
if (part.shape) return pixel_shape(part.shape, part.y);
|
|
const model = find_model(part.model, models);
|
|
return model ? model_shape(model, part.y) : FULL_SHAPE;
|
|
});
|
|
}
|
|
|
|
// shapes by block, then by value with state bits, worked out the first time they're asked for
|
|
const shapes_cache = new WeakMap<BlockRegistry, Map<number, Shape>>();
|
|
const collision_cache = new WeakMap<BlockRegistry, Map<number, Shape>>();
|
|
|
|
function cached(
|
|
cache: WeakMap<BlockRegistry, Map<number, Shape>>,
|
|
block: BlockRegistry,
|
|
value: number,
|
|
make: (block: BlockRegistry, states?: Record<string, number>) => Shape,
|
|
): Shape {
|
|
let shapes = cache.get(block);
|
|
if (!shapes) cache.set(block, shapes = new Map());
|
|
const by_state = looks_by_state(block);
|
|
const key = by_state ? value >>> STATE_SHIFT : 0;
|
|
let shape = shapes.get(key);
|
|
if (!shape) {
|
|
shape = make(block, by_state ? states_of(block, value) : undefined);
|
|
shapes.set(key, shape);
|
|
}
|
|
return shape;
|
|
}
|
|
|
|
// the block's shape at a value from the world, see block_shape
|
|
export function shape_at(block: BlockRegistry, value: number): Shape {
|
|
return cached(shapes_cache, block, value, block_shape);
|
|
}
|
|
|
|
// what entities bump into at a value from the world, see block_collision_shape. nothing for "collision": false
|
|
export function collision_shape_at(block: BlockRegistry, value: number): Shape {
|
|
return cached(collision_cache, block, value, block_collision_shape);
|
|
}
|
|
|
|
// how far along the ray (origin plus direction times distance) it first goes into one of the boxes of a block at
|
|
// x, y, z, and the side it went in by as the axis and which way along it the ray was going. undefined if it misses
|
|
export function clip_shape(
|
|
shape: Shape,
|
|
x: number,
|
|
y: number,
|
|
z: number,
|
|
origin: number[],
|
|
direction: number[],
|
|
max_distance: number,
|
|
): { distance: number; axis: number; sign: number } | undefined {
|
|
const offset = [x, y, z];
|
|
let closest: { distance: number; axis: number; sign: number } | undefined;
|
|
for (const box of shape) {
|
|
let enter = 0;
|
|
let enter_axis = -1;
|
|
let leave = closest?.distance ?? max_distance;
|
|
for (let axis = 0; axis < 3 && enter <= leave; axis++) {
|
|
const min = offset[axis] + box[axis];
|
|
const max = offset[axis] + box[axis + 3];
|
|
if (Math.abs(direction[axis]) < 1e-9) {
|
|
if (origin[axis] < min || origin[axis] > max) enter = Infinity;
|
|
continue;
|
|
}
|
|
const a = (min - origin[axis]) / direction[axis];
|
|
const b = (max - origin[axis]) / direction[axis];
|
|
if (Math.min(a, b) >= enter) {
|
|
enter = Math.min(a, b);
|
|
enter_axis = axis;
|
|
}
|
|
leave = Math.min(leave, Math.max(a, b));
|
|
}
|
|
if (enter <= leave && enter_axis >= 0) {
|
|
closest = { distance: enter, axis: enter_axis, sign: Math.sign(direction[enter_axis]) };
|
|
}
|
|
}
|
|
return closest;
|
|
}
|
|
|
|
export type ShapeFace = "west" | "east" | "bottom" | "top" | "north" | "south";
|
|
|
|
export interface BlockRaycastHit {
|
|
x: number;
|
|
y: number;
|
|
z: number;
|
|
// the side of the block's box it went in by
|
|
face: ShapeFace;
|
|
distance: number;
|
|
}
|
|
|
|
// the first block a ray from origin goes into within max_distance, cell by cell, like minecraft's Level.clip.
|
|
// direction is a unit vector, and shape_of says what each block there is, undefined for nothing to hit. a block the
|
|
// ray starts inside isn't hit
|
|
export function raycast_blocks(
|
|
origin: readonly number[],
|
|
direction: readonly number[],
|
|
max_distance: number,
|
|
shape_of: (x: number, y: number, z: number) => Shape | undefined,
|
|
): BlockRaycastHit | undefined {
|
|
const cell = origin.map(Math.floor);
|
|
const step = direction.map(Math.sign);
|
|
// how far along the ray it takes to cross a whole cell on each axis, and how far until it crosses the next one
|
|
const delta = direction.map((d) => d === 0 ? Infinity : Math.abs(1 / d));
|
|
const next = [0, 1, 2].map((axis) =>
|
|
direction[axis] === 0
|
|
? Infinity
|
|
: (step[axis] > 0 ? cell[axis] + 1 - origin[axis] : origin[axis] - cell[axis]) * delta[axis]
|
|
);
|
|
for (;;) {
|
|
const shape = shape_of(cell[0], cell[1], cell[2]);
|
|
const hit = shape && clip_shape(shape, cell[0], cell[1], cell[2], [...origin], [...direction], max_distance);
|
|
if (hit) {
|
|
const face: ShapeFace = hit.axis === 0
|
|
? (hit.sign > 0 ? "west" : "east")
|
|
: hit.axis === 1
|
|
? (hit.sign > 0 ? "bottom" : "top")
|
|
: (hit.sign > 0 ? "north" : "south");
|
|
return { x: cell[0], y: cell[1], z: cell[2], face, distance: hit.distance };
|
|
}
|
|
const axis = next[0] < next[1] ? (next[0] < next[2] ? 0 : 2) : (next[1] < next[2] ? 1 : 2);
|
|
if (next[axis] > max_distance) return undefined;
|
|
cell[axis] += step[axis];
|
|
next[axis] += delta[axis];
|
|
}
|
|
}
|
|
|
|
// whether the shape's boxes cover the whole of one of the block's sides, like minecraft's Block.isFaceFull. checked
|
|
// pixel by pixel, which is as fine as shapes in pixels get
|
|
export function covers_face(shape: Shape, face: "west" | "east" | "bottom" | "top" | "north" | "south"): boolean {
|
|
const axis = face === "west" || face === "east" ? 0 : face === "bottom" || face === "top" ? 1 : 2;
|
|
const high = face === "east" || face === "top" || face === "south";
|
|
const [a, b] = [(axis + 1) % 3, (axis + 2) % 3];
|
|
const touching = shape.filter((box) => high ? box[axis + 3] >= 1 - FLAT && box[axis] < 1 : box[axis] <= FLAT);
|
|
for (let i = 0; i < 16; i++) {
|
|
for (let j = 0; j < 16; j++) {
|
|
const [u, v] = [(i + 0.5) / 16, (j + 0.5) / 16];
|
|
if (!touching.some((box) => box[a] <= u && u <= box[a + 3] && box[b] <= v && v <= box[b + 3])) return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
function clamp(box: number[], top = 1): Box {
|
|
const c = box.map((v, i) => Math.min(Math.max(v, 0), i % 3 === 1 ? top : 1));
|
|
return [c[0], c[1], c[2], c[3], c[4], c[5]];
|
|
}
|
|
|
|
// clockwise seen from above by 0, 90, 180 or 270 degrees, the same as bake_model turns quads
|
|
function turn(box: Box, y_rotation: number): Box {
|
|
const turns = ((Math.round(y_rotation / 90) % 4) + 4) % 4;
|
|
let [x1, y1, z1, x2, y2, z2] = box;
|
|
for (let t = 0; t < turns; t++) {
|
|
// a point (x, z) goes to (1 - z, x)
|
|
[x1, z1, x2, z2] = [1 - z2, x1, 1 - z1, x2];
|
|
}
|
|
return [x1, y1, z1, x2, y2, z2];
|
|
}
|