The current map played shouldn't appear in MapChooser votes or RTV votes now. It can still be nominated in RTV though.
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 #define TIMER_MIN_ACCURACY 0.1
40 #if !defined ORANGEBOX_BUILD
41 #define CallGlobalChangeCallbacks CallGlobalChangeCallback
44 #if defined ORANGEBOX_BUILD
45 SH_DECL_HOOK3_void(ICvar, CallGlobalChangeCallbacks, SH_NOATTRIB, false, ConVar *, const char *, float);
47 SH_DECL_HOOK2_void(ICvar, CallGlobalChangeCallbacks, SH_NOATTRIB, false, ConVar *, const char *);
51 double g_fUniversalTime = 0.0f;
52 float g_fGameStartTime = 0.0f; /* Game game start time, non-universal */
53 double g_fTimerThink = 0.0f; /* Timer's next think time */
54 const double *g_pUniversalTime = &g_fUniversalTime;
55 ConVar *mp_timelimit = NULL;
56 int g_TimeLeftMode = 0;
58 ConVar sm_time_adjustment("sm_time_adjustment", "0", 0, "Adjusts the server time in seconds");
60 inline double GetSimulatedTime()
62 return g_fUniversalTime;
65 time_t GetAdjustedTime(time_t *buf)
67 time_t val = time(NULL) + sm_time_adjustment.GetInt();
75 class DefaultMapTimer :
85 void OnSourceModLevelChange(const char *mapName)
87 g_fGameStartTime = 0.0f;
92 return mp_timelimit->GetInt();
95 void SetMapTimerStatus(bool enabled)
97 if (enabled && !m_bInUse)
101 else if (!enabled && m_bInUse)
108 void ExtendMapTimeLimit(int extra_time)
112 mp_timelimit->SetValue(0);
118 mp_timelimit->SetValue(mp_timelimit->GetInt() + extra_time);
121 #if defined ORANGEBOX_BUILD
122 void GlobalChangeCallback(ConVar *pVar, const char *old_value, float flOldValue)
124 void GlobalChangeCallback(ConVar *pVar, const char *old_value)
127 if (pVar != mp_timelimit)
132 if (atoi(old_value) == pVar->GetInt())
137 g_Timers.MapTimeLeftChanged();
143 SH_ADD_HOOK_MEMFUNC(ICvar, CallGlobalChangeCallbacks, icvar, this, &DefaultMapTimer::GlobalChangeCallback, false);
148 SH_REMOVE_HOOK_MEMFUNC(ICvar, CallGlobalChangeCallbacks, icvar, this, &DefaultMapTimer::GlobalChangeCallback, false);
156 * If the ticking process has run amok (should be impossible), we
157 * take care of this by "skipping" the in-between time, to prevent
158 * a bazillion times from firing on accident. This has the result
159 * that a drastic jump in time will continue acting normally. Users
160 * may not expect this, but... I think it is the best solution.
162 inline double CalcNextThink(double last, float interval)
164 if (g_fUniversalTime - last - interval <= TIMER_MIN_ACCURACY)
166 return last + interval;
170 return g_fUniversalTime + interval;
174 void ITimer::Initialize(ITimedEvent *pCallbacks, float fInterval, float fToExec, void *pData, int flags)
176 m_Listener = pCallbacks;
177 m_Interval = fInterval;
185 TimerSystem::TimerSystem()
188 m_bHasMapTickedYet = false;
189 m_bHasMapSimulatedYet = false;
190 m_fLastTickedTime = 0.0f;
193 TimerSystem::~TimerSystem()
195 CStack<ITimer *>::iterator iter;
196 for (iter=m_FreeTimers.begin(); iter!=m_FreeTimers.end(); iter++)
200 m_FreeTimers.popall();
203 void TimerSystem::OnSourceModAllInitialized()
205 g_ShareSys.AddInterface(NULL, this);
206 m_pOnGameFrame = g_Forwards.CreateForward("OnGameFrame", ET_Ignore, 0, NULL);
207 m_pOnMapTimeLeftChanged = g_Forwards.CreateForward("OnMapTimeLeftChanged", ET_Ignore, 0, NULL);
210 void TimerSystem::OnSourceModGameInitialized()
212 mp_timelimit = icvar->FindVar("mp_timelimit");
214 if (m_pMapTimer == NULL && mp_timelimit != NULL)
216 SetMapTimer(&s_DefaultMapTimer);
220 void TimerSystem::OnSourceModShutdown()
223 g_Forwards.ReleaseForward(m_pOnGameFrame);
224 g_Forwards.ReleaseForward(m_pOnMapTimeLeftChanged);
227 void TimerSystem::OnSourceModLevelChange(const char *mapName)
232 void TimerSystem::OnSourceModLevelEnd()
234 m_bHasMapTickedYet = false;
235 m_bHasMapSimulatedYet = false;
238 void TimerSystem::GameFrame(bool simulating)
240 if (simulating && m_bHasMapTickedYet)
242 g_fUniversalTime += gpGlobals->curtime - m_fLastTickedTime;
243 if (!m_bHasMapSimulatedYet)
245 m_bHasMapSimulatedYet = true;
246 MapTimeLeftChanged();
251 g_fUniversalTime += gpGlobals->interval_per_tick;
254 m_fLastTickedTime = gpGlobals->curtime;
255 m_bHasMapTickedYet = true;
257 if (g_fUniversalTime >= g_fTimerThink)
261 g_fTimerThink = CalcNextThink(g_fTimerThink, TIMER_MIN_ACCURACY);
266 if (m_pOnGameFrame->GetFunctionCount())
268 m_pOnGameFrame->Execute(NULL);
272 void TimerSystem::RunFrame()
277 double curtime = GetSimulatedTime();
278 for (iter=m_SingleTimers.begin(); iter!=m_SingleTimers.end(); )
281 if (curtime >= pTimer->m_ToExec)
283 pTimer->m_InExec = true;
284 pTimer->m_Listener->OnTimer(pTimer, pTimer->m_pData);
285 pTimer->m_Listener->OnTimerEnd(pTimer, pTimer->m_pData);
286 iter = m_SingleTimers.erase(iter);
287 m_FreeTimers.push(pTimer);
296 for (iter=m_LoopTimers.begin(); iter!=m_LoopTimers.end(); )
299 if (curtime >= pTimer->m_ToExec)
301 pTimer->m_InExec = true;
302 res = pTimer->m_Listener->OnTimer(pTimer, pTimer->m_pData);
303 if (pTimer->m_KillMe || (res == Pl_Stop))
305 pTimer->m_Listener->OnTimerEnd(pTimer, pTimer->m_pData);
306 iter = m_LoopTimers.erase(iter);
307 m_FreeTimers.push(pTimer);
310 pTimer->m_InExec = false;
311 pTimer->m_ToExec = CalcNextThink(pTimer->m_ToExec, pTimer->m_Interval);
317 ITimer *TimerSystem::CreateTimer(ITimedEvent *pCallbacks, float fInterval, void *pData, int flags)
321 float to_exec = GetSimulatedTime() + fInterval;
323 if (m_FreeTimers.empty())
327 pTimer = m_FreeTimers.front();
331 pTimer->Initialize(pCallbacks, fInterval, to_exec, pData, flags);
333 if (flags & TIMER_FLAG_REPEAT)
335 m_LoopTimers.push_back(pTimer);
339 if (m_SingleTimers.size() >= 1)
341 iter = --m_SingleTimers.end();
342 if ((*iter)->m_ToExec <= to_exec)
344 goto normal_insert_end;
348 for (iter=m_SingleTimers.begin(); iter!=m_SingleTimers.end(); iter++)
350 if ((*iter)->m_ToExec >= to_exec)
352 m_SingleTimers.insert(iter, pTimer);
358 m_SingleTimers.push_back(pTimer);
364 void TimerSystem::FireTimerOnce(ITimer *pTimer, bool delayExec)
368 if (pTimer->m_InExec)
373 pTimer->m_InExec = true;
374 res = pTimer->m_Listener->OnTimer(pTimer, pTimer->m_pData);
376 if (!(pTimer->m_Flags & TIMER_FLAG_REPEAT))
378 pTimer->m_Listener->OnTimerEnd(pTimer, pTimer->m_pData);
379 m_SingleTimers.remove(pTimer);
380 m_FreeTimers.push(pTimer);
384 if ((res != Pl_Stop) && !pTimer->m_KillMe)
388 pTimer->m_ToExec = GetSimulatedTime() + pTimer->m_Interval;
390 pTimer->m_InExec = false;
393 pTimer->m_Listener->OnTimerEnd(pTimer, pTimer->m_pData);
394 m_LoopTimers.remove(pTimer);
395 m_FreeTimers.push(pTimer);
399 void TimerSystem::KillTimer(ITimer *pTimer)
401 if (pTimer->m_KillMe)
406 if (pTimer->m_InExec)
408 pTimer->m_KillMe = true;
412 pTimer->m_InExec = true; /* The timer it's not really executed but this check needs to be done */
413 pTimer->m_Listener->OnTimerEnd(pTimer, pTimer->m_pData);
415 if (pTimer->m_Flags & TIMER_FLAG_REPEAT)
417 m_LoopTimers.remove(pTimer);
419 m_SingleTimers.remove(pTimer);
422 m_FreeTimers.push(pTimer);
425 CStack<ITimer *> s_tokill;
426 void TimerSystem::MapChange(bool real_mapchange)
431 for (iter=m_SingleTimers.begin(); iter!=m_SingleTimers.end(); iter++)
434 if (real_mapchange && (pTimer->m_Flags & TIMER_FLAG_NO_MAPCHANGE))
436 s_tokill.push(pTimer);
440 for (iter=m_LoopTimers.begin(); iter!=m_LoopTimers.end(); iter++)
443 if (real_mapchange && (pTimer->m_Flags & TIMER_FLAG_NO_MAPCHANGE))
445 s_tokill.push(pTimer);
449 while (!s_tokill.empty())
451 KillTimer(s_tokill.front());
456 IMapTimer *TimerSystem::SetMapTimer(IMapTimer *pTimer)
458 IMapTimer *old = m_pMapTimer;
460 m_pMapTimer = pTimer;
464 m_pMapTimer->SetMapTimerStatus(true);
469 old->SetMapTimerStatus(false);
475 IMapTimer *TimerSystem::GetMapTimer()
480 void TimerSystem::MapTimeLeftChanged()
482 m_pOnMapTimeLeftChanged->Execute(NULL);
485 void TimerSystem::NotifyOfGameStart(float offset)
487 g_fGameStartTime = gpGlobals->curtime + offset;
490 float TimerSystem::GetTickedTime()
492 return g_fUniversalTime;
495 bool TimerSystem::GetMapTimeLeft(float *time_left)
503 if (!m_bHasMapSimulatedYet || (time_limit = m_pMapTimer->GetMapTimeLimit()) < 1)
509 *time_left = (g_fGameStartTime + time_limit * 60.0f) - gpGlobals->curtime;