mirror of
https://github.com/OpenMW/openmw.git
synced 2025-01-23 14:23:53 +00:00
rewrite pathTo for clearer logic; reapply Scrawl's aifollow threshold
This commit is contained in:
parent
d7d5cc6689
commit
b960b0af93
3 changed files with 45 additions and 70 deletions
|
@ -127,21 +127,21 @@ bool AiFollow::execute (const MWWorld::Ptr& actor, CharacterController& characte
|
|||
|
||||
//Set the target destination from the actor
|
||||
ESM::Pathgrid::Point dest = target.getRefData().getPosition().pos;
|
||||
float dist = distance(dest, pos.pos[0], pos.pos[1], pos.pos[2]);
|
||||
//const float threshold = 10;
|
||||
|
||||
//if (storage.mMoving) //Stop when you get close
|
||||
// storage.mMoving = (dist > followDistance);
|
||||
//else
|
||||
// storage.mMoving = (dist > followDistance + threshold);
|
||||
|
||||
const float threshold = 10; // to avoid constant switching between moving/stopping
|
||||
if (!storage.mMoving) followDistance += threshold;
|
||||
storage.mMoving = !pathTo(actor, dest, duration, followDistance); // Go to the destination
|
||||
|
||||
if (storage.mMoving)
|
||||
{
|
||||
//Check if you're far away
|
||||
if(dist > 450)
|
||||
float dist = distance(dest, pos.pos[0], pos.pos[1], pos.pos[2]);
|
||||
|
||||
if (dist > 450)
|
||||
actor.getClass().getCreatureStats(actor).setMovementFlag(MWMechanics::CreatureStats::Flag_Run, true); //Make NPC run
|
||||
else if(dist < 325) //Have a bit of a dead zone, otherwise npc will constantly flip between running and not when right on the edge of the running threshhold
|
||||
else if (dist < 325) //Have a bit of a dead zone, otherwise npc will constantly flip between running and not when right on the edge of the running threshhold
|
||||
actor.getClass().getCreatureStats(actor).setMovementFlag(MWMechanics::CreatureStats::Flag_Run, false); //make NPC walk
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
|
|
@ -22,6 +22,7 @@ MWMechanics::AiPackage::~AiPackage() {}
|
|||
|
||||
MWMechanics::AiPackage::AiPackage() :
|
||||
mTimer(AI_REACTION_TIME + 1.0f), // to force initial pathbuild
|
||||
mIsShortcutting(false),
|
||||
mShortcutProhibited(false), mShortcutFailPos()
|
||||
{
|
||||
}
|
||||
|
@ -41,10 +42,8 @@ bool MWMechanics::AiPackage::followTargetThroughDoors() const
|
|||
return false;
|
||||
}
|
||||
|
||||
|
||||
bool MWMechanics::AiPackage::pathTo(const MWWorld::Ptr& actor, const ESM::Pathgrid::Point& dest, float duration, float destTolerance)
|
||||
{
|
||||
//Update various Timers
|
||||
mTimer += duration; //Update timer
|
||||
|
||||
ESM::Position pos = actor.getRefData().getPosition(); //position of the actor
|
||||
|
@ -59,39 +58,21 @@ bool MWMechanics::AiPackage::pathTo(const MWWorld::Ptr& actor, const ESM::Pathgr
|
|||
return false;
|
||||
}
|
||||
|
||||
//***********************
|
||||
/// Checks if you can't get to the end position at all, adds end position to end of path
|
||||
/// Rebuilds path every [AI_REACTION_TIME] seconds, in case the target has moved
|
||||
//***********************
|
||||
|
||||
// handle path building and shortcutting
|
||||
ESM::Pathgrid::Point start = pos.pos;
|
||||
|
||||
float distToNextWaypoint = distance(start, dest);
|
||||
bool isDestReached = (distToNextWaypoint <= destTolerance);
|
||||
float distToTarget = distance(start, dest);
|
||||
bool isDestReached = (distToTarget <= destTolerance);
|
||||
|
||||
if (!isDestReached && mTimer > AI_REACTION_TIME)
|
||||
{
|
||||
osg::Vec3f& lastActorPos = mLastActorPos;
|
||||
bool isStuck = distance(start, lastActorPos.x(), lastActorPos.y(), lastActorPos.z()) < actor.getClass().getSpeed(actor)*mTimer
|
||||
&& distance(dest, start) > 20;
|
||||
|
||||
lastActorPos = pos.asVec3();
|
||||
|
||||
const ESM::Cell *cell = actor.getCell()->getCell();
|
||||
bool needPathRecalc = doesPathNeedRecalc(dest, cell); //Only rebuild path if dest point has changed
|
||||
|
||||
bool isWayClear = true;
|
||||
|
||||
if (!needPathRecalc) // TODO: add check if actor is actually shortcutting
|
||||
{
|
||||
isWayClear = checkWayIsClearForActor(start, dest, actor); // check if current shortcut is safe to follow
|
||||
}
|
||||
|
||||
if (!isWayClear || needPathRecalc) // Only rebuild path if the target has moved or can't follow current shortcut
|
||||
{
|
||||
bool wasShortcutting = mIsShortcutting;
|
||||
bool destInLOS = false;
|
||||
mIsShortcutting = shortcutPath(start, dest, actor, &destInLOS); // try to shortcut first
|
||||
|
||||
if (isStuck || !isWayClear || !shortcutPath(start, dest, actor, &destInLOS))
|
||||
if (!mIsShortcutting)
|
||||
{
|
||||
if (wasShortcutting || doesPathNeedRecalc(dest, actor.getCell()->getCell())) // only rebuild path if the target has moved
|
||||
{
|
||||
mPathFinder.buildSyncedPath(start, dest, actor.getCell());
|
||||
|
||||
|
@ -111,7 +92,6 @@ bool MWMechanics::AiPackage::pathTo(const MWWorld::Ptr& actor, const ESM::Pathgr
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(!mPathFinder.getPath().empty()) //Path has points in it
|
||||
{
|
||||
|
@ -120,19 +100,13 @@ bool MWMechanics::AiPackage::pathTo(const MWWorld::Ptr& actor, const ESM::Pathgr
|
|||
if(distance(dest, lastPos) > 100) //End of the path is far from the destination
|
||||
mPathFinder.addPointToPath(dest); //Adds the final destination to the path, to try to get to where you want to go
|
||||
}
|
||||
}
|
||||
|
||||
mTimer = 0;
|
||||
}
|
||||
|
||||
//************************
|
||||
/// Checks if you aren't moving; attempts to unstick you
|
||||
//************************
|
||||
if (isDestReached || mPathFinder.checkPathCompleted(pos.pos[0],pos.pos[1])) //Path finished?
|
||||
{
|
||||
actor.getClass().getMovementSettings(actor).mPosition[0] = 0; // stop moving
|
||||
actor.getClass().getMovementSettings(actor).mPosition[1] = 0;
|
||||
actor.getClass().getMovementSettings(actor).mPosition[2] = 0;
|
||||
|
||||
// turn to destination point
|
||||
zTurn(actor, getZAngleToPoint(start, dest));
|
||||
smoothTurn(actor, getXAngleToPoint(start, dest), 0);
|
||||
|
@ -140,6 +114,8 @@ bool MWMechanics::AiPackage::pathTo(const MWWorld::Ptr& actor, const ESM::Pathgr
|
|||
}
|
||||
else
|
||||
{
|
||||
actor.getClass().getMovementSettings(actor).mPosition[1] = 1; // run to target
|
||||
// handle obstacles on the way
|
||||
evadeObstacles(actor, duration, pos);
|
||||
}
|
||||
|
||||
|
@ -155,25 +131,23 @@ void MWMechanics::AiPackage::evadeObstacles(const MWWorld::Ptr& actor, float dur
|
|||
zTurn(actor, mPathFinder.getZAngleToNext(pos.pos[0], pos.pos[1]));
|
||||
|
||||
MWMechanics::Movement& movement = actor.getClass().getMovementSettings(actor);
|
||||
if (mObstacleCheck.check(actor, duration))
|
||||
{
|
||||
// first check if we're walking into a door
|
||||
MWWorld::Ptr door = getNearbyDoor(actor);
|
||||
if (door != MWWorld::Ptr()) // NOTE: checks interior cells only
|
||||
|
||||
// check if stuck due to obstacles
|
||||
if (!mObstacleCheck.check(actor, duration)) return;
|
||||
|
||||
// first check if obstacle is a door
|
||||
MWWorld::Ptr door = getNearbyDoor(actor); // NOTE: checks interior cells only
|
||||
if (door != MWWorld::Ptr())
|
||||
{
|
||||
if (!door.getCellRef().getTeleport() && door.getCellRef().getTrap().empty()
|
||||
&& door.getCellRef().getLockLevel() <= 0 && door.getClass().getDoorState(door) == 0) {
|
||||
MWBase::Environment::get().getWorld()->activateDoor(door, 1);
|
||||
}
|
||||
}
|
||||
else // probably walking into another NPC
|
||||
else // any other obstacle (NPC, crate, etc.)
|
||||
{
|
||||
mObstacleCheck.takeEvasiveAction(movement);
|
||||
}
|
||||
}
|
||||
else { //Not stuck, so reset things
|
||||
movement.mPosition[1] = 1; //Just run forward
|
||||
}
|
||||
}
|
||||
|
||||
bool MWMechanics::AiPackage::shortcutPath(const ESM::Pathgrid::Point& startPoint, const ESM::Pathgrid::Point& endPoint, const MWWorld::Ptr& actor, bool *destInLOS)
|
||||
|
@ -191,7 +165,7 @@ bool MWMechanics::AiPackage::shortcutPath(const ESM::Pathgrid::Point& startPoint
|
|||
if (!isPathClear
|
||||
&& (!mShortcutProhibited || (PathFinder::MakeOsgVec3(mShortcutFailPos) - PathFinder::MakeOsgVec3(startPoint)).length() >= PATHFIND_SHORTCUT_RETRY_DIST))
|
||||
{
|
||||
// take the direct path only if there aren't any obstacles
|
||||
// check if target is clearly visible
|
||||
isPathClear = !MWBase::Environment::get().getWorld()->castRay(
|
||||
static_cast<float>(startPoint.mX), static_cast<float>(startPoint.mY), static_cast<float>(startPoint.mZ),
|
||||
static_cast<float>(endPoint.mX), static_cast<float>(endPoint.mY), static_cast<float>(endPoint.mZ));
|
||||
|
|
|
@ -82,7 +82,7 @@ namespace MWMechanics
|
|||
bool isTargetMagicallyHidden(const MWWorld::Ptr& target);
|
||||
|
||||
protected:
|
||||
/// Causes the actor to attempt to walk to the specified location
|
||||
/// Handles path building and shortcutting with obstacles avoiding
|
||||
/** \return If the actor has arrived at his destination **/
|
||||
bool pathTo(const MWWorld::Ptr& actor, const ESM::Pathgrid::Point& dest, float duration, float destTolerance = 0.0f);
|
||||
|
||||
|
@ -108,6 +108,7 @@ namespace MWMechanics
|
|||
ESM::Pathgrid::Point mPrevDest;
|
||||
osg::Vec3f mLastActorPos;
|
||||
|
||||
bool mIsShortcutting; // if shortcutting at the moment
|
||||
bool mShortcutProhibited; // shortcutting may be prohibited after unsuccessful attempt
|
||||
ESM::Pathgrid::Point mShortcutFailPos; // position of last shortcut fail
|
||||
|
||||
|
|
Loading…
Reference in a new issue