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.

199 lines
5.8 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: implements various common send proxies
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include "cbase.h"
  8. #include "sendproxy.h"
  9. #include "basetypes.h"
  10. #include "baseentity.h"
  11. #include "team.h"
  12. #include "player.h"
  13. // memdbgon must be the last include file in a .cpp file!!!
  14. #include "tier0/memdbgon.h"
  15. void SendProxy_Color32ToInt( const SendProp *pProp, const void *pStruct, const void *pData, DVariant *pOut, int iElement, int objectID )
  16. {
  17. color32 *pIn = (color32*)pData;
  18. *((unsigned int*)&pOut->m_Int) = ((unsigned int)pIn->r << 24) | ((unsigned int)pIn->g << 16) | ((unsigned int)pIn->b << 8) | ((unsigned int)pIn->a);
  19. }
  20. void SendProxy_EHandleToInt( const SendProp *pProp, const void *pStruct, const void *pVarData, DVariant *pOut, int iElement, int objectID)
  21. {
  22. CBaseHandle *pHandle = (CBaseHandle*)pVarData;
  23. if ( pHandle && pHandle->Get() )
  24. {
  25. int iSerialNum = pHandle->GetSerialNumber() & ( (1 << NUM_NETWORKED_EHANDLE_SERIAL_NUMBER_BITS) - 1 );
  26. pOut->m_Int = pHandle->GetEntryIndex() | (iSerialNum << MAX_EDICT_BITS);
  27. }
  28. else
  29. {
  30. pOut->m_Int = INVALID_NETWORKED_EHANDLE_VALUE;
  31. }
  32. }
  33. void SendProxy_IntAddOne( const SendProp *pProp, const void *pStruct, const void *pVarData, DVariant *pOut, int iElement, int objectID)
  34. {
  35. int *pInt = (int *)pVarData;
  36. pOut->m_Int = (*pInt) + 1;
  37. }
  38. void SendProxy_ShortAddOne( const SendProp *pProp, const void *pStruct, const void *pVarData, DVariant *pOut, int iElement, int objectID)
  39. {
  40. short *pInt = (short *)pVarData;
  41. pOut->m_Int = (*pInt) + 1;
  42. }
  43. SendProp SendPropBool(
  44. const char *pVarName,
  45. int offset,
  46. int sizeofVar )
  47. {
  48. Assert( sizeofVar == sizeof( bool ) );
  49. return SendPropInt( pVarName, offset, sizeofVar, 1, SPROP_UNSIGNED );
  50. }
  51. SendProp SendPropEHandle(
  52. const char *pVarName,
  53. int offset,
  54. int sizeofVar,
  55. int flags,
  56. SendVarProxyFn proxyFn )
  57. {
  58. return SendPropInt( pVarName, offset, sizeofVar, NUM_NETWORKED_EHANDLE_BITS, SPROP_UNSIGNED|flags, proxyFn );
  59. }
  60. SendProp SendPropIntWithMinusOneFlag( const char *pVarName, int offset, int sizeofVar, int nBits, SendVarProxyFn proxyFn )
  61. {
  62. return SendPropInt( pVarName, offset, sizeofVar, nBits, SPROP_UNSIGNED, proxyFn );
  63. }
  64. //-----------------------------------------------------------------------------
  65. // Purpose: Proxy that only sends data to team members
  66. // Input : *pStruct -
  67. // *pData -
  68. // *pOut -
  69. // objectID -
  70. // Output : Returns true on success, false on failure.
  71. //-----------------------------------------------------------------------------
  72. void* SendProxy_OnlyToTeam( const SendProp *pProp, const void *pStruct, const void *pVarData, CSendProxyRecipients *pRecipients, int objectID )
  73. {
  74. CBaseEntity *pEntity = (CBaseEntity*)pStruct;
  75. if ( pEntity )
  76. {
  77. CTeam *pTeam = pEntity->GetTeam();
  78. if ( pTeam )
  79. {
  80. pRecipients->ClearAllRecipients();
  81. for ( int i=0; i < pTeam->GetNumPlayers(); i++ )
  82. pRecipients->SetRecipient( pTeam->GetPlayer( i )->GetClientIndex() );
  83. return (void*)pVarData;
  84. }
  85. }
  86. return NULL;
  87. }
  88. REGISTER_SEND_PROXY_NON_MODIFIED_POINTER( SendProxy_OnlyToTeam );
  89. #define TIME_BITS 24
  90. // This table encodes edict data.
  91. #if 0
  92. static void SendProxy_Time( const SendProp *pProp, const void *pStruct, const void *pVarData, DVariant *pOut, int iElement, int objectID )
  93. {
  94. float clock_base = floor( gpGlobals->curtime );
  95. float t = *( float * )pVarData;
  96. float dt = t - clock_base;
  97. int addt = Floor2Int( 1000.0f * dt + 0.5f );
  98. // TIME_BITS bits gives us TIME_BITS-1 bits plus sign bit
  99. int maxoffset = 1 << ( TIME_BITS - 1);
  100. addt = clamp( addt, -maxoffset, maxoffset );
  101. pOut->m_Int = addt;
  102. }
  103. #endif
  104. //-----------------------------------------------------------------------------
  105. // Purpose:
  106. // Input : *pVarName -
  107. // sizeofVar -
  108. // flags -
  109. // pId -
  110. // Output : SendProp
  111. //-----------------------------------------------------------------------------
  112. SendProp SendPropTime(
  113. const char *pVarName,
  114. int offset,
  115. int sizeofVar )
  116. {
  117. // return SendPropInt( pVarName, offset, sizeofVar, TIME_BITS, 0, SendProxy_Time );
  118. // FIXME: Re-enable above when it doesn't cause lots of deltas
  119. return SendPropFloat( pVarName, offset, sizeofVar, -1, SPROP_NOSCALE );
  120. }
  121. #if !defined( NO_ENTITY_PREDICTION )
  122. #define PREDICTABLE_ID_BITS 31
  123. //-----------------------------------------------------------------------------
  124. // Purpose: Converts a predictable Id to an integer
  125. // Input : *pStruct -
  126. // *pVarData -
  127. // *pOut -
  128. // iElement -
  129. // objectID -
  130. //-----------------------------------------------------------------------------
  131. static void SendProxy_PredictableIdToInt( const SendProp *pProp, const void *pStruct, const void *pVarData, DVariant *pOut, int iElement, int objectID )
  132. {
  133. CPredictableId* pId = ( CPredictableId * )pVarData;
  134. if ( pId )
  135. {
  136. pOut->m_Int = pId->GetRaw();
  137. }
  138. else
  139. {
  140. pOut->m_Int = 0;
  141. }
  142. }
  143. //-----------------------------------------------------------------------------
  144. // Purpose:
  145. // Input : *pVarName -
  146. // sizeofVar -
  147. // flags -
  148. // pId -
  149. // Output : SendProp
  150. //-----------------------------------------------------------------------------
  151. SendProp SendPropPredictableId(
  152. const char *pVarName,
  153. int offset,
  154. int sizeofVar )
  155. {
  156. return SendPropInt( pVarName, offset, sizeofVar, PREDICTABLE_ID_BITS, SPROP_UNSIGNED, SendProxy_PredictableIdToInt );
  157. }
  158. #endif
  159. void SendProxy_StringT_To_String( const SendProp *pProp, const void *pStruct, const void *pVarData, DVariant *pOut, int iElement, int objectID )
  160. {
  161. string_t &str = *((string_t*)pVarData);
  162. pOut->m_pString = (char*)STRING( str );
  163. }
  164. SendProp SendPropStringT( const char *pVarName, int offset, int sizeofVar )
  165. {
  166. // Make sure it's the right type.
  167. Assert( sizeofVar == sizeof( string_t ) );
  168. return SendPropString( pVarName, offset, DT_MAX_STRING_BUFFERSIZE, 0, SendProxy_StringT_To_String );
  169. }