TrinityCore
Loading...
Searching...
No Matches
SpellHistory.h
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#ifndef SpellHistory_h__
19#define SpellHistory_h__
20
21#include "SharedDefines.h"
22#include "DatabaseEnvFwd.h"
23#include "GameTime.h"
24#include <vector>
25#include <unordered_map>
26
27class Item;
28class Player;
29class Spell;
30class SpellInfo;
31class Unit;
32class WorldPacket;
34
36{
37class InitialSpells;
38}
39
47
49{
50public:
51 typedef std::chrono::system_clock Clock;
52
54 {
55 uint32 SpellId = 0;
56 Clock::time_point CooldownEnd;
57 uint32 ItemId = 0;
58 uint32 CategoryId = 0;
59 Clock::time_point CategoryEnd;
60 bool OnHold = false;
61 };
62
63 typedef std::unordered_map<uint32 /*spellId*/, CooldownEntry> CooldownStorageType;
64 typedef std::unordered_map<uint32 /*categoryId*/, CooldownEntry*> CategoryCooldownStorageType;
65 typedef std::unordered_map<uint32 /*categoryId*/, Clock::time_point> GlobalCooldownStorageType;
66
67 explicit SpellHistory(Unit* owner) : _owner(owner), _schoolLockouts() { }
68
69 template<class OwnerType>
70 void LoadFromDB(PreparedQueryResult cooldownsResult);
71
72 template<class OwnerType>
74
75 void Update();
76
77 void HandleCooldowns(SpellInfo const* spellInfo, Item const* item, Spell* spell = nullptr);
78 void HandleCooldowns(SpellInfo const* spellInfo, uint32 itemID, Spell* spell = nullptr);
79 bool IsReady(SpellInfo const* spellInfo, uint32 itemId = 0, bool ignoreCategoryCooldown = false) const;
80 template<class OwnerType>
81 void WritePacket(WorldPacket& packet) const;
82 void WritePacket(WorldPackets::Spells::InitialSpells* initialSpells) const;
83
84 // Cooldowns
85 static Clock::duration const InfinityCooldownDelay; // used for set "infinity cooldowns" for spells and check
86 static Clock::duration const InfinityCooldownDelayCheck;
87
88 void StartCooldown(SpellInfo const* spellInfo, uint32 itemId, Spell* spell = nullptr, bool onHold = false);
89 void SendCooldownEvent(SpellInfo const* spellInfo, uint32 itemId = 0, Spell* spell = nullptr, bool startCooldown = true);
90
91 template<class Type, class Period>
92 void AddCooldown(uint32 spellId, uint32 itemId, std::chrono::duration<Type, Period> cooldownDuration)
93 {
94 Clock::time_point now = GameTime::GetSystemTime();
95 AddCooldown(spellId, itemId, now + std::chrono::duration_cast<Clock::duration>(cooldownDuration), 0, now);
96 }
97
98 void AddCooldown(uint32 spellId, uint32 itemId, Clock::time_point cooldownEnd, uint32 categoryId, Clock::time_point categoryEnd, bool onHold = false);
99 void ModifyCooldown(uint32 spellId, int32 cooldownModMs);
100 void ResetCooldown(uint32 spellId, bool update = false);
101 void ResetCooldown(CooldownStorageType::iterator& itr, bool update = false);
102 template<typename Predicate>
103 void ResetCooldowns(Predicate predicate, bool update = false)
104 {
105 std::vector<int32> resetCooldowns;
106 resetCooldowns.reserve(_spellCooldowns.size());
107 for (auto itr = _spellCooldowns.begin(); itr != _spellCooldowns.end();)
108 {
109 if (predicate(itr))
110 {
111 resetCooldowns.push_back(itr->first);
112 ResetCooldown(itr, false);
113 }
114 else
115 ++itr;
116 }
117
118 if (update && !resetCooldowns.empty())
119 SendClearCooldowns(resetCooldowns);
120 }
121
122 void ResetAllCooldowns();
123 bool HasCooldown(SpellInfo const* spellInfo, uint32 itemId = 0, bool ignoreCategoryCooldown = false) const;
124 bool HasCooldown(uint32 spellId, uint32 itemId = 0, bool ignoreCategoryCooldown = false) const;
125 bool HasCooldownOnHold(uint32 spellId) const;
126 uint32 GetRemainingCooldown(SpellInfo const* spellInfo) const;
127
128 // School lockouts
129 void LockSpellSchool(SpellSchoolMask schoolMask, uint32 lockoutTime);
130 bool IsSchoolLocked(SpellSchoolMask schoolMask) const;
131
132 // Global cooldown
133 bool HasGlobalCooldown(SpellInfo const* spellInfo) const;
134 void AddGlobalCooldown(SpellInfo const* spellInfo, uint32 duration);
135 void CancelGlobalCooldown(SpellInfo const* spellInfo);
136
137 void BuildCooldownPacket(WorldPacket& data, uint8 flags, uint32 spellId, uint32 cooldown) const;
138
139 static void GetCooldownDurations(SpellInfo const* spellInfo, uint32 itemId, int32* cooldown, uint32* categoryId, int32* categoryCooldown);
140
141 CooldownStorageType::size_type GetCooldownsSizeForPacket() const { return _spellCooldowns.size(); }
142 void SaveCooldownStateBeforeDuel();
143 void RestoreCooldownStateAfterDuel();
144
145private:
146 Player* GetPlayerOwner() const;
147 void SendClearCooldowns(std::vector<int32> const& cooldowns) const;
148 CooldownStorageType::iterator EraseCooldown(CooldownStorageType::iterator itr)
149 {
150 _categoryCooldowns.erase(itr->second.CategoryId);
151 return _spellCooldowns.erase(itr);
152 }
153
154 typedef std::unordered_map<uint32, uint32> PacketCooldowns;
155 void BuildCooldownPacket(WorldPacket& data, uint8 flags, PacketCooldowns const& cooldowns) const;
156
161 Clock::time_point _schoolLockouts[MAX_SPELL_SCHOOL];
163
164 template<class T>
166};
167
168#endif // SpellHistory_h__
SQLTransaction< CharacterDatabaseConnection > CharacterDatabaseTransaction
std::shared_ptr< PreparedResultSet > PreparedQueryResult
#define TC_GAME_API
Definition Define.h:114
uint8_t uint8
Definition Define.h:135
int32_t int32
Definition Define.h:129
uint32_t uint32
Definition Define.h:133
uint16 flags
static void SaveToDB(QuestPool const &pool, CharacterDatabaseTransaction trans)
@ MAX_SPELL_SCHOOL
SpellSchoolMask
SpellCooldownFlags
Spell cooldown flags sent in SMSG_SPELL_COOLDOWN.
@ SPELL_COOLDOWN_FLAG_INCLUDE_EVENT_COOLDOWNS
Starts GCD for spells that should start their cooldown on events, requires SPELL_COOLDOWN_FLAG_INCLUD...
@ SPELL_COOLDOWN_FLAG_INCLUDE_GCD
Starts GCD in addition to normal cooldown specified in the packet.
@ SPELL_COOLDOWN_FLAG_NONE
Definition Item.h:62
static Clock::duration const InfinityCooldownDelay
GlobalCooldownStorageType _globalCooldowns
CooldownStorageType::size_type GetCooldownsSizeForPacket() const
std::unordered_map< uint32, Clock::time_point > GlobalCooldownStorageType
static Clock::duration const InfinityCooldownDelayCheck
void AddCooldown(uint32 spellId, uint32 itemId, std::chrono::duration< Type, Period > cooldownDuration)
std::unordered_map< uint32, CooldownEntry * > CategoryCooldownStorageType
SpellHistory(Unit *owner)
CooldownStorageType _spellCooldowns
void WritePacket(WorldPacket &packet) const
std::chrono::system_clock Clock
CooldownStorageType _spellCooldownsBeforeDuel
std::unordered_map< uint32, CooldownEntry > CooldownStorageType
std::unordered_map< uint32, uint32 > PacketCooldowns
CooldownStorageType::iterator EraseCooldown(CooldownStorageType::iterator itr)
void ResetCooldowns(Predicate predicate, bool update=false)
CategoryCooldownStorageType _categoryCooldowns
Definition Spell.h:152
Definition Unit.h:783
SystemTimePoint GetSystemTime()
Current chrono system_clock time point.
Definition GameTime.cpp:52
Clock::time_point CooldownEnd
Clock::time_point CategoryEnd