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.

227 lines
7.2 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 "recvproxy.h"
  9. // memdbgon must be the last include file in a .cpp file!!!
  10. #include "tier0/memdbgon.h"
  11. #include "cdll_client_int.h"
  12. #include "proto_version.h"
  13. void RecvProxy_IntToColor32( const CRecvProxyData *pData, void *pStruct, void *pOut )
  14. {
  15. color32 *pOutColor = (color32*)pOut;
  16. unsigned int inColor = *((unsigned int*)&pData->m_Value.m_Int);
  17. pOutColor->r = (unsigned char)(inColor >> 24);
  18. pOutColor->g = (unsigned char)((inColor >> 16) & 0xFF);
  19. pOutColor->b = (unsigned char)((inColor >> 8) & 0xFF);
  20. pOutColor->a = (unsigned char)(inColor & 0xFF);
  21. }
  22. void RecvProxy_IntSubOne( const CRecvProxyData *pData, void *pStruct, void *pOut )
  23. {
  24. int *pInt = (int *)pOut;
  25. *pInt = pData->m_Value.m_Int - 1;
  26. }
  27. void RecvProxy_ShortSubOne( const CRecvProxyData *pData, void *pStruct, void *pOut )
  28. {
  29. short *pInt = (short *)pOut;
  30. *pInt = pData->m_Value.m_Int - 1;
  31. }
  32. RecvProp RecvPropIntWithMinusOneFlag( const char *pVarName, int offset, int sizeofVar, RecvVarProxyFn proxyFn )
  33. {
  34. return RecvPropInt( pVarName, offset, sizeofVar, 0, proxyFn );
  35. }
  36. void RecvProxy_IntToModelIndex16_BackCompatible( const CRecvProxyData *pData, void *pStruct, void *pOut )
  37. {
  38. int modelIndex = pData->m_Value.m_Int;
  39. if ( modelIndex < -1 && engine->GetProtocolVersion() <= PROTOCOL_VERSION_20 )
  40. {
  41. Assert( modelIndex > -20000 );
  42. modelIndex = -2 - ( ( -2 - modelIndex ) << 1 );
  43. }
  44. *(int16*)pOut = modelIndex;
  45. }
  46. void RecvProxy_IntToModelIndex32_BackCompatible( const CRecvProxyData *pData, void *pStruct, void *pOut )
  47. {
  48. int modelIndex = pData->m_Value.m_Int;
  49. if ( modelIndex < -1 && engine->GetProtocolVersion() <= PROTOCOL_VERSION_20 )
  50. {
  51. Assert( modelIndex > -20000 );
  52. modelIndex = -2 - ( ( -2 - modelIndex ) << 1 );
  53. }
  54. *(int32*)pOut = modelIndex;
  55. }
  56. //-----------------------------------------------------------------------------
  57. // Purpose: Okay, so we have to queue up the actual ehandle to entity lookup for the following reason:
  58. // If a player has an EHandle/CHandle to an object such as a weapon, since the player is in slot 1-31, then
  59. // if the weapon is created and given to the player in the same frame, then the weapon won't have been
  60. // created at the time we parse this EHandle index, since the player is ahead of every other entity in the
  61. // packet (except for the world).
  62. // So instead, we remember which ehandles need to be set and we set them after all network data has
  63. // been received. Sigh.
  64. // Input : *pData -
  65. // *pStruct -
  66. // *pOut -
  67. //-----------------------------------------------------------------------------
  68. void RecvProxy_IntToEHandle( const CRecvProxyData *pData, void *pStruct, void *pOut )
  69. {
  70. CBaseHandle *pEHandle = (CBaseHandle*)pOut;
  71. if ( pData->m_Value.m_Int == INVALID_NETWORKED_EHANDLE_VALUE )
  72. {
  73. *pEHandle = INVALID_EHANDLE_INDEX;
  74. }
  75. else
  76. {
  77. int iEntity = pData->m_Value.m_Int & ((1 << MAX_EDICT_BITS) - 1);
  78. int iSerialNum = pData->m_Value.m_Int >> MAX_EDICT_BITS;
  79. pEHandle->Init( iEntity, iSerialNum );
  80. }
  81. }
  82. RecvProp RecvPropEHandle(
  83. const char *pVarName,
  84. int offset,
  85. int sizeofVar,
  86. RecvVarProxyFn proxyFn )
  87. {
  88. return RecvPropInt( pVarName, offset, sizeofVar, 0, proxyFn );
  89. }
  90. RecvProp RecvPropBool(
  91. const char *pVarName,
  92. int offset,
  93. int sizeofVar )
  94. {
  95. Assert( sizeofVar == sizeof( bool ) );
  96. return RecvPropInt( pVarName, offset, sizeofVar );
  97. }
  98. RecvProp RecvPropBool(
  99. const char *pVarName,
  100. int offset,
  101. int sizeofVar,
  102. int flags,
  103. RecvVarProxyFn varProxy
  104. )
  105. {
  106. Assert( sizeofVar == sizeof( bool ) );
  107. return RecvPropInt( pVarName, offset, sizeofVar, flags, varProxy );
  108. }
  109. //-----------------------------------------------------------------------------
  110. // Moveparent receive proxies
  111. //-----------------------------------------------------------------------------
  112. void RecvProxy_IntToMoveParent( const CRecvProxyData *pData, void *pStruct, void *pOut )
  113. {
  114. CHandle<C_BaseEntity> *pHandle = (CHandle<C_BaseEntity>*)pOut;
  115. RecvProxy_IntToEHandle( pData, pStruct, (CBaseHandle*)pHandle );
  116. }
  117. void RecvProxy_InterpolationAmountChanged( const CRecvProxyData *pData, void *pStruct, void *pOut )
  118. {
  119. // m_bSimulatedEveryTick & m_bAnimatedEveryTick are boolean
  120. if ( *((bool*)pOut) != (pData->m_Value.m_Int != 0) )
  121. {
  122. // Have the regular proxy store the data.
  123. RecvProxy_Int32ToInt8( pData, pStruct, pOut );
  124. C_BaseEntity *pEntity = (C_BaseEntity *) pStruct;
  125. pEntity->Interp_UpdateInterpolationAmounts( pEntity->GetVarMapping() );
  126. }
  127. }
  128. //-----------------------------------------------------------------------------
  129. // Purpose: Decodes a time value
  130. // Input : *pStruct - ( C_BaseEntity * ) used to flag animtime is changine
  131. // *pVarData -
  132. // *pIn -
  133. // objectID -
  134. //-----------------------------------------------------------------------------
  135. static void RecvProxy_Time( const CRecvProxyData *pData, void *pStruct, void *pOut )
  136. {
  137. float t;
  138. float clock_base;
  139. float offset;
  140. // Get msec offset
  141. offset = ( float )pData->m_Value.m_Int / 1000.0f;
  142. // Get base
  143. clock_base = floor( engine->GetLastTimeStamp() );
  144. // Add together and clamp to msec precision
  145. t = ClampToMsec( clock_base + offset );
  146. // Store decoded value
  147. *( float * )pOut = t;
  148. }
  149. //-----------------------------------------------------------------------------
  150. // Purpose:
  151. // Input : *pVarName -
  152. // sizeofVar -
  153. // Output : RecvProp
  154. //-----------------------------------------------------------------------------
  155. RecvProp RecvPropTime(
  156. const char *pVarName,
  157. int offset,
  158. int sizeofVar/*=SIZEOF_IGNORE*/ )
  159. {
  160. // return RecvPropInt( pVarName, offset, sizeofVar, 0, RecvProxy_Time );
  161. return RecvPropFloat( pVarName, offset, sizeofVar );
  162. };
  163. //-----------------------------------------------------------------------------
  164. // Purpose: Okay, so we have to queue up the actual ehandle to entity lookup for the following reason:
  165. // If a player has an EHandle/CHandle to an object such as a weapon, since the player is in slot 1-31, then
  166. // if the weapon is created and given to the player in the same frame, then the weapon won't have been
  167. // created at the time we parse this EHandle index, since the player is ahead of every other entity in the
  168. // packet (except for the world).
  169. // So instead, we remember which ehandles need to be set and we set them after all network data has
  170. // been received. Sigh.
  171. // Input : *pData -
  172. // *pStruct -
  173. // *pOut -
  174. //-----------------------------------------------------------------------------
  175. #if !defined( NO_ENTITY_PREDICTION )
  176. static void RecvProxy_IntToPredictableId( const CRecvProxyData *pData, void *pStruct, void *pOut )
  177. {
  178. CPredictableId *pId = (CPredictableId*)pOut;
  179. Assert( pId );
  180. pId->SetRaw( pData->m_Value.m_Int );
  181. }
  182. //-----------------------------------------------------------------------------
  183. // Purpose:
  184. // Input : *pVarName -
  185. // sizeofVar -
  186. // Output : RecvProp
  187. //-----------------------------------------------------------------------------
  188. RecvProp RecvPropPredictableId(
  189. const char *pVarName,
  190. int offset,
  191. int sizeofVar/*=SIZEOF_IGNORE*/ )
  192. {
  193. return RecvPropInt( pVarName, offset, sizeofVar, 0, RecvProxy_IntToPredictableId );
  194. }
  195. #endif