Add TIMER_FLAG_TICK_PRECISE - a new method for tick-precise timer intervals
This commit is contained in:
+79
-55
@@ -36,12 +36,12 @@
|
||||
#include "ConVarManager.h"
|
||||
#include "logic_bridge.h"
|
||||
|
||||
#define TIMER_MIN_ACCURACY 0.1
|
||||
#define TIMER_MIN_ACCURACY 0.1
|
||||
|
||||
TimerSystem g_Timers;
|
||||
double g_fUniversalTime = 0.0f;
|
||||
float g_fGameStartTime = 0.0f; /* Game game start time, non-universal */
|
||||
double g_fTimerThink = 0.0f; /* Timer's next think time */
|
||||
float g_fGameStartTime = 0.0f; /* Game game start time, non-universal */
|
||||
double g_fTimerThink = 0.0f; /* Timer's next think time */
|
||||
const double *g_pUniversalTime = &g_fUniversalTime;
|
||||
ConVar *mp_timelimit = NULL;
|
||||
int g_TimeLeftMode = 0;
|
||||
@@ -146,9 +146,10 @@ private:
|
||||
*/
|
||||
inline double CalcNextThink(double last, float interval)
|
||||
{
|
||||
if (g_fUniversalTime - last - interval <= TIMER_MIN_ACCURACY)
|
||||
const double next = last + interval;
|
||||
if (g_fUniversalTime - next <= TIMER_MIN_ACCURACY)
|
||||
{
|
||||
return last + interval;
|
||||
return next;
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -156,7 +157,7 @@ inline double CalcNextThink(double last, float interval)
|
||||
}
|
||||
}
|
||||
|
||||
void ITimer::Initialize(ITimedEvent *pCallbacks, float fInterval, float fToExec, void *pData, int flags)
|
||||
void ITimer::Initialize(ITimedEvent *pCallbacks, float fInterval, double fToExec, void *pData, int flags)
|
||||
{
|
||||
m_Listener = pCallbacks;
|
||||
m_Interval = fInterval;
|
||||
@@ -234,10 +235,11 @@ void TimerSystem::GameFrame(bool simulating)
|
||||
m_fLastTickedTime = gpGlobals->curtime;
|
||||
m_bHasMapTickedYet = true;
|
||||
|
||||
if (g_fUniversalTime >= g_fTimerThink)
|
||||
{
|
||||
RunFrame();
|
||||
const bool timerThink = g_fUniversalTime >= g_fTimerThink;
|
||||
RunFrame(timerThink);
|
||||
|
||||
if (timerThink)
|
||||
{
|
||||
g_fTimerThink = CalcNextThink(g_fTimerThink, TIMER_MIN_ACCURACY);
|
||||
}
|
||||
|
||||
@@ -249,31 +251,13 @@ void TimerSystem::GameFrame(bool simulating)
|
||||
}
|
||||
}
|
||||
|
||||
void TimerSystem::RunFrame()
|
||||
void TimerSystem::ProcessRepeatTimers(double curtime, List<ITimer*>& timerList)
|
||||
{
|
||||
ITimer *pTimer;
|
||||
ITimer *pTimer;
|
||||
TimerIter iter;
|
||||
|
||||
double curtime = GetSimulatedTime();
|
||||
for (iter=m_SingleTimers.begin(); iter!=m_SingleTimers.end(); )
|
||||
{
|
||||
pTimer = (*iter);
|
||||
if (curtime >= pTimer->m_ToExec)
|
||||
{
|
||||
pTimer->m_InExec = true;
|
||||
pTimer->m_Listener->OnTimer(pTimer, pTimer->m_pData);
|
||||
pTimer->m_Listener->OnTimerEnd(pTimer, pTimer->m_pData);
|
||||
iter = m_SingleTimers.erase(iter);
|
||||
m_FreeTimers.push(pTimer);
|
||||
}
|
||||
else
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
ResultType res;
|
||||
for (iter=m_LoopTimers.begin(); iter!=m_LoopTimers.end(); )
|
||||
ResultType res;
|
||||
for (iter=timerList.begin(); iter!=timerList.end(); )
|
||||
{
|
||||
pTimer = (*iter);
|
||||
if (curtime >= pTimer->m_ToExec)
|
||||
@@ -283,7 +267,7 @@ void TimerSystem::RunFrame()
|
||||
if (pTimer->m_KillMe || (res == Pl_Stop))
|
||||
{
|
||||
pTimer->m_Listener->OnTimerEnd(pTimer, pTimer->m_pData);
|
||||
iter = m_LoopTimers.erase(iter);
|
||||
iter = timerList.erase(iter);
|
||||
m_FreeTimers.push(pTimer);
|
||||
continue;
|
||||
}
|
||||
@@ -294,11 +278,53 @@ void TimerSystem::RunFrame()
|
||||
}
|
||||
}
|
||||
|
||||
void TimerSystem::RunFrame(bool timerThink)
|
||||
{
|
||||
const double curtime = GetSimulatedTime();
|
||||
|
||||
//// One-off timers
|
||||
ITimer *pTimer;
|
||||
TimerIter iter;
|
||||
for (iter=m_SingleTimers.begin(); iter!=m_SingleTimers.end(); )
|
||||
{
|
||||
pTimer = (*iter);
|
||||
|
||||
// m_SingleTimers is sorted
|
||||
if (curtime < pTimer->m_ToExec)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
if (timerThink || pTimer->m_Flags & TIMER_FLAG_TICK_PRECISE)
|
||||
{
|
||||
pTimer->m_InExec = true;
|
||||
pTimer->m_Listener->OnTimer(pTimer, pTimer->m_pData);
|
||||
pTimer->m_Listener->OnTimerEnd(pTimer, pTimer->m_pData);
|
||||
iter = m_SingleTimers.erase(iter);
|
||||
m_FreeTimers.push(pTimer);
|
||||
}
|
||||
else
|
||||
{
|
||||
iter++;
|
||||
}
|
||||
}
|
||||
|
||||
//// Repeating timers
|
||||
// Most repeating timers do not need to be updated every frame
|
||||
if (timerThink)
|
||||
{
|
||||
ProcessRepeatTimers(curtime, m_LowSpeedLoopTimers);
|
||||
}
|
||||
|
||||
// High speed repeating timers will always update
|
||||
ProcessRepeatTimers(curtime, m_HighSpeedLoopTimers);
|
||||
}
|
||||
|
||||
ITimer *TimerSystem::CreateTimer(ITimedEvent *pCallbacks, float fInterval, void *pData, int flags)
|
||||
{
|
||||
ITimer *pTimer;
|
||||
TimerIter iter;
|
||||
float to_exec = GetSimulatedTime() + fInterval;
|
||||
const double to_exec = GetSimulatedTime() + fInterval;
|
||||
|
||||
if (m_FreeTimers.empty())
|
||||
{
|
||||
@@ -312,7 +338,8 @@ ITimer *TimerSystem::CreateTimer(ITimedEvent *pCallbacks, float fInterval, void
|
||||
|
||||
if (flags & TIMER_FLAG_REPEAT)
|
||||
{
|
||||
m_LoopTimers.push_back(pTimer);
|
||||
List<ITimer*>& timerList = pTimer->m_Flags & TIMER_FLAG_TICK_PRECISE ? m_HighSpeedLoopTimers : m_LowSpeedLoopTimers;
|
||||
timerList.push_back(pTimer);
|
||||
goto return_timer;
|
||||
}
|
||||
|
||||
@@ -370,8 +397,10 @@ void TimerSystem::FireTimerOnce(ITimer *pTimer, bool delayExec)
|
||||
pTimer->m_InExec = false;
|
||||
return;
|
||||
}
|
||||
|
||||
List<ITimer*>& timerList = pTimer->m_Flags & TIMER_FLAG_TICK_PRECISE ? m_HighSpeedLoopTimers : m_LowSpeedLoopTimers;
|
||||
pTimer->m_Listener->OnTimerEnd(pTimer, pTimer->m_pData);
|
||||
m_LoopTimers.remove(pTimer);
|
||||
timerList.remove(pTimer);
|
||||
m_FreeTimers.push(pTimer);
|
||||
}
|
||||
}
|
||||
@@ -389,12 +418,13 @@ void TimerSystem::KillTimer(ITimer *pTimer)
|
||||
return;
|
||||
}
|
||||
|
||||
pTimer->m_InExec = true; /* The timer it's not really executed but this check needs to be done */
|
||||
pTimer->m_InExec = true; /* The timer is not really executed but this check needs to be done */
|
||||
pTimer->m_Listener->OnTimerEnd(pTimer, pTimer->m_pData);
|
||||
|
||||
if (pTimer->m_Flags & TIMER_FLAG_REPEAT)
|
||||
{
|
||||
m_LoopTimers.remove(pTimer);
|
||||
List<ITimer*>& timerList = pTimer->m_Flags & TIMER_FLAG_TICK_PRECISE ? m_HighSpeedLoopTimers : m_LowSpeedLoopTimers;
|
||||
timerList.remove(pTimer);
|
||||
} else {
|
||||
m_SingleTimers.remove(pTimer);
|
||||
}
|
||||
@@ -405,26 +435,20 @@ void TimerSystem::KillTimer(ITimer *pTimer)
|
||||
CStack<ITimer *> s_tokill;
|
||||
void TimerSystem::RemoveMapChangeTimers()
|
||||
{
|
||||
ITimer *pTimer;
|
||||
TimerIter iter;
|
||||
const auto KillMapchangeTimers = [](List<ITimer*>& timerList) {
|
||||
for (ITimer* pTimer : timerList)
|
||||
{
|
||||
if (pTimer->m_Flags & TIMER_FLAG_NO_MAPCHANGE)
|
||||
{
|
||||
s_tokill.push(pTimer);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
for (iter=m_SingleTimers.begin(); iter!=m_SingleTimers.end(); iter++)
|
||||
{
|
||||
pTimer = (*iter);
|
||||
if (pTimer->m_Flags & TIMER_FLAG_NO_MAPCHANGE)
|
||||
{
|
||||
s_tokill.push(pTimer);
|
||||
}
|
||||
}
|
||||
KillMapchangeTimers(m_SingleTimers);
|
||||
|
||||
for (iter=m_LoopTimers.begin(); iter!=m_LoopTimers.end(); iter++)
|
||||
{
|
||||
pTimer = (*iter);
|
||||
if (pTimer->m_Flags & TIMER_FLAG_NO_MAPCHANGE)
|
||||
{
|
||||
s_tokill.push(pTimer);
|
||||
}
|
||||
}
|
||||
KillMapchangeTimers(m_LowSpeedLoopTimers);
|
||||
KillMapchangeTimers(m_HighSpeedLoopTimers);
|
||||
|
||||
while (!s_tokill.empty())
|
||||
{
|
||||
|
||||
+7
-5
@@ -41,13 +41,12 @@
|
||||
using namespace SourceHook;
|
||||
using namespace SourceMod;
|
||||
|
||||
typedef List<ITimer *> TimerList;
|
||||
typedef List<ITimer *>::iterator TimerIter;
|
||||
|
||||
class SourceMod::ITimer
|
||||
{
|
||||
public:
|
||||
void Initialize(ITimedEvent *pCallbacks, float fInterval, float fToExec, void *pData, int flags);
|
||||
void Initialize(ITimedEvent *pCallbacks, float fInterval, double fToExec, void *pData, int flags);
|
||||
ITimedEvent *m_Listener;
|
||||
void *m_pData;
|
||||
float m_Interval;
|
||||
@@ -80,12 +79,15 @@ public: //ITimerSystem
|
||||
bool GetMapTimeLeft(float *pTime);
|
||||
IMapTimer *GetMapTimer();
|
||||
public:
|
||||
void RunFrame();
|
||||
void RunFrame(bool timerThink);
|
||||
void RemoveMapChangeTimers();
|
||||
void GameFrame(bool simulating);
|
||||
private:
|
||||
List<ITimer *> m_SingleTimers;
|
||||
List<ITimer *> m_LoopTimers;
|
||||
void ProcessRepeatTimers(double curtime, List<ITimer*>& timerList);
|
||||
private:
|
||||
List<ITimer*> m_SingleTimers;
|
||||
List<ITimer*> m_LowSpeedLoopTimers;
|
||||
List<ITimer*> m_HighSpeedLoopTimers;
|
||||
CStack<ITimer *> m_FreeTimers;
|
||||
IMapTimer *m_pMapTimer;
|
||||
|
||||
|
||||
@@ -39,6 +39,7 @@
|
||||
|
||||
#define TIMER_REPEAT (1<<0) /**< Timer will repeat until it returns Plugin_Stop */
|
||||
#define TIMER_FLAG_NO_MAPCHANGE (1<<1) /**< Timer will not carry over mapchanges */
|
||||
#define TIMER_FLAG_TICK_PRECISE (1<<2) /**< Timer will have tick level time precision */
|
||||
#define TIMER_HNDL_CLOSE (1<<9) /**< Deprecated define, replaced by below */
|
||||
#define TIMER_DATA_HNDL_CLOSE (1<<9) /**< Timer will automatically call CloseHandle() on its data when finished */
|
||||
|
||||
|
||||
@@ -107,6 +107,7 @@ namespace SourceMod
|
||||
|
||||
#define TIMER_FLAG_REPEAT (1<<0) /**< Timer will repeat until stopped */
|
||||
#define TIMER_FLAG_NO_MAPCHANGE (1<<1) /**< Timer will not carry over mapchanges */
|
||||
#define TIMER_FLAG_TICK_PRECISE (1<<2) /**< Timer will have tick level time precision */
|
||||
|
||||
class ITimerSystem : public SMInterface
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user