Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

346 lines
5.8 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #ifndef SIMTIMER_H
  8. #define SIMTIMER_H
  9. #if defined( _WIN32 )
  10. #pragma once
  11. #endif
  12. #define ST_EPS 0.001
  13. #define DEFINE_SIMTIMER( type, name ) DEFINE_EMBEDDED( type, name )
  14. //-----------------------------------------------------------------------------
  15. class CSimpleSimTimer
  16. {
  17. public:
  18. CSimpleSimTimer()
  19. : m_next( -1 )
  20. {
  21. }
  22. void Force()
  23. {
  24. m_next = -1;
  25. }
  26. bool Expired() const
  27. {
  28. return ( gpGlobals->curtime - m_next > -ST_EPS );
  29. }
  30. float Delay( float delayTime )
  31. {
  32. return (m_next += delayTime);
  33. }
  34. float GetNext() const
  35. {
  36. return m_next;
  37. }
  38. void Set( float interval )
  39. {
  40. m_next = gpGlobals->curtime + interval;
  41. }
  42. void Set( float minInterval, float maxInterval )
  43. {
  44. if ( maxInterval > 0.0 )
  45. m_next = gpGlobals->curtime + random->RandomFloat( minInterval, maxInterval );
  46. else
  47. m_next = gpGlobals->curtime + minInterval;
  48. }
  49. float GetRemaining() const
  50. {
  51. float result = m_next - gpGlobals->curtime;
  52. if (result < 0 )
  53. return 0;
  54. return result;
  55. }
  56. DECLARE_SIMPLE_DATADESC();
  57. protected:
  58. float m_next;
  59. };
  60. //-----------------------------------------------------------------------------
  61. class CSimTimer : public CSimpleSimTimer
  62. {
  63. public:
  64. CSimTimer( float interval = 0.0, bool startExpired = true )
  65. {
  66. Set( interval, startExpired );
  67. }
  68. void Set( float interval, bool startExpired = true )
  69. {
  70. m_interval = interval;
  71. m_next = (startExpired) ? -1.0 : gpGlobals->curtime + m_interval;
  72. }
  73. void Reset( float interval = -1.0 )
  74. {
  75. if ( interval == -1.0 )
  76. {
  77. m_next = gpGlobals->curtime + m_interval;
  78. }
  79. else
  80. {
  81. m_next = gpGlobals->curtime + interval;
  82. }
  83. }
  84. float GetInterval() const
  85. {
  86. return m_interval;
  87. }
  88. DECLARE_SIMPLE_DATADESC();
  89. private:
  90. float m_interval;
  91. };
  92. //-----------------------------------------------------------------------------
  93. //-----------------------------------------------------------------------------
  94. class CRandSimTimer : public CSimpleSimTimer
  95. {
  96. public:
  97. CRandSimTimer( float minInterval = 0.0, float maxInterval = 0.0, bool startExpired = true )
  98. {
  99. Set( minInterval, maxInterval, startExpired );
  100. }
  101. void Set( float minInterval, float maxInterval = 0.0, bool startExpired = true )
  102. {
  103. m_minInterval = minInterval;
  104. m_maxInterval = maxInterval;
  105. if (startExpired)
  106. {
  107. m_next = -1;
  108. }
  109. else
  110. {
  111. if ( m_maxInterval == 0 )
  112. m_next = gpGlobals->curtime + m_minInterval;
  113. else
  114. m_next = gpGlobals->curtime + random->RandomFloat( m_minInterval, m_maxInterval );
  115. }
  116. }
  117. void Reset()
  118. {
  119. if ( m_maxInterval == 0 )
  120. m_next = gpGlobals->curtime + m_minInterval;
  121. else
  122. m_next = gpGlobals->curtime + random->RandomFloat( m_minInterval, m_maxInterval );
  123. }
  124. float GetMinInterval() const
  125. {
  126. return m_minInterval;
  127. }
  128. float GetMaxInterval() const
  129. {
  130. return m_maxInterval;
  131. }
  132. DECLARE_SIMPLE_DATADESC();
  133. private:
  134. float m_minInterval;
  135. float m_maxInterval;
  136. };
  137. //-----------------------------------------------------------------------------
  138. class CStopwatchBase : public CSimpleSimTimer
  139. {
  140. public:
  141. CStopwatchBase()
  142. {
  143. m_fIsRunning = false;
  144. }
  145. bool IsRunning() const
  146. {
  147. return m_fIsRunning;
  148. }
  149. void Stop()
  150. {
  151. m_fIsRunning = false;
  152. }
  153. bool Expired() const
  154. {
  155. return ( m_fIsRunning && CSimpleSimTimer::Expired() );
  156. }
  157. DECLARE_SIMPLE_DATADESC();
  158. protected:
  159. bool m_fIsRunning;
  160. };
  161. //-------------------------------------
  162. class CSimpleStopwatch : public CStopwatchBase
  163. {
  164. public:
  165. void Start( float minCountdown, float maxCountdown = 0.0 )
  166. {
  167. m_fIsRunning = true;
  168. CSimpleSimTimer::Set( minCountdown, maxCountdown );
  169. }
  170. void Stop()
  171. {
  172. m_fIsRunning = false;
  173. }
  174. bool Expired() const
  175. {
  176. return ( m_fIsRunning && CSimpleSimTimer::Expired() );
  177. }
  178. };
  179. //-------------------------------------
  180. class CStopwatch : public CStopwatchBase
  181. {
  182. public:
  183. CStopwatch ( float interval = 0.0 )
  184. {
  185. Set( interval );
  186. }
  187. void Set( float interval )
  188. {
  189. m_interval = interval;
  190. }
  191. void Start( float intervalOverride )
  192. {
  193. m_fIsRunning = true;
  194. m_next = gpGlobals->curtime + intervalOverride;
  195. }
  196. void Start()
  197. {
  198. Start( m_interval );
  199. }
  200. float GetInterval() const
  201. {
  202. return m_interval;
  203. }
  204. DECLARE_SIMPLE_DATADESC();
  205. private:
  206. float m_interval;
  207. };
  208. //-------------------------------------
  209. class CRandStopwatch : public CStopwatchBase
  210. {
  211. public:
  212. CRandStopwatch( float minInterval = 0.0, float maxInterval = 0.0 )
  213. {
  214. Set( minInterval, maxInterval );
  215. }
  216. void Set( float minInterval, float maxInterval = 0.0 )
  217. {
  218. m_minInterval = minInterval;
  219. m_maxInterval = maxInterval;
  220. }
  221. void Start( float minOverride, float maxOverride = 0.0 )
  222. {
  223. m_fIsRunning = true;
  224. if ( maxOverride == 0 )
  225. m_next = gpGlobals->curtime + minOverride;
  226. else
  227. m_next = gpGlobals->curtime + random->RandomFloat( minOverride, maxOverride );
  228. }
  229. void Start()
  230. {
  231. Start( m_minInterval, m_maxInterval );
  232. }
  233. float GetInterval() const
  234. {
  235. return m_minInterval;
  236. }
  237. float GetMinInterval() const
  238. {
  239. return m_minInterval;
  240. }
  241. float GetMaxInterval() const
  242. {
  243. return m_maxInterval;
  244. }
  245. DECLARE_SIMPLE_DATADESC();
  246. private:
  247. float m_minInterval;
  248. float m_maxInterval;
  249. };
  250. //-----------------------------------------------------------------------------
  251. class CThinkOnceSemaphore
  252. {
  253. public:
  254. CThinkOnceSemaphore()
  255. : m_lastTime( -1 )
  256. {
  257. }
  258. bool EnterThink()
  259. {
  260. if ( m_lastTime == gpGlobals->curtime )
  261. return false;
  262. m_lastTime = gpGlobals->curtime;
  263. return true;
  264. }
  265. bool DidThink() const
  266. {
  267. return ( gpGlobals->curtime == m_lastTime );
  268. }
  269. void SetDidThink()
  270. {
  271. m_lastTime = gpGlobals->curtime;
  272. }
  273. private:
  274. float m_lastTime;
  275. };
  276. //-----------------------------------------------------------------------------
  277. #endif // SIMTIMER_H