core/TimerSys.cpp
author Matt Woodrow <pred@alliedmods.net>
Fri Aug 08 00:17:37 2008 +0000 (2008-08-08)
changeset 2224 676c3b826f13
parent 1952 b7aa2b626807
child 2289 44045ead61cd
permissions -rw-r--r--
Initial Import of beta structs extension.
ds@1952
     1
/**
ds@1952
     2
 * vim: set ts=4 :
ds@1952
     3
 * =============================================================================
ds@1952
     4
 * SourceMod
ds@1952
     5
 * Copyright (C) 2004-2008 AlliedModders LLC.  All rights reserved.
ds@1952
     6
 * =============================================================================
ds@1952
     7
 *
ds@1952
     8
 * This program is free software; you can redistribute it and/or modify it under
ds@1952
     9
 * the terms of the GNU General Public License, version 3.0, as published by the
ds@1952
    10
 * Free Software Foundation.
ds@1952
    11
 * 
ds@1952
    12
 * This program is distributed in the hope that it will be useful, but WITHOUT
ds@1952
    13
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
ds@1952
    14
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
ds@1952
    15
 * details.
ds@1952
    16
 *
ds@1952
    17
 * You should have received a copy of the GNU General Public License along with
ds@1952
    18
 * this program.  If not, see <http://www.gnu.org/licenses/>.
ds@1952
    19
 *
ds@1952
    20
 * As a special exception, AlliedModders LLC gives you permission to link the
ds@1952
    21
 * code of this program (as well as its derivative works) to "Half-Life 2," the
ds@1952
    22
 * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
ds@1952
    23
 * by the Valve Corporation.  You must obey the GNU General Public License in
ds@1952
    24
 * all respects for all other code used.  Additionally, AlliedModders LLC grants
ds@1952
    25
 * this exception to all derivative works.  AlliedModders LLC defines further
ds@1952
    26
 * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
ds@1952
    27
 * or <http://www.sourcemod.net/license.php>.
ds@1952
    28
 *
ds@1952
    29
 * Version: $Id$
ds@1952
    30
 */
ds@1952
    31
ds@1952
    32
#include <time.h>
ds@1952
    33
#include "TimerSys.h"
ds@1952
    34
#include "ForwardSys.h"
ds@1952
    35
#include "sourcemm_api.h"
ds@1952
    36
#include "frame_hooks.h"
ds@1952
    37
#include "ConVarManager.h"
ds@1952
    38
ds@1952
    39
#define TIMER_MIN_ACCURACY		0.1
ds@1952
    40
ds@1952
    41
TimerSystem g_Timers;
ds@1952
    42
double g_fUniversalTime = 0.0f;
ds@1952
    43
float g_fGameStartTime = 0.0f;	/* Game game start time, non-universal */
ds@1952
    44
double g_fTimerThink = 0.0f;		/* Timer's next think time */
ds@1952
    45
const double *g_pUniversalTime = &g_fUniversalTime;
ds@1952
    46
ConVar *mp_timelimit = NULL;
ds@1952
    47
int g_TimeLeftMode = 0;
ds@1952
    48
ds@1952
    49
ConVar sm_time_adjustment("sm_time_adjustment", "0", 0, "Adjusts the server time in seconds");
ds@1952
    50
ds@1952
    51
inline double GetSimulatedTime()
ds@1952
    52
{
ds@1952
    53
	return g_fUniversalTime;
ds@1952
    54
}
ds@1952
    55
ds@1952
    56
time_t GetAdjustedTime(time_t *buf)
ds@1952
    57
{
ds@1952
    58
	time_t val = time(NULL) + sm_time_adjustment.GetInt();
ds@1952
    59
	if (buf)
ds@1952
    60
	{
ds@1952
    61
		*buf = val;
ds@1952
    62
	}
ds@1952
    63
	return val;
ds@1952
    64
}
ds@1952
    65
ds@1952
    66
class DefaultMapTimer : 
ds@1952
    67
	public IMapTimer,
ds@1952
    68
	public SMGlobalClass,
ds@1952
    69
	public IConVarChangeListener
