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.

162 lines
3.9 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include "cbase.h"
  8. #include "in_buttons.h"
  9. #include "takedamageinfo.h"
  10. #include "weapon_sdkbase.h"
  11. #include "ammodef.h"
  12. #if defined( CLIENT_DLL )
  13. #include "c_sdk_player.h"
  14. #else
  15. #include "sdk_player.h"
  16. #endif
  17. // ----------------------------------------------------------------------------- //
  18. // Global functions.
  19. // ----------------------------------------------------------------------------- //
  20. //--------------------------------------------------------------------------------------------------------
  21. static const char * s_WeaponAliasInfo[] =
  22. {
  23. "none", // WEAPON_NONE
  24. "mp5", // WEAPON_MP5
  25. "shotgun", // WEAPON_SHOTGUN
  26. "grenade", // WEAPON_GRENADE
  27. NULL, // WEAPON_NONE
  28. };
  29. //--------------------------------------------------------------------------------------------------------
  30. //
  31. // Given an alias, return the associated weapon ID
  32. //
  33. int AliasToWeaponID( const char *alias )
  34. {
  35. if (alias)
  36. {
  37. for( int i=0; s_WeaponAliasInfo[i] != NULL; ++i )
  38. if (!Q_stricmp( s_WeaponAliasInfo[i], alias ))
  39. return i;
  40. }
  41. return WEAPON_NONE;
  42. }
  43. //--------------------------------------------------------------------------------------------------------
  44. //
  45. // Given a weapon ID, return its alias
  46. //
  47. const char *WeaponIDToAlias( int id )
  48. {
  49. if ( (id >= WEAPON_MAX) || (id < 0) )
  50. return NULL;
  51. return s_WeaponAliasInfo[id];
  52. }
  53. // ----------------------------------------------------------------------------- //
  54. // CWeaponSDKBase tables.
  55. // ----------------------------------------------------------------------------- //
  56. IMPLEMENT_NETWORKCLASS_ALIASED( WeaponSDKBase, DT_WeaponSDKBase )
  57. BEGIN_NETWORK_TABLE( CWeaponSDKBase, DT_WeaponSDKBase )
  58. #ifdef CLIENT_DLL
  59. #else
  60. // world weapon models have no animations
  61. SendPropExclude( "DT_AnimTimeMustBeFirst", "m_flAnimTime" ),
  62. SendPropExclude( "DT_BaseAnimating", "m_nSequence" ),
  63. #endif
  64. END_NETWORK_TABLE()
  65. #ifdef CLIENT_DLL
  66. BEGIN_PREDICTION_DATA( CWeaponSDKBase )
  67. DEFINE_PRED_FIELD( m_flTimeWeaponIdle, FIELD_FLOAT, FTYPEDESC_OVERRIDE | FTYPEDESC_NOERRORCHECK ),
  68. END_PREDICTION_DATA()
  69. #endif
  70. LINK_ENTITY_TO_CLASS( weapon_sdk_base, CWeaponSDKBase );
  71. #ifdef GAME_DLL
  72. BEGIN_DATADESC( CWeaponSDKBase )
  73. // New weapon Think and Touch Functions go here..
  74. END_DATADESC()
  75. #endif
  76. // ----------------------------------------------------------------------------- //
  77. // CWeaponCSBase implementation.
  78. // ----------------------------------------------------------------------------- //
  79. CWeaponSDKBase::CWeaponSDKBase()
  80. {
  81. SetPredictionEligible( true );
  82. AddSolidFlags( FSOLID_TRIGGER ); // Nothing collides with these but it gets touches.
  83. }
  84. const CSDKWeaponInfo &CWeaponSDKBase::GetSDKWpnData() const
  85. {
  86. const FileWeaponInfo_t *pWeaponInfo = &GetWpnData();
  87. const CSDKWeaponInfo *pSDKInfo;
  88. #ifdef _DEBUG
  89. pSDKInfo = dynamic_cast< const CSDKWeaponInfo* >( pWeaponInfo );
  90. Assert( pSDKInfo );
  91. #else
  92. pSDKInfo = static_cast< const CSDKWeaponInfo* >( pWeaponInfo );
  93. #endif
  94. return *pSDKInfo;
  95. }
  96. bool CWeaponSDKBase::PlayEmptySound()
  97. {
  98. CPASAttenuationFilter filter( this );
  99. filter.UsePredictionRules();
  100. EmitSound( filter, entindex(), "Default.ClipEmpty_Rifle" );
  101. return 0;
  102. }
  103. CSDKPlayer* CWeaponSDKBase::GetPlayerOwner() const
  104. {
  105. return dynamic_cast< CSDKPlayer* >( GetOwner() );
  106. }
  107. #ifdef GAME_DLL
  108. void CWeaponSDKBase::SendReloadEvents()
  109. {
  110. CSDKPlayer *pPlayer = dynamic_cast< CSDKPlayer* >( GetOwner() );
  111. if ( !pPlayer )
  112. return;
  113. // Send a message to any clients that have this entity to play the reload.
  114. CPASFilter filter( pPlayer->GetAbsOrigin() );
  115. filter.RemoveRecipient( pPlayer );
  116. UserMessageBegin( filter, "ReloadEffect" );
  117. WRITE_SHORT( pPlayer->entindex() );
  118. MessageEnd();
  119. // Make the player play his reload animation.
  120. pPlayer->DoAnimationEvent( PLAYERANIMEVENT_RELOAD );
  121. }
  122. #endif