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.

252 lines
5.9 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================//
  6. #include "cbase.h"
  7. #include "weapon_dodbasegun.h"
  8. #include "fx_dod_shared.h"
  9. //#include "effect_dispatch_data.h"
  10. #ifdef CLIENT_DLL
  11. #include "c_dod_player.h"
  12. #include "prediction.h"
  13. #else
  14. #include "dod_player.h"
  15. #include "te_effect_dispatch.h"
  16. #include "util.h"
  17. #include "ndebugoverlay.h"
  18. #endif
  19. #ifndef CLIENT_DLL
  20. //-----------------------------------------------------------------------------
  21. // Purpose: Only send to local player if this weapon is the active weapon
  22. // Input : *pStruct -
  23. // *pVarData -
  24. // *pRecipients -
  25. // objectID -
  26. // Output : void*
  27. //-----------------------------------------------------------------------------
  28. void* SendProxy_SendActiveLocalBaseGunDataTable( const SendProp *pProp, const void *pStruct, const void *pVarData, CSendProxyRecipients *pRecipients, int objectID )
  29. {
  30. // Get the weapon entity
  31. CBaseCombatWeapon *pWeapon = (CBaseCombatWeapon*)pVarData;
  32. if ( pWeapon )
  33. {
  34. // Only send this chunk of data to the player carrying this weapon
  35. CBasePlayer *pPlayer = ToBasePlayer( pWeapon->GetOwner() );
  36. if ( pPlayer )
  37. {
  38. pRecipients->SetOnly( pPlayer->GetClientIndex() );
  39. return (void*)pVarData;
  40. }
  41. }
  42. return NULL;
  43. }
  44. REGISTER_SEND_PROXY_NON_MODIFIED_POINTER( SendProxy_SendActiveLocalBaseGunDataTable );
  45. #endif
  46. IMPLEMENT_NETWORKCLASS_ALIASED( WeaponDODBaseGun, DT_WeaponDODBaseGun )
  47. BEGIN_NETWORK_TABLE_NOBASE( CWeaponDODBaseGun, DT_LocalActiveWeaponBaseGunData )
  48. END_NETWORK_TABLE()
  49. BEGIN_NETWORK_TABLE( CWeaponDODBaseGun, DT_WeaponDODBaseGun )
  50. #ifdef CLIENT_DLL
  51. RecvPropDataTable("LocalActiveWeaponBaseGunData", 0, 0, &REFERENCE_RECV_TABLE(DT_LocalActiveWeaponBaseGunData))
  52. #else
  53. SendPropDataTable("LocalActiveWeaponBaseGunData", 0, &REFERENCE_SEND_TABLE(DT_LocalActiveWeaponBaseGunData), SendProxy_SendActiveLocalBaseGunDataTable )
  54. #endif
  55. END_NETWORK_TABLE()
  56. #ifdef CLIENT_DLL
  57. BEGIN_PREDICTION_DATA( CWeaponDODBaseGun )
  58. END_PREDICTION_DATA()
  59. #else
  60. BEGIN_DATADESC( CWeaponDODBaseGun )
  61. END_DATADESC()
  62. #endif
  63. LINK_ENTITY_TO_CLASS( weapon_dod_base_gun, CWeaponDODBaseGun );
  64. extern ConVar friendlyfire;
  65. CWeaponDODBaseGun::CWeaponDODBaseGun()
  66. {
  67. }
  68. void CWeaponDODBaseGun::Spawn()
  69. {
  70. DODBaseGunSpawn();
  71. SetZoomed( false );
  72. }
  73. void CWeaponDODBaseGun::Precache()
  74. {
  75. BaseClass::Precache();
  76. // Precache all weapon ejections, since every weapon will appear in the game.
  77. PrecacheModel( "models/shells/shell_small.mdl" );
  78. PrecacheModel( "models/shells/shell_medium.mdl" );
  79. PrecacheModel( "models/shells/shell_large.mdl" );
  80. PrecacheModel( "models/shells/garand_clip.mdl" );
  81. }
  82. void CWeaponDODBaseGun::PrimaryAttack()
  83. {
  84. Assert( m_pWeaponInfo );
  85. DODBaseGunFire();
  86. }
  87. void CWeaponDODBaseGun::DODBaseGunSpawn( void )
  88. {
  89. WEAPON_FILE_INFO_HANDLE hWpnInfo = LookupWeaponInfoSlot( GetClassname() );
  90. Assert( hWpnInfo != GetInvalidWeaponInfoHandle() );
  91. CDODWeaponInfo *pWeaponInfo = dynamic_cast< CDODWeaponInfo* >( GetFileWeaponInfoFromHandle( hWpnInfo ) );
  92. Assert( pWeaponInfo && "Failed to get CDODWeaponInfo in weapon spawn" );
  93. m_pWeaponInfo = pWeaponInfo;
  94. BaseClass::Spawn();
  95. }
  96. float CWeaponDODBaseGun::GetWeaponAccuracy( float flPlayerSpeed )
  97. {
  98. //snipers and deployable weapons inherit this and override when we need a different accuracy
  99. float flSpread = m_pWeaponInfo->m_flAccuracy;
  100. if( flPlayerSpeed > 45 )
  101. flSpread += m_pWeaponInfo->m_flAccuracyMovePenalty;
  102. return flSpread;
  103. }
  104. bool CWeaponDODBaseGun::DODBaseGunFire()
  105. {
  106. CDODPlayer *pPlayer = ToDODPlayer( GetPlayerOwner() );
  107. Assert( pPlayer );
  108. // Out of ammo?
  109. if ( m_iClip1 <= 0 )
  110. {
  111. if (m_bFireOnEmpty)
  112. {
  113. PlayEmptySound();
  114. m_flNextPrimaryAttack = gpGlobals->curtime + 0.2;
  115. }
  116. return false;
  117. }
  118. if( pPlayer->GetWaterLevel() > 2 )
  119. {
  120. PlayEmptySound();
  121. m_flNextPrimaryAttack = gpGlobals->curtime + 1.0;
  122. return false;
  123. }
  124. /* decrement before calling PlayPrimaryAttackAnim, so we can play the empty anim if necessary */
  125. m_iClip1--;
  126. SendWeaponAnim( GetPrimaryAttackActivity() );
  127. // player "shoot" animation
  128. pPlayer->SetAnimation( PLAYER_ATTACK1 );
  129. FX_FireBullets(
  130. pPlayer->entindex(),
  131. pPlayer->Weapon_ShootPosition(),
  132. pPlayer->EyeAngles() + pPlayer->GetPunchAngle(),
  133. GetWeaponID(),
  134. Primary_Mode,
  135. CBaseEntity::GetPredictionRandomSeed() & 255,
  136. GetWeaponAccuracy( pPlayer->GetAbsVelocity().Length2D() ) );
  137. DoFireEffects();
  138. #ifndef CLIENT_DLL
  139. IGameEvent * event = gameeventmanager->CreateEvent( "dod_stats_weapon_attack" );
  140. if ( event )
  141. {
  142. event->SetInt( "attacker", pPlayer->GetUserID() );
  143. event->SetInt( "weapon", GetStatsWeaponID() );
  144. gameeventmanager->FireEvent( event );
  145. }
  146. #endif //CLIENT_DLL
  147. #ifdef CLIENT_DLL
  148. CDODPlayer *p = ToDODPlayer( GetPlayerOwner() );
  149. if ( prediction->IsFirstTimePredicted() )
  150. p->DoRecoil( GetWeaponID(), GetRecoil() );
  151. #endif
  152. m_flNextPrimaryAttack = m_flNextSecondaryAttack = gpGlobals->curtime + GetFireDelay();
  153. m_flTimeWeaponIdle = gpGlobals->curtime + m_pWeaponInfo->m_flTimeToIdleAfterFire;
  154. return true;
  155. }
  156. float CWeaponDODBaseGun::GetFireDelay( void )
  157. {
  158. return m_pWeaponInfo->m_flFireDelay;
  159. }
  160. void CWeaponDODBaseGun::DoFireEffects()
  161. {
  162. CBasePlayer *pPlayer = GetPlayerOwner();
  163. if ( pPlayer )
  164. pPlayer->DoMuzzleFlash();
  165. }
  166. bool CWeaponDODBaseGun::Reload()
  167. {
  168. CBasePlayer *pPlayer = GetPlayerOwner();
  169. if (pPlayer->GetAmmoCount( GetPrimaryAmmoType() ) <= 0 && m_iClip1 <= 0 )
  170. {
  171. CDODPlayer *pDODPlayer = ToDODPlayer( pPlayer );
  172. pDODPlayer->HintMessage( HINT_AMMO_EXHAUSTED );
  173. return false;
  174. }
  175. int iResult = DefaultReload( GetMaxClip1(), GetMaxClip2(), GetReloadActivity() );
  176. if ( !iResult )
  177. return false;
  178. pPlayer->SetAnimation( PLAYER_RELOAD );
  179. return true;
  180. }
  181. Activity CWeaponDODBaseGun::GetPrimaryAttackActivity( void )
  182. {
  183. return ACT_VM_PRIMARYATTACK;
  184. }
  185. Activity CWeaponDODBaseGun::GetReloadActivity( void )
  186. {
  187. return ACT_VM_RELOAD;
  188. }
  189. Activity CWeaponDODBaseGun::GetDrawActivity( void )
  190. {
  191. return ACT_VM_DRAW;
  192. }