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.

469 lines
15 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================//
  6. #include "cbase.h"
  7. #include "player.h"
  8. #include "usercmd.h"
  9. #include "igamemovement.h"
  10. #include "mathlib/mathlib.h"
  11. #include "client.h"
  12. #include "player_command.h"
  13. #include "movehelper_server.h"
  14. #include "iservervehicle.h"
  15. #include "tier0/vprof.h"
  16. // memdbgon must be the last include file in a .cpp file!!!
  17. #include "tier0/memdbgon.h"
  18. extern IGameMovement *g_pGameMovement;
  19. extern CMoveData *g_pMoveData; // This is a global because it is subclassed by each game.
  20. extern ConVar sv_noclipduringpause;
  21. ConVar sv_maxusrcmdprocessticks_warning( "sv_maxusrcmdprocessticks_warning", "-1", FCVAR_NONE, "Print a warning when user commands get dropped due to insufficient usrcmd ticks allocated, number of seconds to throttle, negative disabled" );
  22. static ConVar sv_maxusrcmdprocessticks_holdaim( "sv_maxusrcmdprocessticks_holdaim", "1", FCVAR_CHEAT, "Hold client aim for multiple server sim ticks when client-issued usrcmd contains multiple actions (0: off; 1: hold this server tick; 2+: hold multiple ticks)" );
  23. //-----------------------------------------------------------------------------
  24. // Purpose:
  25. //-----------------------------------------------------------------------------
  26. CPlayerMove::CPlayerMove( void )
  27. {
  28. }
  29. //-----------------------------------------------------------------------------
  30. // Purpose: We're about to run this usercmd for the specified player. We can set up groupinfo and masking here, etc.
  31. // This is the time to examine the usercmd for anything extra. This call happens even if think does not.
  32. // Input : *player -
  33. // *cmd -
  34. //-----------------------------------------------------------------------------
  35. void CPlayerMove::StartCommand( CBasePlayer *player, CUserCmd *cmd )
  36. {
  37. VPROF( "CPlayerMove::StartCommand" );
  38. #if !defined( NO_ENTITY_PREDICTION )
  39. CPredictableId::ResetInstanceCounters();
  40. #endif
  41. player->m_pCurrentCommand = cmd;
  42. CBaseEntity::SetPredictionRandomSeed( cmd );
  43. CBaseEntity::SetPredictionPlayer( player );
  44. #if defined (HL2_DLL)
  45. // pull out backchannel data and move this out
  46. int i;
  47. for (i = 0; i < cmd->entitygroundcontact.Count(); i++)
  48. {
  49. int entindex = cmd->entitygroundcontact[i].entindex;
  50. CBaseEntity *pEntity = CBaseEntity::Instance( engine->PEntityOfEntIndex( entindex) );
  51. if (pEntity)
  52. {
  53. CBaseAnimating *pAnimating = pEntity->GetBaseAnimating();
  54. if (pAnimating)
  55. {
  56. pAnimating->SetIKGroundContactInfo( cmd->entitygroundcontact[i].minheight, cmd->entitygroundcontact[i].maxheight );
  57. }
  58. }
  59. }
  60. #endif
  61. }
  62. //-----------------------------------------------------------------------------
  63. // Purpose: We've finished running a user's command
  64. // Input : *player -
  65. //-----------------------------------------------------------------------------
  66. void CPlayerMove::FinishCommand( CBasePlayer *player )
  67. {
  68. VPROF( "CPlayerMove::FinishCommand" );
  69. player->m_pCurrentCommand = NULL;
  70. CBaseEntity::SetPredictionRandomSeed( NULL );
  71. CBaseEntity::SetPredictionPlayer( NULL );
  72. }
  73. //-----------------------------------------------------------------------------
  74. // Purpose: Checks if the player is standing on a moving entity and adjusts velocity and
  75. // basevelocity appropriately
  76. // Input : *player -
  77. // frametime -
  78. //-----------------------------------------------------------------------------
  79. void CPlayerMove::CheckMovingGround( CBasePlayer *player, double frametime )
  80. {
  81. VPROF( "CPlayerMove::CheckMovingGround()" );
  82. CBaseEntity *groundentity;
  83. if ( player->GetFlags() & FL_ONGROUND )
  84. {
  85. groundentity = player->GetGroundEntity();
  86. if ( groundentity && ( groundentity->GetFlags() & FL_CONVEYOR) )
  87. {
  88. Vector vecNewVelocity;
  89. groundentity->GetGroundVelocityToApply( vecNewVelocity );
  90. if ( player->GetFlags() & FL_BASEVELOCITY )
  91. {
  92. vecNewVelocity += player->GetBaseVelocity();
  93. }
  94. player->SetBaseVelocity( vecNewVelocity );
  95. player->AddFlag( FL_BASEVELOCITY );
  96. }
  97. }
  98. if ( !( player->GetFlags() & FL_BASEVELOCITY ) )
  99. {
  100. // Apply momentum (add in half of the previous frame of velocity first)
  101. player->ApplyAbsVelocityImpulse( (1.0 + ( frametime * 0.5 )) * player->GetBaseVelocity() );
  102. player->SetBaseVelocity( vec3_origin );
  103. }
  104. player->RemoveFlag( FL_BASEVELOCITY );
  105. }
  106. //-----------------------------------------------------------------------------
  107. // Purpose: Prepares for running movement
  108. // Input : *player -
  109. // *ucmd -
  110. // *pHelper -
  111. // *move -
  112. // time -
  113. //-----------------------------------------------------------------------------
  114. void CPlayerMove::SetupMove( CBasePlayer *player, CUserCmd *ucmd, IMoveHelper *pHelper, CMoveData *move )
  115. {
  116. VPROF( "CPlayerMove::SetupMove" );
  117. // Allow sound, etc. to be created by movement code
  118. move->m_bFirstRunOfFunctions = true;
  119. move->m_bGameCodeMovedPlayer = false;
  120. if ( player->GetPreviouslyPredictedOrigin() != player->GetAbsOrigin() )
  121. {
  122. move->m_bGameCodeMovedPlayer = true;
  123. }
  124. // Prepare the usercmd fields
  125. move->m_nImpulseCommand = ucmd->impulse;
  126. move->m_vecViewAngles = ucmd->viewangles;
  127. CBaseEntity *pMoveParent = player->GetMoveParent();
  128. if (!pMoveParent)
  129. {
  130. move->m_vecAbsViewAngles = move->m_vecViewAngles;
  131. }
  132. else
  133. {
  134. matrix3x4_t viewToParent, viewToWorld;
  135. AngleMatrix( move->m_vecViewAngles, viewToParent );
  136. ConcatTransforms( pMoveParent->EntityToWorldTransform(), viewToParent, viewToWorld );
  137. MatrixAngles( viewToWorld, move->m_vecAbsViewAngles );
  138. }
  139. move->m_nButtons = ucmd->buttons;
  140. // Ingore buttons for movement if at controls
  141. if ( player->GetFlags() & FL_ATCONTROLS )
  142. {
  143. move->m_flForwardMove = 0;
  144. move->m_flSideMove = 0;
  145. move->m_flUpMove = 0;
  146. }
  147. else
  148. {
  149. move->m_flForwardMove = ucmd->forwardmove;
  150. move->m_flSideMove = ucmd->sidemove;
  151. move->m_flUpMove = ucmd->upmove;
  152. }
  153. // Prepare remaining fields
  154. move->m_flClientMaxSpeed = player->m_flMaxspeed;
  155. move->m_nOldButtons = player->m_Local.m_nOldButtons;
  156. move->m_flOldForwardMove = player->m_Local.m_flOldForwardMove;
  157. move->m_vecAngles = player->pl.v_angle;
  158. move->m_vecVelocity = player->GetAbsVelocity();
  159. move->m_nPlayerHandle = player;
  160. move->SetAbsOrigin( player->GetAbsOrigin() );
  161. // Copy constraint information
  162. if ( player->m_hConstraintEntity.Get() )
  163. move->m_vecConstraintCenter = player->m_hConstraintEntity.Get()->GetAbsOrigin();
  164. else
  165. move->m_vecConstraintCenter = player->m_vecConstraintCenter;
  166. move->m_flConstraintRadius = player->m_flConstraintRadius;
  167. move->m_flConstraintWidth = player->m_flConstraintWidth;
  168. move->m_flConstraintSpeedFactor = player->m_flConstraintSpeedFactor;
  169. }
  170. //-----------------------------------------------------------------------------
  171. // Purpose: Finishes running movement
  172. // Input : *player -
  173. // *move -
  174. // *ucmd -
  175. // time -
  176. //-----------------------------------------------------------------------------
  177. void CPlayerMove::FinishMove( CBasePlayer *player, CUserCmd *ucmd, CMoveData *move )
  178. {
  179. VPROF( "CPlayerMove::FinishMove" );
  180. // NOTE: Don't copy this. the movement code modifies its local copy but is not expecting to be authoritative
  181. //player->m_flMaxspeed = move->m_flClientMaxSpeed;
  182. player->SetAbsOrigin( move->GetAbsOrigin() );
  183. player->SetAbsVelocity( move->m_vecVelocity );
  184. player->SetPreviouslyPredictedOrigin( move->GetAbsOrigin() );
  185. player->m_Local.m_nOldButtons = move->m_nButtons;
  186. // Convert final pitch to body pitch
  187. float pitch = move->m_vecAngles[ PITCH ];
  188. if ( pitch > 180.0f )
  189. {
  190. pitch -= 360.0f;
  191. }
  192. pitch = clamp( pitch, -90.f, 90.f );
  193. move->m_vecAngles[ PITCH ] = pitch;
  194. player->SetBodyPitch( pitch );
  195. player->SetLocalAngles( move->m_vecAngles );
  196. // The class had better not have changed during the move!!
  197. if ( player->m_hConstraintEntity )
  198. Assert( move->m_vecConstraintCenter == player->m_hConstraintEntity.Get()->GetAbsOrigin() );
  199. else
  200. Assert( move->m_vecConstraintCenter == player->m_vecConstraintCenter );
  201. Assert( move->m_flConstraintRadius == player->m_flConstraintRadius );
  202. Assert( move->m_flConstraintWidth == player->m_flConstraintWidth );
  203. Assert( move->m_flConstraintSpeedFactor == player->m_flConstraintSpeedFactor );
  204. }
  205. //-----------------------------------------------------------------------------
  206. // Purpose: Called before player thinks
  207. // Input : *player -
  208. // thinktime -
  209. //-----------------------------------------------------------------------------
  210. void CPlayerMove::RunPreThink( CBasePlayer *player )
  211. {
  212. VPROF( "CPlayerMove::RunPreThink" );
  213. // Run think functions on the player
  214. VPROF_SCOPE_BEGIN( "player->PhysicsRunThink()" );
  215. if ( !player->PhysicsRunThink() )
  216. return;
  217. VPROF_SCOPE_END();
  218. VPROF_SCOPE_BEGIN( "g_pGameRules->PlayerThink( player )" );
  219. // Called every frame to let game rules do any specific think logic for the player
  220. g_pGameRules->PlayerThink( player );
  221. VPROF_SCOPE_END();
  222. VPROF_SCOPE_BEGIN( "player->PreThink()" );
  223. player->PreThink();
  224. VPROF_SCOPE_END();
  225. }
  226. //-----------------------------------------------------------------------------
  227. // Purpose: Runs the PLAYER's thinking code if time. There is some play in the exact time the think
  228. // function will be called, because it is called before any movement is done
  229. // in a frame. Not used for pushmove objects, because they must be exact.
  230. // Returns false if the entity removed itself.
  231. // Input : *ent -
  232. // frametime -
  233. // clienttimebase -
  234. // Output : void CPlayerMove::RunThink
  235. //-----------------------------------------------------------------------------
  236. void CPlayerMove::RunThink (CBasePlayer *player, double frametime )
  237. {
  238. VPROF( "CPlayerMove::RunThink" );
  239. int thinktick = player->GetNextThinkTick();
  240. if ( thinktick <= 0 || thinktick > player->m_nTickBase )
  241. return;
  242. //gpGlobals->curtime = thinktime;
  243. player->SetNextThink( TICK_NEVER_THINK );
  244. // Think
  245. player->Think();
  246. }
  247. //-----------------------------------------------------------------------------
  248. // Purpose: Called after player movement
  249. // Input : *player -
  250. // thinktime -
  251. // frametime -
  252. //-----------------------------------------------------------------------------
  253. void CPlayerMove::RunPostThink( CBasePlayer *player )
  254. {
  255. VPROF( "CPlayerMove::RunPostThink" );
  256. // Run post-think
  257. player->PostThink();
  258. }
  259. void CommentarySystem_PePlayerRunCommand( CBasePlayer *player, CUserCmd *ucmd );
  260. //-----------------------------------------------------------------------------
  261. // Purpose: Runs movement commands for the player
  262. // Input : *player -
  263. // *ucmd -
  264. // *moveHelper -
  265. // Output : void CPlayerMove::RunCommand
  266. //-----------------------------------------------------------------------------
  267. void CPlayerMove::RunCommand ( CBasePlayer *player, CUserCmd *ucmd, IMoveHelper *moveHelper )
  268. {
  269. const float playerCurTime = player->m_nTickBase * TICK_INTERVAL;
  270. const float playerFrameTime = player->m_bGamePaused ? 0 : TICK_INTERVAL;
  271. const float flTimeAllowedForProcessing = player->ConsumeMovementTimeForUserCmdProcessing( playerFrameTime );
  272. if ( !player->IsBot() && ( flTimeAllowedForProcessing < playerFrameTime ) )
  273. {
  274. // Make sure that the activity in command is erased because player cheated or dropped too many packets
  275. double dblWarningFrequencyThrottle = sv_maxusrcmdprocessticks_warning.GetFloat();
  276. if ( dblWarningFrequencyThrottle >= 0 )
  277. {
  278. static double s_dblLastWarningTime = 0;
  279. double dblTimeNow = Plat_FloatTime();
  280. if ( !s_dblLastWarningTime || ( dblTimeNow - s_dblLastWarningTime >= dblWarningFrequencyThrottle ) )
  281. {
  282. s_dblLastWarningTime = dblTimeNow;
  283. Warning( "sv_maxusrcmdprocessticks_warning at server tick %u: Ignored client %s usrcmd (%.6f < %.6f)!\n", gpGlobals->tickcount, player->GetPlayerName(), flTimeAllowedForProcessing, playerFrameTime );
  284. }
  285. }
  286. return; // Don't process this command
  287. }
  288. StartCommand( player, ucmd );
  289. // Set globals appropriately
  290. gpGlobals->curtime = playerCurTime;
  291. gpGlobals->frametime = playerFrameTime;
  292. // Prevent hacked clients from sending us invalid view angles to try to get leaf server code to crash
  293. if ( !ucmd->viewangles.IsValid() || !IsEntityQAngleReasonable(ucmd->viewangles) )
  294. {
  295. ucmd->viewangles = vec3_angle;
  296. }
  297. // Add and subtract buttons we're forcing on the player
  298. ucmd->buttons |= player->m_afButtonForced;
  299. ucmd->buttons &= ~player->m_afButtonDisabled;
  300. if ( player->m_bGamePaused )
  301. {
  302. // If no clipping and cheats enabled and noclipduring game enabled, then leave
  303. // forwardmove and angles stuff in usercmd
  304. if ( player->GetMoveType() == MOVETYPE_NOCLIP &&
  305. sv_cheats->GetBool() &&
  306. sv_noclipduringpause.GetBool() )
  307. {
  308. gpGlobals->frametime = TICK_INTERVAL;
  309. }
  310. }
  311. /*
  312. // TODO: We can check whether the player is sending more commands than elapsed real time
  313. cmdtimeremaining -= ucmd->msec;
  314. if ( cmdtimeremaining < 0 )
  315. {
  316. // return;
  317. }
  318. */
  319. g_pGameMovement->StartTrackPredictionErrors( player );
  320. CommentarySystem_PePlayerRunCommand( player, ucmd );
  321. // Do weapon selection
  322. if ( ucmd->weaponselect != 0 )
  323. {
  324. CBaseCombatWeapon *weapon = dynamic_cast< CBaseCombatWeapon * >( CBaseEntity::Instance( ucmd->weaponselect ) );
  325. if ( weapon )
  326. {
  327. VPROF( "player->SelectItem()" );
  328. player->SelectItem( weapon->GetName(), ucmd->weaponsubtype );
  329. }
  330. }
  331. IServerVehicle *pVehicle = player->GetVehicle();
  332. // Latch in impulse.
  333. if ( ucmd->impulse )
  334. {
  335. // Discard impulse commands unless the vehicle allows them.
  336. // FIXME: UsingStandardWeapons seems like a bad filter for this. The flashlight is an impulse command, for example.
  337. if ( !pVehicle || player->UsingStandardWeaponsInVehicle() )
  338. {
  339. player->m_nImpulse = ucmd->impulse;
  340. }
  341. }
  342. // Update player input button states
  343. VPROF_SCOPE_BEGIN( "player->UpdateButtonState" );
  344. player->UpdateButtonState( ucmd->buttons );
  345. VPROF_SCOPE_END();
  346. CheckMovingGround( player, TICK_INTERVAL );
  347. g_pMoveData->m_vecOldAngles = player->pl.v_angle;
  348. // Copy from command to player unless game .dll has set angle using fixangle
  349. if ( player->pl.fixangle == FIXANGLE_NONE )
  350. {
  351. player->pl.v_angle = ucmd->viewangles;
  352. }
  353. else if( player->pl.fixangle == FIXANGLE_RELATIVE )
  354. {
  355. player->pl.v_angle = ucmd->viewangles + player->pl.anglechange;
  356. }
  357. // Call standard client pre-think
  358. RunPreThink( player );
  359. // Call Think if one is set
  360. RunThink( player, TICK_INTERVAL );
  361. // Setup input.
  362. SetupMove( player, ucmd, moveHelper, g_pMoveData );
  363. // Let the game do the movement.
  364. if ( !pVehicle )
  365. {
  366. VPROF( "g_pGameMovement->ProcessMovement()" );
  367. Assert( g_pGameMovement );
  368. g_pGameMovement->ProcessMovement( player, g_pMoveData );
  369. }
  370. else
  371. {
  372. VPROF( "pVehicle->ProcessMovement()" );
  373. pVehicle->ProcessMovement( player, g_pMoveData );
  374. }
  375. // Copy output
  376. FinishMove( player, ucmd, g_pMoveData );
  377. // If we have to restore the view angle then do so right now
  378. if ( !player->IsBot() && ( gpGlobals->tickcount - player->GetLockViewanglesTickNumber() < sv_maxusrcmdprocessticks_holdaim.GetInt() ) )
  379. {
  380. player->pl.v_angle = player->GetLockViewanglesData();
  381. }
  382. // Let server invoke any needed impact functions
  383. VPROF_SCOPE_BEGIN( "moveHelper->ProcessImpacts" );
  384. moveHelper->ProcessImpacts();
  385. VPROF_SCOPE_END();
  386. RunPostThink( player );
  387. g_pGameMovement->FinishTrackPredictionErrors( player );
  388. FinishCommand( player );
  389. // Let time pass
  390. if ( gpGlobals->frametime > 0 )
  391. {
  392. player->m_nTickBase++;
  393. }
  394. }