ds@1952
    70
{
ds@1952
    71
public:
ds@1952
    72
	DefaultMapTimer()
ds@1952
    73
	{
ds@1952
    74
		m_bInUse = false;
ds@1952
    75
	}
ds@1952
    76
ds@1952
    77
	void OnSourceModLevelChange(const char *mapName)
ds@1952
    78
	{
ds@1952
    79
		g_fGameStartTime = 0.0f;
ds@1952
    80
	}
ds@1952
    81
ds@1952
    82
	int GetMapTimeLimit()
ds@1952
    83
	{
ds@1952
    84
		return mp_timelimit->GetInt();
ds@1952
    85
	}
ds@1952
    86
ds@1952
    87
	void SetMapTimerStatus(bool enabled)
ds@1952
    88
	{
ds@1952
    89
		if (enabled && !m_bInUse)
ds@1952
    90
		{
ds@1952
    91
			Enable();
ds@1952
    92
		} 
ds@1952
    93
		else if (!enabled && m_bInUse)
ds@1952
    94
		{
ds@1952
    95
			Disable();
ds@1952
    96
		}
ds@1952
    97
		m_bInUse = enabled;
ds@1952
    98
	}
ds@1952
    99
ds@1952
   100
	void ExtendMapTimeLimit(int extra_time)
ds@1952
   101
	{
ds@1952
   102
		if (extra_time == 0)
ds@1952
   103
		{
ds@1952
   104
			mp_timelimit->SetValue(0);
ds@1952
   105
			return;
ds@1952
   106
		}
ds@1952
   107
ds@1952
   108
		extra_time /= 60;
ds@1952
   109
ds@1952
   110
		mp_timelimit->SetValue(mp_timelimit->GetInt() + extra_time);
ds@1952
   111
	}
ds@1952
   112
ds@1952
   113
	void OnConVarChanged(ConVar *pConVar, const char *oldValue, float flOldValue)
ds@1952
   114
	{
ds@1952
   115
		g_Timers.MapTimeLeftChanged();
ds@1952
   116
	}
ds@1952
   117
ds@1952
   118
private:
ds@1952
   119
	void Enable()
ds@1952
   120
	{
ds@1952
   121
		g_ConVarManager.AddConVarChangeListener("mp_timelimit", this);
ds@1952
   122
	}
ds@1952
   123
ds@1952
   124
	void Disable()
ds@1952
   125
	{
ds@1952
   126
		g_ConVarManager.RemoveConVarChangeListener("mp_timelimit", this);
ds@1952
   127
	}
ds@1952
   128
ds@1952
   129
private:
ds@1952
   130
	bool m_bInUse;
ds@1952
   131
} s_DefaultMapTimer;
ds@1952
   132
ds@1952
   133
/**
ds@1952
   134
 * If the ticking process has run amok (should be impossible), we 
ds@1952
   135
 * take care of this by "skipping" the in-between time, to prevent 
ds@1952
   136
 * a bazillion times from firing on accident.  This has the result  
ds@1952
   137
 * that a drastic jump in time will continue acting normally.  Users 
ds@1952
   138
 * may not expect this, but... I think it is the best solution.
ds@1952
   139
 */
ds@1952
   140
inline double CalcNextThink(double last, float interval)
ds@1952
   141
{
ds@1952
   142
	if (g_fUniversalTime - last - interval <= TIMER_MIN_ACCURACY)
ds@1952
   143
	{
ds@1952
   144
		return last + interval;
ds@1952
   145
	}
ds@1952
   146
	else
ds@1952
   147
	{
ds@1952
   148
		return g_fUniversalTime + interval;
ds@1952
   149
	}
ds@1952
   150
}
ds@1952
   151
ds@1952
   152
void ITimer::Initialize(ITimedEvent *pCallbacks, float fInterval, float fToExec, void *pData, int flags)
ds@1952
   153
{
ds@1952
   154
	m_Listener = pCallbacks;
ds@1952
   155
	m_Interval = fInterval;
ds@1952
   156
	m_ToExec = fToExec;
ds@1952
   157
	m_pData = pData;
ds@1952
   158
	m_Flags = flags;
ds@1952
   159
	m_InExec = false;
ds@1952
   160
	m_KillMe = false;
ds@1952
   161
}
ds@1952
   162
