2012-11-14 18:42:04 +01:00
|
|
|
#include "aitravel.hpp"
|
|
|
|
|
2021-09-29 00:42:29 +02:00
|
|
|
#include <algorithm>
|
|
|
|
|
2022-06-17 00:28:44 +02:00
|
|
|
#include <components/detournavigator/agentbounds.hpp>
|
2022-01-22 15:58:41 +01:00
|
|
|
#include <components/esm3/aisequence.hpp>
|
2014-06-12 23:27:04 +02:00
|
|
|
|
2013-03-06 20:31:47 +00:00
|
|
|
#include "../mwbase/environment.hpp"
|
2020-05-17 04:06:39 +03:00
|
|
|
#include "../mwbase/mechanicsmanager.hpp"
|
2018-09-21 16:34:23 +04:00
|
|
|
#include "../mwbase/world.hpp"
|
2014-02-23 20:11:05 +01:00
|
|
|
|
|
|
|
#include "../mwworld/cellstore.hpp"
|
2013-05-31 17:49:52 -07:00
|
|
|
#include "../mwworld/class.hpp"
|
2013-03-06 20:31:47 +00:00
|
|
|
|
2014-04-24 05:17:01 +02:00
|
|
|
#include "creaturestats.hpp"
|
2014-01-29 20:29:07 +01:00
|
|
|
#include "movement.hpp"
|
|
|
|
|
2016-01-19 14:51:37 +01:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
2022-06-19 22:21:41 +03:00
|
|
|
constexpr float TRAVEL_FINISH_TIME = 2.f;
|
|
|
|
|
2016-01-19 14:51:37 +01:00
|
|
|
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
|
2019-04-14 17:51:12 +04:00
|
|
|
// 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;
|
2016-01-19 14:51:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2014-12-31 18:55:07 +01:00
|
|
|
|
2013-05-31 17:49:52 -07:00
|
|
|
namespace MWMechanics
|
2013-05-24 18:16:35 -07:00
|
|
|
{
|
2021-11-16 22:04:04 +01:00
|
|
|
AiTravel::AiTravel(float x, float y, float z, bool repeat, AiTravel*)
|
2022-06-19 22:21:41 +03:00
|
|
|
: TypedAiPackage<AiTravel>(repeat)
|
|
|
|
, mX(x)
|
|
|
|
, mY(y)
|
|
|
|
, mZ(z)
|
|
|
|
, mHidden(false)
|
|
|
|
, mDestinationTimer(TRAVEL_FINISH_TIME)
|
2020-05-16 21:08:39 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
AiTravel::AiTravel(float x, float y, float z, AiInternalTravel* derived)
|
2022-06-19 22:21:41 +03:00
|
|
|
: TypedAiPackage<AiTravel>(derived)
|
|
|
|
, mX(x)
|
|
|
|
, mY(y)
|
|
|
|
, mZ(z)
|
|
|
|
, mHidden(true)
|
|
|
|
, mDestinationTimer(TRAVEL_FINISH_TIME)
|
2020-05-16 21:08:39 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-11-16 22:04:04 +01:00
|
|
|
AiTravel::AiTravel(float x, float y, float z, bool repeat)
|
|
|
|
: AiTravel(x, y, z, repeat, this)
|
2013-05-31 17:49:52 -07:00
|
|
|
{
|
|
|
|
}
|
2013-03-31 17:30:03 +00:00
|
|
|
|
2014-06-12 23:27:04 +02:00
|
|
|
AiTravel::AiTravel(const ESM::AiSequence::AiTravel* travel)
|
2021-11-17 20:44:55 +01:00
|
|
|
: TypedAiPackage<AiTravel>(travel->mRepeat)
|
|
|
|
, mX(travel->mData.mX)
|
|
|
|
, mY(travel->mData.mY)
|
|
|
|
, mZ(travel->mData.mZ)
|
|
|
|
, mHidden(false)
|
2022-06-19 22:21:41 +03:00
|
|
|
, mDestinationTimer(TRAVEL_FINISH_TIME)
|
2014-06-12 23:27:04 +02:00
|
|
|
{
|
2020-05-16 21:08:39 +02:00
|
|
|
// Hidden ESM::AiSequence::AiTravel package should be converted into MWMechanics::AiInternalTravel type
|
|
|
|
assert(!travel->mHidden);
|
2014-06-12 23:27:04 +02:00
|
|
|
}
|
|
|
|
|
2015-06-26 17:47:04 +02:00
|
|
|
bool AiTravel::execute(
|
|
|
|
const MWWorld::Ptr& actor, CharacterController& characterController, AiState& state, float duration)
|
2013-05-31 17:49:52 -07:00
|
|
|
{
|
2020-05-17 04:06:39 +03:00
|
|
|
MWBase::MechanicsManager* mechMgr = MWBase::Environment::get().getMechanicsManager();
|
2021-05-09 19:55:27 +02:00
|
|
|
auto& stats = actor.getClass().getCreatureStats(actor);
|
2019-10-09 20:57:24 +04:00
|
|
|
|
2021-05-09 19:55:27 +02:00
|
|
|
if (!stats.getMovementFlag(CreatureStats::Flag_ForceJump)
|
|
|
|
&& !stats.getMovementFlag(CreatureStats::Flag_ForceSneak)
|
|
|
|
&& (mechMgr->isTurningToPlayer(actor) || mechMgr->getGreetingState(actor) == Greet_InProgress))
|
2019-10-09 20:57:24 +04:00
|
|
|
return false;
|
|
|
|
|
2018-11-02 14:24:43 +03:00
|
|
|
const osg::Vec3f actorPos(actor.getRefData().getPosition().asVec3());
|
|
|
|
const osg::Vec3f targetPos(mX, mY, mZ);
|
2013-04-01 15:44:08 +00:00
|
|
|
|
2019-10-09 20:57:24 +04:00
|
|
|
stats.setMovementFlag(CreatureStats::Flag_Run, false);
|
2022-07-17 19:36:48 +03:00
|
|
|
stats.setDrawState(DrawState::Nothing);
|
2014-07-28 17:28:00 +02:00
|
|
|
|
2020-02-02 11:02:19 +04:00
|
|
|
// Note: we should cancel internal "return after combat" package, if original location is too far away
|
2018-11-02 14:24:43 +03:00
|
|
|
if (!isWithinMaxRange(targetPos, actorPos))
|
2020-02-02 11:02:19 +04:00
|
|
|
return mHidden;
|
2014-09-17 12:39:10 +02:00
|
|
|
|
2018-11-02 14:24:43 +03:00
|
|
|
if (pathTo(actor, targetPos, duration))
|
2013-03-10 15:07:22 +00:00
|
|
|
{
|
2015-06-11 18:28:31 +12:00
|
|
|
actor.getClass().getMovementSettings(actor).mPosition[1] = 0;
|
|
|
|
return true;
|
2013-03-10 15:07:22 +00:00
|
|
|
}
|
2022-06-19 22:21:41 +03:00
|
|
|
|
|
|
|
// If we've been close enough to the destination for some time give up like Morrowind.
|
|
|
|
// The end condition should be pretty much accurate.
|
|
|
|
// FIXME: But the timing isn't. Right now we're being very generous,
|
|
|
|
// but Morrowind might stop the actor prematurely under unclear conditions.
|
|
|
|
|
|
|
|
// Note Morrowind uses the halved eye level, but this is close enough.
|
|
|
|
float dist
|
|
|
|
= distanceIgnoreZ(actorPos, targetPos) - MWBase::Environment::get().getWorld()->getHalfExtents(actor).z();
|
|
|
|
const float endTolerance = std::max(64.f, actor.getClass().getCurrentSpeed(actor) * duration);
|
|
|
|
|
|
|
|
// Even if we have entered the threshold, we might have been pushed away. Reset the timer if we're currently too
|
|
|
|
// far.
|
|
|
|
if (dist > endTolerance)
|
|
|
|
{
|
|
|
|
mDestinationTimer = TRAVEL_FINISH_TIME;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
mDestinationTimer -= duration;
|
|
|
|
if (mDestinationTimer > 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
actor.getClass().getMovementSettings(actor).mPosition[1] = 0;
|
|
|
|
return true;
|
2015-06-11 18:28:31 +12:00
|
|
|
}
|
2013-03-06 20:31:47 +00:00
|
|
|
|
2014-12-31 18:41:57 +01:00
|
|
|
void AiTravel::fastForward(const MWWorld::Ptr& actor, AiState& state)
|
|
|
|
{
|
2021-04-11 18:18:10 +02:00
|
|
|
osg::Vec3f pos(mX, mY, mZ);
|
|
|
|
if (!isWithinMaxRange(pos, actor.getRefData().getPosition().asVec3()))
|
2014-12-31 18:55:07 +01:00
|
|
|
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
|
2021-04-11 18:18:10 +02:00
|
|
|
MWBase::Environment::get().getWorld()->moveObject(actor, pos);
|
2014-12-31 18:55:07 +01:00
|
|
|
actor.getClass().adjustPosition(actor, false);
|
2019-08-01 15:10:46 +02:00
|
|
|
reset();
|
2014-12-31 18:41:57 +01:00
|
|
|
}
|
|
|
|
|
2014-06-12 23:27:04 +02:00
|
|
|
void AiTravel::writeState(ESM::AiSequence::AiSequence& sequence) const
|
|
|
|
{
|
2022-05-29 13:24:48 +02:00
|
|
|
auto travel = std::make_unique<ESM::AiSequence::AiTravel>();
|
2014-06-12 23:27:04 +02:00
|
|
|
travel->mData.mX = mX;
|
|
|
|
travel->mData.mY = mY;
|
|
|
|
travel->mData.mZ = mZ;
|
2018-05-25 19:31:31 +04:00
|
|
|
travel->mHidden = mHidden;
|
2021-11-17 20:44:55 +01:00
|
|
|
travel->mRepeat = getRepeat();
|
2014-06-12 23:27:04 +02:00
|
|
|
|
|
|
|
ESM::AiSequence::AiPackageContainer package;
|
|
|
|
package.mType = ESM::AiSequence::Ai_Travel;
|
2022-02-23 00:39:30 +01:00
|
|
|
package.mPackage = std::move(travel);
|
|
|
|
sequence.mPackages.push_back(std::move(package));
|
2014-06-12 23:27:04 +02:00
|
|
|
}
|
2020-05-16 21:08:39 +02:00
|
|
|
|
|
|
|
AiInternalTravel::AiInternalTravel(float x, float y, float z)
|
|
|
|
: AiTravel(x, y, z, this)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
AiInternalTravel::AiInternalTravel(const ESM::AiSequence::AiTravel* travel)
|
|
|
|
: AiTravel(travel->mData.mX, travel->mData.mY, travel->mData.mZ, this)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<AiPackage> AiInternalTravel::clone() const
|
|
|
|
{
|
|
|
|
return std::make_unique<AiInternalTravel>(*this);
|
|
|
|
}
|
2013-05-24 18:16:35 -07:00
|
|
|
}
|