mirror of
https://github.com/TrinityCore/TrinityCore.git
synced 2026-01-15 23:20:36 +01:00
Core/Common: Add a std::chrono interface to TimeTrackerSmall
New methods: TimeTrackerSmall(Milliseconds expiry); void Update(Milliseconds diff); void Reset(Milliseconds expiry) Milliseconds GetExpiry() const Removed methods: int32 GetExpiry() const Also add basic unit tests. Core/Scripts: Use std::chrono interface of TimeTrackerSmall
This commit is contained in:
committed by
Peter Keresztes Schmidt
parent
9b806c6b5d
commit
e55516348d
@@ -19,7 +19,7 @@
|
||||
#define TRINITY_TIMER_H
|
||||
|
||||
#include "Define.h"
|
||||
#include <chrono>
|
||||
#include "Duration.h"
|
||||
|
||||
inline std::chrono::steady_clock::time_point GetApplicationStartTime()
|
||||
{
|
||||
@@ -155,11 +155,18 @@ public:
|
||||
{
|
||||
}
|
||||
|
||||
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 @@ public:
|
||||
i_expiryTime = interval;
|
||||
}
|
||||
|
||||
int32 GetExpiry() const
|
||||
void Reset(Milliseconds expiry)
|
||||
{
|
||||
return i_expiryTime;
|
||||
Reset(expiry.count());
|
||||
}
|
||||
|
||||
Milliseconds GetExpiry() const
|
||||
{
|
||||
return Milliseconds(i_expiryTime);
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
@@ -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 @@ private:
|
||||
|
||||
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 @@ private:
|
||||
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 @@ private:
|
||||
|
||||
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 @@ private:
|
||||
void DoTalk(uint8 textEntry, Unit* target = nullptr)
|
||||
{
|
||||
Talk(textEntry, target);
|
||||
_laughTimer.Reset(std::min<uint32>(10 * IN_MILLISECONDS, _laughTimer.GetExpiry() + 4 * IN_MILLISECONDS));
|
||||
_laughTimer.Reset(std::min(std::chrono::duration_cast<Milliseconds>(10s), _laughTimer.GetExpiry() + 4s));
|
||||
}
|
||||
|
||||
InstanceScript* _instance;
|
||||
|
||||
@@ -63,12 +63,12 @@ private:
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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 @@ public:
|
||||
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 @@ public:
|
||||
if (_killYellTimer.Passed())
|
||||
{
|
||||
Talk(SAY_MO_KILL);
|
||||
_killYellTimer.Reset(5 * IN_MILLISECONDS);
|
||||
_killYellTimer.Reset(5s);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -242,7 +242,7 @@ private:
|
||||
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 @@ public:
|
||||
|
||||
_events.Reset();
|
||||
_scheduler.CancelAll();
|
||||
_killYellTimer.Reset(0);
|
||||
_killYellTimer.Reset(0s);
|
||||
|
||||
DoCastSelf(SPELL_RETRIBUTION_AURA);
|
||||
me->SetReactState(REACT_AGGRESSIVE);
|
||||
@@ -289,7 +289,7 @@ public:
|
||||
if (_killYellTimer.Passed())
|
||||
{
|
||||
Talk(SAY_WH_KILL);
|
||||
_killYellTimer.Reset(5 * IN_MILLISECONDS);
|
||||
_killYellTimer.Reset(5s);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
59
tests/common/test-Timer.cpp
Normal file
59
tests/common/test-Timer.cpp
Normal file
@@ -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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#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);
|
||||
}
|
||||
Reference in New Issue
Block a user