1
1
// javascript-astar
2
2
// http://github.com/bgrins/javascript-astar
3
3
// Freely distributable under the MIT License.
4
- // Includes Binary Heap (with modifications) from Marijn Haverbeke.
4
+ // Includes Binary Heap (with modifications) from Marijn Haverbeke.
5
5
// http://eloquentjavascript.net/appendix2.html
6
6
7
7
8
- var GraphNodeType = {
9
- OPEN : 1 ,
10
- WALL : 0
8
+ var GraphNodeType = {
9
+ OPEN : 1 ,
10
+ WALL : 0
11
11
} ;
12
12
13
13
// Creates a Graph class used in the astar search algorithm.
@@ -16,7 +16,7 @@ function Graph(grid) {
16
16
17
17
for ( var x = 0 ; x < grid . length ; x ++ ) {
18
18
nodes [ x ] = [ ] ;
19
-
19
+
20
20
for ( var y = 0 , row = grid [ x ] ; y < row . length ; y ++ ) {
21
21
nodes [ x ] [ y ] = new GraphNode ( x , y , row [ y ] ) ;
22
22
}
@@ -46,7 +46,7 @@ function GraphNode(x,y,type) {
46
46
this . x = x ;
47
47
this . y = y ;
48
48
this . pos = {
49
- x : x ,
49
+ x : x ,
50
50
y : y
51
51
} ;
52
52
this . type = type ;
@@ -57,7 +57,7 @@ GraphNode.prototype.toString = function() {
57
57
} ;
58
58
59
59
GraphNode . prototype . isWall = function ( ) {
60
- return this . type == GraphNodeType . WALL ;
60
+ return this . type === GraphNodeType . WALL ;
61
61
} ;
62
62
63
63
@@ -82,21 +82,21 @@ BinaryHeap.prototype = {
82
82
// If there are any elements left, put the end element at the
83
83
// start, and let it bubble up.
84
84
if ( this . content . length > 0 ) {
85
- this . content [ 0 ] = end ;
86
- this . bubbleUp ( 0 ) ;
85
+ this . content [ 0 ] = end ;
86
+ this . bubbleUp ( 0 ) ;
87
87
}
88
88
return result ;
89
89
} ,
90
90
remove : function ( node ) {
91
91
var i = this . content . indexOf ( node ) ;
92
-
92
+
93
93
// When it is found, the process seen in 'pop' is repeated
94
94
// to fill up the hole.
95
95
var end = this . content . pop ( ) ;
96
96
97
97
if ( i !== this . content . length - 1 ) {
98
98
this . content [ i ] = end ;
99
-
99
+
100
100
if ( this . scoreFunction ( end ) < this . scoreFunction ( node ) ) {
101
101
this . sinkDown ( i ) ;
102
102
}
@@ -140,22 +140,24 @@ BinaryHeap.prototype = {
140
140
var length = this . content . length ,
141
141
element = this . content [ n ] ,
142
142
elemScore = this . scoreFunction ( element ) ;
143
-
143
+
144
144
while ( true ) {
145
145
// Compute the indices of the child elements.
146
146
var child2N = ( n + 1 ) << 1 , child1N = child2N - 1 ;
147
147
// This is used to store the new position of the element,
148
148
// if any.
149
149
var swap = null ;
150
+ var child1Score ;
150
151
// If the first child exists (is inside the array)...
151
152
if ( child1N < length ) {
152
- // Look it up and compute its score.
153
- var child1 = this . content [ child1N ] ,
153
+ // Look it up and compute its score.
154
+ var child1 = this . content [ child1N ] ;
154
155
child1Score = this . scoreFunction ( child1 ) ;
155
156
156
- // If the score is less than our element's, we need to swap.
157
- if ( child1Score < elemScore )
158
- swap = child1N ;
157
+ // If the score is less than our element's, we need to swap.
158
+ if ( child1Score < elemScore ) {
159
+ swap = child1N ;
160
+ }
159
161
}
160
162
161
163
// Do the same checks for the other child.
0 commit comments