@esengine/pathfinding 1.0.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/.idea/misc.xml +6 -0
- package/.idea/modules.xml +8 -0
- package/.idea/source.iml +12 -0
- package/.idea/vcs.xml +6 -0
- package/.vscode/tasks.json +13 -0
- package/.wing/settings.json +3 -0
- package/README.md +245 -0
- package/bin/README.md +245 -0
- package/bin/modules/astar.js +559 -0
- package/bin/modules/astar.min.js +1 -0
- package/bin/modules/breadth-first.js +559 -0
- package/bin/modules/breadth-first.min.js +1 -0
- package/bin/package.json +54 -0
- package/bin/pathfinding.d.ts +167 -0
- package/bin/pathfinding.js +559 -0
- package/bin/pathfinding.min.js +1 -0
- package/gulpfile.js +137 -0
- package/lib/wxgame.d.ts +3945 -0
- package/package.json +52 -0
- package/src/AI/Pathfinding/AStar/AStarPathfinder.ts +244 -0
- package/src/AI/Pathfinding/AStar/AstarGridGraph.ts +183 -0
- package/src/AI/Pathfinding/AStar/IAstarGraph.ts +30 -0
- package/src/AI/Pathfinding/BreadthFirst/BreadthFirstPathfinder.ts +109 -0
- package/src/AI/Pathfinding/BreadthFirst/IUnweightedGraph.ts +14 -0
- package/src/AI/Pathfinding/BreadthFirst/UnweightedGraph.ts +29 -0
- package/src/AI/Pathfinding/BreadthFirst/UnweightedGridGraph.ts +81 -0
- package/src/Types/IVector2.ts +102 -0
- package/src/Utils/PriorityQueue.ts +121 -0
- package/src/index.ts +49 -0
- package/tsconfig.json +34 -0
|
@@ -0,0 +1,559 @@
|
|
|
1
|
+
// 广度优先搜索算法模块 v1.0.1
|
|
2
|
+
export { Vector2Utils } from './Types/IVector2';
|
|
3
|
+
export { PriorityQueue } from './Utils/PriorityQueue';
|
|
4
|
+
export { AStarPathfinder } from './AI/Pathfinding/AStar/AStarPathfinder';
|
|
5
|
+
export { AstarGridGraph } from './AI/Pathfinding/AStar/AstarGridGraph';
|
|
6
|
+
export { BreadthFirstPathfinder } from './AI/Pathfinding/BreadthFirst/BreadthFirstPathfinder';
|
|
7
|
+
export { UnweightedGraph } from './AI/Pathfinding/BreadthFirst/UnweightedGraph';
|
|
8
|
+
export { UnweightedGridGraph } from './AI/Pathfinding/BreadthFirst/UnweightedGridGraph';
|
|
9
|
+
|
|
10
|
+
var Vector2Utils = (function () {
|
|
11
|
+
function Vector2Utils() {
|
|
12
|
+
}
|
|
13
|
+
Vector2Utils.equals = function (a, b) {
|
|
14
|
+
if (a.equals) {
|
|
15
|
+
return a.equals(b);
|
|
16
|
+
}
|
|
17
|
+
return a.x === b.x && a.y === b.y;
|
|
18
|
+
};
|
|
19
|
+
Vector2Utils.create = function (x, y) {
|
|
20
|
+
return { x: x, y: y };
|
|
21
|
+
};
|
|
22
|
+
Vector2Utils.clone = function (vector) {
|
|
23
|
+
return { x: vector.x, y: vector.y };
|
|
24
|
+
};
|
|
25
|
+
Vector2Utils.add = function (a, b) {
|
|
26
|
+
return { x: a.x + b.x, y: a.y + b.y };
|
|
27
|
+
};
|
|
28
|
+
Vector2Utils.manhattanDistance = function (a, b) {
|
|
29
|
+
return Math.abs(a.x - b.x) + Math.abs(a.y - b.y);
|
|
30
|
+
};
|
|
31
|
+
Vector2Utils.distance = function (a, b) {
|
|
32
|
+
var dx = a.x - b.x;
|
|
33
|
+
var dy = a.y - b.y;
|
|
34
|
+
return Math.sqrt(dx * dx + dy * dy);
|
|
35
|
+
};
|
|
36
|
+
Vector2Utils.toHash = function (vector) {
|
|
37
|
+
var x = (vector.x + this.MAX_COORD) | 0;
|
|
38
|
+
var y = (vector.y + this.MAX_COORD) | 0;
|
|
39
|
+
return (x << 16) | y;
|
|
40
|
+
};
|
|
41
|
+
Vector2Utils.toKey = function (vector) {
|
|
42
|
+
return "".concat(vector.x, ",").concat(vector.y);
|
|
43
|
+
};
|
|
44
|
+
Vector2Utils.fromHash = function (hash) {
|
|
45
|
+
var x = (hash >> 16) - this.MAX_COORD;
|
|
46
|
+
var y = (hash & 0xFFFF) - this.MAX_COORD;
|
|
47
|
+
return { x: x, y: y };
|
|
48
|
+
};
|
|
49
|
+
Vector2Utils.HASH_MULTIPLIER = 73856093;
|
|
50
|
+
Vector2Utils.MAX_COORD = 32767;
|
|
51
|
+
return Vector2Utils;
|
|
52
|
+
}());
|
|
53
|
+
export { Vector2Utils };
|
|
54
|
+
|
|
55
|
+
var PriorityQueue = (function () {
|
|
56
|
+
function PriorityQueue() {
|
|
57
|
+
this._heap = [];
|
|
58
|
+
this._size = 0;
|
|
59
|
+
}
|
|
60
|
+
Object.defineProperty(PriorityQueue.prototype, "size", {
|
|
61
|
+
get: function () {
|
|
62
|
+
return this._size;
|
|
63
|
+
},
|
|
64
|
+
enumerable: false,
|
|
65
|
+
configurable: true
|
|
66
|
+
});
|
|
67
|
+
Object.defineProperty(PriorityQueue.prototype, "isEmpty", {
|
|
68
|
+
get: function () {
|
|
69
|
+
return this._size === 0;
|
|
70
|
+
},
|
|
71
|
+
enumerable: false,
|
|
72
|
+
configurable: true
|
|
73
|
+
});
|
|
74
|
+
PriorityQueue.prototype.clear = function () {
|
|
75
|
+
this._heap.length = 0;
|
|
76
|
+
this._size = 0;
|
|
77
|
+
};
|
|
78
|
+
PriorityQueue.prototype.enqueue = function (item) {
|
|
79
|
+
this._heap[this._size] = item;
|
|
80
|
+
this._bubbleUp(this._size);
|
|
81
|
+
this._size++;
|
|
82
|
+
};
|
|
83
|
+
PriorityQueue.prototype.dequeue = function () {
|
|
84
|
+
if (this._size === 0) {
|
|
85
|
+
return undefined;
|
|
86
|
+
}
|
|
87
|
+
var result = this._heap[0];
|
|
88
|
+
this._size--;
|
|
89
|
+
if (this._size > 0) {
|
|
90
|
+
this._heap[0] = this._heap[this._size];
|
|
91
|
+
this._bubbleDown(0);
|
|
92
|
+
}
|
|
93
|
+
return result;
|
|
94
|
+
};
|
|
95
|
+
PriorityQueue.prototype.peek = function () {
|
|
96
|
+
return this._size > 0 ? this._heap[0] : undefined;
|
|
97
|
+
};
|
|
98
|
+
PriorityQueue.prototype._bubbleUp = function (index) {
|
|
99
|
+
while (index > 0) {
|
|
100
|
+
var parentIndex = Math.floor((index - 1) / 2);
|
|
101
|
+
if (this._heap[index].priority >= this._heap[parentIndex].priority) {
|
|
102
|
+
break;
|
|
103
|
+
}
|
|
104
|
+
this._swap(index, parentIndex);
|
|
105
|
+
index = parentIndex;
|
|
106
|
+
}
|
|
107
|
+
};
|
|
108
|
+
PriorityQueue.prototype._bubbleDown = function (index) {
|
|
109
|
+
while (true) {
|
|
110
|
+
var minIndex = index;
|
|
111
|
+
var leftChild = 2 * index + 1;
|
|
112
|
+
var rightChild = 2 * index + 2;
|
|
113
|
+
if (leftChild < this._size &&
|
|
114
|
+
this._heap[leftChild].priority < this._heap[minIndex].priority) {
|
|
115
|
+
minIndex = leftChild;
|
|
116
|
+
}
|
|
117
|
+
if (rightChild < this._size &&
|
|
118
|
+
this._heap[rightChild].priority < this._heap[minIndex].priority) {
|
|
119
|
+
minIndex = rightChild;
|
|
120
|
+
}
|
|
121
|
+
if (minIndex === index) {
|
|
122
|
+
break;
|
|
123
|
+
}
|
|
124
|
+
this._swap(index, minIndex);
|
|
125
|
+
index = minIndex;
|
|
126
|
+
}
|
|
127
|
+
};
|
|
128
|
+
PriorityQueue.prototype._swap = function (i, j) {
|
|
129
|
+
var temp = this._heap[i];
|
|
130
|
+
this._heap[i] = this._heap[j];
|
|
131
|
+
this._heap[j] = temp;
|
|
132
|
+
};
|
|
133
|
+
return PriorityQueue;
|
|
134
|
+
}());
|
|
135
|
+
export { PriorityQueue };
|
|
136
|
+
|
|
137
|
+
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
138
|
+
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
139
|
+
if (ar || !(i in from)) {
|
|
140
|
+
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
141
|
+
ar[i] = from[i];
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
return to.concat(ar || Array.prototype.slice.call(from));
|
|
145
|
+
};
|
|
146
|
+
import { Vector2Utils } from '../../../Types/IVector2';
|
|
147
|
+
import { PriorityQueue } from '../../../Utils/PriorityQueue';
|
|
148
|
+
var AStarNode = (function () {
|
|
149
|
+
function AStarNode(node, gCost, hCost, parent) {
|
|
150
|
+
if (gCost === void 0) { gCost = 0; }
|
|
151
|
+
if (hCost === void 0) { hCost = 0; }
|
|
152
|
+
if (parent === void 0) { parent = null; }
|
|
153
|
+
this.priority = 0;
|
|
154
|
+
this.gCost = 0;
|
|
155
|
+
this.hCost = 0;
|
|
156
|
+
this.parent = null;
|
|
157
|
+
this.hash = 0;
|
|
158
|
+
this.node = node;
|
|
159
|
+
this.gCost = gCost;
|
|
160
|
+
this.hCost = hCost;
|
|
161
|
+
this.priority = gCost + hCost;
|
|
162
|
+
this.parent = parent;
|
|
163
|
+
this.hash = Vector2Utils.toHash(node);
|
|
164
|
+
}
|
|
165
|
+
AStarNode.prototype.updateCosts = function (gCost, hCost, parent) {
|
|
166
|
+
if (parent === void 0) { parent = null; }
|
|
167
|
+
this.gCost = gCost;
|
|
168
|
+
this.hCost = hCost;
|
|
169
|
+
this.priority = gCost + hCost;
|
|
170
|
+
this.parent = parent;
|
|
171
|
+
};
|
|
172
|
+
AStarNode.prototype.updateNode = function (node, gCost, hCost, parent) {
|
|
173
|
+
if (gCost === void 0) { gCost = 0; }
|
|
174
|
+
if (hCost === void 0) { hCost = 0; }
|
|
175
|
+
if (parent === void 0) { parent = null; }
|
|
176
|
+
this.node = node;
|
|
177
|
+
this.gCost = gCost;
|
|
178
|
+
this.hCost = hCost;
|
|
179
|
+
this.priority = gCost + hCost;
|
|
180
|
+
this.parent = parent;
|
|
181
|
+
this.hash = Vector2Utils.toHash(node);
|
|
182
|
+
};
|
|
183
|
+
AStarNode.prototype.reset = function () {
|
|
184
|
+
this.node = null;
|
|
185
|
+
this.priority = 0;
|
|
186
|
+
this.gCost = 0;
|
|
187
|
+
this.hCost = 0;
|
|
188
|
+
this.parent = null;
|
|
189
|
+
this.hash = 0;
|
|
190
|
+
};
|
|
191
|
+
return AStarNode;
|
|
192
|
+
}());
|
|
193
|
+
var AStarPathfinder = (function () {
|
|
194
|
+
function AStarPathfinder() {
|
|
195
|
+
}
|
|
196
|
+
AStarPathfinder._getNode = function (node, gCost, hCost, parent) {
|
|
197
|
+
if (gCost === void 0) { gCost = 0; }
|
|
198
|
+
if (hCost === void 0) { hCost = 0; }
|
|
199
|
+
if (parent === void 0) { parent = null; }
|
|
200
|
+
var astarNode = this._nodePool.pop();
|
|
201
|
+
if (!astarNode) {
|
|
202
|
+
astarNode = new AStarNode(node, gCost, hCost, parent);
|
|
203
|
+
}
|
|
204
|
+
else {
|
|
205
|
+
astarNode.updateNode(node, gCost, hCost, parent);
|
|
206
|
+
}
|
|
207
|
+
return astarNode;
|
|
208
|
+
};
|
|
209
|
+
AStarPathfinder._recycleNode = function (node) {
|
|
210
|
+
if (this._nodePool.length < 1000) {
|
|
211
|
+
node.reset();
|
|
212
|
+
this._nodePool.push(node);
|
|
213
|
+
}
|
|
214
|
+
};
|
|
215
|
+
AStarPathfinder.search = function (graph, start, goal) {
|
|
216
|
+
var openSet = new PriorityQueue();
|
|
217
|
+
var closedSet = new Set();
|
|
218
|
+
var openSetMap = new Map();
|
|
219
|
+
var startHash = Vector2Utils.toHash(start);
|
|
220
|
+
var goalHash = Vector2Utils.toHash(goal);
|
|
221
|
+
if (startHash === goalHash) {
|
|
222
|
+
return { found: true, goalNode: this._getNode(start, 0, 0) };
|
|
223
|
+
}
|
|
224
|
+
var startNode = this._getNode(start, 0, graph.heuristic(start, goal));
|
|
225
|
+
openSet.enqueue(startNode);
|
|
226
|
+
openSetMap.set(startHash, startNode);
|
|
227
|
+
var goalNode;
|
|
228
|
+
while (!openSet.isEmpty) {
|
|
229
|
+
var current = openSet.dequeue();
|
|
230
|
+
var currentHash = current.hash;
|
|
231
|
+
openSetMap.delete(currentHash);
|
|
232
|
+
if (currentHash === goalHash) {
|
|
233
|
+
goalNode = current;
|
|
234
|
+
break;
|
|
235
|
+
}
|
|
236
|
+
closedSet.add(currentHash);
|
|
237
|
+
for (var _i = 0, _a = graph.getNeighbors(current.node); _i < _a.length; _i++) {
|
|
238
|
+
var neighbor = _a[_i];
|
|
239
|
+
var neighborHash = Vector2Utils.toHash(neighbor);
|
|
240
|
+
if (closedSet.has(neighborHash)) {
|
|
241
|
+
continue;
|
|
242
|
+
}
|
|
243
|
+
var tentativeGScore = current.gCost + graph.cost(current.node, neighbor);
|
|
244
|
+
var existingNode = openSetMap.get(neighborHash);
|
|
245
|
+
if (existingNode) {
|
|
246
|
+
if (tentativeGScore < existingNode.gCost) {
|
|
247
|
+
var hCost = existingNode.hCost;
|
|
248
|
+
existingNode.updateCosts(tentativeGScore, hCost, current);
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
else {
|
|
252
|
+
var hCost = graph.heuristic(neighbor, goal);
|
|
253
|
+
var neighborNode = this._getNode(neighbor, tentativeGScore, hCost, current);
|
|
254
|
+
openSet.enqueue(neighborNode);
|
|
255
|
+
openSetMap.set(neighborHash, neighborNode);
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
if (current !== goalNode) {
|
|
259
|
+
this._recycleNode(current);
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
while (!openSet.isEmpty) {
|
|
263
|
+
var node = openSet.dequeue();
|
|
264
|
+
if (node !== goalNode) {
|
|
265
|
+
this._recycleNode(node);
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
return { found: !!goalNode, goalNode: goalNode };
|
|
269
|
+
};
|
|
270
|
+
AStarPathfinder.searchPath = function (graph, start, goal) {
|
|
271
|
+
var result = this.search(graph, start, goal);
|
|
272
|
+
if (!result.found || !result.goalNode) {
|
|
273
|
+
return [];
|
|
274
|
+
}
|
|
275
|
+
return this.reconstructPathFromNode(result.goalNode, start);
|
|
276
|
+
};
|
|
277
|
+
AStarPathfinder.reconstructPathFromNode = function (goalNode, start) {
|
|
278
|
+
this._tempPath.length = 0;
|
|
279
|
+
var current = goalNode;
|
|
280
|
+
var startHash = Vector2Utils.toHash(start);
|
|
281
|
+
while (current) {
|
|
282
|
+
this._tempPath.unshift(current.node);
|
|
283
|
+
var currentHash = current.hash;
|
|
284
|
+
var parent_1 = current.parent;
|
|
285
|
+
if (currentHash !== startHash) {
|
|
286
|
+
this._recycleNode(current);
|
|
287
|
+
}
|
|
288
|
+
current = parent_1;
|
|
289
|
+
}
|
|
290
|
+
return __spreadArray([], this._tempPath, true);
|
|
291
|
+
};
|
|
292
|
+
AStarPathfinder.hasPath = function (graph, start, goal) {
|
|
293
|
+
var result = this.search(graph, start, goal);
|
|
294
|
+
if (result.goalNode) {
|
|
295
|
+
this._recycleNode(result.goalNode);
|
|
296
|
+
}
|
|
297
|
+
return result.found;
|
|
298
|
+
};
|
|
299
|
+
AStarPathfinder.clearPool = function () {
|
|
300
|
+
this._nodePool.length = 0;
|
|
301
|
+
this._tempPath.length = 0;
|
|
302
|
+
};
|
|
303
|
+
AStarPathfinder.getPoolStats = function () {
|
|
304
|
+
return {
|
|
305
|
+
poolSize: this._nodePool.length,
|
|
306
|
+
maxPoolSize: 1000
|
|
307
|
+
};
|
|
308
|
+
};
|
|
309
|
+
AStarPathfinder._nodePool = [];
|
|
310
|
+
AStarPathfinder._tempPath = [];
|
|
311
|
+
return AStarPathfinder;
|
|
312
|
+
}());
|
|
313
|
+
export { AStarPathfinder };
|
|
314
|
+
|
|
315
|
+
import { Vector2Utils } from '../../../Types/IVector2';
|
|
316
|
+
import { AStarPathfinder } from './AStarPathfinder';
|
|
317
|
+
var AstarGridGraph = (function () {
|
|
318
|
+
function AstarGridGraph(width, height) {
|
|
319
|
+
this.dirs = [
|
|
320
|
+
Vector2Utils.create(1, 0),
|
|
321
|
+
Vector2Utils.create(0, -1),
|
|
322
|
+
Vector2Utils.create(-1, 0),
|
|
323
|
+
Vector2Utils.create(0, 1)
|
|
324
|
+
];
|
|
325
|
+
this.walls = [];
|
|
326
|
+
this.weightedNodes = [];
|
|
327
|
+
this.defaultWeight = 1;
|
|
328
|
+
this.weightedNodeWeight = 5;
|
|
329
|
+
this._neighbors = new Array(4);
|
|
330
|
+
this._wallsSet = new Set();
|
|
331
|
+
this._weightedNodesSet = new Set();
|
|
332
|
+
this._wallsDirty = true;
|
|
333
|
+
this._weightedNodesDirty = true;
|
|
334
|
+
this._width = width;
|
|
335
|
+
this._height = height;
|
|
336
|
+
}
|
|
337
|
+
AstarGridGraph.prototype.addWall = function (wall) {
|
|
338
|
+
this.walls.push(wall);
|
|
339
|
+
this._wallsDirty = true;
|
|
340
|
+
};
|
|
341
|
+
AstarGridGraph.prototype.addWalls = function (walls) {
|
|
342
|
+
var _a;
|
|
343
|
+
(_a = this.walls).push.apply(_a, walls);
|
|
344
|
+
this._wallsDirty = true;
|
|
345
|
+
};
|
|
346
|
+
AstarGridGraph.prototype.clearWalls = function () {
|
|
347
|
+
this.walls.length = 0;
|
|
348
|
+
this._wallsSet.clear();
|
|
349
|
+
this._wallsDirty = false;
|
|
350
|
+
};
|
|
351
|
+
AstarGridGraph.prototype.addWeightedNode = function (node) {
|
|
352
|
+
this.weightedNodes.push(node);
|
|
353
|
+
this._weightedNodesDirty = true;
|
|
354
|
+
};
|
|
355
|
+
AstarGridGraph.prototype.addWeightedNodes = function (nodes) {
|
|
356
|
+
var _a;
|
|
357
|
+
(_a = this.weightedNodes).push.apply(_a, nodes);
|
|
358
|
+
this._weightedNodesDirty = true;
|
|
359
|
+
};
|
|
360
|
+
AstarGridGraph.prototype.clearWeightedNodes = function () {
|
|
361
|
+
this.weightedNodes.length = 0;
|
|
362
|
+
this._weightedNodesSet.clear();
|
|
363
|
+
this._weightedNodesDirty = false;
|
|
364
|
+
};
|
|
365
|
+
AstarGridGraph.prototype._updateHashSets = function () {
|
|
366
|
+
if (this._wallsDirty) {
|
|
367
|
+
this._wallsSet.clear();
|
|
368
|
+
for (var _i = 0, _a = this.walls; _i < _a.length; _i++) {
|
|
369
|
+
var wall = _a[_i];
|
|
370
|
+
this._wallsSet.add(Vector2Utils.toHash(wall));
|
|
371
|
+
}
|
|
372
|
+
this._wallsDirty = false;
|
|
373
|
+
}
|
|
374
|
+
if (this._weightedNodesDirty) {
|
|
375
|
+
this._weightedNodesSet.clear();
|
|
376
|
+
for (var _b = 0, _c = this.weightedNodes; _b < _c.length; _b++) {
|
|
377
|
+
var node = _c[_b];
|
|
378
|
+
this._weightedNodesSet.add(Vector2Utils.toHash(node));
|
|
379
|
+
}
|
|
380
|
+
this._weightedNodesDirty = false;
|
|
381
|
+
}
|
|
382
|
+
};
|
|
383
|
+
AstarGridGraph.prototype.isNodeInBounds = function (node) {
|
|
384
|
+
return 0 <= node.x && node.x < this._width && 0 <= node.y && node.y < this._height;
|
|
385
|
+
};
|
|
386
|
+
AstarGridGraph.prototype.isNodePassable = function (node) {
|
|
387
|
+
this._updateHashSets();
|
|
388
|
+
return !this._wallsSet.has(Vector2Utils.toHash(node));
|
|
389
|
+
};
|
|
390
|
+
AstarGridGraph.prototype.search = function (start, goal) {
|
|
391
|
+
return AStarPathfinder.hasPath(this, start, goal);
|
|
392
|
+
};
|
|
393
|
+
AstarGridGraph.prototype.searchPath = function (start, goal) {
|
|
394
|
+
return AStarPathfinder.searchPath(this, start, goal);
|
|
395
|
+
};
|
|
396
|
+
AstarGridGraph.prototype.getNeighbors = function (node) {
|
|
397
|
+
this._neighbors.length = 0;
|
|
398
|
+
for (var _i = 0, _a = this.dirs; _i < _a.length; _i++) {
|
|
399
|
+
var dir = _a[_i];
|
|
400
|
+
var next = Vector2Utils.add(node, dir);
|
|
401
|
+
if (this.isNodeInBounds(next) && this.isNodePassable(next)) {
|
|
402
|
+
this._neighbors.push(next);
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
return this._neighbors;
|
|
406
|
+
};
|
|
407
|
+
AstarGridGraph.prototype.cost = function (from, to) {
|
|
408
|
+
this._updateHashSets();
|
|
409
|
+
return this._weightedNodesSet.has(Vector2Utils.toHash(to)) ? this.weightedNodeWeight : this.defaultWeight;
|
|
410
|
+
};
|
|
411
|
+
AstarGridGraph.prototype.heuristic = function (node, goal) {
|
|
412
|
+
return Vector2Utils.manhattanDistance(node, goal);
|
|
413
|
+
};
|
|
414
|
+
AstarGridGraph.prototype.getStats = function () {
|
|
415
|
+
this._updateHashSets();
|
|
416
|
+
return {
|
|
417
|
+
walls: this.walls.length,
|
|
418
|
+
weightedNodes: this.weightedNodes.length,
|
|
419
|
+
gridSize: "".concat(this._width, "x").concat(this._height),
|
|
420
|
+
wallsSetSize: this._wallsSet.size,
|
|
421
|
+
weightedNodesSetSize: this._weightedNodesSet.size
|
|
422
|
+
};
|
|
423
|
+
};
|
|
424
|
+
return AstarGridGraph;
|
|
425
|
+
}());
|
|
426
|
+
export { AstarGridGraph };
|
|
427
|
+
|
|
428
|
+
export {};
|
|
429
|
+
|
|
430
|
+
import { Vector2Utils } from '../../../Types/IVector2';
|
|
431
|
+
var BreadthFirstPathfinder = (function () {
|
|
432
|
+
function BreadthFirstPathfinder() {
|
|
433
|
+
}
|
|
434
|
+
BreadthFirstPathfinder.search = function (graph, start, goal, cameFrom) {
|
|
435
|
+
var frontier = [];
|
|
436
|
+
var visited = new Set();
|
|
437
|
+
var pathMap = cameFrom || new Map();
|
|
438
|
+
var startHash = Vector2Utils.toHash(start);
|
|
439
|
+
var goalHash = Vector2Utils.toHash(goal);
|
|
440
|
+
if (startHash === goalHash) {
|
|
441
|
+
return true;
|
|
442
|
+
}
|
|
443
|
+
frontier.push(start);
|
|
444
|
+
visited.add(startHash);
|
|
445
|
+
while (frontier.length > 0) {
|
|
446
|
+
var current = frontier.shift();
|
|
447
|
+
var currentHash = Vector2Utils.toHash(current);
|
|
448
|
+
if (currentHash === goalHash) {
|
|
449
|
+
return true;
|
|
450
|
+
}
|
|
451
|
+
for (var _i = 0, _a = graph.getNeighbors(current); _i < _a.length; _i++) {
|
|
452
|
+
var neighbor = _a[_i];
|
|
453
|
+
var neighborHash = Vector2Utils.toHash(neighbor);
|
|
454
|
+
if (visited.has(neighborHash)) {
|
|
455
|
+
continue;
|
|
456
|
+
}
|
|
457
|
+
visited.add(neighborHash);
|
|
458
|
+
pathMap.set(neighborHash, current);
|
|
459
|
+
frontier.push(neighbor);
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
return false;
|
|
463
|
+
};
|
|
464
|
+
BreadthFirstPathfinder.searchPath = function (graph, start, goal) {
|
|
465
|
+
var cameFrom = new Map();
|
|
466
|
+
if (this.search(graph, start, goal, cameFrom)) {
|
|
467
|
+
return this.reconstructPath(cameFrom, start, goal);
|
|
468
|
+
}
|
|
469
|
+
return [];
|
|
470
|
+
};
|
|
471
|
+
BreadthFirstPathfinder.reconstructPath = function (cameFrom, start, goal) {
|
|
472
|
+
var path = [];
|
|
473
|
+
var current = goal;
|
|
474
|
+
var startHash = Vector2Utils.toHash(start);
|
|
475
|
+
while (Vector2Utils.toHash(current) !== startHash) {
|
|
476
|
+
path.unshift(current);
|
|
477
|
+
var currentHash = Vector2Utils.toHash(current);
|
|
478
|
+
var parent_1 = cameFrom.get(currentHash);
|
|
479
|
+
if (!parent_1)
|
|
480
|
+
break;
|
|
481
|
+
current = parent_1;
|
|
482
|
+
}
|
|
483
|
+
path.unshift(start);
|
|
484
|
+
return path;
|
|
485
|
+
};
|
|
486
|
+
return BreadthFirstPathfinder;
|
|
487
|
+
}());
|
|
488
|
+
export { BreadthFirstPathfinder };
|
|
489
|
+
|
|
490
|
+
export {};
|
|
491
|
+
|
|
492
|
+
var UnweightedGraph = (function () {
|
|
493
|
+
function UnweightedGraph() {
|
|
494
|
+
this.edges = new Map();
|
|
495
|
+
}
|
|
496
|
+
UnweightedGraph.prototype.addEdgesForNode = function (node, neighbors) {
|
|
497
|
+
this.edges.set(node, neighbors);
|
|
498
|
+
return this;
|
|
499
|
+
};
|
|
500
|
+
UnweightedGraph.prototype.getNeighbors = function (node) {
|
|
501
|
+
return this.edges.get(node) || [];
|
|
502
|
+
};
|
|
503
|
+
return UnweightedGraph;
|
|
504
|
+
}());
|
|
505
|
+
export { UnweightedGraph };
|
|
506
|
+
|
|
507
|
+
import { Vector2Utils } from '../../../Types/IVector2';
|
|
508
|
+
import { BreadthFirstPathfinder } from './BreadthFirstPathfinder';
|
|
509
|
+
var UnweightedGridGraph = (function () {
|
|
510
|
+
function UnweightedGridGraph(width, height, allowDiagonalSearch) {
|
|
511
|
+
if (allowDiagonalSearch === void 0) { allowDiagonalSearch = false; }
|
|
512
|
+
this.walls = [];
|
|
513
|
+
this._neighbors = [];
|
|
514
|
+
this._width = width;
|
|
515
|
+
this._height = height;
|
|
516
|
+
this._dirs = allowDiagonalSearch ? UnweightedGridGraph.COMPASS_DIRS : UnweightedGridGraph.CARDINAL_DIRS;
|
|
517
|
+
}
|
|
518
|
+
UnweightedGridGraph.prototype.isNodeInBounds = function (node) {
|
|
519
|
+
return 0 <= node.x && node.x < this._width && 0 <= node.y && node.y < this._height;
|
|
520
|
+
};
|
|
521
|
+
UnweightedGridGraph.prototype.isNodePassable = function (node) {
|
|
522
|
+
return !this.walls.find(function (wall) { return Vector2Utils.equals(wall, node); });
|
|
523
|
+
};
|
|
524
|
+
UnweightedGridGraph.prototype.getNeighbors = function (node) {
|
|
525
|
+
this._neighbors.length = 0;
|
|
526
|
+
for (var _i = 0, _a = this._dirs; _i < _a.length; _i++) {
|
|
527
|
+
var dir = _a[_i];
|
|
528
|
+
var next = Vector2Utils.add(node, dir);
|
|
529
|
+
if (this.isNodeInBounds(next) && this.isNodePassable(next)) {
|
|
530
|
+
this._neighbors.push(next);
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
return this._neighbors;
|
|
534
|
+
};
|
|
535
|
+
UnweightedGridGraph.prototype.searchPath = function (start, goal) {
|
|
536
|
+
return BreadthFirstPathfinder.searchPath(this, start, goal);
|
|
537
|
+
};
|
|
538
|
+
UnweightedGridGraph.prototype.hasPath = function (start, goal) {
|
|
539
|
+
return BreadthFirstPathfinder.search(this, start, goal);
|
|
540
|
+
};
|
|
541
|
+
UnweightedGridGraph.CARDINAL_DIRS = [
|
|
542
|
+
Vector2Utils.create(1, 0),
|
|
543
|
+
Vector2Utils.create(0, -1),
|
|
544
|
+
Vector2Utils.create(-1, 0),
|
|
545
|
+
Vector2Utils.create(0, 1)
|
|
546
|
+
];
|
|
547
|
+
UnweightedGridGraph.COMPASS_DIRS = [
|
|
548
|
+
Vector2Utils.create(1, 0),
|
|
549
|
+
Vector2Utils.create(1, -1),
|
|
550
|
+
Vector2Utils.create(0, -1),
|
|
551
|
+
Vector2Utils.create(-1, -1),
|
|
552
|
+
Vector2Utils.create(-1, 0),
|
|
553
|
+
Vector2Utils.create(-1, 1),
|
|
554
|
+
Vector2Utils.create(0, 1),
|
|
555
|
+
Vector2Utils.create(1, 1),
|
|
556
|
+
];
|
|
557
|
+
return UnweightedGridGraph;
|
|
558
|
+
}());
|
|
559
|
+
export { UnweightedGridGraph };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export{Vector2Utils}from"./Types/IVector2";export{PriorityQueue}from"./Utils/PriorityQueue";export{AStarPathfinder}from"./AI/Pathfinding/AStar/AStarPathfinder";export{AstarGridGraph}from"./AI/Pathfinding/AStar/AstarGridGraph";export{BreadthFirstPathfinder}from"./AI/Pathfinding/BreadthFirst/BreadthFirstPathfinder";export{UnweightedGraph}from"./AI/Pathfinding/BreadthFirst/UnweightedGraph";export{UnweightedGridGraph}from"./AI/Pathfinding/BreadthFirst/UnweightedGridGraph";var t=function(){function t(){}return t.equals=function(t,e){return t.equals?t.equals(e):t.x===e.x&&t.y===e.y},t.create=function(t,e){return{x:t,y:e}},t.clone=function(t){return{x:t.x,y:t.y}},t.add=function(t,e){return{x:t.x+e.x,y:t.y+e.y}},t.manhattanDistance=function(t,e){return Math.abs(t.x-e.x)+Math.abs(t.y-e.y)},t.distance=function(t,e){var i=t.x-e.x,r=t.y-e.y;return Math.sqrt(i*i+r*r)},t.toHash=function(t){return(t.x+this.MAX_COORD|0)<<16|(t.y+this.MAX_COORD|0)},t.toKey=function(t){return"".concat(t.x,",").concat(t.y)},t.fromHash=function(t){return{x:(t>>16)-this.MAX_COORD,y:(65535&t)-this.MAX_COORD}},t.HASH_MULTIPLIER=73856093,t.MAX_COORD=32767,t}();export{t as Vector2Utils};var e=function(){function t(){this._heap=[],this._size=0}return Object.defineProperty(t.prototype,"size",{get:function(){return this._size},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"isEmpty",{get:function(){return 0===this._size},enumerable:!1,configurable:!0}),t.prototype.clear=function(){this._heap.length=0,this._size=0},t.prototype.enqueue=function(t){this._heap[this._size]=t,this._bubbleUp(this._size),this._size++},t.prototype.dequeue=function(){if(0!==this._size){var t=this._heap[0];return this._size--,this._size>0&&(this._heap[0]=this._heap[this._size],this._bubbleDown(0)),t}},t.prototype.peek=function(){return this._size>0?this._heap[0]:void 0},t.prototype._bubbleUp=function(t){for(;t>0;){var e=Math.floor((t-1)/2);if(this._heap[t].priority>=this._heap[e].priority)break;this._swap(t,e),t=e}},t.prototype._bubbleDown=function(t){for(;;){var e=t,i=2*t+1,r=2*t+2;if(i<this._size&&this._heap[i].priority<this._heap[e].priority&&(e=i),r<this._size&&this._heap[r].priority<this._heap[e].priority&&(e=r),e===t)break;this._swap(t,e),t=e}},t.prototype._swap=function(t,e){var i=this._heap[t];this._heap[t]=this._heap[e],this._heap[e]=i},t}();export{e as PriorityQueue};var i=this&&this.__spreadArray||function(t,e,i){if(i||2===arguments.length)for(var r,s=0,o=e.length;s<o;s++)!r&&s in e||(r||(r=Array.prototype.slice.call(e,0,s)),r[s]=e[s]);return t.concat(r||Array.prototype.slice.call(e))};import{Vector2Utils as t}from"../../../Types/IVector2";import{PriorityQueue as e}from"../../../Utils/PriorityQueue";var r=function(){function e(e,i,r,s){void 0===i&&(i=0),void 0===r&&(r=0),void 0===s&&(s=null),this.priority=0,this.gCost=0,this.hCost=0,this.parent=null,this.hash=0,this.node=e,this.gCost=i,this.hCost=r,this.priority=i+r,this.parent=s,this.hash=t.toHash(e)}return e.prototype.updateCosts=function(t,e,i){void 0===i&&(i=null),this.gCost=t,this.hCost=e,this.priority=t+e,this.parent=i},e.prototype.updateNode=function(e,i,r,s){void 0===i&&(i=0),void 0===r&&(r=0),void 0===s&&(s=null),this.node=e,this.gCost=i,this.hCost=r,this.priority=i+r,this.parent=s,this.hash=t.toHash(e)},e.prototype.reset=function(){this.node=null,this.priority=0,this.gCost=0,this.hCost=0,this.parent=null,this.hash=0},e}(),s=function(){function s(){}return s._getNode=function(t,e,i,s){void 0===e&&(e=0),void 0===i&&(i=0),void 0===s&&(s=null);var o=this._nodePool.pop();return o?o.updateNode(t,e,i,s):o=new r(t,e,i,s),o},s._recycleNode=function(t){this._nodePool.length<1e3&&(t.reset(),this._nodePool.push(t))},s.search=function(i,r,s){var o=new e,h=new Set,n=new Map,a=t.toHash(r),u=t.toHash(s);if(a===u)return{found:!0,goalNode:this._getNode(r,0,0)};var d,p=this._getNode(r,0,i.heuristic(r,s));for(o.enqueue(p),n.set(a,p);!o.isEmpty;){var c=o.dequeue(),l=c.hash;if(n.delete(l),l===u){d=c;break}h.add(l);for(var f=0,_=i.getNeighbors(c.node);f<_.length;f++){var g=_[f],y=t.toHash(g);if(!h.has(y)){var w=c.gCost+i.cost(c.node,g),N=n.get(y);if(N){if(w<N.gCost){var v=N.hCost;N.updateCosts(w,v,c)}}else{v=i.heuristic(g,s);var P=this._getNode(g,w,v,c);o.enqueue(P),n.set(y,P)}}}c!==d&&this._recycleNode(c)}for(;!o.isEmpty;){var S=o.dequeue();S!==d&&this._recycleNode(S)}return{found:!!d,goalNode:d}},s.searchPath=function(t,e,i){var r=this.search(t,e,i);return r.found&&r.goalNode?this.reconstructPathFromNode(r.goalNode,e):[]},s.reconstructPathFromNode=function(e,r){this._tempPath.length=0;for(var s=e,o=t.toHash(r);s;){this._tempPath.unshift(s.node);var h=s.hash,n=s.parent;h!==o&&this._recycleNode(s),s=n}return i([],this._tempPath,!0)},s.hasPath=function(t,e,i){var r=this.search(t,e,i);return r.goalNode&&this._recycleNode(r.goalNode),r.found},s.clearPool=function(){this._nodePool.length=0,this._tempPath.length=0},s.getPoolStats=function(){return{poolSize:this._nodePool.length,maxPoolSize:1e3}},s._nodePool=[],s._tempPath=[],s}();export{s as AStarPathfinder};import{Vector2Utils as t}from"../../../Types/IVector2";import{AStarPathfinder as s}from"./AStarPathfinder";var o=function(){function e(e,i){this.dirs=[t.create(1,0),t.create(0,-1),t.create(-1,0),t.create(0,1)],this.walls=[],this.weightedNodes=[],this.defaultWeight=1,this.weightedNodeWeight=5,this._neighbors=new Array(4),this._wallsSet=new Set,this._weightedNodesSet=new Set,this._wallsDirty=!0,this._weightedNodesDirty=!0,this._width=e,this._height=i}return e.prototype.addWall=function(t){this.walls.push(t),this._wallsDirty=!0},e.prototype.addWalls=function(t){var e;(e=this.walls).push.apply(e,t),this._wallsDirty=!0},e.prototype.clearWalls=function(){this.walls.length=0,this._wallsSet.clear(),this._wallsDirty=!1},e.prototype.addWeightedNode=function(t){this.weightedNodes.push(t),this._weightedNodesDirty=!0},e.prototype.addWeightedNodes=function(t){var e;(e=this.weightedNodes).push.apply(e,t),this._weightedNodesDirty=!0},e.prototype.clearWeightedNodes=function(){this.weightedNodes.length=0,this._weightedNodesSet.clear(),this._weightedNodesDirty=!1},e.prototype._updateHashSets=function(){if(this._wallsDirty){this._wallsSet.clear();for(var e=0,i=this.walls;e<i.length;e++){var r=i[e];this._wallsSet.add(t.toHash(r))}this._wallsDirty=!1}if(this._weightedNodesDirty){this._weightedNodesSet.clear();for(var s=0,o=this.weightedNodes;s<o.length;s++){var h=o[s];this._weightedNodesSet.add(t.toHash(h))}this._weightedNodesDirty=!1}},e.prototype.isNodeInBounds=function(t){return 0<=t.x&&t.x<this._width&&0<=t.y&&t.y<this._height},e.prototype.isNodePassable=function(e){return this._updateHashSets(),!this._wallsSet.has(t.toHash(e))},e.prototype.search=function(t,e){return s.hasPath(this,t,e)},e.prototype.searchPath=function(t,e){return s.searchPath(this,t,e)},e.prototype.getNeighbors=function(e){this._neighbors.length=0;for(var i=0,r=this.dirs;i<r.length;i++){var s=r[i],o=t.add(e,s);this.isNodeInBounds(o)&&this.isNodePassable(o)&&this._neighbors.push(o)}return this._neighbors},e.prototype.cost=function(e,i){return this._updateHashSets(),this._weightedNodesSet.has(t.toHash(i))?this.weightedNodeWeight:this.defaultWeight},e.prototype.heuristic=function(e,i){return t.manhattanDistance(e,i)},e.prototype.getStats=function(){return this._updateHashSets(),{walls:this.walls.length,weightedNodes:this.weightedNodes.length,gridSize:"".concat(this._width,"x").concat(this._height),wallsSetSize:this._wallsSet.size,weightedNodesSetSize:this._weightedNodesSet.size}},e}();export{o as AstarGridGraph};export{};import{Vector2Utils as t}from"../../../Types/IVector2";var h=function(){function e(){}return e.search=function(e,i,r,s){var o=[],h=new Set,n=s||new Map,a=t.toHash(i),u=t.toHash(r);if(a===u)return!0;for(o.push(i),h.add(a);o.length>0;){var d=o.shift();if(t.toHash(d)===u)return!0;for(var p=0,c=e.getNeighbors(d);p<c.length;p++){var l=c[p],f=t.toHash(l);h.has(f)||(h.add(f),n.set(f,d),o.push(l))}}return!1},e.searchPath=function(t,e,i){var r=new Map;return this.search(t,e,i,r)?this.reconstructPath(r,e,i):[]},e.reconstructPath=function(e,i,r){for(var s=[],o=r,h=t.toHash(i);t.toHash(o)!==h;){s.unshift(o);var n=t.toHash(o),a=e.get(n);if(!a)break;o=a}return s.unshift(i),s},e}();export{h as BreadthFirstPathfinder};export{};var n=function(){function t(){this.edges=new Map}return t.prototype.addEdgesForNode=function(t,e){return this.edges.set(t,e),this},t.prototype.getNeighbors=function(t){return this.edges.get(t)||[]},t}();export{n as UnweightedGraph};import{Vector2Utils as t}from"../../../Types/IVector2";import{BreadthFirstPathfinder as h}from"./BreadthFirstPathfinder";var a=function(){function e(t,i,r){void 0===r&&(r=!1),this.walls=[],this._neighbors=[],this._width=t,this._height=i,this._dirs=r?e.COMPASS_DIRS:e.CARDINAL_DIRS}return e.prototype.isNodeInBounds=function(t){return 0<=t.x&&t.x<this._width&&0<=t.y&&t.y<this._height},e.prototype.isNodePassable=function(e){return!this.walls.find((function(i){return t.equals(i,e)}))},e.prototype.getNeighbors=function(e){this._neighbors.length=0;for(var i=0,r=this._dirs;i<r.length;i++){var s=r[i],o=t.add(e,s);this.isNodeInBounds(o)&&this.isNodePassable(o)&&this._neighbors.push(o)}return this._neighbors},e.prototype.searchPath=function(t,e){return h.searchPath(this,t,e)},e.prototype.hasPath=function(t,e){return h.search(this,t,e)},e.CARDINAL_DIRS=[t.create(1,0),t.create(0,-1),t.create(-1,0),t.create(0,1)],e.COMPASS_DIRS=[t.create(1,0),t.create(1,-1),t.create(0,-1),t.create(-1,-1),t.create(-1,0),t.create(-1,1),t.create(0,1),t.create(1,1)],e}();export{a as UnweightedGridGraph};
|
package/bin/package.json
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@esengine/pathfinding",
|
|
3
|
+
"version": "1.0.1",
|
|
4
|
+
"description": "寻路算法库,支持A*、广度优先等算法,适用于Cocos Creator、Laya等游戏引擎",
|
|
5
|
+
"main": "bin/index.js",
|
|
6
|
+
"types": "bin/index.d.ts",
|
|
7
|
+
"keywords": [
|
|
8
|
+
"pathfinding",
|
|
9
|
+
"astar",
|
|
10
|
+
"a-star",
|
|
11
|
+
"algorithm",
|
|
12
|
+
"game",
|
|
13
|
+
"cocos",
|
|
14
|
+
"laya",
|
|
15
|
+
"typescript",
|
|
16
|
+
"navigation",
|
|
17
|
+
"grid"
|
|
18
|
+
],
|
|
19
|
+
"directories": {
|
|
20
|
+
"lib": "lib"
|
|
21
|
+
},
|
|
22
|
+
"scripts": {
|
|
23
|
+
"test": "mocha --recursive --reporter tap --growl",
|
|
24
|
+
"eslint": "eslint src --ext .ts",
|
|
25
|
+
"build": "gulp build",
|
|
26
|
+
"dev": "gulp buildJs"
|
|
27
|
+
},
|
|
28
|
+
"author": "yhh",
|
|
29
|
+
"license": "MIT",
|
|
30
|
+
"devDependencies": {
|
|
31
|
+
"@babel/core": "^7.23.0",
|
|
32
|
+
"@babel/preset-env": "^7.23.0",
|
|
33
|
+
"browserify": "^17.0.0",
|
|
34
|
+
"del": "^8.0.0",
|
|
35
|
+
"gulp": "^5.0.0",
|
|
36
|
+
"gulp-babel": "^8.0.0",
|
|
37
|
+
"gulp-concat": "^2.6.1",
|
|
38
|
+
"gulp-inject-string": "^1.1.2",
|
|
39
|
+
"gulp-string-replace": "^1.1.2",
|
|
40
|
+
"gulp-terser": "^2.1.0",
|
|
41
|
+
"gulp-typescript": "^6.0.0-alpha.1",
|
|
42
|
+
"tsify": "^5.0.4",
|
|
43
|
+
"typedoc": "^0.25.0",
|
|
44
|
+
"typescript": "^4.9.5",
|
|
45
|
+
"vinyl-source-stream": "^2.0.0",
|
|
46
|
+
"watchify": "^4.0.0"
|
|
47
|
+
},
|
|
48
|
+
"publishConfig": {
|
|
49
|
+
"registry": "https://npm.pkg.github.com/359807859@qq.com"
|
|
50
|
+
},
|
|
51
|
+
"dependencies": {
|
|
52
|
+
"@esengine/ecs-framework": "^2.0.5"
|
|
53
|
+
}
|
|
54
|
+
}
|