skip to content

In a strategy game where two hundred units request grid paths across a large map through chokepoints, how do you keep pathfinding inside the frame budget?

level: seniorimportance: should knowfreq 60%

answer

  1. reject unreachable goals first
  2. spread searches across frames
  3. search less of the map
  4. symmetric paths on uniform grids
  5. abstract clusters, then refine

basics

~20 s

Reject unreachable goals with precomputed connected-region labels, queue requests and time-slice them under a per-frame expansion budget, share routes, and shrink each search: Jump Point Search on uniform-cost grids, HPA* clusters for long routes, a weighted heuristic for low-priority units.

solid answer

~40 s

Start with the case that blows up: an unreachable goal makes A* exhaust every reachable cell before failing, so label connected regions ahead of time and reject such requests with one lookup. Then stop paying all at once: put requests in a priority queue and process them under a fixed node-expansion budget per frame, letting units start moving while their search finishes. Then cut the work per search. On uniform-cost 8-way grids, **Jump Point Search** prunes symmetric paths and still returns optimal-length paths with far fewer open-list operations. For long routes, **HPA\*** splits the map into clusters, searches a small abstract graph of cluster entrances, and refines only the chosen route. A weighted heuristic trades a bounded detour for speed on low-priority units, and a group ordered to one spot can share one path.

go deeper

for a junior

Recall that A* cost grows with the nodes it expands, and that an unreachable goal is the worst case.

for a middle

Explain time-slicing a search across frames and the core idea of JPS and HPA*: skip symmetric paths, or search a smaller abstract graph.

for a senior

Combine reachability labels, a prioritised time-sliced queue, JPS or HPA* chosen by terrain and route length, and validation against map edits, and justify each by where the expansions go.

for a principal

Decide which units deserve optimal paths and which get bounded or coarse ones, and set the per-frame pathfinding budget with the rest of the AI team.

## Where the time goes Take the scenario's figures: a 1,024 by 1,024 grid, about 1.05 million cells, with 8-way movement, and two hundred units given orders at once, many across the map through a few chokepoints. A* cost is roughly proportional to the nodes it expands, each with an open-list operation. A long route on an open map with a good heuristic may expand thousands of nodes; a route around a wall may expand far more; and a request to an **unreachable** goal expands every cell reachable from the start before it can report failure, up to the whole map. Two hundred such requests in one frame cannot fit in any frame budget. The fixes fall into three groups: avoid searches, spread them out, and make each one smaller. ## Avoid and share searches - **Reachability labels.** Flood-fill the walkable grid once and give every cell the id of its connected region. A request whose start and goal have different ids is rejected, or redirected to the nearest reachable cell, with one comparison. When the map changes, update the labels for the affected area. - **Shared paths.** Units ordered as a group to one spot can share one route, with formation offsets applied while following it. - **Caching.** Repeated requests between the same regions, such as harvesters running between a base and a resource, can reuse a cached route until the map changes. - When very many units share a single goal, a flow field computed once from the goal serves all of them; building and following it belongs with crowd movement rather than route search. ## Spread the work: time-slicing Search does not have to finish in the frame it was requested. 1. Requests enter a **priority queue**: units near the player or in combat first, distant units later. 2. Each frame the pathfinder runs searches until it has spent a **fixed budget of node expansions**; a search that runs out is suspended with its open and closed lists intact and resumed next frame. 3. While waiting, a unit can start toward its goal along a straight line, or a coarse route, so the order looks acknowledged. 4. When a suspended search completes, validate it against any map changes made in the meantime, and replan if a change touched its cells. The budget is a tuning value set from profiling on target hardware, not a universal number. ## Make each search smaller: Jump Point Search On a **uniform-cost** grid, many paths have the same length and differ only in the order of their moves; A* explores all of these **symmetric** paths. **Jump Point Search (JPS)** prunes them using the direction of travel. From each node it considers only the neighbours a shortest path could need, and it **jumps** in a straight line, or diagonally while checking straight jumps from each diagonal step, until it reaches the goal or a node with a **forced neighbour**, a cell next to an obstacle that a shortest path might have to turn into. Only those **jump points** enter the open list. JPS returns paths of the same optimal length as A* on such grids, needs no preprocessing, and copes with map edits; it relies on every cell costing the same, so weighted terrain needs a different approach. A variant precomputes jump distances for static maps to go faster still. ## Make each search smaller: HPA* **Hierarchical Pathfinding A\* (HPA\*)** works on long routes: - Split the grid into square **clusters**. Where two neighbouring clusters share an open stretch of border, place **transition** nodes on each side. - Inside each cluster, precompute the cost between every pair of its transitions with a local search; those become edges of a small **abstract graph**. - For a query, connect the start and goal to the transitions of their own clusters, search the abstract graph, then refine each abstract edge into cells, or reuse cached refinements. - Paths bend at cluster entrances, so they are **near-optimal** rather than optimal; a smoothing pass recovers most of the difference. - A map change only requires recomputing the edges of the clusters it touched. Chokepoints suit HPA* naturally: they become a few transitions, and the abstract graph routes through them without expanding the open areas on either side. ## Choosing | Technique | Assumes | Optimal | Preprocessing | Map edits | |---|---|---|---|---| | Plain A* | Any graph | Yes, with an admissible heuristic | None | Free | | Weighted A* | Any graph | Within factor `w` | None | Free | | JPS | Uniform-cost grid | Yes | None | Free | | HPA* | Grid or graph split into clusters | Near-optimal | Abstract graph | Recompute touched clusters | In practice these combine: reachability labels and a time-sliced queue always, JPS or HPA* depending on terrain costs and route length, and a weighted heuristic for units nobody is watching.

  • What does HPA* cost compared with plain A*?
    Paths are near-optimal, bending at cluster entrances, so they need smoothing. The abstract graph costs preprocessing time and memory, and a map edit forces recomputing the touched clusters' internal edges. Each query also pays a small local search to connect the start and goal to their clusters' transitions.
  • How do you keep reachability labels valid when a bridge is destroyed?
    Removing a connection can split one region into two or more, so re-flood the region that contained the bridge and assign new ids to the parts. Adding a connection only merges regions, which is a relabel or a union of two ids. Units whose current path crossed the bridge must replan.
  • What breaks if a time-sliced search runs while the map changes under it?
    Its open and closed lists were built against the old map, so the finished path may cross a newly blocked cell or miss a new shortcut. Stamp each search with the map version it started on, and on completion validate the path against changes since then, replanning the affected part.

  • Run every unit's A* to completion in the frame the order was given.
  • An unreachable goal is cheap because A* fails fast.
  • Jump Point Search works on weighted terrain without changes.
  • HPA* always returns the optimal path.
  • A faster priority queue alone will fix two hundred long searches.