Cache the target for ai packages instead of looking for it every frame

pull/3226/head
Matt 3 years ago committed by psi29a
parent 68799cfd2b
commit 56a1505885

@ -451,7 +451,11 @@ namespace MWMechanics
MWWorld::Ptr AiCombat::getTarget() const
{
return MWBase::Environment::get().getWorld()->searchPtrViaActorId(mTargetActorId);
if (mCachedTarget.isEmpty() || mCachedTarget.getRefData().isDeleted() || !mCachedTarget.getRefData().isEnabled())
{
mCachedTarget = MWBase::Environment::get().getWorld()->searchPtrViaActorId(mTargetActorId);
}
return mCachedTarget;
}
void AiCombat::writeState(ESM::AiSequence::AiSequence &sequence) const

@ -47,6 +47,7 @@ MWMechanics::AiPackage::AiPackage(AiPackageTypeId typeId, const Options& options
mReaction(MWBase::Environment::get().getWorld()->getPrng()),
mTargetActorRefId(""),
mTargetActorId(-1),
mCachedTarget(),
mRotateOnTheRunChecks(0),
mIsShortcutting(false),
mShortcutProhibited(false),
@ -56,6 +57,14 @@ MWMechanics::AiPackage::AiPackage(AiPackageTypeId typeId, const Options& options
MWWorld::Ptr MWMechanics::AiPackage::getTarget() const
{
if (!mCachedTarget.isEmpty())
{
if (mCachedTarget.getRefData().isDeleted() || !mCachedTarget.getRefData().isEnabled())
mCachedTarget = MWWorld::Ptr();
else
return mCachedTarget;
}
if (mTargetActorId == -2)
return MWWorld::Ptr();
@ -66,20 +75,22 @@ MWWorld::Ptr MWMechanics::AiPackage::getTarget() const
mTargetActorId = -2;
return MWWorld::Ptr();
}
MWWorld::Ptr target = MWBase::Environment::get().getWorld()->searchPtr(mTargetActorRefId, false);
if (target.isEmpty())
mCachedTarget = MWBase::Environment::get().getWorld()->searchPtr(mTargetActorRefId, false);
if (mCachedTarget.isEmpty())
{
mTargetActorId = -2;
return target;
return mCachedTarget;
}
else
mTargetActorId = target.getClass().getCreatureStats(target).getActorId();
mTargetActorId = mCachedTarget.getClass().getCreatureStats(mCachedTarget).getActorId();
}
if (mTargetActorId != -1)
return MWBase::Environment::get().getWorld()->searchPtrViaActorId(mTargetActorId);
mCachedTarget = MWBase::Environment::get().getWorld()->searchPtrViaActorId(mTargetActorId);
else
return MWWorld::Ptr();
return mCachedTarget;
}
void MWMechanics::AiPackage::reset()
@ -89,6 +100,7 @@ void MWMechanics::AiPackage::reset()
mIsShortcutting = false;
mShortcutProhibited = false;
mShortcutFailPos = osg::Vec3f();
mCachedTarget = MWWorld::Ptr();
mPathFinder.clearPath();
mObstacleCheck.clear();

@ -4,17 +4,13 @@
#include <memory>
#include <components/detournavigator/areatype.hpp>
#include "pathfinding.hpp"
#include "obstacle.hpp"
#include "aistate.hpp"
#include "aipackagetypeid.hpp"
#include "aitimer.hpp"
namespace MWWorld
{
class Ptr;
}
#include "../mwworld/ptr.hpp"
namespace ESM
{
@ -165,6 +161,7 @@ namespace MWMechanics
std::string mTargetActorRefId;
mutable int mTargetActorId;
mutable MWWorld::Ptr mCachedTarget;
short mRotateOnTheRunChecks; // attempts to check rotation to the pathpoint on the run possibility

@ -71,7 +71,15 @@ bool AiPursue::execute (const MWWorld::Ptr& actor, CharacterController& characte
MWWorld::Ptr AiPursue::getTarget() const
{
return MWBase::Environment::get().getWorld()->searchPtrViaActorId(mTargetActorId);
if (!mCachedTarget.isEmpty())
{
if (mCachedTarget.getRefData().isDeleted() || !mCachedTarget.getRefData().isEnabled())
mCachedTarget = MWWorld::Ptr();
else
return mCachedTarget;
}
mCachedTarget = MWBase::Environment::get().getWorld()->searchPtrViaActorId(mTargetActorId);
return mCachedTarget;
}
void AiPursue::writeState(ESM::AiSequence::AiSequence &sequence) const

Loading…
Cancel
Save