-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheasystar-c4a409057f.min.js
More file actions
1 lines (1 loc) · 5.42 KB
/
easystar-c4a409057f.min.js
File metadata and controls
1 lines (1 loc) · 5.42 KB
1
var EasyStar=EasyStar||{};"function"==typeof define&&define.amd&&define("easystar",[],(function(){return EasyStar})),"undefined"!=typeof module&&module.exports&&(module.exports=EasyStar),EasyStar.Node=function(t,i,n,e,s){this.parent=t,this.x=i,this.y=n,this.costSoFar=e,this.simpleDistanceToTarget=s,this.bestGuessDistance=function(){return this.costSoFar+this.simpleDistanceToTarget}},EasyStar.Node.OPEN_LIST=0,EasyStar.Node.CLOSED_LIST=1,EasyStar.PriorityQueue=function(t,i){this.length=0;var n=[],e=!1;if(i==EasyStar.PriorityQueue.MAX_HEAP)e=!0;else{if(i!=EasyStar.PriorityQueue.MIN_HEAP)throw i+" not supported.";e=!1}this.insert=function(i){if(!i.hasOwnProperty(t))throw"Cannot insert "+i+" because it does not have a property by the name of "+t+".";n.push(i),this.length++,s(this.length-1)},this.getHighestPriorityElement=function(){return n[0]},this.shiftHighestPriorityElement=function(){if(0===this.length)throw"There are no more elements in your priority queue.";if(1===this.length){var t=n[0];return n=[],this.length=0,t}var i=n[0],e=n.pop();return this.length--,n[0]=e,a(0),i};var s=function(t){if(0!==t){var i=u(t);r(t,i)&&(o(t,i),s(i))}},a=function(t){var i=l(t),n=h(t);if(r(i,t))o(t,i),a(i);else if(r(n,t))o(t,n),a(n);else{if(0==t)return;a(0)}},o=function(t,i){var e=n[t];n[t]=n[i],n[i]=e},r=function(i,s){return void 0!==n[s]&&void 0!==n[i]&&("function"==typeof n[i][t]?(a=n[i][t](),o=n[s][t]()):(a=n[i][t],o=n[s][t]),e?a>o:o>a);var a,o},u=function(t){return Math.floor(t/2)-1},l=function(t){return 2*t+1},h=function(t){return 2*t+2}},EasyStar.PriorityQueue.MAX_HEAP=0,EasyStar.PriorityQueue.MIN_HEAP=1,EasyStar.instance=function(){this.isDoneCalculating=!0,this.pointsToAvoid={},this.startX,this.callback,this.startY,this.endX,this.endY,this.nodeHash={},this.openList},EasyStar.js=function(){var t,i,n,e=10,s={},a={},o=!0,r=[],u=Number.MAX_VALUE,l=!1;this.setAcceptableTiles=function(t){t instanceof Array?n=t:!isNaN(parseFloat(t))&&isFinite(t)&&(n=[t])},this.enableDiagonals=function(){l=!0},this.disableDiagonals=function(){l=!1},this.setGrid=function(i){t=i;for(var n=0;n<t.length;n++)for(var e=0;e<t[0].length;e++)a[t[n][e]]||(a[t[n][e]]=1)},this.setTileCost=function(t,i){a[t]=i},this.setIterationsPerCalculation=function(t){u=t},this.avoidAdditionalPoint=function(t,i){s[t+"_"+i]=1},this.stopAvoidingAdditionalPoint=function(t,i){delete s[t+"_"+i]},this.enableCornerCutting=function(){o=!0},this.disableCornerCutting=function(){o=!1},this.stopAvoidingAllAdditionalPoints=function(){s={}},this.findPath=function(i,s,a,o,u){if(void 0===n)throw new Error("You can't set a path without first calling setAcceptableTiles() on EasyStar.");if(void 0===t)throw new Error("You can't set a path without first calling setGrid() on EasyStar.");if(0>i||0>s||0>a||0>a||i>t[0].length-1||s>t.length-1||a>t[0].length-1||o>t.length-1)throw new Error("Your start or end point is outside the scope of your grid.");if(i!==a||s!==o){for(var l=t[o][a],h=!1,f=0;f<n.length;f++)if(l===n[f]){h=!0;break}if(!1!==h){var y=new EasyStar.instance;y.openList=new EasyStar.PriorityQueue("bestGuessDistance",EasyStar.PriorityQueue.MIN_HEAP),y.isDoneCalculating=!1,y.nodeHash={},y.startX=i,y.startY=s,y.endX=a,y.endY=o,y.callback=u,y.openList.insert(c(y,y.startX,y.startY,null,e)),r.push(y)}else setTimeout((function(){u(null)}))}else setTimeout((function(){u([])}))},this.calculate=function(){if(0!==r.length&&void 0!==t&&void 0!==n)for(i=0;u>i;i++){if(0===r.length)return;if(0!==r[0].openList.length){var s=r[0].openList.shiftHighestPriorityElement();if(s.list=EasyStar.Node.CLOSED_LIST,s.y>0&&(h(r[0],s,0,-1,e*a[t[s.y-1][s.x]]),!0===r[0].isDoneCalculating))r.shift();else if(s.x<t[0].length-1&&(h(r[0],s,1,0,e*a[t[s.y][s.x+1]]),!0===r[0].isDoneCalculating))r.shift();else if(s.y<t.length-1&&(h(r[0],s,0,1,e*a[t[s.y+1][s.x]]),!0===r[0].isDoneCalculating))r.shift();else if(s.x>0&&(h(r[0],s,-1,0,e*a[t[s.y][s.x-1]]),!0===r[0].isDoneCalculating))r.shift();else if(l){if(s.x>0&&s.y>0&&(o||f(t,n,s.x,s.y-1)&&f(t,n,s.x-1,s.y))&&(h(r[0],s,-1,-1,14*a[t[s.y-1][s.x-1]]),!0===r[0].isDoneCalculating)){r.shift();continue}if(s.x<t[0].length-1&&s.y<t.length-1&&(o||f(t,n,s.x,s.y+1)&&f(t,n,s.x+1,s.y))&&(h(r[0],s,1,1,14*a[t[s.y+1][s.x+1]]),!0===r[0].isDoneCalculating)){r.shift();continue}if(s.x<t[0].length-1&&s.y>0&&(o||f(t,n,s.x,s.y-1)&&f(t,n,s.x+1,s.y))&&(h(r[0],s,1,-1,14*a[t[s.y-1][s.x+1]]),!0===r[0].isDoneCalculating)){r.shift();continue}if(s.x>0&&s.y<t.length-1&&(o||f(t,n,s.x,s.y+1)&&f(t,n,s.x-1,s.y))&&(h(r[0],s,-1,1,14*a[t[s.y+1][s.x-1]]),!0===r[0].isDoneCalculating)){r.shift();continue}}}else{var c=r[0];setTimeout((function(){c.callback(null)})),r.shift()}}};var h=function(i,e,a,o,r){var u=e.x+a,l=e.y+o;if(void 0===s[u+"_"+l]){if(i.endX===u&&i.endY===l){i.isDoneCalculating=!0;var h=[],y=0;h[y]={x:u,y:l},h[++y]={x:e.x,y:e.y},y++;for(var d=e.parent;null!=d;)h[y]={x:d.x,y:d.y},y++,d=d.parent;h.reverse();var g=i,v=h;setTimeout((function(){g.callback(v)}))}if(f(t,n,u,l)){var E=c(i,u,l,e,r);void 0===E.list?(E.list=EasyStar.Node.OPEN_LIST,i.openList.insert(E)):E.list===EasyStar.Node.OPEN_LIST&&e.costSoFar+r<E.costSoFar&&(E.costSoFar=e.costSoFar+r,E.parent=e)}}},f=function(t,i,n,e){for(var s=0;s<i.length;s++)if(t[e][n]===i[s])return!0;return!1},c=function(t,i,n,e,s){if(void 0!==t.nodeHash[i+"_"+n])return t.nodeHash[i+"_"+n];var a=y(i,n,t.endX,t.endY);if(null!==e)var o=e.costSoFar+s;else o=a;var r=new EasyStar.Node(e,i,n,o,a);return t.nodeHash[i+"_"+n]=r,r},y=function(t,i,n,s){return Math.sqrt(Math.abs(n-t)*Math.abs(n-t)+Math.abs(s-i)*Math.abs(s-i))*e}};