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