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

119 lines
3.4 KiB
C++
Raw Normal View History

#include "aifollow.hpp"
2012-11-15 21:32:15 +00:00
#include <iostream>
#include "../mwbase/world.hpp"
#include "../mwbase/environment.hpp"
#include "../mwworld/class.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"
2012-11-16 19:28:20 +00:00
MWMechanics::AiFollow::AiFollow(const std::string &actorId,float duration, float x, float y, float z)
: mDuration(duration), mX(x), mY(y), mZ(z), mActorId(actorId), mCellId(""), mTimer(0), mStuckTimer(0)
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)
: mDuration(duration), mX(x), mY(y), mZ(z), mActorId(actorId), mCellId(cellId), mTimer(0), mStuckTimer(0)
{
}
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()->getPtr(mActorId, false);
mTimer = mTimer + duration;
mStuckTimer = mStuckTimer + duration;
mTotalTime = mTotalTime + duration;
2014-01-11 19:32:38 +00:00
ESM::Position pos = actor.getRefData().getPosition();
if(mTotalTime > mDuration && mDuration != 0)
return true;
if((pos.pos[0]-mX)*(pos.pos[0]-mX) +
2014-01-11 19:32:38 +00:00
(pos.pos[1]-mY)*(pos.pos[1]-mY) +
(pos.pos[2]-mZ)*(pos.pos[2]-mZ) < 100*100)
{
if(actor.getCell()->isExterior())
{
if(mCellId == "")
return true;
}
else
{
if(mCellId == actor.getCell()->mCell->mName)
return true;
}
}
2014-01-29 19:29:07 +00:00
ESM::Pathgrid::Point dest;
dest.mX = target.getRefData().getPosition().pos[0];
dest.mY = target.getRefData().getPosition().pos[1];
dest.mZ = target.getRefData().getPosition().pos[2];
2014-01-29 19:29:07 +00:00
ESM::Pathgrid::Point start;
start.mX = pos.pos[0];
start.mY = pos.pos[1];
2014-01-24 18:13:23 +00:00
start.mZ = pos.pos[2];
if(mPathFinder.getPath().empty())
mPathFinder.buildPath(start, dest, actor.getCell(), true);
if(mTimer > 0.25)
{
2014-01-07 20:10:57 +00:00
if(!mPathFinder.getPath().empty())
{
ESM::Pathgrid::Point lastPos = mPathFinder.getPath().back();
2014-01-07 20:10:57 +00:00
if((dest.mX - lastPos.mX)*(dest.mX - lastPos.mX)
+(dest.mY - lastPos.mY)*(dest.mY - lastPos.mY)
+(dest.mZ - lastPos.mZ)*(dest.mZ - lastPos.mZ)
> 100*100)
2014-01-24 18:13:23 +00:00
mPathFinder.addPointToPath(dest);
2014-01-07 20:10:57 +00:00
}
mTimer = 0;
}
2014-01-07 20:10:57 +00:00
if(mStuckTimer>0.5)
{
if((mStuckPos.pos[0] - pos.pos[0])*(mStuckPos.pos[0] - pos.pos[0])
+(mStuckPos.pos[1] - pos.pos[1])*(mStuckPos.pos[1] - pos.pos[1])
2014-01-24 18:13:23 +00:00
+(mStuckPos.pos[2] - pos.pos[2])*(mStuckPos.pos[2] - pos.pos[2]) < 100) //NPC is stuck
mPathFinder.buildPath(start, dest, actor.getCell(), true);
2014-01-24 18:13:23 +00:00
2014-01-07 20:10:57 +00:00
mStuckTimer = 0;
mStuckPos = pos;
}
if(!mPathFinder.checkPathCompleted(pos.pos[0],pos.pos[1],pos.pos[2]))
{
2014-01-29 19:29:07 +00:00
zTurn(actor, Ogre::Degree(mPathFinder.getZAngleToNext(pos.pos[0], pos.pos[1])));
}
if((dest.mX - pos.pos[0])*(dest.mX - pos.pos[0])+(dest.mY - pos.pos[1])*(dest.mY - pos.pos[1])+(dest.mZ - pos.pos[2])*(dest.mZ - pos.pos[2])
< 100*100)
actor.getClass().getMovementSettings(actor).mPosition[1] = 0;
else
2014-01-29 19:29:07 +00:00
actor.getClass().getMovementSettings(actor).mPosition[1] = 1;
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-29 19:29:07 +00:00
MWMechanics::AiFollow *MWMechanics::AiFollow::clone() const
{
return new AiFollow(*this);
}
int MWMechanics::AiFollow::getTypeId() const
{
return TypeIdFollow;
}