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.

150 lines
4.2 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================
  6. #include "cbase.h"
  7. #include "dod_viewmodel.h"
  8. #ifdef CLIENT_DLL
  9. #include "c_dod_player.h"
  10. #include "prediction.h"
  11. #endif
  12. // memdbgon must be the last include file in a .cpp file!!!
  13. #include "tier0/memdbgon.h"
  14. LINK_ENTITY_TO_CLASS( dod_viewmodel, CDODViewModel );
  15. IMPLEMENT_NETWORKCLASS_ALIASED( DODViewModel, DT_DODViewModel )
  16. BEGIN_NETWORK_TABLE( CDODViewModel, DT_DODViewModel )
  17. END_NETWORK_TABLE()
  18. //-----------------------------------------------------------------------------
  19. // Purpose:
  20. //-----------------------------------------------------------------------------
  21. #ifdef CLIENT_DLL
  22. CDODViewModel::CDODViewModel() : m_LagAnglesHistory("CDODViewModel::m_LagAnglesHistory")
  23. {
  24. m_vLagAngles.Init();
  25. m_LagAnglesHistory.Setup( &m_vLagAngles, 0 );
  26. m_vLoweredWeaponOffset.Init();
  27. }
  28. #else
  29. CDODViewModel::CDODViewModel()
  30. {
  31. }
  32. #endif
  33. //-----------------------------------------------------------------------------
  34. // Purpose:
  35. //-----------------------------------------------------------------------------
  36. CDODViewModel::~CDODViewModel()
  37. {
  38. }
  39. #ifdef CLIENT_DLL
  40. ConVar cl_wpn_sway_interp( "cl_wpn_sway_interp", "0.1", FCVAR_CLIENTDLL );
  41. ConVar cl_wpn_sway_scale( "cl_wpn_sway_scale", "2.6", FCVAR_CLIENTDLL );
  42. #endif
  43. void CDODViewModel::CalcViewModelLag( Vector& origin, QAngle& angles, QAngle& original_angles )
  44. {
  45. #ifdef CLIENT_DLL
  46. if ( prediction->InPrediction() )
  47. {
  48. return;
  49. }
  50. float flSwayScale = cl_wpn_sway_scale.GetFloat();
  51. CWeaponDODBase *pWeapon = dynamic_cast<CWeaponDODBase*>(GetWeapon());
  52. if ( pWeapon )
  53. {
  54. flSwayScale *= pWeapon->GetViewModelSwayScale();
  55. }
  56. // Calculate our drift
  57. Vector forward, right, up;
  58. AngleVectors( angles, &forward, &right, &up );
  59. // Add an entry to the history.
  60. m_vLagAngles = angles;
  61. m_LagAnglesHistory.NoteChanged( gpGlobals->curtime, cl_wpn_sway_interp.GetFloat(), false );
  62. // Interpolate back 100ms.
  63. m_LagAnglesHistory.Interpolate( gpGlobals->curtime, cl_wpn_sway_interp.GetFloat() );
  64. // Now take the 100ms angle difference and figure out how far the forward vector moved in local space.
  65. Vector vLaggedForward;
  66. QAngle angleDiff = m_vLagAngles - angles;
  67. AngleVectors( -angleDiff, &vLaggedForward, 0, 0 );
  68. Vector vForwardDiff = Vector(1,0,0) - vLaggedForward;
  69. // Now offset the origin using that.
  70. vForwardDiff *= flSwayScale;
  71. origin += forward*vForwardDiff.x + right*-vForwardDiff.y + up*vForwardDiff.z;
  72. #endif
  73. }
  74. #ifdef CLIENT_DLL
  75. ConVar cl_gunlowerangle( "cl_gunlowerangle", "30", FCVAR_CLIENTDLL );
  76. ConVar cl_gunlowerspeed( "cl_gunlowerspeed", "2", FCVAR_CLIENTDLL );
  77. ConVar cl_test_vm_offset( "cl_test_vm_offset", "0 0 0", FCVAR_CHEAT | FCVAR_CLIENTDLL );
  78. #endif
  79. void CDODViewModel::CalcViewModelView( CBasePlayer *owner, const Vector& eyePosition, const QAngle& eyeAngles )
  80. {
  81. #if defined( CLIENT_DLL )
  82. Vector vecNewOrigin = eyePosition;
  83. QAngle vecNewAngles = eyeAngles;
  84. // Check for lowering the weapon
  85. C_DODPlayer *pPlayer = ToDODPlayer( owner );
  86. Assert( pPlayer );
  87. bool bLowered = pPlayer->IsWeaponLowered();
  88. QAngle vecLoweredAngles(0,0,0);
  89. m_vLoweredWeaponOffset.x = Approach( bLowered ? cl_gunlowerangle.GetFloat() : 0, m_vLoweredWeaponOffset.x, cl_gunlowerspeed.GetFloat() );
  90. vecLoweredAngles.x += m_vLoweredWeaponOffset.x;
  91. vecNewAngles += vecLoweredAngles;
  92. Vector forward, right, up;
  93. AngleVectors( vecNewAngles, &forward, &right, &up );
  94. Vector test;
  95. const char *szTestOffset = cl_test_vm_offset.GetString();
  96. sscanf( szTestOffset, " %f %f %f", &test[0], &test[1], &test[2] );
  97. // cvar cl_test_vm_offset overrides calculated view model offset
  98. if ( test.Length() > 0 )
  99. {
  100. vecNewOrigin += forward * test[0] + right * test[1] + up * test[2];
  101. }
  102. else
  103. {
  104. // Move the view model origin between the script standing and prone position
  105. // based on the current view height
  106. CWeaponDODBase *pWeapon = dynamic_cast<CWeaponDODBase*>(GetWeapon());
  107. if ( pWeapon )
  108. {
  109. Vector offset = pWeapon->GetDesiredViewModelOffset( pPlayer );
  110. // add our offset in the proper direction
  111. vecNewOrigin += forward * offset[0] + right * offset[1] + up * offset[2];
  112. }
  113. }
  114. BaseClass::CalcViewModelView( owner, vecNewOrigin, vecNewAngles );
  115. #endif
  116. }