-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathasw_mortar_round.cpp
More file actions
388 lines (327 loc) · 11.3 KB
/
Copy pathasw_mortar_round.cpp
File metadata and controls
388 lines (327 loc) · 11.3 KB
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
#include "cbase.h"
#include "asw_mortar_round.h"
#include "asw_gamerules.h"
#include "world.h"
#include "asw_util_shared.h"
#include "asw_marine.h"
#include "asw_marine_resource.h"
#include "particle_parse.h"
#include "Sprite.h"
#include "SpriteTrail.h"
#include "te_effect_dispatch.h"
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
#define ASW_MORTAR_ROUND_MODEL "models/swarm/mortarbugprojectile/mortarbugprojectile.mdl"
#define ASW_MORTAR_WARN_DELAY 2.0f
extern ConVar asw_vindicator_grenade_friction;
extern ConVar asw_vindicator_grenade_elasticity;
static ConVar asw_mortar_round_min_detonation_time( "asw_mortar_round_min_detonation_time", "0.9f", FCVAR_CHEAT, "Min. time before cluster grenade can detonate" );
static ConVar asw_mortar_round_fuse( "asw_mortar_round_fuse", "4.0f", FCVAR_CHEAT, "Fuse length of cluster grenade" );
static ConVar asw_mortar_round_radius_check_interval( "asw_mortar_round_radius_check_interval", "0.5f", FCVAR_CHEAT, "How often the cluster grenade checks for nearby drones to explode against" );
static ConVar asw_mortar_round_radius_check_scale( "asw_mortar_round_radius_check_scale", "0.6f", FCVAR_CHEAT, "What fraction of the grenade's damage radius is used for the early detonation check" );
static ConVar asw_mortar_round_child_fuse_min( "asw_mortar_round_child_fuse_min", "0.5", FCVAR_CHEAT, "Cluster grenade child cluster's minimum fuse length" );
static ConVar asw_mortar_round_child_fuse_max( "asw_mortar_round_child_fuse_max", "1.0", FCVAR_CHEAT, "Cluster grenade child cluster's maximum fuse length" );
ConVar asw_mortar_round_gravity( "asw_mortar_round_gravity", "0.8f", FCVAR_CHEAT, "Gravity of mortar bug's mortar" );
LINK_ENTITY_TO_CLASS( asw_mortar_round, CASW_Mortar_Round );
PRECACHE_REGISTER( asw_mortar_round );
BEGIN_DATADESC( CASW_Mortar_Round )
DEFINE_THINKFUNC( CheckNearbyTargets ),
DEFINE_THINKFUNC( Detonate ),
DEFINE_FIELD( m_fDetonateTime, FIELD_TIME ),
DEFINE_FIELD( m_fEarliestAOEDetonationTime, FIELD_TIME ),
DEFINE_FIELD( m_nGrenadeExplosionType, FIELD_INTEGER ),
DEFINE_INPUTFUNC( FIELD_VOID, "CreateEffects", InputCreateEffects ),
END_DATADESC()
IMPLEMENT_SERVERCLASS_ST( CASW_Mortar_Round, DT_ASW_Mortar_Round )
END_SEND_TABLE();
extern int g_sModelIndexFireball; // (in combatweapon.cpp) holds the index for the smoke cloud
void CASW_Mortar_Round::Spawn( void )
{
//d:\dev\main\game\infested\models\swarm\mortarbugprojectile\mortarbugprojectile.mdl
Precache();
SetModel( ASW_MORTAR_ROUND_MODEL );
SetMoveType( MOVETYPE_FLYGRAVITY, MOVECOLLIDE_FLY_CUSTOM );
m_flDamage = 80.0f;
m_DmgRadius = 220.0f;
m_takedamage = DAMAGE_NO;
m_iHealth = 1;
m_bModelOpening = false;
SetSize( -Vector(4,4,4), Vector(4,4,4) );
SetSolid( SOLID_BBOX );
SetGravity( asw_mortar_round_gravity.GetFloat() );
SetFriction( asw_vindicator_grenade_friction.GetFloat() );
SetElasticity( asw_vindicator_grenade_elasticity.GetFloat() );
SetCollisionGroup( ASW_COLLISION_GROUP_PASSABLE );
SetTouch( &CASW_Mortar_Round::Touch );
//CreateEffects();
m_hFirer = NULL;
// Tumble in air
QAngle vecAngVelocity( random->RandomFloat ( -100, -500 ), 0, 0 );
SetLocalAngularVelocity( vecAngVelocity );
m_fEarliestAOEDetonationTime = GetEarliestAOEDetonationTime();
m_fEarliestTouchDetonationTime = GetEarliestTouchDetonationTime();
m_iClusters = 0;
m_bMaster = true;
ResetSequence( LookupSequence( "MortarBugProjectile_Closed" ) );
//EmitSound( "ASWGrenade.Alarm" );
SetFuseLength( asw_mortar_round_fuse.GetFloat() );
if ( !m_bSilent )
{
EmitSound( "ASW_Ranger_Projectile.Spawned" );
}
if ( m_fDetonateTime <= gpGlobals->curtime + asw_mortar_round_radius_check_interval.GetFloat() )
{
SetThink( &CASW_Mortar_Round::Detonate );
SetNextThink( m_fDetonateTime );
}
else
{
SetThink( &CASW_Mortar_Round::CheckNearbyTargets );
SetNextThink( gpGlobals->curtime + asw_mortar_round_radius_check_interval.GetFloat() );
}
}
void CASW_Mortar_Round::Precache( )
{
BaseClass::Precache();
PrecacheModel( ASW_MORTAR_ROUND_MODEL );
PrecacheScriptSound( "ASW_Ranger_Projectile.Spawned" );
PrecacheScriptSound( "ASW_Freeze_Grenade.Explode" );
PrecacheParticleSystem( "mortar_grenade_main_trail" );
PrecacheParticleSystem( "mortar_explosion" );
PrecacheParticleSystem( "mortar_grenade_open" );
PrecacheParticleSystem( "mortar_launch" );
}
void CASW_Mortar_Round::SetFuseLength( float fSeconds )
{
m_fDetonateTime = gpGlobals->curtime + fSeconds;
}
void CASW_Mortar_Round::Touch( CBaseEntity *pOther )
{
if ( !pOther || pOther == GetOwnerEntity() )
{
return;
}
if ( !pOther->IsSolid() || pOther->IsSolidFlagSet( FSOLID_VOLUME_CONTENTS ) )
{
return;
}
if ( pOther == GetWorldEntity() )
{
// lay flat
QAngle angMortar = GetAbsAngles();
SetAbsAngles( QAngle( 0, angMortar.x, 0 ) );
SetAbsVelocity( vec3_origin );
}
if ( m_bExplodeOnWorldContact )
{
Detonate();
}
// make sure we don't die on things we shouldn't
if ( !ASWGameRules() || !ASWGameRules()->ShouldCollide( GetCollisionGroup(), pOther->GetCollisionGroup() ) )
{
return;
}
if ( pOther->m_takedamage == DAMAGE_NO )
{
if ( GetAbsVelocity().Length2D() > 60 )
{
if ( !m_bSilent )
{
EmitSound( "Grenade.ImpactHard" );
}
}
}
// can't detonate yet?
if ( gpGlobals->curtime < m_fEarliestTouchDetonationTime )
{
return;
}
// we don't want the mortar grenade to explode on impact of marine's because
// it's more fun to have them avoid the area it landed in than to take blind damage
/*
if ( pOther->m_takedamage != DAMAGE_NO )
{
if ( pOther->IsNPC() && pOther->Classify() != CLASS_PLAYER_ALLY_VITAL )
{
Detonate();
}
}*/
}
void CASW_Mortar_Round::CheckNearbyTargets( )
{
// see if an alien is nearby
if ( gpGlobals->curtime >= m_fEarliestAOEDetonationTime )
{
if ( !m_bModelOpening && gpGlobals->curtime >= (m_fDetonateTime - ASW_MORTAR_WARN_DELAY) )
{
// we are one second from detonating, commit to detonating and start the opening sequence
// regardless of anyone nearby
m_bModelOpening = true;
ResetSequence( LookupSequence( "MortarBugProjectile_Opening" ) );
CEffectData data;
data.m_vOrigin = GetAbsOrigin();
CPASFilter filter( data.m_vOrigin );
filter.SetIgnorePredictionCull(true);
DispatchParticleEffect( "mortar_grenade_open", PATTACH_ABSORIGIN_FOLLOW, this, -1, false, -1, &filter );
}
// if we exceeded the detonation time, just detonate
if ( gpGlobals->curtime >= m_fDetonateTime )
{
Detonate();
return;
}
// if the model is opening, do the animation advance
if ( m_bModelOpening )
{
StudioFrameAdvance();
SetNextThink( gpGlobals->curtime );
return;
}
float flRadius = asw_mortar_round_radius_check_scale.GetFloat() * m_DmgRadius;
Vector vecSrc = GetAbsOrigin();
CBaseEntity *pEntity = NULL;
for ( CEntitySphereQuery sphere( vecSrc, flRadius ); ( pEntity = sphere.GetCurrentEntity() ) != NULL; sphere.NextEntity() )
{
if ( !pEntity->IsPlayer() )
continue;
// give them a 2 second warning before detonation
m_fDetonateTime = MIN( m_fDetonateTime, m_fDetonateTime + ASW_MORTAR_WARN_DELAY );
}
}
if ( m_fDetonateTime <= gpGlobals->curtime + asw_mortar_round_radius_check_interval.GetFloat() )
{
SetThink( &CASW_Mortar_Round::Detonate );
SetNextThink( m_fDetonateTime );
}
else
{
SetThink( &CASW_Mortar_Round::CheckNearbyTargets );
SetNextThink( gpGlobals->curtime + asw_mortar_round_radius_check_interval.GetFloat() );
}
}
CASW_Mortar_Round* CASW_Mortar_Round::Mortar_Round_Create( float flDamage, float fRadius, int iClusters, const Vector &position, const QAngle &angles, const Vector &velocity,
const AngularImpulse &angVelocity, CBaseEntity *pOwner, ASW_Mortar_t nExplosionType )
{
CASW_Mortar_Round *pGrenade = ( CASW_Mortar_Round * )CreateEntityByName( "asw_mortar_round" );
pGrenade->SetAbsAngles( angles );
UTIL_SetOrigin( pGrenade, position );
pGrenade->SetExplosionType( nExplosionType );
pGrenade->Spawn();
pGrenade->m_flDamage = flDamage;
pGrenade->m_DmgRadius = fRadius;
pGrenade->m_hFirer = pOwner;
pGrenade->SetOwnerEntity( pOwner );
pGrenade->SetAbsVelocity( velocity );
pGrenade->SetClusters( iClusters, true );
pGrenade->CreateEffects();
// hack attack! for some reason, grenades refuse to be affect by damage forces until they're actually dead
// so we kill it immediately.
// pGrenade->TakeDamage(CTakeDamageInfo(pGrenade, pGrenade, Vector(0, 0, 1), position, 10, DMG_SLASH));
return pGrenade;
}
void CASW_Mortar_Round::DoExplosion( )
{
// scorch the ground
trace_t tr;
UTIL_TraceLine ( GetAbsOrigin(), GetAbsOrigin() + Vector( 0, 0, -80 ), MASK_SHOT, this, COLLISION_GROUP_NONE, &tr);
if ( m_bMaster )
{
if ( ( tr.m_pEnt != GetWorldEntity() ) || ( tr.hitbox != 0 ) )
{
// non-world needs smaller decals
if( tr.m_pEnt && !tr.m_pEnt->IsNPC() )
{
UTIL_DecalTrace( &tr, "SmallScorch" );
}
}
else
{
UTIL_DecalTrace( &tr, "Scorch" );
}
UTIL_ASW_ScreenShake( GetAbsOrigin(), 7.0f, 45.0f, 0.5f, 150, SHAKE_START );
}
// explosion effects
DispatchParticleEffect( "mortar_explosion", GetAbsOrigin(), Vector( m_DmgRadius, 0.0f, 0.0f ), QAngle( 0.0f, 0.0f, 0.0f ) );
if ( !m_bSilent )
{
EmitSound( "ASWGrenade.Explode" );
}
// damage to nearby things
ASWGameRules()->RadiusDamage( CTakeDamageInfo( this, m_hFirer.Get(), m_flDamage, DMG_BLAST ), GetAbsOrigin(), m_DmgRadius, CLASS_NONE, NULL );
}
void CASW_Mortar_Round::Detonate( )
{
m_takedamage = DAMAGE_NO;
switch( m_nGrenadeExplosionType )
{
case ASW_MORTAR_FREEZE:
{
Assert( 0 );
break;
}
default:
{
DoExplosion();
break;
}
}
while ( m_iClusters > 0 )
{
float fYaw = random->RandomFloat( 0.0f, 360.0f );
QAngle ang( 0.0f, fYaw, 0.0f );
Vector newVel;
AngleVectors( ang, &newVel );
newVel *= random->RandomFloat( 150.0f, 200.0f );
newVel.z = random->RandomFloat( 200.0f, 400.0f );
CASW_Mortar_Round *pGrenade = CASW_Mortar_Round::Mortar_Round_Create( m_flDamage, m_DmgRadius, 0,
GetAbsOrigin(), ang, newVel, AngularImpulse( 0.0f, 0.0f, 0.0f ), m_hFirer.Get() );
if (pGrenade)
{
pGrenade->m_fEarliestAOEDetonationTime = 0; // children can go whenever they want
pGrenade->m_fEarliestTouchDetonationTime = 0;
pGrenade->m_takedamage = DAMAGE_NO;
pGrenade->SetFuseLength( random->RandomFloat( asw_mortar_round_child_fuse_min.GetFloat(), asw_mortar_round_child_fuse_max.GetFloat() ) );
pGrenade->SetClusters( 0, false );
pGrenade->m_bKicked = m_bKicked;
}
m_iClusters--;
}
UTIL_Remove( this );
}
float CASW_Mortar_Round::GetEarliestAOEDetonationTime( )
{
return gpGlobals->curtime + asw_mortar_round_min_detonation_time.GetFloat();
}
void CASW_Mortar_Round::SetClusters( int iClusters, bool bMaster )
{
BaseClass::SetClusters( iClusters, bMaster );
if ( !bMaster )
{
SetThink( &CASW_Mortar_Round::Detonate );
SetNextThink( m_fDetonateTime );
}
}
void CASW_Mortar_Round::CreateEffects()
{
switch ( m_nGrenadeExplosionType )
{
case ASW_MORTAR_FREEZE:
{
Assert( 0 );
break;
}
default:
{
CEffectData data;
data.m_vOrigin = GetAbsOrigin();
CPASFilter filter( data.m_vOrigin );
filter.SetIgnorePredictionCull(true);
DispatchParticleEffect( "mortar_grenade_main_trail", PATTACH_ABSORIGIN_FOLLOW, this, -1, false, -1, &filter );
break;
}
}
}
void CASW_Mortar_Round::InputCreateEffects( inputdata_t &data )
{
CreateEffects();
}