161 lines
6.9 KiB
TypeScript
161 lines
6.9 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,
|
|
raycast_blocks,
|
|
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);
|
|
});
|
|
|
|
// a world of full blocks at the listed positions, and half height slabs at the others listed
|
|
function raycast_world(full: number[][], slabs: number[][] = []) {
|
|
const has = (list: number[][], x: number, y: number, z: number) =>
|
|
list.some(([bx, by, bz]) => bx === x && by === y && bz === z);
|
|
return (x: number, y: number, z: number): Shape | undefined =>
|
|
has(full, x, y, z) ? FULL_SHAPE : has(slabs, x, y, z) ? [[0, 0, 0, 1, 0.5, 1]] : undefined;
|
|
}
|
|
|
|
Deno.test("raycast_blocks finds the first block along the ray, and the side it went in by", () => {
|
|
const world = raycast_world([[5, 0, 0], [8, 0, 0]]);
|
|
const east = raycast_blocks([0.5, 0.5, 0.5], [1, 0, 0], 20, world)!;
|
|
assertEquals([east.x, east.y, east.z, east.face], [5, 0, 0, "west"]);
|
|
assertAlmostEquals(east.distance, 4.5);
|
|
|
|
const west = raycast_blocks([10.5, 0.5, 0.5], [-1, 0, 0], 20, world)!;
|
|
assertEquals([west.x, west.face], [8, "east"]);
|
|
assertAlmostEquals(west.distance, 1.5);
|
|
|
|
assertEquals(raycast_blocks([0.5, 0.5, 0.5], [1, 0, 0], 4, world), undefined);
|
|
assertEquals(raycast_blocks([0.5, 0.5, 0.5], [0, 0, 1], 20, world), undefined);
|
|
});
|
|
|
|
Deno.test("raycast_blocks goes diagonally and down onto the top of a block", () => {
|
|
// it reaches y 0 at x and z 2.7, on the block's top
|
|
const world = raycast_world([[2, -1, 2]]);
|
|
const d = 1 / Math.sqrt(3);
|
|
const hit = raycast_blocks([0.2, 2.5, 0.2], [d, -d, d], 20, world)!;
|
|
assertEquals([hit.x, hit.y, hit.z, hit.face], [2, -1, 2, "top"]);
|
|
assertAlmostEquals(hit.distance, 2.5 * Math.sqrt(3));
|
|
});
|
|
|
|
Deno.test("raycast_blocks goes over the empty half of a slab, and not out of a block it starts in", () => {
|
|
const world = raycast_world([[0, 0, 0], [4, 0, 0]], [[2, 0, 0]]);
|
|
const over = raycast_blocks([0.5, 0.75, 0.5], [1, 0, 0], 20, world)!;
|
|
assertEquals([over.x, over.face], [4, "west"]);
|
|
const into = raycast_blocks([0.5, 0.25, 0.5], [1, 0, 0], 20, world)!;
|
|
assertEquals([into.x, into.face], [2, "west"]);
|
|
});
|