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.

273 lines
8.7 KiB

  1. //========= Copyright © 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: Implements visual effects entities: sprites, beams, bubbles, etc.
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #ifndef ENV_WIND_SHARED_H
  8. #define ENV_WIND_SHARED_H
  9. #include "utllinkedlist.h"
  10. #include "vstdlib/random.h"
  11. #include "tier0/dbg.h"
  12. #include "mathlib/vector.h"
  13. #include <float.h>
  14. //-----------------------------------------------------------------------------
  15. // Forward declarations
  16. //-----------------------------------------------------------------------------
  17. class CSoundPatch;
  18. //-----------------------------------------------------------------------------
  19. // Class used to help store events that occurred over time
  20. //-----------------------------------------------------------------------------
  21. template <class T, class I>
  22. class CTimedEventQueue
  23. {
  24. public:
  25. // The time passed in here represents the amount of time the queue stores
  26. CTimedEventQueue( float flMaxTime );
  27. // REI: Not sure why I have to declare these as delete, shouldn't that be inherited from CUtlLinkedList having a deleted copy constructor?
  28. CTimedEventQueue( const CTimedEventQueue& ) = delete;
  29. CTimedEventQueue& operator= ( const CTimedEventQueue& ) = delete;
  30. // Adds an event to the queue, will pop off stale events from the queue
  31. // NOTE: All events added to the queue must monotonically increase in time!
  32. I PushEvent( float flTime, const T &data );
  33. // Grabs the last event that happened before or at the specified time
  34. I GetEventIndex( float flTime ) const;
  35. // Gets event information
  36. float GetEventTime( I i ) const;
  37. const T &GetEventData( I i ) const;
  38. private:
  39. struct QueueEntry_t
  40. {
  41. float m_flTime;
  42. T m_Data;
  43. };
  44. float m_flQueueHeadTime;
  45. float m_flMaxTime;
  46. CUtlLinkedList< T, I > m_Queue;
  47. };
  48. //-----------------------------------------------------------------------------
  49. // The time passed in here represents the amount of time the queue stores
  50. //-----------------------------------------------------------------------------
  51. template <class T, class I>
  52. CTimedEventQueue<T,I>::CTimedEventQueue( float flMaxTime ) : m_flMaxTime(flMaxTime)
  53. {
  54. // The length of time of events in the queue must be reasonable
  55. Assert( m_flMaxTime > 0.0f );
  56. m_flQueueHeadTime = -FLT_MAX;
  57. }
  58. //-----------------------------------------------------------------------------
  59. // Adds an event to the queue, will pop off stale events from the queue
  60. //-----------------------------------------------------------------------------
  61. template <class T, class I>
  62. I CTimedEventQueue<T,I>::PushEvent( float flTime, const T &data )
  63. {
  64. Assert( m_flQueueHeadTime <= flTime );
  65. m_flQueueHeadTime = flTime;
  66. // First push the event...
  67. I idx = m_Queue.AddToHead();
  68. m_Queue[idx].m_flTime = flTime;
  69. m_Queue[idx].m_Data = data;
  70. // Then retire stale events...
  71. I i = m_Queue.Tail();
  72. while (m_Queue[i].m_flTime < m_flQueueHeadTime - m_flMaxTime )
  73. {
  74. I prev = m_Queue.Prev(i);
  75. Assert( prev != m_Queue.InvalidIndex() );
  76. m_Queue.Remove(i);
  77. i = prev;
  78. }
  79. return idx;
  80. }
  81. //-----------------------------------------------------------------------------
  82. // Grabs the last event that happened before or at the specified time
  83. //-----------------------------------------------------------------------------
  84. template <class T, class I>
  85. I CTimedEventQueue<T,I>::GetEventIndex( float flTime ) const
  86. {
  87. // This checks for a request that fell off the queue
  88. Assert( (flTime >= m_flQueueHeadTime - m_flMaxTime) && (flTime <= m_flQueueHeadTime) );
  89. // Then retire stale events...
  90. I i = m_Queue.Head();
  91. while( m_Queue[i].m_flTime > flTime )
  92. {
  93. i = m_Queue.Next(i);
  94. Assert( i != m_Queue.InvalidIndex() );
  95. }
  96. return i;
  97. }
  98. //-----------------------------------------------------------------------------
  99. // Gets event information
  100. //-----------------------------------------------------------------------------
  101. template <class T, class I>
  102. inline float CTimedEventQueue<T,I>::GetEventTime( I i ) const
  103. {
  104. return m_Queue[i].m_flTime;
  105. }
  106. template <class T, class I>
  107. inline const T &CTimedEventQueue<T,I>::GetEventData( I i ) const
  108. {
  109. return m_Queue[i].m_Data;
  110. }
  111. //-----------------------------------------------------------------------------
  112. // Implementation of the class that computes windspeed
  113. //-----------------------------------------------------------------------------
  114. class CEnvWindShared
  115. {
  116. public:
  117. DECLARE_CLASS_NOBASE( CEnvWindShared );
  118. DECLARE_EMBEDDED_NETWORKVAR();
  119. CEnvWindShared();
  120. ~CEnvWindShared();
  121. CEnvWindShared( const CEnvWindShared & ) = delete;
  122. CEnvWindShared& operator= ( const CEnvWindShared& ) = delete;
  123. void Init( int iEntIndex, int iRandomSeed, float flTime, int iWindDir, float flInitialWindSpeed );
  124. void SetLocation( const Vector &location );
  125. // Method to update the wind speed
  126. // Time passed in here is global time, not delta time
  127. // The function returns the time at which it must be called again
  128. float WindThink( float flTime );
  129. // FIXME: These really should be private
  130. CNetworkVar( float, m_flStartTime );
  131. CNetworkVar( int, m_iWindSeed ); // random number seed...
  132. CNetworkVar( int, m_iMinWind ); // the slowest the wind can normally blow
  133. CNetworkVar( int, m_iMaxWind ); // the fastest the wind can normally blow
  134. CNetworkVar( int, m_windRadius ); // the radius this entity affects with its windiness, so a map can have multiple
  135. CNetworkVar( int, m_iMinGust ); // the slowest that a gust can be
  136. CNetworkVar( int, m_iMaxGust ); // the fastest that a gust can be
  137. CNetworkVar( float, m_flMinGustDelay ); // min time between gusts
  138. CNetworkVar( float, m_flMaxGustDelay ); // max time between gusts
  139. CNetworkVar( float, m_flGustDuration ); // max time between gusts
  140. CNetworkVar( int, m_iGustDirChange ); // max number of degrees wind dir changes on gusts.
  141. CNetworkVector( m_location ); // The location of this wind controller
  142. int m_iszGustSound; // name of the wind sound to play for gusts.
  143. int m_iWindDir; // wind direction (yaw)
  144. float m_flWindSpeed; // the wind speed
  145. Vector m_currentWindVector; // For all the talk of proper prediction, we ended up just storing and returning through a static vector. Now we can have multiple env_wind, so we need this in here.
  146. Vector m_CurrentSwayVector;
  147. Vector m_PrevSwayVector;
  148. CNetworkVar( int, m_iInitialWindDir );
  149. CNetworkVar( float, m_flInitialWindSpeed );
  150. #ifndef CLIENT_DLL
  151. COutputEvent m_OnGustStart;
  152. COutputEvent m_OnGustEnd;
  153. #endif
  154. private:
  155. struct WindAveEvent_t
  156. {
  157. float m_flStartWindSpeed; // the wind speed at the time of the event
  158. float m_flAveWindSpeed; // the average wind speed of the event
  159. };
  160. struct WindVariationEvent_t
  161. {
  162. float m_flWindAngleVariation;
  163. float m_flWindSpeedVariation;
  164. };
  165. void ComputeWindVariation( float flTime );
  166. // Updates the wind sound
  167. void UpdateWindSound( float flTotalWindSpeed );
  168. void UpdateTreeSway( float flTime );
  169. float m_flVariationTime;
  170. float m_flSwayTime;
  171. float m_flSimTime; // What's the time I last simulated up to?
  172. float m_flSwitchTime; // when do I actually switch from gust to not gust
  173. float m_flAveWindSpeed; // the average wind speed
  174. bool m_bGusting; // is the wind gusting right now?
  175. float m_flWindAngleVariation;
  176. float m_flWindSpeedVariation;
  177. int m_iEntIndex;
  178. // Used to generate random numbers
  179. CUniformRandomStream m_Stream;
  180. // NOTE: In order to make this algorithm independent of calling frequency
  181. // I have to decouple the stream used to generate average wind speed
  182. // and the stream used to generate wind variation since they are
  183. // simulated using different timesteps
  184. CUniformRandomStream m_WindVariationStream;
  185. // Used to generate the wind sound...
  186. CSoundPatch *m_pWindSound;
  187. // Event history required for prediction
  188. CTimedEventQueue< WindAveEvent_t, unsigned short > m_WindAveQueue;
  189. CTimedEventQueue< WindVariationEvent_t, unsigned short > m_WindVariationQueue;
  190. };
  191. //-----------------------------------------------------------------------------
  192. inline void CEnvWindShared::SetLocation( const Vector &location )
  193. {
  194. m_location = location;
  195. }
  196. //-----------------------------------------------------------------------------
  197. // Method to sample the wind speed at a particular location
  198. //-----------------------------------------------------------------------------
  199. Vector GetWindspeedAtLocation( const Vector &location );
  200. //-----------------------------------------------------------------------------
  201. // Method to sample the windspeed at a particular time
  202. //-----------------------------------------------------------------------------
  203. void GetWindspeedAtTime( float flTime, Vector &vecVelocity );
  204. //-----------------------------------------------------------------------------
  205. // Method to reset wind speed..
  206. //-----------------------------------------------------------------------------
  207. void ResetWindspeed();
  208. #endif // ENV_WIND_SHARED_H