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 #if !defined ORANGEBOX_BUILD
39 #define CallGlobalChangeCallbacks CallGlobalChangeCallback
42 #if defined ORANGEBOX_BUILD
43 SH_DECL_HOOK3_void(ICvar, CallGlobalChangeCallbacks, SH_NOATTRIB, false, ConVar *, const char *, float);
45 SH_DECL_HOOK2_void(ICvar, CallGlobalChangeCallbacks, SH_NOATTRIB, false, ConVar *, const char *);
49 float g_fUniversalTime = 0.0f;
50 float g_fGameStartTime = 0.0f; /* Game game start time, non-universal */
51 const float *g_pUniversalTime = &g_fUniversalTime;
52 ConVar *mp_timelimit = NULL;
53 int g_TimeLeftMode = 0;
55 ConVar sm_time_adjustment("sm_time_adjustment", "0", 0, "Adjusts the server time in seconds");
57 inline float GetSimulatedTime()
59 return g_fUniversalTime;
62 time_t GetAdjustedTime(time_t *buf)
64 time_t val = time(NULL) + sm_time_adjustment.GetInt();
72 class DefaultMapTimer :
82 void OnSourceModLevelChange(const char *mapName)
84 g_fGameStartTime = 0.0f;
89 return mp_timelimit->GetInt();
92 void SetMapTimerStatus(bool enabled)
94 if (enabled && !m_bInUse)
98 else if (!enabled && m_bInUse)
105 void ExtendMapTimeLimit(int extra_time)
109 mp_timelimit->SetValue(0);
115 mp_timelimit->SetValue(mp_timelimit->GetInt() + extra_time);
118 #if defined ORANGEBOX_BUILD
119 void GlobalChangeCallback(ConVar *pVar, const char *old_value, float flOldValue)
121 void GlobalChangeCallback(ConVar *pVar, const char *old_value)
124 if (pVar != mp_timelimit)
129 if (atoi(old_value) == pVar->GetInt())
134 g_Timers.MapTimeLeftChanged();
140 SH_ADD_HOOK_MEMFUNC(ICvar, CallGlobalChangeCallbacks, icvar, this, &DefaultMapTimer::GlobalChangeCallback, false);
145 SH_REMOVE_HOOK_MEMFUNC(ICvar, CallGlobalChangeCallbacks, icvar, this, &DefaultMapTimer::GlobalChangeCallback, false);
152 void ITimer::Initialize(ITimedEvent *pCallbacks, float fInterval, float fToExec, void *pData, int flags)
154 m_Listener = pCallbacks;
155 m_Interval = fInterval;
163 TimerSystem::TimerSystem()
166 m_bHasMapTickedYet = false;
167 m_bHasMapSimulatedYet = false;
168 m_fLastTickedTime = 0.0f;
169 m_LastExecTime = 0.0f;
172 TimerSystem::~TimerSystem()
174 CStack<ITimer *>::iterator iter;
175 for (iter=m_FreeTimers.begin(); iter!=m_FreeTimers.end(); iter++)
179 m_FreeTimers.popall();
182 void TimerSystem::OnSourceModAllInitialized()
184 g_ShareSys.AddInterface(NULL, this);
185 m_pOnGameFrame = g_Forwards.CreateForward("OnGameFrame", ET_Ignore, 0, NULL);
186 m_pOnMapTimeLeftChanged = g_Forwards.CreateForward("OnMapTimeLeftChanged", ET_Ignore, 0, NULL);
189 void TimerSystem::OnSourceModGameInitialized()
191 mp_timelimit = icvar->FindVar("mp_timelimit");
193 if (m_pMapTimer == NULL && mp_timelimit != NULL)
195 SetMapTimer(&s_DefaultMapTimer);
199 void TimerSystem::OnSourceModShutdown()
202 g_Forwards.ReleaseForward(m_pOnGameFrame);
203 g_Forwards.ReleaseForward(m_pOnMapTimeLeftChanged);
206 void TimerSystem::OnSourceModLevelChange(const char *mapName)
211 void TimerSystem::OnSourceModLevelEnd()
213 m_bHasMapTickedYet = false;
214 m_bHasMapSimulatedYet = false;
217 void TimerSystem::GameFrame(bool simulating)
219 if (simulating && m_bHasMapTickedYet)
221 g_fUniversalTime += gpGlobals->curtime - m_fLastTickedTime;
222 if (!m_bHasMapSimulatedYet)
224 m_bHasMapSimulatedYet = true;
225 MapTimeLeftChanged();
230 g_fUniversalTime += gpGlobals->interval_per_tick;
233 m_fLastTickedTime = gpGlobals->curtime;
234 m_bHasMapTickedYet = true;
236 if (g_fUniversalTime - m_LastExecTime >= 0.1)
243 if (m_pOnGameFrame->GetFunctionCount())
245 m_pOnGameFrame->Execute(NULL);
249 void TimerSystem::RunFrame()
254 float curtime = GetSimulatedTime();
255 for (iter=m_SingleTimers.begin(); iter!=m_SingleTimers.end(); )
258 if (curtime >= pTimer->m_ToExec)
260 pTimer->m_InExec = true;
261 pTimer->m_Listener->OnTimer(pTimer, pTimer->m_pData);
262 pTimer->m_Listener->OnTimerEnd(pTimer, pTimer->m_pData);
263 iter = m_SingleTimers.erase(iter);
264 m_FreeTimers.push(pTimer);
271 for (iter=m_LoopTimers.begin(); iter!=m_LoopTimers.end(); )
274 if (curtime >= pTimer->m_ToExec)
276 pTimer->m_InExec = true;
277 res = pTimer->m_Listener->OnTimer(pTimer, pTimer->m_pData);
278 if (pTimer->m_KillMe || (res == Pl_Stop))
280 pTimer->m_Listener->OnTimerEnd(pTimer, pTimer->m_pData);
281 iter = m_LoopTimers.erase(iter);
282 m_FreeTimers.push(pTimer);
285 pTimer->m_InExec = false;
286 pTimer->m_ToExec = curtime + pTimer->m_Interval;
291 m_LastExecTime = curtime;
294 ITimer *TimerSystem::CreateTimer(ITimedEvent *pCallbacks, float fInterval, void *pData, int flags)
298 float to_exec = GetSimulatedTime() + fInterval;
300 if (m_FreeTimers.empty())
304 pTimer = m_FreeTimers.front();
308 pTimer->Initialize(pCallbacks, fInterval, to_exec, pData, flags);
310 if (flags & TIMER_FLAG_REPEAT)
312 m_LoopTimers.push_back(pTimer);
316 if (m_SingleTimers.size() >= 1)
318 iter = --m_SingleTimers.end();
319 if ((*iter)->m_ToExec <= to_exec)
321 goto normal_insert_end;
325 for (iter=m_SingleTimers.begin(); iter!=m_SingleTimers.end(); iter++)
327 if ((*iter)->m_ToExec >= to_exec)
329 m_SingleTimers.insert(iter, pTimer);
335 m_SingleTimers.push_back(pTimer);
341 void TimerSystem::FireTimerOnce(ITimer *pTimer, bool delayExec)
345 if (pTimer->m_InExec)
350 pTimer->m_InExec = true;
351 res = pTimer->m_Listener->OnTimer(pTimer, pTimer->m_pData);
353 if (!(pTimer->m_Flags & TIMER_FLAG_REPEAT))
355 pTimer->m_Listener->OnTimerEnd(pTimer, pTimer->m_pData);
356 m_SingleTimers.remove(pTimer);
357 m_FreeTimers.push(pTimer);
359 if ((res != Pl_Stop) && !pTimer->m_KillMe)
363 pTimer->m_ToExec = GetSimulatedTime() + pTimer->m_Interval;
365 pTimer->m_InExec = false;
368 pTimer->m_Listener->OnTimerEnd(pTimer, pTimer->m_pData);
369 m_LoopTimers.remove(pTimer);
370 m_FreeTimers.push(pTimer);
374 void TimerSystem::KillTimer(ITimer *pTimer)
376 if (pTimer->m_KillMe)
381 if (pTimer->m_InExec)
383 pTimer->m_KillMe = true;
387 pTimer->m_InExec = true; /* The timer it's not really executed but this check needs to be done */
388 pTimer->m_Listener->OnTimerEnd(pTimer, pTimer->m_pData);
390 if (pTimer->m_Flags & TIMER_FLAG_REPEAT)
392 m_LoopTimers.remove(pTimer);
394 m_SingleTimers.remove(pTimer);
397 m_FreeTimers.push(pTimer);
400 CStack<ITimer *> s_tokill;
401 void TimerSystem::MapChange(bool real_mapchange)
406 for (iter=m_SingleTimers.begin(); iter!=m_SingleTimers.end(); iter++)
409 if (real_mapchange && (pTimer->m_Flags & TIMER_FLAG_NO_MAPCHANGE))
411 s_tokill.push(pTimer);
415 for (iter=m_LoopTimers.begin(); iter!=m_LoopTimers.end(); iter++)
418 if (real_mapchange && (pTimer->m_Flags & TIMER_FLAG_NO_MAPCHANGE))
420 s_tokill.push(pTimer);
424 while (!s_tokill.empty())
426 KillTimer(s_tokill.front());
431 IMapTimer *TimerSystem::SetMapTimer(IMapTimer *pTimer)
433 IMapTimer *old = m_pMapTimer;
435 m_pMapTimer = pTimer;
439 m_pMapTimer->SetMapTimerStatus(true);
444 old->SetMapTimerStatus(false);
450 IMapTimer *TimerSystem::GetMapTimer()
455 void TimerSystem::MapTimeLeftChanged()
457 m_pOnMapTimeLeftChanged->Execute(NULL);
460 void TimerSystem::NotifyOfGameStart(float offset)
462 g_fGameStartTime = gpGlobals->curtime + offset;
465 float TimerSystem::GetTickedTime()
467 return g_fUniversalTime;
470 bool TimerSystem::GetMapTimeLeft(float *time_left)
478 if (!m_bHasMapSimulatedYet || (time_limit = m_pMapTimer->GetMapTimeLimit()) < 1)
484 *time_left = (g_fGameStartTime + time_limit * 60.0f) - gpGlobals->curtime;