TrinityCore
PhaseShift.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
18#include "PhaseShift.h"
19#include "Containers.h"
20
21PhaseShift::PhaseShift() = default;
22PhaseShift::PhaseShift(PhaseShift const& right) = default;
23PhaseShift::PhaseShift(PhaseShift&& right) noexcept = default;
24PhaseShift& PhaseShift::operator=(PhaseShift const& right) = default;
25PhaseShift& PhaseShift::operator=(PhaseShift&& right) noexcept = default;
26PhaseShift::~PhaseShift() = default;
27
28bool PhaseShift::AddPhase(uint32 phaseId, PhaseFlags flags, std::vector<Condition> const* areaConditions, int32 references /*= 1*/)
29{
30 auto insertResult = Phases.emplace(phaseId, flags, nullptr);
31 ModifyPhasesReferences(insertResult.first, references);
32 if (areaConditions)
33 insertResult.first->AreaConditions = areaConditions;
34
35 return insertResult.second;
36}
37
39{
40 auto itr = Phases.find(PhaseRef(phaseId, PhaseFlags::None, nullptr));
41 if (itr != Phases.end())
42 {
44 if (!itr->References)
45 return { Phases.erase(itr), true };
46 return { itr, false };
47 }
48 return { Phases.end(), false };
49}
50
51bool PhaseShift::AddVisibleMapId(uint32 visibleMapId, TerrainSwapInfo const* visibleMapInfo, int32 references /*= 1*/)
52{
53 auto insertResult = VisibleMapIds.emplace(visibleMapId, VisibleMapIdRef{ 0, visibleMapInfo });
54 insertResult.first->second.References += references;
55 return insertResult.second;
56}
57
59{
60 auto itr = VisibleMapIds.find(visibleMapId);
61 if (itr != VisibleMapIds.end())
62 {
63 if (!--itr->second.References)
64 return { VisibleMapIds.erase(itr), true };
65 return { itr, false };
66 }
67 return { VisibleMapIds.end(), false };
68}
69
70bool PhaseShift::AddUiMapPhaseId(uint32 uiMapPhaseId, int32 references /*= 1*/)
71{
72 auto insertResult = UiMapPhaseIds.emplace(uiMapPhaseId, UiMapPhaseIdRef{ 0 });
73 insertResult.first->second.References += references;
74 return insertResult.second;
75}
76
78{
79 auto itr = UiMapPhaseIds.find(uiMapPhaseId);
80 if (itr != UiMapPhaseIds.end())
81 {
82 if (!--itr->second.References)
83 return { UiMapPhaseIds.erase(itr), true };
84 return { itr, false };
85 }
86 return { UiMapPhaseIds.end(), false };
87}
88
90{
92 VisibleMapIds.clear();
93 UiMapPhaseIds.clear();
94}
95
97{
100 Phases.clear();
106}
107
108bool PhaseShift::CanSee(PhaseShift const& other) const
109{
111 return true;
113 return true;
115 return true;
116
117 PhaseFlags excludePhasesWithFlag = PhaseFlags::None;
119 excludePhasesWithFlag = PhaseFlags::Cosmetic;
120
122 {
123 ObjectGuid ownerGuid = PersonalGuid;
124 ObjectGuid otherPersonalGuid = other.PersonalGuid;
125 return Trinity::Containers::Intersects(Phases.begin(), Phases.end(), other.Phases.begin(), other.Phases.end(),
126 [&ownerGuid, &otherPersonalGuid, excludePhasesWithFlag](PhaseRef const& myPhase, PhaseRef const& /*otherPhase*/)
127 {
128 return !myPhase.Flags.HasFlag(excludePhasesWithFlag) && (!myPhase.Flags.HasFlag(PhaseFlags::Personal) || ownerGuid == otherPersonalGuid);
129 });
130 }
131
132 auto checkInversePhaseShift = [excludePhasesWithFlag](PhaseShift const& phaseShift, PhaseShift const& excludedPhaseShift)
133 {
134 if (phaseShift.Flags.HasFlag(PhaseShiftFlags::Unphased) && excludedPhaseShift.Flags.HasFlag(PhaseShiftFlags::InverseUnphased))
135 return false;
136
137 for (PhaseRef const& phase : phaseShift.Phases)
138 {
139 if (phase.Flags.HasFlag(excludePhasesWithFlag))
140 continue;
141
142 auto itr2 = std::find(excludedPhaseShift.Phases.begin(), excludedPhaseShift.Phases.end(), phase);
143 if (itr2 != excludedPhaseShift.Phases.end() && !itr2->Flags.HasFlag(excludePhasesWithFlag))
144 return false;
145 }
146 return true;
147 };
148
150 return checkInversePhaseShift(*this, other);
151
152 return checkInversePhaseShift(other, *this);
153}
154
155void PhaseShift::ModifyPhasesReferences(PhaseContainer::iterator itr, int32 references)
156{
157 itr->References += references;
158
159 if (!IsDbPhaseShift)
160 {
161 if (itr->Flags.HasFlag(PhaseFlags::Cosmetic))
162 CosmeticReferences += references;
163 else if (itr->Id != DEFAULT_PHASE)
164 NonCosmeticReferences += references;
165 else
166 DefaultReferences += references;
167
168 if (itr->Flags.HasFlag(PhaseFlags::Personal))
169 PersonalReferences += references;
170
173 else
175
178 }
179}
180
182{
186 Flags &= ~unphasedFlag;
187 else
188 Flags |= unphasedFlag;
189}
190
192{
195}
196
198{
199 for (PhaseRef const& phaseRef : GetPhases())
200 if (phaseRef.IsPersonal())
201 return true;
202 return false;
203}
int32_t int32
Definition: Define.h:138
uint32_t uint32
Definition: Define.h:142
uint16 flags
Definition: DisableMgr.cpp:49
PhaseFlags
Definition: PhaseShift.h:45
#define DEFAULT_PHASE
Definition: PhaseShift.h:31
PhaseShiftFlags
Definition: PhaseShift.h:34
constexpr bool HasFlag(T flag) const
Definition: EnumFlag.h:106
void Clear()
Definition: ObjectGuid.h:286
int32 DefaultReferences
Definition: PhaseShift.h:135
void ClearPhases()
Definition: PhaseShift.cpp:96
PhaseShift & operator=(PhaseShift const &right)
EnumFlag< PhaseShiftFlags > Flags
Definition: PhaseShift.h:123
ObjectGuid PersonalGuid
Definition: PhaseShift.h:124
PhaseContainer const & GetPhases() const
Definition: PhaseShift.h:101
void ModifyPhasesReferences(PhaseContainer::iterator itr, int32 references)
Definition: PhaseShift.cpp:155
int32 NonCosmeticReferences
Definition: PhaseShift.h:132
int32 CosmeticReferences
Definition: PhaseShift.h:133
EraseResult< VisibleMapIdContainer > RemoveVisibleMapId(uint32 visibleMapId)
Definition: PhaseShift.cpp:58
bool HasPersonalPhase() const
Definition: PhaseShift.cpp:197
void UpdateUnphasedFlag()
Definition: PhaseShift.cpp:181
EraseResult< PhaseContainer > RemovePhase(uint32 phaseId)
Definition: PhaseShift.cpp:38
UiMapPhaseIdContainer UiMapPhaseIds
Definition: PhaseShift.h:127
void Clear()
Definition: PhaseShift.cpp:89
bool AddVisibleMapId(uint32 visibleMapId, TerrainSwapInfo const *visibleMapInfo, int32 references=1)
Definition: PhaseShift.cpp:51
EraseResult< UiMapPhaseIdContainer > RemoveUiMapPhaseId(uint32 uiMapPhaseId)
Definition: PhaseShift.cpp:77
bool IsDbPhaseShift
Definition: PhaseShift.h:136
PhaseContainer Phases
Definition: PhaseShift.h:125
void UpdatePersonalGuid()
Definition: PhaseShift.cpp:191
bool AddUiMapPhaseId(uint32 uiMapPhaseId, int32 references=1)
Definition: PhaseShift.cpp:70
bool AddPhase(uint32 phaseId, PhaseFlags flags, std::vector< Condition > const *areaConditions, int32 references=1)
Definition: PhaseShift.cpp:28
int32 PersonalReferences
Definition: PhaseShift.h:134
VisibleMapIdContainer VisibleMapIds
Definition: PhaseShift.h:126
bool CanSee(PhaseShift const &other) const
Definition: PhaseShift.cpp:108
bool Intersects(Iterator1 first1, Iterator1 last1, Iterator2 first2, Iterator2 last2)
Definition: Containers.h:201
EnumFlag< PhaseFlags > Flags
Definition: PhaseShift.h:63