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.

512 lines
15 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include "cbase.h"
  8. #include "basecombatweapon.h"
  9. #include "npcevent.h"
  10. #include "basecombatcharacter.h"
  11. #include "ai_basenpc.h"
  12. #include "player.h"
  13. #include "weapon_ar2.h"
  14. #include "grenade_ar2.h"
  15. #include "gamerules.h"
  16. #include "game.h"
  17. #include "in_buttons.h"
  18. #include "ai_memory.h"
  19. #include "soundent.h"
  20. #include "hl2_player.h"
  21. #include "EntityFlame.h"
  22. #include "weapon_flaregun.h"
  23. #include "te_effect_dispatch.h"
  24. #include "prop_combine_ball.h"
  25. #include "beam_shared.h"
  26. #include "npc_combine.h"
  27. #include "rumble_shared.h"
  28. #include "gamestats.h"
  29. // memdbgon must be the last include file in a .cpp file!!!
  30. #include "tier0/memdbgon.h"
  31. ConVar sk_weapon_ar2_alt_fire_radius( "sk_weapon_ar2_alt_fire_radius", "10" );
  32. ConVar sk_weapon_ar2_alt_fire_duration( "sk_weapon_ar2_alt_fire_duration", "2" );
  33. ConVar sk_weapon_ar2_alt_fire_mass( "sk_weapon_ar2_alt_fire_mass", "150" );
  34. //=========================================================
  35. //=========================================================
  36. BEGIN_DATADESC( CWeaponAR2 )
  37. DEFINE_FIELD( m_flDelayedFire, FIELD_TIME ),
  38. DEFINE_FIELD( m_bShotDelayed, FIELD_BOOLEAN ),
  39. //DEFINE_FIELD( m_nVentPose, FIELD_INTEGER ),
  40. END_DATADESC()
  41. IMPLEMENT_SERVERCLASS_ST(CWeaponAR2, DT_WeaponAR2)
  42. END_SEND_TABLE()
  43. LINK_ENTITY_TO_CLASS( weapon_ar2, CWeaponAR2 );
  44. PRECACHE_WEAPON_REGISTER(weapon_ar2);
  45. acttable_t CWeaponAR2::m_acttable[] =
  46. {
  47. { ACT_RANGE_ATTACK1, ACT_RANGE_ATTACK_AR2, true },
  48. { ACT_RELOAD, ACT_RELOAD_SMG1, true }, // FIXME: hook to AR2 unique
  49. { ACT_IDLE, ACT_IDLE_SMG1, true }, // FIXME: hook to AR2 unique
  50. { ACT_IDLE_ANGRY, ACT_IDLE_ANGRY_SMG1, true }, // FIXME: hook to AR2 unique
  51. { ACT_WALK, ACT_WALK_RIFLE, true },
  52. // Readiness activities (not aiming)
  53. { ACT_IDLE_RELAXED, ACT_IDLE_SMG1_RELAXED, false },//never aims
  54. { ACT_IDLE_STIMULATED, ACT_IDLE_SMG1_STIMULATED, false },
  55. { ACT_IDLE_AGITATED, ACT_IDLE_ANGRY_SMG1, false },//always aims
  56. { ACT_WALK_RELAXED, ACT_WALK_RIFLE_RELAXED, false },//never aims
  57. { ACT_WALK_STIMULATED, ACT_WALK_RIFLE_STIMULATED, false },
  58. { ACT_WALK_AGITATED, ACT_WALK_AIM_RIFLE, false },//always aims
  59. { ACT_RUN_RELAXED, ACT_RUN_RIFLE_RELAXED, false },//never aims
  60. { ACT_RUN_STIMULATED, ACT_RUN_RIFLE_STIMULATED, false },
  61. { ACT_RUN_AGITATED, ACT_RUN_AIM_RIFLE, false },//always aims
  62. // Readiness activities (aiming)
  63. { ACT_IDLE_AIM_RELAXED, ACT_IDLE_SMG1_RELAXED, false },//never aims
  64. { ACT_IDLE_AIM_STIMULATED, ACT_IDLE_AIM_RIFLE_STIMULATED, false },
  65. { ACT_IDLE_AIM_AGITATED, ACT_IDLE_ANGRY_SMG1, false },//always aims
  66. { ACT_WALK_AIM_RELAXED, ACT_WALK_RIFLE_RELAXED, false },//never aims
  67. { ACT_WALK_AIM_STIMULATED, ACT_WALK_AIM_RIFLE_STIMULATED, false },
  68. { ACT_WALK_AIM_AGITATED, ACT_WALK_AIM_RIFLE, false },//always aims
  69. { ACT_RUN_AIM_RELAXED, ACT_RUN_RIFLE_RELAXED, false },//never aims
  70. { ACT_RUN_AIM_STIMULATED, ACT_RUN_AIM_RIFLE_STIMULATED, false },
  71. { ACT_RUN_AIM_AGITATED, ACT_RUN_AIM_RIFLE, false },//always aims
  72. //End readiness activities
  73. { ACT_WALK_AIM, ACT_WALK_AIM_RIFLE, true },
  74. { ACT_WALK_CROUCH, ACT_WALK_CROUCH_RIFLE, true },
  75. { ACT_WALK_CROUCH_AIM, ACT_WALK_CROUCH_AIM_RIFLE, true },
  76. { ACT_RUN, ACT_RUN_RIFLE, true },
  77. { ACT_RUN_AIM, ACT_RUN_AIM_RIFLE, true },
  78. { ACT_RUN_CROUCH, ACT_RUN_CROUCH_RIFLE, true },
  79. { ACT_RUN_CROUCH_AIM, ACT_RUN_CROUCH_AIM_RIFLE, true },
  80. { ACT_GESTURE_RANGE_ATTACK1, ACT_GESTURE_RANGE_ATTACK_AR2, false },
  81. { ACT_COVER_LOW, ACT_COVER_SMG1_LOW, false }, // FIXME: hook to AR2 unique
  82. { ACT_RANGE_AIM_LOW, ACT_RANGE_AIM_AR2_LOW, false },
  83. { ACT_RANGE_ATTACK1_LOW, ACT_RANGE_ATTACK_SMG1_LOW, true }, // FIXME: hook to AR2 unique
  84. { ACT_RELOAD_LOW, ACT_RELOAD_SMG1_LOW, false },
  85. { ACT_GESTURE_RELOAD, ACT_GESTURE_RELOAD_SMG1, true },
  86. // { ACT_RANGE_ATTACK2, ACT_RANGE_ATTACK_AR2_GRENADE, true },
  87. };
  88. IMPLEMENT_ACTTABLE(CWeaponAR2);
  89. CWeaponAR2::CWeaponAR2( )
  90. {
  91. m_fMinRange1 = 65;
  92. m_fMaxRange1 = 2048;
  93. m_fMinRange2 = 256;
  94. m_fMaxRange2 = 1024;
  95. m_nShotsFired = 0;
  96. m_nVentPose = -1;
  97. m_bAltFiresUnderwater = false;
  98. }
  99. void CWeaponAR2::Precache( void )
  100. {
  101. BaseClass::Precache();
  102. UTIL_PrecacheOther( "prop_combine_ball" );
  103. UTIL_PrecacheOther( "env_entity_dissolver" );
  104. }
  105. //-----------------------------------------------------------------------------
  106. // Purpose: Handle grenade detonate in-air (even when no ammo is left)
  107. //-----------------------------------------------------------------------------
  108. void CWeaponAR2::ItemPostFrame( void )
  109. {
  110. // See if we need to fire off our secondary round
  111. if ( m_bShotDelayed && gpGlobals->curtime > m_flDelayedFire )
  112. {
  113. DelayedAttack();
  114. }
  115. // Update our pose parameter for the vents
  116. CBasePlayer *pOwner = ToBasePlayer( GetOwner() );
  117. if ( pOwner )
  118. {
  119. CBaseViewModel *pVM = pOwner->GetViewModel();
  120. if ( pVM )
  121. {
  122. if ( m_nVentPose == -1 )
  123. {
  124. m_nVentPose = pVM->LookupPoseParameter( "VentPoses" );
  125. }
  126. float flVentPose = RemapValClamped( m_nShotsFired, 0, 5, 0.0f, 1.0f );
  127. pVM->SetPoseParameter( m_nVentPose, flVentPose );
  128. }
  129. }
  130. BaseClass::ItemPostFrame();
  131. }
  132. //-----------------------------------------------------------------------------
  133. // Purpose:
  134. // Output : Activity
  135. //-----------------------------------------------------------------------------
  136. Activity CWeaponAR2::GetPrimaryAttackActivity( void )
  137. {
  138. if ( m_nShotsFired < 2 )
  139. return ACT_VM_PRIMARYATTACK;
  140. if ( m_nShotsFired < 3 )
  141. return ACT_VM_RECOIL1;
  142. if ( m_nShotsFired < 4 )
  143. return ACT_VM_RECOIL2;
  144. return ACT_VM_RECOIL3;
  145. }
  146. //-----------------------------------------------------------------------------
  147. // Purpose:
  148. // Input : &tr -
  149. // nDamageType -
  150. //-----------------------------------------------------------------------------
  151. void CWeaponAR2::DoImpactEffect( trace_t &tr, int nDamageType )
  152. {
  153. CEffectData data;
  154. data.m_vOrigin = tr.endpos + ( tr.plane.normal * 1.0f );
  155. data.m_vNormal = tr.plane.normal;
  156. DispatchEffect( "AR2Impact", data );
  157. BaseClass::DoImpactEffect( tr, nDamageType );
  158. }
  159. //-----------------------------------------------------------------------------
  160. // Purpose:
  161. //-----------------------------------------------------------------------------
  162. void CWeaponAR2::DelayedAttack( void )
  163. {
  164. m_bShotDelayed = false;
  165. CBasePlayer *pOwner = ToBasePlayer( GetOwner() );
  166. if ( pOwner == NULL )
  167. return;
  168. // Deplete the clip completely
  169. SendWeaponAnim( ACT_VM_SECONDARYATTACK );
  170. m_flNextSecondaryAttack = pOwner->m_flNextAttack = gpGlobals->curtime + SequenceDuration();
  171. // Register a muzzleflash for the AI
  172. pOwner->DoMuzzleFlash();
  173. pOwner->SetMuzzleFlashTime( gpGlobals->curtime + 0.5 );
  174. WeaponSound( WPN_DOUBLE );
  175. pOwner->RumbleEffect(RUMBLE_SHOTGUN_DOUBLE, 0, RUMBLE_FLAG_RESTART );
  176. // Fire the bullets
  177. Vector vecSrc = pOwner->Weapon_ShootPosition( );
  178. Vector vecAiming = pOwner->GetAutoaimVector( AUTOAIM_SCALE_DEFAULT );
  179. Vector impactPoint = vecSrc + ( vecAiming * MAX_TRACE_LENGTH );
  180. // Fire the bullets
  181. Vector vecVelocity = vecAiming * 1000.0f;
  182. // Fire the combine ball
  183. CreateCombineBall( vecSrc,
  184. vecVelocity,
  185. sk_weapon_ar2_alt_fire_radius.GetFloat(),
  186. sk_weapon_ar2_alt_fire_mass.GetFloat(),
  187. sk_weapon_ar2_alt_fire_duration.GetFloat(),
  188. pOwner );
  189. // View effects
  190. color32 white = {255, 255, 255, 64};
  191. UTIL_ScreenFade( pOwner, white, 0.1, 0, FFADE_IN );
  192. //Disorient the player
  193. QAngle angles = pOwner->GetLocalAngles();
  194. angles.x += random->RandomInt( -4, 4 );
  195. angles.y += random->RandomInt( -4, 4 );
  196. angles.z = 0;
  197. pOwner->SnapEyeAngles( angles );
  198. pOwner->ViewPunch( QAngle( random->RandomInt( -8, -12 ), random->RandomInt( 1, 2 ), 0 ) );
  199. // Decrease ammo
  200. pOwner->RemoveAmmo( 1, m_iSecondaryAmmoType );
  201. // Can shoot again immediately
  202. m_flNextPrimaryAttack = gpGlobals->curtime + 0.5f;
  203. // Can blow up after a short delay (so have time to release mouse button)
  204. m_flNextSecondaryAttack = gpGlobals->curtime + 1.0f;
  205. }
  206. //-----------------------------------------------------------------------------
  207. // Purpose:
  208. //-----------------------------------------------------------------------------
  209. void CWeaponAR2::SecondaryAttack( void )
  210. {
  211. if ( m_bShotDelayed )
  212. return;
  213. // Cannot fire underwater
  214. if ( GetOwner() && GetOwner()->GetWaterLevel() == 3 )
  215. {
  216. SendWeaponAnim( ACT_VM_DRYFIRE );
  217. BaseClass::WeaponSound( EMPTY );
  218. m_flNextSecondaryAttack = gpGlobals->curtime + 0.5f;
  219. return;
  220. }
  221. m_bShotDelayed = true;
  222. m_flNextPrimaryAttack = m_flNextSecondaryAttack = m_flDelayedFire = gpGlobals->curtime + 0.5f;
  223. CBasePlayer *pPlayer = ToBasePlayer( GetOwner() );
  224. if( pPlayer )
  225. {
  226. pPlayer->RumbleEffect(RUMBLE_AR2_ALT_FIRE, 0, RUMBLE_FLAG_RESTART );
  227. }
  228. SendWeaponAnim( ACT_VM_FIDGET );
  229. WeaponSound( SPECIAL1 );
  230. m_iSecondaryAttacks++;
  231. gamestats->Event_WeaponFired( pPlayer, false, GetClassname() );
  232. }
  233. //-----------------------------------------------------------------------------
  234. // Purpose: Override if we're waiting to release a shot
  235. // Output : Returns true on success, false on failure.
  236. //-----------------------------------------------------------------------------
  237. bool CWeaponAR2::CanHolster( void )
  238. {
  239. if ( m_bShotDelayed )
  240. return false;
  241. return BaseClass::CanHolster();
  242. }
  243. //-----------------------------------------------------------------------------
  244. // Purpose: Override if we're waiting to release a shot
  245. //-----------------------------------------------------------------------------
  246. bool CWeaponAR2::Reload( void )
  247. {
  248. if ( m_bShotDelayed )
  249. return false;
  250. return BaseClass::Reload();
  251. }
  252. //-----------------------------------------------------------------------------
  253. // Purpose:
  254. // Input : *pOperator -
  255. //-----------------------------------------------------------------------------
  256. void CWeaponAR2::FireNPCPrimaryAttack( CBaseCombatCharacter *pOperator, bool bUseWeaponAngles )
  257. {
  258. Vector vecShootOrigin, vecShootDir;
  259. CAI_BaseNPC *npc = pOperator->MyNPCPointer();
  260. ASSERT( npc != NULL );
  261. if ( bUseWeaponAngles )
  262. {
  263. QAngle angShootDir;
  264. GetAttachment( LookupAttachment( "muzzle" ), vecShootOrigin, angShootDir );
  265. AngleVectors( angShootDir, &vecShootDir );
  266. }
  267. else
  268. {
  269. vecShootOrigin = pOperator->Weapon_ShootPosition();
  270. vecShootDir = npc->GetActualShootTrajectory( vecShootOrigin );
  271. }
  272. WeaponSoundRealtime( SINGLE_NPC );
  273. CSoundEnt::InsertSound( SOUND_COMBAT|SOUND_CONTEXT_GUNFIRE, pOperator->GetAbsOrigin(), SOUNDENT_VOLUME_MACHINEGUN, 0.2, pOperator, SOUNDENT_CHANNEL_WEAPON, pOperator->GetEnemy() );
  274. pOperator->FireBullets( 1, vecShootOrigin, vecShootDir, VECTOR_CONE_PRECALCULATED, MAX_TRACE_LENGTH, m_iPrimaryAmmoType, 2 );
  275. // NOTENOTE: This is overriden on the client-side
  276. // pOperator->DoMuzzleFlash();
  277. m_iClip1 = m_iClip1 - 1;
  278. }
  279. //-----------------------------------------------------------------------------
  280. // Purpose:
  281. //-----------------------------------------------------------------------------
  282. void CWeaponAR2::FireNPCSecondaryAttack( CBaseCombatCharacter *pOperator, bool bUseWeaponAngles )
  283. {
  284. WeaponSound( WPN_DOUBLE );
  285. if ( !GetOwner() )
  286. return;
  287. CAI_BaseNPC *pNPC = GetOwner()->MyNPCPointer();
  288. if ( !pNPC )
  289. return;
  290. // Fire!
  291. Vector vecSrc;
  292. Vector vecAiming;
  293. if ( bUseWeaponAngles )
  294. {
  295. QAngle angShootDir;
  296. GetAttachment( LookupAttachment( "muzzle" ), vecSrc, angShootDir );
  297. AngleVectors( angShootDir, &vecAiming );
  298. }
  299. else
  300. {
  301. vecSrc = pNPC->Weapon_ShootPosition( );
  302. Vector vecTarget;
  303. CNPC_Combine *pSoldier = dynamic_cast<CNPC_Combine *>( pNPC );
  304. if ( pSoldier )
  305. {
  306. // In the distant misty past, elite soldiers tried to use bank shots.
  307. // Therefore, we must ask them specifically what direction they are shooting.
  308. vecTarget = pSoldier->GetAltFireTarget();
  309. }
  310. else
  311. {
  312. // All other users of the AR2 alt-fire shoot directly at their enemy.
  313. if ( !pNPC->GetEnemy() )
  314. return;
  315. vecTarget = pNPC->GetEnemy()->BodyTarget( vecSrc );
  316. }
  317. vecAiming = vecTarget - vecSrc;
  318. VectorNormalize( vecAiming );
  319. }
  320. Vector impactPoint = vecSrc + ( vecAiming * MAX_TRACE_LENGTH );
  321. float flAmmoRatio = 1.0f;
  322. float flDuration = RemapValClamped( flAmmoRatio, 0.0f, 1.0f, 0.5f, sk_weapon_ar2_alt_fire_duration.GetFloat() );
  323. float flRadius = RemapValClamped( flAmmoRatio, 0.0f, 1.0f, 4.0f, sk_weapon_ar2_alt_fire_radius.GetFloat() );
  324. // Fire the bullets
  325. Vector vecVelocity = vecAiming * 1000.0f;
  326. // Fire the combine ball
  327. CreateCombineBall( vecSrc,
  328. vecVelocity,
  329. flRadius,
  330. sk_weapon_ar2_alt_fire_mass.GetFloat(),
  331. flDuration,
  332. pNPC );
  333. }
  334. //-----------------------------------------------------------------------------
  335. // Purpose:
  336. //-----------------------------------------------------------------------------
  337. void CWeaponAR2::Operator_ForceNPCFire( CBaseCombatCharacter *pOperator, bool bSecondary )
  338. {
  339. if ( bSecondary )
  340. {
  341. FireNPCSecondaryAttack( pOperator, true );
  342. }
  343. else
  344. {
  345. // Ensure we have enough rounds in the clip
  346. m_iClip1++;
  347. FireNPCPrimaryAttack( pOperator, true );
  348. }
  349. }
  350. //-----------------------------------------------------------------------------
  351. // Purpose:
  352. // Input : *pEvent -
  353. // *pOperator -
  354. //-----------------------------------------------------------------------------
  355. void CWeaponAR2::Operator_HandleAnimEvent( animevent_t *pEvent, CBaseCombatCharacter *pOperator )
  356. {
  357. switch( pEvent->event )
  358. {
  359. case EVENT_WEAPON_AR2:
  360. {
  361. FireNPCPrimaryAttack( pOperator, false );
  362. }
  363. break;
  364. case EVENT_WEAPON_AR2_ALTFIRE:
  365. {
  366. FireNPCSecondaryAttack( pOperator, false );
  367. }
  368. break;
  369. default:
  370. CBaseCombatWeapon::Operator_HandleAnimEvent( pEvent, pOperator );
  371. break;
  372. }
  373. }
  374. //-----------------------------------------------------------------------------
  375. // Purpose:
  376. //-----------------------------------------------------------------------------
  377. void CWeaponAR2::AddViewKick( void )
  378. {
  379. #define EASY_DAMPEN 0.5f
  380. #define MAX_VERTICAL_KICK 8.0f //Degrees
  381. #define SLIDE_LIMIT 5.0f //Seconds
  382. //Get the view kick
  383. CBasePlayer *pPlayer = ToBasePlayer( GetOwner() );
  384. if (!pPlayer)
  385. return;
  386. float flDuration = m_fFireDuration;
  387. if( g_pGameRules->GetAutoAimMode() == AUTOAIM_ON_CONSOLE )
  388. {
  389. // On the 360 (or in any configuration using the 360 aiming scheme), don't let the
  390. // AR2 progressive into the late, highly inaccurate stages of its kick. Just
  391. // spoof the time to make it look (to the kicking code) like we haven't been
  392. // firing for very long.
  393. flDuration = MIN( flDuration, 0.75f );
  394. }
  395. DoMachineGunKick( pPlayer, EASY_DAMPEN, MAX_VERTICAL_KICK, flDuration, SLIDE_LIMIT );
  396. }
  397. //-----------------------------------------------------------------------------
  398. const WeaponProficiencyInfo_t *CWeaponAR2::GetProficiencyValues()
  399. {
  400. static WeaponProficiencyInfo_t proficiencyTable[] =
  401. {
  402. { 7.0, 0.75 },
  403. { 5.00, 0.75 },
  404. { 3.0, 0.85 },
  405. { 5.0/3.0, 0.75 },
  406. { 1.00, 1.0 },
  407. };
  408. COMPILE_TIME_ASSERT( ARRAYSIZE(proficiencyTable) == WEAPON_PROFICIENCY_PERFECT + 1);
  409. return proficiencyTable;
  410. }