Counter Strike : Global Offensive Source Code
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.

359 lines
13 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include "cbase.h"
  8. #include "playerlocaldata.h"
  9. #include "player.h"
  10. #include "mathlib/mathlib.h"
  11. #include "entitylist.h"
  12. #include "SkyCamera.h"
  13. #include "playernet_vars.h"
  14. #include "fogcontroller.h"
  15. #include "tier0/vprof.h"
  16. // memdbgon must be the last include file in a .cpp file!!!
  17. #include "tier0/memdbgon.h"
  18. //=============================================================================
  19. BEGIN_SEND_TABLE_NOBASE( CPlayerLocalData, DT_Local )
  20. SendPropArray3 (SENDINFO_ARRAY3(m_chAreaBits), SendPropInt(SENDINFO_ARRAY(m_chAreaBits), 8, SPROP_UNSIGNED)),
  21. SendPropArray3 (SENDINFO_ARRAY3(m_chAreaPortalBits), SendPropInt(SENDINFO_ARRAY(m_chAreaPortalBits), 8, SPROP_UNSIGNED)),
  22. SendPropInt (SENDINFO(m_iHideHUD), HIDEHUD_BITCOUNT, SPROP_UNSIGNED),
  23. SendPropFloat (SENDINFO(m_flFOVRate), 0, SPROP_NOSCALE ),
  24. SendPropInt (SENDINFO(m_bDucked), 1, SPROP_UNSIGNED ),
  25. SendPropInt (SENDINFO(m_bDucking), 1, SPROP_UNSIGNED ),
  26. SendPropFloat (SENDINFO(m_flLastDuckTime), -1, SPROP_NOSCALE ),
  27. SendPropInt (SENDINFO(m_bInDuckJump), 1, SPROP_UNSIGNED ),
  28. #if PREDICTION_ERROR_CHECK_LEVEL > 1
  29. SendPropInt (SENDINFO(m_nDuckTimeMsecs), -1, SPROP_NOSCALE|SPROP_CHANGES_OFTEN ),
  30. SendPropInt (SENDINFO(m_nDuckJumpTimeMsecs), -1, SPROP_NOSCALE ),
  31. SendPropInt (SENDINFO(m_nJumpTimeMsecs), -1, SPROP_NOSCALE ),
  32. SendPropFloat (SENDINFO(m_flFallVelocity), 32, SPROP_NOSCALE ),
  33. SendPropFloat ( SENDINFO_VECTORELEM(m_viewPunchAngle, 0), 32, SPROP_NOSCALE|SPROP_CHANGES_OFTEN ),
  34. SendPropFloat ( SENDINFO_VECTORELEM(m_viewPunchAngle, 1), 32, SPROP_NOSCALE|SPROP_CHANGES_OFTEN ),
  35. SendPropFloat ( SENDINFO_VECTORELEM(m_viewPunchAngle, 2), 32, SPROP_NOSCALE|SPROP_CHANGES_OFTEN ),
  36. SendPropFloat ( SENDINFO_VECTORELEM(m_aimPunchAngle, 0), 32, SPROP_NOSCALE|SPROP_CHANGES_OFTEN ),
  37. SendPropFloat ( SENDINFO_VECTORELEM(m_aimPunchAngle, 1), 32, SPROP_NOSCALE|SPROP_CHANGES_OFTEN ),
  38. SendPropFloat ( SENDINFO_VECTORELEM(m_aimPunchAngle, 2), 32, SPROP_NOSCALE|SPROP_CHANGES_OFTEN ),
  39. SendPropFloat ( SENDINFO_VECTORELEM(m_aimPunchAngleVel, 0), 32, SPROP_NOSCALE|SPROP_CHANGES_OFTEN ),
  40. SendPropFloat ( SENDINFO_VECTORELEM(m_aimPunchAngleVel, 1), 32, SPROP_NOSCALE|SPROP_CHANGES_OFTEN ),
  41. SendPropFloat ( SENDINFO_VECTORELEM(m_aimPunchAngleVel, 2), 32, SPROP_NOSCALE|SPROP_CHANGES_OFTEN ),
  42. #else
  43. SendPropInt ( SENDINFO(m_nDuckTimeMsecs), 10, SPROP_UNSIGNED|SPROP_CHANGES_OFTEN ),
  44. SendPropInt (SENDINFO(m_nDuckJumpTimeMsecs), 10, SPROP_UNSIGNED ),
  45. SendPropInt (SENDINFO(m_nJumpTimeMsecs), 10, SPROP_UNSIGNED ),
  46. SendPropFloat (SENDINFO(m_flFallVelocity), 17, SPROP_CHANGES_OFTEN, -4096.0f, 4096.0f ),
  47. SendPropVector (SENDINFO(m_viewPunchAngle), -1, SPROP_COORD|SPROP_CHANGES_OFTEN),
  48. SendPropVector (SENDINFO(m_aimPunchAngle), -1, SPROP_COORD|SPROP_CHANGES_OFTEN),
  49. SendPropVector (SENDINFO(m_aimPunchAngleVel), -1, SPROP_COORD|SPROP_CHANGES_OFTEN),
  50. #endif
  51. SendPropInt (SENDINFO(m_bDrawViewmodel), 1, SPROP_UNSIGNED ),
  52. SendPropInt (SENDINFO(m_bWearingSuit), 1, SPROP_UNSIGNED ),
  53. SendPropBool (SENDINFO(m_bPoisoned)),
  54. SendPropFloat (SENDINFO(m_flStepSize), 16, SPROP_ROUNDUP, 0.0f, 128.0f ),
  55. SendPropInt (SENDINFO(m_bAllowAutoMovement),1, SPROP_UNSIGNED ),
  56. // Autoaim
  57. // SendPropBool( SENDINFO(m_bAutoAimTarget) ),
  58. // 3d skybox data
  59. SendPropInt(SENDINFO_STRUCTELEM( sky3dparams_t, m_skybox3d, scale), 12),
  60. SendPropVector (SENDINFO_STRUCTELEM(sky3dparams_t, m_skybox3d, origin), -1, SPROP_COORD),
  61. SendPropInt (SENDINFO_STRUCTELEM(sky3dparams_t, m_skybox3d, area), 8, SPROP_UNSIGNED ),
  62. SendPropInt( SENDINFO_STRUCTELEM( fogparams_t, m_skybox3d.fog, enable ), 1, SPROP_UNSIGNED ),
  63. SendPropInt( SENDINFO_STRUCTELEM( fogparams_t, m_skybox3d.fog, blend ), 1, SPROP_UNSIGNED ),
  64. SendPropVector( SENDINFO_STRUCTELEM(fogparams_t, m_skybox3d.fog, dirPrimary), -1, SPROP_COORD),
  65. SendPropInt( SENDINFO_STRUCTELEM( fogparams_t, m_skybox3d.fog, colorPrimary ), 32, SPROP_UNSIGNED, SendProxy_Color32ToInt32 ),
  66. SendPropInt( SENDINFO_STRUCTELEM( fogparams_t, m_skybox3d.fog, colorSecondary ), 32, SPROP_UNSIGNED, SendProxy_Color32ToInt32 ),
  67. SendPropFloat( SENDINFO_STRUCTELEM( fogparams_t, m_skybox3d.fog, start ), 0, SPROP_NOSCALE ),
  68. SendPropFloat( SENDINFO_STRUCTELEM( fogparams_t, m_skybox3d.fog, end ), 0, SPROP_NOSCALE ),
  69. SendPropFloat( SENDINFO_STRUCTELEM( fogparams_t, m_skybox3d.fog, maxdensity ), 0, SPROP_NOSCALE ),
  70. SendPropFloat( SENDINFO_STRUCTELEM( fogparams_t, m_skybox3d.fog, HDRColorScale ), 0, SPROP_NOSCALE ),
  71. // audio data
  72. SendPropVector( SENDINFO_STRUCTARRAYELEM( m_audio.localSound, 0 ), -1, SPROP_COORD),
  73. SendPropVector( SENDINFO_STRUCTARRAYELEM( m_audio.localSound, 1 ), -1, SPROP_COORD),
  74. SendPropVector( SENDINFO_STRUCTARRAYELEM( m_audio.localSound, 2 ), -1, SPROP_COORD),
  75. SendPropVector( SENDINFO_STRUCTARRAYELEM( m_audio.localSound, 3 ), -1, SPROP_COORD),
  76. SendPropVector( SENDINFO_STRUCTARRAYELEM( m_audio.localSound, 4 ), -1, SPROP_COORD),
  77. SendPropVector( SENDINFO_STRUCTARRAYELEM( m_audio.localSound, 5 ), -1, SPROP_COORD),
  78. SendPropVector( SENDINFO_STRUCTARRAYELEM( m_audio.localSound, 6 ), -1, SPROP_COORD),
  79. SendPropVector( SENDINFO_STRUCTARRAYELEM( m_audio.localSound, 7 ), -1, SPROP_COORD),
  80. SendPropInt( SENDINFO_STRUCTELEM( audioparams_t, m_audio, soundscapeIndex ), 17, 0 ),
  81. SendPropInt( SENDINFO_STRUCTELEM( audioparams_t, m_audio, localBits ), NUM_AUDIO_LOCAL_SOUNDS, SPROP_UNSIGNED ),
  82. SendPropInt( SENDINFO_STRUCTELEM( audioparams_t, m_audio, entIndex ) ),
  83. END_SEND_TABLE()
  84. BEGIN_SIMPLE_DATADESC( fogplayerparams_t )
  85. DEFINE_FIELD( m_hCtrl, FIELD_EHANDLE ),
  86. DEFINE_FIELD( m_flTransitionTime, FIELD_FLOAT ),
  87. DEFINE_FIELD( m_OldColor, FIELD_COLOR32 ),
  88. DEFINE_FIELD( m_flOldStart, FIELD_FLOAT ),
  89. DEFINE_FIELD( m_flOldEnd, FIELD_FLOAT ),
  90. DEFINE_FIELD( m_NewColor, FIELD_COLOR32 ),
  91. DEFINE_FIELD( m_flNewStart, FIELD_FLOAT ),
  92. DEFINE_FIELD( m_flNewEnd, FIELD_FLOAT ),
  93. END_DATADESC()
  94. BEGIN_SIMPLE_DATADESC( fogparams_t )
  95. DEFINE_FIELD( enable, FIELD_BOOLEAN ),
  96. DEFINE_FIELD( blend, FIELD_BOOLEAN ),
  97. DEFINE_FIELD( dirPrimary, FIELD_VECTOR ),
  98. DEFINE_FIELD( colorPrimary, FIELD_COLOR32 ),
  99. DEFINE_FIELD( colorSecondary, FIELD_COLOR32 ),
  100. DEFINE_FIELD( start, FIELD_FLOAT ),
  101. DEFINE_FIELD( end, FIELD_FLOAT ),
  102. DEFINE_FIELD( farz, FIELD_FLOAT ),
  103. DEFINE_FIELD( maxdensity, FIELD_FLOAT ),
  104. DEFINE_FIELD( colorPrimaryLerpTo, FIELD_COLOR32 ),
  105. DEFINE_FIELD( colorSecondaryLerpTo, FIELD_COLOR32 ),
  106. DEFINE_FIELD( startLerpTo, FIELD_FLOAT ),
  107. DEFINE_FIELD( endLerpTo, FIELD_FLOAT ),
  108. DEFINE_FIELD( maxdensityLerpTo, FIELD_FLOAT ),
  109. DEFINE_FIELD( lerptime, FIELD_TIME ),
  110. DEFINE_FIELD( duration, FIELD_FLOAT ),
  111. END_DATADESC()
  112. BEGIN_SIMPLE_DATADESC( sky3dparams_t )
  113. DEFINE_FIELD( scale, FIELD_INTEGER ),
  114. DEFINE_FIELD( origin, FIELD_VECTOR ),
  115. DEFINE_FIELD( area, FIELD_INTEGER ),
  116. DEFINE_EMBEDDED( fog ),
  117. END_DATADESC()
  118. BEGIN_SIMPLE_DATADESC( audioparams_t )
  119. DEFINE_AUTO_ARRAY( localSound, FIELD_VECTOR ),
  120. DEFINE_FIELD( soundscapeIndex, FIELD_INTEGER ),
  121. DEFINE_FIELD( localBits, FIELD_INTEGER ),
  122. DEFINE_FIELD( entIndex, FIELD_INTEGER ),
  123. END_DATADESC()
  124. BEGIN_SIMPLE_DATADESC( CPlayerLocalData )
  125. DEFINE_AUTO_ARRAY( m_chAreaBits, FIELD_CHARACTER ),
  126. DEFINE_AUTO_ARRAY( m_chAreaPortalBits, FIELD_CHARACTER ),
  127. DEFINE_FIELD( m_iHideHUD, FIELD_INTEGER ),
  128. DEFINE_FIELD( m_flFOVRate, FIELD_FLOAT ),
  129. DEFINE_FIELD( m_vecOverViewpoint, FIELD_VECTOR ),
  130. DEFINE_FIELD( m_bDucked, FIELD_BOOLEAN ),
  131. DEFINE_FIELD( m_bDucking, FIELD_BOOLEAN ),
  132. DEFINE_FIELD( m_flLastDuckTime, FIELD_FLOAT ),
  133. DEFINE_FIELD( m_bInDuckJump, FIELD_BOOLEAN ),
  134. DEFINE_FIELD( m_nDuckTimeMsecs, FIELD_INTEGER ),
  135. DEFINE_FIELD( m_nDuckJumpTimeMsecs, FIELD_INTEGER ),
  136. DEFINE_FIELD( m_nJumpTimeMsecs, FIELD_INTEGER ),
  137. DEFINE_FIELD( m_nStepside, FIELD_INTEGER ),
  138. DEFINE_FIELD( m_flFallVelocity, FIELD_FLOAT ),
  139. DEFINE_FIELD( m_nOldButtons, FIELD_INTEGER ),
  140. DEFINE_FIELD( m_viewPunchAngle, FIELD_VECTOR ),
  141. DEFINE_FIELD( m_aimPunchAngle, FIELD_VECTOR ),
  142. DEFINE_FIELD( m_aimPunchAngleVel, FIELD_VECTOR ),
  143. DEFINE_FIELD( m_bDrawViewmodel, FIELD_BOOLEAN ),
  144. DEFINE_FIELD( m_bWearingSuit, FIELD_BOOLEAN ),
  145. DEFINE_FIELD( m_bPoisoned, FIELD_BOOLEAN ),
  146. DEFINE_FIELD( m_flStepSize, FIELD_FLOAT ),
  147. DEFINE_FIELD( m_bAllowAutoMovement, FIELD_BOOLEAN ),
  148. DEFINE_EMBEDDED( m_skybox3d ),
  149. DEFINE_EMBEDDED( m_fog ),
  150. DEFINE_EMBEDDED( m_audio ),
  151. // "Why don't we save this field, grandpa?"
  152. //
  153. // "You see Billy, trigger_vphysics_motion uses vPhysics to touch the player,
  154. // so if the trigger is Disabled via an input while the player is inside it,
  155. // the trigger won't get its EndTouch until the player moves. Since touchlinks
  156. // aren't saved and restored, if the we save before EndTouch is called, it
  157. // will never be called after we load."
  158. //DEFINE_FIELD( m_bSlowMovement, FIELD_BOOLEAN ),
  159. //DEFINE_FIELD( m_fTBeamEndTime, FIELD_FLOAT ),
  160. END_DATADESC()
  161. //-----------------------------------------------------------------------------
  162. // Purpose: Constructor.
  163. //-----------------------------------------------------------------------------
  164. CPlayerLocalData::CPlayerLocalData()
  165. {
  166. memset( m_chAreaBits.m_Value, 0, sizeof( m_chAreaBits.m_Value ) );
  167. memset( m_chAreaPortalBits.m_Value, 0, sizeof( m_chAreaPortalBits.m_Value ) );
  168. m_iHideHUD = 0;
  169. m_flFOVRate = 0.0f;
  170. m_vecOverViewpoint.Zero();
  171. m_bDucked = false;
  172. m_bDucking = false;
  173. m_flLastDuckTime = -1.0f;
  174. m_bInDuckJump = false;
  175. m_nDuckTimeMsecs = 0; // REI: LEGACY, should be removed
  176. m_nDuckJumpTimeMsecs = 0; // REI: LEGACY, should be removed
  177. m_nJumpTimeMsecs = 0;
  178. m_nStepside = 0;
  179. m_flFallVelocity = 0.0f;
  180. m_nOldButtons = 0;
  181. m_pOldSkyCamera = nullptr;
  182. // $$$REI What's the safe way to initialize these things in constructor?
  183. m_viewPunchAngle.m_Value.Init();
  184. m_aimPunchAngle.m_Value.Init();
  185. m_aimPunchAngleVel.m_Value.Init();
  186. m_bDrawViewmodel = true;
  187. m_bWearingSuit = false;
  188. m_bPoisoned = false;
  189. m_flStepSize = 0.0f;
  190. m_bAllowAutoMovement = true;
  191. m_bAutoAimTarget = false;
  192. // m_skybox3d?
  193. // m_PlayerFog?
  194. // m_fog?
  195. // other fields on m_audio? should this have a constructor too?
  196. m_audio.soundscapeIndex = 0;
  197. m_audio.localBits = 0;
  198. m_audio.entIndex = 0;
  199. m_bSlowMovement = false;
  200. m_fTBeamEndTime = 0.0f;
  201. }
  202. static Vector GetPlayerViewPosition( CBasePlayer *pl )
  203. {
  204. CBaseEntity *pView = pl->GetViewEntity();
  205. if ( !pView )
  206. {
  207. pView = pl;
  208. }
  209. return pView->EyePosition();
  210. }
  211. void CPlayerLocalData::UpdateAreaBits( CBasePlayer *pl, unsigned char chAreaPortalBits[MAX_AREA_PORTAL_STATE_BYTES] )
  212. {
  213. Vector origin = GetPlayerViewPosition(pl);
  214. unsigned char tempBits[32] = { 0 };
  215. COMPILE_TIME_ASSERT( ( sizeof( tempBits ) % 4 ) == 0 );
  216. int nDWords = sizeof( tempBits ) >> 2;
  217. COMPILE_TIME_ASSERT( sizeof( tempBits ) >= sizeof( ((CPlayerLocalData*)0)->m_chAreaBits ) );
  218. int i;
  219. int area = engine->GetArea( origin );
  220. engine->GetAreaBits( area, tempBits, sizeof( tempBits ) );
  221. CUtlVector< int > vecAreasSeen;
  222. vecAreasSeen.AddToTail( area );
  223. CUtlVector< CHandle< CBasePlayer > > &list = pl->GetSplitScreenAndPictureInPicturePlayers();
  224. for ( i = 0; i < list.Count(); ++i )
  225. {
  226. CBasePlayer *pSplit = list[ i ];
  227. if ( !pSplit )
  228. continue;
  229. unsigned char tempBits2[32] = { 0 };
  230. Vector org2 = GetPlayerViewPosition(pSplit);
  231. int area2 = engine->GetArea( org2 );
  232. // Already merged this area in?
  233. if ( vecAreasSeen.Find( area2 ) != vecAreasSeen.InvalidIndex() )
  234. continue;
  235. vecAreasSeen.AddToTail( area2 );
  236. engine->GetAreaBits( area2, tempBits2, sizeof( tempBits2 ) );
  237. unsigned int *pBase = (unsigned int *)tempBits;
  238. unsigned int *pAdd = (unsigned int *)tempBits2;
  239. // Now merge them together
  240. for ( int j = 0; j < nDWords; ++j )
  241. {
  242. *pBase++ |= *pAdd++;
  243. }
  244. }
  245. for ( i=0; i < m_chAreaBits.Count(); i++ )
  246. {
  247. if ( tempBits[i] != m_chAreaBits[ i ] )
  248. {
  249. m_chAreaBits.Set( i, tempBits[i] );
  250. }
  251. }
  252. for ( i=0; i < MAX_AREA_PORTAL_STATE_BYTES; i++ )
  253. {
  254. if ( chAreaPortalBits[i] != m_chAreaPortalBits[i] )
  255. {
  256. m_chAreaPortalBits.Set( i, chAreaPortalBits[i] );
  257. }
  258. }
  259. }
  260. //-----------------------------------------------------------------------------
  261. // Purpose: Fills in CClientData values for local player just before sending over wire
  262. // Input : player -
  263. //-----------------------------------------------------------------------------
  264. void ClientData_Update( CBasePlayer *pl )
  265. {
  266. // HACKHACK: for 3d skybox
  267. // UNDONE: Support multiple sky cameras?
  268. CSkyCamera *pSkyCamera = GetCurrentSkyCamera();
  269. if ( pSkyCamera != pl->m_Local.m_pOldSkyCamera )
  270. {
  271. pl->m_Local.m_pOldSkyCamera = pSkyCamera;
  272. pl->m_Local.m_skybox3d.CopyFrom(pSkyCamera->m_skyboxData);
  273. }
  274. else if ( !pSkyCamera )
  275. {
  276. pl->m_Local.m_skybox3d.area = 255;
  277. }
  278. }
  279. //-----------------------------------------------------------------------------
  280. // Purpose:
  281. //-----------------------------------------------------------------------------
  282. void UpdateAllClientData( void )
  283. {
  284. VPROF( "UpdateAllClientData" );
  285. SNPROF( "UpdateAllClientData" );
  286. int i;
  287. CBasePlayer *pl;
  288. for ( i = 1; i <= gpGlobals->maxClients; i++ )
  289. {
  290. pl = ( CBasePlayer * )UTIL_PlayerByIndex( i );
  291. if ( !pl )
  292. continue;
  293. ClientData_Update( pl );
  294. }
  295. }