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.

522 lines
14 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================//
  6. #include "cbase.h"
  7. #include "basehlcombatweapon.h"
  8. #include "soundent.h"
  9. #include "ai_basenpc.h"
  10. #include "game.h"
  11. #include "in_buttons.h"
  12. #include "gamestats.h"
  13. // memdbgon must be the last include file in a .cpp file!!!
  14. #include "tier0/memdbgon.h"
  15. IMPLEMENT_SERVERCLASS_ST( CHLMachineGun, DT_HLMachineGun )
  16. END_SEND_TABLE()
  17. //=========================================================
  18. // >> CHLSelectFireMachineGun
  19. //=========================================================
  20. BEGIN_DATADESC( CHLMachineGun )
  21. DEFINE_FIELD( m_nShotsFired, FIELD_INTEGER ),
  22. DEFINE_FIELD( m_flNextSoundTime, FIELD_TIME ),
  23. END_DATADESC()
  24. //-----------------------------------------------------------------------------
  25. // Purpose:
  26. //-----------------------------------------------------------------------------
  27. CHLMachineGun::CHLMachineGun( void )
  28. {
  29. }
  30. const Vector &CHLMachineGun::GetBulletSpread( void )
  31. {
  32. static Vector cone = VECTOR_CONE_3DEGREES;
  33. return cone;
  34. }
  35. //-----------------------------------------------------------------------------
  36. // Purpose:
  37. //
  38. //
  39. //-----------------------------------------------------------------------------
  40. void CHLMachineGun::PrimaryAttack( void )
  41. {
  42. // Only the player fires this way so we can cast
  43. CBasePlayer *pPlayer = ToBasePlayer( GetOwner() );
  44. if (!pPlayer)
  45. return;
  46. // Abort here to handle burst and auto fire modes
  47. if ( (UsesClipsForAmmo1() && m_iClip1 == 0) || ( !UsesClipsForAmmo1() && !pPlayer->GetAmmoCount(m_iPrimaryAmmoType) ) )
  48. return;
  49. m_nShotsFired++;
  50. pPlayer->DoMuzzleFlash();
  51. // To make the firing framerate independent, we may have to fire more than one bullet here on low-framerate systems,
  52. // especially if the weapon we're firing has a really fast rate of fire.
  53. int iBulletsToFire = 0;
  54. float fireRate = GetFireRate();
  55. // MUST call sound before removing a round from the clip of a CHLMachineGun
  56. while ( m_flNextPrimaryAttack <= gpGlobals->curtime )
  57. {
  58. WeaponSound(SINGLE, m_flNextPrimaryAttack);
  59. m_flNextPrimaryAttack = m_flNextPrimaryAttack + fireRate;
  60. iBulletsToFire++;
  61. }
  62. // Make sure we don't fire more than the amount in the clip, if this weapon uses clips
  63. if ( UsesClipsForAmmo1() )
  64. {
  65. if ( iBulletsToFire > m_iClip1 )
  66. iBulletsToFire = m_iClip1;
  67. m_iClip1 -= iBulletsToFire;
  68. }
  69. m_iPrimaryAttacks++;
  70. gamestats->Event_WeaponFired( pPlayer, true, GetClassname() );
  71. // Fire the bullets
  72. FireBulletsInfo_t info;
  73. info.m_iShots = iBulletsToFire;
  74. info.m_vecSrc = pPlayer->Weapon_ShootPosition( );
  75. info.m_vecDirShooting = pPlayer->GetAutoaimVector( AUTOAIM_SCALE_DEFAULT );
  76. info.m_vecSpread = pPlayer->GetAttackSpread( this );
  77. info.m_flDistance = MAX_TRACE_LENGTH;
  78. info.m_iAmmoType = m_iPrimaryAmmoType;
  79. info.m_iTracerFreq = 2;
  80. FireBullets( info );
  81. //Factor in the view kick
  82. AddViewKick();
  83. CSoundEnt::InsertSound( SOUND_COMBAT, GetAbsOrigin(), SOUNDENT_VOLUME_MACHINEGUN, 0.2, pPlayer );
  84. if (!m_iClip1 && pPlayer->GetAmmoCount(m_iPrimaryAmmoType) <= 0)
  85. {
  86. // HEV suit - indicate out of ammo condition
  87. pPlayer->SetSuitUpdate("!HEV_AMO0", FALSE, 0);
  88. }
  89. SendWeaponAnim( GetPrimaryAttackActivity() );
  90. pPlayer->SetAnimation( PLAYER_ATTACK1 );
  91. // Register a muzzleflash for the AI
  92. pPlayer->SetMuzzleFlashTime( gpGlobals->curtime + 0.5 );
  93. }
  94. //-----------------------------------------------------------------------------
  95. // Purpose:
  96. // Input : &info -
  97. //-----------------------------------------------------------------------------
  98. void CHLMachineGun::FireBullets( const FireBulletsInfo_t &info )
  99. {
  100. if(CBasePlayer *pPlayer = ToBasePlayer ( GetOwner() ) )
  101. {
  102. pPlayer->FireBullets(info);
  103. }
  104. }
  105. //-----------------------------------------------------------------------------
  106. // Purpose: Weapon firing conditions
  107. //-----------------------------------------------------------------------------
  108. int CHLMachineGun::WeaponRangeAttack1Condition( float flDot, float flDist )
  109. {
  110. if ( m_iClip1 <=0 )
  111. {
  112. return COND_NO_PRIMARY_AMMO;
  113. }
  114. else if ( flDist < m_fMinRange1 )
  115. {
  116. return COND_TOO_CLOSE_TO_ATTACK;
  117. }
  118. else if ( flDist > m_fMaxRange1 )
  119. {
  120. return COND_TOO_FAR_TO_ATTACK;
  121. }
  122. else if ( flDot < 0.5f ) // UNDONE: Why check this here? Isn't the AI checking this already?
  123. {
  124. return COND_NOT_FACING_ATTACK;
  125. }
  126. return COND_CAN_RANGE_ATTACK1;
  127. }
  128. //-----------------------------------------------------------------------------
  129. // Purpose:
  130. //-----------------------------------------------------------------------------
  131. void CHLMachineGun::DoMachineGunKick( CBasePlayer *pPlayer, float dampEasy, float maxVerticleKickAngle, float fireDurationTime, float slideLimitTime )
  132. {
  133. #define KICK_MIN_X 0.2f //Degrees
  134. #define KICK_MIN_Y 0.2f //Degrees
  135. #define KICK_MIN_Z 0.1f //Degrees
  136. QAngle vecScratch;
  137. //Find how far into our accuracy degradation we are
  138. float duration = ( fireDurationTime > slideLimitTime ) ? slideLimitTime : fireDurationTime;
  139. float kickPerc = duration / slideLimitTime;
  140. // do this to get a hard discontinuity, clear out anything under 10 degrees punch
  141. pPlayer->ViewPunchReset( 10 );
  142. //Apply this to the view angles as well
  143. vecScratch.x = -( KICK_MIN_X + ( maxVerticleKickAngle * kickPerc ) );
  144. vecScratch.y = -( KICK_MIN_Y + ( maxVerticleKickAngle * kickPerc ) ) / 3;
  145. vecScratch.z = KICK_MIN_Z + ( maxVerticleKickAngle * kickPerc ) / 8;
  146. //Wibble left and right
  147. if ( random->RandomInt( -1, 1 ) >= 0 )
  148. vecScratch.y *= -1;
  149. //Wobble up and down
  150. if ( random->RandomInt( -1, 1 ) >= 0 )
  151. vecScratch.z *= -1;
  152. //If we're in easy, dampen the effect a bit
  153. if ( g_pGameRules->IsSkillLevel( SKILL_EASY ) )
  154. {
  155. for ( int i = 0; i < 3; i++ )
  156. {
  157. vecScratch[i] *= dampEasy;
  158. }
  159. }
  160. //Clip this to our desired min/max
  161. UTIL_ClipPunchAngleOffset( vecScratch, pPlayer->m_Local.m_vecPunchAngle, QAngle( 24.0f, 3.0f, 1.0f ) );
  162. //Add it to the view punch
  163. // NOTE: 0.5 is just tuned to match the old effect before the punch became simulated
  164. pPlayer->ViewPunch( vecScratch * 0.5 );
  165. }
  166. //-----------------------------------------------------------------------------
  167. // Purpose: Reset our shots fired
  168. //-----------------------------------------------------------------------------
  169. bool CHLMachineGun::Deploy( void )
  170. {
  171. m_nShotsFired = 0;
  172. return BaseClass::Deploy();
  173. }
  174. //-----------------------------------------------------------------------------
  175. // Purpose: Make enough sound events to fill the estimated think interval
  176. // returns: number of shots needed
  177. //-----------------------------------------------------------------------------
  178. int CHLMachineGun::WeaponSoundRealtime( WeaponSound_t shoot_type )
  179. {
  180. int numBullets = 0;
  181. // ran out of time, clamp to current
  182. if (m_flNextSoundTime < gpGlobals->curtime)
  183. {
  184. m_flNextSoundTime = gpGlobals->curtime;
  185. }
  186. // make enough sound events to fill up the next estimated think interval
  187. float dt = clamp( m_flAnimTime - m_flPrevAnimTime, 0, 0.2 );
  188. if (m_flNextSoundTime < gpGlobals->curtime + dt)
  189. {
  190. WeaponSound( SINGLE_NPC, m_flNextSoundTime );
  191. m_flNextSoundTime += GetFireRate();
  192. numBullets++;
  193. }
  194. if (m_flNextSoundTime < gpGlobals->curtime + dt)
  195. {
  196. WeaponSound( SINGLE_NPC, m_flNextSoundTime );
  197. m_flNextSoundTime += GetFireRate();
  198. numBullets++;
  199. }
  200. return numBullets;
  201. }
  202. //-----------------------------------------------------------------------------
  203. // Purpose:
  204. //-----------------------------------------------------------------------------
  205. void CHLMachineGun::ItemPostFrame( void )
  206. {
  207. CBasePlayer *pOwner = ToBasePlayer( GetOwner() );
  208. if ( pOwner == NULL )
  209. return;
  210. // Debounce the recoiling counter
  211. if ( ( pOwner->m_nButtons & IN_ATTACK ) == false )
  212. {
  213. m_nShotsFired = 0;
  214. }
  215. BaseClass::ItemPostFrame();
  216. }
  217. IMPLEMENT_SERVERCLASS_ST( CHLSelectFireMachineGun, DT_HLSelectFireMachineGun )
  218. END_SEND_TABLE()
  219. //=========================================================
  220. // >> CHLSelectFireMachineGun
  221. //=========================================================
  222. BEGIN_DATADESC( CHLSelectFireMachineGun )
  223. DEFINE_FIELD( m_iBurstSize, FIELD_INTEGER ),
  224. DEFINE_FIELD( m_iFireMode, FIELD_INTEGER ),
  225. // Function pinters
  226. DEFINE_FUNCTION( BurstThink ),
  227. END_DATADESC()
  228. float CHLSelectFireMachineGun::GetBurstCycleRate( void )
  229. {
  230. // this is the time it takes to fire an entire
  231. // burst, plus whatever amount of delay we want
  232. // to have between bursts.
  233. return 0.5f;
  234. }
  235. float CHLSelectFireMachineGun::GetFireRate( void )
  236. {
  237. switch( m_iFireMode )
  238. {
  239. case FIREMODE_FULLAUTO:
  240. // the time between rounds fired on full auto
  241. return 0.1f; // 600 rounds per minute = 0.1 seconds per bullet
  242. break;
  243. case FIREMODE_3RNDBURST:
  244. // the time between rounds fired within a single burst
  245. return 0.1f; // 600 rounds per minute = 0.1 seconds per bullet
  246. break;
  247. default:
  248. return 0.1f;
  249. break;
  250. }
  251. }
  252. bool CHLSelectFireMachineGun::Deploy( void )
  253. {
  254. // Forget about any bursts this weapon was firing when holstered
  255. m_iBurstSize = 0;
  256. return BaseClass::Deploy();
  257. }
  258. //-----------------------------------------------------------------------------
  259. // Purpose:
  260. //
  261. //
  262. //-----------------------------------------------------------------------------
  263. void CHLSelectFireMachineGun::PrimaryAttack( void )
  264. {
  265. if (m_bFireOnEmpty)
  266. {
  267. return;
  268. }
  269. switch( m_iFireMode )
  270. {
  271. case FIREMODE_FULLAUTO:
  272. BaseClass::PrimaryAttack();
  273. // Msg("%.3f\n", m_flNextPrimaryAttack.Get() );
  274. SetWeaponIdleTime( gpGlobals->curtime + 3.0f );
  275. break;
  276. case FIREMODE_3RNDBURST:
  277. m_iBurstSize = GetBurstSize();
  278. // Call the think function directly so that the first round gets fired immediately.
  279. BurstThink();
  280. SetThink( &CHLSelectFireMachineGun::BurstThink );
  281. m_flNextPrimaryAttack = gpGlobals->curtime + GetBurstCycleRate();
  282. m_flNextSecondaryAttack = gpGlobals->curtime + GetBurstCycleRate();
  283. // Pick up the rest of the burst through the think function.
  284. SetNextThink( gpGlobals->curtime + GetFireRate() );
  285. break;
  286. }
  287. CBasePlayer *pOwner = ToBasePlayer( GetOwner() );
  288. if ( pOwner )
  289. {
  290. m_iPrimaryAttacks++;
  291. gamestats->Event_WeaponFired( pOwner, true, GetClassname() );
  292. }
  293. }
  294. //-----------------------------------------------------------------------------
  295. // Purpose:
  296. //
  297. //
  298. //-----------------------------------------------------------------------------
  299. void CHLSelectFireMachineGun::SecondaryAttack( void )
  300. {
  301. // change fire modes.
  302. switch( m_iFireMode )
  303. {
  304. case FIREMODE_FULLAUTO:
  305. //Msg( "Burst\n" );
  306. m_iFireMode = FIREMODE_3RNDBURST;
  307. WeaponSound(SPECIAL2);
  308. break;
  309. case FIREMODE_3RNDBURST:
  310. //Msg( "Auto\n" );
  311. m_iFireMode = FIREMODE_FULLAUTO;
  312. WeaponSound(SPECIAL1);
  313. break;
  314. }
  315. SendWeaponAnim( GetSecondaryAttackActivity() );
  316. m_flNextSecondaryAttack = gpGlobals->curtime + 0.3;
  317. CBasePlayer *pOwner = ToBasePlayer( GetOwner() );
  318. if ( pOwner )
  319. {
  320. m_iSecondaryAttacks++;
  321. gamestats->Event_WeaponFired( pOwner, false, GetClassname() );
  322. }
  323. }
  324. //-----------------------------------------------------------------------------
  325. // Purpose:
  326. //
  327. //
  328. //-----------------------------------------------------------------------------
  329. void CHLSelectFireMachineGun::BurstThink( void )
  330. {
  331. CHLMachineGun::PrimaryAttack();
  332. m_iBurstSize--;
  333. if( m_iBurstSize == 0 )
  334. {
  335. // The burst is over!
  336. SetThink(NULL);
  337. // idle immediately to stop the firing animation
  338. SetWeaponIdleTime( gpGlobals->curtime );
  339. return;
  340. }
  341. SetNextThink( gpGlobals->curtime + GetFireRate() );
  342. }
  343. //-----------------------------------------------------------------------------
  344. // Purpose:
  345. //
  346. //
  347. //-----------------------------------------------------------------------------
  348. void CHLSelectFireMachineGun::WeaponSound( WeaponSound_t shoot_type, float soundtime /*= 0.0f*/ )
  349. {
  350. if (shoot_type == SINGLE)
  351. {
  352. switch( m_iFireMode )
  353. {
  354. case FIREMODE_FULLAUTO:
  355. BaseClass::WeaponSound( SINGLE, soundtime );
  356. break;
  357. case FIREMODE_3RNDBURST:
  358. if( m_iBurstSize == GetBurstSize() && m_iClip1 >= m_iBurstSize )
  359. {
  360. // First round of a burst, and enough bullets remaining in the clip to fire the whole burst
  361. BaseClass::WeaponSound( BURST, soundtime );
  362. }
  363. else if( m_iClip1 < m_iBurstSize )
  364. {
  365. // Not enough rounds remaining in the magazine to fire a burst, so play the gunshot
  366. // sounds individually as each round is fired.
  367. BaseClass::WeaponSound( SINGLE, soundtime );
  368. }
  369. break;
  370. }
  371. return;
  372. }
  373. BaseClass::WeaponSound( shoot_type, soundtime );
  374. }
  375. // BUGBUG: These need to be rethought
  376. //-----------------------------------------------------------------------------
  377. int CHLSelectFireMachineGun::WeaponRangeAttack1Condition( float flDot, float flDist )
  378. {
  379. if (m_iClip1 <=0)
  380. {
  381. return COND_NO_PRIMARY_AMMO;
  382. }
  383. else if ( flDist < m_fMinRange1)
  384. {
  385. return COND_TOO_CLOSE_TO_ATTACK;
  386. }
  387. else if (flDist > m_fMaxRange1)
  388. {
  389. return COND_TOO_FAR_TO_ATTACK;
  390. }
  391. else if (flDot < 0.5) // UNDONE: Why check this here? Isn't the AI checking this already?
  392. {
  393. return COND_NOT_FACING_ATTACK;
  394. }
  395. return COND_CAN_RANGE_ATTACK1;
  396. }
  397. //-----------------------------------------------------------------------------
  398. int CHLSelectFireMachineGun::WeaponRangeAttack2Condition( float flDot, float flDist )
  399. {
  400. return COND_NONE; // FIXME: disabled for now
  401. // m_iClip2 == -1 when no secondary clip is used
  402. if ( m_iClip2 == 0 && UsesSecondaryAmmo() )
  403. {
  404. return COND_NO_SECONDARY_AMMO;
  405. }
  406. else if ( flDist < m_fMinRange2 )
  407. {
  408. // Don't return COND_TOO_CLOSE_TO_ATTACK only for primary attack
  409. return COND_NONE;
  410. }
  411. else if (flDist > m_fMaxRange2 )
  412. {
  413. // Don't return COND_TOO_FAR_TO_ATTACK only for primary attack
  414. return COND_NONE;
  415. }
  416. else if ( flDot < 0.5 ) // UNDONE: Why check this here? Isn't the AI checking this already?
  417. {
  418. return COND_NOT_FACING_ATTACK;
  419. }
  420. return COND_CAN_RANGE_ATTACK2;
  421. }
  422. //-----------------------------------------------------------------------------
  423. // Purpose: Constructor
  424. //-----------------------------------------------------------------------------
  425. CHLSelectFireMachineGun::CHLSelectFireMachineGun( void )
  426. {
  427. m_fMinRange1 = 65;
  428. m_fMinRange2 = 65;
  429. m_fMaxRange1 = 1024;
  430. m_fMaxRange2 = 1024;
  431. m_iFireMode = FIREMODE_FULLAUTO;
  432. }