From 56a1505885c09e1a7088c4c243797f84025babbd Mon Sep 17 00:00:00 2001 From: Matt <3397065-ZehMatt@users.noreply.gitlab.com> Date: Sun, 17 Apr 2022 17:15:00 +0000 Subject: [PATCH] Cache the target for ai packages instead of looking for it every frame --- apps/openmw/mwmechanics/aicombat.cpp | 6 +++++- apps/openmw/mwmechanics/aipackage.cpp | 22 +++++++++++++++++----- apps/openmw/mwmechanics/aipackage.hpp | 9 +++------ apps/openmw/mwmechanics/aipursue.cpp | 10 +++++++++- 4 files changed, 34 insertions(+), 13 deletions(-) diff --git a/apps/openmw/mwmechanics/aicombat.cpp b/apps/openmw/mwmechanics/aicombat.cpp index cbd62d37ff..bd6bd385e8 100644 --- a/apps/openmw/mwmechanics/aicombat.cpp +++ b/apps/openmw/mwmechanics/aicombat.cpp @@ -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 diff --git a/apps/openmw/mwmechanics/aipackage.cpp b/apps/openmw/mwmechanics/aipackage.cpp index e532e71478..7057a99393 100644 --- a/apps/openmw/mwmechanics/aipackage.cpp +++ b/apps/openmw/mwmechanics/aipackage.cpp @@ -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(); diff --git a/apps/openmw/mwmechanics/aipackage.hpp b/apps/openmw/mwmechanics/aipackage.hpp index 498c6e3050..4e5dfcab6b 100644 --- a/apps/openmw/mwmechanics/aipackage.hpp +++ b/apps/openmw/mwmechanics/aipackage.hpp @@ -4,17 +4,13 @@ #include #include - + #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 diff --git a/apps/openmw/mwmechanics/aipursue.cpp b/apps/openmw/mwmechanics/aipursue.cpp index 252bff58f8..25dd08436b 100644 --- a/apps/openmw/mwmechanics/aipursue.cpp +++ b/apps/openmw/mwmechanics/aipursue.cpp @@ -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