6 Module ASTAR
gullradriel edited this page 2026-10-29 20:54:40 +01:00

ASTAR: A* pathfinding for 2D/3D grids

Structures

Name
struct ASTAR_PATH
The computed path result.
struct ASTAR_NODE
A single node in the resulting path.
struct ASTAR_HEAP_NODE
Min-heap entry for the open list priority queue.
struct ASTAR_HEAP
Binary min-heap (priority queue) for the open list.
struct ASTAR_GRID
Grid structure holding walkability, costs, and dimensions.
struct ASTAR_CELL
Internal node data used during pathfinding.

Types

Name
enum ASTAR_HEURISTIC { ASTAR_HEURISTIC_MANHATTAN = 0, ASTAR_HEURISTIC_EUCLIDEAN, ASTAR_HEURISTIC_CHEBYSHEV}
Heuristic function selection for h(n) estimation.

Functions

Name
void n_astar_path_free(ASTAR_PATH * path)
free a path returned by n_astar_find_path
int n_astar_heuristic(int x1, int y1, int z1, int x2, int y2, int z2, ASTAR_HEURISTIC heuristic)
compute heuristic distance between two points (x1000)
void n_astar_grid_set_walkable(ASTAR_GRID * grid, int x, int y, int z, uint8_t walkable)
set a cell's walkability (1=passable, 0=blocked)
void n_astar_grid_set_rect_blocked(ASTAR_GRID * grid, int x1, int y1, int z1, int x2, int y2, int z2)
set a rectangular region as blocked
void n_astar_grid_set_cost(ASTAR_GRID * grid, int x, int y, int z, int cost)
set a cell's movement cost multiplier (x1000)
ASTAR_GRID * n_astar_grid_new(int width, int height, int depth)
create a new grid for pathfinding, all cells default to walkable
uint8_t n_astar_grid_get_walkable(const ASTAR_GRID * grid, int x, int y, int z)
get a cell's walkability (1=passable, 0=blocked, 0 if out of bounds)
int n_astar_grid_get_cost(const ASTAR_GRID * grid, int x, int y, int z)
get a cell's movement cost multiplier (x1000)
void n_astar_grid_free(ASTAR_GRID * grid)
free a grid and all its internal data
ASTAR_PATH * n_astar_find_path(const ASTAR_GRID * grid, int sx, int sy, int sz, int gx, int gy, int gz, int diagonal, ASTAR_HEURISTIC heuristic)
find a path using A* search, returns NULL if no path exists

Defines

Name
ASTAR_SEARCH_MARGIN
Search-window margin (in cells) added around the bounding box of the start and goal.
ASTAR_NODE_OPEN
Node is in the open list.
ASTAR_NODE_NONE
Node has not been visited.
ASTAR_NODE_CLOSED
Node has been fully evaluated.
ASTAR_COST_DIAGONAL3D
Default cost for 3D diagonal movement (sqrt(3)*1000).
ASTAR_COST_DIAGONAL
Default cost for 2D diagonal movement (sqrt(2)*1000).
ASTAR_COST_CARDINAL
Default cost for straight movement (fixed-point x1000).
ASTAR_CARDINAL_ONLY
Movement mode: 4-dir (2D) or 6-dir (3D).
ASTAR_ALLOW_DIAGONAL
Movement mode: 8-dir (2D) or 26-dir (3D).

Types Documentation

enum ASTAR_HEURISTIC

Enumerator Value Description
ASTAR_HEURISTIC_MANHATTAN 0 sum of axis deltas (optimal for 4-dir)
ASTAR_HEURISTIC_EUCLIDEAN straight-line distance
ASTAR_HEURISTIC_CHEBYSHEV max of axis deltas (optimal for 8-dir)

Heuristic function selection for h(n) estimation.

Functions Documentation

function n_astar_path_free

void n_astar_path_free(
    ASTAR_PATH * path
)

free a path returned by n_astar_find_path

Parameters:

  • path path to free (safe to pass NULL)

Free a path returned by n_astar_find_path.

free a path returned by n_astar_find_path

function n_astar_heuristic

int n_astar_heuristic(
    int x1,
    int y1,
    int z1,
    int x2,
    int y2,
    int z2,
    ASTAR_HEURISTIC heuristic
)

compute heuristic distance between two points (x1000)

Parameters:

  • x1 first point X
  • y1 first point Y
  • z1 first point Z
  • x2 second point X
  • y2 second point Y
  • z2 second point Z
  • heuristic the heuristic to use

Return: estimated distance in fixed-point (x1000)

Compute heuristic distance between two 3D points.

compute heuristic distance between two points (x1000)

function n_astar_grid_set_walkable

void n_astar_grid_set_walkable(
    ASTAR_GRID * grid,
    int x,
    int y,
    int z,
    uint8_t walkable
)

set a cell's walkability (1=passable, 0=blocked)

Parameters:

  • grid the grid
  • x cell X coordinate
  • y cell Y coordinate
  • z cell Z coordinate
  • walkable 1=passable, 0=blocked

Set a cell's walkability.

set a cell's walkability (1=passable, 0=blocked)

function n_astar_grid_set_rect_blocked

