-
Notifications
You must be signed in to change notification settings - Fork 271
/
Copy pathprops.cpp
6133 lines (5144 loc) · 181 KB
/
props.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 Valve Corporation, All rights reserved. ============//
//
// Purpose: static_prop - don't move, don't animate, don't do anything.
// physics_prop - move, take damage, but don't animate
//
//===========================================================================//
#include "cbase.h"
#include "BasePropDoor.h"
#include "ai_basenpc.h"
#include "npcevent.h"
#include "engine/IEngineSound.h"
#include "locksounds.h"
#include "filters.h"
#include "physics.h"
#include "vphysics_interface.h"
#include "entityoutput.h"
#include "vcollide_parse.h"
#include "studio.h"
#include "explode.h"
#include "utlrbtree.h"
#include "tier1/strtools.h"
#include "physics_impact_damage.h"
#include "KeyValues.h"
#include "filesystem.h"
#include "scriptevent.h"
#include "entityblocker.h"
#include "soundent.h"
#include "EntityFlame.h"
#include "game.h"
#include "physics_prop_ragdoll.h"
#include "decals.h"
#include "hierarchy.h"
#include "shareddefs.h"
#include "physobj.h"
#include "physics_npc_solver.h"
#include "SoundEmitterSystem/isoundemittersystembase.h"
#include "datacache/imdlcache.h"
#include "doors.h"
#include "physics_collisionevent.h"
#include "gamestats.h"
#include "vehicle_base.h"
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
#define DOOR_HARDWARE_GROUP 1
// Any barrel farther away than this is ignited rather than exploded.
#define PROP_EXPLOSION_IGNITE_RADIUS 32.0f
// How many times to remind the player that supply crates can be broken
// (displayed when the supply crate is picked up)
#define NUM_SUPPLY_CRATE_HUD_HINTS 3
extern CBaseEntity *FindPickerEntity( CBasePlayer *pPlayer );
ConVar g_debug_doors( "g_debug_doors", "0" );
ConVar breakable_disable_gib_limit( "breakable_disable_gib_limit", "0" );
ConVar breakable_multiplayer( "breakable_multiplayer", "1" );
// AI Interaction for being hit by a physics object
int g_interactionHitByPlayerThrownPhysObj = 0;
int g_interactionPlayerPuntedHeavyObject = 0;
int g_ActiveGibCount = 0;
ConVar prop_active_gib_limit( "prop_active_gib_limit", "999999" );
ConVar prop_active_gib_max_fade_time( "prop_active_gib_max_fade_time", "999999" );
#define ACTIVE_GIB_LIMIT prop_active_gib_limit.GetInt()
#define ACTIVE_GIB_FADE prop_active_gib_max_fade_time.GetInt()
// Damage type modifiers for breakable objects.
ConVar func_breakdmg_bullet( "func_breakdmg_bullet", "0.5" );
ConVar func_breakdmg_club( "func_breakdmg_club", "1.5" );
ConVar func_breakdmg_explosive( "func_breakdmg_explosive", "1.25" );
ConVar sv_turbophysics( "sv_turbophysics", "0", FCVAR_REPLICATED, "Turns on turbo physics" );
#ifdef HL2_EPISODIC
#define PROP_FLARE_LIFETIME 30.0f
#define PROP_FLARE_IGNITE_SUBSTRACT 5.0f
CBaseEntity *CreateFlare( Vector vOrigin, QAngle Angles, CBaseEntity *pOwner, float flDuration );
void KillFlare( CBaseEntity *pOwnerEntity, CBaseEntity *pEntity, float flKillTime );
#endif
//-----------------------------------------------------------------------------
// Purpose: Breakable objects take different levels of damage based upon the damage type.
// This isn't contained by CBaseProp, because func_breakables use it as well.
//-----------------------------------------------------------------------------
float GetBreakableDamage( const CTakeDamageInfo &inputInfo, IBreakableWithPropData *pProp )
{
float flDamage = inputInfo.GetDamage();
int iDmgType = inputInfo.GetDamageType();
// Bullet damage?
if ( iDmgType & DMG_BULLET )
{
// Buckshot does double damage to breakables
if ( iDmgType & DMG_BUCKSHOT )
{
if ( pProp )
{
flDamage *= (pProp->GetDmgModBullet() * 2);
}
else
{
// Bullets do little damage to breakables
flDamage *= (func_breakdmg_bullet.GetFloat() * 2);
}
}
else
{
if ( pProp )
{
flDamage *= pProp->GetDmgModBullet();
}
else
{
// Bullets do little damage to breakables
flDamage *= func_breakdmg_bullet.GetFloat();
}
}
}
// Club damage?
if ( iDmgType & DMG_CLUB )
{
if ( pProp )
{
flDamage *= pProp->GetDmgModClub();
}
else
{
// Club does extra damage
flDamage *= func_breakdmg_club.GetFloat();
}
}
// Explosive damage?
if ( iDmgType & DMG_BLAST )
{
if ( pProp )
{
flDamage *= pProp->GetDmgModExplosive();
}
else
{
// Explosions do extra damage
flDamage *= func_breakdmg_explosive.GetFloat();
}
}
if ( (iDmgType & DMG_SLASH) && (iDmgType & DMG_CRUSH) )
{
// Cut by a Ravenholm propeller trap
flDamage *= 10.0f;
}
// Poison & other timebased damage types do no damage
if ( g_pGameRules->Damage_IsTimeBased( iDmgType ) )
{
flDamage = 0;
}
return flDamage;
}
//=============================================================================================================
// BASE PROP
//=============================================================================================================
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CBaseProp::Spawn( void )
{
char *szModel = (char *)STRING( GetModelName() );
if (!szModel || !*szModel)
{
Warning( "prop at %.0f %.0f %0.f missing modelname\n", GetAbsOrigin().x, GetAbsOrigin().y, GetAbsOrigin().z );
UTIL_Remove( this );
return;
}
PrecacheModel( szModel );
Precache();
SetModel( szModel );
// Load this prop's data from the propdata file
int iResult = ParsePropData();
if ( !OverridePropdata() )
{
if ( iResult == PARSE_FAILED_BAD_DATA )
{
DevWarning( "%s at %.0f %.0f %0.f uses model %s, which has an invalid prop_data type. DELETED.\n", GetClassname(), GetAbsOrigin().x, GetAbsOrigin().y, GetAbsOrigin().z, szModel );
UTIL_Remove( this );
return;
}
else if ( iResult == PARSE_FAILED_NO_DATA )
{
// If we don't have data, but we're a prop_physics, fail
if ( FClassnameIs( this, "prop_physics" ) )
{
DevWarning( "%s at %.0f %.0f %0.f uses model %s, which has no propdata which means it must be used on a prop_static. DELETED.\n", GetClassname(), GetAbsOrigin().x, GetAbsOrigin().y, GetAbsOrigin().z, szModel );
UTIL_Remove( this );
return;
}
}
else if ( iResult == PARSE_SUCCEEDED )
{
// If we have data, and we're not a physics prop, fail
if ( !dynamic_cast<CPhysicsProp*>(this) )
{
DevWarning( "%s at %.0f %.0f %0.f uses model %s, which has propdata which means that it be used on a prop_physics. DELETED.\n", GetClassname(), GetAbsOrigin().x, GetAbsOrigin().y, GetAbsOrigin().z, szModel );
UTIL_Remove( this );
return;
}
}
}
SetMoveType( MOVETYPE_PUSH );
m_takedamage = DAMAGE_NO;
SetNextThink( TICK_NEVER_THINK );
m_flAnimTime = gpGlobals->curtime;
m_flPlaybackRate = 0.0;
SetCycle( 0 );
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CBaseProp::Precache( void )
{
if ( GetModelName() == NULL_STRING )
{
Msg( "%s at (%.3f, %.3f, %.3f) has no model name!\n", GetClassname(), GetAbsOrigin().x, GetAbsOrigin().y, GetAbsOrigin().z );
SetModelName( AllocPooledString( "models/error.mdl" ) );
}
PrecacheModel( STRING( GetModelName() ) );
PrecacheScriptSound( "Metal.SawbladeStick" );
PrecacheScriptSound( "PropaneTank.Burst" );
#ifdef HL2_EPISODIC
UTIL_PrecacheOther( "env_flare" );
#endif
BaseClass::Precache();
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CBaseProp::Activate( void )
{
BaseClass::Activate();
// Make sure mapmakers haven't used the wrong prop type.
if ( m_takedamage == DAMAGE_NO && m_iHealth != 0 )
{
Warning("%s has a health specified in model '%s'. Use prop_physics or prop_dynamic instead.\n", GetClassname(), STRING(GetModelName()) );
}
}
//-----------------------------------------------------------------------------
// Purpose: Handles keyvalues from the BSP. Called before spawning.
//-----------------------------------------------------------------------------
bool CBaseProp::KeyValue( const char *szKeyName, const char *szValue )
{
if ( FStrEq(szKeyName, "health") )
{
// Only override props are allowed to override health.
if ( FClassnameIs( this, "prop_physics_override" ) || FClassnameIs( this, "prop_dynamic_override" ) )
return BaseClass::KeyValue( szKeyName, szValue );
return true;
}
else
{
return BaseClass::KeyValue( szKeyName, szValue );
}
return true;
}
//-----------------------------------------------------------------------------
// Purpose: Calculate whether this prop should block LOS or not
//-----------------------------------------------------------------------------
void CBaseProp::CalculateBlockLOS( void )
{
// We block LOS if:
// - One of our dimensions is >40
// - Our other 2 dimensions are >30
// By default, entities block LOS, so we only need to detect non-blockage
bool bFoundLarge = false;
Vector vecSize = CollisionProp()->OBBMaxs() - CollisionProp()->OBBMins();
for ( int i = 0; i < 3; i++ )
{
if ( vecSize[i] > 40 )
{
bFoundLarge = true;
}
if ( vecSize[i] > 30 )
continue;
// Dimension smaller than 30.
SetBlocksLOS( false );
return;
}
if ( !bFoundLarge )
{
// No dimension larger than 40
SetBlocksLOS( false );
}
}
//-----------------------------------------------------------------------------
// Purpose: Parse this prop's data from the model, if it has a keyvalues section.
// Returns true only if this prop is using a model that has a prop_data section that's invalid.
//-----------------------------------------------------------------------------
int CBaseProp::ParsePropData( void )
{
KeyValues *modelKeyValues = new KeyValues("");
if ( !modelKeyValues->LoadFromBuffer( modelinfo->GetModelName( GetModel() ), modelinfo->GetModelKeyValueText( GetModel() ) ) )
{
modelKeyValues->deleteThis();
return PARSE_FAILED_NO_DATA;
}
// Do we have a props section?
KeyValues *pkvPropData = modelKeyValues->FindKey("prop_data");
if ( !pkvPropData )
{
modelKeyValues->deleteThis();
return PARSE_FAILED_NO_DATA;
}
int iResult = g_PropDataSystem.ParsePropFromKV( this, pkvPropData, modelKeyValues );
modelKeyValues->deleteThis();
return iResult;
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CBaseProp::DrawDebugGeometryOverlays( void )
{
BaseClass::DrawDebugGeometryOverlays();
if ( m_debugOverlays & OVERLAY_PROP_DEBUG )
{
if ( m_takedamage == DAMAGE_NO )
{
NDebugOverlay::EntityBounds(this, 255, 0, 0, 0, 0 );
}
else if ( m_takedamage == DAMAGE_EVENTS_ONLY )
{
NDebugOverlay::EntityBounds(this, 255, 255, 255, 0, 0 );
}
else
{
// Remap health to green brightness
float flG = RemapVal( m_iHealth, 0, 100, 64, 255 );
flG = clamp( flG, 0.f, 255.f );
NDebugOverlay::EntityBounds(this, 0, flG, 0, 0, 0 );
}
}
}
class CEnableMotionFixup : public CBaseEntity
{
DECLARE_CLASS( CEnableMotionFixup, CBaseEntity );
};
LINK_ENTITY_TO_CLASS( point_enable_motion_fixup, CEnableMotionFixup );
static const char *s_pFadeScaleThink = "FadeScaleThink";
static const char *s_pPropAnimateThink = "PropAnimateThink";
void CBreakableProp::SetEnableMotionPosition( const Vector &position, const QAngle &angles )
{
ClearEnableMotionPosition();
CBaseEntity *pFixup = CBaseEntity::Create( "point_enable_motion_fixup", position, angles, this );
if ( pFixup )
{
pFixup->SetParent( this );
}
}
CBaseEntity *CBreakableProp::FindEnableMotionFixup()
{
CUtlVector<CBaseEntity*> list;
GetAllChildren( this, list );
for ( int i = list.Count()-1; i >= 0; --i )
{
if ( FClassnameIs( list[i], "point_enable_motion_fixup" ) )
return list[i];
}
return NULL;
}
bool CBreakableProp::GetEnableMotionPosition( Vector *pPosition, QAngle *pAngles )
{
CBaseEntity *pFixup = FindEnableMotionFixup();
if ( !pFixup )
return false;
*pPosition = pFixup->GetAbsOrigin();
*pAngles = pFixup->GetAbsAngles();
return true;
}
void CBreakableProp::ClearEnableMotionPosition()
{
CBaseEntity *pFixup = FindEnableMotionFixup();
if ( pFixup )
{
UnlinkFromParent( pFixup );
UTIL_Remove( pFixup );
}
}
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
void CBreakableProp::Ignite( float flFlameLifetime, bool bNPCOnly, float flSize, bool bCalledByLevelDesigner )
{
if( IsOnFire() )
return;
if( !HasInteraction( PROPINTER_FIRE_FLAMMABLE ) )
return;
BaseClass::Ignite( flFlameLifetime, bNPCOnly, flSize, bCalledByLevelDesigner );
if ( g_pGameRules->ShouldBurningPropsEmitLight() )
{
GetEffectEntity()->AddEffects( EF_DIMLIGHT );
}
// Frighten AIs, just in case this is an exploding thing.
CSoundEnt::InsertSound( SOUND_DANGER, GetAbsOrigin(), 128.0f, 1.0f, this, SOUNDENT_CHANNEL_REPEATED_DANGER );
}
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
void CBreakableProp::HandleFirstCollisionInteractions( int index, gamevcollisionevent_t *pEvent )
{
if ( pEvent->pEntities[ !index ]->IsWorld() )
{
if ( HasInteraction( PROPINTER_PHYSGUN_WORLD_STICK ) )
{
HandleInteractionStick( index, pEvent );
}
}
if( HasInteraction( PROPINTER_PHYSGUN_FIRST_BREAK ) )
{
// Looks like it's best to break by having the object damage itself.
CTakeDamageInfo info;
info.SetDamage( m_iHealth );
info.SetAttacker( this );
info.SetInflictor( this );
info.SetDamageType( DMG_GENERIC );
Vector vecPosition;
Vector vecVelocity;
VPhysicsGetObject()->GetVelocity( &vecVelocity, NULL );
VPhysicsGetObject()->GetPosition( &vecPosition, NULL );
info.SetDamageForce( vecVelocity );
info.SetDamagePosition( vecPosition );
TakeDamage( info );
return;
}
if( HasInteraction( PROPINTER_PHYSGUN_FIRST_PAINT ) )
{
IPhysicsObject *pObj = VPhysicsGetObject();
Vector vecPos;
pObj->GetPosition( &vecPos, NULL );
Vector vecVelocity = pEvent->preVelocity[0];
VectorNormalize(vecVelocity);
trace_t tr;
UTIL_TraceLine( vecPos, vecPos + (vecVelocity * 64), MASK_SHOT, this, COLLISION_GROUP_NONE, &tr );
if ( tr.m_pEnt )
{
#ifdef HL2_DLL
// Don't paintsplat friendlies
int iClassify = tr.m_pEnt->Classify();
if ( iClassify != CLASS_PLAYER_ALLY_VITAL && iClassify != CLASS_PLAYER_ALLY &&
iClassify != CLASS_CITIZEN_PASSIVE && iClassify != CLASS_CITIZEN_REBEL )
#endif
{
switch( entindex() % 3 )
{
case 0:
UTIL_DecalTrace( &tr, "PaintSplatBlue" );
break;
case 1:
UTIL_DecalTrace( &tr, "PaintSplatGreen" );
break;
case 2:
UTIL_DecalTrace( &tr, "PaintSplatPink" );
break;
}
}
}
}
if ( HasInteraction( PROPINTER_PHYSGUN_NOTIFY_CHILDREN ) )
{
CUtlVector<CBaseEntity *> children;
GetAllChildren( this, children );
for (int i = 0; i < children.Count(); i++ )
{
CBaseEntity *pent = children.Element( i );
IParentPropInteraction *pPropInter = dynamic_cast<IParentPropInteraction *>( pent );
if ( pPropInter )
{
pPropInter->OnParentCollisionInteraction( COLLISIONINTER_PARENT_FIRST_IMPACT, index, pEvent );
}
}
}
}
void CBreakableProp::CheckRemoveRagdolls()
{
if ( HasSpawnFlags( SF_PHYSPROP_HAS_ATTACHED_RAGDOLLS ) )
{
DetachAttachedRagdollsForEntity( this );
RemoveSpawnFlags( SF_PHYSPROP_HAS_ATTACHED_RAGDOLLS );
}
}
//-----------------------------------------------------------------------------
// Purpose: Handle special physgun interactions
// Input : index -
// *pEvent -
//-----------------------------------------------------------------------------
void CPhysicsProp::HandleAnyCollisionInteractions( int index, gamevcollisionevent_t *pEvent )
{
// If we're supposed to impale, and we've hit an NPC, impale it
if ( HasInteraction( PROPINTER_PHYSGUN_FIRST_IMPALE ) )
{
Vector vel = pEvent->preVelocity[index];
Vector forward;
QAngle angImpaleForward;
if ( GetPropDataAngles( "impale_forward", angImpaleForward ) )
{
Vector vecImpaleForward;
AngleVectors( angImpaleForward, &vecImpaleForward );
VectorRotate( vecImpaleForward, EntityToWorldTransform(), forward );
}
else
{
GetVectors( &forward, NULL, NULL );
}
float speed = DotProduct( forward, vel );
if ( speed < 1000.0f )
{
// not going to stick, so remove any ragdolls we've got
CheckRemoveRagdolls();
return;
}
CBaseEntity *pHitEntity = pEvent->pEntities[!index];
if ( pHitEntity->IsWorld() )
{
Vector normal;
float sign = index ? -1.0f : 1.0f;
pEvent->pInternalData->GetSurfaceNormal( normal );
float dot = DotProduct( forward, normal );
if ( (sign*dot) < DOT_45DEGREE )
return;
// Impale sticks to the wall if we hit end on
HandleInteractionStick( index, pEvent );
}
else if ( pHitEntity->MyNPCPointer() )
{
CAI_BaseNPC *pNPC = pHitEntity->MyNPCPointer();
IPhysicsObject *pObj = VPhysicsGetObject();
// do not impale NPCs if the impaler is friendly
CBasePlayer *pAttacker = HasPhysicsAttacker( 25.0f );
if (pAttacker && pNPC->IRelationType( pAttacker ) == D_LI)
{
return;
}
Vector vecPos;
pObj->GetPosition( &vecPos, NULL );
// Find the bone for the hitbox we hit
trace_t tr;
UTIL_TraceLine( vecPos, vecPos + pEvent->preVelocity[index] * 1.5, MASK_SHOT, this, COLLISION_GROUP_NONE, &tr );
Vector vecImpalePos = tr.endpos;
int iBone = -1;
if ( tr.hitbox )
{
Vector vecBonePos;
QAngle vecBoneAngles;
iBone = pNPC->GetHitboxBone( tr.hitbox );
pNPC->GetBonePosition( iBone, vecBonePos, vecBoneAngles );
Teleport( &vecBonePos, NULL, NULL );
vecImpalePos = vecBonePos;
}
// Kill the NPC and make an attached ragdoll
pEvent->pInternalData->GetContactPoint( vecImpalePos );
CBaseEntity *pRagdoll = CreateServerRagdollAttached( pNPC, vec3_origin, -1, COLLISION_GROUP_INTERACTIVE_DEBRIS, pObj, this, 0, vecImpalePos, iBone, vec3_origin );
if ( pRagdoll )
{
Vector vecVelocity = pEvent->preVelocity[index] * pObj->GetMass();
PhysCallbackImpulse( pObj, vecVelocity, vec3_origin );
UTIL_Remove( pNPC );
AddSpawnFlags( SF_PHYSPROP_HAS_ATTACHED_RAGDOLLS );
}
}
}
}
void CBreakableProp::StickAtPosition( const Vector &stickPosition, const Vector &savePosition, const QAngle &saveAngles )
{
if ( !VPhysicsGetObject()->IsMotionEnabled() )
return;
EmitSound("Metal.SawbladeStick");
Teleport( &stickPosition, NULL, NULL );
SetEnableMotionPosition( savePosition, saveAngles ); // this uses hierarchy, so it must be set after teleport
VPhysicsGetObject()->EnableMotion( false );
AddSpawnFlags( SF_PHYSPROP_ENABLE_ON_PHYSCANNON );
SetCollisionGroup( COLLISION_GROUP_DEBRIS );
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : index -
// *pEvent -
//-----------------------------------------------------------------------------
void CBreakableProp::HandleInteractionStick( int index, gamevcollisionevent_t *pEvent )
{
Vector vecDir = pEvent->preVelocity[ index ];
float speed = VectorNormalize( vecDir );
// Make sure the object is travelling fast enough to stick.
if( speed > 1000.0f )
{
Vector position;
QAngle angles;
VPhysicsGetObject()->GetPosition( &position, &angles );
Vector vecNormal;
pEvent->pInternalData->GetSurfaceNormal( vecNormal );
// we want the normal that points away from this object
if ( index == 1 )
{
vecNormal *= -1.0f;
}
float flDot = DotProduct( vecDir, vecNormal );
// Make sure the object isn't hitting the world at too sharp an angle.
if( flDot > 0.3 )
{
// Finally, inhibit sticking in metal, grates, sky, or anything else that doesn't make a sound.
const surfacedata_t *psurf = physprops->GetSurfaceData( pEvent->surfaceProps[!index] );
if (psurf->game.material != CHAR_TEX_METAL && psurf->game.material != CHAR_TEX_GRATE && psurf->game.material != 'X' )
{
Vector savePosition = position;
Vector vecEmbed = pEvent->preVelocity[ index ];
VectorNormalize( vecEmbed );
vecEmbed *= 8;
position += vecEmbed;
g_PostSimulationQueue.QueueCall( this, &CBreakableProp::StickAtPosition, position, savePosition, angles );
}
}
}
}
//-----------------------------------------------------------------------------
// Purpose: Turn on prop debugging mode
//-----------------------------------------------------------------------------
void CC_Prop_Debug( void )
{
// Toggle the prop debug bit on all props
for ( CBaseEntity *pEntity = gEntList.FirstEnt(); pEntity != NULL; pEntity = gEntList.NextEnt(pEntity) )
{
CBaseProp *pProp = dynamic_cast<CBaseProp*>(pEntity);
if ( pProp )
{
if ( pProp->m_debugOverlays & OVERLAY_PROP_DEBUG )
{
pProp->m_debugOverlays &= ~OVERLAY_PROP_DEBUG;
}
else
{
pProp->m_debugOverlays |= OVERLAY_PROP_DEBUG;
}
}
}
}
static ConCommand prop_debug("prop_debug", CC_Prop_Debug, "Toggle prop debug mode. If on, props will show colorcoded bounding boxes. Red means ignore all damage. White means respond physically to damage but never break. Green maps health in the range of 100 down to 1.", FCVAR_CHEAT);
//=============================================================================================================
// BREAKABLE PROPS
//=============================================================================================================
IMPLEMENT_SERVERCLASS_ST(CBreakableProp, DT_BreakableProp)
END_SEND_TABLE()
BEGIN_DATADESC( CBreakableProp )
DEFINE_KEYFIELD( m_explodeDamage, FIELD_FLOAT, "ExplodeDamage"),
DEFINE_KEYFIELD( m_explodeRadius, FIELD_FLOAT, "ExplodeRadius"),
DEFINE_KEYFIELD( m_iMinHealthDmg, FIELD_INTEGER, "minhealthdmg" ),
DEFINE_FIELD( m_createTick, FIELD_INTEGER ),
DEFINE_FIELD( m_hBreaker, FIELD_EHANDLE ),
DEFINE_KEYFIELD( m_PerformanceMode, FIELD_INTEGER, "PerformanceMode" ),
DEFINE_KEYFIELD( m_iszBreakModelMessage, FIELD_STRING, "BreakModelMessage" ),
DEFINE_FIELD( m_flDmgModBullet, FIELD_FLOAT ),
DEFINE_FIELD( m_flDmgModClub, FIELD_FLOAT ),
DEFINE_FIELD( m_flDmgModExplosive, FIELD_FLOAT ),
DEFINE_FIELD( m_iszPhysicsDamageTableName, FIELD_STRING ),
DEFINE_FIELD( m_iszBreakableModel, FIELD_STRING ),
DEFINE_FIELD( m_iBreakableSkin, FIELD_INTEGER ),
DEFINE_FIELD( m_iBreakableCount, FIELD_INTEGER ),
DEFINE_FIELD( m_iMaxBreakableSize, FIELD_INTEGER ),
DEFINE_FIELD( m_iszBasePropData, FIELD_STRING ),
DEFINE_FIELD( m_iInteractions, FIELD_INTEGER ),
DEFINE_FIELD( m_iNumBreakableChunks, FIELD_INTEGER ),
DEFINE_FIELD( m_nPhysgunState, FIELD_CHARACTER ),
DEFINE_KEYFIELD( m_iszPuntSound, FIELD_STRING, "puntsound" ),
DEFINE_KEYFIELD( m_flPressureDelay, FIELD_FLOAT, "PressureDelay" ),
DEFINE_FIELD( m_preferredCarryAngles, FIELD_VECTOR ),
DEFINE_FIELD( m_flDefaultFadeScale, FIELD_FLOAT ),
DEFINE_FIELD( m_bUsePuntSound, FIELD_BOOLEAN ),
// DEFINE_FIELD( m_mpBreakMode, mp_break_t ),
// Inputs
DEFINE_INPUTFUNC( FIELD_VOID, "Break", InputBreak ),
DEFINE_INPUTFUNC( FIELD_INTEGER, "SetHealth", InputSetHealth ),
DEFINE_INPUTFUNC( FIELD_INTEGER, "AddHealth", InputAddHealth ),
DEFINE_INPUTFUNC( FIELD_INTEGER, "RemoveHealth", InputRemoveHealth ),
DEFINE_INPUT( m_impactEnergyScale, FIELD_FLOAT, "physdamagescale" ),
DEFINE_INPUTFUNC( FIELD_VOID, "EnablePhyscannonPickup", InputEnablePhyscannonPickup ),
DEFINE_INPUTFUNC( FIELD_VOID, "DisablePhyscannonPickup", InputDisablePhyscannonPickup ),
DEFINE_INPUTFUNC( FIELD_VOID, "EnablePuntSound", InputEnablePuntSound ),
DEFINE_INPUTFUNC( FIELD_VOID, "DisablePuntSound", InputDisablePuntSound ),
// Outputs
DEFINE_OUTPUT( m_OnBreak, "OnBreak" ),
DEFINE_OUTPUT( m_OnHealthChanged, "OnHealthChanged" ),
DEFINE_OUTPUT( m_OnTakeDamage, "OnTakeDamage" ),
DEFINE_OUTPUT( m_OnPhysCannonDetach, "OnPhysCannonDetach" ),
DEFINE_OUTPUT( m_OnPhysCannonAnimatePreStarted, "OnPhysCannonAnimatePreStarted" ),
DEFINE_OUTPUT( m_OnPhysCannonAnimatePullStarted, "OnPhysCannonAnimatePullStarted" ),
DEFINE_OUTPUT( m_OnPhysCannonAnimatePostStarted, "OnPhysCannonAnimatePostStarted" ),
DEFINE_OUTPUT( m_OnPhysCannonPullAnimFinished, "OnPhysCannonPullAnimFinished" ),
// Function Pointers
DEFINE_THINKFUNC( BreakThink ),
DEFINE_THINKFUNC( AnimateThink ),
DEFINE_THINKFUNC( RampToDefaultFadeScale ),
DEFINE_ENTITYFUNC( BreakablePropTouch ),
// Physics Influence
DEFINE_FIELD( m_hPhysicsAttacker, FIELD_EHANDLE ),
DEFINE_FIELD( m_flLastPhysicsInfluenceTime, FIELD_TIME ),
DEFINE_FIELD( m_bOriginalBlockLOS, FIELD_BOOLEAN ),
DEFINE_FIELD( m_bBlockLOSSetByPropData, FIELD_BOOLEAN ),
DEFINE_FIELD( m_bIsWalkableSetByPropData, FIELD_BOOLEAN ),
// Damage
DEFINE_FIELD( m_hLastAttacker, FIELD_EHANDLE ),
DEFINE_FIELD( m_hFlareEnt, FIELD_EHANDLE ),
END_DATADESC()
//-----------------------------------------------------------------------------
// Constructor:
//-----------------------------------------------------------------------------
CBreakableProp::CBreakableProp()
{
m_fadeMinDist = -1;
m_fadeMaxDist = 0;
m_flFadeScale = 1;
m_flDefaultFadeScale = 1;
m_mpBreakMode = MULTIPLAYER_BREAK_DEFAULT;
// This defaults to on. Most times mapmakers won't specify a punt sound to play.
m_bUsePuntSound = true;
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CBreakableProp::Spawn()
{
// Starts out as the default fade scale value
m_flDefaultFadeScale = m_flFadeScale;
// Initialize damage modifiers. Must be done before baseclass spawn.
m_flDmgModBullet = 1.0;
m_flDmgModClub = 1.0;
m_flDmgModExplosive = 1.0;
//jmd: I am guessing that the call to Spawn will set any flags that should be set anyway; this
//clears flags we don't want (specifically the FL_ONFIRE for explosive barrels in HL2MP)]
#ifdef HL2MP
ClearFlags();
#endif
BaseClass::Spawn();
if ( IsMarkedForDeletion() )
return;
CStudioHdr *pStudioHdr = GetModelPtr( );
if ( pStudioHdr->flags() & STUDIOHDR_FLAGS_NO_FORCED_FADE )
{
DisableAutoFade();
}
else
{
m_flFadeScale = m_flDefaultFadeScale;
}
// If we have no custom breakable chunks, see if we're breaking into generic ones
if ( !m_iNumBreakableChunks )
{
IBreakableWithPropData *pBreakableInterface = assert_cast<IBreakableWithPropData*>(this);
if ( pBreakableInterface->GetBreakableModel() != NULL_STRING && pBreakableInterface->GetBreakableCount() )
{
m_iNumBreakableChunks = pBreakableInterface->GetBreakableCount();
}
}
// Setup takedamage based upon the health we parsed earlier, and our interactions
if ( ( m_iHealth == 0 ) ||
( !m_iNumBreakableChunks &&
!HasInteraction( PROPINTER_PHYSGUN_BREAK_EXPLODE ) &&
!HasInteraction( PROPINTER_PHYSGUN_FIRST_BREAK ) &&
!HasInteraction( PROPINTER_FIRE_FLAMMABLE ) &&
!HasInteraction( PROPINTER_FIRE_IGNITE_HALFHEALTH ) &&
!HasInteraction( PROPINTER_FIRE_EXPLOSIVE_RESIST ) ) )
{
m_iHealth = 0;
m_takedamage = DAMAGE_EVENTS_ONLY;
}
else
{
m_takedamage = DAMAGE_YES;
if( g_pGameRules->GetAutoAimMode() == AUTOAIM_ON_CONSOLE )
{
if ( HasInteraction( PROPINTER_PHYSGUN_BREAK_EXPLODE ) ||
HasInteraction( PROPINTER_FIRE_IGNITE_HALFHEALTH ) )
{
// Exploding barrels, exploding gas cans
AddFlag( FL_AIMTARGET );
}
}
}
m_iMaxHealth = ( m_iHealth > 0 ) ? m_iHealth : 1;
m_createTick = gpGlobals->tickcount;
if ( m_impactEnergyScale == 0 )
{
m_impactEnergyScale = 0.1f;
}
m_preferredCarryAngles = QAngle( -5, 0, 0 );
// The presence of this activity causes us to have to detach it before it can be grabbed.
if ( SelectWeightedSequence( ACT_PHYSCANNON_ANIMATE ) != ACTIVITY_NOT_AVAILABLE )
{
m_nPhysgunState = PHYSGUN_ANIMATE_ON_PULL;
}
else if ( SelectWeightedSequence( ACT_PHYSCANNON_DETACH ) != ACTIVITY_NOT_AVAILABLE )
{
m_nPhysgunState = PHYSGUN_MUST_BE_DETACHED;
}
else
{
m_nPhysgunState = PHYSGUN_CAN_BE_GRABBED;
}
m_hLastAttacker = NULL;
m_hBreaker = NULL;
SetTouch( &CBreakableProp::BreakablePropTouch );
}
//-----------------------------------------------------------------------------
// Disable auto fading under dx7 or when level fades are specified
//-----------------------------------------------------------------------------
void CBreakableProp::DisableAutoFade()
{
m_flFadeScale = 0;
m_flDefaultFadeScale = 0;
}
//-----------------------------------------------------------------------------
// Copy fade from another breakable.
//-----------------------------------------------------------------------------
void CBreakableProp::CopyFadeFrom( CBreakableProp *pSource )
{
m_flDefaultFadeScale = pSource->m_flDefaultFadeScale;
m_flFadeScale = pSource->m_flFadeScale;
if ( m_flFadeScale != m_flDefaultFadeScale )
{
float flNextThink = pSource->GetNextThink( s_pFadeScaleThink );
if ( flNextThink < gpGlobals->curtime + TICK_INTERVAL )
{
flNextThink = gpGlobals->curtime + TICK_INTERVAL;
}
SetContextThink( &CBreakableProp::RampToDefaultFadeScale, flNextThink, s_pFadeScaleThink );
}
}
//-----------------------------------------------------------------------------
// Make physcannonable, or not
//-----------------------------------------------------------------------------
void CBreakableProp::InputEnablePhyscannonPickup( inputdata_t &inputdata )
{
RemoveEFlags( EFL_NO_PHYSCANNON_INTERACTION );
}
void CBreakableProp::InputDisablePhyscannonPickup( inputdata_t &inputdata )
{
AddEFlags( EFL_NO_PHYSCANNON_INTERACTION );
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : *pOther -
//-----------------------------------------------------------------------------
void CBreakableProp::BreakablePropTouch( CBaseEntity *pOther )
{
if ( HasSpawnFlags( SF_PHYSPROP_TOUCH ) )
{
// can be broken when run into
float flDamage = pOther->GetSmoothedVelocity().Length() * 0.01;
if ( flDamage >= m_iHealth )
{
// Make sure we can take damage
m_takedamage = DAMAGE_YES;
OnTakeDamage( CTakeDamageInfo( pOther, pOther, flDamage, DMG_CRUSH ) );
// do a little damage to player if we broke glass or computer
CTakeDamageInfo info( pOther, pOther, flDamage/4, DMG_SLASH );
CalculateMeleeDamageForce( &info, (pOther->GetAbsOrigin() - GetAbsOrigin()), GetAbsOrigin() );
pOther->TakeDamage( info );
}
}
if ( HasSpawnFlags( SF_PHYSPROP_PRESSURE ) && pOther->GetGroundEntity() == this )
{
// can be broken when stood upon
// play creaking sound here.
// DamageSound();
m_hBreaker = pOther;