Merge branch 'refactor/cache-target' into 'master'

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

See merge request OpenMW/openmw!1772
pull/3226/head
psi29a 3 years ago
commit c17b6de787

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

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

Loading…
Cancel
Save