Permalink
Browse files

Rebuild to apply performance improvements.

  • Loading branch information...
sbj42 committed Mar 31, 2017
1 parent 22c1a98 commit 077db3ce57ea3bf67345c784a504c7458b6b3ce8
Showing with 44 additions and 52 deletions.
  1. +43 −51 bin/easystar-0.4.0.js
  2. +1 −1 bin/easystar-0.4.0.min.js
View
@@ -166,7 +166,10 @@ var EasyStar =
* @param {Number} The multiplicative cost associated with the given point.
**/
this.setAdditionalPointCost = function (x, y, cost) {
pointsToCost[x + '_' + y] = cost;
if (pointsToCost[y] === undefined) {
pointsToCost[y] = {};
}
pointsToCost[y][x] = cost;
};
/**
@@ -176,7 +179,9 @@ var EasyStar =
* @param {Number} y The y value of the point to stop costing.
**/
this.removeAdditionalPointCost = function (x, y) {
delete pointsToCost[x + '_' + y];
if (pointsToCost[y] !== undefined) {
delete pointsToCost[y][x];
}
};
/**
@@ -195,7 +200,10 @@ var EasyStar =
* the tile.
**/
this.setDirectionalCondition = function (x, y, allowedDirections) {
directionalConditions[x + '_' + y] = allowedDirections;
if (directionalConditions[y] === undefined) {
directionalConditions[y] = {};
}
directionalConditions[y][x] = allowedDirections;
};
/**
@@ -225,7 +233,10 @@ var EasyStar =
* @param {Number} y The y value of the point to avoid.
**/
this.avoidAdditionalPoint = function (x, y) {
pointsToAvoid[x + "_" + y] = 1;
if (pointsToAvoid[y] === undefined) {
pointsToAvoid[y] = {};
}
pointsToAvoid[y][x] = 1;
};
/**
@@ -235,7 +246,9 @@ var EasyStar =
* @param {Number} y The y value of the point to stop avoiding.
**/
this.stopAvoidingAdditionalPoint = function (x, y) {
delete pointsToAvoid[x + "_" + y];
if (pointsToAvoid[y] !== undefined) {
delete pointsToAvoid[y][x];
}
};
/**
@@ -318,8 +331,6 @@ var EasyStar =
return;
}
var instanceId = nextInstanceId++;
// Create the instance
var instance = new Instance();
instance.openList = new Heap(function (nodeA, nodeB) {
@@ -335,6 +346,7 @@ var EasyStar =
instance.openList.push(coordinateToNode(instance, instance.startX, instance.startY, null, STRAIGHT_COST));
var instanceId = nextInstanceId++;
instances[instanceId] = instance;
instanceQueue.push(instanceId);
return instanceId;
@@ -396,7 +408,6 @@ var EasyStar =
// Handles the case where we have found the destination
if (instance.endX === searchNode.x && instance.endY === searchNode.y) {
instance.isDoneCalculating = true;
var path = [];
path.push({ x: searchNode.x, y: searchNode.y });
var parent = searchNode.parent;
@@ -407,79 +418,56 @@ var EasyStar =
path.reverse();
var ip = path;
instance.callback(ip);
return;
delete instances[instanceId];
instanceQueue.shift();
continue;
}
var tilesToSearch = [];
searchNode.list = CLOSED_LIST;
if (searchNode.y > 0) {
tilesToSearch.push({ instance: instance, searchNode: searchNode,
x: 0, y: -1, cost: STRAIGHT_COST * getTileCost(searchNode.x, searchNode.y - 1) });
checkAdjacentNode(instance, searchNode, 0, -1, STRAIGHT_COST * getTileCost(searchNode.x, searchNode.y - 1));
}
if (searchNode.x < collisionGrid[0].length - 1) {
tilesToSearch.push({ instance: instance, searchNode: searchNode,
x: 1, y: 0, cost: STRAIGHT_COST * getTileCost(searchNode.x + 1, searchNode.y) });
checkAdjacentNode(instance, searchNode, 1, 0, STRAIGHT_COST * getTileCost(searchNode.x + 1, searchNode.y));
}
if (searchNode.y < collisionGrid.length - 1) {
tilesToSearch.push({ instance: instance, searchNode: searchNode,
x: 0, y: 1, cost: STRAIGHT_COST * getTileCost(searchNode.x, searchNode.y + 1) });
checkAdjacentNode(instance, searchNode, 0, 1, STRAIGHT_COST * getTileCost(searchNode.x, searchNode.y + 1));
}
if (searchNode.x > 0) {
tilesToSearch.push({ instance: instance, searchNode: searchNode,
x: -1, y: 0, cost: STRAIGHT_COST * getTileCost(searchNode.x - 1, searchNode.y) });
checkAdjacentNode(instance, searchNode, -1, 0, STRAIGHT_COST * getTileCost(searchNode.x - 1, searchNode.y));
}
if (diagonalsEnabled) {
if (searchNode.x > 0 && searchNode.y > 0) {
if (allowCornerCutting || isTileWalkable(collisionGrid, acceptableTiles, searchNode.x, searchNode.y - 1) && isTileWalkable(collisionGrid, acceptableTiles, searchNode.x - 1, searchNode.y)) {
tilesToSearch.push({ instance: instance, searchNode: searchNode,
x: -1, y: -1, cost: DIAGONAL_COST * getTileCost(searchNode.x - 1, searchNode.y - 1) });
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 (allowCornerCutting || isTileWalkable(collisionGrid, acceptableTiles, searchNode.x, searchNode.y + 1) && isTileWalkable(collisionGrid, acceptableTiles, searchNode.x + 1, searchNode.y)) {
tilesToSearch.push({ instance: instance, searchNode: searchNode,
x: 1, y: 1, cost: DIAGONAL_COST * getTileCost(searchNode.x + 1, searchNode.y + 1) });
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 (allowCornerCutting || isTileWalkable(collisionGrid, acceptableTiles, searchNode.x, searchNode.y - 1) && isTileWalkable(collisionGrid, acceptableTiles, searchNode.x + 1, searchNode.y)) {
tilesToSearch.push({ instance: instance, searchNode: searchNode,
x: 1, y: -1, cost: DIAGONAL_COST * getTileCost(searchNode.x + 1, searchNode.y - 1) });
checkAdjacentNode(instance, searchNode, 1, -1, DIAGONAL_COST * getTileCost(searchNode.x + 1, searchNode.y - 1));
}
}
if (searchNode.x > 0 && searchNode.y < collisionGrid.length - 1) {
if (allowCornerCutting || isTileWalkable(collisionGrid, acceptableTiles, searchNode.x, searchNode.y + 1) && isTileWalkable(collisionGrid, acceptableTiles, searchNode.x - 1, searchNode.y)) {
tilesToSearch.push({ instance: instance, searchNode: searchNode,
x: -1, y: 1, cost: DIAGONAL_COST * getTileCost(searchNode.x - 1, searchNode.y + 1) });
checkAdjacentNode(instance, searchNode, -1, 1, DIAGONAL_COST * getTileCost(searchNode.x - 1, searchNode.y + 1));
}
}
}
var isDoneCalculating = false;
// Search all of the surrounding nodes
for (var i = 0; i < tilesToSearch.length; i++) {
checkAdjacentNode(tilesToSearch[i].instance, tilesToSearch[i].searchNode, tilesToSearch[i].x, tilesToSearch[i].y, tilesToSearch[i].cost);
if (tilesToSearch[i].instance.isDoneCalculating === true) {
isDoneCalculating = true;
break;
}
}
if (isDoneCalculating) {
delete instances[instanceId];
instanceQueue.shift();
continue;
}
}
};
@@ -488,7 +476,7 @@ var EasyStar =
var adjacentCoordinateX = searchNode.x + x;
var adjacentCoordinateY = searchNode.y + y;
if (pointsToAvoid[adjacentCoordinateX + "_" + adjacentCoordinateY] === undefined && isTileWalkable(collisionGrid, acceptableTiles, adjacentCoordinateX, adjacentCoordinateY, searchNode)) {
if ((pointsToAvoid[adjacentCoordinateY] === undefined || pointsToAvoid[adjacentCoordinateY][adjacentCoordinateX] === undefined) && isTileWalkable(collisionGrid, acceptableTiles, adjacentCoordinateX, adjacentCoordinateY, searchNode)) {
var node = coordinateToNode(instance, adjacentCoordinateX, adjacentCoordinateY, searchNode, cost);
if (node.list === undefined) {
@@ -504,11 +492,12 @@ var EasyStar =
// Helpers
var isTileWalkable = function (collisionGrid, acceptableTiles, x, y, sourceNode) {
if (directionalConditions[x + "_" + y]) {
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 < directionalConditions[x + "_" + y].length; i++) {
if (directionalConditions[x + "_" + y][i] === direction) return true;
for (var i = 0; i < directionalCondition.length; i++) {
if (directionalCondition[i] === direction) return true;
}
return false;
};
@@ -534,12 +523,16 @@ var EasyStar =
};
var getTileCost = function (x, y) {
return pointsToCost[x + '_' + y] || costMap[collisionGrid[y][x]];
return pointsToCost[y] && pointsToCost[y][x] || costMap[collisionGrid[y][x]];
};
var coordinateToNode = function (instance, x, y, parent, cost) {
if (instance.nodeHash[x + "_" + y] !== undefined) {
return instance.nodeHash[x + "_" + y];
if (instance.nodeHash[y] !== undefined) {
if (instance.nodeHash[y][x] !== undefined) {
return instance.nodeHash[y][x];
}
} else {
instance.nodeHash[y] = {};
}
var simpleDistanceToTarget = getDistance(x, y, instance.endX, instance.endY);
if (parent !== null) {
@@ -548,7 +541,7 @@ var EasyStar =
costSoFar = 0;
}
var node = new Node(parent, x, y, costSoFar, simpleDistanceToTarget);
instance.nodeHash[x + "_" + y] = node;
instance.nodeHash[y][x] = node;
return node;
};
@@ -589,7 +582,6 @@ var EasyStar =
* A path that is in the queue to eventually be found.
*/
module.exports = function () {
this.isDoneCalculating = true;
this.pointsToAvoid = {};
this.startX;
this.callback;
Oops, something went wrong.

0 comments on commit 077db3c

Please sign in to comment.