core/TimerSys.cpp
changeset 1412 a2ca3d22dd71
parent 1410 8feec8371000
child 1414 bb258bffc7df
     1.1 --- a/core/TimerSys.cpp	Wed Sep 12 07:22:38 2007 +0000
     1.2 +++ b/core/TimerSys.cpp	Wed Sep 12 22:46:00 2007 +0000
     1.3 @@ -31,13 +31,20 @@
     1.4  
     1.5  #include <time.h>
     1.6  #include "TimerSys.h"
     1.7 +#include "ForwardSys.h"
     1.8  #include "sourcemm_api.h"
     1.9 +#include "frame_hooks.h"
    1.10  
    1.11  TimerSystem g_Timers;
    1.12 +float g_fUniversalTime = 0.0f;
    1.13 +const float *g_pUniversalTime = &g_fUniversalTime;
    1.14  
    1.15  ConVar sm_time_adjustment("sm_time_adjustment", "0", 0, "Adjusts the server time in seconds");
    1.16  
    1.17 -__BUILD_INTENTIONALLY_BROKEN_BY_BAIL__
    1.18 +inline float GetSimulatedTime()
    1.19 +{
    1.20 +	return g_fUniversalTime;
    1.21 +}
    1.22  
    1.23  time_t GetAdjustedTime(time_t *buf)
    1.24  {
    1.25 @@ -49,11 +56,6 @@
    1.26  	return val;
    1.27  }
    1.28  
    1.29 -inline float GetSimulatedTime()
    1.30 -{
    1.31 -	return engine->Time();
    1.32 -}
    1.33 -
    1.34  void ITimer::Initialize(ITimedEvent *pCallbacks, float fInterval, float fToExec, void *pData, int flags)
    1.35  {
    1.36  	m_Listener = pCallbacks;
    1.37 @@ -68,6 +70,9 @@
    1.38  TimerSystem::TimerSystem()
    1.39  {
    1.40  	m_fnTimeLeft = NULL;
    1.41 +	m_bHasMapTickedYet = false;
    1.42 +	m_fLastTickedTime = 0.0f;
    1.43 +	m_LastExecTime = 0.0f;
    1.44  }
    1.45  
    1.46  TimerSystem::~TimerSystem()
    1.47 @@ -83,6 +88,12 @@
    1.48  void TimerSystem::OnSourceModAllInitialized()
    1.49  {
    1.50  	g_ShareSys.AddInterface(NULL, this);
    1.51 +	m_pOnGameFrame = g_Forwards.CreateForward("OnGameFrame", ET_Ignore, 0, NULL);
    1.52 +}
    1.53 +
    1.54 +void TimerSystem::OnSourceModShutdown()
    1.55 +{
    1.56 +	g_Forwards.ReleaseForward(m_pOnGameFrame);
    1.57  }
    1.58  
    1.59  void TimerSystem::OnSourceModLevelChange(const char *mapName)
    1.60 @@ -90,6 +101,38 @@
    1.61  	MapChange(true);
    1.62  }
    1.63  
    1.64 +void TimerSystem::OnSourceModLevelEnd()
    1.65 +{
    1.66 +	m_bHasMapTickedYet = false;
    1.67 +}
    1.68 +
    1.69 +void TimerSystem::GameFrame(bool simulating)
    1.70 +{
    1.71 +	if (simulating && m_bHasMapTickedYet)
    1.72 +	{
    1.73 +		g_fUniversalTime += gpGlobals->curtime - m_fLastTickedTime;
    1.74 +	}
    1.75 +	else 
    1.76 +	{
    1.77 +		g_fUniversalTime += gpGlobals->interval_per_tick;
    1.78 +	}
    1.79 +
    1.80 +	m_fLastTickedTime = gpGlobals->curtime;
    1.81 +	m_bHasMapTickedYet = true;
    1.82 +
    1.83 +	if (g_fUniversalTime - m_LastExecTime >= 0.1)
    1.84 +	{
    1.85 +		RunFrame();
    1.86 +	}
    1.87 +
    1.88 +	RunFrameHooks();
    1.89 +
    1.90 +	if (m_pOnGameFrame->GetFunctionCount())
    1.91 +	{
    1.92 +		m_pOnGameFrame->Execute(NULL);
    1.93 +	}
    1.94 +}
    1.95 +
    1.96  void TimerSystem::RunFrame()
    1.97  {
    1.98  	ITimer *pTimer;
    1.99 @@ -299,8 +342,6 @@
   1.100  		if (real_mapchange && (pTimer->m_Flags & TIMER_FLAG_NO_MAPCHANGE))
   1.101  		{
   1.102  			s_tokill.push(pTimer);
   1.103 -		} else {
   1.104 -			pTimer->m_ToExec = pTimer->m_ToExec - m_LastExecTime + GetSimulatedTime();
   1.105  		}
   1.106  	}
   1.107  
   1.108 @@ -310,8 +351,6 @@
   1.109  		if (real_mapchange && (pTimer->m_Flags & TIMER_FLAG_NO_MAPCHANGE))
   1.110  		{
   1.111  			s_tokill.push(pTimer);
   1.112 -		} else {
   1.113 -			pTimer->m_ToExec = pTimer->m_ToExec - m_LastExecTime + GetSimulatedTime();
   1.114  		}
   1.115  	}
   1.116  
   1.117 @@ -329,8 +368,6 @@
   1.118  		KillTimer(s_tokill.front());
   1.119  		s_tokill.pop();
   1.120  	}
   1.121 -
   1.122 -	m_LastExecTime = GetSimulatedTime();
   1.123  }
   1.124  
   1.125  SM_TIMELEFT_FUNCTION TimerSystem::SetTimeLeftFunction(SM_TIMELEFT_FUNCTION fn)