openmw-tes3coop/apps/openmw/mwmechanics/aifollow.cpp

124 lines
4.1 KiB
C++
Raw Normal View History

#include "aifollow.hpp"
2014-06-12 21:27:04 +00:00
2012-11-15 21:32:15 +00:00
#include <iostream>
2014-06-12 21:27:04 +00:00
#include <components/esm/aisequence.hpp>
#include "../mwbase/world.hpp"
#include "../mwbase/environment.hpp"
#include "../mwworld/class.hpp"
2014-03-29 17:36:32 +00:00
#include "../mwworld/cellstore.hpp"
#include "creaturestats.hpp"
#include "movement.hpp"
2014-01-24 18:13:23 +00:00
#include <OgreMath.h>
2014-01-29 19:29:07 +00:00
#include "steering.hpp"
MWMechanics::AiFollow::AiFollow(const std::string &actorId,float duration, float x, float y, float z)
2014-06-12 21:27:04 +00:00
: mAlwaysFollow(false), mRemainingDuration(duration), mX(x), mY(y), mZ(z), mActorId(actorId), mCellId("")
2014-01-29 19:29:07 +00:00
{
}
MWMechanics::AiFollow::AiFollow(const std::string &actorId,const std::string &cellId,float duration, float x, float y, float z)
2014-06-12 21:27:04 +00:00
: mAlwaysFollow(false), mRemainingDuration(duration), mX(x), mY(y), mZ(z), mActorId(actorId), mCellId(cellId)
2014-03-05 10:24:39 +00:00
{
}
MWMechanics::AiFollow::AiFollow(const std::string &actorId)
2014-06-12 21:27:04 +00:00
: mAlwaysFollow(true), mRemainingDuration(0), mX(0), mY(0), mZ(0), mActorId(actorId), mCellId("")
2014-01-29 19:29:07 +00:00
{
}
2012-11-15 21:32:15 +00:00
bool MWMechanics::AiFollow::execute (const MWWorld::Ptr& actor,float duration)
2012-11-15 21:32:15 +00:00
{
const MWWorld::Ptr target = MWBase::Environment::get().getWorld()->searchPtr(mActorId, false); //The target to follow
if(target == MWWorld::Ptr()) return true; //Target doesn't exist
ESM::Position pos = actor.getRefData().getPosition(); //position of the actor
if(!mAlwaysFollow) //Update if you only follow for a bit
{
2014-06-12 21:27:04 +00:00
//Check if we've run out of time
if (mRemainingDuration != 0)
{
mRemainingDuration -= duration;
if (duration <= 0)
return true;
}
2014-03-05 10:24:39 +00:00
if((pos.pos[0]-mX)*(pos.pos[0]-mX) +
(pos.pos[1]-mY)*(pos.pos[1]-mY) +
(pos.pos[2]-mZ)*(pos.pos[2]-mZ) < 100*100) //Close-ish to final position
{
if(actor.getCell()->isExterior()) //Outside?
2014-03-05 10:24:39 +00:00
{
if(mCellId == "") //No cell to travel to
2014-03-05 10:24:39 +00:00
return true;
}
else
{
if(mCellId == actor.getCell()->getCell()->mName) //Cell to travel to
2014-03-05 10:24:39 +00:00
return true;
}
}
}
2014-06-12 21:27:04 +00:00
//Set the target destination from the actor
ESM::Pathgrid::Point dest = target.getRefData().getPosition().pos;
if(distance(dest, pos.pos[0], pos.pos[1], pos.pos[2]) < 100) //Stop when you get close
2014-01-29 19:29:07 +00:00
actor.getClass().getMovementSettings(actor).mPosition[1] = 0;
else {
pathTo(actor, dest, duration); //Go to the destination
}
//Check if you're far away
if(distance(dest, pos.pos[0], pos.pos[1], pos.pos[2]) > 1000)
actor.getClass().getCreatureStats(actor).setMovementFlag(MWMechanics::CreatureStats::Flag_Run, true); //Make NPC run
else if(distance(dest, pos.pos[0], pos.pos[1], pos.pos[2]) < 800) //Have a bit of a dead zone, otherwise npc will constantly flip between running and not when right on the edge of the running threshhold
actor.getClass().getCreatureStats(actor).setMovementFlag(MWMechanics::CreatureStats::Flag_Run, false); //make NPC walk
return false;
2012-11-15 21:32:15 +00:00
}
2014-01-12 13:02:40 +00:00
std::string MWMechanics::AiFollow::getFollowedActor()
{
return mActorId;
2014-01-12 13:02:40 +00:00
}
2014-01-29 19:29:07 +00:00
MWMechanics::AiFollow *MWMechanics::AiFollow::clone() const
{
return new AiFollow(*this);
}
2014-06-12 21:27:04 +00:00
int MWMechanics::AiFollow::getTypeId() const
2014-01-29 19:29:07 +00:00
{
return TypeIdFollow;
}
2014-06-12 21:27:04 +00:00
void MWMechanics::AiFollow::writeState(ESM::AiSequence::AiSequence &sequence) const
{
std::auto_ptr<ESM::AiSequence::AiFollow> follow(new ESM::AiSequence::AiFollow());
follow->mData.mX = mX;
follow->mData.mY = mY;
follow->mData.mZ = mZ;
follow->mTargetId = mActorId;
follow->mRemainingDuration = mRemainingDuration;
follow->mCellId = mCellId;
follow->mAlwaysFollow = mAlwaysFollow;
ESM::AiSequence::AiPackageContainer package;
package.mType = ESM::AiSequence::Ai_Follow;
package.mPackage = follow.release();
sequence.mPackages.push_back(package);
}
MWMechanics::AiFollow::AiFollow(const ESM::AiSequence::AiFollow *follow)
: mAlwaysFollow(follow->mAlwaysFollow), mRemainingDuration(follow->mRemainingDuration)
, mX(follow->mData.mX), mY(follow->mData.mY), mZ(follow->mData.mZ)
, mActorId(follow->mTargetId), mCellId(follow->mCellId)
{
}