@flare-engine/pathfinding 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +30 -0
- package/dist/index.d.mts +110 -0
- package/dist/index.d.ts +110 -0
- package/dist/index.js +696 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +655 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +42 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Grzegorz Otto
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
# @flare-engine/pathfinding
|
|
2
|
+
|
|
3
|
+
Grid pathfinding — A* (`findPath`), Jump-Point Search (`findPathJPS`) for uniform-cost grids, and a polygon navmesh fallback (`findNavMeshPath`). Pure data, no classes. Heuristics (`manhattanDistance`, `octileDistance`, `chebyshevDistance`, `euclideanDistance`) are exported as named functions for tree-shake.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
bun add @flare-engine/pathfinding
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```ts
|
|
14
|
+
import { createGrid, setWalkable, findPathJPS, octileDistance } from '@flare-engine/pathfinding';
|
|
15
|
+
|
|
16
|
+
const grid = createGrid({ width: 32, height: 32 });
|
|
17
|
+
setWalkable(grid, 5, 5, false);
|
|
18
|
+
|
|
19
|
+
const result = findPathJPS(grid, { x: 0, y: 0 }, { x: 30, y: 30 }, {
|
|
20
|
+
heuristic: octileDistance,
|
|
21
|
+
diagonal: true,
|
|
22
|
+
});
|
|
23
|
+
if (result.found) console.log(result.cells);
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
See [API spec](../../docs/design/api-specs.md#flare-enginepathfinding--8kb-gzip) and [skill reference](../../.github/skills/flare-pathfinding/SKILL.md).
|
|
27
|
+
|
|
28
|
+
## License
|
|
29
|
+
|
|
30
|
+
MIT
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { Vec2 } from '@flare-engine/math';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Grid data structure for tile-based pathfinding.
|
|
5
|
+
* Uses a flat Uint8Array: 0 = blocked, 1+ = walkable (value = movement cost).
|
|
6
|
+
*/
|
|
7
|
+
interface Grid {
|
|
8
|
+
readonly width: number;
|
|
9
|
+
readonly height: number;
|
|
10
|
+
readonly data: Uint8Array;
|
|
11
|
+
}
|
|
12
|
+
interface GridConfig {
|
|
13
|
+
width: number;
|
|
14
|
+
height: number;
|
|
15
|
+
/** Pre-filled grid data. Length must equal width * height. */
|
|
16
|
+
data?: Uint8Array;
|
|
17
|
+
/** Default cost for all cells when `data` is not provided. 0 = blocked, 1+ = walkable cost. Default 1. */
|
|
18
|
+
defaultCost?: number;
|
|
19
|
+
}
|
|
20
|
+
declare function createGrid(config: GridConfig): Grid;
|
|
21
|
+
declare function inBounds(grid: Grid, x: number, y: number): boolean;
|
|
22
|
+
declare function isWalkable(grid: Grid, x: number, y: number): boolean;
|
|
23
|
+
declare function getCost(grid: Grid, x: number, y: number): number;
|
|
24
|
+
declare function setWalkable(grid: Grid, x: number, y: number, walkable: boolean): void;
|
|
25
|
+
declare function setCost(grid: Grid, x: number, y: number, cost: number): void;
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* A* pathfinding on a Grid. Supports 4-directional and 8-directional movement
|
|
29
|
+
* with weighted costs. Uses a binary min-heap for the open set.
|
|
30
|
+
*/
|
|
31
|
+
|
|
32
|
+
interface PathResult {
|
|
33
|
+
/** Grid coordinates from start to end, inclusive. */
|
|
34
|
+
path: {
|
|
35
|
+
x: number;
|
|
36
|
+
y: number;
|
|
37
|
+
}[];
|
|
38
|
+
/** Total movement cost. */
|
|
39
|
+
cost: number;
|
|
40
|
+
/** Number of nodes explored. */
|
|
41
|
+
explored: number;
|
|
42
|
+
}
|
|
43
|
+
interface PathOptions {
|
|
44
|
+
/** Allow diagonal movement. Default: true. */
|
|
45
|
+
diagonal?: boolean;
|
|
46
|
+
/** Custom heuristic. Default: octile distance (diagonal) or Manhattan (4-dir). */
|
|
47
|
+
heuristic?: (ax: number, ay: number, bx: number, by: number) => number;
|
|
48
|
+
/** Max nodes to explore before giving up. Default: width * height. */
|
|
49
|
+
maxIterations?: number;
|
|
50
|
+
}
|
|
51
|
+
declare function manhattanDistance(ax: number, ay: number, bx: number, by: number): number;
|
|
52
|
+
declare function octileDistance(ax: number, ay: number, bx: number, by: number): number;
|
|
53
|
+
declare function chebyshevDistance(ax: number, ay: number, bx: number, by: number): number;
|
|
54
|
+
declare function euclideanDistance(ax: number, ay: number, bx: number, by: number): number;
|
|
55
|
+
declare function findPath(grid: Grid, startX: number, startY: number, endX: number, endY: number, options?: PathOptions): PathResult | null;
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Jump Point Search — an optimization over A* for uniform-cost grids.
|
|
59
|
+
* Only works when all walkable tiles have cost 1. Falls back to A* behavior
|
|
60
|
+
* if weighted costs are detected.
|
|
61
|
+
*
|
|
62
|
+
* JPS prunes symmetric paths by "jumping" along directions until hitting
|
|
63
|
+
* a wall, the goal, or a forced neighbor.
|
|
64
|
+
*/
|
|
65
|
+
|
|
66
|
+
interface JpsOptions {
|
|
67
|
+
/** Max nodes to explore before giving up. Default: width * height. */
|
|
68
|
+
maxIterations?: number;
|
|
69
|
+
/** Custom heuristic. Default: octile distance. */
|
|
70
|
+
heuristic?: (ax: number, ay: number, bx: number, by: number) => number;
|
|
71
|
+
}
|
|
72
|
+
declare function findPathJPS(grid: Grid, startX: number, startY: number, endX: number, endY: number, options?: JpsOptions): PathResult | null;
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Lightweight navmesh pathfinding for polygon-based environments.
|
|
76
|
+
* Supports convex polygon navigation with A* over the polygon graph
|
|
77
|
+
* and the Simple Stupid Funnel Algorithm for path smoothing.
|
|
78
|
+
*/
|
|
79
|
+
|
|
80
|
+
interface NavMeshPolygon {
|
|
81
|
+
readonly id: number;
|
|
82
|
+
/** Convex polygon vertices in winding order. */
|
|
83
|
+
readonly vertices: readonly Vec2[];
|
|
84
|
+
/** Ids of neighboring polygons (sharing an edge). */
|
|
85
|
+
readonly neighbors: readonly number[];
|
|
86
|
+
/** Shared edge per neighbor: [neighborId] → { a, b } portal endpoints. */
|
|
87
|
+
readonly portals: readonly Portal[];
|
|
88
|
+
}
|
|
89
|
+
interface Portal {
|
|
90
|
+
/** Neighbor polygon id. */
|
|
91
|
+
neighborId: number;
|
|
92
|
+
/** Left endpoint of the shared edge (from the perspective of travel). */
|
|
93
|
+
a: Vec2;
|
|
94
|
+
/** Right endpoint of the shared edge. */
|
|
95
|
+
b: Vec2;
|
|
96
|
+
}
|
|
97
|
+
interface NavMesh {
|
|
98
|
+
readonly polygons: readonly NavMeshPolygon[];
|
|
99
|
+
}
|
|
100
|
+
interface NavMeshPathResult {
|
|
101
|
+
/** Smoothed world-space waypoints. */
|
|
102
|
+
path: Vec2[];
|
|
103
|
+
/** Polygon ids traversed. */
|
|
104
|
+
corridor: number[];
|
|
105
|
+
}
|
|
106
|
+
declare function createNavMesh(polygons: NavMeshPolygon[]): NavMesh;
|
|
107
|
+
declare function findPolygonAt(mesh: NavMesh, x: number, y: number): number;
|
|
108
|
+
declare function findNavMeshPath(mesh: NavMesh, start: Vec2, end: Vec2): NavMeshPathResult | null;
|
|
109
|
+
|
|
110
|
+
export { type Grid, type GridConfig, type JpsOptions, type NavMesh, type NavMeshPathResult, type NavMeshPolygon, type PathOptions, type PathResult, type Portal, chebyshevDistance, createGrid, createNavMesh, euclideanDistance, findNavMeshPath, findPath, findPathJPS, findPolygonAt, getCost, inBounds, isWalkable, manhattanDistance, octileDistance, setCost, setWalkable };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { Vec2 } from '@flare-engine/math';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Grid data structure for tile-based pathfinding.
|
|
5
|
+
* Uses a flat Uint8Array: 0 = blocked, 1+ = walkable (value = movement cost).
|
|
6
|
+
*/
|
|
7
|
+
interface Grid {
|
|
8
|
+
readonly width: number;
|
|
9
|
+
readonly height: number;
|
|
10
|
+
readonly data: Uint8Array;
|
|
11
|
+
}
|
|
12
|
+
interface GridConfig {
|
|
13
|
+
width: number;
|
|
14
|
+
height: number;
|
|
15
|
+
/** Pre-filled grid data. Length must equal width * height. */
|
|
16
|
+
data?: Uint8Array;
|
|
17
|
+
/** Default cost for all cells when `data` is not provided. 0 = blocked, 1+ = walkable cost. Default 1. */
|
|
18
|
+
defaultCost?: number;
|
|
19
|
+
}
|
|
20
|
+
declare function createGrid(config: GridConfig): Grid;
|
|
21
|
+
declare function inBounds(grid: Grid, x: number, y: number): boolean;
|
|
22
|
+
declare function isWalkable(grid: Grid, x: number, y: number): boolean;
|
|
23
|
+
declare function getCost(grid: Grid, x: number, y: number): number;
|
|
24
|
+
declare function setWalkable(grid: Grid, x: number, y: number, walkable: boolean): void;
|
|
25
|
+
declare function setCost(grid: Grid, x: number, y: number, cost: number): void;
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* A* pathfinding on a Grid. Supports 4-directional and 8-directional movement
|
|
29
|
+
* with weighted costs. Uses a binary min-heap for the open set.
|
|
30
|
+
*/
|
|
31
|
+
|
|
32
|
+
interface PathResult {
|
|
33
|
+
/** Grid coordinates from start to end, inclusive. */
|
|
34
|
+
path: {
|
|
35
|
+
x: number;
|
|
36
|
+
y: number;
|
|
37
|
+
}[];
|
|
38
|
+
/** Total movement cost. */
|
|
39
|
+
cost: number;
|
|
40
|
+
/** Number of nodes explored. */
|
|
41
|
+
explored: number;
|
|
42
|
+
}
|
|
43
|
+
interface PathOptions {
|
|
44
|
+
/** Allow diagonal movement. Default: true. */
|
|
45
|
+
diagonal?: boolean;
|
|
46
|
+
/** Custom heuristic. Default: octile distance (diagonal) or Manhattan (4-dir). */
|
|
47
|
+
heuristic?: (ax: number, ay: number, bx: number, by: number) => number;
|
|
48
|
+
/** Max nodes to explore before giving up. Default: width * height. */
|
|
49
|
+
maxIterations?: number;
|
|
50
|
+
}
|
|
51
|
+
declare function manhattanDistance(ax: number, ay: number, bx: number, by: number): number;
|
|
52
|
+
declare function octileDistance(ax: number, ay: number, bx: number, by: number): number;
|
|
53
|
+
declare function chebyshevDistance(ax: number, ay: number, bx: number, by: number): number;
|
|
54
|
+
declare function euclideanDistance(ax: number, ay: number, bx: number, by: number): number;
|
|
55
|
+
declare function findPath(grid: Grid, startX: number, startY: number, endX: number, endY: number, options?: PathOptions): PathResult | null;
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Jump Point Search — an optimization over A* for uniform-cost grids.
|
|
59
|
+
* Only works when all walkable tiles have cost 1. Falls back to A* behavior
|
|
60
|
+
* if weighted costs are detected.
|
|
61
|
+
*
|
|
62
|
+
* JPS prunes symmetric paths by "jumping" along directions until hitting
|
|
63
|
+
* a wall, the goal, or a forced neighbor.
|
|
64
|
+
*/
|
|
65
|
+
|
|
66
|
+
interface JpsOptions {
|
|
67
|
+
/** Max nodes to explore before giving up. Default: width * height. */
|
|
68
|
+
maxIterations?: number;
|
|
69
|
+
/** Custom heuristic. Default: octile distance. */
|
|
70
|
+
heuristic?: (ax: number, ay: number, bx: number, by: number) => number;
|
|
71
|
+
}
|
|
72
|
+
declare function findPathJPS(grid: Grid, startX: number, startY: number, endX: number, endY: number, options?: JpsOptions): PathResult | null;
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Lightweight navmesh pathfinding for polygon-based environments.
|
|
76
|
+
* Supports convex polygon navigation with A* over the polygon graph
|
|
77
|
+
* and the Simple Stupid Funnel Algorithm for path smoothing.
|
|
78
|
+
*/
|
|
79
|
+
|
|
80
|
+
interface NavMeshPolygon {
|
|
81
|
+
readonly id: number;
|
|
82
|
+
/** Convex polygon vertices in winding order. */
|
|
83
|
+
readonly vertices: readonly Vec2[];
|
|
84
|
+
/** Ids of neighboring polygons (sharing an edge). */
|
|
85
|
+
readonly neighbors: readonly number[];
|
|
86
|
+
/** Shared edge per neighbor: [neighborId] → { a, b } portal endpoints. */
|
|
87
|
+
readonly portals: readonly Portal[];
|
|
88
|
+
}
|
|
89
|
+
interface Portal {
|
|
90
|
+
/** Neighbor polygon id. */
|
|
91
|
+
neighborId: number;
|
|
92
|
+
/** Left endpoint of the shared edge (from the perspective of travel). */
|
|
93
|
+
a: Vec2;
|
|
94
|
+
/** Right endpoint of the shared edge. */
|
|
95
|
+
b: Vec2;
|
|
96
|
+
}
|
|
97
|
+
interface NavMesh {
|
|
98
|
+
readonly polygons: readonly NavMeshPolygon[];
|
|
99
|
+
}
|
|
100
|
+
interface NavMeshPathResult {
|
|
101
|
+
/** Smoothed world-space waypoints. */
|
|
102
|
+
path: Vec2[];
|
|
103
|
+
/** Polygon ids traversed. */
|
|
104
|
+
corridor: number[];
|
|
105
|
+
}
|
|
106
|
+
declare function createNavMesh(polygons: NavMeshPolygon[]): NavMesh;
|
|
107
|
+
declare function findPolygonAt(mesh: NavMesh, x: number, y: number): number;
|
|
108
|
+
declare function findNavMeshPath(mesh: NavMesh, start: Vec2, end: Vec2): NavMeshPathResult | null;
|
|
109
|
+
|
|
110
|
+
export { type Grid, type GridConfig, type JpsOptions, type NavMesh, type NavMeshPathResult, type NavMeshPolygon, type PathOptions, type PathResult, type Portal, chebyshevDistance, createGrid, createNavMesh, euclideanDistance, findNavMeshPath, findPath, findPathJPS, findPolygonAt, getCost, inBounds, isWalkable, manhattanDistance, octileDistance, setCost, setWalkable };
|