Navigation · class

NavSearch

A* over a NavGraph, allocating nothing per query and answering the same route every time.

Explained in Navigation.

class NavSearch
import { NavSearch } from '@driftengine/nav';

In depth

Reused rather than constructed per search, because the scratch is proportional to the graph and a world with a dozen agents re-pathing on a schedule would otherwise allocate a dozen arrays the size of the map every time somebody changed their mind. One of these per thread of control; a search is not re-entrant and says so.

The route is a function of the graph and the two endpoints, and of nothing else. Two runs, two machines, a replay: same nodes out. That takes more than avoiding randomness — a heap pops ties in whatever order it happens to hold them, and two equally good routes through a symmetric world are exactly the case a road network produces constantly. So ties break on the node index, which is arbitrary but stable, and the sequence is frozen the same way the engine's RNG is.

The heuristic is straight-line distance, which is admissible precisely because buildNavGraph refuses an edge cheaper than its own span. That refusal is what makes the first route this returns the cheapest one rather than merely a good one, and it is why it is a build error rather than a note.

Constructor

new

constructor(graph: NavGraph)
ParameterTypeDescription
graphNavGraph

Accessors

NameTypeDescription
expandedgetnumberNodes taken off the heap by the last find.

Methods

find

find(from: number, to: number, outPath: Uint32Array): number

Fill outPath with the node indices from from to to, and return how many there are.

ParameterTypeDescription
fromnumber
tonumber
outPathUint32Array
More

Zero means there is no route, which is a real answer and not an error: a road network with a bridge out is a graph with two components, and an agent that asks for the other side has to be told rather than thrown at inside a frame.

A path longer than outPath is refused by returning zero rather than by writing a prefix, because half a route is worse than none — an agent would follow it confidently into the middle of nowhere and stop.