Files
bworld/tests/block_shapes_test.ts
T
2026-10-03 15:57:47 -03:00

121 lines
5.0 KiB
TypeScript

import { assert, assertAlmostEquals, assertEquals } from "@std/assert";
import { BUILTIN_MODELS, type ModelJson } from "$/common/block_models.ts";
import {
block_shape,
clip_shape,
collision_shape_at,
EMPTY_SHAPE,
FULL_SHAPE,
model_shape,
type Shape,
} from "$/common/block_shapes.ts";
import { block_from_json, validate_block } from "$/common/mod_data.ts";
import { move_body } from "$/common/physics.ts";
import { box_overlaps_block } from "$/common/utils.ts";
const slab: ModelJson = {
id: "test:slab",
elements: [{ from: [0, 0, 0], to: [16, 8, 16], faces: { top: { texture: "#side" } } }],
};
const SLAB: Shape = [[0, 0, 0, 1, 0.5, 1]];
Deno.test("a model's shape is the boxes of its elements", () => {
assertEquals(model_shape(BUILTIN_MODELS["engine:cube"]), FULL_SHAPE);
assertEquals(model_shape(slab), SLAB);
const stairs: ModelJson = {
id: "test:stairs",
elements: [
{ from: [0, 0, 0], to: [16, 8, 16], faces: {} },
{ from: [0, 8, 8], to: [16, 16, 16], faces: {} },
],
};
assertEquals(model_shape(stairs), [[0, 0, 0, 1, 0.5, 1], [0, 0.5, 0.5, 1, 1, 1]]);
// turned a quarter clockwise seen from above, the back half goes from south to west
assertEquals(model_shape(stairs, 90)[1], [0, 0.5, 0, 0.5, 1, 1]);
});
Deno.test("models made of planes get one box around them, so plants can still be pointed at", () => {
assertEquals(model_shape(BUILTIN_MODELS["engine:crop"]), [[0, 0, 0, 1, 1, 1]]);
const [cross] = model_shape(BUILTIN_MODELS["engine:cross"]);
assertEquals(cross[1], 0);
assertEquals(cross[4], 1);
assert(cross[0] < 0.1 && cross[3] > 0.9);
});
Deno.test("a block's shape follows its model, or its own shape in pixels, turned with the variant", () => {
const { block } = block_from_json({
id: "test:half",
textures: "test:stone",
shape: [[0, 0, 0, 16, 8, 8]],
states: [{ name: "facing", bits: 2, default: 0 }],
variants: { "facing=1": { y: 90 } },
});
assertEquals(block_shape(block, { facing: 0 }), [[0, 0, 0, 1, 0.5, 0.5]]);
assertEquals(block_shape(block, { facing: 1 }), [[0.5, 0, 0, 1, 0.5, 1]]);
const { block: plant } = block_from_json({
id: "test:plant",
textures: { cross: "test:flower" },
model: "engine:cross",
collision: false,
});
assertEquals(collision_shape_at(plant, 0), EMPTY_SHAPE);
});
Deno.test("shapes must be boxes in pixels", () => {
const block = { id: "test:a", textures: "test:a" };
assertEquals(validate_block({ ...block, shape: [[0, 0, 0, 16, 8, 16]] }), []);
assertEquals(validate_block({ ...block, shape: [[0, 0, 0, 16, 8]] }).length, 1);
assertEquals(validate_block({ ...block, shape: [[0, 8, 0, 16, 0, 16]] }).length, 1);
assertEquals(validate_block({ ...block, shape: [] }).length, 1);
});
Deno.test("bodies land on a slab's top and walk under nothing where its other half is empty", () => {
const slab_at_origin = (x: number, y: number, z: number) => y === -1 || (x === 0 && y === 0 && z === 0 && SLAB);
const falling = { x: 0.5, y: 2, z: 0.5, vx: 0, vy: 0, vz: 0, width: 0.6, height: 1.8 };
for (let i = 0; i < 40; i++) move_body(falling, -32, slab_at_origin);
assertAlmostEquals(falling.y, 0.5, 1e-6);
// walking into it steps up onto it, it's under the step height
const walking = { x: -1, y: 0, z: 0.5, vx: 4, vy: 0, vz: 0, width: 0.6, height: 1.8 };
for (let i = 0; i < 10; i++) {
walking.vx = 4;
move_body(walking, -32, slab_at_origin, 0.6, true);
}
assert(walking.x > 0.5);
assertAlmostEquals(walking.y, 0.5, 1e-6);
// a slab on the ceiling leaves room for its empty bottom half
const upper: Shape = [[0, 0.5, 0, 1, 1, 1]];
const ceiling = (x: number, y: number, z: number) => y === -1 || (x === 0 && y === 1 && z === 0 && upper);
const jumping = { x: 0.5, y: 0, z: 0.5, vx: 0, vy: 10, vz: 0, width: 0.6, height: 1 };
move_body(jumping, 0, ceiling);
assertAlmostEquals(jumping.y, 0.5, 1e-6);
});
Deno.test("a ray goes into a block only where its boxes are", () => {
// level with the slab's empty top half, it misses
assertEquals(clip_shape(SLAB, 0, 0, 0, [-1, 0.75, 0.5], [1, 0, 0], 5), undefined);
// looking down at it, it hits the top 0.5 below the eye, on the top face
const hit = clip_shape(SLAB, 0, 0, 0, [0.5, 1, 0.5], [0, -1, 0], 5)!;
assertAlmostEquals(hit.distance, 0.5);
assertEquals([hit.axis, hit.sign], [1, -1]);
// out of reach
assertEquals(clip_shape(SLAB, 0, 0, 0, [0.5, 10, 0.5], [0, -1, 0], 5), undefined);
});
Deno.test("only a placed block's boxes are in the way of whoever stands there", () => {
const player = { x: 0.5, y: 0.6, z: 0.5, width: 0.6, height: 1.8 };
assert(box_overlaps_block(player, 0, 0, 0));
assert(!box_overlaps_block(player, 0, 0, 0, SLAB));
});
Deno.test("the outline is the edges of the shape's boxes, a little outside them", async () => {
const { shape_edges } = await import("$/client/rendering/level_renderer.ts");
const cube = shape_edges(FULL_SHAPE);
assertEquals(cube.length, 12);
assert(cube.every((edge) => edge.every((v) => v < 0 || v > 1)));
assertEquals(shape_edges(SLAB).length, 12);
// edges two boxes share are drawn once
assertEquals(shape_edges([...SLAB, ...SLAB]).length, 12);
});