Skip to content
This repository was archived by the owner on Jan 5, 2024. It is now read-only.

Commit c44d395

Browse files
committed
Added team pathfinders to be used for default dig strength, so they can cache path costs
Also reorganized pathfinders a bit, so the noteam one isn't part of the array, since I didn't want a noteam default dig strength one Added a bit of tolerance to the pathfinder cache clearing, for safety's sake. I think everything using default dig strength shouldn't have float comparison problems, but better safe than sorry
1 parent 1c55602 commit c44d395

File tree

3 files changed

+28
-11
lines changed

3 files changed

+28
-11
lines changed

Entities/Scene.cpp

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -452,9 +452,13 @@ void Scene::Clear()
452452
m_AutoDesigned = true;
453453
m_TotalInvestment = 0;
454454
m_pTerrain = 0;
455-
for (std::unique_ptr<PathFinder> &pathFinder : m_pPathFinders) {
455+
m_NoTeamPathFinder.reset();
456+
for (std::unique_ptr<PathFinder> &pathFinder : m_PathFinders) {
456457
pathFinder.reset();
457458
}
459+
for (std::unique_ptr<PathFinder> &defaultDigStrengthPathFinder : m_DefaultDigStrengthPathfinders) {
460+
defaultDigStrengthPathFinder.reset();
461+
}
458462
m_PathfindingUpdated = false;
459463
m_PartialPathUpdateTimer.Reset();
460464

@@ -933,8 +937,12 @@ int Scene::LoadData(bool placeObjects, bool initPathfinding, bool placeUnits)
933937
//unsigned int numberOfBlocksToAllocate = std::min(128000, sceneArea / (pathFinderGridNodeSize * pathFinderGridNodeSize));
934938
unsigned int numberOfBlocksToAllocate = 4000;
935939

936-
for (int i = 0; i < m_pPathFinders.size(); ++i) {
937-
m_pPathFinders[i] = std::make_unique<PathFinder>(pathFinderGridNodeSize, numberOfBlocksToAllocate);
940+
m_NoTeamPathFinder = std::make_unique<PathFinder>(pathFinderGridNodeSize, numberOfBlocksToAllocate);
941+
for (int i = 0; i < m_PathFinders.size(); ++i) {
942+
m_PathFinders[i] = std::make_unique<PathFinder>(pathFinderGridNodeSize, numberOfBlocksToAllocate);
943+
}
944+
for (int i = 0; i < m_DefaultDigStrengthPathfinders.size(); ++i) {
945+
m_DefaultDigStrengthPathfinders[i] = std::make_unique<PathFinder>(pathFinderGridNodeSize, numberOfBlocksToAllocate);
938946
}
939947
ResetPathFinding();
940948
}
@@ -2979,6 +2987,7 @@ void Scene::ResetPathFinding() {
29792987
for (int team = Activity::Teams::TeamOne; team < Activity::Teams::MaxTeamCount; ++team) {
29802988
g_MovableMan.OverrideMaterialDoors(true, team);
29812989
GetPathFinder(static_cast<Activity::Teams>(team))->RecalculateAllCosts();
2990+
GetPathFinder(static_cast<Activity::Teams>(team), c_PathFindingDefaultDigStrength)->RecalculateAllCosts();
29822991
g_MovableMan.OverrideMaterialDoors(false, team);
29832992
}
29842993
}
@@ -3014,6 +3023,7 @@ void Scene::UpdatePathFinding()
30143023
g_MovableMan.OverrideMaterialDoors(true, team);
30153024

30163025
GetPathFinder(static_cast<Activity::Teams>(team))->UpdateNodeList(updatedNodes);
3026+
GetPathFinder(static_cast<Activity::Teams>(team), c_PathFindingDefaultDigStrength)->UpdateNodeList(updatedNodes);
30173027

30183028
// Place back the material representation of all doors of this team so they are as we found them.
30193029
g_MovableMan.OverrideMaterialDoors(false, team);
@@ -3034,7 +3044,7 @@ void Scene::UpdatePathFinding()
30343044
float Scene::CalculatePath(const Vector &start, const Vector &end, std::list<Vector> &pathResult, float digStrength, Activity::Teams team) {
30353045
float totalCostResult = -1;
30363046

3037-
if (const std::unique_ptr<PathFinder> &pathFinder = GetPathFinder(team)) {
3047+
if (const std::unique_ptr<PathFinder> &pathFinder = GetPathFinder(team, digStrength)) {
30383048
int result = pathFinder->CalculatePath(start, end, pathResult, totalCostResult, digStrength);
30393049

30403050
// It's ok if start and end nodes happen to be the same, the exact pixel locations are added at the front and end of the result regardless
@@ -3117,9 +3127,14 @@ void Scene::Update()
31173127

31183128
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
31193129

3120-
std::unique_ptr<PathFinder>& Scene::GetPathFinder(Activity::Teams team) {
3121-
// Note - we use + 1 when getting pathfinders by index, because our shared NoTeam pathfinder occupies index 0, and the rest come after that.
3122-
return m_pPathFinders[static_cast<int>(team) + 1];
3130+
std::unique_ptr<PathFinder>& Scene::GetPathFinder(Activity::Teams team, float digStrength) {
3131+
if (team == Activity::Teams::NoTeam) {
3132+
return m_NoTeamPathFinder;
3133+
} else if (std::abs(digStrength - c_PathFindingDefaultDigStrength) < 1.0F) {
3134+
return m_DefaultDigStrengthPathfinders[team];
3135+
} else {
3136+
return m_PathFinders[static_cast<int>(team)];
3137+
}
31233138
}
31243139

31253140
} // namespace RTE

Entities/Scene.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1338,8 +1338,9 @@ const SceneObject * PickPlacedActorInRange(int whichSet, Vector &scenePoint, int
13381338
SLTerrain *m_pTerrain;
13391339

13401340
// Pathfinding graph and logic. Owned by this
1341-
// The array of PathFinders for each team. Because we also have a shared pathfinder using index 0, we need to use MaxTeamCount + 1 to handle all the Teams' PathFinders.
1342-
std::array<std::unique_ptr<PathFinder>, Activity::Teams::MaxTeamCount + 1> m_pPathFinders;
1341+
std::unique_ptr<PathFinder> m_NoTeamPathFinder; //!< The shared pathfinder for when no team is specified.
1342+
std::array<std::unique_ptr<PathFinder>, Activity::Teams::MaxTeamCount> m_PathFinders; //!< The array of pathfinders for each team.
1343+
std::array<std::unique_ptr<PathFinder>, Activity::Teams::MaxTeamCount> m_DefaultDigStrengthPathfinders; //!< The array of pathfinders for each team to be used for default dig strength.
13431344
// Is set to true on any frame the pathfinding data has been updated
13441345
bool m_PathfindingUpdated;
13451346
// Timer for when to do an update of the pathfinding data
@@ -1394,8 +1395,9 @@ const SceneObject * PickPlacedActorInRange(int whichSet, Vector &scenePoint, int
13941395
/// Gets the pathfinder for a given team.
13951396
/// </summary>
13961397
/// <param name="team">The team to get the pathfinder for. NoTeam is valid, and will give a shared pathfinder.</param>
1398+
/// <param name="digStrength">The dig strength to get the pathfinder for. A different pathfinder will be used if it's the default dig strength. Defaults to -1 so the normal pathfinder will be used if no argument is passed in.</param>
13971399
/// <returns>A pointer to the pathfinder for the given team.</returns>
1398-
std::unique_ptr<PathFinder> & GetPathFinder(Activity::Teams team);
1400+
std::unique_ptr<PathFinder> & GetPathFinder(Activity::Teams team, float digStrength = -1);
13991401

14001402
/// <summary>
14011403
/// Serializes the SceneObject via the Writer. Necessary because full serialization doesn't know how to deal with duplicate properties.

System/PathFinder.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ namespace RTE {
118118
// Clear out the results if it happens to contain anything
119119
pathResult.clear();
120120

121-
if (m_DigStrength != digStrength) {
121+
if (std::abs(digStrength - m_DigStrength) > 1.0F) {
122122
// Unfortunately, DigStrength-aware pathing means that we're adjusting node transition costs, so we need to reset our path cache on every call.
123123
// In future we'll potentially store a different pather for different mobility bands, and reuse pathing costs.
124124
// But then again it's probably more fruitful to optimize the graph node to make searches faster, instead.

0 commit comments

Comments
 (0)