diff --git a/bin/easystar-0.4.3.js b/bin/easystar-0.4.3.js index 5ccabb4..e9c45c6 100644 --- a/bin/easystar-0.4.3.js +++ b/bin/easystar-0.4.3.js @@ -81,6 +81,9 @@ var EasyStar = var iterationsPerCalculation = Number.MAX_VALUE; var acceptableTiles; var diagonalsEnabled = false; + var isNdarrayGrid = false; + var gridWidth; + var gridHeight; /** * Sets the collision grid that EasyStar uses. @@ -137,11 +140,20 @@ var EasyStar = this.setGrid = function (grid) { collisionGrid = grid; + if (!Array.isArray(collisionGrid)) { + isNdarrayGrid = true; + gridWidth = collisionGrid.shape[0]; + gridHeight = collisionGrid.shape[1]; + } else { + gridWidth = collisionGrid[0].length; + gridHeight = collisionGrid.length; + } + //Setup cost map - for (var y = 0; y < collisionGrid.length; y++) { - for (var x = 0; x < collisionGrid[0].length; x++) { - if (!costMap[collisionGrid[y][x]]) { - costMap[collisionGrid[y][x]] = 1; + for (var y = 0; y < gridHeight; y++) { + for (var x = 0; x < gridWidth; x++) { + if (!costMap[getTileAt(x, y)]) { + costMap[getTileAt(x, y)] = 1; } } } @@ -306,7 +318,7 @@ var EasyStar = } // Start or endpoint outside of scope. - if (startX < 0 || startY < 0 || endX < 0 || endY < 0 || startX > collisionGrid[0].length - 1 || startY > collisionGrid.length - 1 || endX > collisionGrid[0].length - 1 || endY > collisionGrid.length - 1) { + if (startX < 0 || startY < 0 || endX < 0 || endY < 0 || startX > gridWidth - 1 || startY > gridHeight - 1 || endX > gridWidth - 1 || endY > gridHeight - 1) { throw new Error("Your start or end point is outside the scope of your grid."); } @@ -317,7 +329,7 @@ var EasyStar = } // End point is not an acceptable tile. - var endTile = collisionGrid[endY][endX]; + var endTile = getTileAt(endX, endY); var isAcceptable = false; for (var i = 0; i < acceptableTiles.length; i++) { if (endTile === acceptableTiles[i]) { @@ -326,7 +338,7 @@ var EasyStar = } } - if (isAcceptable === false) { + if (!isAcceptable) { callbackWrapper(null); return; } @@ -428,10 +440,10 @@ var EasyStar = if (searchNode.y > 0) { checkAdjacentNode(instance, searchNode, 0, -1, STRAIGHT_COST * getTileCost(searchNode.x, searchNode.y - 1)); } - if (searchNode.x < collisionGrid[0].length - 1) { + if (searchNode.x < gridWidth - 1) { checkAdjacentNode(instance, searchNode, 1, 0, STRAIGHT_COST * getTileCost(searchNode.x + 1, searchNode.y)); } - if (searchNode.y < collisionGrid.length - 1) { + if (searchNode.y < gridHeight - 1) { checkAdjacentNode(instance, searchNode, 0, 1, STRAIGHT_COST * getTileCost(searchNode.x, searchNode.y + 1)); } if (searchNode.x > 0) { @@ -440,28 +452,28 @@ var EasyStar = if (diagonalsEnabled) { if (searchNode.x > 0 && searchNode.y > 0) { - if (allowCornerCutting || isTileWalkable(collisionGrid, acceptableTiles, searchNode.x, searchNode.y - 1, searchNode) && isTileWalkable(collisionGrid, acceptableTiles, searchNode.x - 1, searchNode.y, searchNode)) { + if (allowCornerCutting || isTileWalkable(searchNode.x, searchNode.y - 1, searchNode) && isTileWalkable(searchNode.x - 1, searchNode.y, searchNode)) { checkAdjacentNode(instance, searchNode, -1, -1, DIAGONAL_COST * getTileCost(searchNode.x - 1, searchNode.y - 1)); } } - if (searchNode.x < collisionGrid[0].length - 1 && searchNode.y < collisionGrid.length - 1) { + if (searchNode.x < gridWidth - 1 && searchNode.y < gridHeight - 1) { - if (allowCornerCutting || isTileWalkable(collisionGrid, acceptableTiles, searchNode.x, searchNode.y + 1, searchNode) && isTileWalkable(collisionGrid, acceptableTiles, searchNode.x + 1, searchNode.y, searchNode)) { + if (allowCornerCutting || isTileWalkable(searchNode.x, searchNode.y + 1, searchNode) && isTileWalkable(searchNode.x + 1, searchNode.y, searchNode)) { checkAdjacentNode(instance, searchNode, 1, 1, DIAGONAL_COST * getTileCost(searchNode.x + 1, searchNode.y + 1)); } } - if (searchNode.x < collisionGrid[0].length - 1 && searchNode.y > 0) { + if (searchNode.x < gridWidth - 1 && searchNode.y > 0) { - if (allowCornerCutting || isTileWalkable(collisionGrid, acceptableTiles, searchNode.x, searchNode.y - 1, searchNode) && isTileWalkable(collisionGrid, acceptableTiles, searchNode.x + 1, searchNode.y, searchNode)) { + if (allowCornerCutting || isTileWalkable(searchNode.x, searchNode.y - 1, searchNode) && isTileWalkable(searchNode.x + 1, searchNode.y, searchNode)) { checkAdjacentNode(instance, searchNode, 1, -1, DIAGONAL_COST * getTileCost(searchNode.x + 1, searchNode.y - 1)); } } - if (searchNode.x > 0 && searchNode.y < collisionGrid.length - 1) { + if (searchNode.x > 0 && searchNode.y < gridHeight - 1) { - if (allowCornerCutting || isTileWalkable(collisionGrid, acceptableTiles, searchNode.x, searchNode.y + 1, searchNode) && isTileWalkable(collisionGrid, acceptableTiles, searchNode.x - 1, searchNode.y, searchNode)) { + if (allowCornerCutting || isTileWalkable(searchNode.x, searchNode.y + 1, searchNode) && isTileWalkable(searchNode.x - 1, searchNode.y, searchNode)) { checkAdjacentNode(instance, searchNode, -1, 1, DIAGONAL_COST * getTileCost(searchNode.x - 1, searchNode.y + 1)); } @@ -475,7 +487,7 @@ var EasyStar = var adjacentCoordinateX = searchNode.x + x; var adjacentCoordinateY = searchNode.y + y; - if ((pointsToAvoid[adjacentCoordinateY] === undefined || pointsToAvoid[adjacentCoordinateY][adjacentCoordinateX] === undefined) && isTileWalkable(collisionGrid, acceptableTiles, adjacentCoordinateX, adjacentCoordinateY, searchNode)) { + if ((pointsToAvoid[adjacentCoordinateY] === undefined || pointsToAvoid[adjacentCoordinateY][adjacentCoordinateX] === undefined) && isTileWalkable(adjacentCoordinateX, adjacentCoordinateY, searchNode)) { var node = coordinateToNode(instance, adjacentCoordinateX, adjacentCoordinateY, searchNode, cost); if (node.list === undefined) { @@ -490,7 +502,7 @@ var EasyStar = }; // Helpers - var isTileWalkable = function (collisionGrid, acceptableTiles, x, y, sourceNode) { + var isTileWalkable = function (x, y, sourceNode) { var directionalCondition = directionalConditions[y] && directionalConditions[y][x]; if (directionalCondition) { var direction = calculateDirection(sourceNode.x - x, sourceNode.y - y); @@ -503,7 +515,7 @@ var EasyStar = if (!directionIncluded()) return false; } for (var i = 0; i < acceptableTiles.length; i++) { - if (collisionGrid[y][x] === acceptableTiles[i]) { + if (getTileAt(x, y) === acceptableTiles[i]) { return true; } } @@ -522,7 +534,7 @@ var EasyStar = }; var getTileCost = function (x, y) { - return pointsToCost[y] && pointsToCost[y][x] || costMap[collisionGrid[y][x]]; + return pointsToCost[y] && pointsToCost[y][x] || costMap[getTileAt(x, y)]; }; var coordinateToNode = function (instance, x, y, parent, cost) { @@ -561,6 +573,10 @@ var EasyStar = return dx + dy; } }; + + var getTileAt = function (x, y) { + return isNdarrayGrid ? +collisionGrid.get(x, y) : +collisionGrid[y][x]; + }; }; EasyStar.TOP = 'TOP'; diff --git a/bin/easystar-0.4.3.min.js b/bin/easystar-0.4.3.min.js index 164e752..1019e2c 100644 --- a/bin/easystar-0.4.3.min.js +++ b/bin/easystar-0.4.3.min.js @@ -1 +1 @@ -var EasyStar=function(t){function n(o){if(e[o])return e[o].exports;var r=e[o]={exports:{},id:o,loaded:!1};return t[o].call(r.exports,r,r.exports,n),r.loaded=!0,r.exports}var e={};return n.m=t,n.c=e,n.p="",n(0)}([function(t,n,e){var o={},r=e(1),i=e(2),s=e(3);const u=0,a=1;t.exports=o;var l=1;o.js=function(){var t,n,e,c=1,f=1.4,h=!1,p={},d={},v={},y={},T=!0,g={},x=[],O=Number.MAX_VALUE,b=!1;this.setAcceptableTiles=function(t){t instanceof Array?e=t:!isNaN(parseFloat(t))&&isFinite(t)&&(e=[t])},this.enableSync=function(){h=!0},this.disableSync=function(){h=!1},this.enableDiagonals=function(){b=!0},this.disableDiagonals=function(){b=!1},this.setGrid=function(n){t=n;for(var e=0;et[0].length-1||o>t.length-1||i>t[0].length-1||u>t.length-1)throw new Error("Your start or end point is outside the scope of your grid.");if(n===i&&o===u)return void f([]);for(var p=t[u][i],d=!1,v=0;v0&&m(r,i,0,-1,c*F(i.x,i.y-1)),i.x0&&m(r,i,-1,0,c*F(i.x-1,i.y)),b&&(i.x>0&&i.y>0&&(T||A(t,e,i.x,i.y-1,i)&&A(t,e,i.x-1,i.y,i))&&m(r,i,-1,-1,f*F(i.x-1,i.y-1)),i.x0&&(T||A(t,e,i.x,i.y-1,i)&&A(t,e,i.x+1,i.y,i))&&m(r,i,1,-1,f*F(i.x+1,i.y-1)),i.x>0&&i.yn?1:0},p=function(t,n,e,o,r){var i;if(null==e&&(e=0),null==r&&(r=s),e<0)throw new Error("lo must be non-negative");for(null==o&&(o=t.length);ee;0<=e?n++:n--)l.push(n);return l}.apply(this).reverse(),a=[],o=0,r=i.length;oy;r=0<=y?++f:--f)T.push(l(t,e));return T},g=function(t,n,e,o){var r,i,u;for(null==o&&(o=s),r=t[e];e>n&&(u=e-1>>1,i=t[u],o(r,i)<0);)t[e]=i,e=u;return t[e]=r},x=function(t,n,e){var o,r,i,u,a;for(null==e&&(e=s),r=t.length,a=n,i=t[n],o=2*n+1;oc-1||r>f-1||i>c-1||u>f-1)throw new Error("Your start or end point is outside the scope of your grid.");if(n===i&&r===u)return void h([]);for(var v=G(i,u),y=!1,T=0;T0&&F(o,i,0,-1,p*w(i.x,i.y-1)),i.x0&&F(o,i,-1,0,p*w(i.x-1,i.y)),A&&(i.x>0&&i.y>0&&(g||P(i.x,i.y-1,i)&&P(i.x-1,i.y,i))&&F(o,i,-1,-1,h*w(i.x-1,i.y-1)),i.x0&&(g||P(i.x,i.y-1,i)&&P(i.x+1,i.y,i))&&F(o,i,1,-1,h*w(i.x+1,i.y-1)),i.x>0&&i.yn?1:0},h=function(t,n,e,r,o){var i;if(null==e&&(e=0),null==o&&(o=s),e<0)throw new Error("lo must be non-negative");for(null==r&&(r=t.length);ee;0<=e?n++:n--)l.push(n);return l}.apply(this).reverse(),a=[],r=0,o=i.length;ry;o=0<=y?++f:--f)T.push(l(t,e));return T},x=function(t,n,e,r){var o,i,u;for(null==r&&(r=s),o=t[e];e>n&&(u=e-1>>1,i=t[u],r(o,i)<0);)t[e]=i,e=u;return t[e]=o},g=function(t,n,e){var r,o,i,u,a;for(null==e&&(e=s),o=t.length,a=n,i=t[n],r=2*n+1;r collisionGrid[0].length-1 || startY > collisionGrid.length-1 || - endX > collisionGrid[0].length-1 || endY > collisionGrid.length-1) { + startX > gridWidth-1 || startY > gridHeight-1 || + endX > gridWidth-1 || endY > gridHeight-1) { throw new Error("Your start or end point is outside the scope of your grid."); } @@ -272,7 +284,7 @@ EasyStar.js = function() { } // End point is not an acceptable tile. - var endTile = collisionGrid[endY][endX]; + var endTile = getTileAt(endX, endY); var isAcceptable = false; for (var i = 0; i < acceptableTiles.length; i++) { if (endTile === acceptableTiles[i]) { @@ -281,7 +293,7 @@ EasyStar.js = function() { } } - if (isAcceptable === false) { + if (!isAcceptable) { callbackWrapper(null); return; } @@ -368,7 +380,7 @@ EasyStar.js = function() { path.push({x: searchNode.x, y: searchNode.y}); var parent = searchNode.parent; while (parent!=null) { - path.push({x: parent.x, y:parent.y}); + path.push({x: parent.x, y: parent.y}); parent = parent.parent; } path.reverse(); @@ -385,11 +397,11 @@ EasyStar.js = function() { checkAdjacentNode(instance, searchNode, 0, -1, STRAIGHT_COST * getTileCost(searchNode.x, searchNode.y-1)); } - if (searchNode.x < collisionGrid[0].length-1) { + if (searchNode.x < gridWidth-1) { checkAdjacentNode(instance, searchNode, 1, 0, STRAIGHT_COST * getTileCost(searchNode.x+1, searchNode.y)); } - if (searchNode.y < collisionGrid.length-1) { + if (searchNode.y < gridHeight-1) { checkAdjacentNode(instance, searchNode, 0, 1, STRAIGHT_COST * getTileCost(searchNode.x, searchNode.y+1)); } @@ -401,38 +413,38 @@ EasyStar.js = function() { if (searchNode.x > 0 && searchNode.y > 0) { if (allowCornerCutting || - (isTileWalkable(collisionGrid, acceptableTiles, searchNode.x, searchNode.y-1, searchNode) && - isTileWalkable(collisionGrid, acceptableTiles, searchNode.x-1, searchNode.y, searchNode))) { + (isTileWalkable(searchNode.x, searchNode.y-1, searchNode) && + isTileWalkable(searchNode.x-1, searchNode.y, searchNode))) { checkAdjacentNode(instance, searchNode, -1, -1, DIAGONAL_COST * getTileCost(searchNode.x-1, searchNode.y-1)); } } - if (searchNode.x < collisionGrid[0].length-1 && searchNode.y < collisionGrid.length-1) { + if (searchNode.x < gridWidth-1 && searchNode.y < gridHeight-1) { if (allowCornerCutting || - (isTileWalkable(collisionGrid, acceptableTiles, searchNode.x, searchNode.y+1, searchNode) && - isTileWalkable(collisionGrid, acceptableTiles, searchNode.x+1, searchNode.y, searchNode))) { + (isTileWalkable(searchNode.x, searchNode.y+1, searchNode) && + isTileWalkable(searchNode.x+1, searchNode.y, searchNode))) { checkAdjacentNode(instance, searchNode, 1, 1, DIAGONAL_COST * getTileCost(searchNode.x+1, searchNode.y+1)); } } - if (searchNode.x < collisionGrid[0].length-1 && searchNode.y > 0) { + if (searchNode.x < gridWidth-1 && searchNode.y > 0) { if (allowCornerCutting || - (isTileWalkable(collisionGrid, acceptableTiles, searchNode.x, searchNode.y-1, searchNode) && - isTileWalkable(collisionGrid, acceptableTiles, searchNode.x+1, searchNode.y, searchNode))) { + (isTileWalkable(searchNode.x, searchNode.y-1, searchNode) && + isTileWalkable(searchNode.x+1, searchNode.y, searchNode))) { checkAdjacentNode(instance, searchNode, 1, -1, DIAGONAL_COST * getTileCost(searchNode.x+1, searchNode.y-1)); } } - if (searchNode.x > 0 && searchNode.y < collisionGrid.length-1) { + if (searchNode.x > 0 && searchNode.y < gridHeight-1) { if (allowCornerCutting || - (isTileWalkable(collisionGrid, acceptableTiles, searchNode.x, searchNode.y+1, searchNode) && - isTileWalkable(collisionGrid, acceptableTiles, searchNode.x-1, searchNode.y, searchNode))) { + (isTileWalkable(searchNode.x, searchNode.y+1, searchNode) && + isTileWalkable(searchNode.x-1, searchNode.y, searchNode))) { checkAdjacentNode(instance, searchNode, -1, 1, DIAGONAL_COST * getTileCost(searchNode.x-1, searchNode.y+1)); @@ -450,7 +462,7 @@ EasyStar.js = function() { if ((pointsToAvoid[adjacentCoordinateY] === undefined || pointsToAvoid[adjacentCoordinateY][adjacentCoordinateX] === undefined) && - isTileWalkable(collisionGrid, acceptableTiles, adjacentCoordinateX, adjacentCoordinateY, searchNode)) { + isTileWalkable(adjacentCoordinateX, adjacentCoordinateY, searchNode)) { var node = coordinateToNode(instance, adjacentCoordinateX, adjacentCoordinateY, searchNode, cost); @@ -466,20 +478,20 @@ EasyStar.js = function() { }; // Helpers - var isTileWalkable = function(collisionGrid, acceptableTiles, x, y, sourceNode) { + var isTileWalkable = function(x, y, sourceNode) { var directionalCondition = directionalConditions[y] && directionalConditions[y][x]; if (directionalCondition) { var direction = calculateDirection(sourceNode.x - x, sourceNode.y - y) var directionIncluded = function () { for (var i = 0; i < directionalCondition.length; i++) { - if (directionalCondition[i] === direction) return true + if (directionalCondition[i] === direction) return true; } - return false + return false; } - if (!directionIncluded()) return false + if (!directionIncluded()) return false; } for (var i = 0; i < acceptableTiles.length; i++) { - if (collisionGrid[y][x] === acceptableTiles[i]) { + if (getTileAt(x, y) === acceptableTiles[i]) { return true; } } @@ -493,19 +505,19 @@ EasyStar.js = function() { * -1, 1 | 0, 1 | 1, 1 */ var calculateDirection = function (diffX, diffY) { - if (diffX === 0 && diffY === -1) return EasyStar.TOP - else if (diffX === 1 && diffY === -1) return EasyStar.TOP_RIGHT - else if (diffX === 1 && diffY === 0) return EasyStar.RIGHT - else if (diffX === 1 && diffY === 1) return EasyStar.BOTTOM_RIGHT - else if (diffX === 0 && diffY === 1) return EasyStar.BOTTOM - else if (diffX === -1 && diffY === 1) return EasyStar.BOTTOM_LEFT - else if (diffX === -1 && diffY === 0) return EasyStar.LEFT - else if (diffX === -1 && diffY === -1) return EasyStar.TOP_LEFT - throw new Error('These differences are not valid: ' + diffX + ', ' + diffY) + if (diffX === 0 && diffY === -1) return EasyStar.TOP; + else if (diffX === 1 && diffY === -1) return EasyStar.TOP_RIGHT; + else if (diffX === 1 && diffY === 0) return EasyStar.RIGHT; + else if (diffX === 1 && diffY === 1) return EasyStar.BOTTOM_RIGHT; + else if (diffX === 0 && diffY === 1) return EasyStar.BOTTOM; + else if (diffX === -1 && diffY === 1) return EasyStar.BOTTOM_LEFT; + else if (diffX === -1 && diffY === 0) return EasyStar.LEFT; + else if (diffX === -1 && diffY === -1) return EasyStar.TOP_LEFT; + throw new Error('These differences are not valid: ' + diffX + ', ' + diffY); }; var getTileCost = function(x, y) { - return (pointsToCost[y] && pointsToCost[y][x]) || costMap[collisionGrid[y][x]] + return (pointsToCost[y] && pointsToCost[y][x]) || costMap[getTileAt(x, y)]; }; var coordinateToNode = function(instance, x, y, parent, cost) { @@ -522,12 +534,12 @@ EasyStar.js = function() { } else { costSoFar = 0; } - var node = new Node(parent,x,y,costSoFar,simpleDistanceToTarget); + var node = new Node(parent, x, y, costSoFar, simpleDistanceToTarget); instance.nodeHash[y][x] = node; return node; }; - var getDistance = function(x1,y1,x2,y2) { + var getDistance = function(x1, y1, x2, y2) { if (diagonalsEnabled) { // Octile distance var dx = Math.abs(x1 - x2); @@ -544,6 +556,10 @@ EasyStar.js = function() { return (dx + dy); } }; + + var getTileAt = function(x, y) { + return isNdarrayGrid ? +collisionGrid.get(x, y) : +collisionGrid[y][x]; + }; } EasyStar.TOP = 'TOP'