henesua

TILES.getPathFromAtoB

May 21st, 2013
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Creates a path object from point A to B
  2. // __arguments__
  3. //  A_X, A_Y                    - x and y coordinates of starting tile
  4. //  B_X, B_Y                    - x and y coordinates of target/goal
  5. //  passableCallback    - a function, given x and y coordinates as arguments, determining whether a tile at x,y is passable
  6. //  maxIterations           - the maximum number of nodes that the search will explore when looking for a path
  7.                                         //  if nothing is given, the search will look at a maximum of 100 nodes
  8. // __properties__
  9. // success              - is true if a path was successfully found between the given points
  10. // goal                     - coordinate holder for goal of the path    ( goal.x goal.y )
  11. // start                    - is the starting node of the path
  12. //      start.x
  13. //      start.y
  14. //      start.next  - the next node in the path. which will also have a prev property which points to start
  15. // end                      - is the last node of the path, but not necessarily the goal given that searching out the path may have failed
  16. //      end.x
  17. //      end.y
  18. //      end.prev        - the previous node in the path, which will also have a next property which points to the end
  19. // [anonymous nodes] have the following properties (x y prev next)
  20.  
  21. TILES.PathFromAtoB  = function(A_X, A_Y, B_X, B_Y, passableCallback, maxIterations)
  22. {
  23.     // information about the Path
  24.     this.success    = false;
  25.     this.start      = {};   // starting mode
  26.     this.end            = {};   // end node of the path
  27.     /* // this is unnecessary since start has the same information
  28.     this.from           = {
  29.                                         x:  A_X,
  30.                                         y:  A_Y
  31.                                     };
  32.     */
  33.     this.goal           = {
  34.                                         x:  B_X,
  35.                                         y:  B_Y
  36.                                     };
  37.  
  38.     // configuration
  39.     //this._passableCallback = passableCallback;
  40.     if(!maxIterations)
  41.         maxIterations   = 100;
  42.  
  43.     // private variables
  44.     this._todo = [];
  45.     this._done = {};
  46.  
  47.     // add the starting node
  48.     this._addCandidate(A_X, A_Y, null);
  49.     this.start  = this._done[A_X+","+A_Y];
  50.     this.end        = this.start;
  51.  
  52.     // the search for the shortest path from point A to point B begins here
  53.     var iterations  = 0;
  54.     while (this._todo.length && iterations<=maxIterations)
  55.     {
  56.         iterations++;
  57.         var node = this._todo.shift();
  58.         if(this.end.h>node.h)
  59.             this.end    = node;
  60.         if (node.x == B_X && node.y == B_Y)
  61.         {
  62.             this.success    = true;
  63.             this.goal           = node;
  64.             break;
  65.         }
  66.  
  67.         var neighbors = TILES.getNeighboringTiles(node.x, node.y, "4", passableCallback);
  68.         for (var i=0;i<neighbors.length;i++)
  69.         {
  70.             var neighbor = neighbors[i];
  71.             var x = neighbor[0];
  72.             var y = neighbor[1];
  73.  
  74.             var id = x+","+y;
  75.             if (id in this._done)
  76.                 continue;
  77.             else
  78.                 this._addCandidate(x, y, node);
  79.         }
  80.     }
  81.  
  82.     // build path
  83.     // by hooking up all of the next properties for each node
  84.     var item        = this._done[this.end.x+","+this.end.y];
  85.     while (item)
  86.     {
  87.         if(item.prev)
  88.             item.prev.next  = item;
  89.         item = item.prev;
  90.     }
  91.     this.end.next   = null;
  92. }
  93.  
  94. TILES.PathFromAtoB.prototype._addCandidate  = function(x, y, prev)
  95. {
  96.     var obj = {
  97.                             x: x,
  98.                             y: y,
  99.                             prev: prev,
  100.                             next: null,
  101.                             g: (prev ? prev.g+1 : 0),
  102.                             h: (TILES.getManhattanDistance(x, y, this.goal.x, this.goal.y))
  103.                         }
  104.     this._done[x+","+y] = obj;
  105.  
  106.     // insert into priority queue
  107.     var f = obj.g + obj.h;
  108.     for (var i=0;i<this._todo.length;i++)
  109.     {
  110.         var item = this._todo[i];
  111.         if (f < item.g + item.h)
  112.         {
  113.             this._todo.splice(i, 0, obj);
  114.             return;
  115.         }
  116.     }
  117.  
  118.     this._todo.push(obj);
  119. }
Advertisement
Add Comment
Please, Sign In to add comment