TrinityCore
PersonalPhaseTracker.cpp
Go to the documentation of this file.
1/*
2 * This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
19#include "Containers.h"
20#include "Log.h"
21#include "Map.h"
22#include "Object.h"
23#include "ObjectGridLoader.h"
24#include "ObjectMgr.h"
25
26 /*********************************************************/
27 /*** PlayerPersonalPhasesTracker ***/
28 /*********************************************************/
29
31{
32 _spawns[phaseId].Objects.insert(object);
33}
34
36{
37 for (auto& [_, spawns] : _spawns)
38 spawns.Objects.erase(object);
39}
40
42{
43 PhaseShift const& phaseShift = owner->GetPhaseShift();
44
45 // Loop over all our tracked phases. If any don't exist - delete them
46 for (auto& [phaseId, spawns] : _spawns)
47 if (!spawns.DurationRemaining && !phaseShift.HasPhase(phaseId))
48 spawns.DurationRemaining = PersonalPhaseSpawns::DELETE_TIME_DEFAULT;
49
50 // loop over all owner phases. If any exist and marked for deletion - reset delete
51 for (PhaseShift::PhaseRef const& phaseRef : phaseShift.GetPhases())
53 spawns->DurationRemaining.reset();
54}
55
57{
58 for (auto& [_, spawns] : _spawns)
59 spawns.DurationRemaining = PersonalPhaseSpawns::DELETE_TIME_DEFAULT;
60}
61
63{
64 for (auto itr = _spawns.begin(); itr != _spawns.end(); )
65 {
66 if (itr->second.DurationRemaining)
67 {
68 itr->second.DurationRemaining = *itr->second.DurationRemaining - Milliseconds(diff);
69 if (itr->second.DurationRemaining <= Milliseconds::zero())
70 {
71 DespawnPhase(map, itr->second);
72 itr = _spawns.erase(itr);
73 continue;
74 }
75 }
76 ++itr;
77 }
78}
79
81{
83 return spawns->Grids.find(gridId) != spawns->Grids.cend();
84
85 return false;
86}
87
89{
90 PersonalPhaseSpawns& group = _spawns[phaseId];
91 group.Grids.insert(gridId);
92}
93
95{
96 for (auto itr = _spawns.begin(); itr != _spawns.end(); )
97 {
98 itr->second.Grids.erase(gridId);
99 if (itr->second.IsEmpty())
100 itr = _spawns.erase(itr);
101 else
102 ++itr;
103 }
104}
105
107{
108 for (WorldObject* obj : spawns.Objects)
109 map->AddObjectToRemoveList(obj);
110
111 spawns.Objects.clear();
112 spawns.Grids.clear();
113}
114
115/*********************************************************/
116/*** MultiPersonalPhaseTracker ***/
117/*********************************************************/
118
119void MultiPersonalPhaseTracker::LoadGrid(PhaseShift const& phaseShift, NGridType& grid, Map* map, Cell const& cell)
120{
121 if (!phaseShift.HasPersonalPhase())
122 return;
123
124 PersonalPhaseGridLoader loader(grid, map, cell, phaseShift.GetPersonalGuid());
125 PlayerPersonalPhasesTracker& playerTracker = _playerData[phaseShift.GetPersonalGuid()];
126
127 for (PhaseShift::PhaseRef const& phaseRef : phaseShift.GetPhases())
128 {
129 if (!phaseRef.IsPersonal())
130 continue;
131
132 if (!sObjectMgr->HasPersonalSpawns(map->GetId(), map->GetDifficultyID(), phaseRef.Id))
133 continue;
134
135 if (playerTracker.IsGridLoadedForPhase(grid.GetGridId(), phaseRef.Id))
136 continue;
137
138 TC_LOG_DEBUG("maps", "Loading personal phase objects (phase {}) in grid[{}, {}] for map {} instance {}",
139 phaseRef.Id, cell.GridX(), cell.GridY(), map->GetId(), map->GetInstanceId());
140
141 loader.Load(phaseRef.Id);
142
143 playerTracker.SetGridLoadedForPhase(grid.GetGridId(), phaseRef.Id);
144 }
145
146 if (loader.GetLoadedGameObjects())
147 map->Balance();
148}
149
151{
152 for (auto itr = _playerData.begin(); itr != _playerData.end(); )
153 {
154 itr->second.SetGridUnloaded(grid.GetGridId());
155 if (itr->second.IsEmpty())
156 itr = _playerData.erase(itr);
157 else
158 ++itr;
159 }
160}
161
163{
164 ASSERT(phaseId);
165 ASSERT(!phaseOwner.IsEmpty());
166 ASSERT(object);
167
168 _playerData[phaseOwner].RegisterTrackedObject(phaseId, object);
169}
170
172{
174 playerTracker->UnregisterTrackedObject(object);
175}
176
177void MultiPersonalPhaseTracker::OnOwnerPhaseChanged(WorldObject const* phaseOwner, NGridType* grid, Map* map, Cell const& cell)
178{
180 playerTracker->OnOwnerPhasesChanged(phaseOwner);
181
182 if (grid)
183 LoadGrid(phaseOwner->GetPhaseShift(), *grid, map, cell);
184}
185
187{
189 playerTracker->MarkAllPhasesForDeletion();
190}
191
193{
194 for (auto itr = _playerData.begin(); itr != _playerData.end(); )
195 {
196 itr->second.Update(map, diff);
197 if (itr->second.IsEmpty())
198 itr = _playerData.erase(itr);
199 else
200 ++itr;
201 }
202}
uint32_t uint32
Definition: Define.h:142
std::chrono::milliseconds Milliseconds
Milliseconds shorthand typedef.
Definition: Duration.h:29
#define ASSERT
Definition: Errors.h:68
#define TC_LOG_DEBUG(filterType__,...)
Definition: Log.h:156
#define sObjectMgr
Definition: ObjectMgr.h:1946
Definition: Map.h:189
void AddObjectToRemoveList(WorldObject *obj)
Definition: Map.cpp:2580
void Balance()
Definition: Map.h:461
Difficulty GetDifficultyID() const
Definition: Map.h:324
uint32 GetId() const
Definition: Map.cpp:3228
uint32 GetInstanceId() const
Definition: Map.h:314
Definition: NGrid.h:70
uint32 GetGridId(void) const
Definition: NGrid.h:90
uint32 GetLoadedGameObjects() const
bool IsEmpty() const
Definition: ObjectGuid.h:319
static ObjectGuid GetGUID(Object const *o)
Definition: Object.h:159
void Load(uint32 phaseId)
bool HasPhase(uint32 phaseId) const
Definition: PhaseShift.h:100
PhaseContainer const & GetPhases() const
Definition: PhaseShift.h:101
bool HasPersonalPhase() const
Definition: PhaseShift.cpp:197
ObjectGuid GetPersonalGuid() const
Definition: PhaseShift.h:96
PhaseShift & GetPhaseShift()
Definition: Object.h:523
auto MapGetValuePtr(M &map, typename M::key_type const &key)
Definition: MapUtils.h:29
Definition: Cell.h:47
uint32 GridX() const
Definition: Cell.h:73
uint32 GridY() const
Definition: Cell.h:74
void Update(Map *map, uint32 diff)
std::unordered_map< ObjectGuid, PlayerPersonalPhasesTracker > _playerData
void MarkAllPhasesForDeletion(ObjectGuid const &phaseOwner)
void RegisterTrackedObject(uint32 phaseId, ObjectGuid const &phaseOwner, WorldObject *object)
void LoadGrid(PhaseShift const &phaseShift, NGridType &grid, Map *map, Cell const &cell)
void UnregisterTrackedObject(WorldObject *object)
void UnloadGrid(NGridType &grid)
void OnOwnerPhaseChanged(WorldObject const *phaseOwner, NGridType *grid, Map *map, Cell const &cell)
static constexpr Milliseconds DELETE_TIME_DEFAULT
std::unordered_set< WorldObject * > Objects
std::unordered_set< uint16 > Grids
void UnregisterTrackedObject(WorldObject *object)
void SetGridLoadedForPhase(uint32 gridId, uint32 phaseId)
void RegisterTrackedObject(uint32 phaseId, WorldObject *object)
bool IsGridLoadedForPhase(uint32 gridId, uint32 phaseId) const
void Update(Map *map, uint32 diff)
void OnOwnerPhasesChanged(WorldObject const *owner)
std::unordered_map< uint32, PersonalPhaseSpawns > _spawns
void DespawnPhase(Map *map, PersonalPhaseSpawns &spawns)