-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathtriggers.cpp
More file actions
5314 lines (4323 loc) · 146 KB
/
Copy pathtriggers.cpp
File metadata and controls
5314 lines (4323 loc) · 146 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
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: Spawn and use functions for editor-placed triggers.
//
//===========================================================================//
#include "cbase.h"
#include "ai_basenpc.h"
#include "player.h"
#include "saverestore.h"
#include "gamerules.h"
#include "entityapi.h"
#include "entitylist.h"
#include "ndebugoverlay.h"
#include "globalstate.h"
#include "filters.h"
#include "vstdlib/random.h"
#include "triggers.h"
#include "saverestoretypes.h"
#include "hierarchy.h"
#include "bspfile.h"
#include "saverestore_utlvector.h"
#include "physics_saverestore.h"
#include "te_effect_dispatch.h"
#include "ammodef.h"
#include "iservervehicle.h"
#include "movevars_shared.h"
#include "physics_prop_ragdoll.h"
#include "props.h"
#include "RagdollBoogie.h"
#include "EntityParticleTrail.h"
#include "in_buttons.h"
#include "ai_behavior_follow.h"
#include "ai_behavior_lead.h"
#include "gameinterface.h"
#include "fmtstr.h"
// reactivedrop: #iss-trigger-bots
#include "asw_shareddefs.h"
#include "asw_marine.h"
#ifdef HL2_DLL
#include "hl2_player.h"
#endif
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
#define DEBUG_TRANSITIONS_VERBOSE 2
ConVar g_debug_transitions( "g_debug_transitions", "0", FCVAR_NONE, "Set to 1 and restart the map to be warned if the map has no trigger_transition volumes. Set to 2 to see a dump of all entities & associated results during a transition." );
// Global list of triggers that care about weapon fire
// Doesn't need saving, the triggers re-add themselves on restore.
CUtlVector< CHandle<CTriggerMultiple> > g_hWeaponFireTriggers;
extern CServerGameDLL g_ServerGameDLL;
extern bool g_fGameOver;
ConVar showtriggers( "showtriggers", "0", FCVAR_CHEAT, "Shows trigger brushes" );
bool IsTriggerClass( CBaseEntity *pEntity );
// Command to dynamically toggle trigger visibility
void Cmd_ShowtriggersToggle_f( const CCommand &args )
{
// Loop through the entities in the game and make visible anything derived from CBaseTrigger
CBaseEntity *pEntity = gEntList.FirstEnt();
while ( pEntity )
{
if ( IsTriggerClass(pEntity) )
{
// If a classname is specified, only show triggers of that type
if ( args.ArgC() > 1 )
{
const char *sClassname = args[1];
if ( sClassname && sClassname[0] )
{
if ( !FClassnameIs( pEntity, sClassname ) )
{
pEntity = gEntList.NextEnt( pEntity );
continue;
}
}
}
if ( pEntity->IsEffectActive( EF_NODRAW ) )
{
pEntity->RemoveEffects( EF_NODRAW );
}
else
{
pEntity->AddEffects( EF_NODRAW );
}
}
pEntity = gEntList.NextEnt( pEntity );
}
}
static ConCommand showtriggers_toggle( "showtriggers_toggle", Cmd_ShowtriggersToggle_f, "Toggle show triggers", FCVAR_CHEAT );
// Global Savedata for base trigger
BEGIN_DATADESC( CBaseTrigger )
// Keyfields
DEFINE_KEYFIELD( m_iFilterName, FIELD_STRING, "filtername" ),
DEFINE_FIELD( m_hFilter, FIELD_EHANDLE ),
DEFINE_KEYFIELD( m_bDisabled, FIELD_BOOLEAN, "StartDisabled" ),
DEFINE_UTLVECTOR( m_hTouchingEntities, FIELD_EHANDLE ),
// Inputs
DEFINE_INPUTFUNC( FIELD_VOID, "Enable", InputEnable ),
DEFINE_INPUTFUNC( FIELD_VOID, "Disable", InputDisable ),
DEFINE_INPUTFUNC( FIELD_VOID, "Toggle", InputToggle ),
DEFINE_INPUTFUNC( FIELD_VOID, "TouchTest", InputTouchTest ),
DEFINE_INPUTFUNC( FIELD_VOID, "StartTouch", InputStartTouch ),
DEFINE_INPUTFUNC( FIELD_VOID, "EndTouch", InputEndTouch ),
// Outputs
DEFINE_OUTPUT( m_OnStartTouch, "OnStartTouch"),
DEFINE_OUTPUT( m_OnStartTouchAll, "OnStartTouchAll"),
DEFINE_OUTPUT( m_OnEndTouch, "OnEndTouch"),
DEFINE_OUTPUT( m_OnEndTouchAll, "OnEndTouchAll"),
DEFINE_OUTPUT( m_OnTouching, "OnTouching" ),
DEFINE_OUTPUT( m_OnNotTouching, "OnNotTouching" ),
END_DATADESC()
LINK_ENTITY_TO_CLASS( trigger, CBaseTrigger );
IMPLEMENT_SERVERCLASS_ST( CBaseTrigger, DT_BaseTrigger )
SendPropBool( SENDINFO( m_bClientSidePredicted ) ),
SendPropInt( SENDINFO(m_spawnflags), -1, SPROP_NOSCALE )
END_SEND_TABLE()
BEGIN_ENT_SCRIPTDESC( CBaseTrigger, CBaseEntity, "Server-side trigger" )
DEFINE_SCRIPTFUNC( Disable, "Disable the trigger" )
DEFINE_SCRIPTFUNC( Enable, "Enable the trigger" )
DEFINE_SCRIPTFUNC_NAMED( ScriptIsTouching, "IsTouching", "Checks whether the passed entity is touching the trigger." )
DEFINE_SCRIPTFUNC( GetNumTouching, "Gets the number of entities currently touching the trigger." )
DEFINE_SCRIPTFUNC_NAMED( ScriptGetTouching, "GetTouching", "Gets the i'th entity currently touching the trigger." )
END_SCRIPTDESC();
CBaseTrigger::CBaseTrigger()
{
AddEFlags( EFL_USE_PARTITION_WHEN_NOT_SOLID );
m_bClientSidePredicted = false;
}
//------------------------------------------------------------------------------
// Purpose: Input handler to turn on this trigger.
//------------------------------------------------------------------------------
void CBaseTrigger::InputEnable( inputdata_t &inputdata )
{
Enable();
}
//------------------------------------------------------------------------------
// Purpose: Input handler to turn off this trigger.
//------------------------------------------------------------------------------
void CBaseTrigger::InputDisable( inputdata_t &inputdata )
{
Disable();
}
void CBaseTrigger::InputTouchTest( inputdata_t &inputdata )
{
TouchTest();
}
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
void CBaseTrigger::Spawn()
{
// reactivedrop: #iss-trigger-bots add SF_TRIGGER_ONLY_BOTS_MARINES
if ( HasSpawnFlags( SF_TRIGGER_ONLY_PLAYER_ALLY_NPCS ) || HasSpawnFlags( SF_TRIGGER_ONLY_NPCS_IN_VEHICLES ) || HasSpawnFlags( SF_TRIGGER_ONLY_BOTS_MARINES ) )
{
// Automatically set this trigger to work with NPC's.
AddSpawnFlags( SF_TRIGGER_ALLOW_NPCS );
}
if ( HasSpawnFlags( SF_TRIGGER_ONLY_CLIENTS_IN_VEHICLES ) )
{
AddSpawnFlags( SF_TRIGGER_ALLOW_CLIENTS );
}
if ( HasSpawnFlags( SF_TRIGGER_ONLY_CLIENTS_OUT_OF_VEHICLES ) )
{
AddSpawnFlags( SF_TRIGGER_ALLOW_CLIENTS );
}
BaseClass::Spawn();
}
//------------------------------------------------------------------------------
// Cleanup
//------------------------------------------------------------------------------
void CBaseTrigger::UpdateOnRemove( void )
{
if ( VPhysicsGetObject())
{
VPhysicsGetObject()->RemoveTrigger();
}
BaseClass::UpdateOnRemove();
}
//------------------------------------------------------------------------------
// Purpose: Turns on this trigger.
//------------------------------------------------------------------------------
void CBaseTrigger::Enable( void )
{
m_bDisabled = false;
if ( VPhysicsGetObject())
{
VPhysicsGetObject()->EnableCollisions( true );
}
if (!IsSolidFlagSet( FSOLID_TRIGGER ))
{
AddSolidFlags( FSOLID_TRIGGER );
PhysicsTouchTriggers();
}
}
//------------------------------------------------------------------------------
// Purpose :
//------------------------------------------------------------------------------
void CBaseTrigger::Activate( void )
{
// Get a handle to my filter entity if there is one
if (m_iFilterName != NULL_STRING)
{
m_hFilter = dynamic_cast<CBaseFilter *>(gEntList.FindEntityByName( NULL, m_iFilterName ));
}
BaseClass::Activate();
}
//-----------------------------------------------------------------------------
// Purpose: Called after player becomes active in the game
//-----------------------------------------------------------------------------
void CBaseTrigger::PostClientActive( void )
{
BaseClass::PostClientActive();
if ( !m_bDisabled )
{
PhysicsTouchTriggers();
}
}
//------------------------------------------------------------------------------
// Purpose: Turns off this trigger.
//------------------------------------------------------------------------------
void CBaseTrigger::Disable( void )
{
m_bDisabled = true;
if ( VPhysicsGetObject())
{
VPhysicsGetObject()->EnableCollisions( false );
}
if (IsSolidFlagSet(FSOLID_TRIGGER))
{
RemoveSolidFlags( FSOLID_TRIGGER );
PhysicsTouchTriggers();
}
}
//------------------------------------------------------------------------------
// Purpose: Tests to see if anything is touching this trigger.
//------------------------------------------------------------------------------
void CBaseTrigger::TouchTest( void )
{
// If the trigger is disabled don't test to see if anything is touching it.
if ( !m_bDisabled )
{
if ( m_hTouchingEntities.Count() !=0 )
{
m_OnTouching.FireOutput( this, this );
}
else
{
m_OnNotTouching.FireOutput( this, this );
}
}
}
//-----------------------------------------------------------------------------
// Purpose: Draw any debug text overlays
// Output : Current text offset from the top
//-----------------------------------------------------------------------------
int CBaseTrigger::DrawDebugTextOverlays(void)
{
int text_offset = BaseClass::DrawDebugTextOverlays();
if (m_debugOverlays & OVERLAY_TEXT_BIT)
{
// --------------
// Print Target
// --------------
char tempstr[255];
if (IsSolidFlagSet(FSOLID_TRIGGER))
{
Q_strncpy(tempstr,"State: Enabled",sizeof(tempstr));
}
else
{
Q_strncpy(tempstr,"State: Disabled",sizeof(tempstr));
}
EntityText(text_offset,tempstr,0);
text_offset++;
}
return text_offset;
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CBaseTrigger::InitTrigger( )
{
SetSolid( GetParent() ? SOLID_VPHYSICS : SOLID_BSP );
AddSolidFlags( FSOLID_NOT_SOLID );
if (m_bDisabled)
{
RemoveSolidFlags( FSOLID_TRIGGER );
}
else
{
AddSolidFlags( FSOLID_TRIGGER );
}
SetMoveType( MOVETYPE_NONE );
SetModel( STRING( GetModelName() ) ); // set size and link into world
if ( showtriggers.GetInt() == 0 )
{
AddEffects( EF_NODRAW );
}
m_hTouchingEntities.Purge();
if ( HasSpawnFlags( SF_TRIG_TOUCH_DEBRIS ) )
{
CollisionProp()->AddSolidFlags( FSOLID_TRIGGER_TOUCH_DEBRIS );
}
}
//-----------------------------------------------------------------------------
// Purpose: Returns true if this entity passes the filter criteria, false if not.
// Input : pOther - The entity to be filtered.
//-----------------------------------------------------------------------------
bool CBaseTrigger::PassesTriggerFilters(CBaseEntity *pOther)
{
// First test spawn flag filters
if ( HasSpawnFlags(SF_TRIGGER_ALLOW_ALL) ||
(HasSpawnFlags(SF_TRIGGER_ALLOW_CLIENTS) && (pOther->GetFlags() & FL_CLIENT)) ||
(HasSpawnFlags(SF_TRIGGER_ALLOW_NPCS) && (pOther->GetFlags() & FL_NPC)) ||
(HasSpawnFlags(SF_TRIGGER_ALLOW_PUSHABLES) && FClassnameIs(pOther, "func_pushable")) ||
(HasSpawnFlags(SF_TRIGGER_ALLOW_PHYSICS) && pOther->GetMoveType() == MOVETYPE_VPHYSICS)
#if defined( HL2_EPISODIC )
||
( HasSpawnFlags(SF_TRIG_TOUCH_DEBRIS) &&
(pOther->GetCollisionGroup() == COLLISION_GROUP_DEBRIS ||
pOther->GetCollisionGroup() == COLLISION_GROUP_DEBRIS_TRIGGER ||
pOther->GetCollisionGroup() == COLLISION_GROUP_INTERACTIVE_DEBRIS)
)
#endif
)
{
if ( pOther->GetFlags() & FL_NPC )
{
CAI_BaseNPC *pNPC = pOther->MyNPCPointer();
if ( HasSpawnFlags( SF_TRIGGER_ONLY_PLAYER_ALLY_NPCS ) )
{
if ( !pNPC || !pNPC->IsPlayerAlly() )
{
return false;
}
}
if ( HasSpawnFlags( SF_TRIGGER_ONLY_NPCS_IN_VEHICLES ) )
{
if ( !pNPC || !pNPC->IsInAVehicle() )
return false;
}
// reactivedrop: #iss-trigger-bots
if ( HasSpawnFlags( SF_TRIGGER_ONLY_BOTS_MARINES ) )
{
if ( !pNPC || pNPC->Classify() != CLASS_ASW_MARINE )
return false;
if ( static_cast<CASW_Marine*>(pNPC)->IsInhabited() )
return false;
}
}
bool bOtherIsPlayer = pOther->IsPlayer();
if ( HasSpawnFlags(SF_TRIGGER_ONLY_CLIENTS_IN_VEHICLES) && bOtherIsPlayer )
{
if ( !((CBasePlayer*)pOther)->IsInAVehicle() )
return false;
// Make sure we're also not exiting the vehicle at the moment
IServerVehicle *pVehicleServer = ((CBasePlayer*)pOther)->GetVehicle();
if ( pVehicleServer == NULL )
return false;
if ( pVehicleServer->IsPassengerExiting() )
return false;
}
if ( HasSpawnFlags(SF_TRIGGER_ONLY_CLIENTS_OUT_OF_VEHICLES) && bOtherIsPlayer )
{
if ( ((CBasePlayer*)pOther)->IsInAVehicle() )
return false;
}
CBaseFilter *pFilter = m_hFilter.Get();
return (!pFilter) ? true : pFilter->PassesFilter( this, pOther );
}
return false;
}
//-----------------------------------------------------------------------------
// Purpose: Called to simulate what happens when an entity touches the trigger.
// Input : pOther - The entity that is touching us.
//-----------------------------------------------------------------------------
void CBaseTrigger::InputStartTouch( inputdata_t &inputdata )
{
//Pretend we just touched the trigger.
StartTouch( inputdata.pCaller );
}
//-----------------------------------------------------------------------------
// Purpose: Called to simulate what happens when an entity leaves the trigger.
// Input : pOther - The entity that is touching us.
//-----------------------------------------------------------------------------
void CBaseTrigger::InputEndTouch( inputdata_t &inputdata )
{
//And... pretend we left the trigger.
EndTouch( inputdata.pCaller );
}
//-----------------------------------------------------------------------------
// Purpose: Called when the first entity that passes filters starts touching us.
// Input : pOther - The entity that is touching us.
//-----------------------------------------------------------------------------
void CBaseTrigger::OnStartTouchAll(CBaseEntity *pOther)
{
m_OnStartTouchAll.FireOutput( pOther, this );
}
//-----------------------------------------------------------------------------
// Purpose: Called when the last entity stops touching us
// Input : pOther - The entity that is touching us.
//-----------------------------------------------------------------------------
void CBaseTrigger::OnEndTouchAll(CBaseEntity *pOther)
{
m_OnEndTouchAll.FireOutput(pOther, this);
}
//-----------------------------------------------------------------------------
// Purpose: Called when an entity starts touching us.
// Input : pOther - The entity that is touching us.
//-----------------------------------------------------------------------------
void CBaseTrigger::StartTouch(CBaseEntity *pOther)
{
if (PassesTriggerFilters(pOther) )
{
EHANDLE hOther;
hOther = pOther;
bool bAdded = false;
if ( m_hTouchingEntities.Find( hOther ) == m_hTouchingEntities.InvalidIndex() )
{
m_hTouchingEntities.AddToTail( hOther );
bAdded = true;
}
m_OnStartTouch.FireOutput(pOther, this);
if ( bAdded && ( m_hTouchingEntities.Count() == 1 ) )
{
// First entity to touch us that passes our filters
OnStartTouchAll( pOther );
}
}
}
//-----------------------------------------------------------------------------
// Purpose: Called when an entity stops touching us.
// Input : pOther - The entity that was touching us.
//-----------------------------------------------------------------------------
void CBaseTrigger::EndTouch(CBaseEntity *pOther)
{
if ( IsTouching( pOther ) )
{
EHANDLE hOther;
hOther = pOther;
m_hTouchingEntities.FindAndRemove( hOther );
//FIXME: Without this, triggers fire their EndTouch outputs when they are disabled!
//if ( !m_bDisabled )
//{
m_OnEndTouch.FireOutput(pOther, this);
//}
// If there are no more entities touching this trigger, fire the lost all touches
// Loop through the touching entities backwards. Clean out old ones, and look for existing
bool bFoundOtherTouchee = false;
int iSize = m_hTouchingEntities.Count();
for ( int i = iSize-1; i >= 0; i-- )
{
hOther = m_hTouchingEntities[i];
if ( !hOther )
{
m_hTouchingEntities.Remove( i );
}
else
{
bFoundOtherTouchee = true;
}
}
//FIXME: Without this, triggers fire their EndTouch outputs when they are disabled!
// Didn't find one?
if ( !bFoundOtherTouchee /*&& !m_bDisabled*/ )
{
OnEndTouchAll( pOther );
}
}
}
//-----------------------------------------------------------------------------
// Purpose: Return true if the specified entity is touching us
//-----------------------------------------------------------------------------
bool CBaseTrigger::IsTouching( CBaseEntity *pOther )
{
EHANDLE hOther;
hOther = pOther;
return ( m_hTouchingEntities.Find( hOther ) != m_hTouchingEntities.InvalidIndex() );
}
bool CBaseTrigger::ScriptIsTouching( HSCRIPT entity )
{
CBaseEntity *pOther = ToEnt(entity);
if ( !pOther )
return false;
return IsTouching( pOther );
}
int CBaseTrigger::GetNumTouching()
{
return m_hTouchingEntities.Count();
}
HSCRIPT CBaseTrigger::ScriptGetTouching( int i )
{
if ( i < 0 || i >= m_hTouchingEntities.Count() )
return NULL;
return ToHScript( m_hTouchingEntities[i] );
}
//-----------------------------------------------------------------------------
// Purpose: Return a pointer to the first entity of the specified type being touched by this trigger
//-----------------------------------------------------------------------------
CBaseEntity *CBaseTrigger::GetTouchedEntityOfType( const char *sClassName )
{
int iCount = m_hTouchingEntities.Count();
for ( int i = 0; i < iCount; i++ )
{
CBaseEntity *pEntity = m_hTouchingEntities[i];
if ( FClassnameIs( pEntity, sClassName ) )
return pEntity;
}
return NULL;
}
//-----------------------------------------------------------------------------
// Purpose: Toggles this trigger between enabled and disabled.
//-----------------------------------------------------------------------------
void CBaseTrigger::InputToggle( inputdata_t &inputdata )
{
if (IsSolidFlagSet( FSOLID_TRIGGER ))
{
RemoveSolidFlags(FSOLID_TRIGGER);
}
else
{
AddSolidFlags(FSOLID_TRIGGER);
}
PhysicsTouchTriggers();
}
//-----------------------------------------------------------------------------
// Purpose: Removes anything that touches it. If the trigger has a targetname,
// firing it will toggle state.
//-----------------------------------------------------------------------------
class CTriggerRemove : public CBaseTrigger
{
public:
DECLARE_CLASS( CTriggerRemove, CBaseTrigger );
void Spawn( void );
void Touch( CBaseEntity *pOther );
DECLARE_DATADESC();
// Outputs
COutputEvent m_OnRemove;
};
BEGIN_DATADESC( CTriggerRemove )
// Outputs
DEFINE_OUTPUT( m_OnRemove, "OnRemove" ),
END_DATADESC()
LINK_ENTITY_TO_CLASS( trigger_remove, CTriggerRemove );
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CTriggerRemove::Spawn( void )
{
BaseClass::Spawn();
InitTrigger();
}
//-----------------------------------------------------------------------------
// Purpose: Trigger hurt that causes radiation will do a radius check and set
// the player's geiger counter level according to distance from center
// of trigger.
//-----------------------------------------------------------------------------
void CTriggerRemove::Touch( CBaseEntity *pOther )
{
if (!PassesTriggerFilters(pOther))
return;
UTIL_Remove( pOther );
}
BEGIN_DATADESC( CTriggerHurt )
// Function Pointers
DEFINE_FUNCTION( RadiationThink ),
DEFINE_FUNCTION( HurtThink ),
DEFINE_FUNCTION( NavThink ),
// Fields
DEFINE_FIELD( m_flOriginalDamage, FIELD_FLOAT ),
DEFINE_KEYFIELD( m_flDamage, FIELD_FLOAT, "damage" ),
DEFINE_KEYFIELD( m_flDamageCap, FIELD_FLOAT, "damagecap" ),
DEFINE_KEYFIELD( m_bitsDamageInflict, FIELD_INTEGER, "damagetype" ),
DEFINE_KEYFIELD( m_damageModel, FIELD_INTEGER, "damagemodel" ),
DEFINE_KEYFIELD( m_bNoDmgForce, FIELD_BOOLEAN, "nodmgforce" ),
DEFINE_FIELD( m_flLastDmgTime, FIELD_TIME ),
DEFINE_FIELD( m_flDmgResetTime, FIELD_TIME ),
DEFINE_UTLVECTOR( m_hurtEntities, FIELD_EHANDLE ),
// Inputs
DEFINE_INPUT( m_flDamage, FIELD_FLOAT, "SetDamage" ),
// Outputs
DEFINE_OUTPUT( m_OnHurt, "OnHurt" ),
DEFINE_OUTPUT( m_OnHurtPlayer, "OnHurtPlayer" ),
END_DATADESC()
LINK_ENTITY_TO_CLASS( trigger_hurt, CTriggerHurt );
//-----------------------------------------------------------------------------
// Purpose: Called when spawning, after keyvalues have been handled.
//-----------------------------------------------------------------------------
void CTriggerHurt::Spawn( void )
{
BaseClass::Spawn();
InitTrigger();
m_flOriginalDamage = m_flDamage;
SetNextThink( TICK_NEVER_THINK );
SetThink( NULL );
if (m_bitsDamageInflict & DMG_RADIATION)
{
SetThink ( &CTriggerHurt::RadiationThink );
SetNextThink( gpGlobals->curtime + random->RandomFloat(0.0, 0.5) );
}
if ( TheNavMesh )
{
SetContextThink( &CTriggerHurt::NavThink, gpGlobals->curtime, "NavContext" );
}
}
//-----------------------------------------------------------------------------
// Purpose: Marks nav areas as damaging.
//-----------------------------------------------------------------------------
void CTriggerHurt::NavThink( void )
{
const float deltaT = 5.0f;
SetContextThink( &CTriggerHurt::NavThink, gpGlobals->curtime + deltaT, "NavContext" );
if ( !TheNavMesh->IsLoaded() )
return;
if ( m_bDisabled )
return;
// mark overlapping nav areas as "damaging"
NavAreaCollector overlap;
Extent extent;
CollisionProp()->WorldSpaceAABB( &extent.lo, &extent.hi );
extent.lo.z -= HumanHeight;
if ( m_bitsDamageInflict & DMG_BURN )
{
const float DangerBloat = HalfHumanWidth;
Vector dangerBloat( DangerBloat, DangerBloat, 0 );
extent.lo -= dangerBloat;
extent.hi += dangerBloat;
}
TheNavMesh->ForAllAreasOverlappingExtent( overlap, extent );
FOR_EACH_VEC( overlap.m_area, it )
{
CNavArea *area = overlap.m_area[ it ];
area->MarkAsDamaging( deltaT + 1.0f );
}
}
int CTriggerHurt::UpdateTransmitState()
{
// HACK! Cull the worm trigger against the PVS rather than hiding it due to EF_NODRAW
if ( !V_stricmp( STRING( gpGlobals->mapname ), "rd-reduction2" ) && !V_stricmp( GetEntityNameAsCStr(), "trigger_pitworm_hitbox" ) )
return SetTransmitState( FL_EDICT_PVSCHECK );
return BaseClass::UpdateTransmitState();
}
//-----------------------------------------------------------------------------
// Purpose: Trigger hurt that causes radiation will do a radius check and set
// the player's geiger counter level according to distance from center
// of trigger.
//-----------------------------------------------------------------------------
void CTriggerHurt::RadiationThink( void )
{
// check to see if a player is in pvs
// if not, continue
Vector vecSurroundMins, vecSurroundMaxs;
CollisionProp()->WorldSpaceSurroundingBounds( &vecSurroundMins, &vecSurroundMaxs );
CBasePlayer *pPlayer = static_cast<CBasePlayer *>(UTIL_FindClientInPVS( vecSurroundMins, vecSurroundMaxs ));
if (pPlayer)
{
// get range to player;
float flRange = CollisionProp()->CalcDistanceFromPoint( pPlayer->WorldSpaceCenter() );
flRange *= 3.0f;
pPlayer->NotifyNearbyRadiationSource(flRange);
}
float dt = gpGlobals->curtime - m_flLastDmgTime;
if ( dt >= 0.5 )
{
HurtAllTouchers( dt );
}
SetNextThink( gpGlobals->curtime + 0.25 );
}
//-----------------------------------------------------------------------------
// Purpose: When touched, a hurt trigger does m_flDamage points of damage each half-second.
// Input : pOther - The entity that is touching us.
//-----------------------------------------------------------------------------
bool CTriggerHurt::HurtEntity( CBaseEntity *pOther, float damage )
{
if ( !pOther->m_takedamage || !PassesTriggerFilters(pOther) )
return false;
if ( damage < 0 )
{
pOther->TakeHealth( -damage, m_bitsDamageInflict );
}
else
{
// The damage position is the nearest point on the damaged entity
// to the trigger's center. Not perfect, but better than nothing.
Vector vecCenter = CollisionProp()->WorldSpaceCenter();
Vector vecDamagePos;
pOther->CollisionProp()->CalcNearestPoint( vecCenter, &vecDamagePos );
CTakeDamageInfo info( this, this, damage, m_bitsDamageInflict );
info.SetDamagePosition( vecDamagePos );
if ( !m_bNoDmgForce )
{
GuessDamageForce( &info, ( vecDamagePos - vecCenter ), vecDamagePos );
}
else
{
info.SetDamageForce( vec3_origin );
}
pOther->TakeDamage( info );
}
if (pOther->IsPlayer())
{
m_OnHurtPlayer.FireOutput(pOther, this);
}
else
{
m_OnHurt.FireOutput(pOther, this);
}
m_hurtEntities.AddToTail( EHANDLE(pOther) );
//NDebugOverlay::Box( pOther->GetAbsOrigin(), pOther->WorldAlignMins(), pOther->WorldAlignMaxs(), 255,0,0,0,0.5 );
return true;
}
void CTriggerHurt::HurtThink()
{
// if I hurt anyone, think again
if ( HurtAllTouchers( 0.5 ) <= 0 )
{
SetThink(NULL);
}
else
{
SetNextThink( gpGlobals->curtime + 0.5f );
}
}
void CTriggerHurt::EndTouch( CBaseEntity *pOther )
{
if (PassesTriggerFilters(pOther))
{
EHANDLE hOther;
hOther = pOther;
// if this guy has never taken damage, hurt him now
if ( !m_hurtEntities.HasElement( hOther ) )
{
HurtEntity( pOther, m_flDamage * 0.5 );
}
}
BaseClass::EndTouch( pOther );
}
//-----------------------------------------------------------------------------
// Purpose: called from RadiationThink() as well as HurtThink()
// This function applies damage to any entities currently touching the
// trigger
// Input : dt - time since last call
// Output : int - number of entities actually hurt
//-----------------------------------------------------------------------------
#define TRIGGER_HURT_FORGIVE_TIME 3.0f // time in seconds
int CTriggerHurt::HurtAllTouchers( float dt )
{
int hurtCount = 0;
// half second worth of damage
float fldmg = m_flDamage * dt;
m_flLastDmgTime = gpGlobals->curtime;
m_hurtEntities.RemoveAll();
touchlink_t *root = ( touchlink_t * )GetDataObject( TOUCHLINK );
if ( root )
{
for ( touchlink_t *link = root->nextLink; link != root; link = link->nextLink )
{
CBaseEntity *pTouch = link->entityTouched;
if ( pTouch )
{
if ( HurtEntity( pTouch, fldmg ) )
{
hurtCount++;
}
}
}
}
if( m_damageModel == DAMAGEMODEL_DOUBLE_FORGIVENESS )
{
if( hurtCount == 0 )
{
if( gpGlobals->curtime > m_flDmgResetTime )
{
// Didn't hurt anyone. Reset the damage if it's time. (hence, the forgiveness)
m_flDamage = m_flOriginalDamage;
}
}
else
{
// Hurt someone! double the damage
m_flDamage *= 2.0f;
if( m_flDamage > m_flDamageCap )
{
// Clamp
m_flDamage = m_flDamageCap;
}
// Now, put the damage reset time into the future. The forgive time is how long the trigger
// must go without harming anyone in order that its accumulated damage be reset to the amount
// set by the level designer. This is a stop-gap for an exploit where players could hop through
// slime and barely take any damage because the trigger would reset damage anytime there was no
// one in the trigger when this function was called. (sjb)
m_flDmgResetTime = gpGlobals->curtime + TRIGGER_HURT_FORGIVE_TIME;
}
}
return hurtCount;
}
void CTriggerHurt::Touch( CBaseEntity *pOther )
{
if ( m_pfnThink == NULL )
{
SetThink( &CTriggerHurt::HurtThink );
SetNextThink( gpGlobals->curtime );
}
}
// ##################################################################################
// >> TriggerMultiple
// ##################################################################################
LINK_ENTITY_TO_CLASS( trigger_multiple, CTriggerMultiple );
BEGIN_DATADESC( CTriggerMultiple )
// Function Pointers
DEFINE_FUNCTION(MultiTouch),
DEFINE_FUNCTION(MultiWaitOver ),
// Outputs
DEFINE_OUTPUT(m_OnTrigger, "OnTrigger")
END_DATADESC()
//-----------------------------------------------------------------------------
// Purpose: Called when spawning, after keyvalues have been handled.
//-----------------------------------------------------------------------------
void CTriggerMultiple::Spawn( void )
{
BaseClass::Spawn();
InitTrigger();
if (m_flWait == 0)
{
m_flWait = 0.2;
}
ASSERTSZ(m_iHealth == 0, "trigger_multiple with health");
SetTouch( &CTriggerMultiple::MultiTouch );
}
//-----------------------------------------------------------------------------
// Purpose: Touch function. Activates the trigger.
// Input : pOther - The thing that touched us.
//-----------------------------------------------------------------------------
void CTriggerMultiple::MultiTouch(CBaseEntity *pOther)
{
if (PassesTriggerFilters(pOther))