ds@1952
   163
TimerSystem::TimerSystem()
ds@1952
   164
{
ds@1952
   165
	m_pMapTimer = NULL;
ds@1952
   166
	m_bHasMapTickedYet = false;
ds@1952
   167
	m_bHasMapSimulatedYet = false;
ds@1952
   168
	m_fLastTickedTime = 0.0f;
ds@1952
   169
}
ds@1952
   170
ds@1952
   171
TimerSystem::~TimerSystem()
ds@1952
   172
{
ds@1952
   173
	CStack<ITimer *>::iterator iter;
ds@1952
   174
	for (iter=m_FreeTimers.begin(); iter!=m_FreeTimers.end(); iter++)
ds@1952
   175
	{
ds@1952
   176
		delete (*iter);
ds@1952
   177
	}
ds@1952
   178
	m_FreeTimers.popall();
ds@1952
   179
}
ds@1952
   180
ds@1952
   181
void TimerSystem::OnSourceModAllInitialized()
ds@1952
   182
{
ds@1952
   183
	g_ShareSys.AddInterface(NULL, this);
ds@1952
   184
	m_pOnGameFrame = g_Forwards.CreateForward("OnGameFrame", ET_Ignore, 0, NULL);
ds@1952
   185
	m_pOnMapTimeLeftChanged = g_Forwards.CreateForward("OnMapTimeLeftChanged", ET_Ignore, 0, NULL);
ds@1952
   186
}
ds@1952
   187
ds@1952
   188
void TimerSystem::OnSourceModGameInitialized()
ds@1952
   189
{
ds@1952
   190
	mp_timelimit = icvar->FindVar("mp_timelimit");
ds@1952
   191
ds@1952
   192
	if (m_pMapTimer == NULL && mp_timelimit != NULL)
ds@1952
   193
	{
ds@1952
   194
		SetMapTimer(&s_DefaultMapTimer);
ds@1952
   195
	}
ds@1952
   196
}
ds@1952
   197
ds@1952
   198
void TimerSystem::OnSourceModShutdown()
ds@1952
   199
{
ds@1952
   200
	SetMapTimer(NULL);
ds@1952
   201
	g_Forwards.ReleaseForward(m_pOnGameFrame);
ds@1952
   202
	g_Forwards.ReleaseForward(m_pOnMapTimeLeftChanged);
ds@1952
   203
}
ds@1952
   204
ds@1952
   205
void TimerSystem::OnSourceModLevelEnd()
ds@1952
   206
{
ds@1952
   207
	m_bHasMapTickedYet = false;
ds@1952
   208
	m_bHasMapSimulatedYet = false;
ds@1952
   209
}
ds@1952
   210
ds@1952
   211
void TimerSystem::GameFrame(bool simulating)
ds@1952
   212
{
ds@1952
   213
	if (simulating && m_bHasMapTickedYet)
ds@1952
   214
	{
ds@1952
   215
		g_fUniversalTime += gpGlobals->curtime - m_fLastTickedTime;
ds@1952
   216
		if (!m_bHasMapSimulatedYet)
ds@1952
   217
		{
ds@1952
   218
			m_bHasMapSimulatedYet = true;
ds@1952
   219
			MapTimeLeftChanged();
ds@1952
   220
		}
ds@1952
   221
	}
ds@1952
   222
	else 
ds@1952
   223
	{
ds@1952
   224
		g_fUniversalTime += gpGlobals->interval_per_tick;
ds@1952
   225
	}
ds@1952
   226
ds@1952
   227
	m_fLastTickedTime = gpGlobals->curtime;
ds@1952
   228
	m_bHasMapTickedYet = true;
ds@1952
   229
ds@1952
   230
	if (g_fUniversalTime >= g_fTimerThink)
ds@1952
   231
	{
ds@1952
   232
		RunFrame();
ds@1952
   233
ds@1952
   234
		g_fTimerThink = CalcNextThink(g_fTimerThink, TIMER_MIN_ACCURACY);
ds@1952
   235
	}
ds@1952
   236
ds@1952
   237
	RunFrameHooks(simulating);
ds@1952
   238
ds@1952
   239
	if (m_pOnGameFrame->GetFunctionCount())
ds@1952
   240
	{
ds@1952
   241
		m_pOnGameFrame->Execute(NULL);
ds@1952
   242
	}
ds@1952
   243
}
ds@1952
   244
