clarified and fixed the meaning of timelimit/timeleft when no frames have been processed
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_fGameStartTime = 0.0f; /* Game game start time, non-universal */
43 const float *g_pUniversalTime = &g_fUniversalTime;
44 ConVar *mp_timelimit = NULL;
45 int g_TimeLeftMode = 0;
47 ConVar sm_time_adjustment("sm_time_adjustment", "0", 0, "Adjusts the server time in seconds");
49 inline float GetSimulatedTime()
51 return g_fUniversalTime;
54 time_t GetAdjustedTime(time_t *buf)
56 time_t val = time(NULL) + sm_time_adjustment.GetInt();
64 class DefaultMapTimer :
74 void OnSourceModLevelChange(const char *mapName)
76 g_fGameStartTime = 0.0f;
81 return mp_timelimit->GetInt();
84 void SetMapTimerStatus(bool enabled)
86 if (enabled && !m_bInUse)
90 else if (!enabled && m_bInUse)
97 void ExtendMapTimeLimit(int extra_time)
103 mp_timelimit->SetValue(0);
107 mp_timelimit->SetValue(mp_timelimit->GetInt() + extra_time);
110 void GlobalChangeCallback(ConVar *pVar, const char *old_value)
112 if (pVar != mp_timelimit)
117 if (atoi(old_value) == pVar->GetInt())
122 g_Timers.MapTimeLeftChanged();
128 SH_ADD_HOOK_MEMFUNC(ICvar, CallGlobalChangeCallback, icvar, this, &DefaultMapTimer::GlobalChangeCallback, false);
133 SH_REMOVE_HOOK_MEMFUNC(ICvar, CallGlobalChangeCallback, icvar, this, &DefaultMapTimer::GlobalChangeCallback, false);
140 void ITimer::Initialize(ITimedEvent *pCallbacks, float fInterval, float fToExec, void *pData, int flags)
142 m_Listener = pCallbacks;
143 m_Interval = fInterval;
151 TimerSystem::TimerSystem()
154 m_bHasMapTickedYet = false;
155 m_bHasMapSimulatedYet = 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);
174 m_pOnMapTimeLeftChanged = g_Forwards.CreateForward("OnMapTimeLeftChanged", ET_Ignore, 0, NULL);
177 void TimerSystem::OnSourceModGameInitialized()
179 mp_timelimit = icvar->FindVar("mp_timelimit");
181 if (m_pMapTimer == NULL && mp_timelimit != NULL)
183 SetMapTimer(&s_DefaultMapTimer);
187 void TimerSystem::OnSourceModShutdown()
190 g_Forwards.ReleaseForward(m_pOnGameFrame);
191 g_Forwards.ReleaseForward(m_pOnMapTimeLeftChanged);
194 void TimerSystem::OnSourceModLevelChange(const char *mapName)
199 void TimerSystem::OnSourceModLevelEnd()
201 m_bHasMapTickedYet = false;
202 m_bHasMapSimulatedYet = false;
205 void TimerSystem::GameFrame(bool simulating)
207 if (simulating && m_bHasMapTickedYet)
209 g_fUniversalTime += gpGlobals->curtime - m_fLastTickedTime;
210 if (!m_bHasMapSimulatedYet)
212 m_bHasMapSimulatedYet = true;
213 MapTimeLeftChanged();
218 g_fUniversalTime += gpGlobals->interval_per_tick;
221 m_fLastTickedTime = gpGlobals->curtime;
222 m_bHasMapTickedYet = true;
224 if (g_fUniversalTime - m_LastExecTime >= 0.1)
231 if (m_pOnGameFrame->GetFunctionCount())
233 m_pOnGameFrame->Execute(NULL);
237 void TimerSystem::RunFrame()
242 float curtime = GetSimulatedTime();
243 for (iter=m_SingleTimers.begin(); iter!=m_SingleTimers.end(); )
246 if (curtime >= pTimer->m_ToExec)
248 pTimer->m_InExec = true;
249 pTimer->m_Listener->OnTimer(pTimer, pTimer->m_pData);
250 pTimer->m_Listener->OnTimerEnd(pTimer, pTimer->m_pData);
251 iter = m_SingleTimers.erase(iter);
252 m_FreeTimers.push(pTimer);
259 for (iter=m_LoopTimers.begin(); iter!=m_LoopTimers.end(); )
262 if (curtime >= pTimer->m_ToExec)
264 pTimer->m_InExec = true;
265 res = pTimer->m_Listener->OnTimer(pTimer, pTimer->m_pData);
266 if (pTimer->m_KillMe || (res == Pl_Stop))
268 pTimer->m_Listener->OnTimerEnd(pTimer, pTimer->m_pData);
269 iter = m_LoopTimers.erase(iter);
270 m_FreeTimers.push(pTimer);
273 pTimer->m_InExec = false;
274 pTimer->m_ToExec = curtime + pTimer->m_Interval;
279 m_LastExecTime = curtime;
282 ITimer *TimerSystem::CreateTimer(ITimedEvent *pCallbacks, float fInterval, void *pData, int flags)
286 float to_exec = GetSimulatedTime() + fInterval;
288 if (m_FreeTimers.empty())
292 pTimer = m_FreeTimers.front();
296 pTimer->Initialize(pCallbacks, fInterval, to_exec, pData, flags);
298 if (flags & TIMER_FLAG_REPEAT)
300 m_LoopTimers.push_back(pTimer);
304 if (m_SingleTimers.size() >= 1)
306 iter = --m_SingleTimers.end();
307 if ((*iter)->m_ToExec <= to_exec)
309 goto normal_insert_end;
313 for (iter=m_SingleTimers.begin(); iter!=m_SingleTimers.end(); iter++)
315 if ((*iter)->m_ToExec >= to_exec)
317 m_SingleTimers.insert(iter, pTimer);
323 m_SingleTimers.push_back(pTimer);
329 void TimerSystem::FireTimerOnce(ITimer *pTimer, bool delayExec)
333 if (pTimer->m_InExec)
338 pTimer->m_InExec = true;
339 res = pTimer->m_Listener->OnTimer(pTimer, pTimer->m_pData);
341 if (!(pTimer->m_Flags & TIMER_FLAG_REPEAT))
343 pTimer->m_Listener->OnTimerEnd(pTimer, pTimer->m_pData);
344 m_SingleTimers.remove(pTimer);
345 m_FreeTimers.push(pTimer);
347 if ((res != Pl_Stop) && !pTimer->m_KillMe)
351 pTimer->m_ToExec = GetSimulatedTime() + pTimer->m_Interval;
353 pTimer->m_InExec = false;
356 pTimer->m_Listener->OnTimerEnd(pTimer, pTimer->m_pData);
357 m_LoopTimers.remove(pTimer);
358 m_FreeTimers.push(pTimer);
362 void TimerSystem::KillTimer(ITimer *pTimer)
364 if (pTimer->m_KillMe)
369 if (pTimer->m_InExec)
371 pTimer->m_KillMe = true;
375 pTimer->m_InExec = true; /* The timer it's not really executed but this check needs to be done */
376 pTimer->m_Listener->OnTimerEnd(pTimer, pTimer->m_pData);
378 if (pTimer->m_Flags & TIMER_FLAG_REPEAT)
380 m_LoopTimers.remove(pTimer);
382 m_SingleTimers.remove(pTimer);
385 m_FreeTimers.push(pTimer);
388 CStack<ITimer *> s_tokill;
389 void TimerSystem::MapChange(bool real_mapchange)
394 for (iter=m_SingleTimers.begin(); iter!=m_SingleTimers.end(); iter++)
397 if (real_mapchange && (pTimer->m_Flags & TIMER_FLAG_NO_MAPCHANGE))
399 s_tokill.push(pTimer);
403 for (iter=m_LoopTimers.begin(); iter!=m_LoopTimers.end(); iter++)
406 if (real_mapchange && (pTimer->m_Flags & TIMER_FLAG_NO_MAPCHANGE))
408 s_tokill.push(pTimer);
412 while (!s_tokill.empty())
414 KillTimer(s_tokill.front());
419 IMapTimer *TimerSystem::SetMapTimer(IMapTimer *pTimer)
421 IMapTimer *old = m_pMapTimer;
423 m_pMapTimer = pTimer;
427 m_pMapTimer->SetMapTimerStatus(true);
432 old->SetMapTimerStatus(false);
438 IMapTimer *TimerSystem::GetMapTimer()
443 void TimerSystem::MapTimeLeftChanged()
445 m_pOnMapTimeLeftChanged->Execute(NULL);
448 void TimerSystem::NotifyOfGameStart(float offset)
450 g_fGameStartTime = gpGlobals->curtime + offset;
453 float TimerSystem::GetTickedTime()
455 return g_fUniversalTime;
458 bool TimerSystem::GetMapTimeLeft(float *time_left)
466 if (!m_bHasMapSimulatedYet || (time_limit = m_pMapTimer->GetMapTimeLimit()) < 1)
472 *time_left = (g_fGameStartTime + time_limit * 60.0f) - gpGlobals->curtime;