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.

220 lines
4.6 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include "cbase.h"
  8. #include "prediction.h"
  9. // memdbgon must be the last include file in a .cpp file!!!
  10. #include "tier0/memdbgon.h"
  11. static IPredictionSystem g_RecipientFilterPredictionSystem;
  12. //-----------------------------------------------------------------------------
  13. // Purpose:
  14. //-----------------------------------------------------------------------------
  15. C_RecipientFilter::C_RecipientFilter()
  16. {
  17. Reset();
  18. }
  19. C_RecipientFilter::~C_RecipientFilter()
  20. {
  21. }
  22. //-----------------------------------------------------------------------------
  23. // Purpose:
  24. // Input : src -
  25. //-----------------------------------------------------------------------------
  26. void C_RecipientFilter::CopyFrom( const C_RecipientFilter& src )
  27. {
  28. m_bReliable = src.IsReliable();
  29. m_bInitMessage = src.IsInitMessage();
  30. m_bUsingPredictionRules = src.IsUsingPredictionRules();
  31. m_bIgnorePredictionCull = src.IgnorePredictionCull();
  32. int c = src.GetRecipientCount();
  33. for ( int i = 0; i < c; ++i )
  34. {
  35. m_Recipients.AddToTail( src.GetRecipientIndex( i ) );
  36. }
  37. }
  38. //-----------------------------------------------------------------------------
  39. // Purpose:
  40. //-----------------------------------------------------------------------------
  41. void C_RecipientFilter::Reset( void )
  42. {
  43. m_bReliable = false;
  44. m_Recipients.RemoveAll();
  45. m_bUsingPredictionRules = false;
  46. m_bIgnorePredictionCull = false;
  47. }
  48. void C_RecipientFilter::MakeReliable( void )
  49. {
  50. m_bReliable = true;
  51. }
  52. bool C_RecipientFilter::IsReliable( void ) const
  53. {
  54. return m_bReliable;
  55. }
  56. int C_RecipientFilter::GetRecipientCount( void ) const
  57. {
  58. return m_Recipients.Size();
  59. }
  60. int C_RecipientFilter::GetRecipientIndex( int slot ) const
  61. {
  62. if ( slot < 0 || slot >= GetRecipientCount() )
  63. return -1;
  64. return m_Recipients[ slot ];
  65. }
  66. void C_RecipientFilter::AddAllPlayers( void )
  67. {
  68. if ( !C_BasePlayer::GetLocalPlayer() )
  69. return;
  70. m_Recipients.RemoveAll();
  71. AddRecipient( C_BasePlayer::GetLocalPlayer() );
  72. }
  73. void C_RecipientFilter::AddRecipient( C_BasePlayer *player )
  74. {
  75. Assert( player );
  76. if ( !player )
  77. return;
  78. int index = player->index;
  79. // If we're predicting and this is not the first time we've predicted this sound
  80. // then don't send it to the local player again.
  81. if ( m_bUsingPredictionRules )
  82. {
  83. Assert( player == C_BasePlayer::GetLocalPlayer() );
  84. Assert( prediction->InPrediction() );
  85. // Only add local player if this is the first time doing prediction
  86. if ( !g_RecipientFilterPredictionSystem.CanPredict() )
  87. {
  88. return;
  89. }
  90. }
  91. // Already in list
  92. if ( m_Recipients.Find( index ) != m_Recipients.InvalidIndex() )
  93. return;
  94. // this is a client side filter, only add the local player
  95. if ( !player->IsLocalPlayer() )
  96. return;
  97. m_Recipients.AddToTail( index );
  98. }
  99. void C_RecipientFilter::RemoveRecipient( C_BasePlayer *player )
  100. {
  101. if ( !player )
  102. return;
  103. int index = player->index;
  104. // Remove it if it's in the list
  105. m_Recipients.FindAndRemove( index );
  106. }
  107. void C_RecipientFilter::AddRecipientsByTeam( C_Team *team )
  108. {
  109. AddAllPlayers();
  110. }
  111. void C_RecipientFilter::RemoveRecipientsByTeam( C_Team *team )
  112. {
  113. Assert ( 0 );
  114. }
  115. void C_RecipientFilter::AddPlayersFromBitMask( CBitVec< ABSOLUTE_PLAYER_LIMIT >& playerbits )
  116. {
  117. C_BasePlayer *pPlayer = C_BasePlayer::GetLocalPlayer();
  118. if ( !pPlayer )
  119. return;
  120. // only add the local player on client side
  121. if ( !playerbits[ pPlayer->index ] )
  122. return;
  123. AddRecipient( pPlayer );
  124. }
  125. void C_RecipientFilter::AddRecipientsByPVS( const Vector& origin )
  126. {
  127. AddAllPlayers();
  128. }
  129. void C_RecipientFilter::AddRecipientsByPAS( const Vector& origin )
  130. {
  131. AddAllPlayers();
  132. }
  133. void C_RecipientFilter::UsePredictionRules( void )
  134. {
  135. if ( m_bUsingPredictionRules )
  136. return;
  137. if ( !prediction->InPrediction() )
  138. {
  139. Assert( 0 );
  140. return;
  141. }
  142. C_BasePlayer *local = C_BasePlayer::GetLocalPlayer();
  143. if ( !local )
  144. {
  145. Assert( 0 );
  146. return;
  147. }
  148. m_bUsingPredictionRules = true;
  149. // Cull list now, if needed
  150. int c = GetRecipientCount();
  151. if ( c == 0 )
  152. return;
  153. if ( !g_RecipientFilterPredictionSystem.CanPredict() )
  154. {
  155. RemoveRecipient( local );
  156. }
  157. }
  158. bool C_RecipientFilter::IsUsingPredictionRules( void ) const
  159. {
  160. return m_bUsingPredictionRules;
  161. }
  162. bool C_RecipientFilter::IgnorePredictionCull( void ) const
  163. {
  164. return m_bIgnorePredictionCull;
  165. }
  166. void C_RecipientFilter::SetIgnorePredictionCull( bool ignore )
  167. {
  168. m_bIgnorePredictionCull = ignore;
  169. }
  170. CLocalPlayerFilter::CLocalPlayerFilter()
  171. {
  172. if ( C_BasePlayer::GetLocalPlayer() )
  173. {
  174. AddRecipient( C_BasePlayer::GetLocalPlayer() );
  175. }
  176. }