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>.
37 inline float GetSimulatedTime()
39 if (g_SimTicks.ticking)
41 return gpGlobals->curtime;
43 return g_SimTicks.ticktime;
47 void ITimer::Initialize(ITimedEvent *pCallbacks, float fInterval, float fToExec, void *pData, int flags)
49 m_Listener = pCallbacks;
50 m_Interval = fInterval;
58 TimerSystem::~TimerSystem()
60 CStack<ITimer *>::iterator iter;
61 for (iter=m_FreeTimers.begin(); iter!=m_FreeTimers.end(); iter++)
65 m_FreeTimers.popall();
68 void TimerSystem::OnSourceModAllInitialized()
70 g_ShareSys.AddInterface(NULL, this);
73 void TimerSystem::OnSourceModLevelChange(const char *mapName)
78 void TimerSystem::RunFrame()
83 float curtime = GetSimulatedTime();
84 for (iter=m_SingleTimers.begin(); iter!=m_SingleTimers.end(); )
87 if (curtime >= pTimer->m_ToExec)
89 pTimer->m_InExec = true;
90 pTimer->m_Listener->OnTimer(pTimer, pTimer->m_pData);
91 pTimer->m_Listener->OnTimerEnd(pTimer, pTimer->m_pData);
92 iter = m_SingleTimers.erase(iter);
93 m_FreeTimers.push(pTimer);
100 for (iter=m_LoopTimers.begin(); iter!=m_LoopTimers.end(); )
103 if (curtime >= pTimer->m_ToExec)
105 pTimer->m_InExec = true;
106 res = pTimer->m_Listener->OnTimer(pTimer, pTimer->m_pData);
107 if (pTimer->m_KillMe || (res == Pl_Stop))
109 pTimer->m_Listener->OnTimerEnd(pTimer, pTimer->m_pData);
110 iter = m_LoopTimers.erase(iter);
111 m_FreeTimers.push(pTimer);
114 pTimer->m_InExec = false;
115 pTimer->m_ToExec = curtime + pTimer->m_Interval;
120 m_LastExecTime = curtime;
123 ITimer *TimerSystem::CreateTimer(ITimedEvent *pCallbacks, float fInterval, void *pData, int flags)
127 float to_exec = GetSimulatedTime() + fInterval;
129 if (m_FreeTimers.empty())
133 pTimer = m_FreeTimers.front();
137 pTimer->Initialize(pCallbacks, fInterval, to_exec, pData, flags);
139 if (flags & TIMER_FLAG_REPEAT)
141 m_LoopTimers.push_back(pTimer);
145 if (m_SingleTimers.size() >= 1)
147 iter = --m_SingleTimers.end();
148 if ((*iter)->m_ToExec <= to_exec)
154 for (iter=m_SingleTimers.begin(); iter!=m_SingleTimers.end(); iter++)
156 if ((*iter)->m_ToExec >= to_exec)
158 m_SingleTimers.insert(iter, pTimer);
164 m_SingleTimers.push_back(pTimer);
170 void TimerSystem::FireTimerOnce(ITimer *pTimer, bool delayExec)
174 if (pTimer->m_InExec)
179 pTimer->m_InExec = true;
180 res = pTimer->m_Listener->OnTimer(pTimer, pTimer->m_pData);
182 if (!(pTimer->m_Flags & TIMER_FLAG_REPEAT))
184 pTimer->m_Listener->OnTimerEnd(pTimer, pTimer->m_pData);
185 m_SingleTimers.remove(pTimer);
186 m_FreeTimers.push(pTimer);
188 if ((res != Pl_Stop) && !pTimer->m_KillMe)
192 pTimer->m_ToExec = GetSimulatedTime() + pTimer->m_Interval;
194 pTimer->m_InExec = false;
197 pTimer->m_Listener->OnTimerEnd(pTimer, pTimer->m_pData);
198 m_LoopTimers.remove(pTimer);
199 m_FreeTimers.push(pTimer);
203 void TimerSystem::KillTimer(ITimer *pTimer)
207 if (pTimer->m_KillMe)
212 if (pTimer->m_InExec)
214 pTimer->m_KillMe = true;
218 pTimer->m_InExec = true; /* The timer it's not really executed but this check needs to be done */
219 pTimer->m_Listener->OnTimerEnd(pTimer, pTimer->m_pData);
221 pList = (pTimer->m_Flags & TIMER_FLAG_REPEAT) ? &m_LoopTimers : &m_SingleTimers;
223 pList->remove(pTimer);
224 m_FreeTimers.push(pTimer);
227 CStack<ITimer *> s_tokill;
228 void TimerSystem::MapChange(bool real_mapchange)
233 for (iter=m_SingleTimers.begin(); iter!=m_SingleTimers.end(); iter++)
236 if (real_mapchange && (pTimer->m_Flags & TIMER_FLAG_NO_MAPCHANGE))
238 s_tokill.push(pTimer);
240 pTimer->m_ToExec = pTimer->m_ToExec - m_LastExecTime + GetSimulatedTime();
244 for (iter=m_LoopTimers.begin(); iter!=m_LoopTimers.end(); iter++)
247 if (real_mapchange && (pTimer->m_Flags & TIMER_FLAG_NO_MAPCHANGE))
249 s_tokill.push(pTimer);
251 pTimer->m_ToExec = pTimer->m_ToExec - m_LastExecTime + GetSimulatedTime();
255 while (!s_tokill.empty())
257 KillTimer(s_tokill.front());
261 m_LastExecTime = GetSimulatedTime();