@codexo/exojs-pathfinding 0.16.1
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 +172 -0
- package/dist/esm/Pathfinder.d.ts +50 -0
- package/dist/esm/Pathfinder.d.ts.map +1 -0
- package/dist/esm/Pathfinder.js +151 -0
- package/dist/esm/Pathfinder.js.map +1 -0
- package/dist/esm/core/BinaryHeap.d.ts +2 -0
- package/dist/esm/core/BinaryHeap.d.ts.map +1 -0
- package/dist/esm/core/BinaryHeap.js +90 -0
- package/dist/esm/core/BinaryHeap.js.map +1 -0
- package/dist/esm/core/SearchState.d.ts +2 -0
- package/dist/esm/core/SearchState.d.ts.map +1 -0
- package/dist/esm/core/SearchState.js +66 -0
- package/dist/esm/core/SearchState.js.map +1 -0
- package/dist/esm/core/search.d.ts +2 -0
- package/dist/esm/core/search.d.ts.map +1 -0
- package/dist/esm/core/search.js +106 -0
- package/dist/esm/core/search.js.map +1 -0
- package/dist/esm/index.d.ts +2 -0
- package/dist/esm/index.d.ts.map +1 -0
- package/dist/esm/index.js +5 -0
- package/dist/esm/public.d.ts +7 -0
- package/dist/esm/public.d.ts.map +1 -0
- package/dist/esm/spaces/GridJumpExpansion.d.ts +2 -0
- package/dist/esm/spaces/GridJumpExpansion.d.ts.map +1 -0
- package/dist/esm/spaces/GridJumpExpansion.js +137 -0
- package/dist/esm/spaces/GridJumpExpansion.js.map +1 -0
- package/dist/esm/spaces/GridSpace.d.ts +131 -0
- package/dist/esm/spaces/GridSpace.d.ts.map +1 -0
- package/dist/esm/spaces/GridSpace.js +296 -0
- package/dist/esm/spaces/GridSpace.js.map +1 -0
- package/dist/esm/spaces/WaypointGraph.d.ts +79 -0
- package/dist/esm/spaces/WaypointGraph.d.ts.map +1 -0
- package/dist/esm/spaces/WaypointGraph.js +167 -0
- package/dist/esm/spaces/WaypointGraph.js.map +1 -0
- package/dist/esm/spaces/gridGeometry.d.ts +3 -0
- package/dist/esm/spaces/gridGeometry.d.ts.map +1 -0
- package/dist/esm/spaces/gridGeometry.js +40 -0
- package/dist/esm/spaces/gridGeometry.js.map +1 -0
- package/dist/esm/types.d.ts +182 -0
- package/dist/esm/types.d.ts.map +1 -0
- package/package.json +46 -0
|
@@ -0,0 +1,296 @@
|
|
|
1
|
+
import { DIRECTION_X, DIRECTION_Y, SQRT2 } from "./gridGeometry.js";
|
|
2
|
+
import { GridJumpExpansion } from "./GridJumpExpansion.js";
|
|
3
|
+
|
|
4
|
+
//#region src/spaces/GridSpace.ts
|
|
5
|
+
/**
|
|
6
|
+
* A rectangular window of weighted, optionally blocked cells.
|
|
7
|
+
*
|
|
8
|
+
* The window is finite by construction: everything outside it is blocked, so a
|
|
9
|
+
* search always terminates and an infinite or streamed world is served by
|
|
10
|
+
* sizing the window to the region the actors are in, then feeding chunk changes
|
|
11
|
+
* back through {@link setCost}. Coordinates in the public API are absolute cell
|
|
12
|
+
* coordinates - the same numbers a tilemap uses - not offsets into the window.
|
|
13
|
+
*
|
|
14
|
+
* Cost `0` blocks a cell, `1` is ordinary ground and larger values are terrain
|
|
15
|
+
* an agent will route around when it is cheaper to do so. Diagonal steps cost
|
|
16
|
+
* their length, so the metric stays consistent with the octile heuristic.
|
|
17
|
+
*
|
|
18
|
+
* The space carries no scene node and no rendering: it is data plus a neighbour
|
|
19
|
+
* relation, and it is built and mutated entirely by the application.
|
|
20
|
+
*/
|
|
21
|
+
var GridSpace = class GridSpace {
|
|
22
|
+
width;
|
|
23
|
+
height;
|
|
24
|
+
originX;
|
|
25
|
+
originY;
|
|
26
|
+
cellSize;
|
|
27
|
+
cellOriginX;
|
|
28
|
+
cellOriginY;
|
|
29
|
+
diagonals;
|
|
30
|
+
maxDegree;
|
|
31
|
+
costs;
|
|
32
|
+
clearance = null;
|
|
33
|
+
jumpExpansion = null;
|
|
34
|
+
weightedCells = 0;
|
|
35
|
+
minCost = 1;
|
|
36
|
+
currentRevision = 0;
|
|
37
|
+
constructor(width, height, options = {}) {
|
|
38
|
+
this.width = width;
|
|
39
|
+
this.height = height;
|
|
40
|
+
this.originX = options.originX ?? 0;
|
|
41
|
+
this.originY = options.originY ?? 0;
|
|
42
|
+
this.cellSize = options.cellSize ?? 1;
|
|
43
|
+
this.cellOriginX = options.cellOriginX ?? 0;
|
|
44
|
+
this.cellOriginY = options.cellOriginY ?? 0;
|
|
45
|
+
this.diagonals = options.diagonals ?? "no-corner-cutting";
|
|
46
|
+
this.maxDegree = this.diagonals === "never" ? 4 : 8;
|
|
47
|
+
this.costs = new Float32Array(width * height).fill(1);
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Builds a window and fills it from a cost callback, which receives absolute
|
|
51
|
+
* cell coordinates. This is the tilemap bridge: return `0` for a solid tile
|
|
52
|
+
* and the terrain's cost for a walkable one, and the grid never learns what a
|
|
53
|
+
* tilemap is.
|
|
54
|
+
*
|
|
55
|
+
* Values that are not finite and positive are stored as blocked.
|
|
56
|
+
*/
|
|
57
|
+
static from(width, height, cost, options = {}) {
|
|
58
|
+
const grid = new GridSpace(width, height, options);
|
|
59
|
+
const { costs, originX, originY } = grid;
|
|
60
|
+
let minCost = Infinity;
|
|
61
|
+
let weighted = 0;
|
|
62
|
+
for (let row = 0; row < height; row++) for (let column = 0; column < width; column++) {
|
|
63
|
+
const raw = cost(originX + column, originY + row);
|
|
64
|
+
const value = Number.isFinite(raw) && raw > 0 ? raw : 0;
|
|
65
|
+
costs[row * width + column] = value;
|
|
66
|
+
if (value === 0) continue;
|
|
67
|
+
if (value !== 1) weighted++;
|
|
68
|
+
if (value < minCost) minCost = value;
|
|
69
|
+
}
|
|
70
|
+
grid.weightedCells = weighted;
|
|
71
|
+
grid.minCost = minCost === Infinity ? 1 : minCost;
|
|
72
|
+
return grid;
|
|
73
|
+
}
|
|
74
|
+
get nodeCapacity() {
|
|
75
|
+
return this.costs.length;
|
|
76
|
+
}
|
|
77
|
+
get revision() {
|
|
78
|
+
return this.currentRevision;
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* `true` while every walkable cell costs exactly `1`. Jump-point search is
|
|
82
|
+
* only available on such a grid.
|
|
83
|
+
*/
|
|
84
|
+
get uniformCost() {
|
|
85
|
+
return this.weightedCells === 0;
|
|
86
|
+
}
|
|
87
|
+
/** The node at absolute cell coordinates, or `-1` outside the window. */
|
|
88
|
+
nodeAt(x, y) {
|
|
89
|
+
const column = x - this.originX;
|
|
90
|
+
const row = y - this.originY;
|
|
91
|
+
if (column < 0 || row < 0 || column >= this.width || row >= this.height) return -1;
|
|
92
|
+
return row * this.width + column;
|
|
93
|
+
}
|
|
94
|
+
/** Absolute cell x of a node. */
|
|
95
|
+
nodeX(node) {
|
|
96
|
+
return this.originX + node % this.width;
|
|
97
|
+
}
|
|
98
|
+
/** Absolute cell y of a node. */
|
|
99
|
+
nodeY(node) {
|
|
100
|
+
return this.originY + Math.floor(node / this.width);
|
|
101
|
+
}
|
|
102
|
+
/** Traversal cost of a cell; `0` for blocked cells and everything outside the window. */
|
|
103
|
+
costAt(x, y) {
|
|
104
|
+
const node = this.nodeAt(x, y);
|
|
105
|
+
return node < 0 ? 0 : this.costs[node];
|
|
106
|
+
}
|
|
107
|
+
isWalkable(x, y) {
|
|
108
|
+
return this.costAt(x, y) > 0;
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Sets a cell's cost and bumps {@link revision}. Values that are not finite
|
|
112
|
+
* and positive block the cell. Coordinates outside the window are ignored.
|
|
113
|
+
*/
|
|
114
|
+
setCost(x, y, cost) {
|
|
115
|
+
const node = this.nodeAt(x, y);
|
|
116
|
+
if (node < 0) return;
|
|
117
|
+
const value = Number.isFinite(cost) && cost > 0 ? cost : 0;
|
|
118
|
+
const previous = this.costs[node];
|
|
119
|
+
if (value === previous) return;
|
|
120
|
+
if (previous > 0 && previous !== 1) this.weightedCells--;
|
|
121
|
+
if (value > 0 && value !== 1) this.weightedCells++;
|
|
122
|
+
if (value > 0 && value < this.minCost) this.minCost = value;
|
|
123
|
+
this.costs[node] = value;
|
|
124
|
+
this.clearance = null;
|
|
125
|
+
this.currentRevision++;
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Largest agent width that fits with its top-left corner on this cell, or `0`
|
|
129
|
+
* for a blocked cell. Recomputed lazily after the first edit that follows a
|
|
130
|
+
* query.
|
|
131
|
+
*/
|
|
132
|
+
clearanceAt(x, y) {
|
|
133
|
+
const node = this.nodeAt(x, y);
|
|
134
|
+
if (node < 0) return 0;
|
|
135
|
+
return this.ensureClearance()[node];
|
|
136
|
+
}
|
|
137
|
+
neighbors(node, agentSize, outNodes, outCosts) {
|
|
138
|
+
const { width, height, costs, diagonals } = this;
|
|
139
|
+
const column = node % width;
|
|
140
|
+
const row = node / width | 0;
|
|
141
|
+
const clearance = agentSize > 1 ? this.ensureClearance() : null;
|
|
142
|
+
const directions = diagonals === "never" ? 4 : 8;
|
|
143
|
+
const guardCorners = diagonals === "no-corner-cutting";
|
|
144
|
+
let count = 0;
|
|
145
|
+
for (let direction = 0; direction < directions; direction++) {
|
|
146
|
+
const stepX = DIRECTION_X[direction];
|
|
147
|
+
const stepY = DIRECTION_Y[direction];
|
|
148
|
+
const nextColumn = column + stepX;
|
|
149
|
+
const nextRow = row + stepY;
|
|
150
|
+
if (nextColumn < 0 || nextRow < 0 || nextColumn >= width || nextRow >= height) continue;
|
|
151
|
+
const next = nextRow * width + nextColumn;
|
|
152
|
+
const cost = costs[next];
|
|
153
|
+
if (cost <= 0) continue;
|
|
154
|
+
if (clearance !== null && clearance[next] < agentSize) continue;
|
|
155
|
+
if (direction < 4) outCosts[count] = cost;
|
|
156
|
+
else {
|
|
157
|
+
if (guardCorners && (!this.fits(column + stepX, row, agentSize, clearance) || !this.fits(column, row + stepY, agentSize, clearance))) continue;
|
|
158
|
+
outCosts[count] = cost * SQRT2;
|
|
159
|
+
}
|
|
160
|
+
outNodes[count] = next;
|
|
161
|
+
count++;
|
|
162
|
+
}
|
|
163
|
+
return count;
|
|
164
|
+
}
|
|
165
|
+
heuristic(node, goal) {
|
|
166
|
+
const { width } = this;
|
|
167
|
+
const deltaX = Math.abs(node % width - goal % width);
|
|
168
|
+
const deltaY = Math.abs((node / width | 0) - (goal / width | 0));
|
|
169
|
+
if (this.diagonals === "never") return (deltaX + deltaY) * this.minCost;
|
|
170
|
+
return (deltaX + deltaY + (SQRT2 - 2) * Math.min(deltaX, deltaY)) * this.minCost;
|
|
171
|
+
}
|
|
172
|
+
nodeToPoint(node, out) {
|
|
173
|
+
const { width, cellSize } = this;
|
|
174
|
+
out.set((this.originX + node % width + .5) * cellSize + this.cellOriginX, (this.originY + (node / width | 0) + .5) * cellSize + this.cellOriginY);
|
|
175
|
+
}
|
|
176
|
+
pointToNode(x, y) {
|
|
177
|
+
const { cellSize } = this;
|
|
178
|
+
return this.nodeAt(Math.floor((x - this.cellOriginX) / cellSize), Math.floor((y - this.cellOriginY) / cellSize));
|
|
179
|
+
}
|
|
180
|
+
nearestNode(x, y) {
|
|
181
|
+
const { width, height, cellSize } = this;
|
|
182
|
+
const column = Math.floor((x - this.cellOriginX) / cellSize) - this.originX;
|
|
183
|
+
const row = Math.floor((y - this.cellOriginY) / cellSize) - this.originY;
|
|
184
|
+
return Math.min(Math.max(row, 0), height - 1) * width + Math.min(Math.max(column, 0), width - 1);
|
|
185
|
+
}
|
|
186
|
+
pruning(agentSize) {
|
|
187
|
+
if (this.weightedCells > 0 || agentSize > 1 || this.diagonals !== "no-corner-cutting") return null;
|
|
188
|
+
this.jumpExpansion ??= new GridJumpExpansion(this.costs, this.width, this.height);
|
|
189
|
+
return this.jumpExpansion;
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* String-pulls the path: keeps a node only when the straight line past it is
|
|
193
|
+
* blocked, so the result is the same route with its staircase removed.
|
|
194
|
+
*
|
|
195
|
+
* The returned nodes are no longer adjacent - the guarantee is that the
|
|
196
|
+
* straight segment between two consecutive ones stays inside walkable cells
|
|
197
|
+
* an agent of `agentSize` fits through, and never crosses terrain more
|
|
198
|
+
* expensive than the section it replaces.
|
|
199
|
+
*/
|
|
200
|
+
smoothPath(nodes, agentSize) {
|
|
201
|
+
const last = nodes.length - 1;
|
|
202
|
+
if (last < 2) return [...nodes];
|
|
203
|
+
const { costs } = this;
|
|
204
|
+
const out = [nodes[0]];
|
|
205
|
+
let anchor = 0;
|
|
206
|
+
while (anchor < last) {
|
|
207
|
+
let best = anchor + 1;
|
|
208
|
+
let budget = Math.max(costs[nodes[anchor]], costs[nodes[best]]);
|
|
209
|
+
for (let index = anchor + 2; index <= last; index++) {
|
|
210
|
+
budget = Math.max(budget, costs[nodes[index]]);
|
|
211
|
+
if (!this.lineOfSight(nodes[anchor], nodes[index], agentSize, budget)) break;
|
|
212
|
+
best = index;
|
|
213
|
+
}
|
|
214
|
+
out.push(nodes[best]);
|
|
215
|
+
anchor = best;
|
|
216
|
+
}
|
|
217
|
+
return out;
|
|
218
|
+
}
|
|
219
|
+
fits(column, row, agentSize, clearance) {
|
|
220
|
+
const { width, height } = this;
|
|
221
|
+
if (column < 0 || row < 0 || column >= width || row >= height) return false;
|
|
222
|
+
const index = row * width + column;
|
|
223
|
+
if (this.costs[index] <= 0) return false;
|
|
224
|
+
return clearance === null || clearance[index] >= agentSize;
|
|
225
|
+
}
|
|
226
|
+
/**
|
|
227
|
+
* Walks the cells a centre-to-centre segment passes through and reports
|
|
228
|
+
* whether all of them are traversable within `budget`. A segment that leaves
|
|
229
|
+
* a cell exactly through its corner counts as a diagonal step and is subject
|
|
230
|
+
* to the same corner rule as movement.
|
|
231
|
+
*/
|
|
232
|
+
lineOfSight(from, to, agentSize, budget) {
|
|
233
|
+
const { width, costs, diagonals } = this;
|
|
234
|
+
const clearance = agentSize > 1 ? this.ensureClearance() : null;
|
|
235
|
+
const targetColumn = to % width;
|
|
236
|
+
const targetRow = to / width | 0;
|
|
237
|
+
let column = from % width;
|
|
238
|
+
let row = from / width | 0;
|
|
239
|
+
const spanX = Math.abs(targetColumn - column);
|
|
240
|
+
const spanY = Math.abs(targetRow - row);
|
|
241
|
+
const stepX = Math.sign(targetColumn - column);
|
|
242
|
+
const stepY = Math.sign(targetRow - row);
|
|
243
|
+
const deltaX = spanX === 0 ? Infinity : 1 / spanX;
|
|
244
|
+
const deltaY = spanY === 0 ? Infinity : 1 / spanY;
|
|
245
|
+
const guardCorners = diagonals !== "always";
|
|
246
|
+
let nextX = spanX === 0 ? Infinity : deltaX / 2;
|
|
247
|
+
let nextY = spanY === 0 ? Infinity : deltaY / 2;
|
|
248
|
+
while (column !== targetColumn || row !== targetRow) {
|
|
249
|
+
if (nextX < nextY) {
|
|
250
|
+
column += stepX;
|
|
251
|
+
nextX += deltaX;
|
|
252
|
+
} else if (nextY < nextX) {
|
|
253
|
+
row += stepY;
|
|
254
|
+
nextY += deltaY;
|
|
255
|
+
} else {
|
|
256
|
+
if (guardCorners && (!this.fits(column + stepX, row, agentSize, clearance) || !this.fits(column, row + stepY, agentSize, clearance))) return false;
|
|
257
|
+
column += stepX;
|
|
258
|
+
row += stepY;
|
|
259
|
+
nextX += deltaX;
|
|
260
|
+
nextY += deltaY;
|
|
261
|
+
}
|
|
262
|
+
if (!this.fits(column, row, agentSize, clearance)) return false;
|
|
263
|
+
if (costs[row * width + column] > budget) return false;
|
|
264
|
+
}
|
|
265
|
+
return true;
|
|
266
|
+
}
|
|
267
|
+
/**
|
|
268
|
+
* Brushfire clearance: how far the walkable block anchored at a cell extends
|
|
269
|
+
* down and to the right. Filling it bottom-right to top-left makes it one
|
|
270
|
+
* pass, because a cell only ever depends on the three cells after it.
|
|
271
|
+
*/
|
|
272
|
+
ensureClearance() {
|
|
273
|
+
const cached = this.clearance;
|
|
274
|
+
if (cached !== null) return cached;
|
|
275
|
+
const { width, height, costs } = this;
|
|
276
|
+
const clearance = new Uint16Array(costs.length);
|
|
277
|
+
for (let row = height - 1; row >= 0; row--) for (let column = width - 1; column >= 0; column--) {
|
|
278
|
+
const index = row * width + column;
|
|
279
|
+
if (costs[index] <= 0) continue;
|
|
280
|
+
if (column === width - 1 || row === height - 1) {
|
|
281
|
+
clearance[index] = 1;
|
|
282
|
+
continue;
|
|
283
|
+
}
|
|
284
|
+
const right = clearance[index + 1];
|
|
285
|
+
const below = clearance[index + width];
|
|
286
|
+
const diagonal = clearance[index + width + 1];
|
|
287
|
+
clearance[index] = Math.min(right, below, diagonal, 65534) + 1;
|
|
288
|
+
}
|
|
289
|
+
this.clearance = clearance;
|
|
290
|
+
return clearance;
|
|
291
|
+
}
|
|
292
|
+
};
|
|
293
|
+
|
|
294
|
+
//#endregion
|
|
295
|
+
export { GridSpace };
|
|
296
|
+
//# sourceMappingURL=GridSpace.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"GridSpace.js","names":[],"sources":["../../../src/spaces/GridSpace.ts"],"sourcesContent":["import type { Vector } from '@codexo/exojs';\n\nimport type { NavigationSpace, PrunedExpansion } from '../types';\nimport { DIRECTION_X, DIRECTION_Y, SQRT2 } from './gridGeometry';\nimport { GridJumpExpansion } from './GridJumpExpansion';\n\n/**\n * How diagonal steps are allowed on a {@link GridSpace}.\n *\n * - `never` - four-connected movement only.\n * - `no-corner-cutting` - a diagonal step needs both cells it passes between to\n * be walkable, which is what stops an agent from clipping through the corner\n * where two walls meet.\n * - `always` - eight-connected movement with no such restriction.\n */\nexport type DiagonalPolicy = 'never' | 'no-corner-cutting' | 'always';\n\n/** Construction options for {@link GridSpace}. */\nexport interface GridSpaceOptions {\n /** Cell x of the window's left column. Defaults to `0`. */\n readonly originX?: number | undefined;\n /** Cell y of the window's top row. Defaults to `0`. */\n readonly originY?: number | undefined;\n /** Defaults to `'no-corner-cutting'`. */\n readonly diagonals?: DiagonalPolicy | undefined;\n /**\n * World size of one cell. The distance metric assumes square cells, so a\n * tilemap with non-square tiles has to pick one axis. Defaults to `1`.\n */\n readonly cellSize?: number | undefined;\n /** World x of cell `0`'s left edge, before the window origin. Defaults to `0`. */\n readonly cellOriginX?: number | undefined;\n /** World y of cell `0`'s top edge, before the window origin. Defaults to `0`. */\n readonly cellOriginY?: number | undefined;\n}\n\n/**\n * A rectangular window of weighted, optionally blocked cells.\n *\n * The window is finite by construction: everything outside it is blocked, so a\n * search always terminates and an infinite or streamed world is served by\n * sizing the window to the region the actors are in, then feeding chunk changes\n * back through {@link setCost}. Coordinates in the public API are absolute cell\n * coordinates - the same numbers a tilemap uses - not offsets into the window.\n *\n * Cost `0` blocks a cell, `1` is ordinary ground and larger values are terrain\n * an agent will route around when it is cheaper to do so. Diagonal steps cost\n * their length, so the metric stays consistent with the octile heuristic.\n *\n * The space carries no scene node and no rendering: it is data plus a neighbour\n * relation, and it is built and mutated entirely by the application.\n */\nexport class GridSpace implements NavigationSpace {\n public readonly width: number;\n public readonly height: number;\n public readonly originX: number;\n public readonly originY: number;\n public readonly cellSize: number;\n public readonly cellOriginX: number;\n public readonly cellOriginY: number;\n public readonly diagonals: DiagonalPolicy;\n public readonly maxDegree: number;\n\n private readonly costs: Float32Array;\n private clearance: Uint16Array | null = null;\n private jumpExpansion: GridJumpExpansion | null = null;\n private weightedCells = 0;\n private minCost = 1;\n private currentRevision = 0;\n\n public constructor(width: number, height: number, options: GridSpaceOptions = {}) {\n if (__DEV__ && (!Number.isInteger(width) || !Number.isInteger(height) || width < 1 || height < 1)) {\n throw new RangeError(`GridSpace needs positive integer dimensions, received ${width}x${height}.`);\n }\n\n this.width = width;\n this.height = height;\n this.originX = options.originX ?? 0;\n this.originY = options.originY ?? 0;\n this.cellSize = options.cellSize ?? 1;\n this.cellOriginX = options.cellOriginX ?? 0;\n this.cellOriginY = options.cellOriginY ?? 0;\n this.diagonals = options.diagonals ?? 'no-corner-cutting';\n this.maxDegree = this.diagonals === 'never' ? 4 : 8;\n this.costs = new Float32Array(width * height).fill(1);\n }\n\n /**\n * Builds a window and fills it from a cost callback, which receives absolute\n * cell coordinates. This is the tilemap bridge: return `0` for a solid tile\n * and the terrain's cost for a walkable one, and the grid never learns what a\n * tilemap is.\n *\n * Values that are not finite and positive are stored as blocked.\n */\n public static from(width: number, height: number, cost: (x: number, y: number) => number, options: GridSpaceOptions = {}): GridSpace {\n const grid = new GridSpace(width, height, options);\n const { costs, originX, originY } = grid;\n\n let minCost = Infinity;\n let weighted = 0;\n\n for (let row = 0; row < height; row++) {\n for (let column = 0; column < width; column++) {\n const raw = cost(originX + column, originY + row);\n const value = Number.isFinite(raw) && raw > 0 ? raw : 0;\n\n costs[row * width + column] = value;\n\n if (value === 0) continue;\n if (value !== 1) weighted++;\n if (value < minCost) minCost = value;\n }\n }\n\n grid.weightedCells = weighted;\n grid.minCost = minCost === Infinity ? 1 : minCost;\n\n return grid;\n }\n\n public get nodeCapacity(): number {\n return this.costs.length;\n }\n\n public get revision(): number {\n return this.currentRevision;\n }\n\n /**\n * `true` while every walkable cell costs exactly `1`. Jump-point search is\n * only available on such a grid.\n */\n public get uniformCost(): boolean {\n return this.weightedCells === 0;\n }\n\n /** The node at absolute cell coordinates, or `-1` outside the window. */\n public nodeAt(x: number, y: number): number {\n const column = x - this.originX;\n const row = y - this.originY;\n\n if (column < 0 || row < 0 || column >= this.width || row >= this.height) return -1;\n\n return row * this.width + column;\n }\n\n /** Absolute cell x of a node. */\n public nodeX(node: number): number {\n return this.originX + (node % this.width);\n }\n\n /** Absolute cell y of a node. */\n public nodeY(node: number): number {\n return this.originY + Math.floor(node / this.width);\n }\n\n /** Traversal cost of a cell; `0` for blocked cells and everything outside the window. */\n public costAt(x: number, y: number): number {\n const node = this.nodeAt(x, y);\n\n return node < 0 ? 0 : this.costs[node]!;\n }\n\n public isWalkable(x: number, y: number): boolean {\n return this.costAt(x, y) > 0;\n }\n\n /**\n * Sets a cell's cost and bumps {@link revision}. Values that are not finite\n * and positive block the cell. Coordinates outside the window are ignored.\n */\n public setCost(x: number, y: number, cost: number): void {\n const node = this.nodeAt(x, y);\n\n if (node < 0) return;\n\n const value = Number.isFinite(cost) && cost > 0 ? cost : 0;\n const previous = this.costs[node]!;\n\n if (value === previous) return;\n\n if (previous > 0 && previous !== 1) this.weightedCells--;\n if (value > 0 && value !== 1) this.weightedCells++;\n // Only lowered costs tighten the bound. A raised cost leaves it looser than\n // it could be, which keeps the heuristic admissible - the direction that\n // matters - without rescanning the whole window on every edit.\n if (value > 0 && value < this.minCost) this.minCost = value;\n\n this.costs[node] = value;\n this.clearance = null;\n this.currentRevision++;\n }\n\n /**\n * Largest agent width that fits with its top-left corner on this cell, or `0`\n * for a blocked cell. Recomputed lazily after the first edit that follows a\n * query.\n */\n public clearanceAt(x: number, y: number): number {\n const node = this.nodeAt(x, y);\n\n if (node < 0) return 0;\n\n return this.ensureClearance()[node]!;\n }\n\n public neighbors(node: number, agentSize: number, outNodes: Int32Array, outCosts: Float64Array): number {\n const { width, height, costs, diagonals } = this;\n const column = node % width;\n const row = (node / width) | 0;\n const clearance = agentSize > 1 ? this.ensureClearance() : null;\n const directions = diagonals === 'never' ? 4 : 8;\n const guardCorners = diagonals === 'no-corner-cutting';\n\n let count = 0;\n\n for (let direction = 0; direction < directions; direction++) {\n const stepX = DIRECTION_X[direction]!;\n const stepY = DIRECTION_Y[direction]!;\n const nextColumn = column + stepX;\n const nextRow = row + stepY;\n\n if (nextColumn < 0 || nextRow < 0 || nextColumn >= width || nextRow >= height) continue;\n\n const next = nextRow * width + nextColumn;\n const cost = costs[next]!;\n\n if (cost <= 0) continue;\n if (clearance !== null && clearance[next]! < agentSize) continue;\n\n if (direction < 4) {\n outCosts[count] = cost;\n } else {\n if (guardCorners && (!this.fits(column + stepX, row, agentSize, clearance) || !this.fits(column, row + stepY, agentSize, clearance))) continue;\n\n outCosts[count] = cost * SQRT2;\n }\n\n outNodes[count] = next;\n count++;\n }\n\n return count;\n }\n\n public heuristic(node: number, goal: number): number {\n const { width } = this;\n const deltaX = Math.abs((node % width) - (goal % width));\n const deltaY = Math.abs(((node / width) | 0) - ((goal / width) | 0));\n\n if (this.diagonals === 'never') return (deltaX + deltaY) * this.minCost;\n\n // Octile distance, scaled by the cheapest walkable cell so that a weighted\n // grid cannot make the estimate exceed the true remaining cost.\n return (deltaX + deltaY + (SQRT2 - 2) * Math.min(deltaX, deltaY)) * this.minCost;\n }\n\n public nodeToPoint(node: number, out: Vector): void {\n const { width, cellSize } = this;\n\n out.set((this.originX + (node % width) + 0.5) * cellSize + this.cellOriginX, (this.originY + ((node / width) | 0) + 0.5) * cellSize + this.cellOriginY);\n }\n\n public pointToNode(x: number, y: number): number {\n const { cellSize } = this;\n\n return this.nodeAt(Math.floor((x - this.cellOriginX) / cellSize), Math.floor((y - this.cellOriginY) / cellSize));\n }\n\n public nearestNode(x: number, y: number): number {\n const { width, height, cellSize } = this;\n const column = Math.floor((x - this.cellOriginX) / cellSize) - this.originX;\n const row = Math.floor((y - this.cellOriginY) / cellSize) - this.originY;\n\n return Math.min(Math.max(row, 0), height - 1) * width + Math.min(Math.max(column, 0), width - 1);\n }\n\n public pruning(agentSize: number): PrunedExpansion | null {\n // Jump-point search derives its pruning rules from a uniform-cost grid with\n // a single-cell agent: weights make the symmetric alternatives it discards\n // no longer equivalent, and clearance changes which of them are legal.\n if (this.weightedCells > 0 || agentSize > 1 || this.diagonals !== 'no-corner-cutting') return null;\n\n this.jumpExpansion ??= new GridJumpExpansion(this.costs, this.width, this.height);\n\n return this.jumpExpansion;\n }\n\n /**\n * String-pulls the path: keeps a node only when the straight line past it is\n * blocked, so the result is the same route with its staircase removed.\n *\n * The returned nodes are no longer adjacent - the guarantee is that the\n * straight segment between two consecutive ones stays inside walkable cells\n * an agent of `agentSize` fits through, and never crosses terrain more\n * expensive than the section it replaces.\n */\n public smoothPath(nodes: readonly number[], agentSize: number): number[] {\n const last = nodes.length - 1;\n\n if (last < 2) return [...nodes];\n\n const { costs } = this;\n const out: number[] = [nodes[0]!];\n\n let anchor = 0;\n\n while (anchor < last) {\n let best = anchor + 1;\n let budget = Math.max(costs[nodes[anchor]!]!, costs[nodes[best]!]!);\n\n for (let index = anchor + 2; index <= last; index++) {\n budget = Math.max(budget, costs[nodes[index]!]!);\n\n if (!this.lineOfSight(nodes[anchor]!, nodes[index]!, agentSize, budget)) break;\n\n best = index;\n }\n\n out.push(nodes[best]!);\n anchor = best;\n }\n\n return out;\n }\n\n private fits(column: number, row: number, agentSize: number, clearance: Uint16Array | null): boolean {\n const { width, height } = this;\n\n if (column < 0 || row < 0 || column >= width || row >= height) return false;\n\n const index = row * width + column;\n\n if (this.costs[index]! <= 0) return false;\n\n return clearance === null || clearance[index]! >= agentSize;\n }\n\n /**\n * Walks the cells a centre-to-centre segment passes through and reports\n * whether all of them are traversable within `budget`. A segment that leaves\n * a cell exactly through its corner counts as a diagonal step and is subject\n * to the same corner rule as movement.\n */\n private lineOfSight(from: number, to: number, agentSize: number, budget: number): boolean {\n const { width, costs, diagonals } = this;\n const clearance = agentSize > 1 ? this.ensureClearance() : null;\n const targetColumn = to % width;\n const targetRow = (to / width) | 0;\n\n let column = from % width;\n let row = (from / width) | 0;\n\n const spanX = Math.abs(targetColumn - column);\n const spanY = Math.abs(targetRow - row);\n const stepX = Math.sign(targetColumn - column);\n const stepY = Math.sign(targetRow - row);\n const deltaX = spanX === 0 ? Infinity : 1 / spanX;\n const deltaY = spanY === 0 ? Infinity : 1 / spanY;\n const guardCorners = diagonals !== 'always';\n\n let nextX = spanX === 0 ? Infinity : deltaX / 2;\n let nextY = spanY === 0 ? Infinity : deltaY / 2;\n\n while (column !== targetColumn || row !== targetRow) {\n if (nextX < nextY) {\n column += stepX;\n nextX += deltaX;\n } else if (nextY < nextX) {\n row += stepY;\n nextY += deltaY;\n } else {\n if (guardCorners && (!this.fits(column + stepX, row, agentSize, clearance) || !this.fits(column, row + stepY, agentSize, clearance))) return false;\n\n column += stepX;\n row += stepY;\n nextX += deltaX;\n nextY += deltaY;\n }\n\n if (!this.fits(column, row, agentSize, clearance)) return false;\n if (costs[row * width + column]! > budget) return false;\n }\n\n return true;\n }\n\n /**\n * Brushfire clearance: how far the walkable block anchored at a cell extends\n * down and to the right. Filling it bottom-right to top-left makes it one\n * pass, because a cell only ever depends on the three cells after it.\n */\n private ensureClearance(): Uint16Array {\n const cached = this.clearance;\n\n if (cached !== null) return cached;\n\n const { width, height, costs } = this;\n const clearance = new Uint16Array(costs.length);\n\n for (let row = height - 1; row >= 0; row--) {\n for (let column = width - 1; column >= 0; column--) {\n const index = row * width + column;\n\n if (costs[index]! <= 0) continue;\n\n if (column === width - 1 || row === height - 1) {\n clearance[index] = 1;\n continue;\n }\n\n const right = clearance[index + 1]!;\n const below = clearance[index + width]!;\n const diagonal = clearance[index + width + 1]!;\n\n clearance[index] = Math.min(right, below, diagonal, 0xfffe) + 1;\n }\n }\n\n this.clearance = clearance;\n\n return clearance;\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAoDA,IAAa,YAAb,MAAa,UAAqC;CAChD,AAAgB;CAChB,AAAgB;CAChB,AAAgB;CAChB,AAAgB;CAChB,AAAgB;CAChB,AAAgB;CAChB,AAAgB;CAChB,AAAgB;CAChB,AAAgB;CAEhB,AAAiB;CACjB,AAAQ,YAAgC;CACxC,AAAQ,gBAA0C;CAClD,AAAQ,gBAAgB;CACxB,AAAQ,UAAU;CAClB,AAAQ,kBAAkB;CAE1B,AAAO,YAAY,OAAe,QAAgB,UAA4B,CAAC,GAAG;EAKhF,KAAK,QAAQ;EACb,KAAK,SAAS;EACd,KAAK,UAAU,QAAQ,WAAW;EAClC,KAAK,UAAU,QAAQ,WAAW;EAClC,KAAK,WAAW,QAAQ,YAAY;EACpC,KAAK,cAAc,QAAQ,eAAe;EAC1C,KAAK,cAAc,QAAQ,eAAe;EAC1C,KAAK,YAAY,QAAQ,aAAa;EACtC,KAAK,YAAY,KAAK,cAAc,UAAU,IAAI;EAClD,KAAK,QAAQ,IAAI,aAAa,QAAQ,MAAM,CAAC,CAAC,KAAK,CAAC;CACtD;;;;;;;;;CAUA,OAAc,KAAK,OAAe,QAAgB,MAAwC,UAA4B,CAAC,GAAc;EACnI,MAAM,OA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;GAE5B,IAAI,MAAM,UAAW,GAAG;GAExB,IAAI,WAAW,QAAQ,KAAK,QAAQ,SAAS,GAAG;IAC9C,UAAU,SAAS;IACnB;GACF;GAEA,MAAM,QAAQ,UAAU,QAAQ;GAChC,MAAM,QAAQ,UAAU,QAAQ;GAChC,MAAM,WAAW,UAAU,QAAQ,QAAQ;GAE3C,UAAU,SAAS,KAAK,IAAI,OAAO,OAAO,UAAU,KAAM,IAAI;EAChE;EAGF,KAAK,YAAY;EAEjB,OAAO;CACT;AACF"}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import type { Vector } from '@codexo/exojs';
|
|
2
|
+
import type { NavigationSpace, PathEdge } from '../types';
|
|
3
|
+
/** Options for {@link WaypointGraph.addEdge} and {@link WaypointGraph.connect}. */
|
|
4
|
+
export interface WaypointEdgeOptions<Payload = unknown> {
|
|
5
|
+
/**
|
|
6
|
+
* Traversal cost. Defaults to the straight-line distance between the two
|
|
7
|
+
* nodes, or `1` when either of them has no position.
|
|
8
|
+
*/
|
|
9
|
+
readonly cost?: number | undefined;
|
|
10
|
+
/**
|
|
11
|
+
* Free-form traversal tag surfaced through {@link PathResult.edges} - the
|
|
12
|
+
* hook a movement controller reads to tell a jump from a walk. Defaults to
|
|
13
|
+
* `'walk'`.
|
|
14
|
+
*/
|
|
15
|
+
readonly kind?: string | undefined;
|
|
16
|
+
/** Payload for the movement controller: jump impulse, ladder id, anything. */
|
|
17
|
+
readonly data?: Payload | undefined;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* A directed graph of hand-placed waypoints.
|
|
21
|
+
*
|
|
22
|
+
* This is the representation for worlds a grid cannot describe: a platformer
|
|
23
|
+
* where traversal is a topology of walk, jump and fall links rather than cell
|
|
24
|
+
* walkability, or a purely abstract graph with no geometry at all. Nodes carry
|
|
25
|
+
* an optional position, edges carry a cost, a {@link WaypointEdgeOptions.kind}
|
|
26
|
+
* and an arbitrary payload, and the resulting path reports the edges it took so
|
|
27
|
+
* the game can execute each step in its own way.
|
|
28
|
+
*
|
|
29
|
+
* With positions the search is A* over straight-line distance; without them the
|
|
30
|
+
* heuristic is zero and the same search degrades cleanly to Dijkstra.
|
|
31
|
+
*/
|
|
32
|
+
export declare class WaypointGraph<Payload = unknown> implements NavigationSpace<Payload> {
|
|
33
|
+
private readonly positionsX;
|
|
34
|
+
private readonly positionsY;
|
|
35
|
+
private readonly adjacency;
|
|
36
|
+
private readonly alive;
|
|
37
|
+
private readonly positioned;
|
|
38
|
+
private readonly freeSlots;
|
|
39
|
+
private positionlessCount;
|
|
40
|
+
private largestDegree;
|
|
41
|
+
private currentRevision;
|
|
42
|
+
private distanceScale;
|
|
43
|
+
get nodeCapacity(): number;
|
|
44
|
+
get maxDegree(): number;
|
|
45
|
+
get revision(): number;
|
|
46
|
+
/** Number of live nodes. */
|
|
47
|
+
get nodeCount(): number;
|
|
48
|
+
/**
|
|
49
|
+
* Adds a node. Omitting the position puts the graph in Dijkstra mode: the
|
|
50
|
+
* heuristic drops to zero for every query, since a positionless node makes no
|
|
51
|
+
* geometric estimate meaningful.
|
|
52
|
+
*/
|
|
53
|
+
addNode(x?: number, y?: number): number;
|
|
54
|
+
/**
|
|
55
|
+
* Removes a node together with every edge touching it.
|
|
56
|
+
*
|
|
57
|
+
* Ids are recycled: a later {@link addNode} may hand out the id this call
|
|
58
|
+
* freed, so a node id held across a removal can silently refer to a different
|
|
59
|
+
* node.
|
|
60
|
+
*/
|
|
61
|
+
removeNode(node: number): void;
|
|
62
|
+
/** Adds a directed edge. A second edge between the same pair replaces the first. */
|
|
63
|
+
addEdge(from: number, to: number, options?: WaypointEdgeOptions<Payload>): void;
|
|
64
|
+
/** Adds the edge in both directions with the same options. */
|
|
65
|
+
connect(a: number, b: number, options?: WaypointEdgeOptions<Payload>): void;
|
|
66
|
+
removeEdge(from: number, to: number): void;
|
|
67
|
+
neighbors(node: number, _agentSize: number, outNodes: Int32Array, outCosts: Float64Array): number;
|
|
68
|
+
heuristic(node: number, goal: number): number;
|
|
69
|
+
nodeToPoint(node: number, out: Vector): void;
|
|
70
|
+
/**
|
|
71
|
+
* The positioned node closest to the point, or `-1` when the graph has none.
|
|
72
|
+
* A graph has no cells, so there is no "outside" for a point to fall into.
|
|
73
|
+
*/
|
|
74
|
+
pointToNode(x: number, y: number): number;
|
|
75
|
+
nearestNode(x: number, y: number): number;
|
|
76
|
+
describeEdge(from: number, to: number): PathEdge<Payload> | null;
|
|
77
|
+
private length;
|
|
78
|
+
}
|
|
79
|
+
//# sourceMappingURL=WaypointGraph.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"WaypointGraph.d.ts","sourceRoot":"","sources":["../../../src/spaces/WaypointGraph.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,eAAe,CAAC;AAE5C,OAAO,KAAK,EAAE,eAAe,EAAE,QAAQ,EAAE,MAAM,UAAU,CAAC;AAE1D,mFAAmF;AACnF,MAAM,WAAW,mBAAmB,CAAC,OAAO,GAAG,OAAO;IACpD;;;OAGG;IACH,QAAQ,CAAC,IAAI,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IACnC;;;;OAIG;IACH,QAAQ,CAAC,IAAI,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IACnC,8EAA8E;IAC9E,QAAQ,CAAC,IAAI,CAAC,EAAE,OAAO,GAAG,SAAS,CAAC;CACrC;AASD;;;;;;;;;;;;GAYG;AACH,qBAAa,aAAa,CAAC,OAAO,GAAG,OAAO,CAAE,YAAW,eAAe,CAAC,OAAO,CAAC;IAC/E,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAgB;IAC3C,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAgB;IAC3C,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAmC;IAC7D,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAiB;IACvC,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAiB;IAC5C,OAAO,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AC,EAAE,MAAM,GAAG,MAAM;IAIzC,YAAY,CAAC,IAAI,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,GAAG,QAAQ,CAAC,OAAO,CAAC,GAAG,IAAI;IAcvE,OAAO,CAAC,MAAM;CAKf"}
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
//#region src/spaces/WaypointGraph.ts
|
|
2
|
+
/**
|
|
3
|
+
* A directed graph of hand-placed waypoints.
|
|
4
|
+
*
|
|
5
|
+
* This is the representation for worlds a grid cannot describe: a platformer
|
|
6
|
+
* where traversal is a topology of walk, jump and fall links rather than cell
|
|
7
|
+
* walkability, or a purely abstract graph with no geometry at all. Nodes carry
|
|
8
|
+
* an optional position, edges carry a cost, a {@link WaypointEdgeOptions.kind}
|
|
9
|
+
* and an arbitrary payload, and the resulting path reports the edges it took so
|
|
10
|
+
* the game can execute each step in its own way.
|
|
11
|
+
*
|
|
12
|
+
* With positions the search is A* over straight-line distance; without them the
|
|
13
|
+
* heuristic is zero and the same search degrades cleanly to Dijkstra.
|
|
14
|
+
*/
|
|
15
|
+
var WaypointGraph = class {
|
|
16
|
+
positionsX = [];
|
|
17
|
+
positionsY = [];
|
|
18
|
+
adjacency = [];
|
|
19
|
+
alive = [];
|
|
20
|
+
positioned = [];
|
|
21
|
+
freeSlots = [];
|
|
22
|
+
positionlessCount = 0;
|
|
23
|
+
largestDegree = 0;
|
|
24
|
+
currentRevision = 0;
|
|
25
|
+
distanceScale = 1;
|
|
26
|
+
get nodeCapacity() {
|
|
27
|
+
return this.alive.length;
|
|
28
|
+
}
|
|
29
|
+
get maxDegree() {
|
|
30
|
+
return this.largestDegree;
|
|
31
|
+
}
|
|
32
|
+
get revision() {
|
|
33
|
+
return this.currentRevision;
|
|
34
|
+
}
|
|
35
|
+
/** Number of live nodes. */
|
|
36
|
+
get nodeCount() {
|
|
37
|
+
return this.alive.length - this.freeSlots.length;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Adds a node. Omitting the position puts the graph in Dijkstra mode: the
|
|
41
|
+
* heuristic drops to zero for every query, since a positionless node makes no
|
|
42
|
+
* geometric estimate meaningful.
|
|
43
|
+
*/
|
|
44
|
+
addNode(x, y) {
|
|
45
|
+
const hasPosition = x !== void 0 && y !== void 0;
|
|
46
|
+
const node = this.freeSlots.pop() ?? this.alive.length;
|
|
47
|
+
this.positionsX[node] = x ?? 0;
|
|
48
|
+
this.positionsY[node] = y ?? 0;
|
|
49
|
+
this.positioned[node] = hasPosition;
|
|
50
|
+
this.alive[node] = true;
|
|
51
|
+
this.adjacency[node] = [];
|
|
52
|
+
if (!hasPosition) this.positionlessCount++;
|
|
53
|
+
this.currentRevision++;
|
|
54
|
+
return node;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Removes a node together with every edge touching it.
|
|
58
|
+
*
|
|
59
|
+
* Ids are recycled: a later {@link addNode} may hand out the id this call
|
|
60
|
+
* freed, so a node id held across a removal can silently refer to a different
|
|
61
|
+
* node.
|
|
62
|
+
*/
|
|
63
|
+
removeNode(node) {
|
|
64
|
+
if (this.alive[node] !== true) return;
|
|
65
|
+
if (this.positioned[node] === false) this.positionlessCount--;
|
|
66
|
+
this.alive[node] = false;
|
|
67
|
+
this.adjacency[node] = [];
|
|
68
|
+
this.freeSlots.push(node);
|
|
69
|
+
for (let index = 0; index < this.adjacency.length; index++) {
|
|
70
|
+
const edges = this.adjacency[index];
|
|
71
|
+
for (let edge = edges.length - 1; edge >= 0; edge--) if (edges[edge].to === node) edges.splice(edge, 1);
|
|
72
|
+
}
|
|
73
|
+
this.currentRevision++;
|
|
74
|
+
}
|
|
75
|
+
/** Adds a directed edge. A second edge between the same pair replaces the first. */
|
|
76
|
+
addEdge(from, to, options = {}) {
|
|
77
|
+
const edges = this.adjacency[from];
|
|
78
|
+
if (edges === void 0 || this.alive[to] !== true) return;
|
|
79
|
+
const length = this.length(from, to);
|
|
80
|
+
const cost = options.cost ?? (length > 0 ? length : 1);
|
|
81
|
+
const edge = {
|
|
82
|
+
to,
|
|
83
|
+
cost,
|
|
84
|
+
kind: options.kind ?? "walk",
|
|
85
|
+
data: options.data ?? null
|
|
86
|
+
};
|
|
87
|
+
const existing = edges.findIndex((candidate) => candidate.to === to);
|
|
88
|
+
if (existing !== -1) edges[existing] = edge;
|
|
89
|
+
else edges.push(edge);
|
|
90
|
+
if (edges.length > this.largestDegree) this.largestDegree = edges.length;
|
|
91
|
+
if (length > 0 && cost / length < this.distanceScale) this.distanceScale = cost / length;
|
|
92
|
+
this.currentRevision++;
|
|
93
|
+
}
|
|
94
|
+
/** Adds the edge in both directions with the same options. */
|
|
95
|
+
connect(a, b, options = {}) {
|
|
96
|
+
this.addEdge(a, b, options);
|
|
97
|
+
this.addEdge(b, a, options);
|
|
98
|
+
}
|
|
99
|
+
removeEdge(from, to) {
|
|
100
|
+
const edges = this.adjacency[from];
|
|
101
|
+
if (edges === void 0) return;
|
|
102
|
+
const index = edges.findIndex((candidate) => candidate.to === to);
|
|
103
|
+
if (index === -1) return;
|
|
104
|
+
edges.splice(index, 1);
|
|
105
|
+
this.currentRevision++;
|
|
106
|
+
}
|
|
107
|
+
neighbors(node, _agentSize, outNodes, outCosts) {
|
|
108
|
+
const edges = this.adjacency[node];
|
|
109
|
+
if (edges === void 0) return 0;
|
|
110
|
+
for (let index = 0; index < edges.length; index++) {
|
|
111
|
+
const edge = edges[index];
|
|
112
|
+
outNodes[index] = edge.to;
|
|
113
|
+
outCosts[index] = edge.cost;
|
|
114
|
+
}
|
|
115
|
+
return edges.length;
|
|
116
|
+
}
|
|
117
|
+
heuristic(node, goal) {
|
|
118
|
+
if (this.positionlessCount > 0) return 0;
|
|
119
|
+
return this.length(node, goal) * this.distanceScale;
|
|
120
|
+
}
|
|
121
|
+
nodeToPoint(node, out) {
|
|
122
|
+
out.set(this.positionsX[node] ?? 0, this.positionsY[node] ?? 0);
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* The positioned node closest to the point, or `-1` when the graph has none.
|
|
126
|
+
* A graph has no cells, so there is no "outside" for a point to fall into.
|
|
127
|
+
*/
|
|
128
|
+
pointToNode(x, y) {
|
|
129
|
+
let best = -1;
|
|
130
|
+
let bestDistance = Infinity;
|
|
131
|
+
for (let node = 0; node < this.alive.length; node++) {
|
|
132
|
+
if (this.alive[node] !== true || this.positioned[node] !== true) continue;
|
|
133
|
+
const deltaX = this.positionsX[node] - x;
|
|
134
|
+
const deltaY = this.positionsY[node] - y;
|
|
135
|
+
const distance = deltaX * deltaX + deltaY * deltaY;
|
|
136
|
+
if (distance >= bestDistance) continue;
|
|
137
|
+
bestDistance = distance;
|
|
138
|
+
best = node;
|
|
139
|
+
}
|
|
140
|
+
return best;
|
|
141
|
+
}
|
|
142
|
+
nearestNode(x, y) {
|
|
143
|
+
return this.pointToNode(x, y);
|
|
144
|
+
}
|
|
145
|
+
describeEdge(from, to) {
|
|
146
|
+
const edges = this.adjacency[from];
|
|
147
|
+
if (edges === void 0) return null;
|
|
148
|
+
for (let index = 0; index < edges.length; index++) {
|
|
149
|
+
const edge = edges[index];
|
|
150
|
+
if (edge.to === to) return {
|
|
151
|
+
from,
|
|
152
|
+
to,
|
|
153
|
+
kind: edge.kind,
|
|
154
|
+
data: edge.data
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
return null;
|
|
158
|
+
}
|
|
159
|
+
length(from, to) {
|
|
160
|
+
if (this.positioned[from] !== true || this.positioned[to] !== true) return 0;
|
|
161
|
+
return Math.hypot(this.positionsX[to] - this.positionsX[from], this.positionsY[to] - this.positionsY[from]);
|
|
162
|
+
}
|
|
163
|
+
};
|
|
164
|
+
|
|
165
|
+
//#endregion
|
|
166
|
+
export { WaypointGraph };
|
|
167
|
+
//# sourceMappingURL=WaypointGraph.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"WaypointGraph.js","names":[],"sources":["../../../src/spaces/WaypointGraph.ts"],"sourcesContent":["import type { Vector } from '@codexo/exojs';\n\nimport type { NavigationSpace, PathEdge } from '../types';\n\n/** Options for {@link WaypointGraph.addEdge} and {@link WaypointGraph.connect}. */\nexport interface WaypointEdgeOptions<Payload = unknown> {\n /**\n * Traversal cost. Defaults to the straight-line distance between the two\n * nodes, or `1` when either of them has no position.\n */\n readonly cost?: number | undefined;\n /**\n * Free-form traversal tag surfaced through {@link PathResult.edges} - the\n * hook a movement controller reads to tell a jump from a walk. Defaults to\n * `'walk'`.\n */\n readonly kind?: string | undefined;\n /** Payload for the movement controller: jump impulse, ladder id, anything. */\n readonly data?: Payload | undefined;\n}\n\ninterface Edge<Payload> {\n to: number;\n cost: number;\n kind: string;\n data: Payload | null;\n}\n\n/**\n * A directed graph of hand-placed waypoints.\n *\n * This is the representation for worlds a grid cannot describe: a platformer\n * where traversal is a topology of walk, jump and fall links rather than cell\n * walkability, or a purely abstract graph with no geometry at all. Nodes carry\n * an optional position, edges carry a cost, a {@link WaypointEdgeOptions.kind}\n * and an arbitrary payload, and the resulting path reports the edges it took so\n * the game can execute each step in its own way.\n *\n * With positions the search is A* over straight-line distance; without them the\n * heuristic is zero and the same search degrades cleanly to Dijkstra.\n */\nexport class WaypointGraph<Payload = unknown> implements NavigationSpace<Payload> {\n private readonly positionsX: number[] = [];\n private readonly positionsY: number[] = [];\n private readonly adjacency: Array<Array<Edge<Payload>>> = [];\n private readonly alive: boolean[] = [];\n private readonly positioned: boolean[] = [];\n private readonly freeSlots: number[] = [];\n private positionlessCount = 0;\n private largestDegree = 0;\n private currentRevision = 0;\n // Smallest ratio of an edge's cost to its straight-line length. A caller who\n // prices an edge below its geometric length (a zipline, a teleporter) would\n // otherwise make the distance heuristic overestimate and cost the search its\n // optimality; scaling by this ratio keeps it admissible instead of forbidding\n // the edge. Never raised when an edge is removed - a looser bound is still a\n // correct one, and rescanning every edge on every removal is not worth it.\n private distanceScale = 1;\n\n public get nodeCapacity(): number {\n return this.alive.length;\n }\n\n public get maxDegree(): number {\n return this.largestDegree;\n }\n\n public get revision(): number {\n return this.currentRevision;\n }\n\n /** Number of live nodes. */\n public get nodeCount(): number {\n return this.alive.length - this.freeSlots.length;\n }\n\n /**\n * Adds a node. Omitting the position puts the graph in Dijkstra mode: the\n * heuristic drops to zero for every query, since a positionless node makes no\n * geometric estimate meaningful.\n */\n public addNode(x?: number, y?: number): number {\n const hasPosition = x !== undefined && y !== undefined;\n const slot = this.freeSlots.pop();\n const node = slot ?? this.alive.length;\n\n this.positionsX[node] = x ?? 0;\n this.positionsY[node] = y ?? 0;\n this.positioned[node] = hasPosition;\n this.alive[node] = true;\n this.adjacency[node] = [];\n\n if (!hasPosition) this.positionlessCount++;\n\n this.currentRevision++;\n\n return node;\n }\n\n /**\n * Removes a node together with every edge touching it.\n *\n * Ids are recycled: a later {@link addNode} may hand out the id this call\n * freed, so a node id held across a removal can silently refer to a different\n * node.\n */\n public removeNode(node: number): void {\n if (this.alive[node] !== true) return;\n\n if (this.positioned[node] === false) this.positionlessCount--;\n\n this.alive[node] = false;\n this.adjacency[node] = [];\n this.freeSlots.push(node);\n\n for (let index = 0; index < this.adjacency.length; index++) {\n const edges = this.adjacency[index]!;\n\n for (let edge = edges.length - 1; edge >= 0; edge--) {\n if (edges[edge]!.to === node) edges.splice(edge, 1);\n }\n }\n\n this.currentRevision++;\n }\n\n /** Adds a directed edge. A second edge between the same pair replaces the first. */\n public addEdge(from: number, to: number, options: WaypointEdgeOptions<Payload> = {}): void {\n const edges = this.adjacency[from];\n\n if (edges === undefined || this.alive[to] !== true) return;\n\n const length = this.length(from, to);\n const cost = options.cost ?? (length > 0 ? length : 1);\n const edge: Edge<Payload> = { to, cost, kind: options.kind ?? 'walk', data: options.data ?? null };\n const existing = edges.findIndex(candidate => candidate.to === to);\n\n if (existing !== -1) edges[existing] = edge;\n else edges.push(edge);\n\n if (edges.length > this.largestDegree) this.largestDegree = edges.length;\n if (length > 0 && cost / length < this.distanceScale) this.distanceScale = cost / length;\n\n this.currentRevision++;\n }\n\n /** Adds the edge in both directions with the same options. */\n public connect(a: number, b: number, options: WaypointEdgeOptions<Payload> = {}): void {\n this.addEdge(a, b, options);\n this.addEdge(b, a, options);\n }\n\n public removeEdge(from: number, to: number): void {\n const edges = this.adjacency[from];\n\n if (edges === undefined) return;\n\n const index = edges.findIndex(candidate => candidate.to === to);\n\n if (index === -1) return;\n\n edges.splice(index, 1);\n this.currentRevision++;\n }\n\n public neighbors(node: number, _agentSize: number, outNodes: Int32Array, outCosts: Float64Array): number {\n const edges = this.adjacency[node];\n\n if (edges === undefined) return 0;\n\n for (let index = 0; index < edges.length; index++) {\n const edge = edges[index]!;\n\n outNodes[index] = edge.to;\n outCosts[index] = edge.cost;\n }\n\n return edges.length;\n }\n\n public heuristic(node: number, goal: number): number {\n if (this.positionlessCount > 0) return 0;\n\n return this.length(node, goal) * this.distanceScale;\n }\n\n public nodeToPoint(node: number, out: Vector): void {\n out.set(this.positionsX[node] ?? 0, this.positionsY[node] ?? 0);\n }\n\n /**\n * The positioned node closest to the point, or `-1` when the graph has none.\n * A graph has no cells, so there is no \"outside\" for a point to fall into.\n */\n public pointToNode(x: number, y: number): number {\n let best = -1;\n let bestDistance = Infinity;\n\n for (let node = 0; node < this.alive.length; node++) {\n if (this.alive[node] !== true || this.positioned[node] !== true) continue;\n\n const deltaX = this.positionsX[node]! - x;\n const deltaY = this.positionsY[node]! - y;\n const distance = deltaX * deltaX + deltaY * deltaY;\n\n if (distance >= bestDistance) continue;\n\n bestDistance = distance;\n best = node;\n }\n\n return best;\n }\n\n public nearestNode(x: number, y: number): number {\n return this.pointToNode(x, y);\n }\n\n public describeEdge(from: number, to: number): PathEdge<Payload> | null {\n const edges = this.adjacency[from];\n\n if (edges === undefined) return null;\n\n for (let index = 0; index < edges.length; index++) {\n const edge = edges[index]!;\n\n if (edge.to === to) return { from, to, kind: edge.kind, data: edge.data };\n }\n\n return null;\n }\n\n private length(from: number, to: number): number {\n if (this.positioned[from] !== true || this.positioned[to] !== true) return 0;\n\n return Math.hypot(this.positionsX[to]! - this.positions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WAAW,KAAM;CAChH;AACF"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"gridGeometry.d.ts","sourceRoot":"","sources":["../../../src/spaces/gridGeometry.ts"],"names":[],"mappings":"AAAA,0FAA0F"}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
//#region src/spaces/gridGeometry.ts
|
|
2
|
+
/** Grid step geometry shared by the neighbour expansion and the jump search. @internal */
|
|
3
|
+
const SQRT2 = Math.SQRT2;
|
|
4
|
+
/**
|
|
5
|
+
* Orthogonal directions first, then diagonals: a four-connected grid is the
|
|
6
|
+
* same table truncated to four entries, and the fixed order is half of what
|
|
7
|
+
* makes equal-cost searches reproducible (the heap tie-break is the other half).
|
|
8
|
+
*
|
|
9
|
+
* @internal
|
|
10
|
+
*/
|
|
11
|
+
const DIRECTION_X = [
|
|
12
|
+
1,
|
|
13
|
+
-1,
|
|
14
|
+
0,
|
|
15
|
+
0,
|
|
16
|
+
1,
|
|
17
|
+
1,
|
|
18
|
+
-1,
|
|
19
|
+
-1
|
|
20
|
+
];
|
|
21
|
+
/** @internal */
|
|
22
|
+
const DIRECTION_Y = [
|
|
23
|
+
0,
|
|
24
|
+
0,
|
|
25
|
+
1,
|
|
26
|
+
-1,
|
|
27
|
+
1,
|
|
28
|
+
-1,
|
|
29
|
+
1,
|
|
30
|
+
-1
|
|
31
|
+
];
|
|
32
|
+
/** Octile length of a straight or diagonal run between two cells. @internal */
|
|
33
|
+
const runLength = (spanX, spanY) => {
|
|
34
|
+
const diagonal = Math.min(spanX, spanY);
|
|
35
|
+
return spanX + spanY - 2 * diagonal + diagonal * SQRT2;
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
//#endregion
|
|
39
|
+
export { DIRECTION_X, DIRECTION_Y, SQRT2, runLength };
|
|
40
|
+
//# sourceMappingURL=gridGeometry.js.map
|