UNPKG

@esengine/pathfinding

Version:

寻路算法库,支持A*、广度优先等算法,适用于Cocos Creator、Laya等游戏引擎

1 lines 9.6 kB
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};