Advertisement
Guest User

Untitled

a guest
May 5th, 2012
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // javascript-astar
  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.  
  6. var astar = {
  7.     init: function(grid) {
  8.         for(var x = 0, xl = grid.length; x < xl; x++) {
  9.             for(var y = 0, yl = grid[x].length; y < yl; y++) {
  10.                 var node = grid[x][y];
  11.                 node.f = 0;
  12.                 node.g = 0;
  13.                 node.h = 0;
  14.                 node.visited = false;
  15.                 node.closed = false;
  16.                 node.parent = null;
  17.             }
  18.         }
  19.     },
  20.     heap: function() {
  21.         return new BinaryHeap(function(node) {
  22.             return node.f;
  23.         });
  24.     },
  25.     search: function(grid, start, end, heuristic) {
  26.         astar.init(grid);
  27.         heuristic = heuristic || astar.manhattan;
  28.  
  29.         var openHeap = astar.heap();
  30.  
  31.         openHeap.push(start);
  32.  
  33.         while(openHeap.size() > 0) {
  34.  
  35.             // Grab the lowest f(x) to process next.  Heap keeps this sorted for us.
  36.             var currentNode = openHeap.pop();
  37.  
  38.             // End case -- result has been found, return the traced path.
  39.             if(currentNode === end) {
  40.                 var curr = currentNode;
  41.                 var ret = [];
  42.                 while(curr.parent) {
  43.                     ret.push(curr);
  44.                     curr = curr.parent;
  45.                 }
  46.                 return ret.reverse();
  47.             }
  48.  
  49.             // Normal case -- move currentNode from open to closed, process each of its neighbors.
  50.             currentNode.closed = true;
  51.  
  52.             var neighbors = astar.neighbors(grid, currentNode);
  53.             for(var i=0, il = neighbors.length; i < il; i++) {
  54.                 var neighbor = neighbors[i];
  55.                
  56.             var getCoords = new String(neighbor);
  57.             var finalCoords = getCoords.split(",");
  58.             var finalX = finalCoords[0];
  59.             var finalY = finalCoords[1];
  60.  
  61.                 if(neighbor.closed) {
  62.                     // Not a valid node to process, skip to next neighbor.
  63.                    
  64.  
  65.                     continue;
  66.                 }
  67.  
  68.                 // The g score is the shortest distance from start to current node.
  69.                 // We need to check if the path we have arrived at this neighbor is the shortest one we have seen yet.
  70.                 // 1 is the distance from a node to it's neighbor - this could be variable for weighted paths.
  71.                 var gScore = currentNode.g + 1;
  72.                 var beenVisited = neighbor.visited;
  73.  
  74.                 if(!beenVisited || gScore < neighbor.g) {
  75.  
  76.                     // Found an optimal (so far) path to this node.  Take score for node to see how good it is.
  77.                     neighbor.visited = true;
  78.                     neighbor.parent = currentNode;
  79.                     neighbor.h = neighbor.h || heuristic(neighbor.pos, end.pos);
  80.                     neighbor.g = gScore;
  81.                     neighbor.f = neighbor.g + neighbor.h;
  82.  
  83.                     if (!beenVisited) {
  84.                         // Pushing to heap will put it in proper place based on the 'f' value.
  85.                         openHeap.push(neighbor);
  86.                     }
  87.                     else {
  88.                         // Already seen the node, but since it has been rescored we need to reorder it in the heap
  89.                         openHeap.rescoreElement(neighbor);
  90.                     }
  91.                 }
  92.             }
  93.         }
  94.  
  95.         // No result was found - empty array signifies failure to find path.
  96.         return [];
  97.     },
  98.     manhattan: function(pos0, pos1) {
  99.         // See list of heuristics: http://theory.stanford.edu/~amitp/GameProgramming/Heuristics.html
  100.  
  101.         var d1 = Math.abs (pos1.x - pos0.x);
  102.         var d2 = Math.abs (pos1.y - pos0.y);
  103.         return d1 + d2;
  104.     },
  105.     neighbors: function(grid, node) {
  106.         var ret = [];
  107.         var x = node.x;
  108.         var y = node.y;
  109.  
  110.         if(grid[x-1] && grid[x-1][y]) {
  111.             ret.push(grid[x-1][y]);
  112.         }
  113.         if(grid[x+1] && grid[x+1][y]) {
  114.             ret.push(grid[x+1][y]);
  115.         }
  116.         if(grid[x] && grid[x][y-1]) {
  117.             ret.push(grid[x][y-1]);
  118.         }
  119.         if(grid[x] && grid[x][y+1]) {
  120.             ret.push(grid[x][y+1]);
  121.         }
  122.         return ret;
  123.     }
  124. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement