-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpathfinder.js
180 lines (146 loc) · 4.17 KB
/
pathfinder.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
// A* algorithm from pseudocode in Wireframe magazine issue 48
// by Paul Roberts
function pathfinder(src, dest)
{
var openlist=[]; // List of node ids yet to visit
var closedlist=[]; // List of visited node ids
const dx=Math.floor(dest%gs.width); // Destination X grid position
const dy=Math.floor(dest/gs.width); // Destination Y grid position
var n={}; // Next node
const nx=Math.floor(src%gs.width); // Next node X grid position
const ny=Math.floor(src/gs.width); // Next node Y grid position
var c=-1; // Check node id
var cx=-1; // Check node X grid position
var cy=-1; // Check node Y grid position
// Check if this grid position is solid (out of bounds to path)
function issolid(x, y)
{
// Out of bounds check
if ((x<0) || (x>=gs.width) || (y<0) || (y>=gs.height))
return true;
// Solid check
return (gs.tiles[(y*gs.width)+x]||0!=0);
}
// Determine cost (rough distance) from x1,y1 to x2,y2
function manhattan_cost(x1, y1, x2, y2)
{
return (Math.abs(x1-x2)+Math.abs(y1-y2));
}
// Add node to open list
function addnode(id, x, y, prev, acc)
{
var g=acc; // Cost to get here
var h=manhattan_cost(x, y, dx, dy);
var f=g+h; // Final cost
openlist.push({id:id, x:x, y:y, p:prev, g:g, h:h, f:f});
}
// Find the node on the open list with the lowest value
function findcheapestopenlist()
{
var cost=-1;
var idx=-1;
for (var i=0; i<openlist.length; i++)
{
if ((cost==-1) || (openlist[i].f<cost))
{
cost=openlist[i].f;
idx=i;
}
}
return openlist[idx];
}
// Get index to node id on given list
function getidx(givenlist, id)
{
for (var i=0; i<givenlist.length; i++)
if (givenlist[i].id==id) return i;
return -1;
}
// Is given node id on the openlist
function isopen(id)
{
for (var i=0; i<openlist.length; i++)
if (openlist[i].id==id) return true;
return false;
}
// Is given node id on the closedlist
function isclosed(id)
{
for (var i=0; i<closedlist.length; i++)
if (closedlist[i].id==id) return true;
return false;
}
// Move given node from open to closed list
function movetoclosedlist(id)
{
// Find id in openlist list
for (var i=0; i<openlist.length; i++)
if (openlist[i].id==id)
{
closedlist.push(JSON.parse(JSON.stringify(openlist[i])));
openlist.splice(i, 1);
return;
}
}
// Find parent of given node, to backtrace path
function findparent(id)
{
var i;
for (i=0; i<closedlist.length; i++)
if (closedlist[i].id==id)
return closedlist[i].p;
for (i=0; i<openlist.length; i++)
if (openlist[i].id==id)
return openlist[i].p;
return -1;
}
// Retrace path back to start position
function retracepath(dest)
{
var finalpath=[];
// Check for path being found
if (n.id==dest)
{
var prev=findparent(dest);
finalpath.unshift(dest);
while (prev!=-1)
{
finalpath.unshift(prev);
prev=findparent(prev);
}
}
return finalpath;
}
// Add source to openlist list
addnode(src, nx, ny, -1, 0);
n=findcheapestopenlist();
// While openlist list has nodes to search
while ((n.id!=dest) && (openlist.length>0))
{
// Set n to cheapest node from openlist list
n=findcheapestopenlist();
// Check if n is the target node
if (n.id==dest) break;
// Check for unexplored nodes connecting to n
[
[0, -1], // Above
[1, 0], // Right
[0, 1], // Below
[-1 , 0] // Left
].forEach(function(dir)
{
c=n.id+(dir[0]+(dir[1]*gs.width));
cx=n.x+dir[0];
cy=n.y+dir[1];
// If it's a valid square, not solid, nor on any list, then add it
if (!issolid(cx, cy) && (!isopen(c)) && (!isclosed(c)))
addnode(c, cx, cy, n.id, n.f+1); // Add to open list
// NOTE : No cheaper path replacements done. This is when a cheaper path
// is found to get to a certain point already visited. If found the costs
// and parent data should be updated.
});
// Move n to the closedlist list
movetoclosedlist(n.id);
}
return retracepath(dest);
}