Debug build fixes.
3 * =============================================================================
5 * Copyright (C) 2004-2008 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"
37 #include "ConVarManager.h"
38 #include "logic_bridge.h"
40 #define TIMER_MIN_ACCURACY 0.1
43 double g_fUniversalTime = 0.0f;
44 float g_fGameStartTime = 0.0f; /* Game game start time, non-universal */
45 double g_fTimerThink = 0.0f; /* Timer's next think time */
46 const double *g_pUniversalTime = &g_fUniversalTime;
47 ConVar *mp_timelimit = NULL;
48 int g_TimeLeftMode = 0;
50 ConVar sm_time_adjustment("sm_time_adjustment", "0", 0, "Adjusts the server time in seconds");
52 inline double GetSimulatedTime()
54 return g_fUniversalTime;
57 time_t GetAdjustedTime(time_t *buf)
59 time_t val = time(NULL) + sm_time_adjustment.GetInt();
67 class DefaultMapTimer :
70 public IConVarChangeListener
78 void OnSourceModLevelChange(const char *mapName)
80 g_fGameStartTime = 0.0f;
85 return mp_timelimit->GetInt();
88 void SetMapTimerStatus(bool enabled)
90 if (enabled && !m_bInUse)
94 else if (!enabled && m_bInUse)
101 void ExtendMapTimeLimit(int extra_time)
105 mp_timelimit->SetValue(0);
111 mp_timelimit->SetValue(mp_timelimit->GetInt() + extra_time);
114 void OnConVarChanged(ConVar *pConVar, const char *oldValue, float flOldValue)
116 g_Timers.MapTimeLeftChanged();
122 g_ConVarManager.AddConVarChangeListener("mp_timelimit", this);
127 g_ConVarManager.RemoveConVarChangeListener("mp_timelimit", this);
135 * If the ticking process has run amok (should be impossible), we
136 * take care of this by "skipping" the in-between time, to prevent
137 * a bazillion times from firing on accident. This has the result
138 * that a drastic jump in time will continue acting normally. Users
139 * may not expect this, but... I think it is the best solution.
141 inline double CalcNextThink(double last, float interval)
143 if (g_fUniversalTime - last - interval <= TIMER_MIN_ACCURACY)
145 return last + interval;
149 return g_fUniversalTime + interval;
153 void ITimer::Initialize(ITimedEvent *pCallbacks, float fInterval, float fToExec, void *pData, int flags)
155 m_Listener = pCallbacks;
156 m_Interval = fInterval;
164 TimerSystem::TimerSystem()
167 m_bHasMapTickedYet = false;
168 m_bHasMapSimulatedYet = false;
169 m_fLastTickedTime = 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 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::OnSourceModLevelEnd()
208 m_bHasMapTickedYet = false;
209 m_bHasMapSimulatedYet = false;
212 void TimerSystem::GameFrame(bool simulating)
214 if (simulating && m_bHasMapTickedYet)
216 g_fUniversalTime += gpGlobals->curtime - m_fLastTickedTime;
217 if (!m_bHasMapSimulatedYet)
219 m_bHasMapSimulatedYet = true;
220 MapTimeLeftChanged();
225 g_fUniversalTime += gpGlobals->interval_per_tick;
228 m_fLastTickedTime = gpGlobals->curtime;
229 m_bHasMapTickedYet = true;
231 if (g_fUniversalTime >= g_fTimerThink)
235 g_fTimerThink = CalcNextThink(g_fTimerThink, TIMER_MIN_ACCURACY);
238 RunFrameHooks(simulating);
240 if (m_pOnGameFrame->GetFunctionCount())
242 m_pOnGameFrame->Execute(NULL);
246 void TimerSystem::RunFrame()
251 double curtime = GetSimulatedTime();
252 for (iter=m_SingleTimers.begin(); iter!=m_SingleTimers.end(); )
255 if (curtime >= pTimer->m_ToExec)
257 pTimer->m_InExec = true;
258 pTimer->m_Listener->OnTimer(pTimer, pTimer->m_pData);
259 pTimer->m_Listener->OnTimerEnd(pTimer, pTimer->m_pData);
260 iter = m_SingleTimers.erase(iter);
261 m_FreeTimers.push(pTimer);
270 for (iter=m_LoopTimers.begin(); iter!=m_LoopTimers.end(); )
273 if (curtime >= pTimer->m_ToExec)
275 pTimer->m_InExec = true;
276 res = pTimer->m_Listener->OnTimer(pTimer, pTimer->m_pData);
277 if (pTimer->m_KillMe || (res == Pl_Stop))
279 pTimer->m_Listener->OnTimerEnd(pTimer, pTimer->m_pData);
280 iter = m_LoopTimers.erase(iter);
281 m_FreeTimers.push(pTimer);
284 pTimer->m_InExec = false;
285 pTimer->m_ToExec = CalcNextThink(pTimer->m_ToExec, pTimer->m_Interval);
291 ITimer *TimerSystem::CreateTimer(ITimedEvent *pCallbacks, float fInterval, void *pData, int flags)
295 float to_exec = GetSimulatedTime() + fInterval;
297 if (m_FreeTimers.empty())
301 pTimer = m_FreeTimers.front();
305 pTimer->Initialize(pCallbacks, fInterval, to_exec, pData, flags);
307 if (flags & TIMER_FLAG_REPEAT)
309 m_LoopTimers.push_back(pTimer);
313 if (m_SingleTimers.size() >= 1)
315 iter = --m_SingleTimers.end();
316 if ((*iter)->m_ToExec <= to_exec)
318 goto normal_insert_end;
322 for (iter=m_SingleTimers.begin(); iter!=m_SingleTimers.end(); iter++)
324 if ((*iter)->m_ToExec >= to_exec)
326 m_SingleTimers.insert(iter, pTimer);
332 m_SingleTimers.push_back(pTimer);
338 void TimerSystem::FireTimerOnce(ITimer *pTimer, bool delayExec)
342 if (pTimer->m_InExec)
347 pTimer->m_InExec = true;
348 res = pTimer->m_Listener->OnTimer(pTimer, pTimer->m_pData);
350 if (!(pTimer->m_Flags & TIMER_FLAG_REPEAT))
352 pTimer->m_Listener->OnTimerEnd(pTimer, pTimer->m_pData);
353 m_SingleTimers.remove(pTimer);
354 m_FreeTimers.push(pTimer);
358 if ((res != Pl_Stop) && !pTimer->m_KillMe)
362 pTimer->m_ToExec = GetSimulatedTime() + pTimer->m_Interval;
364 pTimer->m_InExec = false;
367 pTimer->m_Listener->OnTimerEnd(pTimer, pTimer->m_pData);
368 m_LoopTimers.remove(pTimer);
369 m_FreeTimers.push(pTimer);
373 void TimerSystem::KillTimer(ITimer *pTimer)
375 if (pTimer->m_KillMe)
380 if (pTimer->m_InExec)
382 pTimer->m_KillMe = true;
386 pTimer->m_InExec = true; /* The timer it's not really executed but this check needs to be done */
387 pTimer->m_Listener->OnTimerEnd(pTimer, pTimer->m_pData);
389 if (pTimer->m_Flags & TIMER_FLAG_REPEAT)
391 m_LoopTimers.remove(pTimer);
393 m_SingleTimers.remove(pTimer);
396 m_FreeTimers.push(pTimer);
399 CStack<ITimer *> s_tokill;
400 void TimerSystem::RemoveMapChangeTimers()
405 for (iter=m_SingleTimers.begin(); iter!=m_SingleTimers.end(); iter++)
408 if (pTimer->m_Flags & TIMER_FLAG_NO_MAPCHANGE)
410 s_tokill.push(pTimer);
414 for (iter=m_LoopTimers.begin(); iter!=m_LoopTimers.end(); iter++)
417 if (pTimer->m_Flags & TIMER_FLAG_NO_MAPCHANGE)
419 s_tokill.push(pTimer);
423 while (!s_tokill.empty())
425 KillTimer(s_tokill.front());
430 IMapTimer *TimerSystem::SetMapTimer(IMapTimer *pTimer)
432 IMapTimer *old = m_pMapTimer;
434 m_pMapTimer = pTimer;
438 m_pMapTimer->SetMapTimerStatus(true);
443 old->SetMapTimerStatus(false);
449 IMapTimer *TimerSystem::GetMapTimer()
454 void TimerSystem::MapTimeLeftChanged()
456 m_pOnMapTimeLeftChanged->Execute(NULL);
459 void TimerSystem::NotifyOfGameStart(float offset)
461 g_fGameStartTime = gpGlobals->curtime + offset;
464 float TimerSystem::GetTickedTime()
466 return g_fUniversalTime;
469 bool TimerSystem::GetMapTimeLeft(float *time_left)
477 if (!m_bHasMapSimulatedYet || (time_limit = m_pMapTimer->GetMapTimeLimit()) < 1)
483 *time_left = (g_fGameStartTime + time_limit * 60.0f) - gpGlobals->curtime;