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
|
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
// $NoKeywords: $
//
//=============================================================================//
// nav_entities.cpp
// AI Navigation entities
// Author: Michael S. Booth ([email protected]), January 2003
#include "cbase.h"
#include "nav_mesh.h"
#include "nav_node.h"
#include "nav_pathfind.h"
#include "nav_colors.h"
#include "fmtstr.h"
#include "props_shared.h"
#include "func_breakablesurf.h"
#ifdef TERROR
#include "func_elevator.h"
#include "AmbientLight.h"
#endif
#ifdef TF_DLL
#include "tf_player.h"
#include "bot/tf_bot.h"
#endif
#include "Color.h"
#include "collisionutils.h"
#include "functorutils.h"
#include "team.h"
#include "nav_entities.h"
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
//--------------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------------
BEGIN_DATADESC( CFuncNavCost )
// Inputs
DEFINE_INPUTFUNC( FIELD_VOID, "Enable", InputEnable ),
DEFINE_INPUTFUNC( FIELD_VOID, "Disable", InputDisable ),
DEFINE_KEYFIELD( m_iszTags, FIELD_STRING, "tags" ),
DEFINE_KEYFIELD( m_team, FIELD_INTEGER, "team" ),
DEFINE_KEYFIELD( m_isDisabled, FIELD_BOOLEAN, "start_disabled" ),
DEFINE_THINKFUNC( CostThink ),
END_DATADESC()
LINK_ENTITY_TO_CLASS( func_nav_avoid, CFuncNavAvoid );
LINK_ENTITY_TO_CLASS( func_nav_prefer, CFuncNavPrefer );
CUtlVector< CHandle< CFuncNavCost > > CFuncNavCost::gm_masterCostVector;
CountdownTimer CFuncNavCost::gm_dirtyTimer;
#define UPDATE_DIRTY_TIME 0.2f
//--------------------------------------------------------------------------------------------------------
void CFuncNavCost::Spawn( void )
{
BaseClass::Spawn();
gm_masterCostVector.AddToTail( this );
gm_dirtyTimer.Start( UPDATE_DIRTY_TIME );
SetSolid( SOLID_BSP );
AddSolidFlags( FSOLID_NOT_SOLID );
SetMoveType( MOVETYPE_NONE );
SetModel( STRING( GetModelName() ) );
AddEffects( EF_NODRAW );
SetCollisionGroup( COLLISION_GROUP_NONE );
VPhysicsInitShadow( false, false );
SetThink( &CFuncNavCost::CostThink );
SetNextThink( gpGlobals->curtime + UPDATE_DIRTY_TIME );
m_tags.RemoveAll();
const char *tags = STRING( m_iszTags );
// chop space-delimited string into individual tokens
if ( tags )
{
char *buffer = new char [ strlen( tags ) + 1 ];
Q_strcpy( buffer, tags );
for( char *token = strtok( buffer, " " ); token; token = strtok( NULL, " " ) )
{
m_tags.AddToTail( CFmtStr( "%s", token ) );
}
delete [] buffer;
}
}
//--------------------------------------------------------------------------------------------------------
void CFuncNavCost::UpdateOnRemove( void )
{
gm_masterCostVector.FindAndFastRemove( this );
BaseClass::UpdateOnRemove();
gm_dirtyTimer.Start( UPDATE_DIRTY_TIME );
}
//--------------------------------------------------------------------------------------------------------
void CFuncNavCost::InputEnable( inputdata_t &inputdata )
{
m_isDisabled = false;
gm_dirtyTimer.Start( UPDATE_DIRTY_TIME );
}
//--------------------------------------------------------------------------------------------------------
void CFuncNavCost::InputDisable( inputdata_t &inputdata )
{
m_isDisabled = true;
gm_dirtyTimer.Start( UPDATE_DIRTY_TIME );
}
//--------------------------------------------------------------------------------------------------------
void CFuncNavCost::CostThink( void )
{
SetNextThink( gpGlobals->curtime + UPDATE_DIRTY_TIME );
if ( gm_dirtyTimer.HasStarted() && gm_dirtyTimer.IsElapsed() )
{
// one or more avoid entities have changed - update nav decoration
gm_dirtyTimer.Invalidate();
UpdateAllNavCostDecoration();
}
}
//--------------------------------------------------------------------------------------------------------
bool CFuncNavCost::HasTag( const char *groupname ) const
{
for( int i=0; i<m_tags.Count(); ++i )
{
if ( FStrEq( m_tags[i], groupname ) )
{
return true;
}
}
return false;
}
//--------------------------------------------------------------------------------------------------------
// Return true if this cost applies to the given actor
bool CFuncNavCost::IsApplicableTo( CBaseCombatCharacter *who ) const
{
if ( !who )
{
return false;
}
if ( m_team > 0 )
{
if ( who->GetTeamNumber() != m_team )
{
return false;
}
}
#ifdef TF_DLL
// TODO: Make group comparison efficient and move to base combat character
CTFBot *bot = ToTFBot( who );
if ( bot )
{
if ( bot->HasTheFlag() )
{
if ( HasTag( "bomb_carrier" ) )
{
return true;
}
// check custom bomb_carrier tags for this bot
for( int i=0; i<m_tags.Count(); ++i )
{
const char* pszTag = m_tags[i];
if ( V_stristr( pszTag, "bomb_carrier" ) )
{
if ( bot->HasTag( pszTag ) )
{
return true;
}
}
}
// the bomb carrier only pays attention to bomb_carrier costs
return false;
}
if ( bot->HasMission( CTFBot::MISSION_DESTROY_SENTRIES ) )
{
if ( HasTag( "mission_sentry_buster" ) )
{
return true;
}
}
if ( bot->HasMission( CTFBot::MISSION_SNIPER ) )
{
if ( HasTag( "mission_sniper" ) )
{
return true;
}
}
if ( bot->HasMission( CTFBot::MISSION_SPY ) )
{
if ( HasTag( "mission_spy" ) )
{
return true;
}
}
if ( bot->HasMission( CTFBot::MISSION_REPROGRAMMED ) )
{
return false;
}
if ( !bot->IsOnAnyMission() )
{
if ( HasTag( "common" ) )
{
return true;
}
}
if ( HasTag( bot->GetPlayerClass()->GetName() ) )
{
return true;
}
// check custom tags for this bot
for( int i=0; i<m_tags.Count(); ++i )
{
if ( bot->HasTag( m_tags[i] ) )
{
return true;
}
}
// this cost doesn't apply to me
return false;
}
#endif
return false;
}
//--------------------------------------------------------------------------------------------------------
// Reevaluate all func_nav_cost entities and update the nav decoration accordingly.
// This is required to handle overlapping func_nav_cost entities.
void CFuncNavCost::UpdateAllNavCostDecoration( void )
{
int i, j;
// first, clear all avoid decoration from the mesh
for( i=0; i<TheNavAreas.Count(); ++i )
{
TheNavAreas[i]->ClearAllNavCostEntities();
}
// now, mark all areas with active cost entities overlapping them
for( i=0; i<gm_masterCostVector.Count(); ++i )
{
CFuncNavCost *cost = gm_masterCostVector[i];
if ( !cost || !cost->IsEnabled() )
{
continue;
}
Extent extent;
extent.Init( cost );
CUtlVector< CNavArea * > overlapVector;
TheNavMesh->CollectAreasOverlappingExtent( extent, &overlapVector );
Ray_t ray;
trace_t tr;
ICollideable *pCollide = cost->CollisionProp();
for( j=0; j<overlapVector.Count(); ++j )
{
ray.Init( overlapVector[j]->GetCenter(), overlapVector[j]->GetCenter() );
enginetrace->ClipRayToCollideable( ray, MASK_ALL, pCollide, &tr );
if ( tr.startsolid )
{
overlapVector[j]->AddFuncNavCostEntity( cost );
}
}
}
}
//--------------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------------
// Return pathfind cost multiplier for the given actor
float CFuncNavAvoid::GetCostMultiplier( CBaseCombatCharacter *who ) const
{
if ( IsApplicableTo( who ) )
{
return 25.0f;
}
return 1.0f;
}
//--------------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------------
// Return pathfind cost multiplier for the given actor
float CFuncNavPrefer::GetCostMultiplier( CBaseCombatCharacter *who ) const
{
if ( IsApplicableTo( who ) )
{
return 0.04f; // 1/25th
}
return 1.0f;
}
//--------------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------------
BEGIN_DATADESC( CFuncNavBlocker )
// Inputs
DEFINE_INPUTFUNC( FIELD_VOID, "BlockNav", InputBlockNav ),
DEFINE_INPUTFUNC( FIELD_VOID, "UnblockNav", InputUnblockNav ),
DEFINE_KEYFIELD( m_blockedTeamNumber, FIELD_INTEGER, "teamToBlock" ),
DEFINE_KEYFIELD( m_bDisabled, FIELD_BOOLEAN, "StartDisabled" ),
END_DATADESC()
LINK_ENTITY_TO_CLASS( func_nav_blocker, CFuncNavBlocker );
CUtlLinkedList<CFuncNavBlocker *> CFuncNavBlocker::gm_NavBlockers;
//-----------------------------------------------------------------------------------------------------
int CFuncNavBlocker::DrawDebugTextOverlays( void )
{
int offset = BaseClass::DrawDebugTextOverlays();
if (m_debugOverlays & OVERLAY_TEXT_BIT)
{
CFmtStr str;
// FIRST_GAME_TEAM skips TEAM_SPECTATOR and TEAM_UNASSIGNED, so we can print
// useful team names in a non-game-specific fashion.
for ( int i=FIRST_GAME_TEAM; i<FIRST_GAME_TEAM + MAX_NAV_TEAMS; ++i )
{
if ( IsBlockingNav( i ) )
{
CTeam *team = GetGlobalTeam( i );
if ( team )
{
EntityText( offset++, str.sprintf( "blocking team %s", team->GetName() ), 0 );
}
else
{
EntityText( offset++, str.sprintf( "blocking team %d", i ), 0 );
}
}
}
NavAreaCollector collector( true );
Extent extent;
extent.Init( this );
TheNavMesh->ForAllAreasOverlappingExtent( collector, extent );
for ( int i=0; i<collector.m_area.Count(); ++i )
{
CNavArea *area = collector.m_area[i];
Extent areaExtent;
area->GetExtent( &areaExtent );
debugoverlay->AddBoxOverlay( vec3_origin, areaExtent.lo, areaExtent.hi, vec3_angle, 0, 255, 0, 10, NDEBUG_PERSIST_TILL_NEXT_SERVER );
}
}
return offset;
}
//--------------------------------------------------------------------------------------------------------
void CFuncNavBlocker::UpdateBlocked()
{
NavAreaCollector collector( true );
Extent extent;
extent.Init( this );
TheNavMesh->ForAllAreasOverlappingExtent( collector, extent );
for ( int i=0; i<collector.m_area.Count(); ++i )
{
CNavArea *area = collector.m_area[i];
area->UpdateBlocked( true );
}
}
//--------------------------------------------------------------------------------------------------------
// Forces nav areas to unblock when the nav blocker is deleted (round restart) so flow can compute properly
void CFuncNavBlocker::UpdateOnRemove( void )
{
UnblockNav();
gm_NavBlockers.FindAndRemove( this );
BaseClass::UpdateOnRemove();
}
//--------------------------------------------------------------------------------------------------------
void CFuncNavBlocker::Spawn( void )
{
gm_NavBlockers.AddToTail( this );
if ( !m_blockedTeamNumber )
m_blockedTeamNumber = TEAM_ANY;
SetMoveType( MOVETYPE_NONE );
SetModel( STRING( GetModelName() ) );
AddEffects( EF_NODRAW );
SetCollisionGroup( COLLISION_GROUP_NONE );
SetSolid( SOLID_NONE );
AddSolidFlags( FSOLID_NOT_SOLID );
CollisionProp()->WorldSpaceAABB( &m_CachedMins, &m_CachedMaxs );
if ( m_bDisabled )
{
UnblockNav();
}
else
{
BlockNav();
}
}
//--------------------------------------------------------------------------------------------------------
void CFuncNavBlocker::InputBlockNav( inputdata_t &inputdata )
{
BlockNav();
}
//--------------------------------------------------------------------------------------------------------
void CFuncNavBlocker::InputUnblockNav( inputdata_t &inputdata )
{
UnblockNav();
}
//--------------------------------------------------------------------------------------------------------
void CFuncNavBlocker::BlockNav( void )
{
if ( m_blockedTeamNumber == TEAM_ANY )
{
for ( int i=0; i<MAX_NAV_TEAMS; ++i )
{
m_isBlockingNav[ i ] = true;
}
}
else
{
int teamNumber = m_blockedTeamNumber % MAX_NAV_TEAMS;
m_isBlockingNav[ teamNumber ] = true;
}
Extent extent;
extent.Init( this );
TheNavMesh->ForAllAreasOverlappingExtent( *this, extent );
}
//--------------------------------------------------------------------------------------------------------
void CFuncNavBlocker::UnblockNav( void )
{
if ( m_blockedTeamNumber == TEAM_ANY )
{
for ( int i=0; i<MAX_NAV_TEAMS; ++i )
{
m_isBlockingNav[ i ] = false;
}
}
else
{
int teamNumber = m_blockedTeamNumber % MAX_NAV_TEAMS;
m_isBlockingNav[ teamNumber ] = false;
}
UpdateBlocked();
}
//--------------------------------------------------------------------------------------------------------
// functor that blocks areas in our extent
bool CFuncNavBlocker::operator()( CNavArea *area )
{
area->MarkAsBlocked( m_blockedTeamNumber, this );
return true;
}
//--------------------------------------------------------------------------------------------------------
bool CFuncNavBlocker::CalculateBlocked( bool *pResultByTeam, const Vector &vecMins, const Vector &vecMaxs )
{
int nTeamsBlocked = 0;
int i;
bool bBlocked = false;
for ( i=0; i<MAX_NAV_TEAMS; ++i )
{
pResultByTeam[i] = false;
}
FOR_EACH_LL( gm_NavBlockers, iBlocker )
{
CFuncNavBlocker *pBlocker = gm_NavBlockers[iBlocker];
bool bIsIntersecting = false;
for ( i=0; i<MAX_NAV_TEAMS; ++i )
{
if ( pBlocker->m_isBlockingNav[i] )
{
if ( !pResultByTeam[i] )
{
if ( bIsIntersecting || ( bIsIntersecting = IsBoxIntersectingBox( pBlocker->m_CachedMins, pBlocker->m_CachedMaxs, vecMins, vecMaxs ) ) != false )
{
bBlocked = true;
pResultByTeam[i] = true;
nTeamsBlocked++;
}
else
{
continue;
}
}
}
}
if ( nTeamsBlocked == MAX_NAV_TEAMS )
{
break;
}
}
return bBlocked;
}
//-----------------------------------------------------------------------------------------------------
/**
* An entity that can obstruct nav areas. This is meant for semi-transient areas that obstruct
* pathfinding but can be ignored for longer-term queries like computing L4D flow distances and
* escape routes.
*/
class CFuncNavObstruction : public CBaseEntity, public INavAvoidanceObstacle
{
DECLARE_DATADESC();
DECLARE_CLASS( CFuncNavObstruction, CBaseEntity );
public:
void Spawn();
virtual void UpdateOnRemove( void );
void InputEnable( inputdata_t &inputdata );
void InputDisable( inputdata_t &inputdata );
virtual bool IsPotentiallyAbleToObstructNavAreas( void ) const { return true; } // could we at some future time obstruct nav?
virtual float GetNavObstructionHeight( void ) const { return JumpCrouchHeight; } // height at which to obstruct nav areas
virtual bool CanObstructNavAreas( void ) const { return !m_bDisabled; } // can we obstruct nav right this instant?
virtual CBaseEntity *GetObstructingEntity( void ) { return this; }
virtual void OnNavMeshLoaded( void )
{
if ( !m_bDisabled )
{
ObstructNavAreas();
}
}
int DrawDebugTextOverlays( void );
bool operator()( CNavArea *area ); // functor that obstructs areas in our extent
private:
void ObstructNavAreas( void );
bool m_bDisabled;
};
//--------------------------------------------------------------------------------------------------------
BEGIN_DATADESC( CFuncNavObstruction )
DEFINE_KEYFIELD( m_bDisabled, FIELD_BOOLEAN, "StartDisabled" ),
END_DATADESC()
LINK_ENTITY_TO_CLASS( func_nav_avoidance_obstacle, CFuncNavObstruction );
//-----------------------------------------------------------------------------------------------------
int CFuncNavObstruction::DrawDebugTextOverlays( void )
{
int offset = BaseClass::DrawDebugTextOverlays();
if (m_debugOverlays & OVERLAY_TEXT_BIT)
{
if ( CanObstructNavAreas() )
{
EntityText( offset++, "Obstructing nav", NDEBUG_PERSIST_TILL_NEXT_SERVER );
}
else
{
EntityText( offset++, "Not obstructing nav", NDEBUG_PERSIST_TILL_NEXT_SERVER );
}
}
return offset;
}
//--------------------------------------------------------------------------------------------------------
void CFuncNavObstruction::UpdateOnRemove( void )
{
TheNavMesh->UnregisterAvoidanceObstacle( this );
BaseClass::UpdateOnRemove();
}
//--------------------------------------------------------------------------------------------------------
void CFuncNavObstruction::Spawn( void )
{
SetMoveType( MOVETYPE_NONE );
SetModel( STRING( GetModelName() ) );
AddEffects( EF_NODRAW );
SetCollisionGroup( COLLISION_GROUP_NONE );
SetSolid( SOLID_NONE );
AddSolidFlags( FSOLID_NOT_SOLID );
if ( !m_bDisabled )
{
ObstructNavAreas();
TheNavMesh->RegisterAvoidanceObstacle( this );
}
}
//--------------------------------------------------------------------------------------------------------
void CFuncNavObstruction::InputEnable( inputdata_t &inputdata )
{
m_bDisabled = false;
ObstructNavAreas();
TheNavMesh->RegisterAvoidanceObstacle( this );
}
//--------------------------------------------------------------------------------------------------------
void CFuncNavObstruction::InputDisable( inputdata_t &inputdata )
{
m_bDisabled = true;
TheNavMesh->UnregisterAvoidanceObstacle( this );
}
//--------------------------------------------------------------------------------------------------------
void CFuncNavObstruction::ObstructNavAreas( void )
{
Extent extent;
extent.Init( this );
TheNavMesh->ForAllAreasOverlappingExtent( *this, extent );
}
//--------------------------------------------------------------------------------------------------------
// functor that blocks areas in our extent
bool CFuncNavObstruction::operator()( CNavArea *area )
{
area->MarkObstacleToAvoid( GetNavObstructionHeight() );
return true;
}
//--------------------------------------------------------------------------------------------------------------
|