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.

754 lines
22 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: Parsing of entity network packets.
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include "client_pch.h"
  8. #include "con_nprint.h"
  9. #include "iprediction.h"
  10. #include "cl_entityreport.h"
  11. #include "dt_recv_eng.h"
  12. #include "net_synctags.h"
  13. #include "ispatialpartitioninternal.h"
  14. #include "LocalNetworkBackdoor.h"
  15. #include "basehandle.h"
  16. #include "dt_localtransfer.h"
  17. #include "iprediction.h"
  18. #include "netmessages.h"
  19. #include "ents_shared.h"
  20. #include "cl_ents_parse.h"
  21. // memdbgon must be the last include file in a .cpp file!!!
  22. #include "tier0/memdbgon.h"
  23. static ConVar cl_flushentitypacket("cl_flushentitypacket", "0", FCVAR_CHEAT, "For debugging. Force the engine to flush an entity packet.");
  24. // Prints important entity creation/deletion events to console
  25. #if defined( _DEBUG )
  26. static ConVar cl_deltatrace( "cl_deltatrace", "0", 0, "For debugging, print entity creation/deletion info to console." );
  27. #define TRACE_DELTA( text ) if ( cl_deltatrace.GetInt() ) { ConMsg( "%s", text ); };
  28. #else
  29. #define TRACE_DELTA( funcs )
  30. #endif
  31. //-----------------------------------------------------------------------------
  32. // Debug networking stuff.
  33. //-----------------------------------------------------------------------------
  34. // #define DEBUG_NETWORKING 1
  35. #if defined( DEBUG_NETWORKING )
  36. void SpewToFile( char const* pFmt, ... );
  37. static ConVar cl_packettrace( "cl_packettrace", "1", 0, "For debugging, massive spew to file." );
  38. #define TRACE_PACKET( text ) if ( cl_packettrace.GetInt() ) { SpewToFile text ; };
  39. #else
  40. #define TRACE_PACKET( text )
  41. #endif
  42. #if defined( DEBUG_NETWORKING )
  43. //-----------------------------------------------------------------------------
  44. // Opens the recording file
  45. //-----------------------------------------------------------------------------
  46. static FileHandle_t OpenRecordingFile()
  47. {
  48. FileHandle_t fp = 0;
  49. static bool s_CantOpenFile = false;
  50. static bool s_NeverOpened = true;
  51. if (!s_CantOpenFile)
  52. {
  53. fp = g_pFileSystem->Open( "cltrace.txt", s_NeverOpened ? "wt" : "at" );
  54. if (!fp)
  55. {
  56. s_CantOpenFile = true;
  57. }
  58. s_NeverOpened = false;
  59. }
  60. return fp;
  61. }
  62. //-----------------------------------------------------------------------------
  63. // Records an argument for a command, flushes when the command is done
  64. //-----------------------------------------------------------------------------
  65. void SpewToFile( char const* pFmt, ... )
  66. {
  67. static CUtlVector<unsigned char> s_RecordingBuffer;
  68. char temp[2048];
  69. va_list args;
  70. va_start( args, pFmt );
  71. int len = Q_vsnprintf( temp, sizeof( temp ), pFmt, args );
  72. va_end( args );
  73. assert( len < 2048 );
  74. int idx = s_RecordingBuffer.AddMultipleToTail( len );
  75. memcpy( &s_RecordingBuffer[idx], temp, len );
  76. if ( 1 ) //s_RecordingBuffer.Size() > 8192)
  77. {
  78. FileHandle_t fp = OpenRecordingFile();
  79. g_pFileSystem->Write( s_RecordingBuffer.Base(), s_RecordingBuffer.Size(), fp );
  80. g_pFileSystem->Close( fp );
  81. s_RecordingBuffer.RemoveAll();
  82. }
  83. }
  84. #endif // DEBUG_NETWORKING
  85. //-----------------------------------------------------------------------------
  86. //
  87. //-----------------------------------------------------------------------------
  88. //-----------------------------------------------------------------------------
  89. // Purpose: Frees the client DLL's binding to the object.
  90. // Input : iEnt -
  91. //-----------------------------------------------------------------------------
  92. void CL_DeleteDLLEntity( int iEnt, const char *reason, bool bOnRecreatingAllEntities )
  93. {
  94. IClientNetworkable *pNet = entitylist->GetClientNetworkable( iEnt );
  95. if ( pNet )
  96. {
  97. ClientClass *pClientClass = pNet->GetClientClass();
  98. TRACE_DELTA( va( "Trace %i (%s): delete (%s)\n", iEnt, pClientClass ? pClientClass->m_pNetworkName : "unknown", reason ) );
  99. #ifndef _XBOX
  100. CL_RecordDeleteEntity( iEnt, pClientClass );
  101. #endif
  102. if ( bOnRecreatingAllEntities )
  103. {
  104. pNet->SetDestroyedOnRecreateEntities();
  105. }
  106. pNet->Release();
  107. }
  108. }
  109. //-----------------------------------------------------------------------------
  110. // Purpose: Has the client DLL allocate its data for the object.
  111. // Input : iEnt -
  112. // iClass -
  113. // Output : Returns true on success, false on failure.
  114. //-----------------------------------------------------------------------------
  115. IClientNetworkable* CL_CreateDLLEntity( int iEnt, int iClass, int iSerialNum )
  116. {
  117. #if defined( _DEBUG )
  118. IClientNetworkable *pOldNetworkable = entitylist->GetClientNetworkable( iEnt );
  119. Assert( !pOldNetworkable );
  120. #endif
  121. ClientClass *pClientClass;
  122. if ( ( pClientClass = cl.m_pServerClasses[iClass].m_pClientClass ) != NULL )
  123. {
  124. TRACE_DELTA( va( "Trace %i (%s): create\n", iEnt, pClientClass->m_pNetworkName ) );
  125. #ifndef _XBOX
  126. CL_RecordAddEntity( iEnt );
  127. #endif
  128. if ( !cl.IsActive() )
  129. {
  130. COM_TimestampedLog( "cl: create '%s'", pClientClass->m_pNetworkName );
  131. }
  132. // Create the entity.
  133. return pClientClass->m_pCreateFn( iEnt, iSerialNum );
  134. }
  135. Assert(false);
  136. return NULL;
  137. }
  138. void SpewBitStream( unsigned char* pMem, int bit, int lastbit )
  139. {
  140. int val = 0;
  141. char buf[1024];
  142. char* pTemp = buf;
  143. int bitcount = 0;
  144. int charIdx = 1;
  145. while( bit < lastbit )
  146. {
  147. int byte = bit >> 3;
  148. int bytebit = bit & 0x7;
  149. val |= ((pMem[byte] & bytebit) != 0) << bitcount;
  150. ++bit;
  151. ++bitcount;
  152. if (bitcount == 4)
  153. {
  154. if ((val >= 0) && (val <= 9))
  155. pTemp[charIdx] = '0' + val;
  156. else
  157. pTemp[charIdx] = 'A' + val - 0xA;
  158. if (charIdx == 1)
  159. charIdx = 0;
  160. else
  161. {
  162. charIdx = 1;
  163. pTemp += 2;
  164. }
  165. bitcount = 0;
  166. val = 0;
  167. }
  168. }
  169. if ((bitcount != 0) || (charIdx != 0))
  170. {
  171. if (bitcount > 0)
  172. {
  173. if ((val >= 0) && (val <= 9))
  174. pTemp[charIdx] = '0' + val;
  175. else
  176. pTemp[charIdx] = 'A' + val - 0xA;
  177. }
  178. if (charIdx == 1)
  179. {
  180. pTemp[0] = '0';
  181. }
  182. pTemp += 2;
  183. }
  184. pTemp[0] = '\0';
  185. TRACE_PACKET(( " CL Bitstream %s\n", buf ));
  186. }
  187. inline static void CL_AddPostDataUpdateCall( CEntityReadInfo &u, int iEnt, DataUpdateType_t updateType )
  188. {
  189. ErrorIfNot( u.m_nPostDataUpdateCalls < MAX_EDICTS,
  190. ("CL_AddPostDataUpdateCall: overflowed u.m_PostDataUpdateCalls") );
  191. u.m_PostDataUpdateCalls[u.m_nPostDataUpdateCalls].m_iEnt = iEnt;
  192. u.m_PostDataUpdateCalls[u.m_nPostDataUpdateCalls].m_UpdateType = updateType;
  193. ++u.m_nPostDataUpdateCalls;
  194. }
  195. //-----------------------------------------------------------------------------
  196. // Purpose: Get the receive table for the specified entity
  197. // Input : *pEnt -
  198. // Output : RecvTable*
  199. //-----------------------------------------------------------------------------
  200. static inline RecvTable* GetEntRecvTable( int entnum )
  201. {
  202. IClientNetworkable *pNet = entitylist->GetClientNetworkable( entnum );
  203. if ( pNet )
  204. return pNet->GetClientClass()->m_pRecvTable;
  205. else
  206. return NULL;
  207. }
  208. //-----------------------------------------------------------------------------
  209. // Purpose: Returns true if the entity index corresponds to a player slot
  210. // Input : index -
  211. // Output : bool
  212. //-----------------------------------------------------------------------------
  213. static inline bool CL_IsPlayerIndex( int index )
  214. {
  215. return ( index >= 1 && index <= cl.m_nMaxClients );
  216. }
  217. //-----------------------------------------------------------------------------
  218. // Purpose: Bad data was received, just flushes incoming delta data.
  219. //-----------------------------------------------------------------------------
  220. void CL_FlushEntityPacket( CClientFrame *packet, char const *errorString, ... )
  221. {
  222. con_nprint_t np;
  223. char str[2048];
  224. va_list marker;
  225. // Spit out an error.
  226. va_start(marker, errorString);
  227. Q_vsnprintf(str, sizeof(str), errorString, marker);
  228. va_end(marker);
  229. ConMsg("%s", str);
  230. np.fixed_width_font = false;
  231. np.time_to_live = 1.0;
  232. np.index = 0;
  233. np.color[ 0 ] = 1.0;
  234. np.color[ 1 ] = 0.2;
  235. np.color[ 2 ] = 0.0;
  236. Con_NXPrintf( &np, "WARNING: CL_FlushEntityPacket, %s", str );
  237. // Free packet memory.
  238. delete packet;
  239. }
  240. // ----------------------------------------------------------------------------- //
  241. // Regular handles for ReadPacketEntities.
  242. // ----------------------------------------------------------------------------- //
  243. void CL_CopyNewEntity(
  244. CEntityReadInfo &u,
  245. int iClass,
  246. int iSerialNum
  247. )
  248. {
  249. if ( u.m_nNewEntity < 0 || u.m_nNewEntity >= MAX_EDICTS )
  250. {
  251. Host_Error ("CL_CopyNewEntity: u.m_nNewEntity < 0 || m_nNewEntity >= MAX_EDICTS");
  252. return;
  253. }
  254. // If it's new, make sure we have a slot for it.
  255. IClientNetworkable *ent = entitylist->GetClientNetworkable( u.m_nNewEntity );
  256. if( iClass >= cl.m_nServerClasses )
  257. {
  258. Host_Error("CL_CopyNewEntity: invalid class index (%d).\n", iClass);
  259. return;
  260. }
  261. // Delete the entity.
  262. ClientClass *pClass = cl.m_pServerClasses[iClass].m_pClientClass;
  263. bool bNew = false;
  264. if ( ent )
  265. {
  266. // if serial number is different, destory old entity
  267. if ( ent->GetIClientUnknown()->GetRefEHandle().GetSerialNumber() != iSerialNum )
  268. {
  269. CL_DeleteDLLEntity( u.m_nNewEntity, "CopyNewEntity" );
  270. ent = NULL; // force a recreate
  271. }
  272. }
  273. if ( !ent )
  274. {
  275. // Ok, it doesn't exist yet, therefore this is not an "entered PVS" message.
  276. ent = CL_CreateDLLEntity( u.m_nNewEntity, iClass, iSerialNum );
  277. if( !ent )
  278. {
  279. const char *pNetworkName = cl.m_pServerClasses[iClass].m_pClientClass ? cl.m_pServerClasses[iClass].m_pClientClass->m_pNetworkName : "";
  280. Host_Error( "CL_ParsePacketEntities: Error creating entity %s(%i)\n", pNetworkName, u.m_nNewEntity );
  281. return;
  282. }
  283. bNew = true;
  284. }
  285. int start_bit = u.m_pBuf->GetNumBitsRead();
  286. DataUpdateType_t updateType = bNew ? DATA_UPDATE_CREATED : DATA_UPDATE_DATATABLE_CHANGED;
  287. ent->PreDataUpdate( updateType );
  288. // Get either the static or instance baseline.
  289. const void *pFromData;
  290. int nFromBits;
  291. PackedEntity *baseline = u.m_bAsDelta ? cl.GetEntityBaseline( u.m_nBaseline, u.m_nNewEntity ) : NULL;
  292. if ( baseline && baseline->m_pClientClass == pClass )
  293. {
  294. Assert( !baseline->IsCompressed() );
  295. pFromData = baseline->GetData();
  296. nFromBits = baseline->GetNumBits();
  297. }
  298. else
  299. {
  300. // Every entity must have a static or an instance baseline when we get here.
  301. ErrorIfNot(
  302. cl.GetClassBaseline( iClass, &pFromData, &nFromBits ),
  303. ("CL_CopyNewEntity: GetClassBaseline(%d) failed.", iClass)
  304. );
  305. nFromBits *= 8; // convert to bits
  306. }
  307. // Delta from baseline and merge to new baseline
  308. bf_read fromBuf( "CL_CopyNewEntity->fromBuf", pFromData, Bits2Bytes(nFromBits), nFromBits );
  309. RecvTable *pRecvTable = GetEntRecvTable( u.m_nNewEntity );
  310. if( !pRecvTable )
  311. Host_Error( "CL_ParseDelta: invalid recv table for ent %d.\n", u.m_nNewEntity );
  312. if ( u.m_bUpdateBaselines )
  313. {
  314. // store this baseline in u.m_pUpdateBaselines
  315. ALIGN4 char packedData[MAX_PACKEDENTITY_DATA] ALIGN4_POST;
  316. bf_write writeBuf( "CL_CopyNewEntity->newBuf", packedData, sizeof(packedData) );
  317. RecvTable_MergeDeltas( pRecvTable, &fromBuf, u.m_pBuf, &writeBuf, -1, NULL, true );
  318. // set the other baseline
  319. cl.SetEntityBaseline( (u.m_nBaseline==0)?1:0, pClass, u.m_nNewEntity, packedData, writeBuf.GetNumBytesWritten() );
  320. fromBuf.StartReading( packedData, writeBuf.GetNumBytesWritten() );
  321. RecvTable_Decode( pRecvTable, ent->GetDataTableBasePtr(), &fromBuf, u.m_nNewEntity, false );
  322. }
  323. else
  324. {
  325. // write data from baseline into entity
  326. RecvTable_Decode( pRecvTable, ent->GetDataTableBasePtr(), &fromBuf, u.m_nNewEntity, false );
  327. // Now parse in the contents of the network stream.
  328. RecvTable_Decode( pRecvTable, ent->GetDataTableBasePtr(), u.m_pBuf, u.m_nNewEntity, true );
  329. }
  330. CL_AddPostDataUpdateCall( u, u.m_nNewEntity, updateType );
  331. // If ent doesn't think it's in PVS, signal that it is
  332. Assert( u.m_pTo->last_entity <= u.m_nNewEntity );
  333. u.m_pTo->last_entity = u.m_nNewEntity;
  334. Assert( !u.m_pTo->transmit_entity.Get(u.m_nNewEntity) );
  335. u.m_pTo->transmit_entity.Set( u.m_nNewEntity );
  336. //
  337. // Net stats..
  338. //
  339. int bit_count = u.m_pBuf->GetNumBitsRead() - start_bit;
  340. #ifndef _XBOX
  341. if ( cl_entityreport.GetBool() )
  342. CL_RecordEntityBits( u.m_nNewEntity, bit_count );
  343. #endif
  344. if ( CL_IsPlayerIndex( u.m_nNewEntity ) )
  345. {
  346. if ( u.m_nNewEntity == cl.m_nPlayerSlot + 1 )
  347. {
  348. u.m_nLocalPlayerBits += bit_count;
  349. }
  350. else
  351. {
  352. u.m_nOtherPlayerBits += bit_count;
  353. }
  354. }
  355. }
  356. void CL_PreserveExistingEntity( int nOldEntity )
  357. {
  358. IClientNetworkable *pEnt = entitylist->GetClientNetworkable( nOldEntity );
  359. if ( !pEnt )
  360. {
  361. // If you hit this, this is because there's a networked client entity that got released
  362. // by some method other than a server update. This can happen if client code calls
  363. // release on a networked entity.
  364. #if defined( STAGING_ONLY )
  365. // Try to use the cl_removeentity_backtrace_capture code in cliententitylist.cpp...
  366. Msg( "%s: missing client entity %d.\n", __FUNCTION__, nOldEntity );
  367. Cbuf_AddText( CFmtStr( "cl_removeentity_backtrace_dump %d\n", nOldEntity ) );
  368. Cbuf_Execute();
  369. #endif // STAGING_ONLY
  370. Host_Error( "CL_PreserveExistingEntity: missing client entity %d.\n", nOldEntity );
  371. return;
  372. }
  373. pEnt->OnDataUnchangedInPVS();
  374. }
  375. void CL_CopyExistingEntity( CEntityReadInfo &u )
  376. {
  377. int start_bit = u.m_pBuf->GetNumBitsRead();
  378. IClientNetworkable *pEnt = entitylist->GetClientNetworkable( u.m_nNewEntity );
  379. if ( !pEnt )
  380. {
  381. Host_Error( "CL_CopyExistingEntity: missing client entity %d.\n", u.m_nNewEntity );
  382. return;
  383. }
  384. Assert( u.m_pFrom->transmit_entity.Get(u.m_nNewEntity) );
  385. // Read raw data from the network stream
  386. pEnt->PreDataUpdate( DATA_UPDATE_DATATABLE_CHANGED );
  387. RecvTable *pRecvTable = GetEntRecvTable( u.m_nNewEntity );
  388. if( !pRecvTable )
  389. {
  390. Host_Error( "CL_ParseDelta: invalid recv table for ent %d.\n", u.m_nNewEntity );
  391. return;
  392. }
  393. RecvTable_Decode( pRecvTable, pEnt->GetDataTableBasePtr(), u.m_pBuf, u.m_nNewEntity );
  394. CL_AddPostDataUpdateCall( u, u.m_nNewEntity, DATA_UPDATE_DATATABLE_CHANGED );
  395. u.m_pTo->last_entity = u.m_nNewEntity;
  396. Assert( !u.m_pTo->transmit_entity.Get(u.m_nNewEntity) );
  397. u.m_pTo->transmit_entity.Set( u.m_nNewEntity );
  398. int bit_count = u.m_pBuf->GetNumBitsRead() - start_bit;
  399. #ifndef _XBOX
  400. if ( cl_entityreport.GetBool() )
  401. CL_RecordEntityBits( u.m_nNewEntity, bit_count );
  402. #endif
  403. if ( CL_IsPlayerIndex( u.m_nNewEntity ) )
  404. {
  405. if ( u.m_nNewEntity == cl.m_nPlayerSlot + 1 )
  406. {
  407. u.m_nLocalPlayerBits += bit_count;
  408. }
  409. else
  410. {
  411. u.m_nOtherPlayerBits += bit_count;
  412. }
  413. }
  414. }
  415. //-----------------------------------------------------------------------------
  416. // Purpose:
  417. //-----------------------------------------------------------------------------
  418. void CL_MarkEntitiesOutOfPVS( CBitVec<MAX_EDICTS> *pvs_flags )
  419. {
  420. int highest_index = entitylist->GetHighestEntityIndex();
  421. // Note that we go up to and including the highest_index
  422. for ( int i = 0; i <= highest_index; i++ )
  423. {
  424. IClientNetworkable *ent = entitylist->GetClientNetworkable( i );
  425. if ( !ent )
  426. continue;
  427. // FIXME: We can remove IClientEntity here if we keep track of the
  428. // last frame's entity_in_pvs
  429. bool curstate = !ent->IsDormant();
  430. bool newstate = pvs_flags->Get( i ) ? true : false;
  431. if ( !curstate && newstate )
  432. {
  433. // Inform the client entity list that the entity entered the PVS
  434. ent->NotifyShouldTransmit( SHOULDTRANSMIT_START );
  435. }
  436. else if ( curstate && !newstate )
  437. {
  438. // Inform the client entity list that the entity left the PVS
  439. ent->NotifyShouldTransmit( SHOULDTRANSMIT_END );
  440. #ifndef _XBOX
  441. CL_RecordLeavePVS( i );
  442. #endif
  443. }
  444. }
  445. }
  446. static void CL_CallPostDataUpdates( CEntityReadInfo &u )
  447. {
  448. for ( int i=0; i < u.m_nPostDataUpdateCalls; i++ )
  449. {
  450. MDLCACHE_CRITICAL_SECTION_(g_pMDLCache);
  451. CPostDataUpdateCall *pCall = &u.m_PostDataUpdateCalls[i];
  452. IClientNetworkable *pEnt = entitylist->GetClientNetworkable( pCall->m_iEnt );
  453. ErrorIfNot( pEnt,
  454. ("CL_CallPostDataUpdates: missing ent %d", pCall->m_iEnt) );
  455. pEnt->PostDataUpdate( pCall->m_UpdateType );
  456. }
  457. }
  458. static float g_flLastPerfRequest = 0.0f;
  459. static ConVar cl_debug_player_perf( "cl_debug_player_perf", "0", 0 );
  460. //-----------------------------------------------------------------------------
  461. // Purpose: An svc_packetentities has just been parsed, deal with the
  462. // rest of the data stream. This can be a delta from the baseline or from a previous
  463. // client frame for this client.
  464. // Input : delta -
  465. // *playerbits -
  466. // Output : void CL_ParsePacketEntities
  467. //-----------------------------------------------------------------------------
  468. bool CL_ProcessPacketEntities ( SVC_PacketEntities *entmsg )
  469. {
  470. VPROF( "_CL_ParsePacketEntities" );
  471. // Packed entities for that frame
  472. // Allocate space for new packet info.
  473. CClientFrame *newFrame = cl.AllocateFrame();
  474. newFrame->Init( cl.GetServerTickCount() );
  475. CClientFrame *oldFrame = NULL;
  476. // if cl_flushentitypacket is set to N, the next N entity updates will be flushed
  477. if ( cl_flushentitypacket.GetInt() )
  478. {
  479. // we can't use this, it is too old
  480. CL_FlushEntityPacket( newFrame, "Forced by cvar\n" );
  481. cl_flushentitypacket.SetValue( cl_flushentitypacket.GetInt() - 1 ); // Reduce the cvar.
  482. return false;
  483. }
  484. if ( entmsg->m_bIsDelta )
  485. {
  486. int nDeltaTicks = cl.GetServerTickCount() - entmsg->m_nDeltaFrom;
  487. float flDeltaSeconds = TICKS_TO_TIME( nDeltaTicks );
  488. // If we have cl_debug_player_perf set and we see a huge delta between what we've ack'd to the server and where it's at
  489. // ask it for an instantaneous perf snapshot
  490. if ( cl_debug_player_perf.GetBool() &&
  491. ( flDeltaSeconds > 0.5f ) && // delta is pretty out of date
  492. ( ( realtime - g_flLastPerfRequest ) > 5.0f ) ) // haven't requested in a while
  493. {
  494. g_flLastPerfRequest = realtime;
  495. Warning( "Gap in server data, requesting connection perf data\n" );
  496. cl.SendStringCmd( "playerperf\n" );
  497. }
  498. if ( cl.GetServerTickCount() == entmsg->m_nDeltaFrom )
  499. {
  500. Host_Error( "Update self-referencing, connection dropped.\n" );
  501. return false;
  502. }
  503. // Otherwise, mark where we are valid to and point to the packet entities we'll be updating from.
  504. oldFrame = cl.GetClientFrame( entmsg->m_nDeltaFrom );
  505. if ( !oldFrame )
  506. {
  507. CL_FlushEntityPacket( newFrame, "Update delta not found.\n" );
  508. return false;
  509. }
  510. }
  511. else
  512. {
  513. if ( cl_debug_player_perf.GetBool() )
  514. {
  515. Warning( "Received uncompressed server update\n" );
  516. }
  517. // Clear out the client's entity states..
  518. for ( int i=0; i <= entitylist->GetHighestEntityIndex(); i++ )
  519. {
  520. CL_DeleteDLLEntity( i, "ProcessPacketEntities", true );
  521. }
  522. }
  523. // signal client DLL that we have started updating entities
  524. ClientDLL_FrameStageNotify( FRAME_NET_UPDATE_START );
  525. g_nPropsDecoded = 0;
  526. Assert( entmsg->m_nBaseline >= 0 && entmsg->m_nBaseline < 2 );
  527. if ( entmsg->m_bUpdateBaseline )
  528. {
  529. // server requested to use this snapshot as baseline update
  530. int nUpdateBaseline = (entmsg->m_nBaseline == 0) ? 1 : 0;
  531. cl.CopyEntityBaseline( entmsg->m_nBaseline, nUpdateBaseline );
  532. // send new baseline acknowledgement(as reliable)
  533. cl.m_NetChannel->SendNetMsg( CLC_BaselineAck( cl.GetServerTickCount(), entmsg->m_nBaseline ), true );
  534. }
  535. CEntityReadInfo u;
  536. u.m_pBuf = &entmsg->m_DataIn;
  537. u.m_pFrom = oldFrame;
  538. u.m_pTo = newFrame;
  539. u.m_bAsDelta = entmsg->m_bIsDelta;
  540. u.m_nHeaderCount = entmsg->m_nUpdatedEntries;
  541. u.m_nBaseline = entmsg->m_nBaseline;
  542. u.m_bUpdateBaselines = entmsg->m_bUpdateBaseline;
  543. // update the entities
  544. cl.ReadPacketEntities( u );
  545. ClientDLL_FrameStageNotify( FRAME_NET_UPDATE_POSTDATAUPDATE_START );
  546. // call PostDataUpdate() for each entity
  547. CL_CallPostDataUpdates( u );
  548. ClientDLL_FrameStageNotify( FRAME_NET_UPDATE_POSTDATAUPDATE_END );
  549. // call NotifyShouldTransmit() for entities that entered or left the PVS
  550. CL_MarkEntitiesOutOfPVS( &newFrame->transmit_entity );
  551. // adjust net channel stats
  552. cl.m_NetChannel->UpdateMessageStats( INetChannelInfo::LOCALPLAYER, u.m_nLocalPlayerBits );
  553. cl.m_NetChannel->UpdateMessageStats( INetChannelInfo::OTHERPLAYERS, u.m_nOtherPlayerBits );
  554. cl.m_NetChannel->UpdateMessageStats( INetChannelInfo::ENTITIES, -(u.m_nLocalPlayerBits+u.m_nOtherPlayerBits) );
  555. cl.DeleteClientFrames( entmsg->m_nDeltaFrom );
  556. // If the client has more than 64 frames, the host will start to eat too much memory.
  557. // TODO: We should enforce this somehow.
  558. if ( MAX_CLIENT_FRAMES < cl.AddClientFrame( newFrame ) )
  559. {
  560. DevMsg( 1, "CL_ProcessPacketEntities: frame window too big (>%i)\n", MAX_CLIENT_FRAMES );
  561. }
  562. // all update activities are finished
  563. ClientDLL_FrameStageNotify( FRAME_NET_UPDATE_END );
  564. return true;
  565. }
  566. /*
  567. ==================
  568. CL_PreprocessEntities
  569. Server information pertaining to this client only
  570. ==================
  571. */
  572. namespace CDebugOverlay
  573. {
  574. extern void PurgeServerOverlays( void );
  575. }
  576. void CL_PreprocessEntities( void )
  577. {
  578. // Zero latency!!! (single player or listen server?)
  579. bool bIsUsingMultiplayerNetworking = NET_IsMultiplayer();
  580. bool bLastOutgoingCommandEqualsLastAcknowledgedCommand = cl.lastoutgoingcommand == cl.command_ack;
  581. // We always want to re-run prediction when using the multiplayer networking, or if we're the listen server and we get a packet
  582. // before any frames have run
  583. if ( bIsUsingMultiplayerNetworking ||
  584. bLastOutgoingCommandEqualsLastAcknowledgedCommand )
  585. {
  586. //Msg( "%i/%i CL_ParseClientdata: no latency server ack %i\n",
  587. // host_framecount, cl.tickcount,
  588. // command_ack );
  589. CL_RunPrediction( PREDICTION_SIMULATION_RESULTS_ARRIVING_ON_SEND_FRAME );
  590. }
  591. // Copy some results from prediction back into right spot
  592. // Anything not sent over the network from server to client must be specified here.
  593. //if ( cl.last_command_ack )
  594. {
  595. int number_of_commands_executed = ( cl.command_ack - cl.last_command_ack );
  596. #if 0
  597. COM_Log( "cl.log", "Receiving frame acknowledging %i commands\n",
  598. number_of_commands_executed );
  599. COM_Log( "cl.log", " last command number executed %i\n",
  600. cl.command_ack );
  601. COM_Log( "cl.log", " previous last command number executed %i\n",
  602. cl.last_command_ack );
  603. COM_Log( "cl.log", " current world frame %i\n",
  604. cl.m_nCurrentSequence );
  605. #endif
  606. // Copy last set of changes right into current frame.
  607. g_pClientSidePrediction->PreEntityPacketReceived( number_of_commands_executed, cl.m_nCurrentSequence );
  608. }
  609. CDebugOverlay::PurgeServerOverlays();
  610. }