ds@1952
   245
void TimerSystem::RunFrame()
ds@1952
   246
{
ds@1952
   247
	ITimer *pTimer;
ds@1952
   248
	TimerIter iter;
ds@1952
   249
ds@1952
   250
	double curtime = GetSimulatedTime();
ds@1952
   251
	for (iter=m_SingleTimers.begin(); iter!=m_SingleTimers.end(); )
ds@1952
   252
	{
ds@1952
   253
		pTimer = (*iter);
ds@1952
   254
		if (curtime >= pTimer->m_ToExec)
ds@1952
   255
		{
ds@1952
   256
			pTimer->m_InExec = true;
ds@1952
   257
			pTimer->m_Listener->OnTimer(pTimer, pTimer->m_pData);
ds@1952
   258
			pTimer->m_Listener->OnTimerEnd(pTimer, pTimer->m_pData);
ds@1952
   259
			iter = m_SingleTimers.erase(iter);
ds@1952
   260
			m_FreeTimers.push(pTimer);
ds@1952
   261
		} 
ds@1952
   262
		else 
ds@1952
   263
		{
ds@1952
   264
			break;
ds@1952
   265
		}
ds@1952
   266
	}
ds@1952
   267
ds@1952
   268
	ResultType res;
ds@1952
   269
	for (iter=m_LoopTimers.begin(); iter!=m_LoopTimers.end(); )
ds@1952
   270
	{
ds@1952
   271
		pTimer = (*iter);
ds@1952
   272
		if (curtime >= pTimer->m_ToExec)
ds@1952
   273
		{
ds@1952
   274
			pTimer->m_InExec = true;
ds@1952
   275
			res = pTimer->m_Listener->OnTimer(pTimer, pTimer->m_pData);
ds@1952
   276
			if (pTimer->m_KillMe || (res == Pl_Stop))
ds@1952
   277
			{
ds@1952
   278
				pTimer->m_Listener->OnTimerEnd(pTimer, pTimer->m_pData);
ds@1952
   279
				iter = m_LoopTimers.erase(iter);
ds@1952
   280
				m_FreeTimers.push(pTimer);
ds@1952
   281
				continue;
ds@1952
   282
			}
ds@1952
   283
			pTimer->m_InExec = false;
ds@1952
   284
			pTimer->m_ToExec = CalcNextThink(pTimer->m_ToExec, pTimer->m_Interval);
ds@1952
   285
		}
ds@1952
   286
		iter++;
ds@1952
   287
	}
ds@1952
   288
}
ds@1952
   289
ds@1952
   290
ITimer *TimerSystem::CreateTimer(ITimedEvent *pCallbacks, float fInterval, void *pData, int flags)
ds@1952
   291
{
ds@1952
   292
	ITimer *pTimer;
ds@1952
   293
	TimerIter iter;
ds@1952
   294
	float to_exec = GetSimulatedTime() + fInterval;
ds@1952
   295
ds@1952
   296
	if (m_FreeTimers.empty())
ds@1952
   297
	{
ds@1952
   298
		pTimer = new ITimer;
ds@1952
   299
	} else {
ds@1952
   300
		pTimer = m_FreeTimers.front();
ds@1952
   301
		m_FreeTimers.pop();
ds@1952
   302
	}
ds@1952
   303
ds@1952
   304
	pTimer->Initialize(pCallbacks, fInterval, to_exec, pData, flags);
ds@1952
   305
ds@1952
   306
	if (flags & TIMER_FLAG_REPEAT)
ds@1952
   307
	{
ds@1952
   308
		m_LoopTimers.push_back(pTimer);
ds@1952
   309
		goto return_timer;
ds@1952
   310
	}
ds@1952
   311
ds@1952
   312
	if (m_SingleTimers.size() >= 1)
ds@1952
   313
	{
ds@1952
   314
		iter = --m_SingleTimers.end();
ds@1952
   315
		if ((*iter)->m_ToExec <= to_exec)
ds@1952
   316
		{
ds@1952
   317
			goto normal_insert_end;
ds@1952
   318
		}
ds@1952
   319
	}
ds@1952
   320
ds@1952
   321
	for (iter=m_SingleTimers.begin(); iter!=m_SingleTimers.end(); iter++)
ds@1952
   322
	{
ds@1952
   323
		if ((*iter)->m_ToExec >= to_exec)
ds@1952
   324
		{
ds@1952
   325
			m_SingleTimers.insert(iter, pTimer);
ds@1952
   326
			goto return_timer;
ds@1952
   327
		}
ds@1952
   328
	}
ds@1952
   329
ds@1952
   330
normal_insert_end:
ds@1952
   331
	m_SingleTimers.push_back(pTimer);
ds@1952
   332
ds@1952
   333
return_timer:
ds@1952
   334
	return pTimer;
ds@1952
   335
}
ds@1952
   336
