TrinityCore
duel_reset.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 "ScriptMgr.h"
19#include "GameTime.h"
20#include "Pet.h"
21#include "Player.h"
22#include "SpellHistory.h"
23#include "SpellInfo.h"
24#include "SpellMgr.h"
25#include "World.h"
26
28{
29 public:
30 DuelResetScript() : PlayerScript("DuelResetScript") { }
31
32 // Called when a duel starts (after 3s countdown)
33 void OnDuelStart(Player* player1, Player* player2) override
34 {
35 // Cooldowns reset
36 if (sWorld->getBoolConfig(CONFIG_RESET_DUEL_COOLDOWNS))
37 {
40
41 ResetSpellCooldowns(player1, true);
42 ResetSpellCooldowns(player2, true);
43 }
44
45 // Health and mana reset
46 if (sWorld->getBoolConfig(CONFIG_RESET_DUEL_HEALTH_MANA))
47 {
48 player1->SaveHealthBeforeDuel();
49 player1->SaveManaBeforeDuel();
50 player1->ResetAllPowers();
51
52 player2->SaveHealthBeforeDuel();
53 player2->SaveManaBeforeDuel();
54 player2->ResetAllPowers();
55 }
56 }
57
58 // Called when a duel ends
59 void OnDuelEnd(Player* winner, Player* loser, DuelCompleteType type) override
60 {
61 // do not reset anything if DUEL_INTERRUPTED or DUEL_FLED
62 if (type == DUEL_WON)
63 {
64 // Cooldown restore
65 if (sWorld->getBoolConfig(CONFIG_RESET_DUEL_COOLDOWNS))
66 {
67 ResetSpellCooldowns(winner, false);
68 ResetSpellCooldowns(loser, false);
69
72 }
73
74 // Health and mana restore
75 if (sWorld->getBoolConfig(CONFIG_RESET_DUEL_HEALTH_MANA))
76 {
77 winner->RestoreHealthAfterDuel();
79
80 // check if player1 class uses mana
81 if (winner->GetPowerType() == POWER_MANA || winner->GetClass() == CLASS_DRUID)
82 winner->RestoreManaAfterDuel();
83
84 // check if player2 class uses mana
85 if (loser->GetPowerType() == POWER_MANA || loser->GetClass() == CLASS_DRUID)
86 loser->RestoreManaAfterDuel();
87 }
88 }
89 }
90
91 static void ResetSpellCooldowns(Player* player, bool onStartDuel)
92 {
93 // remove cooldowns on spells that have < 10 min CD > 30 sec and has no onHold
94 player->GetSpellHistory()->ResetCooldowns([player, onStartDuel](SpellHistory::CooldownStorageType::iterator itr) -> bool
95 {
96 SpellInfo const* spellInfo = sSpellMgr->AssertSpellInfo(itr->first, DIFFICULTY_NONE);
97 Milliseconds remainingCooldown = player->GetSpellHistory()->GetRemainingCooldown(spellInfo);
98 Milliseconds totalCooldown = Milliseconds(spellInfo->RecoveryTime);
99 Milliseconds categoryCooldown = Milliseconds(spellInfo->CategoryRecoveryTime);
100
101 auto applySpellMod = [&](Milliseconds& value)
102 {
103 int32 intValue = value.count();
104 player->ApplySpellMod(spellInfo, SpellModOp::Cooldown, intValue, nullptr);
105 value = Milliseconds(intValue);
106 };
107
108 applySpellMod(totalCooldown);
109
110 if (int32 cooldownMod = player->GetTotalAuraModifier(SPELL_AURA_MOD_COOLDOWN))
111 totalCooldown += Milliseconds(cooldownMod);
112
114 applySpellMod(categoryCooldown);
115
116 return remainingCooldown > 0ms
117 && !itr->second.OnHold
118 && Milliseconds(totalCooldown) < 10min
119 && Milliseconds(categoryCooldown) < 10min
120 && Milliseconds(remainingCooldown) < 10min
121 && (onStartDuel ? totalCooldown - remainingCooldown > 30s : true)
122 && (onStartDuel ? categoryCooldown - remainingCooldown > 30s : true);
123 }, true);
124
125 // pet cooldowns
126 if (Pet* pet = player->GetPet())
127 pet->GetSpellHistory()->ResetAllCooldowns();
128 }
129};
130
132{
133 new DuelResetScript();
134}
@ DIFFICULTY_NONE
Definition: DBCEnums.h:874
int32_t int32
Definition: Define.h:138
std::chrono::milliseconds Milliseconds
Milliseconds shorthand typedef.
Definition: Duration.h:29
@ CLASS_DRUID
@ POWER_MANA
DuelCompleteType
@ DUEL_WON
@ SPELL_ATTR6_NO_CATEGORY_COOLDOWN_MODS
@ SPELL_AURA_MOD_COOLDOWN
#define sSpellMgr
Definition: SpellMgr.h:849
void OnDuelEnd(Player *winner, Player *loser, DuelCompleteType type) override
Definition: duel_reset.cpp:59
void OnDuelStart(Player *player1, Player *player2) override
Definition: duel_reset.cpp:33
static void ResetSpellCooldowns(Player *player, bool onStartDuel)
Definition: duel_reset.cpp:91
Definition: Pet.h:40
void SaveHealthBeforeDuel()
Definition: Player.h:2271
Pet * GetPet() const
Definition: Player.cpp:21513
void RestoreManaAfterDuel()
Definition: Player.h:2274
void RestoreHealthAfterDuel()
Definition: Player.h:2273
void SaveManaBeforeDuel()
Definition: Player.h:2272
void ResetAllPowers()
Definition: Player.cpp:1903
Duration GetRemainingCooldown(SpellInfo const *spellInfo) const
void RestoreCooldownStateAfterDuel()
void SaveCooldownStateBeforeDuel()
void ResetCooldowns(Predicate predicate, bool update=false)
Definition: SpellHistory.h:131
uint32 RecoveryTime
Definition: SpellInfo.h:366
uint32 CategoryRecoveryTime
Definition: SpellInfo.h:367
bool HasAttribute(SpellAttr0 attribute) const
Definition: SpellInfo.h:449
uint8 GetClass() const
Definition: Unit.h:752
Powers GetPowerType() const
Definition: Unit.h:799
int32 GetTotalAuraModifier(AuraType auraType) const
Definition: Unit.cpp:4929
SpellHistory * GetSpellHistory()
Definition: Unit.h:1457
void AddSC_duel_reset()
Definition: duel_reset.cpp:131
#define sWorld
Definition: World.h:931
@ CONFIG_RESET_DUEL_HEALTH_MANA
Definition: World.h:180
@ CONFIG_RESET_DUEL_COOLDOWNS
Definition: World.h:179