@@ -23,7 +23,9 @@ var astar = {
23
23
return node . f ;
24
24
} ) ;
25
25
} ,
26
- search : function ( grid , start , end , diagonal , heuristic ) {
26
+ search : function ( grid , start , end , diagonal , heuristic , costDiagonal , costStraight ) {
27
+ costDiagonal = costDiagonal || 1 ;
28
+ costStraight = costStraight || 1 ;
27
29
astar . init ( grid ) ;
28
30
heuristic = heuristic || astar . manhattan ;
29
31
diagonal = ! ! diagonal ;
@@ -72,7 +74,7 @@ var astar = {
72
74
// Found an optimal (so far) path to this node. Take score for node to see how good it is.
73
75
neighbor . visited = true ;
74
76
neighbor . parent = currentNode ;
75
- neighbor . h = neighbor . h || heuristic ( neighbor . pos , end . pos ) ;
77
+ neighbor . h = neighbor . h || heuristic ( neighbor . pos , end . pos , costStraight , costDiagonal ) ;
76
78
neighbor . g = gScore ;
77
79
neighbor . f = neighbor . g + neighbor . h ;
78
80
@@ -98,6 +100,11 @@ var astar = {
98
100
var d2 = Math . abs ( pos1 . y - pos0 . y ) ;
99
101
return d1 + d2 ;
100
102
} ,
103
+ diagonal : function ( pos0 , pos1 , D , D2 ) {
104
+ var d1 = Math . abs ( pos1 . x - pos0 . x ) ;
105
+ var d2 = Math . abs ( pos1 . y - pos0 . y ) ;
106
+ return ( D * ( d1 + d2 ) ) + ( ( D2 - ( 2 * D ) ) * Math . min ( d1 , d2 ) ) ;
107
+ } ,
101
108
neighbors : function ( grid , node , diagonals ) {
102
109
var ret = [ ] ;
103
110
var x = node . x ;
0 commit comments