TrinityCore
Loading...
Searching...
No Matches
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);
97 uint32 remainingCooldown = player->GetSpellHistory()->GetRemainingCooldown(spellInfo);
98 int32 totalCooldown = spellInfo->RecoveryTime;
99 int32 categoryCooldown = spellInfo->CategoryRecoveryTime;
100
101 player->ApplySpellMod(spellInfo->Id, SPELLMOD_COOLDOWN, totalCooldown, nullptr);
102
103 if (int32 cooldownMod = player->GetTotalAuraModifier(SPELL_AURA_MOD_COOLDOWN))
104 totalCooldown += cooldownMod * IN_MILLISECONDS;
105
107 player->ApplySpellMod(spellInfo->Id, SPELLMOD_COOLDOWN, categoryCooldown, nullptr);
108
109 return remainingCooldown > 0
110 && !itr->second.OnHold
111 && Milliseconds(totalCooldown) < Minutes(10)
112 && Milliseconds(categoryCooldown) < Minutes(10)
113 && Milliseconds(remainingCooldown) < Minutes(10)
114 && (onStartDuel ? Milliseconds(totalCooldown - remainingCooldown) > Seconds(30) : true)
115 && (onStartDuel ? Milliseconds(categoryCooldown - remainingCooldown) > Seconds(30) : true);
116 }, true);
117
118 // pet cooldowns
119 if (Pet* pet = player->GetPet())
120 pet->GetSpellHistory()->ResetAllCooldowns();
121 }
122};
123
125{
126 new DuelResetScript();
127}
@ IN_MILLISECONDS
Definition Common.h:35
int32_t int32
Definition Define.h:129
uint32_t uint32
Definition Define.h:133
std::chrono::seconds Seconds
Seconds shorthand typedef.
Definition Duration.h:27
std::chrono::milliseconds Milliseconds
Milliseconds shorthand typedef.
Definition Duration.h:24
std::chrono::minutes Minutes
Minutes shorthand typedef.
Definition Duration.h:30
@ CLASS_DRUID
@ POWER_MANA
DuelCompleteType
@ DUEL_WON
@ SPELL_ATTR6_IGNORE_CATEGORY_COOLDOWN_MODS
@ SPELL_AURA_MOD_COOLDOWN
@ SPELLMOD_COOLDOWN
#define sSpellMgr
Definition SpellMgr.h:738
void OnDuelEnd(Player *winner, Player *loser, DuelCompleteType type) override
void OnDuelStart(Player *player1, Player *player2) override
static void ResetSpellCooldowns(Player *player, bool onStartDuel)
Definition Pet.h:40
void SaveHealthBeforeDuel()
Definition Player.h:1875
Pet * GetPet() const
Definition Player.cpp:20286
void RestoreManaAfterDuel()
Definition Player.h:1878
void RestoreHealthAfterDuel()
Definition Player.h:1877
void ApplySpellMod(uint32 spellId, SpellModOp op, T &basevalue, Spell *spell=nullptr) const
Definition Player.cpp:20807
void SaveManaBeforeDuel()
Definition Player.h:1876
void ResetAllPowers()
Definition Player.cpp:2054
uint32 GetRemainingCooldown(SpellInfo const *spellInfo) const
void RestoreCooldownStateAfterDuel()
void SaveCooldownStateBeforeDuel()
void ResetCooldowns(Predicate predicate, bool update=false)
uint32 RecoveryTime
Definition SpellInfo.h:317
uint32 Id
Definition SpellInfo.h:289
uint32 CategoryRecoveryTime
Definition SpellInfo.h:318
bool HasAttribute(SpellAttr0 attribute) const
Definition SpellInfo.h:375
uint8 GetClass() const
Definition Unit.h:895
Powers GetPowerType() const
Definition Unit.h:931
int32 GetTotalAuraModifier(AuraType auraType) const
Definition Unit.cpp:4792
SpellHistory * GetSpellHistory()
Definition Unit.h:1484
void AddSC_duel_reset()
#define sWorld
Definition World.h:900
@ CONFIG_RESET_DUEL_HEALTH_MANA
Definition World.h:166
@ CONFIG_RESET_DUEL_COOLDOWNS
Definition World.h:165