-
Notifications
You must be signed in to change notification settings - Fork 4
/
astar.js
78 lines (77 loc) · 2.05 KB
/
astar.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
Crafty.c("AStar",{
_compareNode:function(node1,node2){
if(node1.tile[0] != node2.tile[0]) return false;
return true;
},
_nodeInArray:function(node,array){
for(var i in array){
if(this._compareNode(node,array[i]))
return true;
}
return false;
},
_heuristic:undefined,
heuristic:function(f){
this._heuristic = f;
return this;
},
_findAdjacent:undefined,
findAdjacent:function(f){
this._findAdjacent = f;
return this;
},
findPath:function(position,ignore,weighted,begining,end){
if(this._heuristic == undefined || this._findAdjacent == undefined)
throw("Exception: You have to declare a heuristic and an adjacent function");
function Node(tile,parent,g,h,f){
this.tile = tile;
this.parent = parent;
this.g = g;
this.h = h;
this.f = f;
}
var start = new Node(begining,-1,-1,-1,-1);
var destination = new Node(end,-1,-1,-1,-1);
var open = [];
var closed = [];
var g = 0;
var h = this._heuristic(start, destination);
var f = g+h;
open.push(start);
while(open.length > 0){
open.sort(function(a,b){
var x = a.f;
var y = b.f;
return ((x < y) ? -1 : ((x > y) ? 1 : 0));
})
var current_node = open[0];
if(this._compareNode(current_node,destination)){
var path = [destination.tile];
while(current_node.parent != -1){
current_node = closed[current_node.parent];
path.unshift(current_node.tile);
}
return path;
}
open.shift();
closed.push(current_node);
var adj = this._findAdjacent(current_node);
for(var i in adj){
if((ignore != undefined && !ignore(current_node.tile,adj[i]))){
if(this._nodeInArray(new Node(adj[i]),closed))
{continue;}
if(!this._nodeInArray(new Node(adj[i]),open)) {
var new_node = new Node(adj[i],closed.length-1,-1,-1,-1);
new_node.g = 0;
if(weighted != undefined)
new_node.g += weighted(current_node.tile,new_node.tile);
new_node.h = this._heuristic(new_node, destination);
new_node.f = new_node.g+new_node.h;
open.push(new_node);
}
}
}
}
return [];
}
});