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.

139 lines
3.7 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //===========================================================================//
  6. #include "cbase.h"
  7. #include "vgui/IInput.h"
  8. #include "materialsystem/imaterialvar.h"
  9. #include <vgui_controls/EditablePanel.h>
  10. #include <mathlib/mathlib.h>
  11. #include "view.h"
  12. #include "studio_stats.h"
  13. #include "coordsize.h"
  14. #include "collisionutils.h"
  15. enum
  16. {
  17. RSTUDIOSTATMODE_NONE = 0,
  18. RSTUDIOSTATMODE_HELDWEAPON,
  19. RSTUDIOSTATMODE_VIEWMODEL,
  20. RSTUDIOSTATMODE_VIEWMODEL_ATTACHMENT,
  21. };
  22. IClientRenderable *g_pStudioStatsEntity = NULL;
  23. static ConVar r_studio_stats( "r_studio_stats", "0", FCVAR_CHEAT );
  24. static ConVar r_studio_stats_lock( "r_studio_stats_lock", "0", FCVAR_CHEAT, "Lock the current studio stats entity selection" );
  25. static ConVar r_studio_stats_mode( "r_studio_stats_mode", "0", FCVAR_CHEAT, "Sets a mode for r_studio_stats. Modes are as follows:\n\t0 = Entity under your crosshair\n\t1 = Weapon held by player under your crosshair\n\t2 = Your viewmodel\n\t3 = The first entity attached to your viewmodel" );
  26. //-----------------------------------------------------------------------------
  27. // Purpose:
  28. //-----------------------------------------------------------------------------
  29. class CStudioStatsEnumerator : public IPartitionEnumerator
  30. {
  31. public:
  32. CStudioStatsEnumerator( Ray_t& shot )
  33. {
  34. m_rayShot = shot;
  35. m_bHit = false;
  36. }
  37. virtual IterationRetval_t EnumElement( IHandleEntity *pHandleEntity )
  38. {
  39. trace_t tr;
  40. enginetrace->ClipRayToEntity( m_rayShot, MASK_SHOT, pHandleEntity, &tr );
  41. if ( tr.fraction < 1.0 )
  42. {
  43. ICollideable *pCollideable = enginetrace->GetCollideable( pHandleEntity );
  44. IClientUnknown *pUnk = pCollideable->GetIClientUnknown();
  45. if ( pUnk )
  46. {
  47. g_pStudioStatsEntity = pUnk->GetClientRenderable();
  48. if ( g_pStudioStatsEntity )
  49. {
  50. m_bHit = true;
  51. return ITERATION_STOP;
  52. }
  53. }
  54. }
  55. return ITERATION_CONTINUE;
  56. }
  57. bool Hit( void ) const { return m_bHit; }
  58. private:
  59. Ray_t m_rayShot;
  60. bool m_bHit;
  61. };
  62. void StudioStats_FindClosestEntity( CClientRenderablesList *pClientRenderablesList )
  63. {
  64. if ( r_studio_stats_lock.GetBool() )
  65. return;
  66. g_pStudioStatsEntity = NULL;
  67. if ( r_studio_stats.GetBool() == false )
  68. return;
  69. C_BasePlayer *pPlayer = C_BasePlayer::GetLocalPlayer();
  70. if ( !pPlayer )
  71. return;
  72. int iStudioStatMode = r_studio_stats_mode.GetInt();
  73. if ( iStudioStatMode == RSTUDIOSTATMODE_VIEWMODEL )
  74. {
  75. g_pStudioStatsEntity = pPlayer->GetViewModel();
  76. return;
  77. }
  78. if ( iStudioStatMode == RSTUDIOSTATMODE_VIEWMODEL_ATTACHMENT )
  79. {
  80. C_BaseEntity *pVM = pPlayer->GetViewModel();
  81. if ( pVM )
  82. {
  83. g_pStudioStatsEntity = pVM->FirstMoveChild();
  84. }
  85. return;
  86. }
  87. trace_t tr;
  88. Vector vecStart, vecEnd;
  89. VectorMA( MainViewOrigin(), MAX_TRACE_LENGTH, MainViewForward(), vecEnd );
  90. VectorMA( MainViewOrigin(), 10, MainViewForward(), vecStart );
  91. Ray_t shotRay;
  92. shotRay.Init( vecStart, vecEnd );
  93. CStudioStatsEnumerator studioEnum( shotRay );
  94. ::partition->EnumerateElementsAlongRay( PARTITION_ALL_CLIENT_EDICTS, shotRay, false, &studioEnum );
  95. if ( g_pStudioStatsEntity )
  96. {
  97. C_BaseEntity *pEntity = g_pStudioStatsEntity->GetIClientUnknown()->GetBaseEntity();
  98. if ( pEntity && ( pEntity != C_BasePlayer::GetLocalPlayer() ) )
  99. {
  100. switch ( iStudioStatMode )
  101. {
  102. default:
  103. case RSTUDIOSTATMODE_NONE:
  104. g_pStudioStatsEntity = pEntity->GetClientRenderable();
  105. break;
  106. case RSTUDIOSTATMODE_HELDWEAPON:
  107. {
  108. C_BasePlayer *pTargetPlayer = ToBasePlayer( pEntity );
  109. if ( pTargetPlayer && pTargetPlayer->GetActiveWeapon() )
  110. {
  111. g_pStudioStatsEntity = pTargetPlayer->GetActiveWeapon()->GetClientRenderable();
  112. }
  113. }
  114. break;
  115. }
  116. }
  117. }
  118. }