1
0
Fork 1
mirror of https://github.com/TES3MP/openmw-tes3mp.git synced 2025-01-15 22:19:54 +00:00
openmw-tes3mp/apps/openmw/mwmechanics/aipursue.cpp

86 lines
2.5 KiB
C++
Raw Normal View History

2014-05-03 10:23:22 +00:00
#include "aipursue.hpp"
2014-06-12 21:27:04 +00:00
#include <components/esm/aisequence.hpp>
2015-07-25 02:14:22 +00:00
#include <components/esm/loadmgef.hpp>
2014-06-12 21:27:04 +00:00
#include "../mwbase/environment.hpp"
#include "../mwworld/class.hpp"
#include "../mwworld/action.hpp"
#include "movement.hpp"
#include "creaturestats.hpp"
2014-05-17 15:20:57 +00:00
namespace MWMechanics
{
AiPursue::AiPursue(const MWWorld::Ptr& actor)
: mTargetActorId(actor.getClass().getCreatureStats(actor).getActorId())
{
}
2014-06-12 21:27:04 +00:00
AiPursue::AiPursue(const ESM::AiSequence::AiPursue *pursue)
: mTargetActorId(pursue->mTargetActorId)
{
}
2014-05-17 15:20:57 +00:00
AiPursue *MWMechanics::AiPursue::clone() const
{
2014-05-03 10:23:22 +00:00
return new AiPursue(*this);
}
bool AiPursue::execute (const MWWorld::Ptr& actor, CharacterController& characterController, AiState& state, float duration)
{
if(actor.getClass().getCreatureStats(actor).isDead())
return true;
2014-05-17 15:20:57 +00:00
const MWWorld::Ptr target = MWBase::Environment::get().getWorld()->searchPtrViaActorId(mTargetActorId); //The target to follow
if(target == MWWorld::Ptr() || !target.getRefData().getCount() || !target.getRefData().isEnabled() // Really we should be checking whether the target is currently registered
// with the MechanicsManager
)
2014-05-17 15:20:57 +00:00
return true; //Target doesn't exist
if (isTargetMagicallyHidden(target))
return true;
if(target.getClass().getCreatureStats(target).isDead())
return true;
actor.getClass().getCreatureStats(actor).setDrawState(DrawState_Nothing);
//Set the target desition from the actor
ESM::Pathgrid::Point dest = target.getRefData().getPosition().pos;
if (pathTo(actor, dest, duration, 100)) {
target.getClass().activate(target,actor).get()->execute(actor); //Arrest player when reached
return true;
}
actor.getClass().getCreatureStats(actor).setMovementFlag(MWMechanics::CreatureStats::Flag_Run, true); //Make NPC run
return false;
}
2014-05-17 15:20:57 +00:00
int AiPursue::getTypeId() const
{
2014-05-03 10:23:22 +00:00
return TypeIdPursue;
}
2014-05-17 15:20:57 +00:00
MWWorld::Ptr AiPursue::getTarget() const
{
2014-05-17 15:20:57 +00:00
return MWBase::Environment::get().getWorld()->searchPtrViaActorId(mTargetActorId);
}
2014-05-17 15:20:57 +00:00
2014-06-12 21:27:04 +00:00
void AiPursue::writeState(ESM::AiSequence::AiSequence &sequence) const
{
std::auto_ptr<ESM::AiSequence::AiPursue> pursue(new ESM::AiSequence::AiPursue());
pursue->mTargetActorId = mTargetActorId;
ESM::AiSequence::AiPackageContainer package;
package.mType = ESM::AiSequence::Ai_Pursue;
package.mPackage = pursue.release();
sequence.mPackages.push_back(package);
}
2014-05-17 15:20:57 +00:00
} // namespace MWMechanics