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
|
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: Hand grenade
//
// $NoKeywords: $
//=============================================================================//
#include "cbase.h"
#include "npcevent.h"
#include "hl1mp_basecombatweapon_shared.h"
//#include "basecombatcharacter.h"
//#include "AI_BaseNPC.h"
#ifdef CLIENT_DLL
#include "hl1/hl1_c_player.h"
#else
#include "hl1_player.h"
#endif
#include "gamerules.h"
#include "in_buttons.h"
#ifdef CLIENT_DLL
#else
#include "soundent.h"
#include "game.h"
#endif
#include "vstdlib/random.h"
#include "engine/IEngineSound.h"
#ifdef CLIENT_DLL
#else
#include "hl1_basegrenade.h"
#endif
#define HANDGRENADE_MODEL "models/w_grenade.mdl"
#ifndef CLIENT_DLL
extern ConVar sk_plr_dmg_grenade;
//-----------------------------------------------------------------------------
// CHandGrenade
//-----------------------------------------------------------------------------
LINK_ENTITY_TO_CLASS( grenade_hand, CHandGrenade );
BEGIN_DATADESC( CHandGrenade )
DEFINE_ENTITYFUNC( BounceTouch ),
END_DATADESC()
void CHandGrenade::Spawn( void )
{
SetMoveType( MOVETYPE_FLYGRAVITY, MOVECOLLIDE_FLY_BOUNCE );
SetSolid( SOLID_BBOX );
AddSolidFlags( FSOLID_NOT_STANDABLE );
SetModel( HANDGRENADE_MODEL );
UTIL_SetSize( this, Vector( 0, 0, 0 ), Vector( 0, 0, 0 ) );
m_bHasWarnedAI = false;
}
void CHandGrenade::Precache( void )
{
BaseClass::Precache( );
PrecacheScriptSound( "Weapon_HandGrenade.GrenadeBounce" );
PrecacheModel( HANDGRENADE_MODEL );
}
void CHandGrenade::ShootTimed( CBaseCombatCharacter *pOwner, Vector vecVelocity, float flTime )
{
SetAbsVelocity( vecVelocity );
SetThrower( pOwner );
SetOwnerEntity( pOwner );
SetTouch( &CHandGrenade::BounceTouch ); // Bounce if touched
m_flDetonateTime = gpGlobals->curtime + flTime;
SetThink( &CBaseGrenade::TumbleThink );
SetNextThink( gpGlobals->curtime + 0.1 );
if ( flTime < 0.1 )
{
SetNextThink( gpGlobals->curtime );
SetAbsVelocity( vec3_origin );
}
// SetSequence( SelectWeightedSequence( ACT_GRENADE_TOSS ) );
SetSequence( 0 );
m_flPlaybackRate = 1.0;
SetAbsAngles( QAngle( 0,0,60) );
AngularImpulse angImpulse;
angImpulse[0] = random->RandomInt( -200, 200 );
angImpulse[1] = random->RandomInt( 400, 500 );
angImpulse[2] = random->RandomInt( -100, 100 );
ApplyLocalAngularVelocityImpulse( angImpulse );
SetGravity( UTIL_ScaleForGravity( 400 ) ); // use a lower gravity for grenades to make them easier to see
SetFriction( 0.8 );
SetDamage( sk_plr_dmg_grenade.GetFloat() );
SetDamageRadius( GetDamage() * 2.5 );
}
void CHandGrenade ::BounceSound( void )
{
EmitSound( "Weapon_HandGrenade.GrenadeBounce" );
}
void CHandGrenade::BounceTouch( CBaseEntity *pOther )
{
if ( pOther->IsSolidFlagSet(FSOLID_TRIGGER | FSOLID_VOLUME_CONTENTS) )
return;
// don't hit the guy that launched this grenade
if ( pOther == GetThrower() )
return;
// Do a special test for players
if ( pOther->IsPlayer() )
{
// Never hit a player again (we'll explode and fixup anyway)
SetCollisionGroup( COLLISION_GROUP_DEBRIS );
}
// only do damage if we're moving fairly fast
if ( (pOther->m_takedamage != DAMAGE_NO) && (m_flNextAttack < gpGlobals->curtime && GetAbsVelocity().Length() > 100))
{
if ( GetThrower() )
{
trace_t tr;
tr = CBaseEntity::GetTouchTrace( );
ClearMultiDamage( );
Vector forward;
AngleVectors( GetAbsAngles(), &forward );
CTakeDamageInfo info( this, GetThrower(), 1, DMG_CLUB );
CalculateMeleeDamageForce( &info, forward, tr.endpos );
pOther->DispatchTraceAttack( info, forward, &tr );
ApplyMultiDamage();
}
m_flNextAttack = gpGlobals->curtime + 1.0; // debounce
}
Vector vecTestVelocity;
// m_vecAngVelocity = Vector (300, 300, 300);
// this is my heuristic for modulating the grenade velocity because grenades dropped purely vertical
// or thrown very far tend to slow down too quickly for me to always catch just by testing velocity.
// trimming the Z velocity a bit seems to help quite a bit.
vecTestVelocity = GetAbsVelocity();
vecTestVelocity.z *= 0.45;
if ( !m_bHasWarnedAI && vecTestVelocity.Length() <= 60 )
{
// grenade is moving really slow. It's probably very close to where it will ultimately stop moving.
// emit the danger sound.
// register a radius louder than the explosion, so we make sure everyone gets out of the way
CSoundEnt::InsertSound ( SOUND_DANGER, GetAbsOrigin(), m_flDamage / 0.4, 0.3 );
m_bHasWarnedAI = TRUE;
}
// HACKHACK - On ground isn't always set, so look for ground underneath
trace_t tr;
UTIL_TraceLine( GetAbsOrigin(), GetAbsOrigin() - Vector(0,0,10), MASK_SOLID_BRUSHONLY, this, COLLISION_GROUP_NONE, &tr );
if ( tr.fraction < 1.0 )
{
// add a bit of static friction
// SetAbsVelocity( GetAbsVelocity() * 0.8 );
SetSequence( SelectWeightedSequence( ACT_IDLE ) );
SetAbsAngles( vec3_angle );
}
// play bounce sound
BounceSound();
m_flPlaybackRate = GetAbsVelocity().Length() / 200.0;
if (m_flPlaybackRate > 1.0)
m_flPlaybackRate = 1;
else if (m_flPlaybackRate < 0.5)
m_flPlaybackRate = 0;
}
#endif
#ifdef CLIENT_DLL
#define CWeaponHandGrenade C_WeaponHandGrenade
#endif
//-----------------------------------------------------------------------------
// CWeaponHandGrenade
//-----------------------------------------------------------------------------
class CWeaponHandGrenade : public CBaseHL1MPCombatWeapon
{
DECLARE_CLASS( CWeaponHandGrenade, CBaseHL1MPCombatWeapon );
public:
DECLARE_NETWORKCLASS();
DECLARE_PREDICTABLE();
CWeaponHandGrenade( void );
void Precache( void );
void PrimaryAttack( void );
void WeaponIdle( void );
bool Deploy( void );
bool Holster( CBaseCombatWeapon *pSwitchingTo = NULL );
// DECLARE_SERVERCLASS();
DECLARE_DATADESC();
private:
// float m_flStartThrow;
// float m_flReleaseThrow;
CNetworkVar( float, m_flStartThrow );
CNetworkVar( float, m_flReleaseThrow );
};
IMPLEMENT_NETWORKCLASS_ALIASED( WeaponHandGrenade, DT_WeaponHandGrenade );
BEGIN_NETWORK_TABLE( CWeaponHandGrenade, DT_WeaponHandGrenade )
#ifdef CLIENT_DLL
RecvPropFloat( RECVINFO( m_flStartThrow ) ),
RecvPropFloat( RECVINFO( m_flReleaseThrow ) ),
#else
SendPropFloat( SENDINFO( m_flStartThrow ) ),
SendPropFloat( SENDINFO( m_flReleaseThrow ) ),
#endif
END_NETWORK_TABLE()
BEGIN_PREDICTION_DATA( CWeaponHandGrenade )
#ifdef CLIENT_DLL
DEFINE_PRED_FIELD( m_flStartThrow, FIELD_INTEGER, FTYPEDESC_INSENDTABLE ),
DEFINE_PRED_FIELD( m_flReleaseThrow, FIELD_BOOLEAN, FTYPEDESC_INSENDTABLE ),
#endif
END_PREDICTION_DATA()
LINK_ENTITY_TO_CLASS( weapon_handgrenade, CWeaponHandGrenade );
PRECACHE_WEAPON_REGISTER( weapon_handgrenade );
//IMPLEMENT_SERVERCLASS_ST( CWeaponHandGrenade, DT_WeaponHandGrenade )
//END_SEND_TABLE()
BEGIN_DATADESC( CWeaponHandGrenade )
END_DATADESC()
//-----------------------------------------------------------------------------
// Purpose: Constructor
//-----------------------------------------------------------------------------
CWeaponHandGrenade::CWeaponHandGrenade( void )
{
m_bReloadsSingly = false;
m_bFiresUnderwater = true;
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CWeaponHandGrenade::Precache( void )
{
#ifndef CLIENT_DLL
UTIL_PrecacheOther( "grenade_hand" );
#endif
BaseClass::Precache();
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CWeaponHandGrenade::PrimaryAttack( void )
{
CBasePlayer *pPlayer = ToBasePlayer( GetOwner() );
if ( !pPlayer )
{
return;
}
if ( ( m_flStartThrow <= 0 ) && pPlayer->GetAmmoCount( m_iPrimaryAmmoType ) > 0 )
{
m_flStartThrow = gpGlobals->curtime;
m_flReleaseThrow = 0;
SendWeaponAnim( ACT_VM_PRIMARYATTACK );
SetWeaponIdleTime( gpGlobals->curtime + 0.5 );
}
}
void CWeaponHandGrenade::WeaponIdle( void )
{
if ( m_flReleaseThrow == 0 && m_flStartThrow )
m_flReleaseThrow = gpGlobals->curtime;
if ( !HasWeaponIdleTimeElapsed() )
return;
CBasePlayer *pPlayer = ToBasePlayer( GetOwner() );
if ( !pPlayer )
{
return;
}
if ( m_flStartThrow )
{
Vector vecAiming = pPlayer->GetAutoaimVector( 0 );
QAngle angThrow;
VectorAngles( vecAiming, angThrow );
Vector vecUp;
Vector vecRight;
AngleVectors( angThrow, NULL, &vecRight, &vecUp );
if ( angThrow.x > 180 ) // player is pitching up
angThrow.x = -15 - ( 360 - angThrow.x ) * ( ( 90 - 10 ) / 90.0 );
else // player is pitching down
angThrow.x = -15 + angThrow.x * ( ( 90 + 10 ) / 90.0 );
float flVel = ( 90 - angThrow.x ) * 4;
if ( flVel > 500 )
flVel = 500;
Vector vecFwd;
AngleVectors( angThrow, &vecFwd );
Vector vecSrc = pPlayer->EyePosition() + vecFwd * 16;
Vector vecThrow = vecFwd * flVel + pPlayer->GetAbsVelocity();
QAngle angles;
VectorAngles( vecThrow, angles );
#ifndef CLIENT_DLL
CHandGrenade *pGrenade = (CHandGrenade*)Create( "grenade_hand", vecSrc, angles );
if ( pGrenade )
{
// always explode 3 seconds after the pin was pulled
float flTime = m_flStartThrow - gpGlobals->curtime + 3.0;
if ( flTime < 0 )
{
flTime = 0;
}
pGrenade->ShootTimed( pPlayer, vecThrow, flTime );
}
#endif
if ( flVel < 500 )
{
SendWeaponAnim( ACT_HANDGRENADE_THROW1 );
}
else if ( flVel < 1000 )
{
SendWeaponAnim( ACT_HANDGRENADE_THROW2 );
}
else
{
SendWeaponAnim( ACT_HANDGRENADE_THROW3 );
}
// player "shoot" animation
pPlayer->SetAnimation( PLAYER_ATTACK1 );
m_flReleaseThrow = 0;
m_flStartThrow = 0;
SetWeaponIdleTime( gpGlobals->curtime + 0.5 );
m_flNextPrimaryAttack = gpGlobals->curtime + 0.5;
m_flNextSecondaryAttack = gpGlobals->curtime + 0.5;
pPlayer->RemoveAmmo( 1, m_iPrimaryAmmoType );
return;
}
else if ( m_flReleaseThrow > 0 )
{
// we've finished the throw, restart.
m_flStartThrow = 0;
if ( pPlayer->GetAmmoCount( m_iPrimaryAmmoType ) > 0 )
{
SendWeaponAnim( ACT_VM_DRAW );
}
else
{
// RetireWeapon();
return;
}
SetWeaponIdleTime( gpGlobals->curtime + random->RandomFloat( 10, 15 ) );
m_flReleaseThrow = -1;
return;
}
if ( pPlayer->GetAmmoCount( m_iPrimaryAmmoType ) > 0 )
{
float flRand = random->RandomFloat( 0, 1 );
if ( flRand <= 0.75 )
{
SendWeaponAnim( ACT_VM_IDLE );
SetWeaponIdleTime( gpGlobals->curtime + random->RandomFloat( 10, 15 ) );// how long till we do this again.
}
else
{
SendWeaponAnim( ACT_VM_FIDGET );
}
}
}
bool CWeaponHandGrenade::Deploy( void )
{
m_flReleaseThrow = -1;
return DefaultDeploy( (char*)GetViewModel(), (char*)GetWorldModel(), ACT_VM_DRAW, (char*)GetAnimPrefix() );
}
bool CWeaponHandGrenade::Holster( CBaseCombatWeapon *pSwitchingTo )
{
CBasePlayer *pPlayer = ToBasePlayer( GetOwner() );
if ( !pPlayer )
{
return false;
}
if ( m_flStartThrow > 0 )
{
return false;
}
if ( !BaseClass::Holster( pSwitchingTo ) )
{
return false;
}
if ( pPlayer->GetAmmoCount( m_iPrimaryAmmoType ) <= 0 )
{
#ifndef CLIENT_DLL
SetThink( &CWeaponHandGrenade::DestroyItem );
SetNextThink( gpGlobals->curtime + 0.1 );
#endif
}
pPlayer->SetNextAttack( gpGlobals->curtime + 0.5 );
return true;
}
|