3 * =============================================================================
5 * Copyright (C) 2004-2007 AlliedModders LLC. All rights reserved.
6 * =============================================================================
8 * This program is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License, version 3.0, as published by the
10 * Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
17 * You should have received a copy of the GNU General Public License along with
18 * this program. If not, see <http://www.gnu.org/licenses/>.
20 * As a special exception, AlliedModders LLC gives you permission to link the
21 * code of this program (as well as its derivative works) to "Half-Life 2," the
22 * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
23 * by the Valve Corporation. You must obey the GNU General Public License in
24 * all respects for all other code used. Additionally, AlliedModders LLC grants
25 * this exception to all derivative works. AlliedModders LLC defines further
26 * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
27 * or <http://www.sourcemod.net/license.php>.
34 #include "ForwardSys.h"
35 #include "sourcemm_api.h"
36 #include "frame_hooks.h"
39 float g_fUniversalTime = 0.0f;
40 const float *g_pUniversalTime = &g_fUniversalTime;
42 ConVar sm_time_adjustment("sm_time_adjustment", "0", 0, "Adjusts the server time in seconds");
44 inline float GetSimulatedTime()
46 return g_fUniversalTime;
49 time_t GetAdjustedTime(time_t *buf)
51 time_t val = time(NULL) + sm_time_adjustment.GetInt();
59 void ITimer::Initialize(ITimedEvent *pCallbacks, float fInterval, float fToExec, void *pData, int flags)
61 m_Listener = pCallbacks;
62 m_Interval = fInterval;
70 TimerSystem::TimerSystem()
73 m_bHasMapTickedYet = false;
74 m_fLastTickedTime = 0.0f;
75 m_LastExecTime = 0.0f;
78 TimerSystem::~TimerSystem()
80 CStack<ITimer *>::iterator iter;
81 for (iter=m_FreeTimers.begin(); iter!=m_FreeTimers.end(); iter++)
85 m_FreeTimers.popall();
88 void TimerSystem::OnSourceModAllInitialized()
90 g_ShareSys.AddInterface(NULL, this);
91 m_pOnGameFrame = g_Forwards.CreateForward("OnGameFrame", ET_Ignore, 0, NULL);
94 void TimerSystem::OnSourceModShutdown()
96 g_Forwards.ReleaseForward(m_pOnGameFrame);
99 void TimerSystem::OnSourceModLevelChange(const char *mapName)
104 void TimerSystem::OnSourceModLevelEnd()
106 m_bHasMapTickedYet = false;
109 void TimerSystem::GameFrame(bool simulating)
111 if (simulating && m_bHasMapTickedYet)
113 g_fUniversalTime += gpGlobals->curtime - m_fLastTickedTime;
117 g_fUniversalTime += gpGlobals->interval_per_tick;
120 m_fLastTickedTime = gpGlobals->curtime;
121 m_bHasMapTickedYet = true;
123 if (g_fUniversalTime - m_LastExecTime >= 0.1)
130 if (m_pOnGameFrame->GetFunctionCount())
132 m_pOnGameFrame->Execute(NULL);
136 void TimerSystem::RunFrame()
141 float curtime = GetSimulatedTime();
142 for (iter=m_SingleTimers.begin(); iter!=m_SingleTimers.end(); )
145 if (curtime >= pTimer->m_ToExec)
147 pTimer->m_InExec = true;
148 pTimer->m_Listener->OnTimer(pTimer, pTimer->m_pData);
149 pTimer->m_Listener->OnTimerEnd(pTimer, pTimer->m_pData);
150 iter = m_SingleTimers.erase(iter);
151 m_FreeTimers.push(pTimer);
157 if (m_fnTimeLeft != NULL && m_MapEndTimers.size())
159 float time_left = m_fnTimeLeft();
160 for (iter=m_MapEndTimers.begin(); iter!=m_MapEndTimers.end();)
163 if ((*iter)->m_Interval < time_left)
165 pTimer->m_InExec = true;
166 pTimer->m_Listener->OnTimer(pTimer, pTimer->m_pData);
167 pTimer->m_Listener->OnTimerEnd(pTimer, pTimer->m_pData);
168 iter = m_MapEndTimers.erase(iter);
169 m_FreeTimers.push(pTimer);
177 for (iter=m_LoopTimers.begin(); iter!=m_LoopTimers.end(); )
180 if (curtime >= pTimer->m_ToExec)
182 pTimer->m_InExec = true;
183 res = pTimer->m_Listener->OnTimer(pTimer, pTimer->m_pData);
184 if (pTimer->m_KillMe || (res == Pl_Stop))
186 pTimer->m_Listener->OnTimerEnd(pTimer, pTimer->m_pData);
187 iter = m_LoopTimers.erase(iter);
188 m_FreeTimers.push(pTimer);
191 pTimer->m_InExec = false;
192 pTimer->m_ToExec = curtime + pTimer->m_Interval;
197 m_LastExecTime = curtime;
200 ITimer *TimerSystem::CreateTimer(ITimedEvent *pCallbacks, float fInterval, void *pData, int flags)
204 float to_exec = GetSimulatedTime() + fInterval;
206 if ((flags & TIMER_FLAG_BEFORE_MAP_END)
207 && m_fnTimeLeft == NULL)
212 if (m_FreeTimers.empty())
216 pTimer = m_FreeTimers.front();
220 pTimer->Initialize(pCallbacks, fInterval, to_exec, pData, flags);
222 if (flags & TIMER_FLAG_REPEAT)
224 m_LoopTimers.push_back(pTimer);
228 if (flags & TIMER_FLAG_BEFORE_MAP_END)
230 for (iter=m_MapEndTimers.begin(); iter!=m_MapEndTimers.end(); iter++)
232 if (fInterval >= (*iter)->m_Interval)
234 m_MapEndTimers.insert(iter, pTimer);
239 m_MapEndTimers.push_back(pTimer);
241 if (m_SingleTimers.size() >= 1)
243 iter = --m_SingleTimers.end();
244 if ((*iter)->m_ToExec <= to_exec)
246 goto normal_insert_end;
250 for (iter=m_SingleTimers.begin(); iter!=m_SingleTimers.end(); iter++)
252 if ((*iter)->m_ToExec >= to_exec)
254 m_SingleTimers.insert(iter, pTimer);
260 m_SingleTimers.push_back(pTimer);
267 void TimerSystem::FireTimerOnce(ITimer *pTimer, bool delayExec)
271 if (pTimer->m_InExec)
276 pTimer->m_InExec = true;
277 res = pTimer->m_Listener->OnTimer(pTimer, pTimer->m_pData);
279 if (!(pTimer->m_Flags & TIMER_FLAG_REPEAT))
281 pTimer->m_Listener->OnTimerEnd(pTimer, pTimer->m_pData);
282 if (pTimer->m_Flags & TIMER_FLAG_BEFORE_MAP_END)
284 m_MapEndTimers.remove(pTimer);
286 m_SingleTimers.remove(pTimer);
288 m_FreeTimers.push(pTimer);
290 if ((res != Pl_Stop) && !pTimer->m_KillMe)
294 pTimer->m_ToExec = GetSimulatedTime() + pTimer->m_Interval;
296 pTimer->m_InExec = false;
299 pTimer->m_Listener->OnTimerEnd(pTimer, pTimer->m_pData);
300 m_LoopTimers.remove(pTimer);
301 m_FreeTimers.push(pTimer);
305 void TimerSystem::KillTimer(ITimer *pTimer)
307 if (pTimer->m_KillMe)
312 if (pTimer->m_InExec)
314 pTimer->m_KillMe = true;
318 pTimer->m_InExec = true; /* The timer it's not really executed but this check needs to be done */
319 pTimer->m_Listener->OnTimerEnd(pTimer, pTimer->m_pData);
321 if (pTimer->m_Flags & TIMER_FLAG_REPEAT)
323 m_LoopTimers.remove(pTimer);
324 } else if (pTimer->m_Flags & TIMER_FLAG_BEFORE_MAP_END) {
325 m_MapEndTimers.remove(pTimer);
327 m_SingleTimers.remove(pTimer);
330 m_FreeTimers.push(pTimer);
333 CStack<ITimer *> s_tokill;
334 void TimerSystem::MapChange(bool real_mapchange)
339 for (iter=m_SingleTimers.begin(); iter!=m_SingleTimers.end(); iter++)
342 if (real_mapchange && (pTimer->m_Flags & TIMER_FLAG_NO_MAPCHANGE))
344 s_tokill.push(pTimer);
348 for (iter=m_LoopTimers.begin(); iter!=m_LoopTimers.end(); iter++)
351 if (real_mapchange && (pTimer->m_Flags & TIMER_FLAG_NO_MAPCHANGE))
353 s_tokill.push(pTimer);
359 for (iter=m_MapEndTimers.begin(); iter!=m_MapEndTimers.end(); iter++)
362 s_tokill.push(pTimer);
366 while (!s_tokill.empty())
368 KillTimer(s_tokill.front());
373 SM_TIMELEFT_FUNCTION TimerSystem::SetTimeLeftFunction(SM_TIMELEFT_FUNCTION fn)
375 SM_TIMELEFT_FUNCTION old = m_fnTimeLeft;