Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
n_astar.h
Go to the documentation of this file.
1/*
2 * Nilorea Library
3 * Copyright (C) 2005-2026 Castagnier Mickael
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 */
18
55#ifndef N_ASTAR_H
56#define N_ASTAR_H
57
58#ifdef __cplusplus
59extern "C" {
60#endif
61
67#include <stdlib.h>
68#include <stdint.h>
69
70/*---------------------------------------------------------------------------
71 * Constants
72 *-------------------------------------------------------------------------*/
73
75#define ASTAR_CARDINAL_ONLY 0
77#define ASTAR_ALLOW_DIAGONAL 1
78
80#define ASTAR_COST_CARDINAL 1000
82#define ASTAR_COST_DIAGONAL 1414
84#define ASTAR_COST_DIAGONAL3D 1732
85
87#define ASTAR_NODE_NONE 0
89#define ASTAR_NODE_OPEN 1
91#define ASTAR_NODE_CLOSED 2
92
93/*---------------------------------------------------------------------------
94 * Heuristic Types
95 *-------------------------------------------------------------------------*/
96
103
104/*---------------------------------------------------------------------------
105 * Data Structures
106 *-------------------------------------------------------------------------*/
107
109typedef struct ASTAR_NODE {
110 int x;
111 int y;
112 int z;
113} ASTAR_NODE;
114
116typedef struct ASTAR_PATH {
118 int length;
119 int cost;
120} ASTAR_PATH;
121
123typedef struct ASTAR_CELL {
124 int g;
125 int h;
126 int f;
130 uint8_t status;
131} ASTAR_CELL;
132
134typedef struct ASTAR_HEAP_NODE {
135 int x, y, z;
136 int f;
138
145
147typedef struct ASTAR_GRID {
148 int width;
149 int height;
150 int depth;
151 uint8_t* walkable;
152 int* cost;
153} ASTAR_GRID;
154
155/*---------------------------------------------------------------------------
156 * API Functions
157 *-------------------------------------------------------------------------*/
158
160ASTAR_GRID* n_astar_grid_new(int width, int height, int depth);
162void n_astar_grid_free(ASTAR_GRID* grid);
164void n_astar_grid_set_walkable(ASTAR_GRID* grid, int x, int y, int z, uint8_t walkable);
166uint8_t n_astar_grid_get_walkable(const ASTAR_GRID* grid, int x, int y, int z);
168void n_astar_grid_set_cost(ASTAR_GRID* grid, int x, int y, int z, int cost);
170int n_astar_grid_get_cost(const ASTAR_GRID* grid, int x, int y, int z);
173 int x1,
174 int y1,
175 int z1,
176 int x2,
177 int y2,
178 int z2);
181 int sx,
182 int sy,
183 int sz,
184 int gx,
185 int gy,
186 int gz,
187 int diagonal,
188 ASTAR_HEURISTIC heuristic);
190void n_astar_path_free(ASTAR_PATH* path);
192int n_astar_heuristic(int x1, int y1, int z1, int x2, int y2, int z2, ASTAR_HEURISTIC heuristic);
193
198#ifdef __cplusplus
199}
200#endif
201
202#endif /* N_ASTAR_H */
int f
priority (f = g + h)
Definition n_astar.h:136
int capacity
allocated capacity
Definition n_astar.h:143
int parent_z
parent cell Z
Definition n_astar.h:129
int parent_x
parent cell X (-1 if none)
Definition n_astar.h:127
int x
grid X coordinate
Definition n_astar.h:110
int depth
grid depth (Z axis, 1 for 2D)
Definition n_astar.h:150
int cost
total path cost (x1000 fixed-point)
Definition n_astar.h:119
uint8_t status
ASTAR_NODE_NONE / OPEN / CLOSED.
Definition n_astar.h:130
int width
grid width (X axis)
Definition n_astar.h:148
int f
g + h
Definition n_astar.h:126
ASTAR_NODE * nodes
array of path nodes from start to goal
Definition n_astar.h:117
int y
grid Y coordinate
Definition n_astar.h:111
int height
grid height (Y axis)
Definition n_astar.h:149
int g
cost from start to this cell
Definition n_astar.h:124
int z
grid coordinates
Definition n_astar.h:135
uint8_t * walkable
walkability map: 1=passable, 0=blocked
Definition n_astar.h:151
int * cost
per-cell movement cost multiplier (x1000)
Definition n_astar.h:152
ASTAR_HEAP_NODE * data
heap array
Definition n_astar.h:141
int size
current number of elements
Definition n_astar.h:142
int h
heuristic estimate to goal
Definition n_astar.h:125
int parent_y
parent cell Y
Definition n_astar.h:128
int z
grid Z coordinate (0 for 2D)
Definition n_astar.h:112
int length
number of nodes in the path
Definition n_astar.h:118
int n_astar_grid_get_cost(const ASTAR_GRID *grid, int x, int y, int z)
get a cell's movement cost multiplier (x1000)
Definition n_astar.c:309
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
Definition n_astar.c:477
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)
Definition n_astar.c:296
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)
Definition n_astar.c:283
ASTAR_HEURISTIC
Heuristic function selection for h(n) estimation.
Definition n_astar.h:98
void n_astar_grid_free(ASTAR_GRID *grid)
free a grid and all its internal data
Definition n_astar.c:255
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)
Definition n_astar.c:166
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
Definition n_astar.c:324
void n_astar_path_free(ASTAR_PATH *path)
free a path returned by n_astar_find_path
Definition n_astar.c:707
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)
Definition n_astar.c:270
ASTAR_GRID * n_astar_grid_new(int width, int height, int depth)
create a new grid for pathfinding, all cells default to walkable
Definition n_astar.c:219
@ ASTAR_HEURISTIC_EUCLIDEAN
straight-line distance
Definition n_astar.h:100
@ ASTAR_HEURISTIC_CHEBYSHEV
max of axis deltas (optimal for 8-dir)
Definition n_astar.h:101
@ ASTAR_HEURISTIC_MANHATTAN
sum of axis deltas (optimal for 4-dir)
Definition n_astar.h:99
Internal node data used during pathfinding.
Definition n_astar.h:123
Grid structure holding walkability, costs, and dimensions.
Definition n_astar.h:147
Binary min-heap (priority queue) for the open list.
Definition n_astar.h:140
Min-heap entry for the open list priority queue.
Definition n_astar.h:134
A single node in the resulting path.
Definition n_astar.h:109
The computed path result.
Definition n_astar.h:116