void n_astar_grid_set_rect_blocked(
    ASTAR_GRID * grid,
    int x1,
    int y1,
    int z1,
    int x2,
    int y2,
    int z2
)

set a rectangular region as blocked

Parameters:

  • grid the grid
  • x1 start corner X (inclusive)
  • y1 start corner Y (inclusive)
  • z1 start corner Z (inclusive)
  • x2 end corner X (inclusive)
  • y2 end corner Y (inclusive)
  • z2 end corner Z (inclusive)

Set a rectangular region as blocked (wall).

set a rectangular region as blocked

function n_astar_grid_set_cost

void n_astar_grid_set_cost(
    ASTAR_GRID * grid,
    int x,
    int y,
    int z,
    int cost
)

set a cell's movement cost multiplier (x1000)

Parameters:

  • grid the grid
  • x cell X coordinate
  • y cell Y coordinate
  • z cell Z coordinate
  • cost cost multiplier (x1000)

Set a cell's movement cost multiplier.

set a cell's movement cost multiplier (x1000)

function n_astar_grid_new

ASTAR_GRID * n_astar_grid_new(
    int width,
    int height,
    int depth
)

create a new grid for pathfinding, all cells default to walkable

Parameters:

  • width grid width (X)
  • height grid height (Y)
  • depth grid depth (Z, use 1 for 2D)

Return: new grid or NULL. All cells default to walkable with cost 1000.

Create a new grid for A* pathfinding.

create a new grid for pathfinding, all cells default to walkable

function n_astar_grid_get_walkable

uint8_t n_astar_grid_get_walkable(
    const ASTAR_GRID * grid,
    int x,
    int y,
    int z
)

get a cell's walkability (1=passable, 0=blocked, 0 if out of bounds)

Parameters:

  • grid the grid
  • x cell X coordinate
  • y cell Y coordinate
  • z cell Z coordinate

Return: 1 if passable, 0 if blocked or out of bounds

Get a cell's walkability.

get a cell's walkability (1=passable, 0=blocked, 0 if out of bounds)

function n_astar_grid_get_cost

int n_astar_grid_get_cost(
    const ASTAR_GRID * grid,
    int x,
    int y,
    int z
)

get a cell's movement cost multiplier (x1000)

Parameters:

  • grid the grid
  • x cell X coordinate
  • y cell Y coordinate
  • z cell Z coordinate

Return: cost multiplier (x1000), or 0 if out of bounds

Get a cell's movement cost multiplier.

get a cell's movement cost multiplier (x1000)

function n_astar_grid_free

void n_astar_grid_free(
    ASTAR_GRID * grid
)

free a grid and all its internal data

Parameters:

  • grid grid to free (safe to pass NULL)

Free a grid and all its internal data.

free a grid and all its internal data

function n_astar_find_path

ASTAR_PATH * n_astar_find_path(
    const ASTAR_GRID * grid,
    int sx,
    int sy,
    int sz,
    int gx,
    int gy,
    int gz,
    int diagonal,
    ASTAR_HEURISTIC heuristic
)

find a path using A* search, returns NULL if no path exists

Parameters:

  • grid the grid to search
  • sx start X
  • sy start Y
  • sz start Z
  • gx goal X
  • gy goal Y
  • gz goal Z
  • diagonal ASTAR_CARDINAL_ONLY or ASTAR_ALLOW_DIAGONAL
  • heuristic heuristic function to use

Return: path from start to goal, or NULL if no path exists. Caller must free with n_astar_path_free().

Find a path using A* search.

find a path using A* search, returns NULL if no path exists

Macros Documentation

define ASTAR_SEARCH_MARGIN

#define ASTAR_SEARCH_MARGIN 128

Search-window margin (in cells) added around the bounding box of the start and goal.

n_astar_find_path() allocates its per-call working set for this window only, not the whole grid, so a short path on a huge map costs a few KB instead of widthheightdepth cells. A route that must detour more than this many cells outside the start-goal box will not be found (the call returns NULL). Raise it if you need wider detours at the cost of a larger per-call allocation.

define ASTAR_NODE_OPEN

#define ASTAR_NODE_OPEN 1

Node is in the open list.

define ASTAR_NODE_NONE

#define ASTAR_NODE_NONE 0

Node has not been visited.

define ASTAR_NODE_CLOSED

#define ASTAR_NODE_CLOSED 2

Node has been fully evaluated.

define ASTAR_COST_DIAGONAL3D

#define ASTAR_COST_DIAGONAL3D 1732

Default cost for 3D diagonal movement (sqrt(3)*1000).

define ASTAR_COST_DIAGONAL

#define ASTAR_COST_DIAGONAL 1414

Default cost for 2D diagonal movement (sqrt(2)*1000).

define ASTAR_COST_CARDINAL

#define ASTAR_COST_CARDINAL 1000

Default cost for straight movement (fixed-point x1000).

define ASTAR_CARDINAL_ONLY

#define ASTAR_CARDINAL_ONLY 0

Movement mode: 4-dir (2D) or 6-dir (3D).

define ASTAR_ALLOW_DIAGONAL

#define ASTAR_ALLOW_DIAGONAL 1

Movement mode: 8-dir (2D) or 26-dir (3D).