Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

feat(nav): an agent sent somewhere it cannot reach walks to the door

esengine869f084d46ea
diff --git a/CHANGELOG.md b/CHANGELOG.md
index e60ba305e..0cca12357 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -170,6 +170,19 @@ published separately; it ships inside the editor.
than the box around them. Nothing waits for the solver any more: the geometry is
in the scene from the first frame.
+- **An agent sent somewhere it cannot reach walks to the door.** `findWorldPath`
+ and `findPath` returned nothing at all when the goal could not be reached, so an
+ agent whose target was behind a shut door — or momentarily on the far side of
+ one — stood exactly where it was and waited. They now answer with the route to
+ the nearest place that CAN be reached, which is what every game wants and what
+ the docs already promised for a goal standing in a wall. `null` is left meaning
+ one thing: the agent itself is nowhere.
+
+ Whether it got there stays visible, because the caller named the goal and the
+ last waypoint either is at it or is not — no flag, and no second question to
+ ask. A goal off the mesh entirely is a direction too: the walk toward it is
+ still a walk, round the wall rather than through it.
+
- **A 3D AI example, and the last thing sensing could not act on.** `Perception`
has reported where a target is on all three axes since sensing became
three-dimensional, and `targetZ` / `dirZ` had no reader anywhere: the chase a
diff --git a/docs/astro/src/content/docs/gameplay/ai/navigation.mdx b/docs/astro/src/content/docs/gameplay/ai/navigation.mdx
index 21c8d94a7..155a04622 100644
--- a/docs/astro/src/content/docs/gameplay/ai/navigation.mdx
+++ b/docs/astro/src/content/docs/gameplay/ai/navigation.mdx
@@ -192,8 +192,16 @@ big world.
<Aside type="tip">
The `Nav` resource also answers one-off queries directly:
- `nav.findWorldPath(from, to)` returns an array of world-space waypoints (or
- `null` if there's no route) without needing a `NavAgent`.
+ `nav.findWorldPath(from, to)` returns an array of world-space waypoints without
+ needing a `NavAgent`.
+
+ A goal it cannot reach — behind a shut door, on an island, off the mesh
+ entirely — is answered with the route to the nearest place it CAN reach, because
+ an agent sent somewhere it cannot get to should walk to the door rather than
+ stand where it was told. Whether it got there is yours to see: you named the
+ goal, and the last waypoint either is at it or is not. `null` means the agent
+ itself is nowhere — there is no route from a point that is not on the navigable
+ world at all.
</Aside>
### Moving an agent
diff --git a/docs/astro/src/content/docs/zh-cn/gameplay/ai/navigation.mdx b/docs/astro/src/content/docs/zh-cn/gameplay/ai/navigation.mdx
index e509e187b..1c9f62744 100644
--- a/docs/astro/src/content/docs/zh-cn/gameplay/ai/navigation.mdx
+++ b/docs/astro/src/content/docs/zh-cn/gameplay/ai/navigation.mdx
@@ -165,8 +165,13 @@ app.getResource(NavDebugDraw).enabled = true;
(一个 surface 是每帧几千个环),而且画到几千个面就停:想看清更大的世界,调 `cellSize`。
<Aside type="tip">
- `Nav` 资源也能直接回答一次性查询:`nav.findWorldPath(from, to)` 返回一组世界空间路点
- (没有路线时返回 `null`),无需 `NavAgent`。
+ `Nav` 资源也能直接回答一次性查询:`nav.findWorldPath(from, to)` 返回一组世界空间路点,
+ 无需 `NavAgent`。
+
+ **到不了的目标**——门关着、在另一座岛上、根本不在网格上——回答的是「到它**最近**的那个
+ 到得了的地方」的路线:被派往一个到不了的地方的 agent,应该走到门口,而不是站在原地。
+ 它到没到是**你**该看的:目标是你给的,最后一个路点要么在它上面,要么不在。返回 `null`
+ 只有一种意思——**agent 自己不在任何地方**,一个不在可走世界上的点没有任何路线可言。
</Aside>
### 移动 agent
diff --git a/sdk/src/ai/nav/NavMesh.ts b/sdk/src/ai/nav/NavMesh.ts
index ebab3c2fc..24695f1d2 100644
--- a/sdk/src/ai/nav/NavMesh.ts
+++ b/sdk/src/ai/nav/NavMesh.ts
@@ -335,13 +335,15 @@ export class NavMesh implements NavSurface {
const start: Vec3 = { x: 0, y: 0, z: 0 };
const end: Vec3 = { x: 0, y: 0, z: 0 };
const startPoly = this.findPoly(from, start);
+ // A goal off the mesh is still a direction to walk in. Only an agent that
+ // is itself nowhere has no route at all.
const endPoly = this.findPoly(to, end);
- if (startPoly < 0 || endPoly < 0) return null;
+ if (startPoly < 0) return null;
+ if (endPoly < 0) { end.x = to.x; end.y = to.y; end.z = to.z ?? 0; }
if (startPoly === endPoly) return [start, end];
const polyPath = this.findPolyPath(startPoly, endPoly, start, end);
- if (!polyPath) return null;
- return this.routeThroughLinks(polyPath, start, end);
+ return this.routeThroughLinks(polyPath, start, polyPath.end);
}
/**
@@ -367,8 +369,13 @@ export class NavMesh implements NavSurface {
return out;
}
- /** A* over the polygon graph, keyed on where each doorway is crossed. */
- private findPolyPath(startPoly: number, endPoly: number, start: Vec3, end: Vec3): PolyPath | null {
+ /**
+ * A* over the polygon graph, keyed on where each doorway is crossed. A goal it
+ * cannot reach yields the route to the polygon it got NEAREST: an agent sent
+ * somewhere it cannot get to walks to the door, and the caller sees it fell
+ * short because it named the goal itself.
+ */
+ private findPolyPath(startPoly: number, endPoly: number, start: Vec3, end: Vec3): PolyPath {
const n = this.polyCount;
const cost = new Float64Array(n).fill(Infinity);
const parent = new Int32Array(n).fill(-1);
@@ -384,8 +391,10 @@ export class NavMesh implements NavSurface {
cost[startPoly] = 0;
enterX[startPoly] = start.x; enterY[startPoly] = start.y; enterZ[startPoly] = start.z;
+ let nearest = startPoly;
+ let nearestScore = distance(start.x, start.y, start.z, end.x, end.y, end.z);
const heap = new MinHeap(n);
- heap.push(startPoly, distance(start.x, start.y, start.z, end.x, end.y, end.z));
+ heap.push(startPoly, nearestScore);
const a: Vec3 = { x: 0, y: 0, z: 0 };
const b: Vec3 = { x: 0, y: 0, z: 0 };
@@ -411,7 +420,9 @@ export class NavMesh implements NavSurface {
parent[next] = cur;
via[next] = -1;
enterX[next] = mx; enterY[next] = my; enterZ[next] = mz;
- heap.push(next, g + heuristic(mx, my, mz, end));
+ const toGoal = distance(mx, my, mz, end.x, end.y, end.z);
+ if (toGoal < nearestScore) { nearestScore = toGoal; nearest = next; }
+ heap.push(next, g + toGoal * HEURISTIC_SCALE);
}
// The ways the ground does not join: crossing one costs getting to the
@@ -430,14 +441,22 @@ export class NavMesh implements NavSurface {
parent[next] = cur;
via[next] = li;
enterX[next] = link.end.x; enterY[next] = link.end.y; enterZ[next] = link.end.z;
- heap.push(next, g + heuristic(link.end.x, link.end.y, link.end.z, end));
+ const toGoal = distance(link.end.x, link.end.y, link.end.z, end.x, end.y, end.z);
+ if (toGoal < nearestScore) { nearestScore = toGoal; nearest = next; }
+ heap.push(next, g + toGoal * HEURISTIC_SCALE);
}
}
- if (parent[endPoly] === -1 && endPoly !== startPoly) return null;
+ // Where the route actually finishes: the goal if it was reached, and the
+ // nearest the search ever came to it if it was not.
+ const reached = endPoly >= 0 && cost[endPoly]! < Infinity;
+ const last = reached ? endPoly : nearest;
+ const finish: Vec3 = reached
+ ? end
+ : { x: enterX[last]!, y: enterY[last]!, z: enterZ[last]! };
const polys: number[] = [];
const links: number[] = [];
- for (let p = endPoly; p !== -1; p = parent[p]!) {
+ for (let p = last; p !== -1; p = parent[p]!) {
polys.push(p);
// The link recorded on a polygon is the one taken to REACH it, so it
// belongs to the step out of the polygon before it.
@@ -449,7 +468,7 @@ export class NavMesh implements NavSurface {
// Shift by one: `links[i]` is now the step from `polys[i]` to `polys[i+1]`.
links.shift();
links.push(-1);
- return polys[0] === startPoly ? { polys, links } : null;
+ return { polys, links, end: finish };
}
/**
@@ -568,6 +587,8 @@ export class NavMesh implements NavSurface {
interface PolyPath {
polys: number[];
links: number[];
+ /** Where the route finishes, which is the goal only when it was reached. */
+ end: Vec3;
}
const EMPTY_LINKS: readonly number[] = [];
@@ -582,10 +603,6 @@ function append(out: Vec3[], points: readonly Vec3[]): void {
}
}
-/** Straight-line distance to the goal, trusted as Detour trusts it. */
-function heuristic(x: number, y: number, z: number, end: Vec3): number {
- return distance(x, y, z, end.x, end.y, end.z) * HEURISTIC_SCALE;
-}
/**
* Put the taut route back on the ground. The funnel answers in the ground plane,
diff --git a/sdk/src/ai/nav/pathfind.ts b/sdk/src/ai/nav/pathfind.ts
index e9fc30bc6..2fcec5752 100644
--- a/sdk/src/ai/nav/pathfind.ts
+++ b/sdk/src/ai/nav/pathfind.ts
@@ -42,9 +42,10 @@ const DIAG: ReadonlyArray<readonly [number, number]> = [
];
/**
- * Find a grid path from `start` to `goal`, inclusive of both endpoints.
- * Returns null if unreachable (or if either endpoint can't be snapped to a
- * walkable cell). A start equal to goal yields a single-cell path.
+ * Find a grid path from `start` to `goal`, inclusive of both endpoints. A goal
+ * that cannot be reached yields the route to the reachable cell NEAREST it, and
+ * the caller sees it fell short because it named the goal. Null means the START
+ * is nowhere: a cell off the grid has no route from it.
*/
export function findPath(
grid: NavGrid,
@@ -71,8 +72,12 @@ export function findPath(
const closed = new Uint8Array(n);
gScore[startIdx] = 0;
+ // The nearest the search ever got, in case it never gets there.
+ let nearestIdx = startIdx;
+ let nearestScore = heuristic(s.x, s.y, g.x, g.y, diagonal);
+
const heap = new MinHeap(n);
- heap.push(startIdx, heuristic(s.x, s.y, g.x, g.y, diagonal));
+ heap.push(startIdx, nearestScore);
while (heap.size > 0) {
const current = heap.pop();
@@ -100,12 +105,17 @@ export function findPath(
if (tentative < gScore[nIdx]) {
gScore[nIdx] = tentative;
cameFrom[nIdx] = current;
- heap.push(nIdx, tentative + heuristic(nx, ny, g.x, g.y, diagonal));
+ const toGoal = heuristic(nx, ny, g.x, g.y, diagonal);
+ if (toGoal < nearestScore) {
+ nearestScore = toGoal;
+ nearestIdx = nIdx;
+ }
+ heap.push(nIdx, tentative + toGoal);
}
}
}
- return null;
+ return reconstruct(cameFrom, nearestIdx, width);
}
/**
diff --git a/sdk/tests/nav-clearance.test.ts b/sdk/tests/nav-clearance.test.ts
index 5c2b2b33f..c8e6e85c2 100644
--- a/sdk/tests/nav-clearance.test.ts
+++ b/sdk/tests/nav-clearance.test.ts
@@ -68,7 +68,9 @@ describe('findPath with clearance', () => {
});
it('refuses the pinch for a body that does not fit', () => {
- expect(findPath(pinch, { x: 3, y: 0 }, { x: 3, y: 4 }, { clearance: 2 })).toBeNull();
+ const body = findPath(pinch, { x: 3, y: 0 }, { x: 3, y: 4 }, { clearance: 2 })!;
+ // It walks up to the gap and no further: the far side is not reached.
+ expect(body[body.length - 1]!.y).toBeLessThan(2);
});
it('routes a body the long way round when there is one', () => {
@@ -81,11 +83,11 @@ describe('findPath with clearance', () => {
'..........',
'..........',
]);
- const point = findPath(room, { x: 1, y: 1 }, { x: 1, y: 5 });
- const body = findPath(room, { x: 1, y: 1 }, { x: 1, y: 5 }, { clearance: 2 });
- expect(point).not.toBeNull();
- // The only gap is one cell wide, so a two-cell body has nowhere to go.
- expect(body).toBeNull();
+ const point = findPath(room, { x: 1, y: 1 }, { x: 1, y: 5 })!;
+ const body = findPath(room, { x: 1, y: 1 }, { x: 1, y: 5 }, { clearance: 2 })!;
+ expect(point[point.length - 1]).toEqual({ x: 1, y: 5 });
+ // The only gap is one cell wide, so a two-cell body never crosses the row.
+ expect(body[body.length - 1]!.y).toBeLessThan(3);
});
// Goals are where the game put them, which is often against a wall. A body
diff --git a/sdk/tests/nav-links.test.ts b/sdk/tests/nav-links.test.ts
index fc132c562..9a6a4475f 100644
--- a/sdk/tests/nav-links.test.ts
+++ b/sdk/tests/nav-links.test.ts
@@ -57,8 +57,8 @@ const HIGH: Vec3 = { x: 500, y: 300, z: 0 };
describe('a link between two floors', () => {
it('is what makes the two of them one navigable world', () => {
- expect(bake().findWorldPath(LOW, HIGH)).toBeNull();
- expect(bake([LADDER]).findWorldPath(LOW, HIGH)).not.toBeNull();
+ expect(reaches(bake().findWorldPath(LOW, HIGH), HIGH)).toBe(false);
+ expect(reaches(bake([LADDER]).findWorldPath(LOW, HIGH), HIGH)).toBe(true);
});
// The route has to go THROUGH the link, not across the gap it spans: a funnel
@@ -75,8 +75,8 @@ describe('a link between two floors', () => {
it('is one way when the scene says so', () => {
const drop = bake([{ ...LADDER, bidirectional: false }]);
- expect(drop.findWorldPath(LOW, HIGH)).not.toBeNull();
- expect(drop.findWorldPath(HIGH, LOW)).toBeNull();
+ expect(reaches(drop.findWorldPath(LOW, HIGH), HIGH)).toBe(true);
+ expect(reaches(drop.findWorldPath(HIGH, LOW), LOW)).toBe(false);
});
// A link is a claim about ground at both ends. One end over nothing joins
@@ -84,7 +84,7 @@ describe('a link between two floors', () => {
it('joins nothing when an end reaches no ground', () => {
const intoTheAir: NavLinkSegment = { ...LADDER, end: { x: 200, y: 1000, z: 0 } };
expect(bake([intoTheAir]).linkCount).toBe(0);
- expect(bake([intoTheAir]).findWorldPath(LOW, HIGH)).toBeNull();
+ expect(reaches(bake([intoTheAir]).findWorldPath(LOW, HIGH), HIGH)).toBe(false);
});
it('counts both directions of a two-way one, and one of a one-way', () => {
@@ -137,3 +137,11 @@ describe('updateLinks', () => {
expect((nav.surface as NavMesh).linkCount).toBe(before);
});
});
+
+/** Whether a route actually got where it was sent — a route that could not is
+ * answered with the way to the nearest place it could, not with nothing. */
+function reaches(path: Vec3[] | null, to: Vec3, slack = 120): boolean {
+ if (!path || path.length === 0) return false;
+ const end = path[path.length - 1]!;
+ return Math.hypot(end.x - to.x, end.y - to.y, end.z - to.z) < slack;
+}
diff --git a/sdk/tests/nav-obstacles.test.ts b/sdk/tests/nav-obstacles.test.ts
index fca1749ff..244bad590 100644
--- a/sdk/tests/nav-obstacles.test.ts
+++ b/sdk/tests/nav-obstacles.test.ts
@@ -15,6 +15,7 @@ import { Navigation } from '../src/ai/nav/Navigation';
import { updateObstacles, type ObstacleState } from '../src/ai/nav/NavPlugin';
import { applyObstaclesToGrid, navObstacleDigest } from '../src/ai/nav/navObstacles';
import type { NavObstacleBox } from '../src/ai/nav/navmesh/compact';
+import type { Vec3 } from '../src/types';
const NO_TURN = { x: 0, y: 0, z: 0, w: 1 };
const QUARTER_TURN = { x: 0, y: Math.SQRT1_2, z: 0, w: Math.SQRT1_2 };
@@ -90,8 +91,8 @@ describe('an obstacle in a baked mesh', () => {
const open = bake([]);
const from = { x: -500, y: 0, z: 0 };
const to = { x: 500, y: 0, z: 0 };
- expect(open.findWorldPath(from, to)).not.toBeNull();
- expect(shut.findWorldPath(from, to)).toBeNull();
+ expect(reaches(open.findWorldPath(from, to), to)).toBe(true);
+ expect(reaches(shut.findWorldPath(from, to), to)).toBe(false);
});
});
@@ -170,3 +171,11 @@ describe('updateObstacles', () => {
expect(grid.isWalkable(4, 4)).toBe(false);
});
});
+
+/** Whether a route actually got where it was sent — a route that could not is
+ * answered with the way to the nearest place it could, not with nothing. */
+function reaches(path: Vec3[] | null, to: Vec3, slack = 120): boolean {
+ if (!path || path.length === 0) return false;
+ const end = path[path.length - 1]!;
+ return Math.hypot(end.x - to.x, end.y - to.y, end.z - to.z) < slack;
+}
diff --git a/sdk/tests/nav-pathfind.test.ts b/sdk/tests/nav-pathfind.test.ts
index d29b99d40..79bdf45fb 100644
--- a/sdk/tests/nav-pathfind.test.ts
+++ b/sdk/tests/nav-pathfind.test.ts
@@ -99,7 +99,10 @@ describe('findPath', () => {
for (const c of path) expect(grid.isWalkable(c.x, c.y)).toBe(true);
});
- it('returns null for an unreachable goal', () => {
+ // A goal nothing can reach is answered with the route to the nearest cell that
+ // can be. Whether it got there is the caller's to see: it named the goal, and
+ // the last cell either is that goal or is not.
+ it('stops at the wall when the goal is on the far side of it', () => {
const grid = gridFromAscii([
'..#..',
'..#..',
@@ -107,7 +110,10 @@ describe('findPath', () => {
'..#..',
'..#..',
]);
- expect(findPath(grid, { x: 0, y: 0 }, { x: 4, y: 0 }, { snapRadius: 0 })).toBeNull();
+ const path = findPath(grid, { x: 0, y: 0 }, { x: 4, y: 0 }, { snapRadius: 0 })!;
+ expect(path).not.toBeNull();
+ expect(path[path.length - 1]).toEqual({ x: 1, y: 0 }); // against the wall
+ for (const c of path) expect(grid.isWalkable(c.x, c.y)).toBe(true);
});
it('does not cut diagonal corners', () => {
@@ -117,8 +123,10 @@ describe('findPath', () => {
'.#',
'#.',
]);
- // With a 2x2 fully pinched map there is no path at all.
- expect(findPath(grid, { x: 0, y: 0 }, { x: 1, y: 1 }, { snapRadius: 0 })).toBeNull();
+ // With a 2x2 fully pinched map there is nowhere to go: the route is the
+ // cell it started on and nothing more.
+ expect(findPath(grid, { x: 0, y: 0 }, { x: 1, y: 1 }, { snapRadius: 0 }))
+ .toEqual([{ x: 0, y: 0 }]);
});
it('4-connected paths never move diagonally', () => {
diff --git a/sdk/tests/nav-step.test.ts b/sdk/tests/nav-step.test.ts
index 203795267..f786e55e7 100644
--- a/sdk/tests/nav-step.test.ts
+++ b/sdk/tests/nav-step.test.ts
@@ -137,7 +137,9 @@ describe('stepNavigation', () => {
expect(150 - stopped.x).toBeGreaterThan(20);
});
- it('does not move an agent toward an unreachable target', () => {
+ // An agent sent somewhere it cannot get to walks to the door rather than
+ // standing where it was told, and stops on the near side of the wall.
+ it('walks an agent as close to an unreachable target as it can get', () => {
// Full wall column with no gap.
const nav = new Navigation();
const grid = new NavGrid({ width: 10, height: 3, cellSize: 10 });
@@ -147,10 +149,9 @@ describe('stepNavigation', () => {
const runtimes = new Map<Entity, AgentRuntime>();
const e = world.spawnAgent(0, 0, { speed: 100, repathInterval: 0, hasTarget: true, targetX: 90, targetY: 0 });
- stepNavigation(world, nav, 0.1, runtimes);
- expect(world.pos(e)).toEqual({ x: 0, y: 0 });
- expect(world.agent(e).arrived).toBe(false);
- expect(runtimes.get(e)?.reachable).toBe(false);
+ for (let i = 0; i < 20; i++) stepNavigation(world, nav, 0.1, runtimes);
+ expect(world.pos(e).x).toBeGreaterThan(30);
+ expect(world.pos(e).x).toBeLessThan(50); // the wall stands at x = 50
});
it('replans when the target moves', () => {
diff --git a/sdk/tests/navmesh-build.test.ts b/sdk/tests/navmesh-build.test.ts
index 482a59b68..57b8044cc 100644
--- a/sdk/tests/navmesh-build.test.ts
+++ b/sdk/tests/navmesh-build.test.ts
@@ -176,10 +176,12 @@ describe('buildNavMesh', () => {
return s;
};
const low = bake(stepped(30));
- expect(low.findWorldPath({ x: -300, y: 0, z: 0 }, { x: 300, y: 30, z: 0 })).not.toBeNull();
+ expect(reaches(low.findWorldPath({ x: -300, y: 0, z: 0 }, { x: 300, y: 30, z: 0 }),
+ { x: 300, y: 30, z: 0 })).toBe(true);
const high = bake(stepped(200));
- expect(high.findWorldPath({ x: -300, y: 0, z: 0 }, { x: 300, y: 200, z: 0 })).toBeNull();
+ expect(reaches(high.findWorldPath({ x: -300, y: 0, z: 0 }, { x: 300, y: 200, z: 0 }),
+ { x: 300, y: 200, z: 0 })).toBe(false);
});
// Two floors with the SAME outline, one over the other: their corners land on
@@ -199,13 +201,17 @@ describe('buildNavMesh', () => {
expect(upper.y).toBeGreaterThan(290);
});
- // Two levels with no way between them: the mesh has both, and no route.
- it('reports no route from the ground to a deck nothing reaches', () => {
+ // Two levels with no way between them: the mesh has both, and the route to the
+ // deck gets no further than the ground under it.
+ it('gets no further than the ground when nothing reaches the deck', () => {
const s = soup();
addBox(s, { x: 0, y: -20, z: 0 }, { x: 400, y: 20, z: 400 });
addBox(s, { x: 0, y: 300, z: 0 }, { x: 400, y: 20, z: 200 });
const mesh = bake(s);
- expect(mesh.findWorldPath({ x: 0, y: 0, z: -350 }, { x: 0, y: 320, z: 0 })).toBeNull();
+ const goal = { x: 0, y: 320, z: 0 };
+ const path = mesh.findWorldPath({ x: 0, y: 0, z: -350 }, goal)!;
+ expect(reaches(path, goal)).toBe(false);
+ expect(path[path.length - 1]!.y).toBeLessThan(50);
});
// A volume covering a city at a centimetre a cell is not a slow bake, it is
@@ -220,6 +226,37 @@ describe('buildNavMesh', () => {
expect(huge.polyCount).toBe(0);
});
+ // A goal that is not on the mesh at all is still a direction to walk in, and
+ // the walk still has to be a walk: round the wall, not through it.
+ it('walks round the wall toward a goal off the mesh entirely', () => {
+ const s = soup();
+ addBox(s, { x: 0, y: -20, z: 0 }, { x: 500, y: 20, z: 500 });
+ // A wall across all but the east end of the floor.
+ addBox(s, { x: -150, y: 80, z: 0 }, { x: 350, y: 100, z: 20 });
+ const mesh = bake(s);
+
+ const goal = { x: -300, y: 0, z: 900 }; // beyond the floor, past the wall
+ const path = mesh.findWorldPath({ x: -300, y: 0, z: -300 }, goal)!;
+ expect(path).not.toBeNull();
+ expect(Math.hypot(path[path.length - 1]!.x - goal.x,
+ path[path.length - 1]!.z - goal.z)).toBeGreaterThan(200);
+ // Every step of it is somewhere an agent may stand — a route that gave up
+ // and pointed at the nearest place would cut straight through the wall.
+ for (let i = 1; i < path.length; i++) {
+ const a = path[i - 1]!;
+ const b = path[i]!;
+ const steps = Math.max(2, Math.ceil(Math.hypot(b.x - a.x, b.z - a.z) / 20));
+ for (let t = 0; t <= steps; t++) {
+ const at = {
+ x: a.x + (b.x - a.x) * (t / steps),
+ y: a.y + (b.y - a.y) * (t / steps),
+ z: a.z + (b.z - a.z) * (t / steps),
+ };
+ expect(mesh.findPoly(at)).toBeGreaterThanOrEqual(0);
+ }
+ }
+ });
+
it('hands its shape to a drawer as faces and the edges they stop at', () => {
const s = soup();
addBox(s, { x: 0, y: -20, z: 0 }, { x: 400, y: 20, z: 400 });
@@ -245,3 +282,11 @@ function pathLength(path: Vec3[]): number {
}
return total;
}
+
+/** Whether a route actually got where it was sent — a route that could not is
+ * answered with the way to the nearest place it could, not with nothing. */
+function reaches(path: Vec3[] | null, to: Vec3, slack = 120): boolean {
+ if (!path || path.length === 0) return false;
+ const end = path[path.length - 1]!;
+ return Math.hypot(end.x - to.x, end.y - to.y, end.z - to.z) < slack;
+}