You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

413 lines
11 KiB

  1. // javascript-astar 0.4.1
  2. // http://github.com/bgrins/javascript-astar
  3. // Freely distributable under the MIT License.
  4. // Implements the astar search algorithm in javascript using a Binary Heap.
  5. // Includes Binary Heap (with modifications) from Marijn Haverbeke.
  6. // http://eloquentjavascript.net/appendix2.html
  7. (function (definition) {
  8. /* global module, define */
  9. if (typeof module === 'object' && typeof module.exports === 'object')
  10. module.exports = definition();
  11. else if (typeof define === 'function' && define.amd)
  12. define([], definition);
  13. else {
  14. let exports = definition();
  15. window.astar = exports.astar;
  16. window.Graph = exports.Graph;
  17. }
  18. })(function () {
  19. function pathTo (node) {
  20. let curr = node;
  21. let path = [];
  22. while (curr.parent) {
  23. path.unshift(curr);
  24. curr = curr.parent;
  25. }
  26. return path;
  27. }
  28. function getHeap () {
  29. return new BinaryHeap(function (node) {
  30. return node.f;
  31. });
  32. }
  33. var astar = {
  34. /**
  35. * Perform an A* Search on a graph given a start and end node.
  36. * @param {Graph} graph
  37. * @param {GridNode} start
  38. * @param {GridNode} end
  39. * @param {Object} [options]
  40. * @param {bool} [options.closest] Specifies whether to return the
  41. path to the closest node if the target is unreachable.
  42. * @param {
  43. Function
  44. }[options.heuristic] Heuristic
  45. function (see
  46. * astar.heuristics).
  47. */
  48. search: function (graph, start, end, options) {
  49. start = graph.grid[start.x][start.y] || start;
  50. end = graph.grid[end.x][end.y] || end;
  51. graph.cleanDirty();
  52. options = options || {};
  53. let heuristic = options.heuristic || astar.heuristics.manhattan;
  54. let closest = options.closest || false;
  55. let distance = options.distance;
  56. if (distance)
  57. heuristic = astar.heuristics.manhattanDistance;
  58. let openHeap = getHeap();
  59. let closestNode = start; // set the start node to be the closest if required
  60. start.h = heuristic(start, end, distance);
  61. graph.markDirty(start);
  62. openHeap.push(start);
  63. while (openHeap.size() > 0) {
  64. // Grab the lowest f(x) to process next. Heap keeps this sorted for us.
  65. let currentNode = openHeap.pop();
  66. let onWall = !currentNode.isWall || currentNode.isWall();
  67. if (!onWall) {
  68. if (distance) {
  69. if (currentNode.h == distance)
  70. return pathTo(currentNode);
  71. } else {
  72. // End case -- result has been found, return the traced path.
  73. if (currentNode === end)
  74. return pathTo(currentNode);
  75. }
  76. }
  77. // Normal case -- move currentNode from open to closed, process each of its neighbors.
  78. currentNode.closed = true;
  79. // Find all neighbors for the current node.
  80. let neighbors = graph.neighbors(currentNode);
  81. for (let i = 0, il = neighbors.length; i < il; ++i) {
  82. let neighbor = neighbors[i];
  83. if (neighbor.closed || neighbor.isWall()) {
  84. // Not a valid node to process, skip to next neighbor.
  85. continue;
  86. }
  87. // The g score is the shortest distance from start to current node.
  88. // We need to check if the path we have arrived at this neighbor is the shortest one we have seen yet.
  89. let gScore = currentNode.g + neighbor.getCost(currentNode);
  90. let beenVisited = neighbor.visited;
  91. if (!beenVisited || gScore < neighbor.g) {
  92. // Found an optimal (so far) path to this node. Take score for node to see how good it is.
  93. neighbor.visited = true;
  94. neighbor.parent = currentNode;
  95. neighbor.h = neighbor.h || heuristic(neighbor, end, distance);
  96. neighbor.g = gScore;
  97. neighbor.f = neighbor.g + neighbor.h;
  98. graph.markDirty(neighbor);
  99. if (closest) {
  100. // If the neighbour is closer than the current closestNode or if it's equally close but has
  101. // a cheaper path than the current closest node then it becomes the closest node
  102. if (neighbor.h < closestNode.h || (neighbor.h === closestNode.h && neighbor.g < closestNode.g))
  103. closestNode = neighbor;
  104. }
  105. if (!beenVisited) {
  106. // Pushing to heap will put it in proper place based on the 'f' value.
  107. openHeap.push(neighbor);
  108. } else {
  109. // Already seen the node, but since it has been rescored we need to reorder it in the heap
  110. openHeap.rescoreElement(neighbor);
  111. }
  112. }
  113. }
  114. }
  115. if (closest)
  116. return pathTo(closestNode);
  117. // No result was found - empty array signifies failure to find path.
  118. return [];
  119. },
  120. // See list of heuristics: http://theory.stanford.edu/~amitp/GameProgramming/Heuristics.html
  121. heuristics: {
  122. manhattan: function (pos0, pos1) {
  123. let d1 = Math.abs(pos1.x - pos0.x);
  124. let d2 = Math.abs(pos1.y - pos0.y);
  125. return Math.max(d1, d2);
  126. },
  127. manhattanDistance: function (pos0, pos1, distance) {
  128. let d1 = Math.abs(pos1.x - pos0.x);
  129. let d2 = Math.abs(pos1.y - pos0.y);
  130. return Math.abs(distance - Math.max(d1, d2)) + 1;
  131. },
  132. diagonal: function (pos0, pos1) {
  133. let D = 1;
  134. let D2 = Math.sqrt(2);
  135. let d1 = Math.abs(pos1.x - pos0.x);
  136. let d2 = Math.abs(pos1.y - pos0.y);
  137. return (D * (d1 + d2)) + ((D2 - (2 * D)) * Math.min(d1, d2));
  138. }
  139. },
  140. cleanNode: function (node) {
  141. if (!node)
  142. return;
  143. node.f = 0;
  144. node.g = 0;
  145. node.h = 0;
  146. node.visited = false;
  147. node.closed = false;
  148. node.parent = null;
  149. }
  150. };
  151. /**
  152. * A graph memory structure
  153. * @param {Array} gridIn 2D array of input weights
  154. * @param {Object} [options]
  155. * @param {bool} [options.diagonal] Specifies whether diagonal moves are allowed
  156. */
  157. function Graph (gridIn, options) {
  158. options = options || {};
  159. this.nodes = [];
  160. this.diagonal = !!options.diagonal;
  161. this.grid = [];
  162. for (let x = 0; x < gridIn.length; x++) {
  163. this.grid[x] = [];
  164. for (let y = 0, row = gridIn[x]; y < row.length; y++) {
  165. if (!row[y]) {
  166. node = new GridNode(x, y, row[y] ? 0 : 1);
  167. this.grid[x][y] = node;
  168. this.nodes.push(node);
  169. }
  170. }
  171. }
  172. this.init();
  173. }
  174. Graph.prototype.init = function () {
  175. this.dirtyNodes = [];
  176. for (let i = 0; i < this.nodes.length; i++)
  177. astar.cleanNode(this.nodes[i]);
  178. };
  179. Graph.prototype.cleanDirty = function () {
  180. for (let i = 0; i < this.dirtyNodes.length; i++)
  181. astar.cleanNode(this.dirtyNodes[i]);
  182. this.dirtyNodes = [];
  183. };
  184. Graph.prototype.markDirty = function (node) {
  185. this.dirtyNodes.push(node);
  186. };
  187. Graph.prototype.neighbors = function (node) {
  188. let ret = [];
  189. let x = node.x;
  190. let y = node.y;
  191. let grid = this.grid;
  192. // West
  193. if (grid[x - 1] && grid[x - 1][y])
  194. ret.push(grid[x - 1][y]);
  195. // East
  196. if (grid[x + 1] && grid[x + 1][y])
  197. ret.push(grid[x + 1][y]);
  198. // South
  199. if (grid[x] && grid[x][y - 1])
  200. ret.push(grid[x][y - 1]);
  201. // North
  202. if (grid[x] && grid[x][y + 1])
  203. ret.push(grid[x][y + 1]);
  204. if (this.diagonal) {
  205. // Southwest
  206. if (grid[x - 1] && grid[x - 1][y - 1])
  207. ret.push(grid[x - 1][y - 1]);
  208. // Southeast
  209. if (grid[x + 1] && grid[x + 1][y - 1])
  210. ret.push(grid[x + 1][y - 1]);
  211. // Northwest
  212. if (grid[x - 1] && grid[x - 1][y + 1])
  213. ret.push(grid[x - 1][y + 1]);
  214. // Northeast
  215. if (grid[x + 1] && grid[x + 1][y + 1])
  216. ret.push(grid[x + 1][y + 1]);
  217. }
  218. return ret;
  219. };
  220. Graph.prototype.toString = function () {
  221. let graphString = [];
  222. let nodes = this.grid;
  223. for (let x = 0; x < nodes.length; x++) {
  224. let rowDebug = [];
  225. let row = nodes[x];
  226. for (let y = 0; y < row.length; y++)
  227. rowDebug.push(row[y].weight);
  228. graphString.push(rowDebug.join(' '));
  229. }
  230. return graphString.join('\n');
  231. };
  232. function GridNode (x, y, weight) {
  233. this.x = x;
  234. this.y = y;
  235. this.weight = weight;
  236. }
  237. GridNode.prototype.toString = function () {
  238. return '[' + this.x + ' ' + this.y + ']';
  239. };
  240. GridNode.prototype.getCost = function (fromNeighbor) {
  241. // Take diagonal weight into consideration.
  242. if (fromNeighbor && fromNeighbor.x != this.x && fromNeighbor.y != this.y)
  243. return this.weight * 1.41421;
  244. return this.weight;
  245. };
  246. GridNode.prototype.isWall = function () {
  247. return this.weight === 0;
  248. };
  249. function BinaryHeap (scoreFunction) {
  250. this.content = [];
  251. this.scoreFunction = scoreFunction;
  252. }
  253. BinaryHeap.prototype = {
  254. push: function (element) {
  255. // Add the new element to the end of the array.
  256. this.content.push(element);
  257. // Allow it to sink down.
  258. this.sinkDown(this.content.length - 1);
  259. },
  260. pop: function () {
  261. // Store the first element so we can return it later.
  262. let result = this.content[0];
  263. // Get the element at the end of the array.
  264. let end = this.content.pop();
  265. // If there are any elements left, put the end element at the
  266. // start, and let it bubble up.
  267. if (this.content.length > 0) {
  268. this.content[0] = end;
  269. this.bubbleUp(0);
  270. }
  271. return result;
  272. },
  273. remove: function (node) {
  274. let i = this.content.indexOf(node);
  275. // When it is found, the process seen in 'pop' is repeated
  276. // to fill up the hole.
  277. let end = this.content.pop();
  278. if (i !== this.content.length - 1) {
  279. this.content[i] = end;
  280. if (this.scoreFunction(end) < this.scoreFunction(node))
  281. this.sinkDown(i);
  282. else
  283. this.bubbleUp(i);
  284. }
  285. },
  286. size: function () {
  287. return this.content.length;
  288. },
  289. rescoreElement: function (node) {
  290. this.sinkDown(this.content.indexOf(node));
  291. },
  292. sinkDown: function (n) {
  293. // Fetch the element that has to be sunk.
  294. let element = this.content[n];
  295. // When at 0, an element can not sink any further.
  296. while (n > 0) {
  297. // Compute the parent element's index, and fetch it.
  298. let parentN = ((n + 1) >> 1) - 1;
  299. let parent = this.content[parentN];
  300. // Swap the elements if the parent is greater.
  301. if (this.scoreFunction(element) < this.scoreFunction(parent)) {
  302. this.content[parentN] = element;
  303. this.content[n] = parent;
  304. // Update 'n' to continue at the new position.
  305. n = parentN;
  306. }
  307. // Found a parent that is less, no need to sink any further.
  308. else
  309. break;
  310. }
  311. },
  312. bubbleUp: function (n) {
  313. // Look up the target element and its score.
  314. let length = this.content.length;
  315. let element = this.content[n];
  316. let elemScore = this.scoreFunction(element);
  317. while (true) {
  318. // Compute the indices of the child elements.
  319. let child2N = (n + 1) << 1;
  320. let child1N = child2N - 1;
  321. // This is used to store the new position of the element, if any.
  322. let swap = null;
  323. var child1Score;
  324. // If the first child exists (is inside the array)...
  325. if (child1N < length) {
  326. // Look it up and compute its score.
  327. let child1 = this.content[child1N];
  328. child1Score = this.scoreFunction(child1);
  329. // If the score is less than our element's, we need to swap.
  330. if (child1Score < elemScore)
  331. swap = child1N;
  332. }
  333. // Do the same checks for the other child.
  334. if (child2N < length) {
  335. let child2 = this.content[child2N];
  336. let child2Score = this.scoreFunction(child2);
  337. if (child2Score < (swap === null ? elemScore : child1Score))
  338. swap = child2N;
  339. }
  340. // If the element needs to be moved, swap it, and continue.
  341. if (swap !== null) {
  342. this.content[n] = this.content[swap];
  343. this.content[swap] = element;
  344. n = swap;
  345. }
  346. // Otherwise, we are done.
  347. else
  348. break;
  349. }
  350. }
  351. };
  352. return {
  353. astar: astar,
  354. Graph: Graph,
  355. gridNode: GridNode
  356. };
  357. });