mirror of
https://github.com/mod-playerbots/azerothcore-wotlk.git
synced 2026-01-13 09:17:18 +00:00
208 lines
3.8 KiB
C++
208 lines
3.8 KiB
C++
/*
|
|
* Copyright (C) 2016+ AzerothCore <www.azerothcore.org>, released under GNU GPL v2 license: https://github.com/azerothcore/azerothcore-wotlk/blob/master/LICENSE-GPL2
|
|
* Copyright (C) 2008-2016 TrinityCore <http://www.trinitycore.org/>
|
|
* Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
|
|
*/
|
|
|
|
#ifndef ACORE_TIMER_H
|
|
#define ACORE_TIMER_H
|
|
|
|
#include "Common.h"
|
|
#include "Duration.h"
|
|
|
|
inline TimePoint GetApplicationStartTime()
|
|
{
|
|
using namespace std::chrono;
|
|
|
|
static const steady_clock::time_point ApplicationStartTime = steady_clock::now();
|
|
|
|
return ApplicationStartTime;
|
|
}
|
|
|
|
inline uint32 getMSTime()
|
|
{
|
|
using namespace std::chrono;
|
|
|
|
return uint32(duration_cast<milliseconds>(steady_clock::now() - GetApplicationStartTime()).count());
|
|
}
|
|
|
|
inline uint32 getMSTimeDiff(uint32 oldMSTime, uint32 newMSTime)
|
|
{
|
|
// getMSTime() have limited data range and this is case when it overflow in this tick
|
|
if (oldMSTime > newMSTime)
|
|
return (0xFFFFFFFF - oldMSTime) + newMSTime;
|
|
else
|
|
return newMSTime - oldMSTime;
|
|
}
|
|
|
|
inline uint32 getMSTimeDiff(uint32 oldMSTime, TimePoint newTime)
|
|
{
|
|
using namespace std::chrono;
|
|
|
|
uint32 newMSTime = uint32(duration_cast<milliseconds>(newTime - GetApplicationStartTime()).count());
|
|
return getMSTimeDiff(oldMSTime, newMSTime);
|
|
}
|
|
|
|
inline uint32 GetMSTimeDiffToNow(uint32 oldMSTime)
|
|
{
|
|
return getMSTimeDiff(oldMSTime, getMSTime());
|
|
}
|
|
|
|
struct IntervalTimer
|
|
{
|
|
public:
|
|
|
|
IntervalTimer()
|
|
|
|
{
|
|
}
|
|
|
|
void Update(time_t diff)
|
|
{
|
|
_current += diff;
|
|
if (_current < 0)
|
|
_current = 0;
|
|
}
|
|
|
|
bool Passed()
|
|
{
|
|
return _current >= _interval;
|
|
}
|
|
|
|
void Reset()
|
|
{
|
|
if (_current >= _interval)
|
|
_current %= _interval;
|
|
}
|
|
|
|
void SetCurrent(time_t current)
|
|
{
|
|
_current = current;
|
|
}
|
|
|
|
void SetInterval(time_t interval)
|
|
{
|
|
_interval = interval;
|
|
}
|
|
|
|
[[nodiscard]] time_t GetInterval() const
|
|
{
|
|
return _interval;
|
|
}
|
|
|
|
[[nodiscard]] time_t GetCurrent() const
|
|
{
|
|
return _current;
|
|
}
|
|
|
|
private:
|
|
|
|
time_t _interval{0};
|
|
time_t _current{0};
|
|
};
|
|
|
|
struct TimeTracker
|
|
{
|
|
public:
|
|
|
|
TimeTracker(time_t expiry)
|
|
: i_expiryTime(expiry)
|
|
{
|
|
}
|
|
|
|
void Update(time_t diff)
|
|
{
|
|
i_expiryTime -= diff;
|
|
}
|
|
|
|
[[nodiscard]] bool Passed() const
|
|
{
|
|
return i_expiryTime <= 0;
|
|
}
|
|
|
|
void Reset(time_t interval)
|
|
{
|
|
i_expiryTime = interval;
|
|
}
|
|
|
|
[[nodiscard]] time_t GetExpiry() const
|
|
{
|
|
return i_expiryTime;
|
|
}
|
|
|
|
private:
|
|
|
|
time_t i_expiryTime;
|
|
};
|
|
|
|
struct TimeTrackerSmall
|
|
{
|
|
public:
|
|
|
|
TimeTrackerSmall(uint32 expiry = 0)
|
|
: i_expiryTime(expiry)
|
|
{
|
|
}
|
|
|
|
void Update(int32 diff)
|
|
{
|
|
i_expiryTime -= diff;
|
|
}
|
|
|
|
[[nodiscard]] bool Passed() const
|
|
{
|
|
return i_expiryTime <= 0;
|
|
}
|
|
|
|
void Reset(uint32 interval)
|
|
{
|
|
i_expiryTime = interval;
|
|
}
|
|
|
|
[[nodiscard]] int32 GetExpiry() const
|
|
{
|
|
return i_expiryTime;
|
|
}
|
|
|
|
private:
|
|
|
|
int32 i_expiryTime;
|
|
};
|
|
|
|
struct PeriodicTimer
|
|
{
|
|
public:
|
|
|
|
PeriodicTimer(int32 period, int32 start_time)
|
|
: i_period(period), i_expireTime(start_time)
|
|
{
|
|
}
|
|
|
|
bool Update(const uint32 diff)
|
|
{
|
|
if ((i_expireTime -= diff) > 0)
|
|
return false;
|
|
|
|
i_expireTime += i_period > int32(diff) ? i_period : diff;
|
|
return true;
|
|
}
|
|
|
|
void SetPeriodic(int32 period, int32 start_time)
|
|
{
|
|
i_expireTime = start_time;
|
|
i_period = period;
|
|
}
|
|
|
|
// Tracker interface
|
|
void TUpdate(int32 diff) { i_expireTime -= diff; }
|
|
[[nodiscard]] bool TPassed() const { return i_expireTime <= 0; }
|
|
void TReset(int32 diff, int32 period) { i_expireTime += period > diff ? period : diff; }
|
|
|
|
private:
|
|
|
|
int32 i_period;
|
|
int32 i_expireTime;
|
|
};
|
|
|
|
#endif
|