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 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14 * implied. See the License for the specific language governing
15 * permissions and limitations under the License.
16 *
17 * SPDX-License-Identifier: Apache-2.0
18 */
19
56#ifndef N_ASTAR_H
57#define N_ASTAR_H
58
59#ifdef __cplusplus
60extern "C" {
61#endif
62
68#include <stdlib.h>
69#include <stdint.h>
70
71/* Constants */
72
74#define ASTAR_CARDINAL_ONLY 0
76#define ASTAR_ALLOW_DIAGONAL 1
77
79#define ASTAR_COST_CARDINAL 1000
81#define ASTAR_COST_DIAGONAL 1414
83#define ASTAR_COST_DIAGONAL3D 1732
84
86#define ASTAR_NODE_NONE 0
88#define ASTAR_NODE_OPEN 1
90#define ASTAR_NODE_CLOSED 2
91
99#ifndef ASTAR_SEARCH_MARGIN
100#define ASTAR_SEARCH_MARGIN 128
101#endif
102
103/* Heuristic Types */
104
111
112/* Data Structures */
113
115typedef struct ASTAR_NODE {
116 int x;
117 int y;
118 int z;
119} ASTAR_NODE;
120
122typedef struct ASTAR_PATH {
124 int length;
125 int cost;
126} ASTAR_PATH;
127
129typedef struct ASTAR_CELL {
130 int g;
131 int h;
132 int f;
136 uint8_t status;
137} ASTAR_CELL;
138
140typedef struct ASTAR_HEAP_NODE {
141 int x, y, z;
142 int f;
144
151
153typedef struct ASTAR_GRID {
154 int width;
155 int height;
156 int depth;
157 uint8_t* walkable;
158 int* cost;
159} ASTAR_GRID;
160
161/* API Functions */
162
164ASTAR_GRID* n_astar_grid_new(int width, int height, int depth);
166void n_astar_grid_free(ASTAR_GRID* grid);
168void n_astar_grid_set_walkable(ASTAR_GRID* grid, int x, int y, int z, uint8_t walkable);
170uint8_t n_astar_grid_get_walkable(const ASTAR_GRID* grid, int x, int y, int z);
172void n_astar_grid_set_cost(ASTAR_GRID* grid, int x, int y, int z, int cost);
174int n_astar_grid_get_cost(const ASTAR_GRID* grid, int x, int y, int z);
177 int x1,
178 int y1,
179 int z1,
180 int x2,
181 int y2,
182 int z2);
185 int sx,
186 int sy,
187 int sz,
188 int gx,
189 int gy,
190 int gz,
191 int diagonal,
192 ASTAR_HEURISTIC heuristic);
194void n_astar_path_free(ASTAR_PATH* path);
196int n_astar_heuristic(int x1, int y1, int z1, int x2, int y2, int z2, ASTAR_HEURISTIC heuristic);
197
202#ifdef __cplusplus
203}
204#endif
205
206#endif /* N_ASTAR_H */
int f
priority (f = g + h)
Definition n_astar.h:142
int capacity
allocated capacity
Definition n_astar.h:149
int parent_z
parent cell Z
Definition n_astar.h:135
int parent_x
parent cell X (-1 if none)
Definition n_astar.h:133
int x
grid X coordinate
Definition n_astar.h:116
int depth
grid depth (Z axis, 1 for 2D)
Definition n_astar.h:156
int cost
total path cost (x1000 fixed-point)
Definition n_astar.h:125
uint8_t status
ASTAR_NODE_NONE / OPEN / CLOSED.
Definition n_astar.h:136
int width
grid width (X axis)
Definition n_astar.h:154
int f
g + h
Definition n_astar.h:132
ASTAR_NODE * nodes
array of path nodes from start to goal
Definition n_astar.h:123
int y
grid Y coordinate
Definition n_astar.h:117
int height
grid height (Y axis)
Definition n_astar.h:155
int g
cost from start to this cell
Definition n_astar.h:130
int z
grid coordinates
Definition n_astar.h:141
uint8_t * walkable
walkability map: 1=passable, 0=blocked
Definition n_astar.h:157
int * cost
per-cell movement cost multiplier (x1000)
Definition n_astar.h:158
ASTAR_HEAP_NODE * data
heap array
Definition n_astar.h:147
int size
current number of elements
Definition n_astar.h:148
int h
heuristic estimate to goal
Definition n_astar.h:131
int parent_y
parent cell Y
Definition n_astar.h:134
int z
grid Z coordinate (0 for 2D)
Definition n_astar.h:118
int length
number of nodes in the path
Definition n_astar.h:124
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:325
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:485
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:312
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:299
ASTAR_HEURISTIC
Heuristic function selection for h(n) estimation.
Definition n_astar.h:106
void n_astar_grid_free(ASTAR_GRID *grid)
free a grid and all its internal data
Definition n_astar.c:271
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:184
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:340
void n_astar_path_free(ASTAR_PATH *path)
free a path returned by n_astar_find_path
Definition n_astar.c:759
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:286
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:235
@ ASTAR_HEURISTIC_EUCLIDEAN
straight-line distance
Definition n_astar.h:108
@ ASTAR_HEURISTIC_CHEBYSHEV
max of axis deltas (optimal for 8-dir)
Definition n_astar.h:109
@ ASTAR_HEURISTIC_MANHATTAN
sum of axis deltas (optimal for 4-dir)
Definition n_astar.h:107
Internal node data used during pathfinding.
Definition n_astar.h:129
Grid structure holding walkability, costs, and dimensions.
Definition n_astar.h:153
Binary min-heap (priority queue) for the open list.
Definition n_astar.h:146
Min-heap entry for the open list priority queue.
Definition n_astar.h:140
A single node in the resulting path.
Definition n_astar.h:115
The computed path result.
Definition n_astar.h:122