Advertisement
Guest User

Untitled

a guest
May 5th, 2012
138
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. // Includes Binary Heap (with modifications) from Marijn Haverbeke.
  5. // http://eloquentjavascript.net/appendix2.html
  6.  
  7. // Creates a Graph class used in the astar search algorithm.
  8. function Graph(grid) {
  9.     var nodes = [];
  10.  
  11.     var row, rowLength, len = grid.length;
  12. for (y = 0; y <= 15; y++) {
  13.    
  14.              row = grid[y];
  15.              nodes[y] = new Array(15);
  16.    
  17.             for (x = 0; x <= 10; x++) {
  18.                
  19.                    nodes[x][y] = new GraphNode(x, y, row[x]);
  20.                 }
  21.             }
  22.  
  23.     this.input = grid;
  24.     this.nodes = nodes;
  25. }
  26.  
  27. Graph.prototype.toString = function() {
  28.  
  29.     var graphString = "\n";
  30.     var nodes = this.nodes;
  31.     var rowDebug, row, y, l;
  32.    // console.log("X Length: " + len);
  33.     for (var x = 0, len = nodes.length; x < len; x++) {
  34.         rowDebug = "";
  35.         row = nodes[x];
  36.         for (y = 0, l = row.length; y < l; y++) {
  37.             rowDebug += row[y].type + " ";
  38.         }
  39.         graphString = graphString + rowDebug + "\n";
  40.     }
  41.     return graphString;
  42. };
  43.  
  44. function GraphNode(x,y,type) {
  45.     this.data = { };
  46.     this.x = x;
  47.     this.y = y;
  48.     this.pos = {
  49.         x: x,
  50.         y: y
  51.     };
  52.     this.type = type;
  53. }
  54.  
  55. GraphNode.prototype.toString = function() {
  56.     return this.x + "," + this.y;
  57. };
  58.  
  59. GraphNode.prototype.isWall = function() {
  60.     return this.type == GraphNodeType.WALL;
  61. };
  62.  
  63.  
  64. function BinaryHeap(scoreFunction){
  65.     this.content = [];
  66.     this.scoreFunction = scoreFunction;
  67. }
  68.  
  69. BinaryHeap.prototype = {
  70.     push: function(element) {
  71.         // Add the new element to the end of the array.
  72.         this.content.push(element);
  73.  
  74.         // Allow it to sink down.
  75.         this.sinkDown(this.content.length - 1);
  76.     },
  77.     pop: function() {
  78.         // Store the first element so we can return it later.
  79.         var result = this.content[0];
  80.         // Get the element at the end of the array.
  81.         var end = this.content.pop();
  82.         // If there are any elements left, put the end element at the
  83.         // start, and let it bubble up.
  84.         if (this.content.length > 0) {
  85.              this.content[0] = end;
  86.              this.bubbleUp(0);
  87.         }
  88.         return result;
  89.     },
  90.     remove: function(node) {
  91.         var i = this.content.indexOf(node);
  92.    
  93.         // When it is found, the process seen in 'pop' is repeated
  94.         // to fill up the hole.
  95.         var end = this.content.pop();
  96.  
  97.         if (i !== this.content.length - 1) {
  98.             this.content[i] = end;
  99.            
  100.             if (this.scoreFunction(end) < this.scoreFunction(node)) {
  101.                 this.sinkDown(i);
  102.             }
  103.             else {
  104.                 this.bubbleUp(i);
  105.             }
  106.         }
  107.     },
  108.     size: function() {
  109.         return this.content.length;
  110.     },
  111.     rescoreElement: function(node) {
  112.         this.sinkDown(this.content.indexOf(node));
  113.     },
  114.     sinkDown: function(n) {
  115.         // Fetch the element that has to be sunk.
  116.         var element = this.content[n];
  117.  
  118.         // When at 0, an element can not sink any further.
  119.         while (n > 0) {
  120.  
  121.             // Compute the parent element's index, and fetch it.
  122.             var parentN = ((n + 1) >> 1) - 1,
  123.                 parent = this.content[parentN];
  124.             // Swap the elements if the parent is greater.
  125.             if (this.scoreFunction(element) < this.scoreFunction(parent)) {
  126.                 this.content[parentN] = element;
  127.                 this.content[n] = parent;
  128.                 // Update 'n' to continue at the new position.
  129.                 n = parentN;
  130.             }
  131.  
  132.             // Found a parent that is less, no need to sink any further.
  133.             else {
  134.                 break;
  135.             }
  136.         }
  137.     },
  138.     bubbleUp: function(n) {
  139.         // Look up the target element and its score.
  140.         var length = this.content.length,
  141.             element = this.content[n],
  142.             elemScore = this.scoreFunction(element);
  143.        
  144.         while(true) {
  145.             // Compute the indices of the child elements.
  146.             var child2N = (n + 1) << 1, child1N = child2N - 1;
  147.             // This is used to store the new position of the element,
  148.             // if any.
  149.             var swap = null;
  150.             // If the first child exists (is inside the array)...
  151.             if (child1N < length) {
  152.             // Look it up and compute its score.
  153.             var child1 = this.content[child1N],
  154.                 child1Score = this.scoreFunction(child1);
  155.  
  156.             // If the score is less than our element's, we need to swap.
  157.             if (child1Score < elemScore)
  158.                 swap = child1N;
  159.             }
  160.  
  161.             // Do the same checks for the other child.
  162.             if (child2N < length) {
  163.                 var child2 = this.content[child2N],
  164.                     child2Score = this.scoreFunction(child2);
  165.                 if (child2Score < (swap === null ? elemScore : child1Score)) {
  166.                     swap = child2N;
  167.                 }
  168.             }
  169.  
  170.             // If the element needs to be moved, swap it, and continue.
  171.             if (swap !== null) {
  172.                 this.content[n] = this.content[swap];
  173.                 this.content[swap] = element;
  174.                 n = swap;
  175.             }
  176.  
  177.             // Otherwise, we are done.
  178.             else {
  179.                 break;
  180.             }
  181.         }
  182.     }
  183. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement