2012-11-14 17:42:04 +00:00
|
|
|
#include "aiescort.hpp"
|
2013-05-08 22:27:20 +00:00
|
|
|
|
2013-05-09 03:02:24 +00:00
|
|
|
#include "movement.hpp"
|
2013-05-08 22:27:20 +00:00
|
|
|
|
|
|
|
#include "../mwworld/class.hpp"
|
|
|
|
#include "../mwworld/timestamp.hpp"
|
2013-05-09 03:02:24 +00:00
|
|
|
#include "../mwbase/world.hpp"
|
2013-05-08 22:27:20 +00:00
|
|
|
#include "../mwbase/environment.hpp"
|
2013-05-09 03:02:24 +00:00
|
|
|
#include "../mwbase/mechanicsmanager.hpp"
|
2013-05-08 22:27:20 +00:00
|
|
|
|
2014-01-29 19:29:07 +00:00
|
|
|
#include "steering.hpp"
|
|
|
|
|
2013-05-08 22:27:20 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
float sgn(float a)
|
|
|
|
{
|
2013-06-01 00:49:52 +00:00
|
|
|
if(a > 0)
|
|
|
|
return 1.0;
|
|
|
|
return -1.0;
|
2013-05-08 22:27:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-09 03:02:24 +00:00
|
|
|
/*
|
|
|
|
TODO: Test vanilla behavior on passing x0, y0, and z0 with duration of anything including 0.
|
|
|
|
TODO: Different behavior for AIEscort a d x y z and AIEscortCell a c d x y z.
|
|
|
|
TODO: Take account for actors being in different cells.
|
|
|
|
*/
|
2013-05-08 22:27:20 +00:00
|
|
|
|
2013-06-01 00:49:52 +00:00
|
|
|
namespace MWMechanics
|
2013-05-08 22:27:20 +00:00
|
|
|
{
|
2013-06-01 00:49:52 +00:00
|
|
|
AiEscort::AiEscort(const std::string &actorId, int duration, float x, float y, float z)
|
|
|
|
: mActorId(actorId), mX(x), mY(y), mZ(z), mDuration(duration)
|
2014-01-12 21:47:22 +00:00
|
|
|
, mCellX(std::numeric_limits<int>::max())
|
|
|
|
, mCellY(std::numeric_limits<int>::max())
|
2013-06-01 00:49:52 +00:00
|
|
|
{
|
|
|
|
mMaxDist = 470;
|
2013-05-09 03:02:24 +00:00
|
|
|
|
2014-01-29 19:29:07 +00:00
|
|
|
// The CS Help File states that if a duration is given, the AI package will run for that long
|
2013-06-01 00:49:52 +00:00
|
|
|
// BUT if a location is givin, it "trumps" the duration so it will simply escort to that location.
|
|
|
|
if(mX != 0 || mY != 0 || mZ != 0)
|
|
|
|
mDuration = 0;
|
2013-05-08 22:27:20 +00:00
|
|
|
|
2013-06-01 00:49:52 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
MWWorld::TimeStamp startTime = MWBase::Environment::get().getWorld()->getTimeStamp();
|
|
|
|
mStartingSecond = ((startTime.getHour() - int(startTime.getHour())) * 100);
|
|
|
|
}
|
2013-05-08 22:27:20 +00:00
|
|
|
}
|
2012-11-30 00:16:16 +00:00
|
|
|
|
2013-06-01 00:49:52 +00:00
|
|
|
AiEscort::AiEscort(const std::string &actorId, const std::string &cellId,int duration, float x, float y, float z)
|
|
|
|
: mActorId(actorId), mCellId(cellId), mX(x), mY(y), mZ(z), mDuration(duration)
|
2014-01-12 21:47:22 +00:00
|
|
|
, mCellX(std::numeric_limits<int>::max())
|
|
|
|
, mCellY(std::numeric_limits<int>::max())
|
2013-06-01 00:49:52 +00:00
|
|
|
{
|
|
|
|
mMaxDist = 470;
|
2013-05-09 03:02:24 +00:00
|
|
|
|
2014-01-29 19:29:07 +00:00
|
|
|
// The CS Help File states that if a duration is given, the AI package will run for that long
|
2013-06-01 00:49:52 +00:00
|
|
|
// BUT if a location is givin, it "trumps" the duration so it will simply escort to that location.
|
|
|
|
if(mX != 0 || mY != 0 || mZ != 0)
|
|
|
|
mDuration = 0;
|
2013-05-08 22:27:20 +00:00
|
|
|
|
2013-06-01 00:49:52 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
MWWorld::TimeStamp startTime = MWBase::Environment::get().getWorld()->getTimeStamp();
|
|
|
|
mStartingSecond = ((startTime.getHour() - int(startTime.getHour())) * 100);
|
|
|
|
}
|
2013-05-08 22:27:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-01 00:49:52 +00:00
|
|
|
AiEscort *MWMechanics::AiEscort::clone() const
|
2013-05-08 22:27:20 +00:00
|
|
|
{
|
2013-06-01 00:49:52 +00:00
|
|
|
return new AiEscort(*this);
|
2013-05-08 22:27:20 +00:00
|
|
|
}
|
|
|
|
|
2013-10-30 19:42:50 +00:00
|
|
|
bool AiEscort::execute (const MWWorld::Ptr& actor,float duration)
|
2013-06-01 00:49:52 +00:00
|
|
|
{
|
|
|
|
// If AiEscort has ran for as long or longer then the duration specified
|
|
|
|
// and the duration is not infinite, the package is complete.
|
|
|
|
if(mDuration != 0)
|
|
|
|
{
|
|
|
|
MWWorld::TimeStamp current = MWBase::Environment::get().getWorld()->getTimeStamp();
|
|
|
|
unsigned int currentSecond = ((current.getHour() - int(current.getHour())) * 100);
|
|
|
|
if(currentSecond - mStartingSecond >= mDuration)
|
|
|
|
return true;
|
|
|
|
}
|
2013-05-08 22:27:20 +00:00
|
|
|
|
2014-01-08 17:39:44 +00:00
|
|
|
MWWorld::Ptr player = MWBase::Environment::get().getWorld()->getPlayerPtr();
|
2013-06-01 00:49:52 +00:00
|
|
|
ESM::Position pos = actor.getRefData().getPosition();
|
2014-01-12 21:47:22 +00:00
|
|
|
bool cellChange = actor.getCell()->mCell->mData.mX != mCellX || actor.getCell()->mCell->mData.mY != mCellY;
|
2013-05-08 22:27:20 +00:00
|
|
|
|
2013-06-01 00:49:52 +00:00
|
|
|
if(actor.getCell()->mCell->mData.mX != player.getCell()->mCell->mData.mX)
|
2013-05-08 22:27:20 +00:00
|
|
|
{
|
2013-06-01 00:49:52 +00:00
|
|
|
int sideX = sgn(actor.getCell()->mCell->mData.mX - player.getCell()->mCell->mData.mX);
|
2014-01-29 19:29:07 +00:00
|
|
|
// Check if actor is near the border of an inactive cell. If so, pause walking.
|
2013-06-01 00:49:52 +00:00
|
|
|
if(sideX * (pos.pos[0] - actor.getCell()->mCell->mData.mX * ESM::Land::REAL_SIZE) > sideX * (ESM::Land::REAL_SIZE /
|
|
|
|
2.0 - 200))
|
|
|
|
{
|
|
|
|
MWWorld::Class::get(actor).getMovementSettings(actor).mPosition[1] = 0;
|
2014-01-29 19:29:07 +00:00
|
|
|
return false;
|
2013-06-01 00:49:52 +00:00
|
|
|
}
|
2013-05-08 22:27:20 +00:00
|
|
|
}
|
2013-06-01 00:49:52 +00:00
|
|
|
if(actor.getCell()->mCell->mData.mY != player.getCell()->mCell->mData.mY)
|
2013-05-08 22:27:20 +00:00
|
|
|
{
|
2013-06-01 00:49:52 +00:00
|
|
|
int sideY = sgn(actor.getCell()->mCell->mData.mY - player.getCell()->mCell->mData.mY);
|
2014-01-29 19:29:07 +00:00
|
|
|
// Check if actor is near the border of an inactive cell. If so, pause walking.
|
2013-06-01 00:49:52 +00:00
|
|
|
if(sideY*(pos.pos[1] - actor.getCell()->mCell->mData.mY * ESM::Land::REAL_SIZE) > sideY * (ESM::Land::REAL_SIZE /
|
|
|
|
2.0 - 200))
|
|
|
|
{
|
|
|
|
MWWorld::Class::get(actor).getMovementSettings(actor).mPosition[1] = 0;
|
2014-01-29 19:29:07 +00:00
|
|
|
return false;
|
2013-06-01 00:49:52 +00:00
|
|
|
}
|
2013-05-08 22:27:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-01 00:49:52 +00:00
|
|
|
if(!mPathFinder.isPathConstructed() || cellChange)
|
2013-05-08 22:27:20 +00:00
|
|
|
{
|
2014-01-12 21:47:22 +00:00
|
|
|
mCellX = actor.getCell()->mCell->mData.mX;
|
|
|
|
mCellY = actor.getCell()->mCell->mData.mY;
|
2013-06-01 00:49:52 +00:00
|
|
|
|
|
|
|
ESM::Pathgrid::Point dest;
|
|
|
|
dest.mX = mX;
|
|
|
|
dest.mY = mY;
|
|
|
|
dest.mZ = mZ;
|
|
|
|
|
|
|
|
ESM::Pathgrid::Point start;
|
|
|
|
start.mX = pos.pos[0];
|
|
|
|
start.mY = pos.pos[1];
|
|
|
|
start.mZ = pos.pos[2];
|
|
|
|
|
2014-01-12 17:42:31 +00:00
|
|
|
mPathFinder.buildPath(start, dest, actor.getCell(), true);
|
2013-05-08 22:27:20 +00:00
|
|
|
}
|
|
|
|
|
2013-06-01 00:49:52 +00:00
|
|
|
if(mPathFinder.checkPathCompleted(pos.pos[0],pos.pos[1],pos.pos[2]))
|
|
|
|
{
|
|
|
|
MWWorld::Class::get(actor).getMovementSettings(actor).mPosition[1] = 0;
|
|
|
|
return true;
|
|
|
|
}
|
2013-05-08 22:27:20 +00:00
|
|
|
|
2013-06-01 00:49:52 +00:00
|
|
|
const MWWorld::Ptr follower = MWBase::Environment::get().getWorld()->getPtr(mActorId, false);
|
|
|
|
const float* const leaderPos = actor.getRefData().getPosition().pos;
|
|
|
|
const float* const followerPos = follower.getRefData().getPosition().pos;
|
|
|
|
double differenceBetween[3];
|
2013-05-08 22:27:20 +00:00
|
|
|
|
2013-06-01 00:49:52 +00:00
|
|
|
for (short counter = 0; counter < 3; counter++)
|
|
|
|
differenceBetween[counter] = (leaderPos[counter] - followerPos[counter]);
|
2013-05-09 00:19:47 +00:00
|
|
|
|
2013-06-01 00:49:52 +00:00
|
|
|
float distanceBetweenResult =
|
|
|
|
(differenceBetween[0] * differenceBetween[0]) + (differenceBetween[1] * differenceBetween[1]) + (differenceBetween[2] *
|
|
|
|
differenceBetween[2]);
|
2013-05-09 00:19:47 +00:00
|
|
|
|
2013-06-01 00:49:52 +00:00
|
|
|
if(distanceBetweenResult <= mMaxDist * mMaxDist)
|
|
|
|
{
|
|
|
|
float zAngle = mPathFinder.getZAngleToNext(pos.pos[0], pos.pos[1]);
|
2014-01-29 19:29:07 +00:00
|
|
|
zTurn(actor, Ogre::Degree(zAngle));
|
2013-06-01 00:49:52 +00:00
|
|
|
MWWorld::Class::get(actor).getMovementSettings(actor).mPosition[1] = 1;
|
|
|
|
mMaxDist = 470;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Stop moving if the player is to far away
|
|
|
|
MWBase::Environment::get().getMechanicsManager()->playAnimationGroup(actor, "idle3", 0, 1);
|
|
|
|
MWWorld::Class::get(actor).getMovementSettings(actor).mPosition[1] = 0;
|
|
|
|
mMaxDist = 330;
|
|
|
|
}
|
2013-05-09 00:19:47 +00:00
|
|
|
|
2013-06-01 00:49:52 +00:00
|
|
|
return false;
|
2013-05-08 22:27:20 +00:00
|
|
|
}
|
2013-06-01 00:49:52 +00:00
|
|
|
|
|
|
|
int AiEscort::getTypeId() const
|
2013-05-08 22:27:20 +00:00
|
|
|
{
|
2014-01-07 00:12:37 +00:00
|
|
|
return TypeIdEscort;
|
2013-05-08 22:27:20 +00:00
|
|
|
}
|
2012-11-30 00:16:16 +00:00
|
|
|
}
|
|
|
|
|