-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
HierarchicalPathFinder.cs
1275 lines (1124 loc) · 53.2 KB
/
HierarchicalPathFinder.cs
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#region Copyright & License Information
/*
* Copyright (c) The OpenRA Developers and Contributors
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version. For more
* information, see COPYING.
*/
#endregion
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using OpenRA.Mods.Common.Traits;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Pathfinder
{
/// <summary>
/// Provides pathfinding abilities for actors that use a specific <see cref="Locomotor"/>.
/// Maintains a hierarchy of abstract graphs that provide a more accurate heuristic function during
/// A* pathfinding than the one available from <see cref="PathSearch.DefaultCostEstimator(Locomotor)"/>.
/// This allows for faster pathfinding.
/// </summary>
/// <remarks>
/// <para>The goal of this pathfinder is to increase performance of path searches. <see cref="PathSearch"/> is used
/// to perform a path search as usual, but a different heuristic function is provided that is more accurate. This
/// means fewer nodes have to be explored during the search, resulting in a performance increase.</para>
///
/// <para>When an A* path search is performed, the search expands outwards from the source location until the
/// target is found. The heuristic controls how this expansion occurs. When the heuristic of h(n) = 0 is given, we
/// get Dijkstra's algorithm. The search grows outwards from the source node in an expanding circle with no sense
/// of direction. This will find the shortest path by brute force. It will explore many nodes during the search,
/// including lots of nodes in the opposite direction to the target.</para>
///
/// <para><see cref="PathSearch.DefaultCostEstimator(Locomotor)"/> provides heuristic for searching a 2D grid. It
/// estimates the cost as the straight-line distance between the source and target nodes. The search grows in a
/// straight line towards the target node. This is a vast improvement over Dijkstra's algorithm as we now
/// prioritize exploring nodes that lie closer to the target, rather than exploring nodes that take us away from
/// the target.</para>
///
/// <para>This default straight-line heuristic still has drawbacks - it is unaware of the obstacles on the grid. If
/// the route to be found requires steering around obstacles then this heuristic can perform badly. Imagine a path
/// that must steer around a lake, or move back on itself to get out of a dead end. In these cases the straight-line
/// heuristic moves blindly towards the target, when actually the path requires that we move sidewards or even
/// backwards to find a route. When this occurs then the straight-line heuristic ends up exploring nodes that
/// aren't useful - they lead us into dead ends or directly into an obstacle that we need to go around instead.
/// </para>
///
/// <para>The <see cref="HierarchicalPathFinder"/> improves the heuristic by making it aware of unreachable map
/// terrain. A "low-resolution" version of the map is maintained, and used to provide an initial route. When the
/// search is conducted it explores along this initial route. This allows the search to "know" it needs to go
/// sideways around the lake or backwards out of the dead-end, meaning we can explore even fewer nodes.</para>
///
/// <para>The "low-resolution" version of the map is referred to as the abstract graph. The abstract graph is
/// created by dividing the map up into a series of grids, of say 10x10 nodes. Within each grid, we determine the
/// connected regions of nodes within that grid. If all the nodes within the grid connect to each other, we have
/// one such region. If they are split up by impassable terrain then we may have two or more regions within the
/// grid. Every region will be represented by one node in the abstract graph (an abstract node, for short).</para>
///
/// <para>When a path search is to be performed, we first perform a A* search on the abstract graph with the
/// <see cref="PathSearch.DefaultCostEstimator(Locomotor)"/>. This graph is much smaller than the full map, so
/// this search is quick. The resulting path gives us the initial route between each abstract node. We can then use
/// this to create the improved heuristic for use on the path search on the full resolution map. When determining
/// the cost for the node, we can use the straight-line distance towards the next abstract node as our estimate.
/// Our search is therefore guided along the initial route.</para>
///
/// <para>This implementation only maintains one level of abstract graph, but a hierarchy of such graphs is
/// possible. This allows the top-level and lowest resolution graph to be as small as possible - important because
/// it will be searched using the dumbest heuristic. Each level underneath is higher-resolution and contains more
/// nodes, but uses a heuristic informed from the previous level to guide the search in the right direction.</para>
///
/// <para>This implementation is aware of movement costs over terrain given by
/// <see cref="Locomotor.MovementCostToEnterCell(Actor, CPos, CPos, BlockedByActor, Actor, bool)"/>. It is aware of
/// changes to the costs in terrain and able to update the abstract graph when this occurs. It is able to search
/// the abstract graph as if <see cref="BlockedByActor.None"/> had been specified. If
/// <see cref="BlockedByActor.Immovable"/> is given in the constructor, the abstract graph will additionally
/// account for a subset of immovable actors using the same rules as
/// <see cref="Locomotor.CanMoveFreelyInto(Actor, CPos, SubCell, BlockedByActor, Actor, bool)"/>. It will be aware
/// of changes to actors on the map and update the abstract graph when this occurs. Other types of blocking actors
/// will not be accounted for in the heuristic.</para>
///
/// <para>If the obstacle on the map is from terrain (e.g. a cliff or lake) the heuristic will work well. If the
/// obstacle is from the subset of immovable actors (e.g. trees, walls, buildings) and
/// <see cref="BlockedByActor.Immovable"/> was given, the heuristic will work well. If the obstacle is from other
/// actors (e.g. units) then the heuristic is unaware of these. Therefore the same problem where the search goes in
/// the wrong direction is possible, e.g. through a choke-point that has units blocking it. In this scenario the
/// performance benefit will be lost, as the search will have to explore more nodes until it can get around the
/// obstacle.</para>
///
/// <para>In summary, the <see cref="HierarchicalPathFinder"/> reduces the performance impact of path searches that
/// must go around terrain, and some types of actor, but does not improve performance of searches that must go
/// around the remaining types of actor.</para>
/// </remarks>
public sealed class HierarchicalPathFinder
{
// This value determined via empiric testing as the best performance trade-off.
const int GridSize = 10;
readonly World world;
readonly Locomotor locomotor;
readonly IActorMap actorMap;
readonly Func<CPos, CPos, int> costEstimator;
readonly HashSet<int> dirtyGridIndexes = new();
readonly HashSet<CPos> cellsWithBlockingActor;
Grid mapBounds;
int gridXs;
int gridYs;
/// <summary>
/// Index by a <see cref="GridIndex"/>.
/// </summary>
GridInfo[] gridInfos;
/// <summary>
/// The abstract graph is represented here.
/// An abstract node is the key, and costs to other abstract nodes are then available.
/// Abstract nodes with no connections are NOT present in the graph.
/// A lookup will fail, rather than return an empty list.
/// </summary>
Dictionary<CPos, List<GraphConnection>> abstractGraph;
/// <summary>
/// The abstract domains are represented here.
/// An abstract node is the key, and a domain index is given.
/// If the domain index of two nodes is equal, a path exists between them (ignoring all blocking actors).
/// If unequal, no path is possible.
/// </summary>
readonly Dictionary<CPos, uint> abstractDomains;
/// <summary>
/// Knows about the abstract nodes within a grid. Can map a local cell to its abstract node.
/// </summary>
readonly struct GridInfo
{
readonly CPos?[] singleAbstractCellForLayer;
readonly Dictionary<CPos, CPos> localCellToAbstractCell;
public GridInfo(CPos?[] singleAbstractCellForLayer, Dictionary<CPos, CPos> localCellToAbstractCell)
{
this.singleAbstractCellForLayer = singleAbstractCellForLayer;
this.localCellToAbstractCell = localCellToAbstractCell;
}
/// <summary>
/// Maps a local cell to a abstract node in the graph.
/// Returns null when the local cell is unreachable.
/// Pass a null <paramref name="hpf"/> to skip cost checks if the caller already checked.
/// </summary>
public CPos? AbstractCellForLocalCell(CPos localCell, HierarchicalPathFinder hpf)
{
var abstractCell = singleAbstractCellForLayer[localCell.Layer];
if (abstractCell != null)
{
// All reachable cells in the grid are joined together so only a single abstract cell was needed,
// but there may be unreachable cells in the grid which we must exclude.
if (hpf != null && !hpf.CellIsAccessible(localCell))
return null;
return abstractCell;
}
// Only reachable cells would be populated in the lookup, so no need to check their cost.
if (localCellToAbstractCell.TryGetValue(localCell, out var abstractCellFromMap))
return abstractCellFromMap;
return null;
}
public void CopyAbstractCellsInto(HashSet<CPos> set)
{
foreach (var single in singleAbstractCellForLayer)
if (single != null)
set.Add(single.Value);
foreach (var cell in localCellToAbstractCell.Values)
set.Add(cell);
}
}
/// <summary>
/// Represents an abstract graph with some extra edges inserted.
/// Instead of building a new dictionary with the edges added, we build a supplemental dictionary of changes.
/// This is to avoid copying the entire abstract graph.
/// </summary>
sealed class AbstractGraphWithInsertedEdges
{
readonly Dictionary<CPos, List<GraphConnection>> abstractEdges;
readonly Dictionary<CPos, IEnumerable<GraphConnection>> changedEdges;
public AbstractGraphWithInsertedEdges(
Dictionary<CPos, List<GraphConnection>> abstractEdges,
IList<GraphEdge> sourceEdges,
GraphEdge? targetEdge,
Func<CPos, CPos, int> costEstimator)
{
this.abstractEdges = abstractEdges;
changedEdges = new Dictionary<CPos, IEnumerable<GraphConnection>>(sourceEdges.Count * 9 + (targetEdge != null ? 9 : 0));
foreach (var sourceEdge in sourceEdges)
InsertEdgeAsBidirectional(sourceEdge, costEstimator);
if (targetEdge != null)
InsertEdgeAsBidirectional(targetEdge.Value, costEstimator);
}
void InsertEdgeAsBidirectional(GraphEdge edge, Func<CPos, CPos, int> costEstimator)
{
InsertConnections(edge.Source, edge.Destination, costEstimator);
}
void InsertConnections(CPos localCell, CPos abstractCell, Func<CPos, CPos, int> costEstimator)
{
if (!abstractEdges.TryGetValue(abstractCell, out var edges))
edges = new List<GraphConnection>();
changedEdges[localCell] = edges
.Select(e => new GraphConnection(e.Destination, costEstimator(localCell, e.Destination)))
.Append(new GraphConnection(abstractCell, costEstimator(localCell, abstractCell)));
IEnumerable<GraphConnection> abstractChangedEdges = edges;
if (changedEdges.TryGetValue(abstractCell, out var existingEdges))
abstractChangedEdges = existingEdges;
changedEdges[abstractCell] = abstractChangedEdges
.Append(new GraphConnection(localCell, costEstimator(abstractCell, localCell)));
foreach (var conn in edges)
{
IEnumerable<GraphConnection> connChangedEdges;
if (changedEdges.TryGetValue(conn.Destination, out var existingConnEdges))
connChangedEdges = existingConnEdges;
else
connChangedEdges = abstractEdges[conn.Destination];
changedEdges[conn.Destination] = connChangedEdges
.Append(new GraphConnection(localCell, costEstimator(conn.Destination, localCell)));
}
}
public List<GraphConnection> GetConnections(CPos position)
{
if (changedEdges.TryGetValue(position, out var changedEdge))
return changedEdge.ToList();
if (abstractEdges.TryGetValue(position, out var abstractEdge))
return abstractEdge;
return new List<GraphConnection>();
}
}
public HierarchicalPathFinder(World world, Locomotor locomotor, IActorMap actorMap, BlockedByActor check)
{
this.world = world;
this.locomotor = locomotor;
this.actorMap = actorMap;
if (locomotor.Info.TerrainSpeeds.Count == 0)
return;
if (check == BlockedByActor.Immovable)
{
// When we account for immovable actors, it depends on the actors on the map.
// When this changes, we must update the cost table.
actorMap.CellUpdated += RequireBlockingRefreshInCell;
// Determine immovable cells from actors already on the map.
cellsWithBlockingActor = actorMap.AllActors()
.Where(ActorIsBlocking)
.SelectMany(a =>
a.OccupiesSpace.OccupiedCells()
.Select(oc => oc.Cell)
.Where(c => ActorCellIsBlocking(a, c)))
.ToHashSet();
}
else if (check != BlockedByActor.None)
throw new System.ComponentModel.InvalidEnumArgumentException(
$"{nameof(HierarchicalPathFinder)} supports {nameof(BlockedByActor.None)} " +
$"and {nameof(BlockedByActor.Immovable)} only for {nameof(check)}");
costEstimator = PathSearch.DefaultCostEstimator(locomotor);
BuildGrids();
BuildCostTable();
abstractDomains = new Dictionary<CPos, uint>(gridXs * gridYs);
RebuildDomains();
// When we build the cost table, it depends on the movement costs of the cells at that time.
// When this changes, we must update the cost table.
locomotor.CellCostChanged += RequireCostRefreshInCell;
// If the map projection changes, the result of Map.Contains(CPos) may change.
// We need to rebuild grids to account for this possibility.
this.world.Map.CellProjectionChanged += RequireProjectionRefreshInCell;
}
public (
IReadOnlyDictionary<CPos, List<GraphConnection>> AbstractGraph,
IReadOnlyDictionary<CPos, uint> AbstractDomains) GetOverlayData()
{
if (costEstimator == null)
return default;
// Ensure the abstract graph and domains are up to date when using the overlay.
RebuildDirtyGrids();
RebuildDomains();
return (
new ReadOnlyDictionary<CPos, List<GraphConnection>>(abstractGraph),
new ReadOnlyDictionary<CPos, uint>(abstractDomains));
}
/// <summary>
/// Divides the map area up into a series of grids.
/// </summary>
void BuildGrids()
{
static Grid GetCPosBounds(Map map)
{
if (map.Grid.Type == MapGridType.RectangularIsometric)
{
var bottomRight = map.AllCells.BottomRight;
var bottomRightU = bottomRight.ToMPos(map).U;
return new Grid(
new CPos(0, -bottomRightU),
new CPos(bottomRight.X + 1, bottomRight.Y + bottomRightU + 1),
false);
}
return new Grid(CPos.Zero, (CPos)map.MapSize, false);
}
mapBounds = GetCPosBounds(world.Map);
gridXs = Exts.IntegerDivisionRoundingAwayFromZero(mapBounds.Width, GridSize);
gridYs = Exts.IntegerDivisionRoundingAwayFromZero(mapBounds.Height, GridSize);
var customMovementLayers = world.GetCustomMovementLayers();
gridInfos = new GridInfo[gridXs * gridYs];
for (var gridX = mapBounds.TopLeft.X; gridX < mapBounds.BottomRight.X; gridX += GridSize)
for (var gridY = mapBounds.TopLeft.Y; gridY < mapBounds.BottomRight.Y; gridY += GridSize)
gridInfos[GridIndex(new CPos(gridX, gridY))] = BuildGrid(gridX, gridY, customMovementLayers);
}
/// <summary>
/// Determines the abstract nodes within a single grid. One abstract node will be created for each set of cells
/// that are reachable from each other within the grid area. A grid with open terrain will commonly have one
/// abstract node. If impassable terrain such as cliffs or water divides the cells into 2 or more distinct
/// regions, one abstract node is created for each region. We also remember which cells belong to which
/// abstract node. Given a local cell, this allows us to determine which abstract node it belongs to.
/// </summary>
GridInfo BuildGrid(int gridX, int gridY, ICustomMovementLayer[] customMovementLayers)
{
var singleAbstractCellForLayer = new CPos?[customMovementLayers.Length];
var localCellToAbstractCell = new Dictionary<CPos, CPos>();
// When accounting for immovable actors, use a custom cost so those cells become invalid paths.
var customCost = cellsWithBlockingActor == null
? (Func<CPos, int>)null
: c => cellsWithBlockingActor.Contains(c) ? PathGraph.PathCostForInvalidPath : 0;
var accessibleCells = new HashSet<CPos>(GridSize * GridSize);
for (byte gridLayer = 0; gridLayer < customMovementLayers.Length; gridLayer++)
{
if (gridLayer != 0 &&
(customMovementLayers[gridLayer] == null ||
!customMovementLayers[gridLayer].EnabledForLocomotor(locomotor.Info)))
continue;
var grid = GetGrid(new CPos(gridX, gridY, gridLayer), mapBounds);
for (var y = gridY; y < grid.BottomRight.Y; y++)
{
for (var x = gridX; x < grid.BottomRight.X; x++)
{
var cell = new CPos(x, y, gridLayer);
if (CellIsAccessible(cell))
accessibleCells.Add(cell);
}
}
static CPos AbstractCellForLocalCells(List<CPos> cells, byte layer)
{
var minX = int.MaxValue;
var minY = int.MaxValue;
var maxX = int.MinValue;
var maxY = int.MinValue;
foreach (var cell in cells)
{
minX = Math.Min(cell.X, minX);
minY = Math.Min(cell.Y, minY);
maxX = Math.Max(cell.X, maxX);
maxY = Math.Max(cell.Y, maxY);
}
var regionWidth = maxX - minX;
var regionHeight = maxY - minY;
var desired = new CPos(minX + regionWidth / 2, minY + regionHeight / 2, layer);
// Make sure the abstract cell is one of the available local cells.
// This ensures each abstract cell we generate is unique.
// We'll choose an abstract node as close to the middle of the region as possible.
var abstractCell = desired;
var distance = int.MaxValue;
foreach (var cell in cells)
{
var newDistance = (cell - desired).LengthSquared;
if (distance > newDistance)
{
distance = newDistance;
abstractCell = cell;
}
}
return abstractCell;
}
// Flood fill the search area from one of the accessible cells.
// We can use this to determine the connected regions within the grid.
// Each region we discover will be represented by an abstract node.
// Repeat this process until all disjoint regions are discovered.
var hasPopulatedAbstractCellForLayer = false;
while (accessibleCells.Count > 0)
{
var src = accessibleCells.First();
using (var search = GetLocalPathSearch(
null, new[] { src }, src, customCost, null, BlockedByActor.None, false, grid, 100))
{
var localCellsInRegion = search.ExpandAll();
var abstractCell = AbstractCellForLocalCells(localCellsInRegion, gridLayer);
accessibleCells.ExceptWith(localCellsInRegion);
// PERF: If there is only one distinct region of cells,
// we can represent this grid with one abstract cell.
// We don't need to remember how to map back from a local cell to an abstract cell.
if (!hasPopulatedAbstractCellForLayer && accessibleCells.Count == 0)
singleAbstractCellForLayer[gridLayer] = abstractCell;
else
{
// When there is more than one region within the grid
// (imagine a wall or stream separating the grid into disjoint areas)
// then we will remember a mapping from local cells to each of their abstract cells.
hasPopulatedAbstractCellForLayer = true;
foreach (var localCell in localCellsInRegion)
localCellToAbstractCell.Add(localCell, abstractCell);
}
}
}
}
return new GridInfo(singleAbstractCellForLayer, localCellToAbstractCell);
}
/// <summary>
/// Builds the abstract graph in entirety. The abstract graph contains edges between all the abstract nodes
/// that represent the costs to move between them.
/// </summary>
void BuildCostTable()
{
abstractGraph = new Dictionary<CPos, List<GraphConnection>>(gridXs * gridYs);
var customMovementLayers = world.GetCustomMovementLayers();
for (var gridX = mapBounds.TopLeft.X; gridX < mapBounds.BottomRight.X; gridX += GridSize)
for (var gridY = mapBounds.TopLeft.Y; gridY < mapBounds.BottomRight.Y; gridY += GridSize)
foreach (var edges in GetAbstractEdgesForGrid(gridX, gridY, customMovementLayers))
abstractGraph.Add(edges.Key, edges.Value);
}
/// <summary>
/// For a given grid, determines the edges between the abstract nodes within the grid and the abstract nodes
/// within adjacent grids on the same layer. Also determines any edges available to grids on other layers via
/// custom movement layers.
/// </summary>
IEnumerable<KeyValuePair<CPos, List<GraphConnection>>> GetAbstractEdgesForGrid(int gridX, int gridY, ICustomMovementLayer[] customMovementLayers)
{
var abstractEdges = new HashSet<(CPos Src, CPos Dst)>();
for (byte gridLayer = 0; gridLayer < customMovementLayers.Length; gridLayer++)
{
if (gridLayer != 0 &&
(customMovementLayers[gridLayer] == null ||
!customMovementLayers[gridLayer].EnabledForLocomotor(locomotor.Info)))
continue;
void AddEdgesIfMovementAllowedBetweenCells(CPos cell, CPos candidateCell)
{
if (!MovementAllowedBetweenCells(cell, candidateCell))
return;
var abstractCell = AbstractCellForLocalCellNoAccessibleCheck(cell);
if (abstractCell == null)
return;
var abstractCellAdjacent = AbstractCellForLocalCellNoAccessibleCheck(candidateCell);
if (abstractCellAdjacent == null)
return;
abstractEdges.Add((abstractCell.Value, abstractCellAdjacent.Value));
}
// Searches along edges of all grids within a layer.
// Checks for the local edge cell if we can traverse to any of the three adjacent cells in the next grid.
// Builds connections in the abstract graph when any local cells have connections.
void AddAbstractEdges(int xIncrement, int yIncrement, CVec adjacentVec, int2 offset)
{
var startY = gridY + offset.Y;
var startX = gridX + offset.X;
for (var y = startY; y < startY + GridSize; y += yIncrement)
{
for (var x = startX; x < startX + GridSize; x += xIncrement)
{
var cell = new CPos(x, y, gridLayer);
if (!CellIsAccessible(cell))
continue;
var adjacentCell = cell + adjacentVec;
for (var i = -1; i <= 1; i++)
{
var candidateCell = adjacentCell + i * new CVec(adjacentVec.Y, adjacentVec.X);
AddEdgesIfMovementAllowedBetweenCells(cell, candidateCell);
}
}
}
}
// Searches all cells within a layer.
// Checks for the local cell if we can traverse from/to a custom movement layer.
// Builds connections in the abstract graph when any local cells have connections.
void AddAbstractCustomLayerEdges()
{
var gridCml = customMovementLayers[gridLayer];
for (byte candidateLayer = 0; candidateLayer < customMovementLayers.Length; candidateLayer++)
{
if (gridLayer == candidateLayer)
continue;
var candidateCml = customMovementLayers[candidateLayer];
if (candidateLayer != 0 && (candidateCml == null || !candidateCml.EnabledForLocomotor(locomotor.Info)))
continue;
for (var y = gridY; y < gridY + GridSize; y++)
{
for (var x = gridX; x < gridX + GridSize; x++)
{
var cell = new CPos(x, y, gridLayer);
if (!CellIsAccessible(cell))
continue;
CPos candidateCell;
if (gridLayer == 0)
{
candidateCell = new CPos(cell.X, cell.Y, candidateLayer);
if (candidateCml.EntryMovementCost(locomotor.Info, candidateCell) == PathGraph.MovementCostForUnreachableCell)
continue;
}
else
{
candidateCell = new CPos(cell.X, cell.Y, 0);
if (gridCml.ExitMovementCost(locomotor.Info, candidateCell) == PathGraph.MovementCostForUnreachableCell)
continue;
}
AddEdgesIfMovementAllowedBetweenCells(cell, candidateCell);
}
}
}
}
// Top, Left, Bottom, Right
AddAbstractEdges(1, GridSize, new CVec(0, -1), new int2(0, 0));
AddAbstractEdges(GridSize, 1, new CVec(-1, 0), new int2(0, 0));
AddAbstractEdges(1, GridSize, new CVec(0, 1), new int2(0, GridSize - 1));
AddAbstractEdges(GridSize, 1, new CVec(1, 0), new int2(GridSize - 1, 0));
AddAbstractCustomLayerEdges();
}
return abstractEdges
.GroupBy(edge => edge.Src)
.Select(group => KeyValuePair.Create(
group.Key,
group.Select(edge => new GraphConnection(edge.Dst, costEstimator(edge.Src, edge.Dst))).ToList()));
}
/// <summary>
/// When reachability changes for a cell, marks the grid it belongs to as out of date.
/// </summary>
void RequireCostRefreshInCell(CPos cell, short oldCost, short newCost)
{
// We don't care about the specific cost of the cell, just whether it is reachable or not.
// This is good because updating the table is expensive, so only having to update it when
// the reachability changes rather than for all costs changes saves us a lot of time.
if (oldCost == PathGraph.MovementCostForUnreachableCell ^ newCost == PathGraph.MovementCostForUnreachableCell)
dirtyGridIndexes.Add(GridIndex(cell));
}
bool CellIsAccessible(CPos cell)
{
return locomotor.MovementCostForCell(cell) != PathGraph.MovementCostForUnreachableCell &&
(cellsWithBlockingActor == null || !cellsWithBlockingActor.Contains(cell));
}
bool MovementAllowedBetweenCells(CPos accessibleSrcCell, CPos destCell)
{
return locomotor.MovementCostToEnterCell(
null, accessibleSrcCell, destCell, BlockedByActor.None, null) != PathGraph.MovementCostForUnreachableCell &&
(cellsWithBlockingActor == null || !cellsWithBlockingActor.Contains(destCell));
}
/// <summary>
/// When actors change for a cell, marks the grid it belongs to as out of date.
/// </summary>
void RequireBlockingRefreshInCell(CPos cell)
{
var cellHasBlockingActor = false;
var actors = actorMap.GetActorsAt(cell);
foreach (var actor in actors)
{
if (ActorIsBlocking(actor) && ActorCellIsBlocking(actor, cell))
{
cellHasBlockingActor = true;
break;
}
}
if (cellHasBlockingActor)
{
if (cellsWithBlockingActor.Add(cell))
dirtyGridIndexes.Add(GridIndex(cell));
}
else
{
if (cellsWithBlockingActor.Remove(cell))
dirtyGridIndexes.Add(GridIndex(cell));
}
}
/// <summary>
/// When map projection changes for a cell, marks the grid it belongs to as out of date.
/// </summary>
void RequireProjectionRefreshInCell(CPos cell)
{
dirtyGridIndexes.Add(GridIndex(cell));
}
/// <summary>
/// <see cref="BlockedByActor.Immovable"/> defines immovability based on the mobile trait. The blocking rules
/// in <see cref="Locomotor.CanMoveFreelyInto(Actor, CPos, SubCell, BlockedByActor, Actor, bool)"/> allow units
/// to pass these immovable actors if they are temporary blockers (e.g. gates) or crushable by the locomotor.
/// Since our abstract graph must work for any actor, we have to be conservative and can only consider a subset
/// of the immovable actors in the graph - ones we know cannot be passed by some actors due to these rules.
/// Both this and <see cref="ActorCellIsBlocking"/> must be true for a cell to be blocked.
///
/// This method is dependant on the logic in
/// <see cref="Locomotor.CanMoveFreelyInto(Actor, CPos, SubCell, BlockedByActor, Actor, bool)"/> and
/// <see cref="Locomotor.UpdateCellBlocking"/>. This method must be kept in sync with changes in the locomotor
/// rules.
/// </summary>
bool ActorIsBlocking(Actor actor)
{
var isMovable = actor.OccupiesSpace is Mobile mobile && !mobile.IsTraitDisabled && !mobile.IsTraitPaused && !mobile.IsImmovable;
if (isMovable)
return false;
var isTemporaryBlocker = world.RulesContainTemporaryBlocker && actor.TraitOrDefault<ITemporaryBlocker>() != null;
if (isTemporaryBlocker)
return false;
var crushables = actor.TraitsImplementing<ICrushable>();
foreach (var crushable in crushables)
if (world.NoPlayersMask != crushable.CrushableBy(actor, locomotor.Info.Crushes))
return false;
return true;
}
/// <summary>
/// The blocking rules additionally allow some cells to be considered passable even if the actor is blocking.
/// A cell is passable if the locomotor can share the cell and a subcell is available. It is also passable if
/// it is a transit only cell of a <see cref="Building"/>. We cannot consider these cells to be blocked.
/// Both this and <see cref="ActorIsBlocking"/> must be true for a cell to be blocked.
/// </summary>
bool ActorCellIsBlocking(Actor actor, CPos cell)
{
var canShareCell = locomotor.Info.SharesCell && actorMap.HasFreeSubCell(cell);
if (canShareCell)
return false;
var isTransitOnly = actor.OccupiesSpace is Building building && building.TransitOnlyCells().Contains(cell);
if (isTransitOnly)
return false;
return true;
}
int GridIndex(CPos cellInGrid)
{
return
(cellInGrid.Y - mapBounds.TopLeft.Y) / GridSize * gridXs +
(cellInGrid.X - mapBounds.TopLeft.X) / GridSize;
}
CPos GetGridTopLeft(int gridIndex, byte layer)
{
return new CPos(
gridIndex % gridXs * GridSize + mapBounds.TopLeft.X,
gridIndex / gridXs * GridSize + mapBounds.TopLeft.Y,
layer);
}
static CPos GetGridTopLeft(CPos cellInGrid, Grid mapBounds)
{
return new CPos(
(cellInGrid.X - mapBounds.TopLeft.X) / GridSize * GridSize + mapBounds.TopLeft.X,
(cellInGrid.Y - mapBounds.TopLeft.Y) / GridSize * GridSize + mapBounds.TopLeft.Y,
cellInGrid.Layer);
}
static Grid GetGrid(CPos cellInGrid, Grid mapBounds)
{
var gridTopLeft = GetGridTopLeft(cellInGrid, mapBounds);
var width = Math.Min(mapBounds.BottomRight.X - gridTopLeft.X, GridSize);
var height = Math.Min(mapBounds.BottomRight.Y - gridTopLeft.Y, GridSize);
return new Grid(
gridTopLeft,
gridTopLeft + new CVec(width, height),
true);
}
/// <summary>
/// Calculates a path for the actor from multiple possible sources to target, using a unidirectional search.
/// Returned path is *reversed* and given target to source.
/// The actor must use the same <see cref="Locomotor"/> as this <see cref="HierarchicalPathFinder"/>.
/// </summary>
public List<CPos> FindPath(Actor self, IReadOnlyCollection<CPos> sources, CPos target,
BlockedByActor check, int heuristicWeightPercentage, Func<CPos, int> customCost,
Actor ignoreActor, bool laneBias, PathFinderOverlay pathFinderOverlay)
{
if (costEstimator == null)
return PathFinder.NoPath;
pathFinderOverlay?.NewRecording(self, sources, target);
if (!world.Map.Contains(target))
return PathFinder.NoPath;
RebuildDirtyGrids();
var targetAbstractCell = AbstractCellForLocalCell(target);
if (targetAbstractCell == null)
return PathFinder.NoPath;
// Unlike the target cell, the source cell is allowed to be an unreachable location.
// Instead, what matters is whether any cell adjacent to the source cell can be reached.
var sourcesWithReachableNodes = new List<(CPos Source, CPos AdjacentSource)>(sources.Count);
var sourceEdges = new List<GraphEdge>(sources.Count);
foreach (var source in sources)
{
if (!world.Map.Contains(source))
continue;
// The source cell is reachable, we can add an edge from there and have no need to check adjacent cells.
var sourceAbstractCell = AbstractCellForLocalCell(source);
if (sourceAbstractCell != null)
{
sourcesWithReachableNodes.Add((source, source));
var sourceEdge = EdgeFromLocalToAbstract(source, sourceAbstractCell.Value);
if (sourceEdge != null)
sourceEdges.Add(sourceEdge.Value);
continue;
}
// If the source cell is unreachable, we must add edges from any adjacent cells that are reachable instead.
foreach (var dir in CVec.Directions)
{
var adjacentSource = source + dir;
if (!MovementAllowedBetweenCells(source, adjacentSource))
continue;
var adjacentSourceAbstractCell = AbstractCellForLocalCell(adjacentSource);
if (adjacentSourceAbstractCell == null)
continue;
sourcesWithReachableNodes.Add((source, adjacentSource));
var sourceEdge = EdgeFromLocalToAbstract(adjacentSource, adjacentSourceAbstractCell.Value);
if (sourceEdge != null)
sourceEdges.Add(sourceEdge.Value);
}
}
if (sourcesWithReachableNodes.Count == 0)
return PathFinder.NoPath;
var targetEdge = EdgeFromLocalToAbstract(target, targetAbstractCell.Value);
// The new edges will be treated as bi-directional.
var fullGraph = new AbstractGraphWithInsertedEdges(abstractGraph, sourceEdges, targetEdge, costEstimator);
// Determine an abstract path to all sources, for use in a unidirectional search.
var estimatedSearchSize = (abstractGraph.Count + 2) / 8;
using (var reverseAbstractSearch = PathSearch.ToTargetCellOverGraph(
fullGraph.GetConnections, locomotor, target, target, estimatedSearchSize, pathFinderOverlay?.RecordAbstractEdges(self)))
{
var sourcesWithPathableNodes = new HashSet<CPos>(sources.Count);
List<CPos> unpathableNodes = null;
foreach (var (source, adjacentSource) in sourcesWithReachableNodes)
{
// Check if we have already found a route to this node before we attempt to expand the search.
var sourceStatus = reverseAbstractSearch.Graph[adjacentSource];
if (sourceStatus.Status == CellStatus.Closed)
{
if (sourceStatus.CostSoFar != PathGraph.PathCostForInvalidPath)
sourcesWithPathableNodes.Add(source);
else
{
unpathableNodes ??= new List<CPos>();
unpathableNodes.Add(adjacentSource);
}
}
else
{
reverseAbstractSearch.TargetPredicate = cell => cell == adjacentSource;
if (reverseAbstractSearch.ExpandToTarget())
sourcesWithPathableNodes.Add(source);
else
{
unpathableNodes ??= new List<CPos>();
unpathableNodes.Add(adjacentSource);
}
}
}
if (sourcesWithPathableNodes.Count == 0)
return PathFinder.NoPath;
using (var fromSrc = GetLocalPathSearch(
self, sourcesWithPathableNodes, target, customCost, ignoreActor, check, laneBias, null, heuristicWeightPercentage,
heuristic: Heuristic(reverseAbstractSearch, estimatedSearchSize, sourcesWithPathableNodes, unpathableNodes),
recorder: pathFinderOverlay?.RecordLocalEdges(self)))
return fromSrc.FindPath();
}
}
/// <summary>
/// Calculates a path for the actor from source to target, using a bidirectional search.
/// Returned path is *reversed* and given target to source.
/// The actor must use the same <see cref="Locomotor"/> as this <see cref="HierarchicalPathFinder"/>.
/// </summary>
public List<CPos> FindPath(Actor self, CPos source, CPos target,
BlockedByActor check, int heuristicWeightPercentage, Func<CPos, int> customCost,
Actor ignoreActor, bool laneBias, PathFinderOverlay pathFinderOverlay)
{
if (costEstimator == null)
return PathFinder.NoPath;
// If the source and target are close, see if they can be reached locally.
// This avoids the cost of an abstract search unless we need one.
const int CloseGridDistance = 2;
if ((target - source).LengthSquared < GridSize * GridSize * CloseGridDistance * CloseGridDistance && source.Layer == target.Layer)
{
var gridToSearch = new Grid(
new CPos(
Math.Min(source.X, target.X) - GridSize / 2,
Math.Min(source.Y, target.Y) - GridSize / 2,
source.Layer),
new CPos(
Math.Max(source.X, target.X) + GridSize / 2,
Math.Max(source.Y, target.Y) + GridSize / 2,
source.Layer),
false);
pathFinderOverlay?.NewRecording(self, new[] { source }, target);
List<CPos> localPath;
using (var search = GetLocalPathSearch(
self, new[] { source }, target, customCost, ignoreActor, check, laneBias, gridToSearch, heuristicWeightPercentage,
recorder: pathFinderOverlay?.RecordLocalEdges(self)))
localPath = search.FindPath();
if (localPath.Count > 0)
return localPath;
}
pathFinderOverlay?.NewRecording(self, new[] { source }, target);
RebuildDirtyGrids();
// If the target cell is unreachable, there is no path.
var targetAbstractCell = AbstractCellForLocalCell(target);
if (targetAbstractCell == null)
return PathFinder.NoPath;
// If the source cell is unreachable, there may still be a path.
// As long as one of the cells adjacent to the source is reachable, the path can be made.
// Call the other overload which can handle this scenario.
var sourceAbstractCell = AbstractCellForLocalCell(source);
if (sourceAbstractCell == null)
return FindPath(self, new[] { source }, target, check, heuristicWeightPercentage, customCost, ignoreActor, laneBias, pathFinderOverlay);
var targetEdge = EdgeFromLocalToAbstract(target, targetAbstractCell.Value);
var sourceEdge = EdgeFromLocalToAbstract(source, sourceAbstractCell.Value);
// The new edges will be treated as bi-directional.
var fullGraph = new AbstractGraphWithInsertedEdges(
abstractGraph, sourceEdge != null ? new[] { sourceEdge.Value } : Array.Empty<GraphEdge>(), targetEdge, costEstimator);
// Determine an abstract path in both directions, for use in a bidirectional search.
var estimatedSearchSize = (abstractGraph.Count + 2) / 8;
using (var forwardAbstractSearch = PathSearch.ToTargetCellOverGraph(
fullGraph.GetConnections, locomotor, source, target, estimatedSearchSize, pathFinderOverlay?.RecordAbstractEdges(self)))
{
if (!forwardAbstractSearch.ExpandToTarget())
return PathFinder.NoPath;
using (var reverseAbstractSearch = PathSearch.ToTargetCellOverGraph(
fullGraph.GetConnections, locomotor, target, source, estimatedSearchSize, pathFinderOverlay?.RecordAbstractEdges(self)))
{
reverseAbstractSearch.ExpandToTarget();
using (var fromSrc = GetLocalPathSearch(
self, new[] { source }, target, customCost, ignoreActor, check, laneBias, null, heuristicWeightPercentage,
heuristic: Heuristic(reverseAbstractSearch, estimatedSearchSize, null, null),
recorder: pathFinderOverlay?.RecordLocalEdges(self)))
using (var fromDest = GetLocalPathSearch(
self, new[] { target }, source, customCost, ignoreActor, check, laneBias, null, heuristicWeightPercentage,
heuristic: Heuristic(forwardAbstractSearch, estimatedSearchSize, null, null),
recorder: pathFinderOverlay?.RecordLocalEdges(self),
inReverse: true))
return PathSearch.FindBidiPath(fromDest, fromSrc);
}
}
}
/// <summary>
/// Determines if a path exists between source and target.
/// When <see cref="BlockedByActor.None"/> was given, only terrain is taken into account,
/// i.e. as if <see cref="BlockedByActor.None"/> was used when finding a path.
/// When <see cref="BlockedByActor.Immovable"/> was given, a subset of immovable actors are also taken into
/// account. If the method returns false, there is definitely no path. If it returns true there could be a
/// path, but it is possible that there is no path because of an immovable actor that does not belong to the
/// subset of actors that can be accounted for. So be careful.
/// This would apply for any actor using the same <see cref="Locomotor"/> as this <see cref="HierarchicalPathFinder"/>.
/// </summary>
public bool PathExists(CPos source, CPos target)
{
if (costEstimator == null)
return false;
if (!world.Map.Contains(source) || !world.Map.Contains(target))
return false;
RebuildDomains();
var abstractTarget = AbstractCellForLocalCell(target);
if (abstractTarget == null)
return false;
var targetDomain = abstractDomains[abstractTarget.Value];
// The source cell is reachable, we can compare the domains directly.
var abstractSource = AbstractCellForLocalCell(source);
if (abstractSource != null)
{
var sourceDomain = abstractDomains[abstractSource.Value];
return sourceDomain == targetDomain;
}
// Unlike the target cell, the source cell is allowed to be an unreachable location.
// Instead, what matters is whether any cell adjacent to the source cell can be reached.
// So we need to compare the domains of reachable cells adjacent to the source location.
foreach (var dir in CVec.Directions)
{
var adjacentSource = source + dir;
if (!MovementAllowedBetweenCells(source, adjacentSource))
continue;
var abstractAdjacentSource = AbstractCellForLocalCell(adjacentSource);
if (abstractAdjacentSource == null)
continue;
var adjacentSourceDomain = abstractDomains[abstractAdjacentSource.Value];
if (adjacentSourceDomain == targetDomain)
return true;
}
return false;
}
/// <summary>
/// The abstract graph can become out of date when reachability costs for terrain change.
/// When this occurs, we must rebuild any affected parts of the abstract graph so it remains correct.
/// </summary>
void RebuildDirtyGrids()
{
if (dirtyGridIndexes.Count == 0)
return;
// An empty domain indicates it is out of date and will require rebuilding when next accessed.
abstractDomains.Clear();
var customMovementLayers = world.GetCustomMovementLayers();
foreach (var gridIndex in dirtyGridIndexes)
{
var oldGrid = gridInfos[gridIndex];
var gridTopLeft = GetGridTopLeft(gridIndex, 0);
gridInfos[gridIndex] = BuildGrid(gridTopLeft.X, gridTopLeft.Y, customMovementLayers);
RebuildCostTable(gridTopLeft.X, gridTopLeft.Y, oldGrid, customMovementLayers);
}
dirtyGridIndexes.Clear();
}