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"
38 #define TIMER_MIN_ACCURACY 0.1
40 #if !defined ORANGEBOX_BUILD
41 #define CallGlobalChangeCallbacks CallGlobalChangeCallback
44 #if defined ORANGEBOX_BUILD
45 SH_DECL_HOOK3_void(ICvar, CallGlobalChangeCallbacks, SH_NOATTRIB, false, ConVar *, const char *, float);
47 SH_DECL_HOOK2_void(ICvar, CallGlobalChangeCallbacks, SH_NOATTRIB, false, ConVar *, const char *);
51 double g_fUniversalTime = 0.0f;
52 float g_fGameStartTime = 0.0f; /* Game game start time, non-universal */
53 double g_fTimerThink = 0.0f; /* Timer's next think time */
54 const double *g_pUniversalTime = &g_fUniversalTime;
55 ConVar *mp_timelimit = NULL;
56 int g_TimeLeftMode = 0;
58 ConVar sm_time_adjustment("sm_time_adjustment", "0", 0, "Adjusts the server time in seconds");
60 inline double GetSimulatedTime()
62 return g_fUniversalTime;
65 time_t GetAdjustedTime(time_t *buf)
67 time_t val = time(NULL) + sm_time_adjustment.GetInt();
75 class DefaultMapTimer :
85 void OnSourceModLevelChange(const char *mapName)
87 g_fGameStartTime = 0.0f;
92 return mp_timelimit->GetInt();
95 void SetMapTimerStatus(bool enabled)
97 if (enabled && !m_bInUse)
101 else if (!enabled && m_bInUse)
108 void ExtendMapTimeLimit(int extra_time)
112 mp_timelimit->SetValue(0);
118 mp_timelimit->SetValue(mp_timelimit->GetInt() + extra_time);
121 #if defined ORANGEBOX_BUILD
122 void GlobalChangeCallback(ConVar *pVar, const char *old_value, float flOldValue)
124 void GlobalChangeCallback(ConVar *pVar, const char *old_value)
127 if (pVar != mp_timelimit)
132 if (atoi(old_value) == pVar->GetInt())
137 g_Timers.MapTimeLeftChanged();
143 SH_ADD_HOOK_MEMFUNC(ICvar, CallGlobalChangeCallbacks, icvar, this, &DefaultMapTimer::GlobalChangeCallback, false);
148 SH_REMOVE_HOOK_MEMFUNC(ICvar, CallGlobalChangeCallbacks, icvar, this, &DefaultMapTimer::GlobalChangeCallback, false);
156 * If the ticking process has run amok (should be impossible), we
157 * take care of this by "skipping" the in-between time, to prevent
158 * a bazillion times from firing on accident. This has the result
159 * that a drastic jump in time will continue acting normally. Users
160 * may not expect this, but... I think it is the best solution.
162 inline double CalcNextThink(double last, float interval)
164 if (g_fUniversalTime - last - interval <= TIMER_MIN_ACCURACY)
166 return last + interval;
170 return g_fUniversalTime + interval;
174 void ITimer::Initialize(ITimedEvent *pCallbacks, float fInterval, float fToExec, void *pData, int flags)
176 m_Listener = pCallbacks;
177 m_Interval = fInterval;
185 TimerSystem::TimerSystem()
188 m_bHasMapTickedYet = false;
189 m_bHasMapSimulatedYet = false;
190 m_fLastTickedTime = 0.0f;
193 TimerSystem::~TimerSystem()
195 CStack<ITimer *>::iterator iter;
196 for (iter=m_FreeTimers.begin(); iter!=m_FreeTimers.end(); iter++)
200 m_FreeTimers.popall();
203 void TimerSystem::OnSourceModAllInitialized()
205 g_ShareSys.AddInterface(NULL, this);
206 m_pOnGameFrame = g_Forwards.CreateForward("OnGameFrame", ET_Ignore, 0, NULL);
207 m_pOnMapTimeLeftChanged = g_Forwards.CreateForward("OnMapTimeLeftChanged", ET_Ignore, 0, NULL);
210 void TimerSystem::OnSourceModGameInitialized()
212 mp_timelimit = icvar->FindVar("mp_timelimit");
214 if (m_pMapTimer == NULL && mp_timelimit != NULL)
216 SetMapTimer(&s_DefaultMapTimer);
220 void TimerSystem::OnSourceModShutdown()
223 g_Forwards.ReleaseForward(m_pOnGameFrame);
224 g_Forwards.ReleaseForward(m_pOnMapTimeLeftChanged);
227 void TimerSystem::OnSourceModLevelEnd()
229 m_bHasMapTickedYet = false;
230 m_bHasMapSimulatedYet = false;
233 void TimerSystem::GameFrame(bool simulating)
235 if (simulating && m_bHasMapTickedYet)
237 g_fUniversalTime += gpGlobals->curtime - m_fLastTickedTime;
238 if (!m_bHasMapSimulatedYet)
240 m_bHasMapSimulatedYet = true;
241 MapTimeLeftChanged();
246 g_fUniversalTime += gpGlobals->interval_per_tick;
249 m_fLastTickedTime = gpGlobals->curtime;
250 m_bHasMapTickedYet = true;
252 if (g_fUniversalTime >= g_fTimerThink)
256 g_fTimerThink = CalcNextThink(g_fTimerThink, TIMER_MIN_ACCURACY);
261 if (m_pOnGameFrame->GetFunctionCount())
263 m_pOnGameFrame->Execute(NULL);
267 void TimerSystem::RunFrame()
272 double curtime = GetSimulatedTime();
273 for (iter=m_SingleTimers.begin(); iter!=m_SingleTimers.end(); )
276 if (curtime >= pTimer->m_ToExec)
278 pTimer->m_InExec = true;
279 pTimer->m_Listener->OnTimer(pTimer, pTimer->m_pData);
280 pTimer->m_Listener->OnTimerEnd(pTimer, pTimer->m_pData);
281 iter = m_SingleTimers.erase(iter);
282 m_FreeTimers.push(pTimer);
291 for (iter=m_LoopTimers.begin(); iter!=m_LoopTimers.end(); )
294 if (curtime >= pTimer->m_ToExec)
296 pTimer->m_InExec = true;
297 res = pTimer->m_Listener->OnTimer(pTimer, pTimer->m_pData);
298 if (pTimer->m_KillMe || (res == Pl_Stop))
300 pTimer->m_Listener->OnTimerEnd(pTimer, pTimer->m_pData);
301 iter = m_LoopTimers.erase(iter);
302 m_FreeTimers.push(pTimer);
305 pTimer->m_InExec = false;
306 pTimer->m_ToExec = CalcNextThink(pTimer->m_ToExec, pTimer->m_Interval);
312 ITimer *TimerSystem::CreateTimer(ITimedEvent *pCallbacks, float fInterval, void *pData, int flags)
316 float to_exec = GetSimulatedTime() + fInterval;
318 if (m_FreeTimers.empty())
322 pTimer = m_FreeTimers.front();
326 pTimer->Initialize(pCallbacks, fInterval, to_exec, pData, flags);
328 if (flags & TIMER_FLAG_REPEAT)
330 m_LoopTimers.push_back(pTimer);
334 if (m_SingleTimers.size() >= 1)
336 iter = --m_SingleTimers.end();
337 if ((*iter)->m_ToExec <= to_exec)
339 goto normal_insert_end;
343 for (iter=m_SingleTimers.begin(); iter!=m_SingleTimers.end(); iter++)
345 if ((*iter)->m_ToExec >= to_exec)
347 m_SingleTimers.insert(iter, pTimer);
353 m_SingleTimers.push_back(pTimer);
359 void TimerSystem::FireTimerOnce(ITimer *pTimer, bool delayExec)
363 if (pTimer->m_InExec)
368 pTimer->m_InExec = true;
369 res = pTimer->m_Listener->OnTimer(pTimer, pTimer->m_pData);
371 if (!(pTimer->m_Flags & TIMER_FLAG_REPEAT))
373 pTimer->m_Listener->OnTimerEnd(pTimer, pTimer->m_pData);
374 m_SingleTimers.remove(pTimer);
375 m_FreeTimers.push(pTimer);
379 if ((res != Pl_Stop) && !pTimer->m_KillMe)
383 pTimer->m_ToExec = GetSimulatedTime() + pTimer->m_Interval;
385 pTimer->m_InExec = false;
388 pTimer->m_Listener->OnTimerEnd(pTimer, pTimer->m_pData);
389 m_LoopTimers.remove(pTimer);
390 m_FreeTimers.push(pTimer);
394 void TimerSystem::KillTimer(ITimer *pTimer)
396 if (pTimer->m_KillMe)
401 if (pTimer->m_InExec)
403 pTimer->m_KillMe = true;
407 pTimer->m_InExec = true; /* The timer it's not really executed but this check needs to be done */
408 pTimer->m_Listener->OnTimerEnd(pTimer, pTimer->m_pData);
410 if (pTimer->m_Flags & TIMER_FLAG_REPEAT)
412 m_LoopTimers.remove(pTimer);
414 m_SingleTimers.remove(pTimer);
417 m_FreeTimers.push(pTimer);
420 CStack<ITimer *> s_tokill;
421 void TimerSystem::RemoveMapChangeTimers()
426 for (iter=m_SingleTimers.begin(); iter!=m_SingleTimers.end(); iter++)
429 if (pTimer->m_Flags & TIMER_FLAG_NO_MAPCHANGE)
431 s_tokill.push(pTimer);
435 for (iter=m_LoopTimers.begin(); iter!=m_LoopTimers.end(); iter++)
438 if (pTimer->m_Flags & TIMER_FLAG_NO_MAPCHANGE)
440 s_tokill.push(pTimer);
444 while (!s_tokill.empty())
446 KillTimer(s_tokill.front());
451 IMapTimer *TimerSystem::SetMapTimer(IMapTimer *pTimer)
453 IMapTimer *old = m_pMapTimer;
455 m_pMapTimer = pTimer;
459 m_pMapTimer->SetMapTimerStatus(true);
464 old->SetMapTimerStatus(false);
470 IMapTimer *TimerSystem::GetMapTimer()
475 void TimerSystem::MapTimeLeftChanged()
477 m_pOnMapTimeLeftChanged->Execute(NULL);
480 void TimerSystem::NotifyOfGameStart(float offset)
482 g_fGameStartTime = gpGlobals->curtime + offset;
485 float TimerSystem::GetTickedTime()
487 return g_fUniversalTime;
490 bool TimerSystem::GetMapTimeLeft(float *time_left)
498 if (!m_bHasMapSimulatedYet || (time_limit = m_pMapTimer->GetMapTimeLimit()) < 1)
504 *time_left = (g_fGameStartTime + time_limit * 60.0f) - gpGlobals->curtime;