@@ -25,16 +25,27 @@ public GoalBasedPathfinder(IMapGrid mapGrid, IHeuristic heuristic)
2525 _heuristic = heuristic ;
2626 }
2727
28+ public GoalBasedPathfinder ( IMapGrid mapGrid , IHeuristic heuristic , BrushFire brushfire ) : this ( mapGrid , heuristic )
29+ {
30+ CacheBrushFire ( brushfire , brushfire . Origin ) ;
31+ }
32+
33+
2834 private BrushFire CacheBrushFire ( BrushFire brushFire , ( short X , short Y ) start )
2935 {
3036 Node ? GetParent ( ( short X , short Y ) currentnode )
3137 {
32- var neighbor = _mapGrid . GetNeighbors ( currentnode ) . Select ( s => new Node ( ( s . X , s . Y ) , brushFire . Grid [ ( s . X , s . Y ) ] ? . Value ?? 0 ) ) . OrderBy ( s => s . Value ) . FirstOrDefault ( ) ;
38+ var neighbor = _mapGrid . GetNeighbors ( currentnode ) . Select ( s => new Node ( ( s . X , s . Y ) , brushFire . Grid . ContainsKey ( ( s . X , s . Y ) ) ? brushFire . Grid [ ( s . X , s . Y ) ] ? . Value ?? 0 : 0 ) ) . OrderBy ( s => s . Value ) . FirstOrDefault ( ) ;
3339 if ( ! ( neighbor is { } neighborCell ) )
3440 {
3541 return null ;
3642 }
3743
44+ if ( ! brushFire . Grid . ContainsKey ( ( neighborCell . Position . X , neighborCell . Position . Y ) ) )
45+ {
46+ brushFire . Grid . Add ( ( neighborCell . Position . X , neighborCell . Position . Y ) , new Node ( neighborCell . Position , null ) ) ;
47+ }
48+
3849 brushFire . Grid [ ( neighborCell . Position . X , neighborCell . Position . Y ) ] ??= new Node ( neighborCell . Position , null ) ;
3950
4051 if ( neighborCell . Value > 0 && brushFire . Grid [ ( neighborCell . Position . X , neighborCell . Position . Y ) ] ! . Closed == false )
@@ -59,10 +70,10 @@ private BrushFire CacheBrushFire(BrushFire brushFire, (short X, short Y) start)
5970
6071 public IEnumerable < ( short X , short Y ) > FindPath ( ( short X , short Y ) start , ( short X , short Y ) end )
6172 {
62- List < ( short X , short Y ) > list = new List < ( short X , short Y ) > ( ) ;
73+ List < ( short X , short Y ) > list = new ( ) ;
6374 BrushFirecache . TryGetValue ( end , out BrushFire ? brushFireOut ) ;
6475
65- if ( ! _mapGrid . IsWalkable ( start . X , start . Y ) || ! _mapGrid . IsWalkable ( end . X , end . Y ) )
76+ if ( ! _mapGrid . IsWalkable ( start . X , start . Y ) || ! _mapGrid . IsWalkable ( end . X , end . Y ) || ( brushFireOut != null && ! brushFireOut . Value . Grid . ContainsKey ( ( start . X , start . Y ) ) ) )
6677 {
6778 return list ;
6879 }
0 commit comments