Skip to content

Commit

Permalink
handle raw movement directly in the PFS
Browse files Browse the repository at this point in the history
  • Loading branch information
rt committed Jul 22, 2017
1 parent bac89bd commit 51e1163
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 12 deletions.
3 changes: 3 additions & 0 deletions rts/Sim/MoveTypes/MoveDefHandler.cpp
Expand Up @@ -40,6 +40,7 @@ CR_REG_METADATA(MoveDef, (

CR_MEMBER(avoidMobilesOnPath),
CR_MEMBER(allowTerrainCollisions),
CR_MEMBER(allowRawMovement),

CR_MEMBER(heatMapping),
CR_MEMBER(flowMapping),
Expand Down Expand Up @@ -178,6 +179,7 @@ MoveDef::MoveDef()

, avoidMobilesOnPath(true)
, allowTerrainCollisions(true)
, allowRawMovement(false)

, heatMapping(true)
, flowMapping(true)
Expand Down Expand Up @@ -244,6 +246,7 @@ MoveDef::MoveDef(const LuaTable& moveDefTable, int moveDefID) {

avoidMobilesOnPath = moveDefTable.GetBool("avoidMobilesOnPath", true);
allowTerrainCollisions = moveDefTable.GetBool("allowTerrainCollisions", true);
allowRawMovement = moveDefTable.GetBool("allowRawMovement", false);

heatMapping = moveDefTable.GetBool("heatMapping", false);
flowMapping = moveDefTable.GetBool("flowMapping", true);
Expand Down
9 changes: 5 additions & 4 deletions rts/Sim/MoveTypes/MoveDefHandler.h
Expand Up @@ -29,8 +29,8 @@ struct MoveDef {
bool testTerrain = true,
bool testObjects = true,
bool centerOnly = false,
float* minSpeedMod = NULL,
int* maxBlockBit = NULL
float* minSpeedMod = nullptr,
int* maxBlockBit = nullptr
) const;
bool TestMoveSquare(
const CSolidObject* collider,
Expand All @@ -39,8 +39,8 @@ struct MoveDef {
bool testTerrain = true,
bool testObjects = true,
bool centerOnly = false,
float* minSpeedMod = NULL,
int* maxBlockBit = NULL
float* minSpeedMod = nullptr,
int* maxBlockBit = nullptr
) const;

// aircraft and buildings defer to UnitDef::floatOnWater
Expand Down Expand Up @@ -130,6 +130,7 @@ struct MoveDef {
/// otherwise, since they are never initialized)
bool avoidMobilesOnPath;
bool allowTerrainCollisions;
bool allowRawMovement;

/// do we leave heat and avoid any left by others?
bool heatMapping;
Expand Down
14 changes: 7 additions & 7 deletions rts/Sim/Path/Default/IPathFinder.cpp
Expand Up @@ -203,8 +203,7 @@ IPath::SearchResult IPathFinder::InitSearch(const MoveDef& moveDef, const CPathF

dirtyBlocks.push_back(mStartBlockIdx);

// start a new search and
// add the starting block to the open-blocks-queue
// start a new search and add the starting block to the open-blocks-queue
openBlockBuffer.SetSize(0);
PathNode* ob = openBlockBuffer.GetNode(openBlockBuffer.GetSize());
ob->fCost = 0.0f;
Expand All @@ -218,15 +217,16 @@ IPath::SearchResult IPathFinder::InitSearch(const MoveDef& moveDef, const CPathF
mGoalHeuristic = pfDef.Heuristic(square.x, square.y, BLOCK_SIZE);

// perform the search
const IPath::SearchResult result = DoSearch(moveDef, pfDef, owner);
const IPath::SearchResult rawResult = DoRawSearch(moveDef, pfDef, owner);
const IPath::SearchResult ipfResult = (rawResult == IPath::Error)? DoSearch(moveDef, pfDef, owner): rawResult;

if (result == IPath::Ok)
return result;
if (ipfResult == IPath::Ok)
return ipfResult;
if (mGoalBlockIdx != mStartBlockIdx)
return result;
return ipfResult;

// if start and goal are within the same block, but distinct squares
// or considered a single point for search purposes, then we probably
// can not get closer
return (!isStartGoal || startInGoal)? IPath::CantGetCloser: result;
return (!isStartGoal || startInGoal)? IPath::CantGetCloser: ipfResult;
}
1 change: 1 addition & 0 deletions rts/Sim/Path/Default/IPathFinder.h
Expand Up @@ -76,6 +76,7 @@ class IPathFinder {
void ResetSearch();

protected: // pure virtuals
virtual IPath::SearchResult DoRawSearch(const MoveDef&, const CPathFinderDef&, const CSolidObject* owner) { return IPath::Error; }
virtual IPath::SearchResult DoSearch(const MoveDef&, const CPathFinderDef&, const CSolidObject* owner) = 0;

/**
Expand Down
56 changes: 55 additions & 1 deletion rts/Sim/Path/Default/PathFinder.cpp
Expand Up @@ -68,6 +68,60 @@ const float3* CPathFinder::GetDirectionVectorsTable3D() { return (&PF_DIRECTION_



IPath::SearchResult CPathFinder::DoRawSearch(
const MoveDef& moveDef,
const CPathFinderDef& pfDef,
const CSolidObject* owner
) {
if (!moveDef.allowRawMovement || !pfDef.allowRawPath)
return IPath::Error;

const int2 strtBlk = BlockIdxToPos(mStartBlockIdx);
const int2 goalBlk = {int(pfDef.goalSquareX), int(pfDef.goalSquareZ)};
const int2 diffBlk = goalBlk - strtBlk;
// const int2 goalBlk = BlockIdxToPos(mGoalBlockIdx);

const float pathLen = math::sqrt(Square(diffBlk.x) + Square(diffBlk.y));
const float testLen = math::ceil(pathLen);

const float2 step = {diffBlk.x / pathLen, diffBlk.y / pathLen};
// const float2 mods = {CMoveMath::GetPosSpeedMod(moveDef, strtBlk.x, strtBlk.y), CMoveMath::GetPosSpeedMod(moveDef, goalBlk.x, goalBlk.y)};
const float2 lims = {pfDef.maxRawPathLen, pfDef.minRawSpeedMod};

if (pathLen > lims.x)
return IPath::Error;

int2 testBlk;
int2 prevBlk = {-1, -1};

// NOTE:
// no need to integrate with backtracking in FinishSearch
// the final "path" only contains startPos which is consumed
// immediately, after which NextWayPoint keeps returning the
// goal until owner reaches it
for (float i = 0.0f; i <= testLen; i += 1.0f) {
testBlk = strtBlk + step * i;

assert(std::abs(testBlk.x - prevBlk.x) <= 1);
assert(std::abs(testBlk.y - prevBlk.y) <= 1);

if (testBlk == prevBlk)
continue;
if (testBlk == goalBlk)
break;

if ((blockCheckFunc(moveDef, testBlk.x, testBlk.y, owner) & CMoveMath::BLOCK_STRUCTURE) != 0)
return IPath::Error;

if (CMoveMath::GetPosSpeedMod(moveDef, testBlk.x, testBlk.y) <= lims.y)
return IPath::Error;

prevBlk = testBlk;
}

return IPath::Ok;
}

IPath::SearchResult CPathFinder::DoSearch(
const MoveDef& moveDef,
const CPathFinderDef& pfDef,
Expand Down Expand Up @@ -351,7 +405,7 @@ IPath::SearchResult CPathFinder::FinishSearch(const MoveDef& moveDef, const CPat
}
}

// Adds the cost of the path.
// copy the path-cost
foundPath.pathCost = blockStates.fCost[mGoalBlockIdx];

return IPath::Ok;
Expand Down
1 change: 1 addition & 0 deletions rts/Sim/Path/Default/PathFinder.h
Expand Up @@ -30,6 +30,7 @@ class CPathFinder: public IPathFinder {

protected: // IPathFinder impl
/// Performs the actual search.
IPath::SearchResult DoRawSearch(const MoveDef& moveDef, const CPathFinderDef& pfDef, const CSolidObject* owner);
IPath::SearchResult DoSearch(const MoveDef& moveDef, const CPathFinderDef& pfDef, const CSolidObject* owner);

/**
Expand Down
5 changes: 5 additions & 0 deletions rts/Sim/Path/Default/PathFinderDef.cpp
Expand Up @@ -8,7 +8,11 @@

CPathFinderDef::CPathFinderDef(const float3& goalCenter, float goalRadius, float sqGoalDistance)
: goal(goalCenter)

, sqGoalRadius(goalRadius * goalRadius)
, maxRawPathLen(std::numeric_limits<float>::max())
, minRawSpeedMod(0.0f)

, constraintDisabled(false)
, testMobile(true)
, needPath(true)
Expand All @@ -21,6 +25,7 @@ CPathFinderDef::CPathFinderDef(const float3& goalCenter, float goalRadius, float
// regenerating after a terrain change) paths, so prefer to
// keep PF and PE in sync
, exactPath(false)
, allowRawPath(false)
, dirIndependent(false)
, synced(true)
{
Expand Down
4 changes: 4 additions & 0 deletions rts/Sim/Path/Default/PathFinderDef.h
Expand Up @@ -19,6 +19,7 @@ class CPathFinderDef {

virtual bool WithinConstraints(unsigned int xSquare, unsigned int zSquare) const = 0;
void DisableConstraint(bool b) { constraintDisabled = b; }
void AllowRawPathSearch(bool b) { allowRawPath = b; }

bool IsGoal(unsigned int xSquare, unsigned int zSquare) const;
float Heuristic(unsigned int xSquare, unsigned int zSquare, unsigned int blockSize) const;
Expand All @@ -30,6 +31,8 @@ class CPathFinderDef {
float3 goal;

float sqGoalRadius;
float maxRawPathLen;
float minRawSpeedMod;

// if true, do not need to generate any waypoints
bool startInGoalRadius;
Expand All @@ -38,6 +41,7 @@ class CPathFinderDef {
bool testMobile;
bool needPath;
bool exactPath;
bool allowRawPath;
bool dirIndependent;
bool synced;

Expand Down
2 changes: 2 additions & 0 deletions rts/Sim/Path/Default/PathManager.cpp
Expand Up @@ -163,7 +163,9 @@ IPath::SearchResult CPathManager::ArrangePath(
if (heurGoalDist2D > searchDistances[n])
continue;

// enable raw search only for fully high-res paths
pfDef->DisableConstraint(!useConstraints[n]);
pfDef->AllowRawPathSearch(n == PATH_MAX_RES);

const IPath::SearchResult currResult = pathFinders[n]->GetPath(*moveDef, *pfDef, caller, startPos, *pathObjects[n], nodeLimits[n]);

Expand Down

0 comments on commit 51e1163

Please sign in to comment.