forked from hitmen047/Source-PlusPlus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweapon_c4.cpp
1230 lines (974 loc) · 30.8 KB
/
weapon_c4.cpp
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
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//========= Copyright © 1996-2005, Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
//=============================================================================//
#include "cbase.h"
#include "weapon_c4.h"
#include "in_buttons.h"
#include "cs_gamerules.h"
#include "decals.h"
#include "SoundEmitterSystem/isoundemittersystembase.h"
#include "KeyValues.h"
#include "fx_cs_shared.h"
#include "obstacle_pushaway.h"
#if defined( CLIENT_DLL )
#include "c_cs_player.h"
#else
#include "cs_player.h"
#include "explode.h"
#include "mapinfo.h"
#include "team.h"
#include "func_bomb_target.h"
#include "vguiscreen.h"
#include "bot.h"
#include "cs_player.h"
#include <KeyValues.h>
#endif
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
#define BLINK_INTERVAL 2.0
#define PLANTED_C4_MODEL "models/weapons/w_c4_planted.mdl"
#define HEIST_MODE_C4_TIME 25
int g_sModelIndexC4Glow = -1;
#define WEAPON_C4_ARM_TIME 3.0
#ifdef CLIENT_DLL
#else
LINK_ENTITY_TO_CLASS( planted_c4, CPlantedC4 );
PRECACHE_REGISTER( planted_c4 );
BEGIN_DATADESC( CPlantedC4 )
DEFINE_FUNCTION( C4Think )
END_DATADESC()
IMPLEMENT_SERVERCLASS_ST( CPlantedC4, DT_PlantedC4 )
SendPropBool( SENDINFO(m_bBombTicking) ),
SendPropFloat( SENDINFO(m_flC4Blow), 0, SPROP_NOSCALE ),
SendPropFloat( SENDINFO(m_flTimerLength), 0, SPROP_NOSCALE ),
SendPropFloat( SENDINFO(m_flDefuseLength), 0, SPROP_NOSCALE ),
SendPropFloat( SENDINFO(m_flDefuseCountDown), 0, SPROP_NOSCALE ),
END_SEND_TABLE()
BEGIN_PREDICTION_DATA( CPlantedC4 )
END_PREDICTION_DATA()
CUtlVector< CPlantedC4* > g_PlantedC4s;
CPlantedC4::CPlantedC4()
{
g_PlantedC4s.AddToTail( this );
}
CPlantedC4::~CPlantedC4()
{
g_PlantedC4s.FindAndRemove( this );
int i;
// Kill the control panels
for ( i = m_hScreens.Count(); --i >= 0; )
{
DestroyVGuiScreen( m_hScreens[i].Get() );
}
m_hScreens.RemoveAll();
}
int CPlantedC4::UpdateTransmitState()
{
return SetTransmitState( FL_EDICT_FULLCHECK );
}
int CPlantedC4::ShouldTransmit( const CCheckTransmitInfo *pInfo )
{
// Terrorists always need this object for the radar
// Everybody needs it for hiding the round timer and showing the planted C4 scenario icon
return FL_EDICT_ALWAYS;
}
void CPlantedC4::Precache()
{
g_sModelIndexC4Glow = PrecacheModel( "sprites/ledglow.vmt" );
PrecacheModel( PLANTED_C4_MODEL );
PrecacheVGuiScreen( "c4_panel" );
engine->ForceModelBounds( PLANTED_C4_MODEL, Vector( -7, -13, -3 ), Vector( 9, 12, 11 ) );
}
void CPlantedC4::GetControlPanelInfo( int nPanelIndex, const char *&pPanelName )
{
pPanelName = "c4_panel";
}
void CPlantedC4::GetControlPanelClassName( int nPanelIndex, const char *&pPanelName )
{
pPanelName = "vgui_screen";
}
//-----------------------------------------------------------------------------
// This is called by the base object when it's time to spawn the control panels
//-----------------------------------------------------------------------------
void CPlantedC4::SpawnControlPanels()
{
char buf[64];
// FIXME: Deal with dynamically resizing control panels?
// If we're attached to an entity, spawn control panels on it instead of use
CBaseAnimating *pEntityToSpawnOn = this;
char *pOrgLL = "controlpanel%d_ll";
char *pOrgUR = "controlpanel%d_ur";
char *pAttachmentNameLL = pOrgLL;
char *pAttachmentNameUR = pOrgUR;
Assert( pEntityToSpawnOn );
// Lookup the attachment point...
int nPanel;
for ( nPanel = 0; true; ++nPanel )
{
Q_snprintf( buf, sizeof( buf ), pAttachmentNameLL, nPanel );
int nLLAttachmentIndex = pEntityToSpawnOn->LookupAttachment(buf);
if (nLLAttachmentIndex <= 0)
{
// Try and use my panels then
pEntityToSpawnOn = this;
Q_snprintf( buf, sizeof( buf ), pOrgLL, nPanel );
nLLAttachmentIndex = pEntityToSpawnOn->LookupAttachment(buf);
if (nLLAttachmentIndex <= 0)
return;
}
Q_snprintf( buf, sizeof( buf ), pAttachmentNameUR, nPanel );
int nURAttachmentIndex = pEntityToSpawnOn->LookupAttachment(buf);
if (nURAttachmentIndex <= 0)
{
// Try and use my panels then
Q_snprintf( buf, sizeof( buf ), pOrgUR, nPanel );
nURAttachmentIndex = pEntityToSpawnOn->LookupAttachment(buf);
if (nURAttachmentIndex <= 0)
return;
}
const char *pScreenName;
GetControlPanelInfo( nPanel, pScreenName );
if (!pScreenName)
continue;
const char *pScreenClassname;
GetControlPanelClassName( nPanel, pScreenClassname );
if ( !pScreenClassname )
continue;
// Compute the screen size from the attachment points...
matrix3x4_t panelToWorld;
pEntityToSpawnOn->GetAttachment( nLLAttachmentIndex, panelToWorld );
matrix3x4_t worldToPanel;
MatrixInvert( panelToWorld, worldToPanel );
// Now get the lower right position + transform into panel space
Vector lr, lrlocal;
pEntityToSpawnOn->GetAttachment( nURAttachmentIndex, panelToWorld );
MatrixGetColumn( panelToWorld, 3, lr );
VectorTransform( lr, worldToPanel, lrlocal );
float flWidth = lrlocal.x;
float flHeight = lrlocal.y;
CVGuiScreen *pScreen = CreateVGuiScreen( pScreenClassname, pScreenName, pEntityToSpawnOn, this, nLLAttachmentIndex );
pScreen->ChangeTeam( GetTeamNumber() );
pScreen->SetActualSize( flWidth, flHeight );
pScreen->SetActive( true );
pScreen->MakeVisibleOnlyToTeammates( false );
int nScreen = m_hScreens.AddToTail( );
m_hScreens[nScreen].Set( pScreen );
}
}
void CPlantedC4::SetTransmit( CCheckTransmitInfo *pInfo, bool bAlways )
{
// Are we already marked for transmission?
if ( pInfo->m_pTransmitEdict->Get( entindex() ) )
return;
BaseClass::SetTransmit( pInfo, bAlways );
// Force our screens to be sent too.
for ( int i=0; i < m_hScreens.Count(); i++ )
{
CVGuiScreen *pScreen = m_hScreens[i].Get();
pScreen->SetTransmit( pInfo, bAlways );
}
}
CPlantedC4* CPlantedC4::ShootSatchelCharge( CCSPlayer *pevOwner, Vector vecStart, QAngle vecAngles )
{
CPlantedC4 *pGrenade = dynamic_cast< CPlantedC4* >( CreateEntityByName( "planted_c4" ) );
if ( pGrenade )
{
vecAngles[0] = 0;
vecAngles[2] = 0;
pGrenade->Init( pevOwner, vecStart, vecAngles );
return pGrenade;
}
else
{
Warning( "Can't create planted_c4 entity!\n" );
return NULL;
}
}
void CPlantedC4::Init( CCSPlayer *pevOwner, Vector vecStart, QAngle vecAngles )
{
SetMoveType( MOVETYPE_NONE );
SetSolid( SOLID_NONE );
SetModel( PLANTED_C4_MODEL ); // Change this to c4 model
SetCollisionBounds( Vector( 0, 0, 0 ), Vector( 8, 8, 8 ) );
SetAbsOrigin( vecStart );
SetAbsAngles( vecAngles );
SetOwnerEntity( pevOwner );
// Detonate in "time" seconds
SetThink( &CPlantedC4::C4Think );
SetNextThink( gpGlobals->curtime + 0.1f );
m_flTimerLength = mp_c4timer.GetInt();
m_flC4Blow = gpGlobals->curtime + m_flTimerLength;
m_flNextDefuse = 0;
m_bStartDefuse = false;
m_bBombTicking = true;
SetFriction( 0.9 );
m_flDefuseLength = 0.0f;
SpawnControlPanels();
}
void CPlantedC4::C4Think()
{
if (!IsInWorld())
{
UTIL_Remove( this );
return;
}
//Bomb is dead, don't think anymore
if( !m_bBombTicking )
{
SetThink( NULL );
return;
}
SetNextThink( gpGlobals->curtime + 0.12 );
#ifndef CLIENT_DLL
// let the bots hear the bomb beeping
// BOTPORT: Emit beep events at same time as client effects
IGameEvent * event = gameeventmanager->CreateEvent( "bomb_beep" );
if( event )
{
event->SetInt( "entindex", entindex() );
gameeventmanager->FireEvent( event );
}
#endif
// IF the timer has expired ! blow this bomb up!
if (m_flC4Blow <= gpGlobals->curtime)
{
// give the defuser credit for defusing the bomb
CBasePlayer *pBombOwner = dynamic_cast< CBasePlayer* >( GetOwnerEntity() );
if ( pBombOwner )
{
pBombOwner->IncrementFragCount( 3 );
}
CSGameRules()->m_bBombDropped = false;
trace_t tr;
Vector vecSpot = GetAbsOrigin();
vecSpot[2] += 8;
UTIL_TraceLine( vecSpot, vecSpot + Vector ( 0, 0, -40 ), MASK_SOLID, this, COLLISION_GROUP_NONE, &tr );
Explode( &tr, DMG_BLAST );
CSGameRules()->m_bBombPlanted = false;
IGameEvent * event = gameeventmanager->CreateEvent( "bomb_exploded" );
if( event )
{
event->SetInt( "userid", pBombOwner?pBombOwner->GetUserID():-1 );
event->SetInt( "site", m_iBombSiteIndex );
event->SetInt( "priority", 9 );
gameeventmanager->FireEvent( event );
}
}
//if the defusing process has started
if ((m_bStartDefuse == true) && (m_pBombDefuser != NULL))
{
//if the defusing process has not ended yet
if ( m_flDefuseCountDown > gpGlobals->curtime)
{
int iOnGround = FBitSet( m_pBombDefuser->GetFlags(), FL_ONGROUND );
//if the bomb defuser has stopped defusing the bomb
if( m_flNextDefuse < gpGlobals->curtime || !iOnGround )
{
if ( !iOnGround && m_pBombDefuser->IsAlive() )
ClientPrint( m_pBombDefuser, HUD_PRINTCENTER, "#C4_Defuse_Must_Be_On_Ground");
// release the player from being frozen
m_pBombDefuser->ResetMaxSpeed();
m_pBombDefuser->m_bIsDefusing = false;
#ifndef CLIENT_DLL
// tell the bots someone has aborted defusing
IGameEvent * event = gameeventmanager->CreateEvent( "bomb_abortdefuse" );
if( event )
{
event->SetInt("userid", m_pBombDefuser->GetUserID() );
event->SetInt( "priority", 6 );
gameeventmanager->FireEvent( event );
}
#endif
//cancel the progress bar
m_pBombDefuser->SetProgressBarTime( 0 );
m_pBombDefuser = NULL;
m_bStartDefuse = false;
m_flDefuseCountDown = 0;
m_flDefuseLength = 0; //force it to show completely defused
}
return;
}
//if the defuse process has ended, kill the c4
else if ( !m_pBombDefuser->IsDead() )
{
IGameEvent * event = gameeventmanager->CreateEvent( "bomb_defused" );
if( event )
{
event->SetInt("userid", m_pBombDefuser->GetUserID() );
event->SetInt("site", m_iBombSiteIndex );
event->SetInt( "priority", 9 );
gameeventmanager->FireEvent( event );
}
Vector soundPosition = m_pBombDefuser->GetAbsOrigin() + Vector( 0, 0, 5 );
CPASAttenuationFilter filter( soundPosition );
EmitSound( filter, entindex(), "c4.disarmfinish" );
// The bomb has just been disarmed.. Check to see if the round should end now
m_bBombTicking = false;
// release the player from being frozen
m_pBombDefuser->ResetMaxSpeed();
m_pBombDefuser->m_bIsDefusing = false;
CSGameRules()->m_bBombDefused = true;
CSGameRules()->CheckWinConditions();
// give the defuser credit for defusing the bomb
m_pBombDefuser->IncrementFragCount( 3 );
CSGameRules()->m_bBombDropped = false;
CSGameRules()->m_bBombPlanted = false;
// Clear their progress bar.
m_pBombDefuser->SetProgressBarTime( 0 );
m_pBombDefuser = NULL;
m_bStartDefuse = false;
m_flDefuseLength = 10;
return;
}
#ifndef CLIENT_DLL
// tell the bots someone has aborted defusing
IGameEvent * event = gameeventmanager->CreateEvent( "bomb_abortdefuse" );
if( event )
{
event->SetInt("userid", m_pBombDefuser->GetUserID() );
event->SetInt( "priority", 6 );
gameeventmanager->FireEvent( event );
}
#endif
//if it gets here then the previouse defuser has taken off or been killed
// release the player from being frozen
m_pBombDefuser->ResetMaxSpeed();
m_pBombDefuser->m_bIsDefusing = false;
m_bStartDefuse = false;
m_pBombDefuser = NULL;
}
}
// Regular explosions
void CPlantedC4::Explode( trace_t *pTrace, int bitsDamageType )
{
// Check to see if the round is over after the bomb went off...
CSGameRules()->m_bTargetBombed = true;
m_bBombTicking = false;
CSGameRules()->CheckWinConditions();
// Do the Damage
float flBombRadius = 500;
if ( g_pMapInfo )
flBombRadius = g_pMapInfo->m_flBombRadius;
// Output to the bomb target ent
CBaseEntity *pTarget = NULL;
variant_t emptyVariant;
while ((pTarget = gEntList.FindEntityByClassname( pTarget, "func_bomb_target" )) != NULL)
{
//Adrian - But only to the one we want!
if ( pTarget->entindex() != m_iBombSiteIndex )
continue;
pTarget->AcceptInput( "BombExplode", this, this, emptyVariant, 0 );
break;
}
// Pull out of the wall a bit
if ( pTrace->fraction != 1.0 )
{
SetAbsOrigin( pTrace->endpos + (pTrace->plane.normal * 0.6) );
}
{
Vector pos = GetAbsOrigin() + Vector( 0,0,8 );
// add an explosion TE so it affects clientside physics
CPASFilter filter( pos );
te->Explosion( filter, 0.0,
&pos,
g_sModelIndexFireball,
50.0,
25,
TE_EXPLFLAG_NONE,
flBombRadius * 3.5,
200 );
}
// Fireball sprite and sound!!
{
Vector fireballPos = GetAbsOrigin();
CPVSFilter filter( fireballPos );
te->Sprite( filter, 0, &fireballPos, g_sModelIndexFireball, 100, 150 );
}
{
Vector fireballPos = GetAbsOrigin() + Vector(
random->RandomFloat( -512, 512 ),
random->RandomFloat( -512, 512 ),
random->RandomFloat( -10, 10 ) );
CPVSFilter filter( fireballPos );
te->Sprite( filter, 0, &fireballPos, g_sModelIndexFireball, 100, 150 );
}
{
Vector fireballPos = GetAbsOrigin() + Vector(
random->RandomFloat( -512, 512 ),
random->RandomFloat( -512, 512 ),
random->RandomFloat( -10, 10 ) );
CPVSFilter filter( fireballPos );
te->Sprite( filter, 0, &fireballPos, g_sModelIndexFireball, 100, 150 );
}
// Sound! for everyone
CBroadcastRecipientFilter filter;
EmitSound( filter, entindex(), "c4.explode" );
// Decal!
UTIL_DecalTrace( pTrace, "Scorch" );
// Shake!
UTIL_ScreenShake( pTrace->endpos, 25.0, 150.0, 1.0, 3000, SHAKE_START );
SetOwnerEntity( NULL ); // can't traceline attack owner if this is set
CSGameRules()->RadiusDamage(
CTakeDamageInfo( this, GetOwnerEntity(), flBombRadius, bitsDamageType ),
GetAbsOrigin(),
flBombRadius * 3.5, //Matt - don't ask me, this is how CS does it.
CLASS_NONE,
true ); // IGNORE THE WORLD!!
// send director message, that something important happed here
/*
MESSAGE_BEGIN( MSG_SPEC, SVC_DIRECTOR );
WRITE_BYTE ( 9 ); // command length in bytes
WRITE_BYTE ( DRC_CMD_EVENT ); // bomb explode
WRITE_SHORT( ENTINDEX(this->edict()) ); // index number of primary entity
WRITE_SHORT( 0 ); // index number of secondary entity
WRITE_LONG( 15 | DRC_FLAG_FINAL ); // eventflags (priority and flags)
MESSAGE_END();
*/
UTIL_Remove( this );
}
// For CTs to defuse the c4
void CPlantedC4::Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value )
{
//Can't defuse if its already defused or if it has blown up
if( !m_bBombTicking )
{
SetUse( NULL );
return;
}
CCSPlayer *player = dynamic_cast< CCSPlayer* >( pActivator );
if ( !player || player->GetTeamNumber() != TEAM_CT )
return;
if ( m_bStartDefuse )
{
if ( player != m_pBombDefuser )
{
if ( player->m_iNextTimeCheck < gpGlobals->curtime )
{
ClientPrint( player, HUD_PRINTCENTER, "#Bomb_Already_Being_Defused" );
player->m_iNextTimeCheck = gpGlobals->curtime + 1;
}
return;
}
m_flNextDefuse = gpGlobals->curtime + 0.5;
}
else
{
// freeze the player in place while defusing
player->SetMaxSpeed( 1 );
IGameEvent * event = gameeventmanager->CreateEvent("bomb_begindefuse" );
if( event )
{
event->SetInt( "userid", player->GetUserID() );
if ( player->HasDefuser() )
{
event->SetInt( "haskit", 1 );
// TODO show messages on clients on event
ClientPrint( player, HUD_PRINTCENTER, "#Defusing_Bomb_With_Defuse_Kit" );
}
else
{
event->SetInt( "haskit", 0 );
// TODO show messages on clients on event
ClientPrint( player, HUD_PRINTCENTER, "#Defusing_Bomb_Without_Defuse_Kit" );
}
event->SetInt( "priority", 8 );
gameeventmanager->FireEvent( event );
}
Vector soundPosition = player->GetAbsOrigin() + Vector( 0, 0, 5 );
CPASAttenuationFilter filter( soundPosition );
EmitSound( filter, entindex(), "c4.disarmstart" );
m_flDefuseLength = player->HasDefuser() ? 5 : 10;
m_flNextDefuse = gpGlobals->curtime + 0.5;
m_pBombDefuser = player;
m_bStartDefuse = TRUE;
player->m_bIsDefusing = true;
m_flDefuseCountDown = gpGlobals->curtime + m_flDefuseLength;
//start the progress bar
player->SetProgressBarTime( m_flDefuseLength );
}
}
#endif
// -------------------------------------------------------------------------------- //
// Tables.
// -------------------------------------------------------------------------------- //
IMPLEMENT_NETWORKCLASS_ALIASED( C4, DT_WeaponC4 )
BEGIN_NETWORK_TABLE( CC4, DT_WeaponC4 )
#ifdef CLIENT_DLL
RecvPropBool( RECVINFO( m_bStartedArming ) ),
RecvPropBool( RECVINFO( m_bBombPlacedAnimation ) ),
RecvPropFloat( RECVINFO( m_fArmedTime ) )
#else
SendPropBool( SENDINFO( m_bStartedArming ) ),
SendPropBool( SENDINFO( m_bBombPlacedAnimation ) ),
SendPropFloat( SENDINFO( m_fArmedTime ), 0, SPROP_NOSCALE )
#endif
END_NETWORK_TABLE()
#if defined CLIENT_DLL
BEGIN_PREDICTION_DATA( CC4 )
DEFINE_PRED_FIELD( m_bStartedArming, FIELD_INTEGER, FTYPEDESC_INSENDTABLE ),
DEFINE_PRED_FIELD( m_bBombPlacedAnimation, FIELD_INTEGER, FTYPEDESC_INSENDTABLE ),
DEFINE_PRED_FIELD( m_fArmedTime, FIELD_FLOAT, FTYPEDESC_INSENDTABLE )
END_PREDICTION_DATA()
#endif
LINK_ENTITY_TO_CLASS( weapon_c4, CC4 );
PRECACHE_WEAPON_REGISTER( weapon_c4 );
// -------------------------------------------------------------------------------- //
// Globals.
// -------------------------------------------------------------------------------- //
CUtlVector< CC4* > g_C4s;
// -------------------------------------------------------------------------------- //
// CC4 implementation.
// -------------------------------------------------------------------------------- //
CC4::CC4()
{
g_C4s.AddToTail( this );
#if defined( CLIENT_DLL )
m_szScreenText[0] = '\0';
#endif
}
CC4::~CC4()
{
g_C4s.FindAndRemove( this );
}
void CC4::Spawn()
{
BaseClass::Spawn();
//Don't allow players to shoot the C4 around
SetCollisionGroup( COLLISION_GROUP_DEBRIS );
//Don't be damaged / moved by explosions
m_takedamage = DAMAGE_NO;
m_bBombPlanted = false;
}
void CC4::ItemPostFrame()
{
CCSPlayer *pPlayer = GetPlayerOwner();
if ( !pPlayer )
return;
// Disable all the firing code.. the C4 grenade is all custom.
if ( pPlayer->m_nButtons & IN_ATTACK )
{
PrimaryAttack();
}
else
{
WeaponIdle();
#ifndef CLIENT_DLL
pPlayer->ResetMaxSpeed();
#endif
}
}
#if defined( CLIENT_DLL )
bool CC4::OnFireEvent( C_BaseViewModel *pViewModel, const Vector& origin, const QAngle& angles, int event, const char *options )
{
if( event == 7001 )
{
//set the screen text to the string in 'options'
Q_strncpy( m_szScreenText, options, 16 );
return true;
}
return BaseClass::OnFireEvent( pViewModel, origin, angles, event, options );
}
char *CC4::GetScreenText( void )
{
if( m_bStartedArming )
return m_szScreenText;
else
return "";
}
#endif //CLIENT_DLL
#ifdef GAME_DLL
unsigned int CC4::PhysicsSolidMaskForEntity( void ) const
{
return BaseClass::PhysicsSolidMaskForEntity() | CONTENTS_PLAYERCLIP;
}
void CC4::Precache()
{
PrecacheVGuiScreen( "c4_view_panel" );
PrecacheScriptSound( "c4.disarmfinish" );
PrecacheScriptSound( "c4.explode" );
PrecacheScriptSound( "c4.disarmstart" );
PrecacheScriptSound( "c4.plant" );
PrecacheScriptSound( "C4.PlantSound" );
BaseClass::Precache();
}
//-----------------------------------------------------------------------------
// Purpose: Gets info about the control panels
//-----------------------------------------------------------------------------
void CC4::GetControlPanelInfo( int nPanelIndex, const char *&pPanelName )
{
pPanelName = "c4_view_panel";
}
bool CC4::Holster( CBaseCombatWeapon *pSwitchingTo )
{
CCSPlayer *pPlayer = GetPlayerOwner();
if ( pPlayer )
pPlayer->SetProgressBarTime( 0 );
m_bStartedArming = false; // stop arming sequence
return BaseClass::Holster( pSwitchingTo );
}
bool CC4::ShouldRemoveOnRoundRestart()
{
// Doesn't matter if we have an owner or not.. always remove the C4 when the round restarts.
// The gamerules will give another C4 to some lucky player.
CCSPlayer *pPlayer = GetPlayerOwner();
if ( pPlayer && pPlayer->GetActiveWeapon() == this )
engine->ClientCommand( pPlayer->edict(), "lastinv reset\n" );
return true;
}
#endif
void CC4::PrimaryAttack()
{
bool PlaceBomb = false;
CCSPlayer *pPlayer = GetPlayerOwner();
if ( !pPlayer )
return;
int onGround = FBitSet( pPlayer->GetFlags(), FL_ONGROUND );
CBaseEntity *groundEntity = (onGround) ? pPlayer->GetGroundEntity() : NULL;
if ( groundEntity )
{
// Don't let us stand on players, breakables, or pushaway physics objects to plant
if ( groundEntity->IsPlayer() ||
IsPushableEntity( groundEntity ) ||
#ifndef CLIENT_DLL
IsBreakableEntity( groundEntity ) ||
#endif // !CLIENT_DLL
IsPushAwayEntity( groundEntity ) )
{
onGround = false;
}
}
if( m_bStartedArming == false && m_bBombPlanted == false )
{
if( pPlayer->m_bInBombZone && onGround )
{
m_bStartedArming = true;
m_fArmedTime = gpGlobals->curtime + WEAPON_C4_ARM_TIME;
m_bBombPlacedAnimation = false;
#if !defined( CLIENT_DLL )
// init the beep flags
int i;
for( i=0;i<NUM_BEEPS;i++ )
m_bPlayedArmingBeeps[i] = false;
// freeze the player in place while planting
pPlayer->SetMaxSpeed( 1 );
// player "arming bomb" animation
pPlayer->SetAnimation( PLAYER_ATTACK1 );
pPlayer->SetNextAttack( gpGlobals->curtime );
IGameEvent * event = gameeventmanager->CreateEvent( "bomb_beginplant" );
if( event )
{
event->SetInt("userid", pPlayer->GetUserID() );
event->SetInt("site", pPlayer->m_iBombSiteIndex );
event->SetInt( "priority", 8 );
gameeventmanager->FireEvent( event );
}
#endif
SendWeaponAnim( ACT_VM_PRIMARYATTACK );
FX_PlantBomb( pPlayer->entindex(), pPlayer->Weapon_ShootPosition() );
}
else
{
if ( !pPlayer->m_bInBombZone )
{
ClientPrint( pPlayer, HUD_PRINTCENTER, "#C4_Plant_At_Bomb_Spot");
}
else
{
ClientPrint( pPlayer, HUD_PRINTCENTER, "#C4_Plant_Must_Be_On_Ground");
}
m_flNextPrimaryAttack = gpGlobals->curtime + 1.0;
return;
}
}
else
{
if ( !onGround || !pPlayer->m_bInBombZone )
{
if( !pPlayer->m_bInBombZone )
{
ClientPrint( pPlayer, HUD_PRINTCENTER, "#C4_Arming_Cancelled" );
}
else
{
ClientPrint( pPlayer, HUD_PRINTCENTER, "#C4_Plant_Must_Be_On_Ground" );
}
m_flNextPrimaryAttack = gpGlobals->curtime + 1.5;
m_bStartedArming = false;
#if !defined( CLIENT_DLL )
// release the player from being frozen, we've somehow left the bomb zone
pPlayer->ResetMaxSpeed();
pPlayer->SetProgressBarTime( 0 );
//pPlayer->SetAnimation( PLAYER_HOLDBOMB );
IGameEvent * event = gameeventmanager->CreateEvent( "bomb_abortplant" );
if( event )
{
event->SetInt("userid", pPlayer->GetUserID() );
event->SetInt("site", pPlayer->m_iBombSiteIndex );
event->SetInt( "priority", 8 );
gameeventmanager->FireEvent( event );
}
#endif
if(m_bBombPlacedAnimation == true) //this means the placement animation is canceled
{
SendWeaponAnim( ACT_VM_DRAW );
}
else
{
SendWeaponAnim( ACT_VM_IDLE );
}
return;
}
else
{
#ifndef CLIENT_DLL
PlayArmingBeeps();
#endif
if( gpGlobals->curtime >= m_fArmedTime ) //the c4 is ready to be armed
{
//check to make sure the player is still in the bomb target area
PlaceBomb = true;
}
else if( ( gpGlobals->curtime >= (m_fArmedTime - 0.75) ) && ( !m_bBombPlacedAnimation ) )
{
//call the c4 Placement animation
m_bBombPlacedAnimation = true;
SendWeaponAnim( ACT_VM_SECONDARYATTACK );
#if !defined( CLIENT_DLL )
// player "place" animation
//pPlayer->SetAnimation( PLAYER_HOLDBOMB );
#endif
}
}
}
if ( PlaceBomb && m_bStartedArming )
{
m_bStartedArming = false;
m_fArmedTime = 0;
if( pPlayer->m_bInBombZone )
{
#if !defined( CLIENT_DLL )
CPlantedC4 *pC4 = CPlantedC4::ShootSatchelCharge( pPlayer, pPlayer->GetAbsOrigin(), pPlayer->GetAbsAngles() );
if ( pC4 )
{
pC4->SetBombSiteIndex( pPlayer->m_iBombSiteIndex );
trace_t tr;
UTIL_TraceEntity( pC4, GetAbsOrigin(), GetAbsOrigin() + Vector(0,0,-200), MASK_SOLID, this, COLLISION_GROUP_NONE, &tr );
pC4->SetAbsOrigin( tr.endpos );
CBombTarget *pBombTarget = (CBombTarget*)UTIL_EntityByIndex( pPlayer->m_iBombSiteIndex );
if ( pBombTarget )
{
CBaseEntity *pAttachPoint = gEntList.FindEntityByName( NULL, pBombTarget->GetBombMountTarget() );
if ( pAttachPoint )
{
pC4->SetAbsOrigin( pAttachPoint->GetAbsOrigin() );
pC4->SetAbsAngles( pAttachPoint->GetAbsAngles() );
pC4->SetParent( pAttachPoint );
}
variant_t emptyVariant;
pBombTarget->AcceptInput( "BombPlanted", pC4, pC4, emptyVariant, 0 );
}
}
IGameEvent * event = gameeventmanager->CreateEvent( "bomb_planted" );
if( event )
{
event->SetInt("userid", pPlayer->GetUserID() );
event->SetInt("site", pPlayer->m_iBombSiteIndex );
event->SetInt("posx", pPlayer->GetAbsOrigin().x );
event->SetInt("posy", pPlayer->GetAbsOrigin().y );
event->SetInt( "priority", 8 );
gameeventmanager->FireEvent( event );
}
// Fire a beep event also so the bots have a chance to hear the bomb
event = gameeventmanager->CreateEvent( "bomb_beep" );
if ( event )
{
event->SetInt( "entindex", entindex() );
gameeventmanager->FireEvent( event );
}
pPlayer->SetProgressBarTime( 0 );