astarjs
Version:
Pathfinding algorithm library.
1 lines • 6.59 kB
JavaScript
var Pathfinding;(()=>{"use strict";var t={738:(t,e)=>{Object.defineProperty(e,"__esModule",{value:!0}),e.Node=void 0;var n=function(){function t(t,e,n){void 0===n&&(n=0),this._row=t,this._col=e,this._weight=n,this._h=0,this._g=0,this._parent=null}return t.prototype.getRow=function(){return this._row},t.prototype.getCol=function(){return this._col},t.prototype.getWeight=function(){return this._weight},t.prototype.setH=function(t){this._h=t},t.prototype.getH=function(){return this._h},t.prototype.setG=function(t){this._g=t},t.prototype.getG=function(){return this._g+(this._parent?this._parent.getG():0)},t.prototype.getValue=function(){return this.getH()+this.getG()},t.prototype.setParent=function(t){this._parent=t},t.prototype.getParent=function(){return this._parent},t}();e.Node=n},784:(t,e,n)=>{Object.defineProperty(e,"__esModule",{value:!0}),e.PathFinding=e.Heuristic=e.Types=void 0;var i,r,o=n(738);!function(t){t.START="s",t.END="e",t.WALKABLE="w",t.NON_WALKABLE="nw"}(i||(e.Types=i={})),function(t){t[t.MANHATTAN=0]="MANHATTAN",t[t.DIAGONAL=1]="DIAGONAL"}(r||(e.Heuristic=r={}));var s=function(){function t(t){this.DEFAULT_DISTANCE=1,this.DIAGONAL_DISTANCE=Math.sqrt(2),this.heuristic=r.MANHATTAN,this.allowDiagonal=!1,this.walkableTypes=[],t&&t.heuristic&&(this.heuristic=t.heuristic,this.allowDiagonal=t.allowDiagonal||!1)}return t.prototype.setWalkable=function(){for(var t,e=this,n=[],i=0;i<arguments.length;i++)n[i]=arguments[i];return this.walkableTypes=(t=this.walkableTypes).concat.apply(t,n).map((function(t){return e.isNumber(t)?{type:t,weight:0}:{type:t.type,weight:t.weight?t.weight:0}})),this},t.prototype.setStart=function(t){return this.start=t,this},t.prototype.setEnd=function(t){return this.end=t,this},t.prototype.gameMapToPathfind=function(t){var e=this,n="number"==typeof this.start,r="number"==typeof this.end;return t.map((function(t,o){return t.map((function(t,s){if(n&&e.start==t||!n&&e.start.row==o&&e.start.col==s)return i.START;if(r&&e.end==t||!r&&e.end.row==o&&e.end.col==s)return i.END;if(e.isTileWalkable(t)){var u=e.getTileWalkable(t);return u.weight?u.weight:0}return i.NON_WALKABLE}))}))},t.prototype.isTileWalkable=function(t){var e=this;return this.walkableTypes.some((function(n){return e.isNumber(n)?n==t:n.type==t}))},t.prototype.getTileWalkable=function(t){var e=this;return this.walkableTypes.filter((function(n){return e.isNumber(n)?n===t:n.type===t}))[0]},t.prototype.find=function(t){if(null==this.start||null==this.start)throw new Error("There is no start point. Please, use setStart() to configure the path's start point.");if(null==this.end||null==this.end)throw new Error("There is no end point. Please, use setEnd() to configure the path's end point.");var e=this.gameMapToPathfind(t),n="number"!=typeof this.start?new o.Node(this.start.row,this.start.col,0):this.findStartElement(e),i="number"!=typeof this.end?new o.Node(this.end.row,this.end.col,0):this.findEndElement(e);return this.findBestPath(n,i,e)},t.prototype.findStartElement=function(t){var e=this.findElement(t,i.START);if(null==e)throw new Error("Couldn't find a start point.");return e},t.prototype.findEndElement=function(t){var e=this.findElement(t,i.END);if(null==e)throw new Error("Couldn't find a end point.");return e},t.prototype.findElement=function(t,e){var n=null;return t.forEach((function(t,i){t.forEach((function(t,r){t==e&&(n=new o.Node(i,r,0))}))})),n},t.prototype.findBestPath=function(t,e,n){var i,r=this,o=[],s=[],u=!1,h=t;for(o.push(t);!u;)this.updateLists(n,h,o,s,e),s.length>0&&o.push(s.pop()),h=o[o.length-1],i=this.findAdjacents(n,h).filter((function(t){return r.elementNotExistsInside(s,t)&&r.elementNotExistsInside(o,t)})),u=this.isNodeEqual(o[o.length-1],e)||0==s.length&&!i.length;return s.length>0?this.getPath(o[o.length-1]):[]},t.prototype.updateLists=function(t,e,n,i,r){var o=this,s=this.findAdjacents(t,e).filter((function(t){return o.elementNotExistsInside(n,t)}));s.filter((function(t){return o.elementExistsInside(i,t)})).forEach((function(t){var n=i.filter((function(e){return o.isNodeEqual(e,t)}))[0];e.getG()+o.getMoveValue(n,e)<n.getG()&&(n.setG(o.getMoveValue(n,e)),n.setParent(e))})),s.filter((function(t){return o.elementNotExistsInside(i,t)})).forEach((function(t){t.setParent(e),t.setH(o.distanceBetweenNodes(t,r)),t.setG(o.getMoveValue(e,t)),i.push(t)})),i.sort((function(t,e){return e.getValue()-t.getValue()}))},t.prototype.elementNotExistsInside=function(t,e){return!this.elementExistsInside(t,e)},t.prototype.elementExistsInside=function(t,e){var n=this;return t.some((function(t){return n.isNodeEqual(t,e)}))},t.prototype.distanceBetweenNodes=function(t,e){var n=Math.abs(e.getCol()-t.getCol()),i=Math.abs(e.getRow()-t.getRow());return this.heuristic===r.MANHATTAN?this.DEFAULT_DISTANCE*(n+i):this.DEFAULT_DISTANCE*(n+i)+(this.DIAGONAL_DISTANCE-2*this.DEFAULT_DISTANCE)*Math.min(n,i)},t.prototype.getMoveValue=function(t,e){return t.getRow()!=e.getRow()&&t.getCol()!=e.getCol()?this.DIAGONAL_DISTANCE*(1+e.getWeight()):this.DEFAULT_DISTANCE*(1+e.getWeight())},t.prototype.findAdjacents=function(t,e){for(var n,s=[],u=[[-1,-1],[-1,1],[1,-1],[1,1]],h=[[-1,0],[0,-1],[0,1],[1,0]],a=t,l=0,p=0;p<h.length;p++)n=e.getRow()+h[p][0],l=e.getCol()+h[p][1],n>-1&&l>-1&&n<a.length&&l<a[n].length&&(this.isNumber(a[n][l])||a[n][l]==i.END)&&s.push(new o.Node(n,l,a[n][l]));var f=!1;if(this.heuristic===r.DIAGONAL)for(p=0;p<u.length;p++)if(n=e.getRow()+u[p][0],l=e.getCol()+u[p][1],n>-1&&l>-1&&n<a.length&&l<a[n].length&&(this.isNumber(a[n][l])||a[n][l]==i.END))if(this.allowDiagonal)s.push(new o.Node(n,l,a[n][l]));else{f=!1;for(var c=0;c<u.length;c++)c==p&&this.isNumber(a[n-u[p][0]][l])&&this.isNumber(a[n][l-u[p][1]])&&(f=!0);f&&s.push(new o.Node(n,l,a[n][l]))}return s},t.prototype.isNumber=function(t){return Object.prototype.toString.apply(t).indexOf("Number")>-1},t.prototype.getPath=function(t){for(var e=t,n=[];e;)n.push(this.nodeToObject(e)),e=e.getParent();return n.reverse()},t.prototype.nodeToObject=function(t){return{col:t.getCol(),row:t.getRow()}},t.prototype.isNodeEqual=function(t,e){return t.getRow()==e.getRow()&&t.getCol()==e.getCol()},t}();e.PathFinding=s}},e={};function n(i){var r=e[i];if(void 0!==r)return r.exports;var o=e[i]={exports:{}};return t[i](o,o.exports,n),o.exports}var i={};(()=>{var t=i;Object.defineProperty(t,"__esModule",{value:!0}),t.Heuristic=t.PathFinding=void 0;var e=n(784);Object.defineProperty(t,"PathFinding",{enumerable:!0,get:function(){return e.PathFinding}}),Object.defineProperty(t,"Heuristic",{enumerable:!0,get:function(){return e.Heuristic}})})(),Pathfinding=i})();