Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Creates a path object from point A to B
- // __arguments__
- // A_X, A_Y - x and y coordinates of starting tile
- // B_X, B_Y - x and y coordinates of target/goal
- // passableCallback - a function, given x and y coordinates as arguments, determining whether a tile at x,y is passable
- // maxIterations - the maximum number of nodes that the search will explore when looking for a path
- // if nothing is given, the search will look at a maximum of 100 nodes
- // __properties__
- // success - is true if a path was successfully found between the given points
- // goal - coordinate holder for goal of the path ( goal.x goal.y )
- // start - is the starting node of the path
- // start.x
- // start.y
- // start.next - the next node in the path. which will also have a prev property which points to start
- // end - is the last node of the path, but not necessarily the goal given that searching out the path may have failed
- // end.x
- // end.y
- // end.prev - the previous node in the path, which will also have a next property which points to the end
- // [anonymous nodes] have the following properties (x y prev next)
- TILES.PathFromAtoB = function(A_X, A_Y, B_X, B_Y, passableCallback, maxIterations)
- {
- // information about the Path
- this.success = false;
- this.start = {}; // starting mode
- this.end = {}; // end node of the path
- /* // this is unnecessary since start has the same information
- this.from = {
- x: A_X,
- y: A_Y
- };
- */
- this.goal = {
- x: B_X,
- y: B_Y
- };
- // configuration
- //this._passableCallback = passableCallback;
- if(!maxIterations)
- maxIterations = 100;
- // private variables
- this._todo = [];
- this._done = {};
- // add the starting node
- this._addCandidate(A_X, A_Y, null);
- this.start = this._done[A_X+","+A_Y];
- this.end = this.start;
- // the search for the shortest path from point A to point B begins here
- var iterations = 0;
- while (this._todo.length && iterations<=maxIterations)
- {
- iterations++;
- var node = this._todo.shift();
- if(this.end.h>node.h)
- this.end = node;
- if (node.x == B_X && node.y == B_Y)
- {
- this.success = true;
- this.goal = node;
- break;
- }
- var neighbors = TILES.getNeighboringTiles(node.x, node.y, "4", passableCallback);
- for (var i=0;i<neighbors.length;i++)
- {
- var neighbor = neighbors[i];
- var x = neighbor[0];
- var y = neighbor[1];
- var id = x+","+y;
- if (id in this._done)
- continue;
- else
- this._addCandidate(x, y, node);
- }
- }
- // build path
- // by hooking up all of the next properties for each node
- var item = this._done[this.end.x+","+this.end.y];
- while (item)
- {
- if(item.prev)
- item.prev.next = item;
- item = item.prev;
- }
- this.end.next = null;
- }
- TILES.PathFromAtoB.prototype._addCandidate = function(x, y, prev)
- {
- var obj = {
- x: x,
- y: y,
- prev: prev,
- next: null,
- g: (prev ? prev.g+1 : 0),
- h: (TILES.getManhattanDistance(x, y, this.goal.x, this.goal.y))
- }
- this._done[x+","+y] = obj;
- // insert into priority queue
- var f = obj.g + obj.h;
- for (var i=0;i<this._todo.length;i++)
- {
- var item = this._todo[i];
- if (f < item.g + item.h)
- {
- this._todo.splice(i, 0, obj);
- return;
- }
- }
- this._todo.push(obj);
- }
Advertisement
Add Comment
Please, Sign In to add comment