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 uint32 GetRemainingCooldown(SpellInfo const* spellInfo) const;
126
127 // School lockouts
128 void LockSpellSchool(SpellSchoolMask schoolMask, uint32 lockoutTime);
129 bool IsSchoolLocked(SpellSchoolMask schoolMask) const;
130
131 // Global cooldown
132 bool HasGlobalCooldown(SpellInfo const* spellInfo) const;
133 void AddGlobalCooldown(SpellInfo const* spellInfo, uint32 duration);
134 void CancelGlobalCooldown(SpellInfo const* spellInfo);
135
136 void BuildCooldownPacket(WorldPacket& data, uint8 flags, uint32 spellId, uint32 cooldown) const;
137
138 static void GetCooldownDurations(SpellInfo const* spellInfo, uint32 itemId, int32* cooldown, uint32* categoryId, int32* categoryCooldown);
139
140 CooldownStorageType::size_type GetCooldownsSizeForPacket() const { return _spellCooldowns.size(); }
141 void SaveCooldownStateBeforeDuel();
142 void RestoreCooldownStateAfterDuel();
143
144private:
145 Player* GetPlayerOwner() const;
146 void SendClearCooldowns(std::vector<int32> const& cooldowns) const;
147 CooldownStorageType::iterator EraseCooldown(CooldownStorageType::iterator itr)
148 {
149 _categoryCooldowns.erase(itr->second.CategoryId);
150 return _spellCooldowns.erase(itr);
151 }
152
153 typedef std::unordered_map<uint32, uint32> PacketCooldowns;
154 void BuildCooldownPacket(WorldPacket& data, uint8 flags, PacketCooldowns const& cooldowns) const;
155
160 Clock::time_point _schoolLockouts[MAX_SPELL_SCHOOL];
162
163 template<class T>
165};
166
167#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:769
SystemTimePoint GetSystemTime()
Current chrono system_clock time point.
Definition GameTime.cpp:52
Clock::time_point CooldownEnd
Clock::time_point CategoryEnd