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
|
//Set the target destination from the actor
|
||||||
ESM::Pathgrid::Point dest = target.getRefData().getPosition().pos;
|
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
|
storage.mMoving = !pathTo(actor, dest, duration, followDistance); // Go to the destination
|
||||||
|
|
||||||
//Check if you're far away
|
if (storage.mMoving)
|
||||||
if(dist > 450)
|
{
|
||||||
actor.getClass().getCreatureStats(actor).setMovementFlag(MWMechanics::CreatureStats::Flag_Run, true); //Make NPC run
|
//Check if you're far away
|
||||||
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
|
float dist = distance(dest, pos.pos[0], pos.pos[1], pos.pos[2]);
|
||||||
actor.getClass().getCreatureStats(actor).setMovementFlag(MWMechanics::CreatureStats::Flag_Run, false); //make NPC walk
|
|
||||||
|
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
|
||||||
|
actor.getClass().getCreatureStats(actor).setMovementFlag(MWMechanics::CreatureStats::Flag_Run, false); //make NPC walk
|
||||||
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,6 +22,7 @@ MWMechanics::AiPackage::~AiPackage() {}
|
||||||
|
|
||||||
MWMechanics::AiPackage::AiPackage() :
|
MWMechanics::AiPackage::AiPackage() :
|
||||||
mTimer(AI_REACTION_TIME + 1.0f), // to force initial pathbuild
|
mTimer(AI_REACTION_TIME + 1.0f), // to force initial pathbuild
|
||||||
|
mIsShortcutting(false),
|
||||||
mShortcutProhibited(false), mShortcutFailPos()
|
mShortcutProhibited(false), mShortcutFailPos()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
@ -41,10 +42,8 @@ bool MWMechanics::AiPackage::followTargetThroughDoors() const
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
bool MWMechanics::AiPackage::pathTo(const MWWorld::Ptr& actor, const ESM::Pathgrid::Point& dest, float duration, float destTolerance)
|
bool MWMechanics::AiPackage::pathTo(const MWWorld::Ptr& actor, const ESM::Pathgrid::Point& dest, float duration, float destTolerance)
|
||||||
{
|
{
|
||||||
//Update various Timers
|
|
||||||
mTimer += duration; //Update timer
|
mTimer += duration; //Update timer
|
||||||
|
|
||||||
ESM::Position pos = actor.getRefData().getPosition(); //position of the actor
|
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;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
//***********************
|
// handle path building and shortcutting
|
||||||
/// 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
|
|
||||||
//***********************
|
|
||||||
|
|
||||||
ESM::Pathgrid::Point start = pos.pos;
|
ESM::Pathgrid::Point start = pos.pos;
|
||||||
|
|
||||||
float distToNextWaypoint = distance(start, dest);
|
float distToTarget = distance(start, dest);
|
||||||
bool isDestReached = (distToNextWaypoint <= destTolerance);
|
bool isDestReached = (distToTarget <= destTolerance);
|
||||||
|
|
||||||
if (!isDestReached && mTimer > AI_REACTION_TIME)
|
if (!isDestReached && mTimer > AI_REACTION_TIME)
|
||||||
{
|
{
|
||||||
osg::Vec3f& lastActorPos = mLastActorPos;
|
bool wasShortcutting = mIsShortcutting;
|
||||||
bool isStuck = distance(start, lastActorPos.x(), lastActorPos.y(), lastActorPos.z()) < actor.getClass().getSpeed(actor)*mTimer
|
bool destInLOS = false;
|
||||||
&& distance(dest, start) > 20;
|
mIsShortcutting = shortcutPath(start, dest, actor, &destInLOS); // try to shortcut first
|
||||||
|
|
||||||
lastActorPos = pos.asVec3();
|
if (!mIsShortcutting)
|
||||||
|
|
||||||
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 (wasShortcutting || doesPathNeedRecalc(dest, actor.getCell()->getCell())) // only rebuild path if the target has moved
|
||||||
}
|
|
||||||
|
|
||||||
if (!isWayClear || needPathRecalc) // Only rebuild path if the target has moved or can't follow current shortcut
|
|
||||||
{
|
|
||||||
bool destInLOS = false;
|
|
||||||
|
|
||||||
if (isStuck || !isWayClear || !shortcutPath(start, dest, actor, &destInLOS))
|
|
||||||
{
|
{
|
||||||
mPathFinder.buildSyncedPath(start, dest, actor.getCell());
|
mPathFinder.buildSyncedPath(start, dest, actor.getCell());
|
||||||
|
|
||||||
|
@ -111,28 +92,21 @@ bool MWMechanics::AiPackage::pathTo(const MWWorld::Ptr& actor, const ESM::Pathgr
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if(!mPathFinder.getPath().empty()) //Path has points in it
|
if(!mPathFinder.getPath().empty()) //Path has points in it
|
||||||
{
|
{
|
||||||
ESM::Pathgrid::Point lastPos = mPathFinder.getPath().back(); //Get the end of the proposed path
|
ESM::Pathgrid::Point lastPos = mPathFinder.getPath().back(); //Get the end of the proposed path
|
||||||
|
|
||||||
if(distance(dest, lastPos) > 100) //End of the path is far from the destination
|
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
|
mPathFinder.addPointToPath(dest); //Adds the final destination to the path, to try to get to where you want to go
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
mTimer = 0;
|
mTimer = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
//************************
|
|
||||||
/// Checks if you aren't moving; attempts to unstick you
|
|
||||||
//************************
|
|
||||||
if (isDestReached || mPathFinder.checkPathCompleted(pos.pos[0],pos.pos[1])) //Path finished?
|
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
|
// turn to destination point
|
||||||
zTurn(actor, getZAngleToPoint(start, dest));
|
zTurn(actor, getZAngleToPoint(start, dest));
|
||||||
smoothTurn(actor, getXAngleToPoint(start, dest), 0);
|
smoothTurn(actor, getXAngleToPoint(start, dest), 0);
|
||||||
|
@ -140,6 +114,8 @@ bool MWMechanics::AiPackage::pathTo(const MWWorld::Ptr& actor, const ESM::Pathgr
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
actor.getClass().getMovementSettings(actor).mPosition[1] = 1; // run to target
|
||||||
|
// handle obstacles on the way
|
||||||
evadeObstacles(actor, duration, pos);
|
evadeObstacles(actor, duration, pos);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -155,24 +131,22 @@ void MWMechanics::AiPackage::evadeObstacles(const MWWorld::Ptr& actor, float dur
|
||||||
zTurn(actor, mPathFinder.getZAngleToNext(pos.pos[0], pos.pos[1]));
|
zTurn(actor, mPathFinder.getZAngleToNext(pos.pos[0], pos.pos[1]));
|
||||||
|
|
||||||
MWMechanics::Movement& movement = actor.getClass().getMovementSettings(actor);
|
MWMechanics::Movement& movement = actor.getClass().getMovementSettings(actor);
|
||||||
if (mObstacleCheck.check(actor, duration))
|
|
||||||
|
// 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())
|
||||||
{
|
{
|
||||||
// first check if we're walking into a door
|
if (!door.getCellRef().getTeleport() && door.getCellRef().getTrap().empty()
|
||||||
MWWorld::Ptr door = getNearbyDoor(actor);
|
&& door.getCellRef().getLockLevel() <= 0 && door.getClass().getDoorState(door) == 0) {
|
||||||
if (door != MWWorld::Ptr()) // NOTE: checks interior cells only
|
MWBase::Environment::get().getWorld()->activateDoor(door, 1);
|
||||||
{
|
|
||||||
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
|
|
||||||
{
|
|
||||||
mObstacleCheck.takeEvasiveAction(movement);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else { //Not stuck, so reset things
|
else // any other obstacle (NPC, crate, etc.)
|
||||||
movement.mPosition[1] = 1; //Just run forward
|
{
|
||||||
|
mObstacleCheck.takeEvasiveAction(movement);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -191,7 +165,7 @@ bool MWMechanics::AiPackage::shortcutPath(const ESM::Pathgrid::Point& startPoint
|
||||||
if (!isPathClear
|
if (!isPathClear
|
||||||
&& (!mShortcutProhibited || (PathFinder::MakeOsgVec3(mShortcutFailPos) - PathFinder::MakeOsgVec3(startPoint)).length() >= PATHFIND_SHORTCUT_RETRY_DIST))
|
&& (!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(
|
isPathClear = !MWBase::Environment::get().getWorld()->castRay(
|
||||||
static_cast<float>(startPoint.mX), static_cast<float>(startPoint.mY), static_cast<float>(startPoint.mZ),
|
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));
|
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);
|
bool isTargetMagicallyHidden(const MWWorld::Ptr& target);
|
||||||
|
|
||||||
protected:
|
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 **/
|
/** \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);
|
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;
|
ESM::Pathgrid::Point mPrevDest;
|
||||||
osg::Vec3f mLastActorPos;
|
osg::Vec3f mLastActorPos;
|
||||||
|
|
||||||
|
bool mIsShortcutting; // if shortcutting at the moment
|
||||||
bool mShortcutProhibited; // shortcutting may be prohibited after unsuccessful attempt
|
bool mShortcutProhibited; // shortcutting may be prohibited after unsuccessful attempt
|
||||||
ESM::Pathgrid::Point mShortcutFailPos; // position of last shortcut fail
|
ESM::Pathgrid::Point mShortcutFailPos; // position of last shortcut fail
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue