Team Fortress 2 Source Code as on 22/4/2020
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.

293 lines
11 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. // Information about algorithmic stuff that can occur on both client + server
  5. //
  6. // In order to reduce network traffic, it's possible to create a algorithms
  7. // that will work on both the client and the server and be totally repeatable.
  8. // All we need do is to send down initial conditions and let the algorithm
  9. // compute the values at various times. Note that this algorithm will be called
  10. // at different times with different frequencies on the client and server.
  11. //
  12. // The trick here is that in order for it to be repeatable, the algorithm either
  13. // cannot depend on random numbers, or, if it does, we need to make sure that
  14. // the random numbers generated are effectively done at the beginning of time,
  15. // so that differences in frame rate on client and server won't matter. It also
  16. // is important that the initial state sent across the network is identical
  17. // bitwise so that we produce the exact same results. Therefore no compression
  18. // should be used in the datatables.
  19. //
  20. // Note also that each algorithm must have its own random number stream so that
  21. // it cannot possibly interact with other code using random numbers that will
  22. // be called at various different intervals on the client + server. Use the
  23. // CUniformRandomStream class for this.
  24. //
  25. // There are two types of client-server neutral code: Code that doesn't interact
  26. // with player prediction, and code that does. The code that doesn't interact
  27. // with player prediction simply has to be able to produce the result f(time)
  28. // where time is monotonically increasing. For prediction, we have to produce
  29. // the result f(time) where time does *not* monotonically increase (time can be
  30. // anywhere between the "current" time and the prior 10 seconds).
  31. //
  32. // Code that is not used by player prediction can maintain state because later
  33. // calls will always compute the value at some future time. This computation can
  34. // use random number generation, but with the following restriction: Your code
  35. // must generate exactly the same number of random numbers regardless of how
  36. // frequently the code is called.
  37. //
  38. // In specific, this means that all random numbers used must either be computed
  39. // at init time, or must be used in an 'event-based form'. Namely, use random
  40. // numbers to compute the time at which events occur and the random inputs for
  41. // those events. When simulating forward, you must simulate all intervening
  42. // time and generate the same number of random numbers.
  43. //
  44. // For functions planned to be used by player prediction, one method is to use
  45. // some sort of stateless computation (where the only states are the initial
  46. // state and time). Note that random number generators have state implicit in
  47. // the number of calls made to that random number generator, and therefore you
  48. // cannot call a random number generator unless you are able to
  49. //
  50. // 1) Use a random number generator that can return the ith random number, namely:
  51. //
  52. // float r = random( i ); // i == the ith number in the random sequence
  53. //
  54. // 2) Be able to accurately know at any given time t how many random numbers
  55. // have already been generated (namely, compute the i in part 1 above).
  56. //
  57. // There is another alternative for code meant to be used by player prediction:
  58. // you could just store a history of 'events' from which you could completely
  59. // determine the value of f(time). That history would need to be at least 10
  60. // seconds long, which is guaranteed to be longer than the amount of time that
  61. // prediction would need. I've written a class which I haven't tested yet (but
  62. // will be using soon) called CTimedEventQueue (currently located in
  63. // env_wind_shared.h) which I plan to use to solve my problem (getting wind to
  64. // blow players).
  65. //
  66. //=============================================================================//
  67. #include "cbase.h"
  68. #include "env_wind_shared.h"
  69. #include "soundenvelope.h"
  70. #include "IEffects.h"
  71. #include "engine/IEngineSound.h"
  72. #include "sharedInterface.h"
  73. // memdbgon must be the last include file in a .cpp file!!!
  74. #include "tier0/memdbgon.h"
  75. //-----------------------------------------------------------------------------
  76. // globals
  77. //-----------------------------------------------------------------------------
  78. static Vector s_vecWindVelocity( 0, 0, 0 );
  79. CEnvWindShared::CEnvWindShared() : m_WindAveQueue(10), m_WindVariationQueue(10)
  80. {
  81. m_pWindSound = NULL;
  82. }
  83. CEnvWindShared::~CEnvWindShared()
  84. {
  85. if (m_pWindSound)
  86. {
  87. CSoundEnvelopeController::GetController().Shutdown( m_pWindSound );
  88. }
  89. }
  90. void CEnvWindShared::Init( int nEntIndex, int iRandomSeed, float flTime,
  91. int iInitialWindYaw, float flInitialWindSpeed )
  92. {
  93. m_iEntIndex = nEntIndex;
  94. m_flWindAngleVariation = m_flWindSpeedVariation = 1.0f;
  95. m_flStartTime = m_flSimTime = m_flSwitchTime = m_flVariationTime = flTime;
  96. m_iWindSeed = iRandomSeed;
  97. m_Stream.SetSeed( iRandomSeed );
  98. m_WindVariationStream.SetSeed( iRandomSeed );
  99. m_iWindDir = m_iInitialWindDir = iInitialWindYaw;
  100. m_flAveWindSpeed = m_flWindSpeed = m_flInitialWindSpeed = flInitialWindSpeed;
  101. /*
  102. // Cache in the wind sound...
  103. if (!g_pEffects->IsServer())
  104. {
  105. CSoundEnvelopeController &controller = CSoundEnvelopeController::GetController();
  106. m_pWindSound = controller.SoundCreate( -1, CHAN_STATIC,
  107. "EnvWind.Loop", ATTN_NONE );
  108. controller.Play( m_pWindSound, 0.0f, 100 );
  109. }
  110. */
  111. // Next time a change happens (which will happen immediately), it'll stop gusting
  112. m_bGusting = true;
  113. }
  114. //-----------------------------------------------------------------------------
  115. // Computes wind variation
  116. //-----------------------------------------------------------------------------
  117. #define WIND_VARIATION_UPDATE_TIME 0.1f
  118. void CEnvWindShared::ComputeWindVariation( float flTime )
  119. {
  120. // The wind variation is updated every 10th of a second..
  121. while( flTime >= m_flVariationTime )
  122. {
  123. m_flWindAngleVariation = m_WindVariationStream.RandomFloat( -10, 10 );
  124. m_flWindSpeedVariation = 1.0 + m_WindVariationStream.RandomFloat( -0.2, 0.2 );
  125. m_flVariationTime += WIND_VARIATION_UPDATE_TIME;
  126. }
  127. }
  128. //-----------------------------------------------------------------------------
  129. // Updates the wind sound
  130. //-----------------------------------------------------------------------------
  131. void CEnvWindShared::UpdateWindSound( float flTotalWindSpeed )
  132. {
  133. if (!g_pEffects->IsServer())
  134. {
  135. float flDuration = random->RandomFloat( 1.0f, 2.0f );
  136. CSoundEnvelopeController &controller = CSoundEnvelopeController::GetController();
  137. // FIXME: Tweak with these numbers
  138. float flNormalizedWindSpeed = flTotalWindSpeed / 150.0f;
  139. if (flNormalizedWindSpeed > 1.0f)
  140. flNormalizedWindSpeed = 1.0f;
  141. float flPitch = 120 * Bias( flNormalizedWindSpeed, 0.3f ) + 100;
  142. float flVolume = 0.3f * Bias( flNormalizedWindSpeed, 0.3f ) + 0.7f;
  143. controller.SoundChangePitch( m_pWindSound, flPitch, flDuration );
  144. controller.SoundChangeVolume( m_pWindSound, flVolume, flDuration );
  145. }
  146. }
  147. //-----------------------------------------------------------------------------
  148. // Updates the wind speed
  149. //-----------------------------------------------------------------------------
  150. #define WIND_ACCELERATION 150.0f // wind speed can accelerate this many units per second
  151. #define WIND_DECELERATION 15.0f // wind speed can decelerate this many units per second
  152. float CEnvWindShared::WindThink( float flTime )
  153. {
  154. // NOTE: This algorithm can be client-server neutal because we're using
  155. // the random number generator to generate *time* at which the wind changes.
  156. // We therefore need to structure the algorithm so that no matter the
  157. // frequency of calls to this function we produce the same wind speeds...
  158. ComputeWindVariation( flTime );
  159. while (true)
  160. {
  161. // First, simulate up to the next switch time...
  162. float flTimeToSwitch = m_flSwitchTime - m_flSimTime;
  163. float flMaxDeltaTime = flTime - m_flSimTime;
  164. bool bGotToSwitchTime = (flMaxDeltaTime > flTimeToSwitch);
  165. float flSimDeltaTime = bGotToSwitchTime ? flTimeToSwitch : flMaxDeltaTime;
  166. // Now that we've chosen
  167. // either ramp up, or sleep till change
  168. bool bReachedSteadyState = true;
  169. if ( m_flAveWindSpeed > m_flWindSpeed )
  170. {
  171. m_flWindSpeed += WIND_ACCELERATION * flSimDeltaTime;
  172. if (m_flWindSpeed > m_flAveWindSpeed)
  173. m_flWindSpeed = m_flAveWindSpeed;
  174. else
  175. bReachedSteadyState = false;
  176. }
  177. else if ( m_flAveWindSpeed < m_flWindSpeed )
  178. {
  179. m_flWindSpeed -= WIND_DECELERATION * flSimDeltaTime;
  180. if (m_flWindSpeed < m_flAveWindSpeed)
  181. m_flWindSpeed = m_flAveWindSpeed;
  182. else
  183. bReachedSteadyState = false;
  184. }
  185. // Update the sim time
  186. // If we didn't get to a switch point, then we're done simulating for now
  187. if (!bGotToSwitchTime)
  188. {
  189. m_flSimTime = flTime;
  190. // We're about to exit, let's set the wind velocity...
  191. QAngle vecWindAngle( 0, m_iWindDir + m_flWindAngleVariation, 0 );
  192. AngleVectors( vecWindAngle, &s_vecWindVelocity );
  193. float flTotalWindSpeed = m_flWindSpeed * m_flWindSpeedVariation;
  194. s_vecWindVelocity *= flTotalWindSpeed;
  195. // If we reached a steady state, we don't need to be called until the switch time
  196. // Otherwise, we should be called immediately
  197. // FIXME: If we ever call this from prediction, we'll need
  198. // to only update the sound if it's a new time
  199. // Or, we'll need to update the sound elsewhere.
  200. // Update the sound....
  201. // UpdateWindSound( flTotalWindSpeed );
  202. // Always immediately call, the wind is forever varying
  203. return ( flTime + 0.01f );
  204. }
  205. m_flSimTime = m_flSwitchTime;
  206. // Switch gusting state..
  207. if( m_bGusting )
  208. {
  209. // wind is gusting, so return to normal wind
  210. m_flAveWindSpeed = m_Stream.RandomInt( m_iMinWind, m_iMaxWind );
  211. // set up for another gust later
  212. m_bGusting = false;
  213. m_flSwitchTime += m_flMinGustDelay + m_Stream.RandomFloat( 0, m_flMaxGustDelay );
  214. #ifndef CLIENT_DLL
  215. m_OnGustEnd.FireOutput( NULL, NULL );
  216. #endif
  217. }
  218. else
  219. {
  220. // time for a gust.
  221. m_flAveWindSpeed = m_Stream.RandomInt( m_iMinGust, m_iMaxGust );
  222. // change wind direction, maybe a lot
  223. m_iWindDir = anglemod( m_iWindDir + m_Stream.RandomInt(-m_iGustDirChange, m_iGustDirChange) );
  224. // set up to stop the gust in a short while
  225. m_bGusting = true;
  226. #ifndef CLIENT_DLL
  227. m_OnGustStart.FireOutput( NULL, NULL );
  228. #endif
  229. // !!!HACKHACK - gust duration tied to the length of a particular wave file
  230. m_flSwitchTime += m_flGustDuration;
  231. }
  232. }
  233. }
  234. //-----------------------------------------------------------------------------
  235. // Method to reset windspeed..
  236. //-----------------------------------------------------------------------------
  237. void ResetWindspeed()
  238. {
  239. s_vecWindVelocity.Init( 0, 0, 0 );
  240. }
  241. //-----------------------------------------------------------------------------
  242. // Method to sample the windspeed at a particular time
  243. //-----------------------------------------------------------------------------
  244. void GetWindspeedAtTime( float flTime, Vector &vecVelocity )
  245. {
  246. // For now, ignore history and time.. fix later when we use wind to affect
  247. // client-side prediction
  248. VectorCopy( s_vecWindVelocity, vecVelocity );
  249. }