Skip to content

Commit

Permalink
fix issue where Y impacted waypoint following. QTPFS next waypoint co…
Browse files Browse the repository at this point in the history
…de changed to avoid units trying to go straight to end if it seems to be closer.
  • Loading branch information
marcushutchings committed Aug 21, 2023
1 parent a192508 commit 2c8ba76
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
2 changes: 1 addition & 1 deletion rts/Sim/MoveTypes/GroundMoveType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1962,7 +1962,7 @@ bool CGroundMoveType::CanSetNextWayPoint(int thread) {
}
}

float cwpDistSq = (cwp - pos).SqLength();
float cwpDistSq = cwp.SqDistance2D(pos);
const bool allowSkip = (cwpDistSq <= Square(SQUARE_SIZE));
if (!allowSkip) {

Expand Down
17 changes: 11 additions & 6 deletions rts/Sim/Path/QTPFS/PathManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ void QTPFS::PathManager::Load() {

char loadMsg[512] = {'\0'};
const char* fmtString = "[PathManager::%s] Complete. Used %u threads for %u node-layers";
sprintf(loadMsg, fmtString, __func__, ThreadPool::GetNumThreads(), nodeLayers.size());
snprintf(loadMsg, sizeof(loadMsg), fmtString, __func__, ThreadPool::GetNumThreads(), nodeLayers.size());

loadscreen->SetLoadMessage(loadMsg);
}
Expand Down Expand Up @@ -390,7 +390,7 @@ void QTPFS::PathManager::InitNodeLayersThreaded(const SRectangle& rect) {

char loadMsg[512] = {'\0'};
const char* fmtString = "[PathManager::%s] using %u threads for %u node-layers";
sprintf(loadMsg, fmtString, __func__, ThreadPool::GetNumThreads(), nodeLayers.size());
snprintf(loadMsg, sizeof(loadMsg), fmtString, __func__, ThreadPool::GetNumThreads(), nodeLayers.size());
pmLoadScreen.AddMessage(loadMsg);

#ifndef NDEBUG
Expand All @@ -401,7 +401,7 @@ void QTPFS::PathManager::InitNodeLayersThreaded(const SRectangle& rect) {
for_mt(0, nodeLayers.size(), [=,&loadMsg, &rect](const int layerNum){
int currentThread = ThreadPool::GetThreadNum();
#ifndef NDEBUG
sprintf(loadMsg, preFmtStr, layerNum);
snprintf(loadMsg, sizeof(loadMsg), preFmtStr, layerNum);
pmLoadScreen.AddMessage(loadMsg);
#endif

Expand Down Expand Up @@ -1218,7 +1218,7 @@ float3 QTPFS::PathManager::NextWayPoint(
float minRadiusSq = QTPFS_POSITIVE_INFINITY;

unsigned int minPointIdx = livePath->GetNextPointIndex();
unsigned int nxtPointIdx = 1;
unsigned int nxtPointIdx = 0;

for (unsigned int i = (livePath->GetNextPointIndex()); i < (livePath->NumPoints() - 1); i++) {
const float radiusSq = (point - livePath->GetPoint(i)).SqLength2D();
Expand All @@ -1240,20 +1240,25 @@ float3 QTPFS::PathManager::NextWayPoint(
// and we must fall back to the radius-based closest point
if (v0.SqLength() < 0.1f) { nxtPointIdx = i + 1; break; }
if (v1.SqLength() < 0.1f) { nxtPointIdx = i + 2; break; }
if (v0.dot(v1) <= -0.01f) { nxtPointIdx = i + 1; }
if (v0.dot(v1) <= -0.01f) { nxtPointIdx = i + 1; break; }

if (radiusSq < minRadiusSq) {
minRadiusSq = radiusSq;
minPointIdx = i + 0;
}
}

if ((livePath->GetNextPointIndex() == 0)) {
livePath->SetNextPointIndex(nxtPointIdx);
return (livePath->GetPoint(livePath->GetNextPointIndex()));
}

// handle a corner-case in which a unit is at the start of its path
// and the goal is in front of it, but on the other side of a cliff
if ((livePath->GetNextPointIndex() == 0) && (nxtPointIdx == (livePath->NumPoints() - 1)))
nxtPointIdx = 1;

if (minPointIdx < nxtPointIdx) {
if (minPointIdx < nxtPointIdx || livePath->GetNextPointIndex() < nxtPointIdx) {
// if close enough to at least one waypoint <i>,
// switch to the point immediately following it
livePath->SetNextPointIndex(nxtPointIdx);
Expand Down

0 comments on commit 2c8ba76

Please sign in to comment.