overhaul of the map timer idea -- function is now an interface, and the timer flag is removed
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 g_Timers.MapTimeLimitExtended(atoi(old_value) * 60, pVar->GetInt() * 60);
129 SH_ADD_HOOK_MEMFUNC(ICvar, CallGlobalChangeCallback, icvar, this, &DefaultMapTimer::GlobalChangeCallback, false);
134 SH_REMOVE_HOOK_MEMFUNC(ICvar, CallGlobalChangeCallback, icvar, this, &DefaultMapTimer::GlobalChangeCallback, false);
141 void ITimer::Initialize(ITimedEvent *pCallbacks, float fInterval, float fToExec, void *pData, int flags)
143 m_Listener = pCallbacks;
144 m_Interval = fInterval;
152 TimerSystem::TimerSystem()
155 m_bHasMapTickedYet = false;
156 m_fLastTickedTime = 0.0f;
157 m_LastExecTime = 0.0f;
160 TimerSystem::~TimerSystem()
162 CStack<ITimer *>::iterator iter;
163 for (iter=m_FreeTimers.begin(); iter!=m_FreeTimers.end(); iter++)
167 m_FreeTimers.popall();
170 void TimerSystem::OnSourceModAllInitialized()
172 g_ShareSys.AddInterface(NULL, this);
173 m_pOnGameFrame = g_Forwards.CreateForward("OnGameFrame", ET_Ignore, 0, NULL);
176 void TimerSystem::OnSourceModGameInitialized()
178 mp_timelimit = icvar->FindVar("mp_timelimit");
180 if (m_pMapTimer == NULL && mp_timelimit != NULL)
182 SetMapTimer(&s_DefaultMapTimer);
186 void TimerSystem::OnSourceModShutdown()
189 g_Forwards.ReleaseForward(m_pOnGameFrame);
192 void TimerSystem::OnSourceModLevelChange(const char *mapName)
197 void TimerSystem::OnSourceModLevelEnd()
199 m_bHasMapTickedYet = false;
202 void TimerSystem::GameFrame(bool simulating)
204 if (simulating && m_bHasMapTickedYet)
206 g_fUniversalTime += gpGlobals->curtime - m_fLastTickedTime;
210 g_fUniversalTime += gpGlobals->interval_per_tick;
213 m_fLastTickedTime = gpGlobals->curtime;
214 if (!m_bHasMapTickedYet)
216 g_fMapStartTime = g_fUniversalTime;
217 m_bHasMapTickedYet = true;
220 if (g_fUniversalTime - m_LastExecTime >= 0.1)
227 if (m_pOnGameFrame->GetFunctionCount())
229 m_pOnGameFrame->Execute(NULL);
233 void TimerSystem::RunFrame()
238 float curtime = GetSimulatedTime();
239 for (iter=m_SingleTimers.begin(); iter!=m_SingleTimers.end(); )
242 if (curtime >= pTimer->m_ToExec)
244 pTimer->m_InExec = true;
245 pTimer->m_Listener->OnTimer(pTimer, pTimer->m_pData);
246 pTimer->m_Listener->OnTimerEnd(pTimer, pTimer->m_pData);
247 iter = m_SingleTimers.erase(iter);
248 m_FreeTimers.push(pTimer);
255 for (iter=m_LoopTimers.begin(); iter!=m_LoopTimers.end(); )
258 if (curtime >= pTimer->m_ToExec)
260 pTimer->m_InExec = true;
261 res = pTimer->m_Listener->OnTimer(pTimer, pTimer->m_pData);
262 if (pTimer->m_KillMe || (res == Pl_Stop))
264 pTimer->m_Listener->OnTimerEnd(pTimer, pTimer->m_pData);
265 iter = m_LoopTimers.erase(iter);
266 m_FreeTimers.push(pTimer);
269 pTimer->m_InExec = false;
270 pTimer->m_ToExec = curtime + pTimer->m_Interval;
275 m_LastExecTime = curtime;
278 ITimer *TimerSystem::CreateTimer(ITimedEvent *pCallbacks, float fInterval, void *pData, int flags)
282 float to_exec = GetSimulatedTime() + fInterval;
284 if (m_FreeTimers.empty())
288 pTimer = m_FreeTimers.front();
292 pTimer->Initialize(pCallbacks, fInterval, to_exec, pData, flags);
294 if (flags & TIMER_FLAG_REPEAT)
296 m_LoopTimers.push_back(pTimer);
300 if (m_SingleTimers.size() >= 1)
302 iter = --m_SingleTimers.end();
303 if ((*iter)->m_ToExec <= to_exec)
305 goto normal_insert_end;
309 for (iter=m_SingleTimers.begin(); iter!=m_SingleTimers.end(); iter++)
311 if ((*iter)->m_ToExec >= to_exec)
313 m_SingleTimers.insert(iter, pTimer);
319 m_SingleTimers.push_back(pTimer);
325 void TimerSystem::FireTimerOnce(ITimer *pTimer, bool delayExec)
329 if (pTimer->m_InExec)
334 pTimer->m_InExec = true;
335 res = pTimer->m_Listener->OnTimer(pTimer, pTimer->m_pData);
337 if (!(pTimer->m_Flags & TIMER_FLAG_REPEAT))
339 pTimer->m_Listener->OnTimerEnd(pTimer, pTimer->m_pData);
340 m_SingleTimers.remove(pTimer);
341 m_FreeTimers.push(pTimer);
343 if ((res != Pl_Stop) && !pTimer->m_KillMe)
347 pTimer->m_ToExec = GetSimulatedTime() + pTimer->m_Interval;
349 pTimer->m_InExec = false;
352 pTimer->m_Listener->OnTimerEnd(pTimer, pTimer->m_pData);
353 m_LoopTimers.remove(pTimer);
354 m_FreeTimers.push(pTimer);
358 void TimerSystem::KillTimer(ITimer *pTimer)
360 if (pTimer->m_KillMe)
365 if (pTimer->m_InExec)
367 pTimer->m_KillMe = true;
371 pTimer->m_InExec = true; /* The timer it's not really executed but this check needs to be done */
372 pTimer->m_Listener->OnTimerEnd(pTimer, pTimer->m_pData);
374 if (pTimer->m_Flags & TIMER_FLAG_REPEAT)
376 m_LoopTimers.remove(pTimer);
378 m_SingleTimers.remove(pTimer);
381 m_FreeTimers.push(pTimer);
384 CStack<ITimer *> s_tokill;
385 void TimerSystem::MapChange(bool real_mapchange)
390 for (iter=m_SingleTimers.begin(); iter!=m_SingleTimers.end(); iter++)
393 if (real_mapchange && (pTimer->m_Flags & TIMER_FLAG_NO_MAPCHANGE))
395 s_tokill.push(pTimer);
399 for (iter=m_LoopTimers.begin(); iter!=m_LoopTimers.end(); iter++)
402 if (real_mapchange && (pTimer->m_Flags & TIMER_FLAG_NO_MAPCHANGE))
404 s_tokill.push(pTimer);
408 while (!s_tokill.empty())
410 KillTimer(s_tokill.front());
415 IMapTimer *TimerSystem::SetMapTimer(IMapTimer *pTimer)
417 IMapTimer *old = m_pMapTimer;
419 m_pMapTimer = pTimer;
423 m_pMapTimer->SetMapTimerStatus(true);
428 old->SetMapTimerStatus(false);
434 IMapTimer *TimerSystem::GetMapTimer()
439 void TimerSystem::MapTimeLimitExtended(int old_limit, int new_limit)
441 if (old_limit == new_limit)