From fa14df62b7e863ba17ee7f82dd3363e0bba62b6f Mon Sep 17 00:00:00 2001 From: mrcheko Date: Sun, 18 May 2014 23:15:22 +0400 Subject: [PATCH] fix crash on disappeared target --- apps/openmw/mwmechanics/aisequence.cpp | 28 ++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/apps/openmw/mwmechanics/aisequence.cpp b/apps/openmw/mwmechanics/aisequence.cpp index 9dc2380f85..6a5cf572a9 100644 --- a/apps/openmw/mwmechanics/aisequence.cpp +++ b/apps/openmw/mwmechanics/aisequence.cpp @@ -141,17 +141,33 @@ void AiSequence::execute (const MWWorld::Ptr& actor,float duration) { if ((*it)->getTypeId() != AiPackage::TypeIdCombat) break; - ESM::Position &targetPos = static_cast(*it)->getTarget().getRefData().getPosition(); + MWWorld::Ptr target = static_cast(*it)->getTarget(); - float distTo = (Ogre::Vector3(targetPos.pos) - vActorPos).length(); - if (distTo < nearestDist) + // target disappeared (e.g. summoned creatures) + if (target.isEmpty()) { - nearestDist = distTo; - itActualCombat = it; + mPackages.erase(it++); + } + else + { + ESM::Position &targetPos = target.getRefData().getPosition(); + + float distTo = (Ogre::Vector3(targetPos.pos) - vActorPos).length(); + if (distTo < nearestDist) + { + nearestDist = distTo; + itActualCombat = it; + } } } - if (mPackages.begin() != itActualCombat) + // all targets disappeared + if (nearestDist == std::numeric_limits::max()) + { + mDone = true; + return; + } + else if (mPackages.begin() != itActualCombat) { // move combat package with nearest target to the front mPackages.splice(mPackages.begin(), mPackages, itActualCombat);