Skip to content

Commit

Permalink
Core/Common: Add a std::chrono interface to TimeTrackerSmall
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Carbenium committed Aug 1, 2020
1 parent 9b806c6 commit e555163
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 26 deletions.
18 changes: 15 additions & 3 deletions src/common/Utilities/Timer.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#define TRINITY_TIMER_H

#include "Define.h"
#include <chrono>
#include "Duration.h"

inline std::chrono::steady_clock::time_point GetApplicationStartTime()
{
Expand Down Expand Up @@ -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;
Expand All @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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:
Expand Down Expand Up @@ -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);
}
}

Expand All @@ -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();
Expand All @@ -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
Expand Down Expand Up @@ -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));

Expand Down Expand Up @@ -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);
Expand All @@ -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();

Expand All @@ -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);
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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));
}

Expand All @@ -803,7 +803,7 @@ struct boss_headless_horseman : public ScriptedAI
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand All @@ -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);
Expand Down Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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();
}
Expand All @@ -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);
Expand Down Expand Up @@ -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);
}
}

Expand Down
59 changes: 59 additions & 0 deletions tests/common/test-Timer.cpp
Original file line number Diff line number Diff line change
@@ -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);
}

0 comments on commit e555163

Please sign in to comment.