1
0
mirror of https://gitlab.com/OpenMW/openmw.git synced 2025-01-27 03:35:27 +00:00
OpenMW/apps/openmw/mwmechanics/aifollow.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

268 lines
9.1 KiB
C++
Raw Normal View History

#include "aifollow.hpp"
2014-06-12 23:27:04 +02:00
#include <components/esm3/aisequence.hpp>
#include <components/esm3/loadcell.hpp>
2023-01-19 17:31:45 +01:00
#include <components/misc/algorithm.hpp>
2014-06-12 23:27:04 +02:00
#include "../mwbase/environment.hpp"
#include "../mwbase/mechanicsmanager.hpp"
#include "../mwbase/world.hpp"
2016-06-17 23:07:16 +09:00
2014-03-29 18:36:32 +01:00
#include "../mwworld/cellstore.hpp"
#include "../mwworld/class.hpp"
2016-06-17 23:07:16 +09:00
#include "creaturestats.hpp"
2014-01-29 20:29:07 +01:00
#include "steering.hpp"
namespace
{
osg::Vec3f::value_type getHalfExtents(const MWWorld::ConstPtr& actor)
{
if (actor.getClass().isNpc())
return 64;
return MWBase::Environment::get().getWorld()->getHalfExtents(actor).y();
}
}
namespace MWMechanics
{
int AiFollow::mFollowIndexCounter = 0;
2022-09-22 21:26:05 +03:00
Initial commit: In ESM structures, replace the string members that are RefIds to other records, to a new strong type The strong type is actually just a string underneath, but this will help in the future to have a distinction so it's easier to search and replace when we use an integer ID Slowly going through all the changes to make, still hundreds of errors a lot of functions/structures use std::string or stringview to designate an ID. So it takes time Continues slowly replacing ids. There are technically more and more compilation errors I have good hope that there is a point where the amount of errors will dramatically go down as all the main functions use the ESM::RefId type Continue moving forward, changes to the stores slowly moving along Starting to see the fruit of those changes. still many many error, but more and more Irun into a situation where a function is sandwiched between two functions that use the RefId type. More replacements. Things are starting to get easier I can see more and more often the issue is that the function is awaiting a RefId, but is given a string there is less need to go down functions and to fix a long list of them. Still moving forward, and for the first time error count is going down! Good pace, not sure about topics though, mId and mName are actually the same thing and are used interchangeably Cells are back to using string for the name, haven't fixed everything yet. Many other changes Under the bar of 400 compilation errors. more good progress <100 compile errors! More progress Game settings store can use string for find, it was a bit absurd how every use of it required to create refId from string some more progress on other fronts Mostly game settings clean one error opened a lot of other errors. Down to 18, but more will prbably appear only link errors left?? Fixed link errors OpenMW compiles, and launches, with some issues, but still!
2022-09-25 13:17:09 +02:00
AiFollow::AiFollow(const ESM::RefId& actorId, float duration, float x, float y, float z, bool repeat)
: TypedAiPackage<AiFollow>(repeat)
, mAlwaysFollow(false)
, mDuration(duration)
, mRemainingDuration(duration)
, mX(x)
, mY(y)
, mZ(z)
, mActive(false)
, mFollowIndex(mFollowIndexCounter++)
{
Initial commit: In ESM structures, replace the string members that are RefIds to other records, to a new strong type The strong type is actually just a string underneath, but this will help in the future to have a distinction so it's easier to search and replace when we use an integer ID Slowly going through all the changes to make, still hundreds of errors a lot of functions/structures use std::string or stringview to designate an ID. So it takes time Continues slowly replacing ids. There are technically more and more compilation errors I have good hope that there is a point where the amount of errors will dramatically go down as all the main functions use the ESM::RefId type Continue moving forward, changes to the stores slowly moving along Starting to see the fruit of those changes. still many many error, but more and more Irun into a situation where a function is sandwiched between two functions that use the RefId type. More replacements. Things are starting to get easier I can see more and more often the issue is that the function is awaiting a RefId, but is given a string there is less need to go down functions and to fix a long list of them. Still moving forward, and for the first time error count is going down! Good pace, not sure about topics though, mId and mName are actually the same thing and are used interchangeably Cells are back to using string for the name, haven't fixed everything yet. Many other changes Under the bar of 400 compilation errors. more good progress <100 compile errors! More progress Game settings store can use string for find, it was a bit absurd how every use of it required to create refId from string some more progress on other fronts Mostly game settings clean one error opened a lot of other errors. Down to 18, but more will prbably appear only link errors left?? Fixed link errors OpenMW compiles, and launches, with some issues, but still!
2022-09-25 13:17:09 +02:00
mTargetActorRefId = actorId;
}
2018-11-02 14:24:43 +03:00
AiFollow::AiFollow(
2023-01-19 17:31:45 +01:00
const ESM::RefId& actorId, std::string_view cellId, float duration, float x, float y, float z, bool repeat)
2018-11-02 14:24:43 +03:00
: TypedAiPackage<AiFollow>(repeat)
, mAlwaysFollow(false)
2021-11-17 20:44:55 +01:00
, mDuration(duration)
2018-11-02 14:24:43 +03:00
, mRemainingDuration(duration)
2022-09-22 21:26:05 +03:00
, mX(x)
, mY(y)
, mZ(z)
, mCellId(cellId)
, mActive(false)
2018-11-02 14:24:43 +03:00
, mFollowIndex(mFollowIndexCounter++)
2022-09-22 21:26:05 +03:00
{
Initial commit: In ESM structures, replace the string members that are RefIds to other records, to a new strong type The strong type is actually just a string underneath, but this will help in the future to have a distinction so it's easier to search and replace when we use an integer ID Slowly going through all the changes to make, still hundreds of errors a lot of functions/structures use std::string or stringview to designate an ID. So it takes time Continues slowly replacing ids. There are technically more and more compilation errors I have good hope that there is a point where the amount of errors will dramatically go down as all the main functions use the ESM::RefId type Continue moving forward, changes to the stores slowly moving along Starting to see the fruit of those changes. still many many error, but more and more Irun into a situation where a function is sandwiched between two functions that use the RefId type. More replacements. Things are starting to get easier I can see more and more often the issue is that the function is awaiting a RefId, but is given a string there is less need to go down functions and to fix a long list of them. Still moving forward, and for the first time error count is going down! Good pace, not sure about topics though, mId and mName are actually the same thing and are used interchangeably Cells are back to using string for the name, haven't fixed everything yet. Many other changes Under the bar of 400 compilation errors. more good progress <100 compile errors! More progress Game settings store can use string for find, it was a bit absurd how every use of it required to create refId from string some more progress on other fronts Mostly game settings clean one error opened a lot of other errors. Down to 18, but more will prbably appear only link errors left?? Fixed link errors OpenMW compiles, and launches, with some issues, but still!
2022-09-25 13:17:09 +02:00
mTargetActorRefId = actorId;
2022-09-22 21:26:05 +03:00
}
AiFollow::AiFollow(const MWWorld::Ptr& actor, bool commanded)
: TypedAiPackage<AiFollow>(makeDefaultOptions().withShouldCancelPreviousAi(!commanded))
, mAlwaysFollow(true)
, mDuration(0)
, mRemainingDuration(0)
2022-09-22 21:26:05 +03:00
, mX(0)
, mY(0)
, mZ(0)
, mActive(false)
, mFollowIndex(mFollowIndexCounter++)
2022-09-22 21:26:05 +03:00
{
mTargetActorRefId = actor.getCellRef().getRefId();
mTargetActorId = actor.getClass().getCreatureStats(actor).getActorId();
2022-09-22 21:26:05 +03:00
}
AiFollow::AiFollow(const ESM::AiSequence::AiFollow* follow)
: TypedAiPackage<AiFollow>(
makeDefaultOptions().withShouldCancelPreviousAi(!follow->mCommanded).withRepeat(follow->mRepeat))
, mAlwaysFollow(follow->mAlwaysFollow)
, mDuration(follow->mData.mDuration)
, mRemainingDuration(follow->mRemainingDuration)
, mX(follow->mData.mX)
, mY(follow->mData.mY)
, mZ(follow->mData.mZ)
, mCellId(follow->mCellId)
, mActive(follow->mActive)
, mFollowIndex(mFollowIndexCounter++)
2022-09-22 21:26:05 +03:00
{
mTargetActorRefId = follow->mTargetId;
mTargetActorId = follow->mTargetActorId;
2022-09-22 21:26:05 +03:00
}
bool AiFollow::execute(
const MWWorld::Ptr& actor, CharacterController& characterController, AiState& state, float duration)
{
const MWWorld::Ptr target = getTarget();
2018-11-02 14:24:43 +03:00
// Target is not here right now, wait for it to return
// Really we should be checking whether the target is currently registered with the MechanicsManager
if (target == MWWorld::Ptr() || !target.getRefData().getCount() || !target.getRefData().isEnabled())
return false;
2018-11-02 14:24:43 +03:00
actor.getClass().getCreatureStats(actor).setDrawState(DrawState::Nothing);
AiFollowStorage& storage = state.get<AiFollowStorage>();
bool& rotate = storage.mTurnActorToTarget;
if (rotate)
{
2018-11-02 14:24:43 +03:00
if (zTurn(actor, storage.mTargetAngleRadians))
rotate = false;
2022-09-22 21:26:05 +03:00
return false;
}
const osg::Vec3f actorPos(actor.getRefData().getPosition().asVec3());
2018-11-02 14:24:43 +03:00
const osg::Vec3f targetPos(target.getRefData().getPosition().asVec3());
const osg::Vec3f targetDir = targetPos - actorPos;
2022-09-22 21:26:05 +03:00
// AiFollow requires the target to be in range and within sight for the initial activation
if (!mActive)
{
storage.mTimer -= duration;
if (storage.mTimer < 0)
2022-09-22 21:26:05 +03:00
{
if (targetDir.length2() < 500 * 500 && MWBase::Environment::get().getWorld()->getLOS(actor, target))
mActive = true;
storage.mTimer = 0.5f;
2022-09-22 21:26:05 +03:00
}
}
if (!mActive)
return false;
2022-09-22 21:26:05 +03:00
// In the original engine the first follower stays closer to the player than any subsequent followers.
2014-06-12 23:27:04 +02:00
// Followers beyond the first usually attempt to stand inside each other.
osg::Vec3f::value_type floatingDistance = 0;
auto followers = MWBase::Environment::get().getMechanicsManager()->getActorsFollowingByIndex(target);
if (followers.size() >= 2 && followers.cbegin()->first != mFollowIndex)
2014-06-12 23:27:04 +02:00
{
2016-06-11 22:34:49 +09:00
for (auto& follower : followers)
{
auto halfExtent = getHalfExtents(follower.second);
if (halfExtent > floatingDistance)
floatingDistance = halfExtent;
}
floatingDistance += 128;
2014-06-12 23:27:04 +02:00
}
floatingDistance += getHalfExtents(target) + 64;
floatingDistance += getHalfExtents(actor) * 2;
short followDistance = static_cast<short>(floatingDistance);
2014-03-05 11:24:39 +01:00
2018-11-02 14:24:43 +03:00
if (!mAlwaysFollow) // Update if you only follow for a bit
{
// Check if we've run out of time
if (mDuration > 0)
2014-03-05 11:24:39 +01:00
{
mRemainingDuration -= ((duration * MWBase::Environment::get().getWorld()->getTimeScaleFactor()) / 3600);
if (mRemainingDuration <= 0)
2021-11-17 20:44:55 +01:00
{
mRemainingDuration = mDuration;
2014-03-05 11:24:39 +01:00
return true;
2021-11-17 20:44:55 +01:00
}
2014-03-05 11:24:39 +01:00
}
2022-09-22 21:26:05 +03:00
2021-11-17 20:44:55 +01:00
osg::Vec3f finalPos(mX, mY, mZ);
if ((actorPos - finalPos).length2() < followDistance * followDistance) // Close-ish to final position
2014-03-05 11:24:39 +01:00
{
if (actor.getCell()->isExterior()) // Outside?
2022-09-22 21:26:05 +03:00
{
if (mCellId.empty()) // No cell to travel to
2022-09-22 21:26:05 +03:00
{
2021-11-17 20:44:55 +01:00
mRemainingDuration = mDuration;
2014-03-05 11:24:39 +01:00
return true;
2022-09-22 21:26:05 +03:00
}
}
else if (mCellId == actor.getCell()->getCell()->getWorldSpace()) // Cell to travel to
2022-09-22 21:26:05 +03:00
{
2021-11-17 20:44:55 +01:00
mRemainingDuration = mDuration;
return true;
2022-09-22 21:26:05 +03:00
}
2014-03-05 11:24:39 +01:00
}
}
short baseFollowDistance = followDistance;
short threshold = 30; // to avoid constant switching between moving/stopping
if (storage.mMoving)
followDistance -= threshold;
2022-09-22 21:26:05 +03:00
else
followDistance += threshold;
if (targetDir.length2() <= followDistance * followDistance)
2022-09-22 21:26:05 +03:00
{
float faceAngleRadians = std::atan2(targetDir.x(), targetDir.y());
2018-11-02 14:24:43 +03:00
if (!zTurn(actor, faceAngleRadians, osg::DegreesToRadians(45.f)))
{
2018-11-02 14:24:43 +03:00
storage.mTargetAngleRadians = faceAngleRadians;
storage.mTurnActorToTarget = true;
2022-09-22 21:26:05 +03:00
}
return false;
2022-09-22 21:26:05 +03:00
}
storage.mMoving = !pathTo(actor, targetPos, duration, baseFollowDistance); // Go to the destination
2022-09-22 21:26:05 +03:00
if (storage.mMoving)
{
// Check if you're far away
if (targetDir.length2() > 450 * 450)
actor.getClass().getCreatureStats(actor).setMovementFlag(
MWMechanics::CreatureStats::Flag_Run, true); // Make NPC run
else if (targetDir.length2()
< 325 * 325) // Have a bit of a dead zone, otherwise npc will constantly flip between running and not
// when right on the edge of the running threshold
actor.getClass().getCreatureStats(actor).setMovementFlag(
MWMechanics::CreatureStats::Flag_Run, false); // make NPC walk
}
return false;
2016-01-03 18:20:34 +01:00
}
Initial commit: In ESM structures, replace the string members that are RefIds to other records, to a new strong type The strong type is actually just a string underneath, but this will help in the future to have a distinction so it's easier to search and replace when we use an integer ID Slowly going through all the changes to make, still hundreds of errors a lot of functions/structures use std::string or stringview to designate an ID. So it takes time Continues slowly replacing ids. There are technically more and more compilation errors I have good hope that there is a point where the amount of errors will dramatically go down as all the main functions use the ESM::RefId type Continue moving forward, changes to the stores slowly moving along Starting to see the fruit of those changes. still many many error, but more and more Irun into a situation where a function is sandwiched between two functions that use the RefId type. More replacements. Things are starting to get easier I can see more and more often the issue is that the function is awaiting a RefId, but is given a string there is less need to go down functions and to fix a long list of them. Still moving forward, and for the first time error count is going down! Good pace, not sure about topics though, mId and mName are actually the same thing and are used interchangeably Cells are back to using string for the name, haven't fixed everything yet. Many other changes Under the bar of 400 compilation errors. more good progress <100 compile errors! More progress Game settings store can use string for find, it was a bit absurd how every use of it required to create refId from string some more progress on other fronts Mostly game settings clean one error opened a lot of other errors. Down to 18, but more will prbably appear only link errors left?? Fixed link errors OpenMW compiles, and launches, with some issues, but still!
2022-09-25 13:17:09 +02:00
ESM::RefId AiFollow::getFollowedActor()
{
return mTargetActorRefId;
}
bool AiFollow::isCommanded() const
{
return !mOptions.mShouldCancelPreviousAi;
}
void AiFollow::writeState(ESM::AiSequence::AiSequence& sequence) const
2014-06-12 23:27:04 +02:00
{
2022-05-29 13:24:48 +02:00
auto follow = std::make_unique<ESM::AiSequence::AiFollow>();
2014-06-12 23:27:04 +02:00
follow->mData.mX = mX;
follow->mData.mY = mY;
follow->mData.mZ = mZ;
2021-11-17 20:44:55 +01:00
follow->mData.mDuration = mDuration;
follow->mTargetId = mTargetActorRefId;
follow->mTargetActorId = mTargetActorId;
2014-06-12 23:27:04 +02:00
follow->mRemainingDuration = mRemainingDuration;
follow->mCellId = mCellId;
follow->mAlwaysFollow = mAlwaysFollow;
follow->mCommanded = isCommanded();
follow->mActive = mActive;
2021-11-17 20:44:55 +01:00
follow->mRepeat = getRepeat();
2022-09-22 21:26:05 +03:00
2014-06-12 23:27:04 +02:00
ESM::AiSequence::AiPackageContainer package;
package.mType = ESM::AiSequence::Ai_Follow;
package.mPackage = std::move(follow);
sequence.mPackages.push_back(std::move(package));
2014-06-12 23:27:04 +02:00
}
int AiFollow::getFollowIndex() const
{
return mFollowIndex;
}
2016-06-11 22:34:49 +09:00
void AiFollow::fastForward(const MWWorld::Ptr& actor, AiState& state)
{
// Update duration counter if this package has a duration
if (mDuration > 0)
mRemainingDuration--;
2016-06-11 22:34:49 +09:00
}
}