Core/Movement: ported and merged Follow and Chase movement generator rewrite
This commit is contained in:
1 parent
33566426aa
commit
7edabcbd88
25 files changed
+666
-725
No files matched your search
@@ -906,43 +906,50 @@ float PathGenerator::Dist3DSqr(G3D::Vector3 const& p1, G3D::Vector3 const& p2) c
|
||||
return (p1 - p2).squaredLength();
|
||||
}
|
||||
|
||||
void PathGenerator::ReducePathLenghtByDist(float dist)
|
||||
void PathGenerator::ShortenPathUntilDist(G3D::Vector3 const& target, float dist)
|
||||
{
|
||||
if (GetPathType() == PATHFIND_BLANK)
|
||||
if (GetPathType() == PATHFIND_BLANK || _pathPoints.size() < 2)
|
||||
{
|
||||
TC_LOG_ERROR("maps", "PathGenerator::ReducePathLenghtByDist called before path was built");
|
||||
TC_LOG_ERROR("maps", "PathGenerator::ReducePathLengthByDist called before path was successfully built");
|
||||
return;
|
||||
}
|
||||
|
||||
if (_pathPoints.size() < 2) // path building failure
|
||||
float const distSq = dist * dist;
|
||||
|
||||
// the first point of the path must be outside the specified range
|
||||
// (this should have really been checked by the caller...)
|
||||
if ((_pathPoints[0] - target).squaredLength() < distSq)
|
||||
return;
|
||||
|
||||
uint32 i = _pathPoints.size();
|
||||
G3D::Vector3 nextVec = _pathPoints[--i];
|
||||
while (i > 0)
|
||||
// check if we even need to do anything
|
||||
if ((*_pathPoints.rbegin() - target).squaredLength() >= distSq)
|
||||
return;
|
||||
|
||||
size_t i = _pathPoints.size()-1;
|
||||
// find the first i s.t.:
|
||||
// - _pathPoints[i] is still too close
|
||||
// - _pathPoints[i-1] is too far away
|
||||
// => the end point is somewhere on the line between the two
|
||||
while (1)
|
||||
{
|
||||
G3D::Vector3 currVec = _pathPoints[--i];
|
||||
G3D::Vector3 diffVec = (nextVec - currVec);
|
||||
float len = diffVec.length();
|
||||
if (len > dist)
|
||||
// we know that pathPoints[i] is too close already (from the previous iteration)
|
||||
if ((_pathPoints[i-1] - target).squaredLength() >= distSq)
|
||||
break; // bingo!
|
||||
|
||||
if (!--i)
|
||||
{
|
||||
float step = dist / len;
|
||||
// same as nextVec
|
||||
_pathPoints[i + 1] -= diffVec * step;
|
||||
_sourceUnit->UpdateAllowedPositionZ(_pathPoints[i + 1].x, _pathPoints[i + 1].y, _pathPoints[i + 1].z);
|
||||
_pathPoints.resize(i + 2);
|
||||
break;
|
||||
}
|
||||
else if (i == 0) // at second point
|
||||
{
|
||||
_pathPoints[1] = _pathPoints[0];
|
||||
// no point found that fulfills the condition
|
||||
_pathPoints[0] = _pathPoints[1];
|
||||
_pathPoints.resize(2);
|
||||
break;
|
||||
return;
|
||||
}
|
||||
|
||||
dist -= len;
|
||||
nextVec = currVec; // we're going backwards
|
||||
}
|
||||
|
||||
// ok, _pathPoints[i] is too close, _pathPoints[i-1] is not, so our target point is somewhere between the two...
|
||||
// ... settle for a guesstimate since i'm not confident in doing trig on every chase motion tick...
|
||||
// (@todo review this)
|
||||
_pathPoints[i] += (_pathPoints[i - 1] - _pathPoints[i]).direction() * (dist - (_pathPoints[i] - target).length());
|
||||
_pathPoints.resize(i+1);
|
||||
}
|
||||
|
||||
bool PathGenerator::IsInvalidDestinationZ(Unit const* target) const
|
||||
|
||||
Reference in new issue
Block a user