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

115 lines
3.9 KiB
C++
Raw Normal View History

2012-11-14 17:42:04 +00:00
#include "aitravel.hpp"
2014-06-12 21:27:04 +00:00
#include <components/esm/aisequence.hpp>
#include "../mwbase/environment.hpp"
#include "../mwbase/world.hpp"
2014-02-23 19:11:05 +00:00
#include "../mwworld/class.hpp"
2014-02-23 19:11:05 +00:00
#include "../mwworld/cellstore.hpp"
2014-01-29 19:29:07 +00:00
#include "movement.hpp"
2014-04-24 03:17:01 +00:00
#include "creaturestats.hpp"
2014-01-29 19:29:07 +00:00
namespace
{
bool isWithinMaxRange(const osg::Vec3f& pos1, const osg::Vec3f& pos2)
{
// Maximum travel distance for vanilla compatibility.
// Was likely meant to prevent NPCs walking into non-loaded exterior cells, but for some reason is used in interior cells as well.
// We can make this configurable at some point, but the default *must* be the below value. Anything else will break shoddily-written content (*cough* MW *cough*) in bizarre ways.
return (pos1 - pos2).length2() <= 7168*7168;
}
}
namespace MWMechanics
{
2017-12-01 15:58:42 +00:00
AiTravel::AiTravel(float x, float y, float z, bool hidden)
: mX(x),mY(y),mZ(z),mHidden(hidden)
{
}
2013-03-31 17:30:03 +00:00
2014-06-12 21:27:04 +00:00
AiTravel::AiTravel(const ESM::AiSequence::AiTravel *travel)
: mX(travel->mData.mX), mY(travel->mData.mY), mZ(travel->mData.mZ), mHidden(travel->mHidden)
2014-06-12 21:27:04 +00:00
{
}
AiTravel *MWMechanics::AiTravel::clone() const
{
return new AiTravel(*this);
}
bool AiTravel::execute (const MWWorld::Ptr& actor, CharacterController& characterController, AiState& state, float duration)
{
auto& stats = actor.getClass().getCreatureStats(actor);
if (stats.isTurningToPlayer() || stats.getGreetingState() == Greet_InProgress)
return false;
2018-11-02 11:24:43 +00:00
const osg::Vec3f actorPos(actor.getRefData().getPosition().asVec3());
const osg::Vec3f targetPos(mX, mY, mZ);
stats.setMovementFlag(CreatureStats::Flag_Run, false);
stats.setDrawState(DrawState_Nothing);
// Note: we should cancel internal "return after combat" package, if original location is too far away
2018-11-02 11:24:43 +00:00
if (!isWithinMaxRange(targetPos, actorPos))
return mHidden;
// Unfortunately, with vanilla assets destination is sometimes blocked by other actor.
// If we got close to target, check for actors nearby. If they are, finish AI package.
int destinationTolerance = 64;
2018-11-02 11:24:43 +00:00
if (distance(actorPos, targetPos) <= destinationTolerance)
{
std::vector<MWWorld::Ptr> targetActors;
std::pair<MWWorld::Ptr, osg::Vec3f> result = MWBase::Environment::get().getWorld()->getHitContact(actor, destinationTolerance, targetActors);
if (!result.first.isEmpty())
{
actor.getClass().getMovementSettings(actor).mPosition[1] = 0;
return true;
}
}
2018-11-02 11:24:43 +00:00
if (pathTo(actor, targetPos, duration))
{
2015-06-11 06:28:31 +00:00
actor.getClass().getMovementSettings(actor).mPosition[1] = 0;
return true;
}
2015-06-11 06:28:31 +00:00
return false;
}
int AiTravel::getTypeId() const
{
2017-12-01 15:58:42 +00:00
return mHidden ? TypeIdInternalTravel : TypeIdTravel;
}
2014-06-12 21:27:04 +00:00
void AiTravel::fastForward(const MWWorld::Ptr& actor, AiState& state)
{
2015-06-03 17:41:19 +00:00
if (!isWithinMaxRange(osg::Vec3f(mX, mY, mZ), actor.getRefData().getPosition().asVec3()))
return;
// does not do any validation on the travel target (whether it's in air, inside collision geometry, etc),
// that is the user's responsibility
MWBase::Environment::get().getWorld()->moveObject(actor, mX, mY, mZ);
actor.getClass().adjustPosition(actor, false);
reset();
}
2014-06-12 21:27:04 +00:00
void AiTravel::writeState(ESM::AiSequence::AiSequence &sequence) const
{
std::unique_ptr<ESM::AiSequence::AiTravel> travel(new ESM::AiSequence::AiTravel());
2014-06-12 21:27:04 +00:00
travel->mData.mX = mX;
travel->mData.mY = mY;
travel->mData.mZ = mZ;
travel->mHidden = mHidden;
2014-06-12 21:27:04 +00:00
ESM::AiSequence::AiPackageContainer package;
package.mType = ESM::AiSequence::Ai_Travel;
package.mPackage = travel.release();
sequence.mPackages.push_back(package);
}
}