This repository has been archived by the owner on May 4, 2022. It is now read-only.
forked from FWGS/hlsdk-portable
-
Notifications
You must be signed in to change notification settings - Fork 0
/
func_break.cpp
998 lines (852 loc) · 24.1 KB
/
func_break.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
/***
*
* Copyright (c) 1996-2002, Valve LLC. All rights reserved.
*
* This product contains software technology licensed from Id
* Software, Inc. ("Id Technology"). Id Technology (c) 1996 Id Software, Inc.
* All Rights Reserved.
*
* Use, distribution, and modification of this source code and/or resulting
* object code is restricted to non-commercial enhancements to products from
* Valve LLC. All other use, distribution, or modification is prohibited
* without written permission from Valve LLC.
*
****/
/*
===== bmodels.cpp ========================================================
spawn, think, and use functions for entities that use brush models
*/
#include "extdll.h"
#include "util.h"
#include "cbase.h"
#include "saverestore.h"
#include "func_break.h"
#include "decals.h"
#include "explode.h"
extern DLL_GLOBAL Vector g_vecAttackDir;
// =================== FUNC_Breakable ==============================================
// Just add more items to the bottom of this array and they will automagically be supported
// This is done instead of just a classname in the FGD so we can control which entities can
// be spawned, and still remain fairly flexible
const char *CBreakable::pSpawnObjects[] =
{
NULL, // 0
"item_battery", // 1
"item_healthkit", // 2
"weapon_9mmhandgun", // 3
"ammo_9mmclip", // 4
"weapon_9mmAR", // 5
"ammo_9mmAR", // 6
"ammo_ARgrenades", // 7
"weapon_shotgun", // 8
"ammo_buckshot", // 9
"weapon_crossbow", // 10
"ammo_crossbow", // 11
"weapon_357", // 12
"ammo_357", // 13
"weapon_rpg", // 14
"ammo_rpgclip", // 15
"ammo_gaussclip", // 16
"weapon_handgrenade", // 17
"weapon_tripmine", // 18
"weapon_satchel", // 19
"weapon_snark", // 20
"weapon_hornetgun", // 21
};
void CBreakable::KeyValue( KeyValueData* pkvd )
{
// UNDONE_WC: explicitly ignoring these fields, but they shouldn't be in the map file!
if( FStrEq( pkvd->szKeyName, "explosion" ) )
{
if( !stricmp( pkvd->szValue, "directed" ) )
m_Explosion = expDirected;
else if( !stricmp( pkvd->szValue, "random" ) )
m_Explosion = expRandom;
else
m_Explosion = expRandom;
pkvd->fHandled = TRUE;
}
else if( FStrEq( pkvd->szKeyName, "material" ) )
{
int i = atoi( pkvd->szValue );
// 0:glass, 1:metal, 2:flesh, 3:wood
if( ( i < 0 ) || ( i >= matLastMaterial ) )
m_Material = matWood;
else
m_Material = (Materials)i;
pkvd->fHandled = TRUE;
}
else if( FStrEq( pkvd->szKeyName, "deadmodel" ) )
{
pkvd->fHandled = TRUE;
}
else if( FStrEq( pkvd->szKeyName, "shards" ) )
{
//m_iShards = atof( pkvd->szValue );
pkvd->fHandled = TRUE;
}
else if( FStrEq( pkvd->szKeyName, "gibmodel" ) )
{
m_iszGibModel = ALLOC_STRING( pkvd->szValue );
pkvd->fHandled = TRUE;
}
else if( FStrEq( pkvd->szKeyName, "spawnobject" ) )
{
int object = atoi( pkvd->szValue );
if( object > 0 && object < (int)ARRAYSIZE( pSpawnObjects ) )
m_iszSpawnObject = MAKE_STRING( pSpawnObjects[object] );
pkvd->fHandled = TRUE;
}
else if( FStrEq( pkvd->szKeyName, "explodemagnitude" ) )
{
ExplosionSetMagnitude( atoi( pkvd->szValue ) );
pkvd->fHandled = TRUE;
}
else if( FStrEq( pkvd->szKeyName, "lip" ) )
pkvd->fHandled = TRUE;
else
CBaseDelay::KeyValue( pkvd );
}
//
// func_breakable - bmodel that breaks into pieces after taking damage
//
LINK_ENTITY_TO_CLASS( func_breakable, CBreakable )
TYPEDESCRIPTION CBreakable::m_SaveData[] =
{
DEFINE_FIELD( CBreakable, m_Material, FIELD_INTEGER ),
DEFINE_FIELD( CBreakable, m_Explosion, FIELD_INTEGER ),
// Don't need to save/restore these because we precache after restore
// DEFINE_FIELD( CBreakable, m_idShard, FIELD_INTEGER ),
DEFINE_FIELD( CBreakable, m_angle, FIELD_FLOAT ),
DEFINE_FIELD( CBreakable, m_iszGibModel, FIELD_STRING ),
DEFINE_FIELD( CBreakable, m_iszSpawnObject, FIELD_STRING ),
// Explosion magnitude is stored in pev->impulse
};
IMPLEMENT_SAVERESTORE( CBreakable, CBaseEntity )
void CBreakable::Spawn( void )
{
Precache();
if( FBitSet( pev->spawnflags, SF_BREAK_TRIGGER_ONLY ) )
pev->takedamage = DAMAGE_NO;
else
pev->takedamage = DAMAGE_YES;
pev->solid = SOLID_BSP;
pev->movetype = MOVETYPE_PUSH;
m_angle = pev->angles.y;
pev->angles.y = 0;
// HACK: matGlass can receive decals, we need the client to know about this
// so use class to store the material flag
if( m_Material == matGlass )
{
pev->playerclass = 1;
}
SET_MODEL( ENT( pev ), STRING( pev->model ) );//set size and link into world.
SetTouch( &CBreakable::BreakTouch );
if( FBitSet( pev->spawnflags, SF_BREAK_TRIGGER_ONLY ) ) // Only break on trigger
SetTouch( NULL );
// Flag unbreakable glass as "worldbrush" so it will block ALL tracelines
if( !IsBreakable() && pev->rendermode != kRenderNormal )
pev->flags |= FL_WORLDBRUSH;
}
const char *CBreakable::pSoundsWood[] =
{
"debris/wood1.wav",
"debris/wood2.wav",
"debris/wood3.wav",
};
const char *CBreakable::pSoundsFlesh[] =
{
"debris/flesh1.wav",
"debris/flesh2.wav",
"debris/flesh3.wav",
"debris/flesh5.wav",
"debris/flesh6.wav",
"debris/flesh7.wav",
};
const char *CBreakable::pSoundsMetal[] =
{
"debris/metal1.wav",
"debris/metal2.wav",
"debris/metal3.wav",
};
const char *CBreakable::pSoundsConcrete[] =
{
"debris/concrete1.wav",
"debris/concrete2.wav",
"debris/concrete3.wav",
};
const char *CBreakable::pSoundsGlass[] =
{
"debris/glass1.wav",
"debris/glass2.wav",
"debris/glass3.wav",
};
const char **CBreakable::MaterialSoundList( Materials precacheMaterial, int &soundCount )
{
const char **pSoundList = NULL;
switch( precacheMaterial )
{
case matWood:
pSoundList = pSoundsWood;
soundCount = ARRAYSIZE( pSoundsWood );
break;
case matFlesh:
pSoundList = pSoundsFlesh;
soundCount = ARRAYSIZE( pSoundsFlesh );
break;
case matComputer:
case matUnbreakableGlass:
case matGlass:
pSoundList = pSoundsGlass;
soundCount = ARRAYSIZE( pSoundsGlass );
break;
case matMetal:
pSoundList = pSoundsMetal;
soundCount = ARRAYSIZE( pSoundsMetal );
break;
case matCinderBlock:
case matRocks:
pSoundList = pSoundsConcrete;
soundCount = ARRAYSIZE( pSoundsConcrete );
break;
case matCeilingTile:
case matNone:
default:
soundCount = 0;
break;
}
return pSoundList;
}
void CBreakable::MaterialSoundPrecache( Materials precacheMaterial )
{
const char **pSoundList;
int i, soundCount = 0;
pSoundList = MaterialSoundList( precacheMaterial, soundCount );
for( i = 0; i < soundCount; i++ )
{
PRECACHE_SOUND( pSoundList[i] );
}
}
void CBreakable::MaterialSoundRandom( edict_t *pEdict, Materials soundMaterial, float volume )
{
const char **pSoundList;
int soundCount = 0;
pSoundList = MaterialSoundList( soundMaterial, soundCount );
if( soundCount )
EMIT_SOUND( pEdict, CHAN_BODY, pSoundList[RANDOM_LONG( 0, soundCount - 1 )], volume, 1.0f );
}
void CBreakable::Precache( void )
{
const char *pGibName = NULL;
switch( m_Material )
{
case matWood:
pGibName = "models/woodgibs.mdl";
PRECACHE_SOUND( "debris/bustcrate1.wav" );
PRECACHE_SOUND( "debris/bustcrate2.wav" );
break;
case matFlesh:
pGibName = "models/fleshgibs.mdl";
PRECACHE_SOUND( "debris/bustflesh1.wav" );
PRECACHE_SOUND( "debris/bustflesh2.wav" );
break;
case matComputer:
PRECACHE_SOUND( "buttons/spark5.wav" );
PRECACHE_SOUND( "buttons/spark6.wav" );
pGibName = "models/computergibs.mdl";
PRECACHE_SOUND( "debris/bustmetal1.wav" );
PRECACHE_SOUND( "debris/bustmetal2.wav" );
break;
case matUnbreakableGlass:
case matGlass:
pGibName = "models/glassgibs.mdl";
PRECACHE_SOUND( "debris/bustglass1.wav" );
PRECACHE_SOUND( "debris/bustglass2.wav" );
break;
case matMetal:
pGibName = "models/metalplategibs.mdl";
PRECACHE_SOUND( "debris/bustmetal1.wav" );
PRECACHE_SOUND( "debris/bustmetal2.wav" );
break;
case matCinderBlock:
pGibName = "models/cindergibs.mdl";
PRECACHE_SOUND( "debris/bustconcrete1.wav" );
PRECACHE_SOUND( "debris/bustconcrete2.wav" );
break;
case matRocks:
pGibName = "models/rockgibs.mdl";
PRECACHE_SOUND( "debris/bustconcrete1.wav" );
PRECACHE_SOUND( "debris/bustconcrete2.wav" );
break;
case matCeilingTile:
pGibName = "models/ceilinggibs.mdl";
PRECACHE_SOUND( "debris/bustceiling.wav" );
break;
case matNone:
case matLastMaterial:
break;
default:
break;
}
MaterialSoundPrecache( m_Material );
if( m_iszGibModel )
pGibName = STRING( m_iszGibModel );
m_idShard = PRECACHE_MODEL( pGibName );
// Precache the spawn item's data
if( m_iszSpawnObject )
UTIL_PrecacheOther( STRING( m_iszSpawnObject ) );
}
// play shard sound when func_breakable takes damage.
// the more damage, the louder the shard sound.
void CBreakable::DamageSound( void )
{
int pitch;
float fvol;
const char *rgpsz[6];
int i = 0;
int material = m_Material;
//if( RANDOM_LONG( 0, 1 ) )
// return;
if( RANDOM_LONG( 0, 2 ) )
pitch = PITCH_NORM;
else
pitch = 95 + RANDOM_LONG( 0, 34 );
fvol = RANDOM_FLOAT( 0.75, 1.0 );
if( material == matComputer && RANDOM_LONG( 0, 1 ) )
material = matMetal;
switch( material )
{
case matComputer:
case matGlass:
case matUnbreakableGlass:
rgpsz[0] = "debris/glass1.wav";
rgpsz[1] = "debris/glass2.wav";
rgpsz[2] = "debris/glass3.wav";
i = 3;
break;
case matWood:
rgpsz[0] = "debris/wood1.wav";
rgpsz[1] = "debris/wood2.wav";
rgpsz[2] = "debris/wood3.wav";
i = 3;
break;
case matMetal:
rgpsz[0] = "debris/metal1.wav";
rgpsz[1] = "debris/metal3.wav";
rgpsz[2] = "debris/metal2.wav";
i = 2;
break;
case matFlesh:
rgpsz[0] = "debris/flesh1.wav";
rgpsz[1] = "debris/flesh2.wav";
rgpsz[2] = "debris/flesh3.wav";
rgpsz[3] = "debris/flesh5.wav";
rgpsz[4] = "debris/flesh6.wav";
rgpsz[5] = "debris/flesh7.wav";
i = 6;
break;
case matRocks:
case matCinderBlock:
rgpsz[0] = "debris/concrete1.wav";
rgpsz[1] = "debris/concrete2.wav";
rgpsz[2] = "debris/concrete3.wav";
i = 3;
break;
case matCeilingTile:
// UNDONE: no ceiling tile shard sound yet
i = 0;
break;
}
if( i )
EMIT_SOUND_DYN( ENT( pev ), CHAN_VOICE, rgpsz[ RANDOM_LONG( 0, i - 1 )], fvol, ATTN_NORM, 0, pitch );
}
void CBreakable::BreakTouch( CBaseEntity *pOther )
{
float flDamage;
entvars_t* pevToucher = pOther->pev;
// only players can break these right now
if( !pOther->IsPlayer() || !IsBreakable() )
{
return;
}
if( FBitSet( pev->spawnflags, SF_BREAK_TOUCH ) )
{
// can be broken when run into
flDamage = pevToucher->velocity.Length() * 0.01f;
if( flDamage >= pev->health )
{
SetTouch( NULL );
TakeDamage( pevToucher, pevToucher, flDamage, DMG_CRUSH );
// do a little damage to player if we broke glass or computer
pOther->TakeDamage( pev, pev, flDamage/4, DMG_SLASH );
}
}
if( FBitSet( pev->spawnflags, SF_BREAK_PRESSURE ) && pevToucher->absmin.z >= pev->maxs.z - 2 )
{
// can be broken when stood upon
// play creaking sound here.
DamageSound();
SetThink( &CBreakable::Die );
SetTouch( NULL );
if( m_flDelay == 0.0f )
{
// !!!BUGBUG - why doesn't zero delay work?
m_flDelay = 0.1f;
}
pev->nextthink = pev->ltime + m_flDelay;
}
}
//
// Smash the our breakable object
//
// Break when triggered
void CBreakable::Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value )
{
if( IsBreakable() )
{
pev->angles.y = m_angle;
UTIL_MakeVectors( pev->angles );
g_vecAttackDir = gpGlobals->v_forward;
Die();
}
}
void CBreakable::TraceAttack( entvars_t *pevAttacker, float flDamage, Vector vecDir, TraceResult *ptr, int bitsDamageType )
{
// random spark if this is a 'computer' object
if( RANDOM_LONG( 0, 1 ) )
{
switch( m_Material )
{
case matComputer:
{
UTIL_Sparks( ptr->vecEndPos );
float flVolume = RANDOM_FLOAT( 0.7f, 1.0f );//random volume range
switch( RANDOM_LONG( 0, 1 ) )
{
case 0:
EMIT_SOUND( ENT( pev ), CHAN_VOICE, "buttons/spark5.wav", flVolume, ATTN_NORM );
break;
case 1:
EMIT_SOUND( ENT( pev ), CHAN_VOICE, "buttons/spark6.wav", flVolume, ATTN_NORM );
break;
}
}
break;
case matUnbreakableGlass:
UTIL_Ricochet( ptr->vecEndPos, RANDOM_FLOAT( 0.5f, 1.5f ) );
break;
default:
break;
}
}
CBaseDelay::TraceAttack( pevAttacker, flDamage, vecDir, ptr, bitsDamageType );
}
//=========================================================
// Special takedamage for func_breakable. Allows us to make
// exceptions that are breakable-specific
// bitsDamageType indicates the type of damage sustained ie: DMG_CRUSH
//=========================================================
int CBreakable::TakeDamage( entvars_t *pevInflictor, entvars_t *pevAttacker, float flDamage, int bitsDamageType )
{
Vector vecTemp;
// if Attacker == Inflictor, the attack was a melee or other instant-hit attack.
// (that is, no actual entity projectile was involved in the attack so use the shooter's origin).
if( pevAttacker == pevInflictor )
{
vecTemp = pevInflictor->origin - ( pev->absmin + ( pev->size * 0.5f ) );
// if a client hit the breakable with a crowbar, and breakable is crowbar-sensitive, break it now.
if( FBitSet ( pevAttacker->flags, FL_CLIENT ) &&
FBitSet ( pev->spawnflags, SF_BREAK_CROWBAR ) && ( bitsDamageType & DMG_CLUB ) )
flDamage = pev->health;
}
else
// an actual missile was involved.
{
vecTemp = pevInflictor->origin - ( pev->absmin + ( pev->size * 0.5f ) );
}
if( !IsBreakable() )
return 0;
// Breakables take double damage from the crowbar
if( bitsDamageType & DMG_CLUB )
flDamage *= 2.0f;
// Boxes / glass / etc. don't take much poison damage, just the impact of the dart - consider that 10%
if( bitsDamageType & DMG_POISON )
flDamage *= 0.1f;
// this global is still used for glass and other non-monster killables, along with decals.
g_vecAttackDir = vecTemp.Normalize();
// do the damage
pev->health -= flDamage;
if( pev->health <= 0 )
{
Killed( pevAttacker, GIB_NORMAL );
Die();
return 0;
}
// Make a shard noise each time func breakable is hit.
// Don't play shard noise if cbreakable actually died.
DamageSound();
return 1;
}
void CBreakable::Die( void )
{
Vector vecSpot;// shard origin
Vector vecVelocity;// shard velocity
char cFlag = 0;
int pitch;
float fvol;
pitch = 95 + RANDOM_LONG( 0, 29 );
if( pitch > 97 && pitch < 103 )
pitch = 100;
// The more negative pev->health, the louder
// the sound should be.
fvol = RANDOM_FLOAT( 0.85f, 1.0 ) + ( fabs( pev->health ) / 100.0f );
if( fvol > 1.0f )
fvol = 1.0f;
switch( m_Material )
{
case matGlass:
switch( RANDOM_LONG( 0, 1 ) )
{
case 0:
EMIT_SOUND_DYN( ENT( pev ), CHAN_VOICE, "debris/bustglass1.wav", fvol, ATTN_NORM, 0, pitch );
break;
case 1:
EMIT_SOUND_DYN( ENT( pev ), CHAN_VOICE, "debris/bustglass2.wav", fvol, ATTN_NORM, 0, pitch );
break;
}
cFlag = BREAK_GLASS;
break;
case matWood:
switch( RANDOM_LONG( 0, 1 ) )
{
case 0:
EMIT_SOUND_DYN( ENT( pev ), CHAN_VOICE, "debris/bustcrate1.wav", fvol, ATTN_NORM, 0, pitch );
break;
case 1:
EMIT_SOUND_DYN( ENT( pev ), CHAN_VOICE, "debris/bustcrate2.wav", fvol, ATTN_NORM, 0, pitch );
break;
}
cFlag = BREAK_WOOD;
break;
case matComputer:
case matMetal:
switch( RANDOM_LONG( 0, 1 ) )
{
case 0:
EMIT_SOUND_DYN( ENT( pev ), CHAN_VOICE, "debris/bustmetal1.wav", fvol, ATTN_NORM, 0, pitch );
break;
case 1:
EMIT_SOUND_DYN( ENT( pev ), CHAN_VOICE, "debris/bustmetal2.wav", fvol, ATTN_NORM, 0, pitch );
break;
}
cFlag = BREAK_METAL;
break;
case matFlesh:
switch( RANDOM_LONG( 0, 1 ) )
{
case 0:
EMIT_SOUND_DYN( ENT( pev ), CHAN_VOICE, "debris/bustflesh1.wav", fvol, ATTN_NORM, 0, pitch );
break;
case 1:
EMIT_SOUND_DYN( ENT( pev ), CHAN_VOICE, "debris/bustflesh2.wav", fvol, ATTN_NORM, 0, pitch );
break;
}
cFlag = BREAK_FLESH;
break;
case matRocks:
case matCinderBlock:
switch( RANDOM_LONG( 0, 1 ) )
{
case 0:
EMIT_SOUND_DYN( ENT( pev ), CHAN_VOICE, "debris/bustconcrete1.wav", fvol, ATTN_NORM, 0, pitch );
break;
case 1:
EMIT_SOUND_DYN( ENT( pev ), CHAN_VOICE, "debris/bustconcrete2.wav", fvol, ATTN_NORM, 0, pitch );
break;
}
cFlag = BREAK_CONCRETE;
break;
case matCeilingTile:
EMIT_SOUND_DYN( ENT( pev ), CHAN_VOICE, "debris/bustceiling.wav", fvol, ATTN_NORM, 0, pitch );
break;
case matNone:
case matLastMaterial:
case matUnbreakableGlass:
break;
default:
break;
}
if( m_Explosion == expDirected )
vecVelocity = g_vecAttackDir * 200.0f;
else
{
vecVelocity.x = 0;
vecVelocity.y = 0;
vecVelocity.z = 0;
}
vecSpot = pev->origin + ( pev->mins + pev->maxs ) * 0.5f;
MESSAGE_BEGIN( MSG_PVS, SVC_TEMPENTITY, vecSpot );
WRITE_BYTE( TE_BREAKMODEL );
// position
WRITE_COORD( vecSpot.x );
WRITE_COORD( vecSpot.y );
WRITE_COORD( vecSpot.z );
// size
WRITE_COORD( pev->size.x );
WRITE_COORD( pev->size.y );
WRITE_COORD( pev->size.z );
// velocity
WRITE_COORD( vecVelocity.x );
WRITE_COORD( vecVelocity.y );
WRITE_COORD( vecVelocity.z );
// randomization
WRITE_BYTE( 10 );
// Model
WRITE_SHORT( m_idShard ); //model id#
// # of shards
WRITE_BYTE( 0 ); // let client decide
// duration
WRITE_BYTE( 25 );// 2.5 seconds
// flags
WRITE_BYTE( cFlag );
MESSAGE_END();
/*float size = pev->size.x;
if( size < pev->size.y )
size = pev->size.y;
if( size < pev->size.z )
size = pev->size.z;*/
// !!! HACK This should work!
// Build a box above the entity that looks like an 8 pixel high sheet
Vector mins = pev->absmin;
Vector maxs = pev->absmax;
mins.z = pev->absmax.z;
maxs.z += 8;
// BUGBUG -- can only find 256 entities on a breakable -- should be enough
CBaseEntity *pList[256];
int count = UTIL_EntitiesInBox( pList, 256, mins, maxs, FL_ONGROUND );
if( count )
{
for( int i = 0; i < count; i++ )
{
ClearBits( pList[i]->pev->flags, FL_ONGROUND );
pList[i]->pev->groundentity = NULL;
}
}
// Don't fire something that could fire myself
pev->targetname = 0;
pev->solid = SOLID_NOT;
// Fire targets on break
SUB_UseTargets( NULL, USE_TOGGLE, 0 );
SetThink( &CBaseEntity::SUB_Remove );
pev->nextthink = pev->ltime + 0.1f;
if( m_iszSpawnObject )
CBaseEntity::Create( STRING( m_iszSpawnObject ), VecBModelOrigin( pev ), pev->angles, edict() );
if( Explodable() )
{
ExplosionCreate( Center(), pev->angles, edict(), ExplosionMagnitude(), TRUE );
}
}
BOOL CBreakable::IsBreakable( void )
{
return m_Material != matUnbreakableGlass;
}
int CBreakable::DamageDecal( int bitsDamageType )
{
if( m_Material == matGlass )
return DECAL_GLASSBREAK1 + RANDOM_LONG( 0, 2 );
if( m_Material == matUnbreakableGlass )
return DECAL_BPROOF1;
return CBaseEntity::DamageDecal( bitsDamageType );
}
class CPushable : public CBreakable
{
public:
void Spawn ( void );
void Precache( void );
void Touch ( CBaseEntity *pOther );
void Move( CBaseEntity *pMover, int push );
void KeyValue( KeyValueData *pkvd );
void Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value );
void EXPORT StopSound( void );
//virtual void SetActivator( CBaseEntity *pActivator ) { m_pPusher = pActivator; }
virtual int ObjectCaps( void ) { return ( CBaseEntity::ObjectCaps() & ~FCAP_ACROSS_TRANSITION ) | FCAP_CONTINUOUS_USE; }
virtual int Save( CSave &save );
virtual int Restore( CRestore &restore );
inline float MaxSpeed( void ) { return m_maxSpeed; }
// breakables use an overridden takedamage
virtual int TakeDamage( entvars_t *pevInflictor, entvars_t *pevAttacker, float flDamage, int bitsDamageType );
static TYPEDESCRIPTION m_SaveData[];
static const char *m_soundNames[3];
int m_lastSound; // no need to save/restore, just keeps the same sound from playing twice in a row
float m_maxSpeed;
float m_soundTime;
};
TYPEDESCRIPTION CPushable::m_SaveData[] =
{
DEFINE_FIELD( CPushable, m_maxSpeed, FIELD_FLOAT ),
DEFINE_FIELD( CPushable, m_soundTime, FIELD_TIME ),
};
IMPLEMENT_SAVERESTORE( CPushable, CBreakable )
LINK_ENTITY_TO_CLASS( func_pushable, CPushable )
const char *CPushable::m_soundNames[3] =
{
"debris/pushbox1.wav",
"debris/pushbox2.wav",
"debris/pushbox3.wav"
};
void CPushable::Spawn( void )
{
if( pev->spawnflags & SF_PUSH_BREAKABLE )
CBreakable::Spawn();
else
Precache();
pev->movetype = MOVETYPE_PUSHSTEP;
pev->solid = SOLID_BBOX;
SET_MODEL( ENT( pev ), STRING( pev->model ) );
if( pev->friction > 399 )
pev->friction = 399;
m_maxSpeed = 400 - pev->friction;
SetBits( pev->flags, FL_FLOAT );
pev->friction = 0;
pev->origin.z += 1; // Pick up off of the floor
UTIL_SetOrigin( pev, pev->origin );
// Multiply by area of the box's cross-section (assume 1000 units^3 standard volume)
pev->skin = (int)( ( pev->skin * ( pev->maxs.x - pev->mins.x ) * ( pev->maxs.y - pev->mins.y ) ) * 0.0005f );
m_soundTime = 0;
}
void CPushable::Precache( void )
{
for( int i = 0; i < 3; i++ )
PRECACHE_SOUND( m_soundNames[i] );
if( pev->spawnflags & SF_PUSH_BREAKABLE )
CBreakable::Precache();
}
void CPushable::KeyValue( KeyValueData *pkvd )
{
if( FStrEq( pkvd->szKeyName, "size" ) )
{
int bbox = atoi( pkvd->szValue );
pkvd->fHandled = TRUE;
switch( bbox )
{
case 0:
// Point
UTIL_SetSize( pev, Vector( -8.0f, -8.0f, -8.0f ), Vector( 8.0f, 8.0f, 8.0f ) );
break;
case 2:
// Big Hull!?!? !!!BUGBUG Figure out what this hull really is
UTIL_SetSize( pev, VEC_DUCK_HULL_MIN * 2.0f, VEC_DUCK_HULL_MAX * 2.0f );
break;
case 3:
// Player duck
UTIL_SetSize( pev, VEC_DUCK_HULL_MIN, VEC_DUCK_HULL_MAX );
break;
default:
case 1:
// Player
UTIL_SetSize( pev, VEC_HULL_MIN, VEC_HULL_MAX );
break;
}
}
else if( FStrEq( pkvd->szKeyName, "buoyancy" ) )
{
pev->skin = atoi( pkvd->szValue );
pkvd->fHandled = TRUE;
}
else
CBreakable::KeyValue( pkvd );
}
// Pull the func_pushable
void CPushable::Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value )
{
if( !pActivator || !pActivator->IsPlayer() )
{
if( pev->spawnflags & SF_PUSH_BREAKABLE )
this->CBreakable::Use( pActivator, pCaller, useType, value );
return;
}
if( pActivator->pev->velocity != g_vecZero )
Move( pActivator, 0 );
}
void CPushable::Touch( CBaseEntity *pOther )
{
if( FClassnameIs( pOther->pev, "worldspawn" ) )
return;
Move( pOther, 1 );
}
void CPushable::Move( CBaseEntity *pOther, int push )
{
entvars_t* pevToucher = pOther->pev;
int playerTouch = 0;
// Is entity standing on this pushable ?
if( FBitSet( pevToucher->flags,FL_ONGROUND ) && pevToucher->groundentity && VARS( pevToucher->groundentity ) == pev )
{
// Only push if floating
if( pev->waterlevel > 0 )
pev->velocity.z += pevToucher->velocity.z * 0.1f;
return;
}
// g-cont. fix pushable acceleration bug (reverted as it used in mods)
if( pOther->IsPlayer() )
{
// Don't push unless the player is pushing forward and NOT use (pull)
if( push && !( pevToucher->button & ( IN_FORWARD | IN_USE ) ) )
return;
playerTouch = 1;
}
float factor;
if( playerTouch )
{
if( !( pevToucher->flags & FL_ONGROUND ) ) // Don't push away from jumping/falling players unless in water
{
if( pev->waterlevel < 1 )
return;
else
factor = 0.1f;
}
else
factor = 1.0f;
}
else
factor = 0.25f;
pev->velocity.x += pevToucher->velocity.x * factor;
pev->velocity.y += pevToucher->velocity.y * factor;
float length = sqrt( pev->velocity.x * pev->velocity.x + pev->velocity.y * pev->velocity.y );
if( push && ( length > MaxSpeed() ) )
{
pev->velocity.x = (pev->velocity.x * MaxSpeed() / length );
pev->velocity.y = (pev->velocity.y * MaxSpeed() / length );
}
if( playerTouch )
{
pevToucher->velocity.x = pev->velocity.x;
pevToucher->velocity.y = pev->velocity.y;
if( ( gpGlobals->time - m_soundTime ) > 0.7f )
{
m_soundTime = gpGlobals->time;
if( length > 0 && FBitSet( pev->flags,FL_ONGROUND ) )
{
m_lastSound = RANDOM_LONG( 0, 2 );
EMIT_SOUND( ENT( pev ), CHAN_WEAPON, m_soundNames[m_lastSound], 0.5f, ATTN_NORM );
//SetThink( &StopSound );
//pev->nextthink = pev->ltime + 0.1f;
}
else
STOP_SOUND( ENT( pev ), CHAN_WEAPON, m_soundNames[m_lastSound] );
}
}
}
#if 0
void CPushable::StopSound( void )
{
Vector dist = pev->oldorigin - pev->origin;
if( dist.Length() <= 0 )
STOP_SOUND( ENT( pev ), CHAN_WEAPON, m_soundNames[m_lastSound] );
}
#endif
int CPushable::TakeDamage( entvars_t *pevInflictor, entvars_t *pevAttacker, float flDamage, int bitsDamageType )
{
if( pev->spawnflags & SF_PUSH_BREAKABLE )
return CBreakable::TakeDamage( pevInflictor, pevAttacker, flDamage, bitsDamageType );
return 1;
}