ds@1952
   337
void TimerSystem::FireTimerOnce(ITimer *pTimer, bool delayExec)
ds@1952
   338
{
ds@1952
   339
	ResultType res;
ds@1952
   340
ds@1952
   341
	if (pTimer->m_InExec)
ds@1952
   342
	{
ds@1952
   343
		return;
ds@1952
   344
	}
ds@1952
   345
ds@1952
   346
	pTimer->m_InExec = true;
ds@1952
   347
	res = pTimer->m_Listener->OnTimer(pTimer, pTimer->m_pData);
ds@1952
   348
ds@1952
   349
	if (!(pTimer->m_Flags & TIMER_FLAG_REPEAT))
ds@1952
   350
	{
ds@1952
   351
		pTimer->m_Listener->OnTimerEnd(pTimer, pTimer->m_pData);
ds@1952
   352
		m_SingleTimers.remove(pTimer);
ds@1952
   353
		m_FreeTimers.push(pTimer);
ds@1952
   354
	} 
ds@1952
   355
	else 
ds@1952
   356
	{
ds@1952
   357
		if ((res != Pl_Stop) && !pTimer->m_KillMe)
ds@1952
   358
		{
ds@1952
   359
			if (delayExec)
ds@1952
   360
			{
ds@1952
   361
				pTimer->m_ToExec = GetSimulatedTime() + pTimer->m_Interval;
ds@1952
   362
			}
ds@1952
   363
			pTimer->m_InExec = false;
ds@1952
   364
			return;
ds@1952
   365
		}
ds@1952
   366
		pTimer->m_Listener->OnTimerEnd(pTimer, pTimer->m_pData);
ds@1952
   367
		m_LoopTimers.remove(pTimer);
ds@1952
   368
		m_FreeTimers.push(pTimer);
ds@1952
   369
	}
ds@1952
   370
}
ds@1952
   371
ds@1952
   372
void TimerSystem::KillTimer(ITimer *pTimer)
ds@1952
   373
{
ds@1952
   374
	if (pTimer->m_KillMe)
ds@1952
   375
	{
ds@1952
   376
		return;
ds@1952
   377
	}
ds@1952
   378
ds@1952
   379
	if (pTimer->m_InExec)
ds@1952
   380
	{
ds@1952
   381
		pTimer->m_KillMe = true;
ds@1952
   382
		return;
ds@1952
   383
	}
ds@1952
   384
ds@1952
   385
	pTimer->m_InExec = true; /* The timer it's not really executed but this check needs to be done */
ds@1952
   386
	pTimer->m_Listener->OnTimerEnd(pTimer, pTimer->m_pData);
ds@1952
   387
ds@1952
   388
	if (pTimer->m_Flags & TIMER_FLAG_REPEAT)
ds@1952
   389
	{
ds@1952
   390
		m_LoopTimers.remove(pTimer);
ds@1952
   391
	} else {
ds@1952
   392
		m_SingleTimers.remove(pTimer);
ds@1952
   393
	}
ds@1952
   394
ds@1952
   395
	m_FreeTimers.push(pTimer);
ds@1952
   396
}
ds@1952
   397
ds@1952
   398
CStack<ITimer *> s_tokill;
ds@1952
   399
