diff --git a/src/common/Utilities/Timer.h b/src/common/Utilities/Timer.h index 4abd6cc8ca1..8acd5cbdc11 100644 --- a/src/common/Utilities/Timer.h +++ b/src/common/Utilities/Timer.h @@ -19,7 +19,7 @@ #define TRINITY_TIMER_H #include "Define.h" -#include +#include "Duration.h" inline std::chrono::steady_clock::time_point GetApplicationStartTime() { @@ -155,11 +155,18 @@ struct TimeTrackerSmall { } + TimeTrackerSmall(Milliseconds expiry) : i_expiryTime(expiry.count()) { } + void Update(int32 diff) { i_expiryTime -= diff; } + void Update(Milliseconds diff) + { + Update(diff.count()); + } + bool Passed() const { return i_expiryTime <= 0; @@ -170,9 +177,14 @@ struct TimeTrackerSmall i_expiryTime = interval; } - int32 GetExpiry() const + void Reset(Milliseconds expiry) { - return i_expiryTime; + Reset(expiry.count()); + } + + Milliseconds GetExpiry() const + { + return Milliseconds(i_expiryTime); } private: diff --git a/src/server/scripts/EasternKingdoms/ScarletMonastery/boss_headless_horseman.cpp b/src/server/scripts/EasternKingdoms/ScarletMonastery/boss_headless_horseman.cpp index 013552adeca..1db5952bd57 100644 --- a/src/server/scripts/EasternKingdoms/ScarletMonastery/boss_headless_horseman.cpp +++ b/src/server/scripts/EasternKingdoms/ScarletMonastery/boss_headless_horseman.cpp @@ -171,7 +171,7 @@ static char const* HeadlessHorsemanInitialPlayerTexts[] = struct npc_wisp_invis : public ScriptedAI { - npc_wisp_invis(Creature* creature) : ScriptedAI(creature), _timer(0), _creatureType(0), _firstSpell(0), _secondSpell(0) + npc_wisp_invis(Creature* creature) : ScriptedAI(creature), _timer(0s), _creatureType(0), _firstSpell(0), _secondSpell(0) { creature->SetDisplayId(DISPLAYID_INVIS_WISP_INVISIBLE); } @@ -187,16 +187,16 @@ struct npc_wisp_invis : public ScriptedAI _firstSpell = SPELL_HEADLESS_HORSEMAN___PUMPKIN_AURA; break; case INVIS_WISP_CREATURE_TYPE_FLAME: - _timer.Reset(15 * IN_MILLISECONDS); + _timer.Reset(15s); _firstSpell = SPELL_HEADLESS_HORSEMAN___FIRE; _secondSpell = SPELL_HEADLESS_HORSEMAN_CLIMAX___HEAD_IS_DEAD; break; case INVIS_WISP_CREATURE_TYPE_SMOKE: - _timer.Reset(15 * IN_MILLISECONDS); + _timer.Reset(15s); _firstSpell = SPELL_HEADLESS_HORSEMAN___SMOKE; break; case INVIS_WISP_CREATURE_TYPE_BLUE: - _timer.Reset(7 * IN_MILLISECONDS); + _timer.Reset(7s); _secondSpell = SPELL_HEADLESS_HORSEMAN___WISP_FLIGHT_MISSILE; break; default: @@ -232,7 +232,7 @@ struct npc_wisp_invis : public ScriptedAI me->RemoveAurasDueToSpell(SPELL_HEADLESS_HORSEMAN___SMOKE); if (_secondSpell) DoCast(me, _secondSpell); - _timer.Reset(0); + _timer.Reset(0s); } } @@ -245,7 +245,7 @@ struct npc_wisp_invis : public ScriptedAI struct npc_head : public ScriptedAI { - npc_head(Creature* creature) : ScriptedAI(creature), _laughTimer(urand(15 * IN_MILLISECONDS, 30 * IN_MILLISECONDS)) + npc_head(Creature* creature) : ScriptedAI(creature), _laughTimer(randtime(15s, 30s)) { creature->SetReactState(REACT_PASSIVE); Initialize(); @@ -262,7 +262,7 @@ struct npc_head : public ScriptedAI void Reset() override { Initialize(); - _laughTimer.Reset(urand(15 * IN_MILLISECONDS, 30 * IN_MILLISECONDS)); + _laughTimer.Reset(randtime(15s, 30s)); _scheduler.CancelAll(); // Just to be sure it's MOTION_SLOT_DEFAULT is static @@ -384,7 +384,7 @@ struct npc_head : public ScriptedAI if (_laughTimer.Passed()) { - _laughTimer.Reset(urand(15 * IN_MILLISECONDS, 30 * IN_MILLISECONDS)); + _laughTimer.Reset(randtime(15s, 30s)); DoPlaySoundToSet(me, Trinity::Containers::SelectRandomContainerElement(HeadlessHorsemanRandomLaughSound)); @@ -422,7 +422,7 @@ struct npc_head : public ScriptedAI void DoTalk(uint32 entry) { Talk(entry); - _laughTimer.Reset(3 * IN_MILLISECONDS); + _laughTimer.Reset(3s); if (Creature* speaker = DoSpawnCreature(NPC_HELPER, 0.f, 0.f, 0.f, 0.f, TEMPSUMMON_TIMED_DESPAWN, 1s)) speaker->CastSpell(speaker, SPELL_HEADLESS_HORSEMAN___SPEAKS, false); @@ -438,7 +438,7 @@ struct npc_head : public ScriptedAI struct boss_headless_horseman : public ScriptedAI { - boss_headless_horseman(Creature* creature) : ScriptedAI(creature), _instance(creature->GetInstanceScript()), _laughTimer(0), _phase(0), _id(0) + boss_headless_horseman(Creature* creature) : ScriptedAI(creature), _instance(creature->GetInstanceScript()), _laughTimer(0s), _phase(0), _id(0) { Initialize(); @@ -461,7 +461,7 @@ struct boss_headless_horseman : public ScriptedAI void Reset() override { - _laughTimer.Reset(0); + _laughTimer.Reset(0s); Initialize(); DoCastSelf(SPELL_HEADLESS_HORSEMAN_CLIMAX___HEAD_VISUAL); @@ -654,7 +654,7 @@ struct boss_headless_horseman : public ScriptedAI if (spellInfo->Id != SPELL_HEADLESS_HORSEMAN_CLIMAX___SEND_HEAD) return; - _laughTimer.Reset(urand(2 * IN_MILLISECONDS, 5 * IN_MILLISECONDS)); + _laughTimer.Reset(randtime(2s, 5s)); _withHead = true; _scheduler.CancelGroup(TASK_GROUP_WITHOUT_HEAD); @@ -783,7 +783,7 @@ struct boss_headless_horseman : public ScriptedAI if (_withHead && _laughTimer.Passed()) { - _laughTimer.Reset(urand(11 * IN_MILLISECONDS, 22 * IN_MILLISECONDS)); + _laughTimer.Reset(randtime(11s, 22s)); DoPlaySoundToSet(me, Trinity::Containers::SelectRandomContainerElement(HeadlessHorsemanRandomLaughSound)); } @@ -803,7 +803,7 @@ struct boss_headless_horseman : public ScriptedAI void DoTalk(uint8 textEntry, Unit* target = nullptr) { Talk(textEntry, target); - _laughTimer.Reset(std::min(10 * IN_MILLISECONDS, _laughTimer.GetExpiry() + 4 * IN_MILLISECONDS)); + _laughTimer.Reset(std::min(std::chrono::duration_cast(10s), _laughTimer.GetExpiry() + 4s)); } InstanceScript* _instance; diff --git a/src/server/scripts/EasternKingdoms/ScarletMonastery/boss_high_inquisitor_fairbanks.cpp b/src/server/scripts/EasternKingdoms/ScarletMonastery/boss_high_inquisitor_fairbanks.cpp index c6532c4209c..2f74d20bc49 100644 --- a/src/server/scripts/EasternKingdoms/ScarletMonastery/boss_high_inquisitor_fairbanks.cpp +++ b/src/server/scripts/EasternKingdoms/ScarletMonastery/boss_high_inquisitor_fairbanks.cpp @@ -63,12 +63,12 @@ class HighInquisitorFairbanksDispelMagicTargetSelector struct boss_high_inquisitor_fairbanks : public BossAI { - boss_high_inquisitor_fairbanks(Creature* creature) : BossAI(creature, DATA_HIGH_INQUISITOR_FAIRBANKS), _healTimer(0), _powerWordShield(false) { } + boss_high_inquisitor_fairbanks(Creature* creature) : BossAI(creature, DATA_HIGH_INQUISITOR_FAIRBANKS), _healTimer(0s), _powerWordShield(false) { } void Reset() override { _Reset(); - _healTimer.Reset(0); + _healTimer.Reset(0s); _powerWordShield = false; me->SetStandState(UNIT_STAND_STATE_DEAD); } @@ -95,7 +95,7 @@ struct boss_high_inquisitor_fairbanks : public BossAI if (!me->IsNonMeleeSpellCast(false) && _healTimer.Passed()) { - _healTimer.Reset(30 * IN_MILLISECONDS); + _healTimer.Reset(30s); DoCastSelf(SPELL_HEAL); } } diff --git a/src/server/scripts/EasternKingdoms/ScarletMonastery/boss_mograine_and_whitemane.cpp b/src/server/scripts/EasternKingdoms/ScarletMonastery/boss_mograine_and_whitemane.cpp index f00a7c1c3db..ab07d735037 100644 --- a/src/server/scripts/EasternKingdoms/ScarletMonastery/boss_mograine_and_whitemane.cpp +++ b/src/server/scripts/EasternKingdoms/ScarletMonastery/boss_mograine_and_whitemane.cpp @@ -77,7 +77,7 @@ Position const WhitemaneIntroMovePos = { 1163.113370f, 1398.856812f, 32.527786f, struct boss_scarlet_commander_mograine : public BossAI { public: - boss_scarlet_commander_mograine(Creature* creature) : BossAI(creature, DATA_MOGRAINE_AND_WHITE_EVENT), _killYellTimer(0) + boss_scarlet_commander_mograine(Creature* creature) : BossAI(creature, DATA_MOGRAINE_AND_WHITE_EVENT), _killYellTimer(0s) { Initialize(); } @@ -93,7 +93,7 @@ struct boss_scarlet_commander_mograine : public BossAI Initialize(); _Reset(); - _killYellTimer.Reset(0); + _killYellTimer.Reset(0s); DoCastSelf(SPELL_RETRIBUTION_AURA, true); me->RemoveFlag(UNIT_FIELD_FLAGS, UNIT_FLAG_NOT_SELECTABLE | UNIT_FLAG_NON_ATTACKABLE); @@ -128,7 +128,7 @@ struct boss_scarlet_commander_mograine : public BossAI if (_killYellTimer.Passed()) { Talk(SAY_MO_KILL); - _killYellTimer.Reset(5 * IN_MILLISECONDS); + _killYellTimer.Reset(5s); } } @@ -242,7 +242,7 @@ struct boss_scarlet_commander_mograine : public BossAI struct boss_high_inquisitor_whitemane : public ScriptedAI { public: - boss_high_inquisitor_whitemane(Creature* creature) : ScriptedAI(creature), _instance(creature->GetInstanceScript()), _killYellTimer(0) + boss_high_inquisitor_whitemane(Creature* creature) : ScriptedAI(creature), _instance(creature->GetInstanceScript()), _killYellTimer(0s) { Initialize(); } @@ -259,7 +259,7 @@ struct boss_high_inquisitor_whitemane : public ScriptedAI _events.Reset(); _scheduler.CancelAll(); - _killYellTimer.Reset(0); + _killYellTimer.Reset(0s); DoCastSelf(SPELL_RETRIBUTION_AURA); me->SetReactState(REACT_AGGRESSIVE); @@ -289,7 +289,7 @@ struct boss_high_inquisitor_whitemane : public ScriptedAI if (_killYellTimer.Passed()) { Talk(SAY_WH_KILL); - _killYellTimer.Reset(5 * IN_MILLISECONDS); + _killYellTimer.Reset(5s); } } diff --git a/tests/common/test-Timer.cpp b/tests/common/test-Timer.cpp new file mode 100644 index 00000000000..b71251b2ea9 --- /dev/null +++ b/tests/common/test-Timer.cpp @@ -0,0 +1,59 @@ +/* + * This file is part of the TrinityCore Project. See AUTHORS file for Copyright information + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or (at your + * option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + +#define CATCH_CONFIG_ENABLE_CHRONO_STRINGMAKER +#include "catch2/catch.hpp" + +#include "Timer.h" + +TEST_CASE("TimerTrackerSmall: Check if time passed") +{ + TimeTrackerSmall tracker(1000 /*ms*/); + REQUIRE_FALSE(tracker.Passed()); + REQUIRE(tracker.GetExpiry() == 1s); + + tracker.Update(500 /*ms*/); + REQUIRE_FALSE(tracker.Passed()); + REQUIRE(tracker.GetExpiry() == 500ms); + + tracker.Update(500 /*ms*/); + REQUIRE(tracker.Passed()); + REQUIRE(tracker.GetExpiry() == 0s); + + tracker.Update(500 /*ms*/); + REQUIRE(tracker.Passed()); + REQUIRE(tracker.GetExpiry() == -500ms); +} + +TEST_CASE("TimerTrackerSmall: Reset timer") +{ + TimeTrackerSmall tracker(1000 /*ms*/); + REQUIRE_FALSE(tracker.Passed()); + REQUIRE(tracker.GetExpiry() == 1s); + + tracker.Update(1000 /*ms*/); + REQUIRE(tracker.Passed()); + REQUIRE(tracker.GetExpiry() == 0s); + + tracker.Reset(1000 /*ms*/); + REQUIRE_FALSE(tracker.Passed()); + REQUIRE(tracker.GetExpiry() == 1s); + + tracker.Update(1000 /*ms*/); + REQUIRE(tracker.Passed()); + REQUIRE(tracker.GetExpiry() == 0s); +}