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.

178 lines
4.3 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include "cbase.h"
  8. #include "player_command.h"
  9. #include "player.h"
  10. #include "igamemovement.h"
  11. #include "hl_movedata.h"
  12. #include "ipredictionsystem.h"
  13. #include "iservervehicle.h"
  14. #include "hl2_player.h"
  15. #include "vehicle_base.h"
  16. #include "gamestats.h"
  17. // memdbgon must be the last include file in a .cpp file!!!
  18. #include "tier0/memdbgon.h"
  19. class CHLPlayerMove : public CPlayerMove
  20. {
  21. DECLARE_CLASS( CHLPlayerMove, CPlayerMove );
  22. public:
  23. CHLPlayerMove() :
  24. m_bWasInVehicle( false ),
  25. m_bVehicleFlipped( false ),
  26. m_bInGodMode( false ),
  27. m_bInNoClip( false )
  28. {
  29. m_vecSaveOrigin.Init();
  30. }
  31. void SetupMove( CBasePlayer *player, CUserCmd *ucmd, IMoveHelper *pHelper, CMoveData *move );
  32. void FinishMove( CBasePlayer *player, CUserCmd *ucmd, CMoveData *move );
  33. private:
  34. Vector m_vecSaveOrigin;
  35. bool m_bWasInVehicle;
  36. bool m_bVehicleFlipped;
  37. bool m_bInGodMode;
  38. bool m_bInNoClip;
  39. };
  40. //
  41. //
  42. // PlayerMove Interface
  43. static CHLPlayerMove g_PlayerMove;
  44. //-----------------------------------------------------------------------------
  45. // Singleton accessor
  46. //-----------------------------------------------------------------------------
  47. CPlayerMove *PlayerMove()
  48. {
  49. return &g_PlayerMove;
  50. }
  51. //
  52. static CHLMoveData g_HLMoveData;
  53. CMoveData *g_pMoveData = &g_HLMoveData;
  54. IPredictionSystem *IPredictionSystem::g_pPredictionSystems = NULL;
  55. void CHLPlayerMove::SetupMove( CBasePlayer *player, CUserCmd *ucmd, IMoveHelper *pHelper, CMoveData *move )
  56. {
  57. // Call the default SetupMove code.
  58. BaseClass::SetupMove( player, ucmd, pHelper, move );
  59. // Convert to HL2 data.
  60. CHL2_Player *pHLPlayer = static_cast<CHL2_Player*>( player );
  61. Assert( pHLPlayer );
  62. CHLMoveData *pHLMove = static_cast<CHLMoveData*>( move );
  63. Assert( pHLMove );
  64. player->m_flForwardMove = ucmd->forwardmove;
  65. player->m_flSideMove = ucmd->sidemove;
  66. pHLMove->m_bIsSprinting = pHLPlayer->IsSprinting();
  67. if ( gpGlobals->frametime != 0 )
  68. {
  69. IServerVehicle *pVehicle = player->GetVehicle();
  70. if ( pVehicle )
  71. {
  72. pVehicle->SetupMove( player, ucmd, pHelper, move );
  73. if ( !m_bWasInVehicle )
  74. {
  75. m_bWasInVehicle = true;
  76. m_vecSaveOrigin.Init();
  77. }
  78. }
  79. else
  80. {
  81. m_vecSaveOrigin = player->GetAbsOrigin();
  82. if ( m_bWasInVehicle )
  83. {
  84. m_bWasInVehicle = false;
  85. }
  86. }
  87. }
  88. }
  89. void CHLPlayerMove::FinishMove( CBasePlayer *player, CUserCmd *ucmd, CMoveData *move )
  90. {
  91. // Call the default FinishMove code.
  92. BaseClass::FinishMove( player, ucmd, move );
  93. if ( gpGlobals->frametime != 0 )
  94. {
  95. float distance = 0.0f;
  96. IServerVehicle *pVehicle = player->GetVehicle();
  97. if ( pVehicle )
  98. {
  99. pVehicle->FinishMove( player, ucmd, move );
  100. IPhysicsObject *obj = player->GetVehicleEntity()->VPhysicsGetObject();
  101. if ( obj )
  102. {
  103. Vector newPos;
  104. obj->GetPosition( &newPos, NULL );
  105. distance = VectorLength( newPos - m_vecSaveOrigin );
  106. if ( m_vecSaveOrigin == vec3_origin || distance > 100.0f )
  107. distance = 0.0f;
  108. m_vecSaveOrigin = newPos;
  109. }
  110. CPropVehicleDriveable *driveable = dynamic_cast< CPropVehicleDriveable * >( player->GetVehicleEntity() );
  111. if ( driveable )
  112. {
  113. // Overturned and at rest (if still moving it can fix itself)
  114. bool bFlipped = driveable->IsOverturned() && ( distance < 0.5f );
  115. if ( m_bVehicleFlipped != bFlipped )
  116. {
  117. if ( bFlipped )
  118. {
  119. gamestats->Event_FlippedVehicle( player, driveable );
  120. }
  121. m_bVehicleFlipped = bFlipped;
  122. }
  123. }
  124. else
  125. {
  126. m_bVehicleFlipped = false;
  127. }
  128. }
  129. else
  130. {
  131. m_bVehicleFlipped = false;
  132. distance = VectorLength( player->GetAbsOrigin() - m_vecSaveOrigin );
  133. }
  134. if ( distance > 0 )
  135. {
  136. gamestats->Event_PlayerTraveled( player, distance, pVehicle ? true : false, !pVehicle && static_cast< CHL2_Player * >( player )->IsSprinting() );
  137. }
  138. }
  139. bool bGodMode = ( player->GetFlags() & FL_GODMODE ) ? true : false;
  140. if ( m_bInGodMode != bGodMode )
  141. {
  142. m_bInGodMode = bGodMode;
  143. if ( bGodMode )
  144. {
  145. gamestats->Event_PlayerEnteredGodMode( player );
  146. }
  147. }
  148. bool bNoClip = ( player->GetMoveType() == MOVETYPE_NOCLIP );
  149. if ( m_bInNoClip != bNoClip )
  150. {
  151. m_bInNoClip = bNoClip;
  152. if ( bNoClip )
  153. {
  154. gamestats->Event_PlayerEnteredNoClip( player );
  155. }
  156. }
  157. }