Update gamedata for CS:S update.
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 "sourcemm_api.h"
35 #include "frame_hooks.h"
36 #include "ConVarManager.h"
37 #include "logic_bridge.h"
39 #define TIMER_MIN_ACCURACY 0.1
42 double g_fUniversalTime = 0.0f;
43 float g_fGameStartTime = 0.0f; /* Game game start time, non-universal */
44 double g_fTimerThink = 0.0f; /* Timer's next think time */
45 const double *g_pUniversalTime = &g_fUniversalTime;
46 ConVar *mp_timelimit = NULL;
47 int g_TimeLeftMode = 0;
49 ConVar sm_time_adjustment("sm_time_adjustment", "0", 0, "Adjusts the server time in seconds");
51 inline double GetSimulatedTime()
53 return g_fUniversalTime;
56 time_t GetAdjustedTime(time_t *buf)
58 time_t val = time(NULL) + sm_time_adjustment.GetInt();
66 class DefaultMapTimer :
69 public IConVarChangeListener
77 void OnSourceModLevelChange(const char *mapName)
79 g_fGameStartTime = 0.0f;
84 return mp_timelimit->GetInt();
87 void SetMapTimerStatus(bool enabled)
89 if (enabled && !m_bInUse)
93 else if (!enabled && m_bInUse)
100 void ExtendMapTimeLimit(int extra_time)
104 mp_timelimit->SetValue(0);
110 mp_timelimit->SetValue(mp_timelimit->GetInt() + extra_time);
113 void OnConVarChanged(ConVar *pConVar, const char *oldValue, float flOldValue)
115 g_Timers.MapTimeLeftChanged();
121 g_ConVarManager.AddConVarChangeListener("mp_timelimit", this);
126 g_ConVarManager.RemoveConVarChangeListener("mp_timelimit", this);
134 * If the ticking process has run amok (should be impossible), we
135 * take care of this by "skipping" the in-between time, to prevent
136 * a bazillion times from firing on accident. This has the result
137 * that a drastic jump in time will continue acting normally. Users
138 * may not expect this, but... I think it is the best solution.
140 inline double CalcNextThink(double last, float interval)
142 if (g_fUniversalTime - last - interval <= TIMER_MIN_ACCURACY)
144 return last + interval;
148 return g_fUniversalTime + interval;
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;
171 TimerSystem::~TimerSystem()
173 CStack<ITimer *>::iterator iter;
174 for (iter=m_FreeTimers.begin(); iter!=m_FreeTimers.end(); iter++)
178 m_FreeTimers.popall();
181 void TimerSystem::OnSourceModAllInitialized()
183 sharesys->AddInterface(NULL, this);
184 m_pOnGameFrame = forwardsys->CreateForward("OnGameFrame", ET_Ignore, 0, NULL);
185 m_pOnMapTimeLeftChanged = forwardsys->CreateForward("OnMapTimeLeftChanged", ET_Ignore, 0, NULL);
188 void TimerSystem::OnSourceModGameInitialized()
190 mp_timelimit = icvar->FindVar("mp_timelimit");
192 if (m_pMapTimer == NULL && mp_timelimit != NULL)
194 SetMapTimer(&s_DefaultMapTimer);
198 void TimerSystem::OnSourceModShutdown()
201 forwardsys->ReleaseForward(m_pOnGameFrame);
202 forwardsys->ReleaseForward(m_pOnMapTimeLeftChanged);
205 void TimerSystem::OnSourceModLevelEnd()
207 m_bHasMapTickedYet = false;
208 m_bHasMapSimulatedYet = false;
211 void TimerSystem::GameFrame(bool simulating)
213 if (simulating && m_bHasMapTickedYet)
215 g_fUniversalTime += gpGlobals->curtime - m_fLastTickedTime;
216 if (!m_bHasMapSimulatedYet)
218 m_bHasMapSimulatedYet = true;
219 MapTimeLeftChanged();
224 g_fUniversalTime += gpGlobals->interval_per_tick;
227 m_fLastTickedTime = gpGlobals->curtime;
228 m_bHasMapTickedYet = true;
230 if (g_fUniversalTime >= g_fTimerThink)
234 g_fTimerThink = CalcNextThink(g_fTimerThink, TIMER_MIN_ACCURACY);
237 RunFrameHooks(simulating);
239 if (m_pOnGameFrame->GetFunctionCount())
241 m_pOnGameFrame->Execute(NULL);
245 void TimerSystem::RunFrame()
250 double curtime = GetSimulatedTime();
251 for (iter=m_SingleTimers.begin(); iter!=m_SingleTimers.end(); )
254 if (curtime >= pTimer->m_ToExec)
256 pTimer->m_InExec = true;
257 pTimer->m_Listener->OnTimer(pTimer, pTimer->m_pData);
258 pTimer->m_Listener->OnTimerEnd(pTimer, pTimer->m_pData);
259 iter = m_SingleTimers.erase(iter);
260 m_FreeTimers.push(pTimer);
269 for (iter=m_LoopTimers.begin(); iter!=m_LoopTimers.end(); )
272 if (curtime >= pTimer->m_ToExec)
274 pTimer->m_InExec = true;
275 res = pTimer->m_Listener->OnTimer(pTimer, pTimer->m_pData);
276 if (pTimer->m_KillMe || (res == Pl_Stop))
278 pTimer->m_Listener->OnTimerEnd(pTimer, pTimer->m_pData);
279 iter = m_LoopTimers.erase(iter);
280 m_FreeTimers.push(pTimer);
283 pTimer->m_InExec = false;
284 pTimer->m_ToExec = CalcNextThink(pTimer->m_ToExec, pTimer->m_Interval);
290 ITimer *TimerSystem::CreateTimer(ITimedEvent *pCallbacks, float fInterval, void *pData, int flags)
294 float to_exec = GetSimulatedTime() + fInterval;
296 if (m_FreeTimers.empty())
300 pTimer = m_FreeTimers.front();
304 pTimer->Initialize(pCallbacks, fInterval, to_exec, pData, flags);
306 if (flags & TIMER_FLAG_REPEAT)
308 m_LoopTimers.push_back(pTimer);
312 if (m_SingleTimers.size() >= 1)
314 iter = --m_SingleTimers.end();
315 if ((*iter)->m_ToExec <= to_exec)
317 goto normal_insert_end;
321 for (iter=m_SingleTimers.begin(); iter!=m_SingleTimers.end(); iter++)
323 if ((*iter)->m_ToExec >= to_exec)
325 m_SingleTimers.insert(iter, pTimer);
331 m_SingleTimers.push_back(pTimer);
337 void TimerSystem::FireTimerOnce(ITimer *pTimer, bool delayExec)
341 if (pTimer->m_InExec)
346 pTimer->m_InExec = true;
347 res = pTimer->m_Listener->OnTimer(pTimer, pTimer->m_pData);
349 if (!(pTimer->m_Flags & TIMER_FLAG_REPEAT))
351 pTimer->m_Listener->OnTimerEnd(pTimer, pTimer->m_pData);
352 m_SingleTimers.remove(pTimer);
353 m_FreeTimers.push(pTimer);
357 if ((res != Pl_Stop) && !pTimer->m_KillMe)
361 pTimer->m_ToExec = GetSimulatedTime() + pTimer->m_Interval;
363 pTimer->m_InExec = false;
366 pTimer->m_Listener->OnTimerEnd(pTimer, pTimer->m_pData);
367 m_LoopTimers.remove(pTimer);
368 m_FreeTimers.push(pTimer);
372 void TimerSystem::KillTimer(ITimer *pTimer)
374 if (pTimer->m_KillMe)
379 if (pTimer->m_InExec)
381 pTimer->m_KillMe = true;
385 pTimer->m_InExec = true; /* The timer it's not really executed but this check needs to be done */
386 pTimer->m_Listener->OnTimerEnd(pTimer, pTimer->m_pData);
388 if (pTimer->m_Flags & TIMER_FLAG_REPEAT)
390 m_LoopTimers.remove(pTimer);
392 m_SingleTimers.remove(pTimer);
395 m_FreeTimers.push(pTimer);
398 CStack<ITimer *> s_tokill;
399 void TimerSystem::RemoveMapChangeTimers()
404 for (iter=m_SingleTimers.begin(); iter!=m_SingleTimers.end(); iter++)
407 if (pTimer->m_Flags & TIMER_FLAG_NO_MAPCHANGE)
409 s_tokill.push(pTimer);
413 for (iter=m_LoopTimers.begin(); iter!=m_LoopTimers.end(); iter++)
416 if (pTimer->m_Flags & TIMER_FLAG_NO_MAPCHANGE)
418 s_tokill.push(pTimer);
422 while (!s_tokill.empty())
424 KillTimer(s_tokill.front());
429 IMapTimer *TimerSystem::SetMapTimer(IMapTimer *pTimer)
431 IMapTimer *old = m_pMapTimer;
433 m_pMapTimer = pTimer;
437 m_pMapTimer->SetMapTimerStatus(true);
442 old->SetMapTimerStatus(false);
448 IMapTimer *TimerSystem::GetMapTimer()
453 void TimerSystem::MapTimeLeftChanged()
455 m_pOnMapTimeLeftChanged->Execute(NULL);
458 void TimerSystem::NotifyOfGameStart(float offset)
460 g_fGameStartTime = gpGlobals->curtime + offset;
463 float TimerSystem::GetTickedTime()
465 return g_fUniversalTime;
468 bool TimerSystem::GetMapTimeLeft(float *time_left)
476 if (!m_bHasMapSimulatedYet || (time_limit = m_pMapTimer->GetMapTimeLimit()) < 1)
482 *time_left = (g_fGameStartTime + time_limit * 60.0f) - gpGlobals->curtime;