summaryrefslogtreecommitdiff
path: root/apps/openmw/mwmechanics/aifollow.hpp
blob: b83e464fa10cfd6a73c7815f05eefcb3dfc9cb76 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#ifndef GAME_MWMECHANICS_AIFOLLOW_H
#define GAME_MWMECHANICS_AIFOLLOW_H

#include "typedaipackage.hpp"

#include <string>

#include <components/esm/defs.hpp>

#include "../mwworld/ptr.hpp"

namespace ESM
{
namespace AiSequence
{
    struct AiFollow;
}
}

namespace MWMechanics
{
    struct AiFollowStorage : AiTemporaryBase
    {
        float mTimer;
        bool mMoving;
        float mTargetAngleRadians;
        bool mTurnActorToTarget;

        AiFollowStorage() :
            mTimer(0.f),
            mMoving(false),
            mTargetAngleRadians(0.f),
            mTurnActorToTarget(false)
        {}
    };

    /// \brief AiPackage for an actor to follow another actor/the PC
    /** The AI will follow the target until a condition (time, or position) are set. Both can be disabled to cause the actor to follow the other indefinitely
    **/
    class AiFollow final : public TypedAiPackage<AiFollow>
    {
        public:
            /// Follow Actor for duration or until you arrive at a world position
            AiFollow(const std::string &actorId, float duration, float x, float y, float z, bool repeat);
            /// Follow Actor for duration or until you arrive at a position in a cell
            AiFollow(const std::string &actorId, const std::string &CellId, float duration, float x, float y, float z, bool repeat);
            /// Follow Actor indefinitively
            AiFollow(const MWWorld::Ptr& actor, bool commanded=false);

            AiFollow(const ESM::AiSequence::AiFollow* follow);

            bool execute (const MWWorld::Ptr& actor, CharacterController& characterController, AiState& state, float duration) override;

            static constexpr AiPackageTypeId getTypeId() { return AiPackageTypeId::Follow; }

            static constexpr Options makeDefaultOptions()
            {
                AiPackage::Options options;
                options.mUseVariableSpeed = true;
                options.mSideWithTarget = true;
                options.mFollowTargetThroughDoors = true;
                return options;
            }

            /// Returns the actor being followed
            std::string getFollowedActor();

            void writeState (ESM::AiSequence::AiSequence& sequence) const override;

            bool isCommanded() const;

            int getFollowIndex() const;

            void fastForward(const MWWorld::Ptr& actor, AiState& state) override;

            osg::Vec3f getDestination() const override
            {
                MWWorld::Ptr target = getTarget();
                if (target.isEmpty())
                    return osg::Vec3f(0, 0, 0);

                return target.getRefData().getPosition().asVec3();
            }

        private:
            /// This will make the actor always follow.
            /** Thus ignoring mDuration and mX,mY,mZ (used for summoned creatures). **/
            const bool mAlwaysFollow;
            const float mDuration; // Hours
            float mRemainingDuration; // Hours
            const float mX;
            const float mY;
            const float mZ;
            const std::string mCellId;
            bool mActive; // have we spotted the target?
            const int mFollowIndex;

            static int mFollowIndexCounter;
    };
}
#endif