2014-05-03 12:23:22 +02:00
|
|
|
#include "aipursue.hpp"
|
2014-04-02 00:18:22 -04:00
|
|
|
|
2022-01-22 15:58:41 +01:00
|
|
|
#include <components/esm3/aisequence.hpp>
|
2014-06-12 23:27:04 +02:00
|
|
|
|
2014-04-02 00:18:22 -04:00
|
|
|
#include "../mwbase/environment.hpp"
|
2020-05-09 21:14:58 +03:00
|
|
|
#include "../mwbase/mechanicsmanager.hpp"
|
2019-11-18 12:41:11 +04:00
|
|
|
#include "../mwbase/windowmanager.hpp"
|
2019-02-19 01:10:55 +03:00
|
|
|
#include "../mwbase/world.hpp"
|
2014-04-02 00:18:22 -04:00
|
|
|
|
|
|
|
#include "../mwworld/class.hpp"
|
|
|
|
|
2022-07-29 10:47:50 +03:00
|
|
|
#include "actorutil.hpp"
|
2014-04-02 00:18:22 -04:00
|
|
|
#include "creaturestats.hpp"
|
|
|
|
|
2014-05-17 19:20:57 +04:00
|
|
|
namespace MWMechanics
|
|
|
|
{
|
|
|
|
|
|
|
|
AiPursue::AiPursue(const MWWorld::Ptr& actor)
|
2014-04-02 00:18:22 -04:00
|
|
|
{
|
2017-11-21 20:00:51 +04:00
|
|
|
mTargetActorId = actor.getClass().getCreatureStats(actor).getActorId();
|
2014-04-02 00:18:22 -04:00
|
|
|
}
|
2014-06-12 23:27:04 +02:00
|
|
|
|
|
|
|
AiPursue::AiPursue(const ESM::AiSequence::AiPursue* pursue)
|
|
|
|
{
|
2017-11-21 20:00:51 +04:00
|
|
|
mTargetActorId = pursue->mTargetActorId;
|
2014-06-12 23:27:04 +02:00
|
|
|
}
|
|
|
|
|
2015-06-26 17:47:04 +02:00
|
|
|
bool AiPursue::execute(
|
|
|
|
const MWWorld::Ptr& actor, CharacterController& characterController, AiState& state, float duration)
|
2014-04-02 00:18:22 -04:00
|
|
|
{
|
2014-08-23 22:55:32 +02:00
|
|
|
if (actor.getClass().getCreatureStats(actor).isDead())
|
|
|
|
return true;
|
|
|
|
|
2014-05-17 19:20:57 +04:00
|
|
|
const MWWorld::Ptr target
|
|
|
|
= MWBase::Environment::get().getWorld()->searchPtrViaActorId(mTargetActorId); // The target to follow
|
2014-04-02 00:18:22 -04:00
|
|
|
|
2018-11-02 14:24:43 +03:00
|
|
|
// Stop if the target doesn't exist
|
|
|
|
// Really we should be checking whether the target is currently registered with the MechanicsManager
|
|
|
|
if (target == MWWorld::Ptr() || !target.getRefData().getCount() || !target.getRefData().isEnabled())
|
|
|
|
return true;
|
2014-05-03 17:14:59 +02:00
|
|
|
|
2020-08-09 12:48:59 +03:00
|
|
|
if (isTargetMagicallyHidden(target)
|
|
|
|
&& !MWBase::Environment::get().getMechanicsManager()->awarenessCheck(target, actor))
|
2020-05-09 21:14:58 +03:00
|
|
|
return false;
|
2014-12-21 17:34:34 +01:00
|
|
|
|
2018-11-02 14:24:43 +03:00
|
|
|
if (target.getClass().getCreatureStats(target).isDead())
|
2014-08-23 22:55:32 +02:00
|
|
|
return true;
|
|
|
|
|
2022-07-17 19:36:48 +03:00
|
|
|
actor.getClass().getCreatureStats(actor).setDrawState(DrawState::Nothing);
|
2014-07-28 17:28:00 +02:00
|
|
|
|
2018-11-02 14:24:43 +03:00
|
|
|
// Set the target destination
|
|
|
|
const osg::Vec3f dest = target.getRefData().getPosition().asVec3();
|
|
|
|
const osg::Vec3f actorPos = actor.getRefData().getPosition().asVec3();
|
2014-04-02 00:18:22 -04:00
|
|
|
|
2018-11-02 14:24:43 +03:00
|
|
|
const float pathTolerance = 100.f;
|
2017-11-02 20:01:22 +01:00
|
|
|
|
2021-03-03 22:04:10 +03:00
|
|
|
// check the true distance in case the target is far away in Z-direction
|
2021-08-18 22:46:14 +02:00
|
|
|
bool reached = pathTo(actor, dest, duration, pathTolerance, (actorPos - dest).length(), PathType::Partial)
|
2021-03-03 22:04:10 +03:00
|
|
|
&& std::abs(dest.z() - actorPos.z()) < pathTolerance;
|
|
|
|
|
|
|
|
if (reached)
|
2017-11-02 20:01:22 +01:00
|
|
|
{
|
2021-03-03 22:04:10 +03:00
|
|
|
if (!MWBase::Environment::get().getWorld()->getLOS(target, actor))
|
|
|
|
return false;
|
2019-11-18 12:41:11 +04:00
|
|
|
MWBase::Environment::get().getWindowManager()->pushGuiMode(
|
|
|
|
MWGui::GM_Dialogue, actor); // Arrest player when reached
|
2014-04-02 00:18:22 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-05-12 21:05:32 -04:00
|
|
|
actor.getClass().getCreatureStats(actor).setMovementFlag(
|
|
|
|
MWMechanics::CreatureStats::Flag_Run, true); // Make NPC run
|
2014-04-02 00:18:22 -04:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-05-17 19:20:57 +04:00
|
|
|
MWWorld::Ptr AiPursue::getTarget() const
|
2022-04-17 17:15:00 +00:00
|
|
|
{
|
|
|
|
if (!mCachedTarget.isEmpty())
|
2022-09-22 21:26:05 +03:00
|
|
|
{
|
2022-04-17 17:15:00 +00:00
|
|
|
if (mCachedTarget.getRefData().isDeleted() || !mCachedTarget.getRefData().isEnabled())
|
|
|
|
mCachedTarget = MWWorld::Ptr();
|
|
|
|
else
|
|
|
|
return mCachedTarget;
|
2022-09-22 21:26:05 +03:00
|
|
|
}
|
2022-04-17 17:15:00 +00:00
|
|
|
mCachedTarget = MWBase::Environment::get().getWorld()->searchPtrViaActorId(mTargetActorId);
|
|
|
|
return mCachedTarget;
|
|
|
|
}
|
2014-05-17 19:20:57 +04:00
|
|
|
|
2014-06-12 23:27:04 +02:00
|
|
|
void AiPursue::writeState(ESM::AiSequence::AiSequence& sequence) const
|
|
|
|
{
|
2022-05-29 13:24:48 +02:00
|
|
|
auto pursue = std::make_unique<ESM::AiSequence::AiPursue>();
|
2014-06-12 23:27:04 +02:00
|
|
|
pursue->mTargetActorId = mTargetActorId;
|
2022-09-22 21:26:05 +03:00
|
|
|
|
2014-06-12 23:27:04 +02:00
|
|
|
ESM::AiSequence::AiPackageContainer package;
|
|
|
|
package.mType = ESM::AiSequence::Ai_Pursue;
|
2022-02-23 00:39:30 +01:00
|
|
|
package.mPackage = std::move(pursue);
|
|
|
|
sequence.mPackages.push_back(std::move(package));
|
2014-06-12 23:27:04 +02:00
|
|
|
}
|
|
|
|
|
2014-05-17 19:20:57 +04:00
|
|
|
} // namespace MWMechanics
|