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 SH_DECL_HOOK2_void(ICvar, CallGlobalChangeCallback, SH_NOATTRIB, false, ConVar *, const char *);
41 float g_fUniversalTime = 0.0f;
42 float g_fMapStartTime = 0.0f;
43 const float *g_pUniversalTime = &g_fUniversalTime;
44 ConVar *mp_timelimit = NULL;
46 ConVar sm_time_adjustment("sm_time_adjustment", "0", 0, "Adjusts the server time in seconds");
48 inline float GetSimulatedTime()
50 return g_fUniversalTime;
53 time_t GetAdjustedTime(time_t *buf)
55 time_t val = time(NULL) + sm_time_adjustment.GetInt();
63 class DefaultMapTimer :
75 return mp_timelimit->GetInt();
78 void SetMapTimerStatus(bool enabled)
80 if (enabled && !m_bInUse)
84 else if (!enabled && m_bInUse)
91 bool GetMapTimeLeft(int *time_left)
93 if (GetMapTimeLimit() == 0)
98 *time_left = (int)((g_fMapStartTime + mp_timelimit->GetInt() * 60.0f) - g_fUniversalTime);
103 void ExtendMapTimeLimit(int extra_time)
109 mp_timelimit->SetValue(0);
113 mp_timelimit->SetValue(mp_timelimit->GetInt() + extra_time);
116 void GlobalChangeCallback(ConVar *pVar, const char *old_value)
118 if (pVar != mp_timelimit)
123 if (atoi(old_value) == pVar->GetInt())
128 g_Timers.MapTimeLeftChanged();
134 SH_ADD_HOOK_MEMFUNC(ICvar, CallGlobalChangeCallback, icvar, this, &DefaultMapTimer::GlobalChangeCallback, false);
139 SH_REMOVE_HOOK_MEMFUNC(ICvar, CallGlobalChangeCallback, icvar, this, &DefaultMapTimer::GlobalChangeCallback, false);
146 void ITimer::Initialize(ITimedEvent *pCallbacks, float fInterval, float fToExec, void *pData, int flags)
148 m_Listener = pCallbacks;
149 m_Interval = fInterval;
157 TimerSystem::TimerSystem()
160 m_bHasMapTickedYet = false;
161 m_fLastTickedTime = 0.0f;
162 m_LastExecTime = 0.0f;
165 TimerSystem::~TimerSystem()
167 CStack<ITimer *>::iterator iter;
168 for (iter=m_FreeTimers.begin(); iter!=m_FreeTimers.end(); iter++)
172 m_FreeTimers.popall();
175 void TimerSystem::OnSourceModAllInitialized()
177 g_ShareSys.AddInterface(NULL, this);
178 m_pOnGameFrame = g_Forwards.CreateForward("OnGameFrame", ET_Ignore, 0, NULL);
181 void TimerSystem::OnSourceModGameInitialized()
183 mp_timelimit = icvar->FindVar("mp_timelimit");
185 if (m_pMapTimer == NULL && mp_timelimit != NULL)
187 SetMapTimer(&s_DefaultMapTimer);
191 void TimerSystem::OnSourceModShutdown()
194 g_Forwards.ReleaseForward(m_pOnGameFrame);
197 void TimerSystem::OnSourceModLevelChange(const char *mapName)
202 void TimerSystem::OnSourceModLevelEnd()
204 m_bHasMapTickedYet = false;
207 void TimerSystem::GameFrame(bool simulating)
209 if (simulating && m_bHasMapTickedYet)
211 g_fUniversalTime += gpGlobals->curtime - m_fLastTickedTime;
215 g_fUniversalTime += gpGlobals->interval_per_tick;
218 m_fLastTickedTime = gpGlobals->curtime;
219 if (!m_bHasMapTickedYet)
221 g_fMapStartTime = g_fUniversalTime;
222 m_bHasMapTickedYet = true;
225 if (g_fUniversalTime - m_LastExecTime >= 0.1)
232 if (m_pOnGameFrame->GetFunctionCount())
234 m_pOnGameFrame->Execute(NULL);
238 void TimerSystem::RunFrame()
243 float curtime = GetSimulatedTime();
244 for (iter=m_SingleTimers.begin(); iter!=m_SingleTimers.end(); )
247 if (curtime >= pTimer->m_ToExec)
249 pTimer->m_InExec = true;
250 pTimer->m_Listener->OnTimer(pTimer, pTimer->m_pData);
251 pTimer->m_Listener->OnTimerEnd(pTimer, pTimer->m_pData);
252 iter = m_SingleTimers.erase(iter);
253 m_FreeTimers.push(pTimer);
260 for (iter=m_LoopTimers.begin(); iter!=m_LoopTimers.end(); )
263 if (curtime >= pTimer->m_ToExec)
265 pTimer->m_InExec = true;
266 res = pTimer->m_Listener->OnTimer(pTimer, pTimer->m_pData);
267 if (pTimer->m_KillMe || (res == Pl_Stop))
269 pTimer->m_Listener->OnTimerEnd(pTimer, pTimer->m_pData);
270 iter = m_LoopTimers.erase(iter);
271 m_FreeTimers.push(pTimer);
274 pTimer->m_InExec = false;
275 pTimer->m_ToExec = curtime + pTimer->m_Interval;
280 m_LastExecTime = curtime;
283 ITimer *TimerSystem::CreateTimer(ITimedEvent *pCallbacks, float fInterval, void *pData, int flags)
287 float to_exec = GetSimulatedTime() + fInterval;
289 if (m_FreeTimers.empty())
293 pTimer = m_FreeTimers.front();
297 pTimer->Initialize(pCallbacks, fInterval, to_exec, pData, flags);
299 if (flags & TIMER_FLAG_REPEAT)
301 m_LoopTimers.push_back(pTimer);
305 if (m_SingleTimers.size() >= 1)
307 iter = --m_SingleTimers.end();
308 if ((*iter)->m_ToExec <= to_exec)
310 goto normal_insert_end;
314 for (iter=m_SingleTimers.begin(); iter!=m_SingleTimers.end(); iter++)
316 if ((*iter)->m_ToExec >= to_exec)
318 m_SingleTimers.insert(iter, pTimer);
324 m_SingleTimers.push_back(pTimer);
330 void TimerSystem::FireTimerOnce(ITimer *pTimer, bool delayExec)
334 if (pTimer->m_InExec)
339 pTimer->m_InExec = true;
340 res = pTimer->m_Listener->OnTimer(pTimer, pTimer->m_pData);
342 if (!(pTimer->m_Flags & TIMER_FLAG_REPEAT))
344 pTimer->m_Listener->OnTimerEnd(pTimer, pTimer->m_pData);
345 m_SingleTimers.remove(pTimer);
346 m_FreeTimers.push(pTimer);
348 if ((res != Pl_Stop) && !pTimer->m_KillMe)
352 pTimer->m_ToExec = GetSimulatedTime() + pTimer->m_Interval;
354 pTimer->m_InExec = false;
357 pTimer->m_Listener->OnTimerEnd(pTimer, pTimer->m_pData);
358 m_LoopTimers.remove(pTimer);
359 m_FreeTimers.push(pTimer);
363 void TimerSystem::KillTimer(ITimer *pTimer)
365 if (pTimer->m_KillMe)
370 if (pTimer->m_InExec)
372 pTimer->m_KillMe = true;
376 pTimer->m_InExec = true; /* The timer it's not really executed but this check needs to be done */
377 pTimer->m_Listener->OnTimerEnd(pTimer, pTimer->m_pData);
379 if (pTimer->m_Flags & TIMER_FLAG_REPEAT)
381 m_LoopTimers.remove(pTimer);
383 m_SingleTimers.remove(pTimer);
386 m_FreeTimers.push(pTimer);
389 CStack<ITimer *> s_tokill;
390 void TimerSystem::MapChange(bool real_mapchange)
395 for (iter=m_SingleTimers.begin(); iter!=m_SingleTimers.end(); iter++)
398 if (real_mapchange && (pTimer->m_Flags & TIMER_FLAG_NO_MAPCHANGE))
400 s_tokill.push(pTimer);
404 for (iter=m_LoopTimers.begin(); iter!=m_LoopTimers.end(); iter++)
407 if (real_mapchange && (pTimer->m_Flags & TIMER_FLAG_NO_MAPCHANGE))
409 s_tokill.push(pTimer);
413 while (!s_tokill.empty())
415 KillTimer(s_tokill.front());
420 IMapTimer *TimerSystem::SetMapTimer(IMapTimer *pTimer)
422 IMapTimer *old = m_pMapTimer;
424 m_pMapTimer = pTimer;
428 m_pMapTimer->SetMapTimerStatus(true);
433 old->SetMapTimerStatus(false);
439 IMapTimer *TimerSystem::GetMapTimer()
444 void TimerSystem::MapTimeLeftChanged()
449 float TimerSystem::GetTickedTime()
451 return g_fUniversalTime;