void TimerSystem::RemoveMapChangeTimers()
ds@1952
   400
{
ds@1952
   401
	ITimer *pTimer;
ds@1952
   402
	TimerIter iter;
ds@1952
   403
ds@1952
   404
	for (iter=m_SingleTimers.begin(); iter!=m_SingleTimers.end(); iter++)
ds@1952
   405
	{
ds@1952
   406
		pTimer = (*iter);
ds@1952
   407
		if (pTimer->m_Flags & TIMER_FLAG_NO_MAPCHANGE)
ds@1952
   408
		{
ds@1952
   409
			s_tokill.push(pTimer);
ds@1952
   410
		}
ds@1952
   411
	}
ds@1952
   412
ds@1952
   413
	for (iter=m_LoopTimers.begin(); iter!=m_LoopTimers.end(); iter++)
ds@1952
   414
	{
ds@1952
   415
		pTimer = (*iter);
ds@1952
   416
		if (pTimer->m_Flags & TIMER_FLAG_NO_MAPCHANGE)
ds@1952
   417
		{
ds@1952
   418
			s_tokill.push(pTimer);
ds@1952
   419
		}
ds@1952
   420
	}
ds@1952
   421
ds@1952
   422
	while (!s_tokill.empty())
ds@1952
   423
	{
ds@1952
   424
		KillTimer(s_tokill.front());
ds@1952
   425
		s_tokill.pop();
ds@1952
   426
	}
ds@1952
   427
}
ds@1952
   428
ds@1952
   429
IMapTimer *TimerSystem::SetMapTimer(IMapTimer *pTimer)
ds@1952
   430
{
ds@1952
   431
	IMapTimer *old = m_pMapTimer;
ds@1952
   432
ds@1952
   433
	m_pMapTimer = pTimer;
ds@1952
   434
ds@1952
   435
	if (m_pMapTimer)
ds@1952
   436
	{
ds@1952
   437
		m_pMapTimer->SetMapTimerStatus(true);
ds@1952
   438
	}
ds@1952
   439
ds@1952
   440
	if (old)
ds@1952
   441
	{
ds@1952
   442
		old->SetMapTimerStatus(false);
ds@1952
   443
	}
ds@1952
   444
ds@1952
   445
	return old;
ds@1952
   446
}
ds@1952
   447
ds@1952
   448
IMapTimer *TimerSystem::GetMapTimer()
ds@1952
   449
{
ds@1952
   450
	return m_pMapTimer;
ds@1952
   451
}
ds@1952
   452
ds@1952
   453
void TimerSystem::MapTimeLeftChanged()
ds@1952
   454
{
ds@1952
   455
	m_pOnMapTimeLeftChanged->Execute(NULL);
ds@1952
   456
}
ds@1952
   457
ds@1952
   458
void TimerSystem::NotifyOfGameStart(float offset)
ds@1952
   459
{
ds@1952
   460
	g_fGameStartTime = gpGlobals->curtime + offset;
ds@1952
   461
}
ds@1952
   462
ds@1952
   463
float TimerSystem::GetTickedTime()
ds@1952
   464
{
ds@1952
   465
	return g_fUniversalTime;
ds@1952
   466
}
ds@1952
   467
ds@1952
   468
bool TimerSystem::GetMapTimeLeft(float *time_left)
ds@1952
   469
{
ds@1952
   470
	if (!m_pMapTimer)
ds@1952
   471
	{
ds@1952
   472
		return false;
ds@1952
   473
	}
ds@1952
   474
ds@1952
   475
	int time_limit;
ds@1952
   476
	if (!m_bHasMapSimulatedYet || (time_limit = m_pMapTimer->GetMapTimeLimit()) < 1)
ds@1952
   477
	{
ds@1952
   478
		*time_left = -1.0f;
ds@1952
   479
	}
ds@1952
   480
	else
ds@1952
   481
	{
ds@1952
   482
		*time_left = (g_fGameStartTime + time_limit * 60.0f) - gpGlobals->curtime;
ds@1952
   483
	}
ds@1952
   484
ds@1952
   485
	return true;
ds@1952
   486
}
ds@1952
   487