aboutsummaryrefslogtreecommitdiff
path: root/src/server/game/Maps/TransportMgr.h
blob: a2b0accfb81c8968dfb87b550e0c559ef1bd7089 (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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*
 * This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef TRANSPORTMGR_H
#define TRANSPORTMGR_H

#include "ObjectGuid.h"
#include "Optional.h"
#include "Position.h"
#include <map>
#include <memory>
#include <unordered_map>

struct GameObjectTemplate;
struct TaxiPathNodeEntry;
struct TransportAnimationEntry;
struct TransportRotationEntry;
struct TransportTemplate;
class Transport;
class Map;

namespace Movement
{
    template <typename length_type> class Spline;
}

using TransportSpline = Movement::Spline<double>;

enum class TransportMovementState : uint8
{
    Moving,
    WaitingOnPauseWaypoint
};

// Represents a segment within path leg between stops
struct TransportPathSegment
{
    uint32 SegmentEndArrivalTimestamp = 0;
    uint32 Delay = 0;
    double DistanceFromLegStartAtEnd = 0.0;
};

struct TransportPathEvent
{
    uint32 Timestamp = 0;
    uint32 EventId = 0;
};

// Represents a contignuous part of transport path (without map changes or teleports)
struct TransportPathLeg
{
    TransportPathLeg();
    ~TransportPathLeg();

    TransportPathLeg(TransportPathLeg const&) = delete;
    TransportPathLeg(TransportPathLeg&&) noexcept;
    TransportPathLeg& operator=(TransportPathLeg const&) = delete;
    TransportPathLeg& operator=(TransportPathLeg&&) noexcept;

    uint32 MapId = 0;
    std::unique_ptr<TransportSpline> Spline;
    uint32 StartTimestamp = 0;
    uint32 Duration = 0;
    std::vector<TransportPathSegment> Segments;
};

struct TransportTemplate
{
    TransportTemplate();
    ~TransportTemplate();

    TransportTemplate(TransportTemplate const&) = delete;
    TransportTemplate(TransportTemplate&&) noexcept;
    TransportTemplate& operator=(TransportTemplate const&) = delete;
    TransportTemplate& operator=(TransportTemplate&&) noexcept;

    uint32 TotalPathTime = 0;
    double Speed = 0.0;
    double AccelerationRate = 0.0;
    double AccelerationTime = 0.0;
    double AccelerationDistance = 0.0;
    std::vector<TransportPathLeg> PathLegs;
    std::vector<TransportPathEvent> Events;

    Optional<Position> ComputePosition(uint32 time, TransportMovementState* moveState, size_t* legIndex) const;
    TransportPathLeg const* GetLegForTime(uint32 time) const;
    uint32 GetNextPauseWaypointTimestamp(uint32 time) const;

    double CalculateDistanceMoved(double timePassedInSegment, double segmentDuration, bool isFirstSegment, bool isLastSegment) const;

    std::set<uint32> MapIds;
};

struct TransportAnimation
{
    TransportAnimation();
    ~TransportAnimation();

    TransportAnimation(TransportAnimation const&) = delete;
    TransportAnimation(TransportAnimation&&) noexcept;
    TransportAnimation& operator=(TransportAnimation const&) = delete;
    TransportAnimation& operator=(TransportAnimation&&) noexcept;

    std::map<uint32, TransportAnimationEntry const*> Path;
    std::map<uint32, TransportRotationEntry const*> Rotations;
    uint32 TotalTime = 0;

    TransportAnimationEntry const* GetPrevAnimNode(uint32 time) const;
    TransportRotationEntry const* GetPrevAnimRotation(uint32 time) const;

    TransportAnimationEntry const* GetNextAnimNode(uint32 time) const;
    TransportRotationEntry const* GetNextAnimRotation(uint32 time) const;
};

struct TransportSpawn
{
    ObjectGuid::LowType SpawnId = UI64LIT(0);
    uint32 TransportGameObjectId = 0; // entry in respective _template table
    uint8 PhaseUseFlags = 0;
    uint32 PhaseId = 0;
    uint32 PhaseGroup = 0;
};

class TC_GAME_API TransportMgr
{
    public:
        TransportMgr(TransportMgr const&) = delete;
        TransportMgr(TransportMgr&&) = delete;
        TransportMgr& operator=(TransportMgr const&) = delete;
        TransportMgr& operator=(TransportMgr&&) = delete;

        static TransportMgr* instance();

        void Unload();

        void LoadTransportTemplates();

        void LoadTransportAnimationAndRotation();

        void LoadTransportSpawns();

        // Creates a transport using given GameObject template entry
        Transport* CreateTransport(uint32 entry, Map* map, ObjectGuid::LowType guid = 0, uint8 phaseUseFlags = 0, uint32 phaseId = 0, uint32 phaseGroupId = 0);

        // creates all transports for map
        void CreateTransportsForMap(Map* map);

        TransportTemplate const* GetTransportTemplate(uint32 entry) const;

        TransportAnimation const* GetTransportAnimInfo(uint32 entry) const;

        TransportSpawn const* GetTransportSpawn(ObjectGuid::LowType spawnId) const;

    private:
        TransportMgr();
        ~TransportMgr();

        // Generates and precaches a path for transport to avoid generation each time transport instance is created
        void GeneratePath(GameObjectTemplate const* goInfo, TransportTemplate* transport);

        void AddPathNodeToTransport(uint32 transportEntry, uint32 timeSeg, TransportAnimationEntry const* node);

        void AddPathRotationToTransport(uint32 transportEntry, uint32 timeSeg, TransportRotationEntry const* node);

        // Container storing transport templates
        std::unordered_map<uint32, TransportTemplate> _transportTemplates;

        // Container storing transport entries to create for instanced maps
        std::unordered_map<uint32, std::set<TransportSpawn*>> _transportsByMap;

        std::map<uint32, TransportAnimation> _transportAnimations;

        std::unordered_map<ObjectGuid::LowType, TransportSpawn> _transportSpawns;
};

#define sTransportMgr TransportMgr::instance()

#endif // TRANSPORTMGR_H