Skip to main content

Module pathfind

Module pathfind 

Source
Expand description

Path finding (banished-path) and the terrain (banished-terrain), from their own crates. Path finding over the map’s cells: the walkable regions and the A* pathfinder.

  • regions gives every walkable cell the number of its connected region. A request between two regions fails at once, without a search.
  • Pathfinder is the A* search, with a cost for each cell type (mode_costs).
  • request checks a request as the original does, then searches.
  • nearest_neighbour finds the allowed cell next to a cell that is nearest to a target.

The original solves paths on a worker thread, and a walker polls for the result. The port solves a path when it is requested; the walker still reads the result on its next update, so the timing is the same.

See Citizens for how citizens use it.

Structs§

Pathfinder
The pathfinder: the map’s cells, the cost of each cell type, and the state of the A* search.

Constants§

MAX_PATH
Longest path a search returns.
WALKABLE
Cell types a walker may enter (0–4, 9–11).
WATER
Water and blocking types (5–8): requests touching them skip the region check.

Functions§

bit
The bit of a cell type in a type mask such as WALKABLE.
mode_costs
Per-type cost tables a mover selects (MoveTo) and whether the search may step diagonally.
nearest_neighbour
Of the 8 cells around cell whose type is in mask, the one nearest (squared cell distance) to toward; the first found wins ties (x outer, y inner).
regions
Label connected walkable cells in scan order, starting at 1 (0 = none).
request
Validate a request (both end types join the mask; without water types both ends must share a non-zero region), set the mask and search.
walkable
Whether a walker may enter a cell of this type (the test regions labels by).