forked from nillerusr/source-engine
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathai_pathfinder.cpp
2137 lines (1775 loc) · 69.1 KB
/
ai_pathfinder.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:
//
// $NoKeywords: $
//=============================================================================//
#include "cbase.h"
#include "ndebugoverlay.h"
#include "ai_pathfinder.h"
#include "ai_basenpc.h"
#include "ai_node.h"
#include "ai_network.h"
#include "ai_waypoint.h"
#include "ai_link.h"
#include "ai_routedist.h"
#include "ai_moveprobe.h"
#include "ai_dynamiclink.h"
#include "ai_hint.h"
#include "bitstring.h"
//@todo: bad dependency!
#include "ai_navigator.h"
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
#define NUM_NPC_DEBUG_OVERLAYS 50
const float MAX_LOCAL_NAV_DIST_GROUND[2] = { (50*12), (25*12) };
const float MAX_LOCAL_NAV_DIST_FLY[2] = { (750*12), (750*12) };
//-----------------------------------------------------------------------------
// CAI_Pathfinder
//
BEGIN_SIMPLE_DATADESC( CAI_Pathfinder )
// m_TriDebugOverlay
// m_bIgnoreStaleLinks
DEFINE_FIELD( m_flLastStaleLinkCheckTime, FIELD_TIME ),
// m_pNetwork
END_DATADESC()
//-----------------------------------------------------------------------------
// Compute move type bits to nav type
//-----------------------------------------------------------------------------
Navigation_t MoveBitsToNavType( int fBits )
{
switch (fBits)
{
case bits_CAP_MOVE_GROUND:
return NAV_GROUND;
case bits_CAP_MOVE_FLY:
return NAV_FLY;
case bits_CAP_MOVE_CLIMB:
return NAV_CLIMB;
case bits_CAP_MOVE_JUMP:
return NAV_JUMP;
default:
// This will only happen if more than one bit is set
Assert(0);
return NAV_NONE;
}
}
//-----------------------------------------------------------------------------
void CAI_Pathfinder::Init( CAI_Network *pNetwork )
{
Assert( pNetwork );
m_pNetwork = pNetwork;
}
//-----------------------------------------------------------------------------
//
//-----------------------------------------------------------------------------
bool CAI_Pathfinder::UseStrongOptimizations()
{
if ( !AIStrongOpt() )
{
return false;
}
#ifdef HL2_DLL
if( GetOuter()->Classify() == CLASS_PLAYER_ALLY_VITAL )
{
return false;
}
#endif//HL2_DLL
return true;
}
//-----------------------------------------------------------------------------
// Computes the link type
//-----------------------------------------------------------------------------
Navigation_t CAI_Pathfinder::ComputeWaypointType( CAI_Node **ppNodes, int parentID, int destID )
{
Navigation_t navType = NAV_NONE;
CAI_Node *pNode = ppNodes[parentID];
for (int link=0; link < pNode->NumLinks();link++)
{
if (pNode->GetLinkByIndex(link)->DestNodeID(parentID) == destID)
{
// BRJ 10/1/02
// FIXME: pNPC->CapabilitiesGet() is actually the mechanism by which fliers
// filter out the bitfields in the waypoint type (most importantly, bits_MOVE_CAP_GROUND)
// that would cause the waypoint distance to be computed in a 2D, as opposed to 3D fashion
// This is a super-scary weak link if you ask me.
int linkMoveTypeBits = pNode->GetLinkByIndex(link)->m_iAcceptedMoveTypes[GetHullType()];
int moveTypeBits = ( linkMoveTypeBits & CapabilitiesGet());
if ( !moveTypeBits && linkMoveTypeBits == bits_CAP_MOVE_JUMP )
{
Assert( pNode->GetHint() && pNode->GetHint()->HintType() == HINT_JUMP_OVERRIDE );
ppNodes[destID]->Lock(0.3);
moveTypeBits = linkMoveTypeBits;
}
Navigation_t linkType = MoveBitsToNavType( moveTypeBits );
// This will only trigger if the links disagree about their nav type
Assert( (navType == NAV_NONE) || (navType == linkType) );
navType = linkType;
break;
}
}
// @TODO (toml 10-15-02): one would not expect to come out of the above logic
// with NAV_NONE. However, if a graph is newly built, it can contain malformed
// links that are referred to by the destination node, not the source node.
// This has to be fixed
if ( navType == NAV_NONE )
{
pNode = ppNodes[destID];
for (int link=0; link < pNode->NumLinks();link++)
{
if (pNode->GetLinkByIndex(link)->DestNodeID(parentID) == destID)
{
int npcMoveBits = CapabilitiesGet();
int nodeMoveBits = pNode->GetLinkByIndex(link)->m_iAcceptedMoveTypes[GetHullType()];
int moveTypeBits = ( npcMoveBits & nodeMoveBits );
Navigation_t linkType = MoveBitsToNavType( moveTypeBits );
Assert( (navType == NAV_NONE) || (navType == linkType) );
navType = linkType;
DevMsg( "Note: Strange link found between nodes in AI node graph\n" );
break;
}
}
}
AssertMsg( navType != NAV_NONE, "Pathfinder appears to have output a path with consecutive nodes thate are not actually connected\n" );
return navType;
}
//-----------------------------------------------------------------------------
// Purpose: Given an array of parentID's and endID, contruct a linked
// list of waypoints through those parents
//-----------------------------------------------------------------------------
AI_Waypoint_t* CAI_Pathfinder::MakeRouteFromParents( int *parentArray, int endID )
{
AI_Waypoint_t *pOldWaypoint = NULL;
AI_Waypoint_t *pNewWaypoint = NULL;
int currentID = endID;
CAI_Node **pAInode = GetNetwork()->AccessNodes();
while (currentID != NO_NODE)
{
// Try to link it to the previous waypoint
int prevID = parentArray[currentID];
int destID;
if (prevID != NO_NODE)
{
destID = prevID;
}
else
{
// If we have no previous node, then use the next node
if ( !pOldWaypoint )
return NULL;
destID = pOldWaypoint->iNodeID;
}
Navigation_t waypointType = ComputeWaypointType( pAInode, currentID, destID );
// BRJ 10/1/02
// FIXME: It appears potentially possible for us to compute waypoints
// here which the NPC is not capable of traversing (because
// pNPC->CapabilitiesGet() in ComputeWaypointType() above filters it out).
// It's also possible if none of the lines have an appropriate DestNodeID.
// Um, shouldn't such a waypoint not be allowed?!?!?
Assert( waypointType != NAV_NONE );
pNewWaypoint = new AI_Waypoint_t( pAInode[currentID]->GetPosition(GetHullType()),
pAInode[currentID]->GetYaw(), waypointType, bits_WP_TO_NODE, currentID );
// Link it up...
pNewWaypoint->SetNext( pOldWaypoint );
pOldWaypoint = pNewWaypoint;
currentID = prevID;
}
return pOldWaypoint;
}
//------------------------------------------------------------------------------
// Purpose : Test if stale link is no longer stale
//------------------------------------------------------------------------------
bool CAI_Pathfinder::IsLinkStillStale(int moveType, CAI_Link *nodeLink)
{
if ( m_bIgnoreStaleLinks )
return false;
if ( !(nodeLink->m_LinkInfo & bits_LINK_STALE_SUGGESTED ) )
return false;
if ( gpGlobals->curtime < nodeLink->m_timeStaleExpires )
return true;
// NPC should only check one stale link per think
if (gpGlobals->curtime == m_flLastStaleLinkCheckTime)
{
return true;
}
else
{
m_flLastStaleLinkCheckTime = gpGlobals->curtime;
}
// Test movement, if suceeds, clear the stale bit
if (CheckStaleRoute(GetNetwork()->GetNode(nodeLink->m_iSrcID)->GetPosition(GetHullType()),
GetNetwork()->GetNode(nodeLink->m_iDestID)->GetPosition(GetHullType()), moveType))
{
nodeLink->m_LinkInfo &= ~bits_LINK_STALE_SUGGESTED;
return false;
}
nodeLink->m_timeStaleExpires = gpGlobals->curtime + 1.0;
return true;
}
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
int CAI_Pathfinder::NearestNodeToNPC()
{
return GetNetwork()->NearestNodeToPoint( GetOuter(), GetAbsOrigin() );
}
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
int CAI_Pathfinder::NearestNodeToPoint( const Vector &vecOrigin )
{
return GetNetwork()->NearestNodeToPoint( GetOuter(), vecOrigin );
}
//-----------------------------------------------------------------------------
// Purpose: Build a path between two nodes
//-----------------------------------------------------------------------------
AI_Waypoint_t *CAI_Pathfinder::FindBestPath(int startID, int endID)
{
AI_PROFILE_SCOPE( CAI_Pathfinder_FindBestPath );
if ( !GetNetwork()->NumNodes() )
return NULL;
#ifdef AI_PERF_MON
m_nPerfStatPB++;
#endif
int nNodes = GetNetwork()->NumNodes();
CAI_Node **pAInode = GetNetwork()->AccessNodes();
CVarBitVec openBS(nNodes);
CVarBitVec closeBS(nNodes);
// ------------- INITIALIZE ------------------------
float* nodeG = (float *)stackalloc( nNodes * sizeof(float) );
float* nodeH = (float *)stackalloc( nNodes * sizeof(float) );
float* nodeF = (float *)stackalloc( nNodes * sizeof(float) );
int* nodeP = (int *)stackalloc( nNodes * sizeof(int) ); // Node parent
for (int node=0;node<nNodes;node++)
{
nodeG[node] = FLT_MAX;
nodeP[node] = -1;
}
nodeG[startID] = 0;
nodeH[startID] = 0.1*(pAInode[startID]->GetPosition(GetHullType())-pAInode[endID]->GetPosition(GetHullType())).Length(); // Don't want to over estimate
nodeF[startID] = nodeG[startID] + nodeH[startID];
openBS.Set(startID);
closeBS.Set( startID );
// --------------- FIND BEST PATH ------------------
while (!openBS.IsAllClear())
{
int smallestID = CAI_Network::FindBSSmallest(&openBS,nodeF,nNodes);
openBS.Clear(smallestID);
CAI_Node *pSmallestNode = pAInode[smallestID];
if (GetOuter()->IsUnusableNode(smallestID, pSmallestNode->GetHint()))
continue;
if (smallestID == endID)
{
AI_Waypoint_t* route = MakeRouteFromParents(&nodeP[0], endID);
return route;
}
// Check this if the node is immediately in the path after the startNode
// that it isn't blocked
for (int link=0; link < pSmallestNode->NumLinks();link++)
{
CAI_Link *nodeLink = pSmallestNode->GetLinkByIndex(link);
if (!IsLinkUsable(nodeLink,smallestID))
continue;
// FIXME: the cost function should take into account Node costs (danger, flanking, etc).
int moveType = nodeLink->m_iAcceptedMoveTypes[GetHullType()] & CapabilitiesGet();
int testID = nodeLink->DestNodeID(smallestID);
Vector r1 = pSmallestNode->GetPosition(GetHullType());
Vector r2 = pAInode[testID]->GetPosition(GetHullType());
float dist = GetOuter()->GetNavigator()->MovementCost( moveType, r1, r2 ); // MovementCost takes ref parameters!!
if ( dist == FLT_MAX )
continue;
float new_g = nodeG[smallestID] + dist;
if ( !closeBS.IsBitSet(testID) || (new_g < nodeG[testID]) )
{
nodeP[testID] = smallestID;
nodeG[testID] = new_g;
nodeH[testID] = (pAInode[testID]->GetPosition(GetHullType())-pAInode[endID]->GetPosition(GetHullType())).Length();
nodeF[testID] = nodeG[testID] + nodeH[testID];
closeBS.Set( testID );
openBS.Set( testID );
}
}
}
return NULL;
}
//-----------------------------------------------------------------------------
// Purpose: Find a short random path of at least pathLength distance. If
// vDirection is given random path will expand in the given direction,
// and then attempt to go generally straight
//-----------------------------------------------------------------------------
AI_Waypoint_t* CAI_Pathfinder::FindShortRandomPath(int startID, float minPathLength, const Vector &directionIn)
{
int pNeighbor[AI_MAX_NODE_LINKS];
int pStaleNeighbor[AI_MAX_NODE_LINKS];
int numNeighbors = 1; // The start node
int numStaleNeighbors = 0;
int neighborID = NO_NODE;
int nNodes = GetNetwork()->NumNodes();
CAI_Node **pAInode = GetNetwork()->AccessNodes();
if ( !nNodes )
return NULL;
MARK_TASK_EXPENSIVE();
int *nodeParent = (int *)stackalloc( sizeof(int) * nNodes );
CVarBitVec closeBS(nNodes);
Vector vDirection = directionIn;
// ------------------------------------------
// Bail immediately if node has no neighbors
// ------------------------------------------
if (pAInode[startID]->NumLinks() == 0)
{
return NULL;
}
// ------------- INITIALIZE ------------------------
nodeParent[startID] = NO_NODE;
pNeighbor[0] = startID;
// --------------- FIND PATH ---------------------------------------------------------------
// Quit when path is long enough, and I've run out of neighbors unless I'm on a climb node
// in which case I'm not allowed to stop
// -----------------------------------------------------------------------------------------
float pathLength = 0;
int nSearchCount = 0;
while ( (pathLength < minPathLength) ||
(neighborID != NO_NODE && pAInode[neighborID]->GetType() == NODE_CLIMB))
{
nSearchCount++;
// If no neighbors try circling back to last node
if (neighborID != NO_NODE &&
numNeighbors == 0 &&
numStaleNeighbors == 0 )
{
// If we dead ended on a climb node we've failed as we
// aren't allowed to stop on a climb node
if (pAInode[neighborID]->GetType() == NODE_CLIMB)
{
// If no neighbors exist we've failed.
return NULL;
}
// Otherwise accept this path to a dead end
else
{
AI_Waypoint_t* route = MakeRouteFromParents(&nodeParent[0], neighborID);
return route;
}
}
// ----------------------
// Pick a neighbor
// ----------------------
int lastID = neighborID;
// If vDirection is non-zero attempt to expand close to current direction
if (vDirection != vec3_origin)
{
float bestDot = -1;
Vector vLastPos;
if (lastID == NO_NODE)
{
vLastPos = GetLocalOrigin();
}
else
{
vLastPos = pAInode[lastID]->GetOrigin();
}
// If no neighbors, try using a stale one
if (numNeighbors == 0)
{
neighborID = pStaleNeighbor[random->RandomInt(0,numStaleNeighbors-1)];
}
else
{
for (int i=0;i<numNeighbors;i++)
{
Vector nodeDir = vLastPos - pAInode[pNeighbor[i]]->GetOrigin();
VectorNormalize(nodeDir);
float fDotPr = DotProduct(vDirection,nodeDir);
if (fDotPr > bestDot)
{
bestDot = fDotPr;
neighborID = pNeighbor[i];
}
}
}
if (neighborID != NO_NODE)
{
vDirection = vLastPos - pAInode[neighborID]->GetOrigin();
VectorNormalize(vDirection);
}
}
// Pick random neighbor
else if (numNeighbors != 0)
{
neighborID = pNeighbor[random->RandomInt(0,numNeighbors-1)];
}
// If no neighbors, try using a stale one
else
{
neighborID = pStaleNeighbor[random->RandomInt(0,numStaleNeighbors-1)];
}
// BUGBUG: This routine is totally hosed!
if ( neighborID < 0 )
return NULL;
// Set previous nodes parent
nodeParent[neighborID] = lastID;
closeBS.Set(neighborID);
// Add the new length
if (lastID != NO_NODE)
{
pathLength += (pAInode[lastID]->GetOrigin() - pAInode[neighborID]->GetOrigin()).Length();
}
// If path is long enough or we've hit a maximum number of search nodes,
// we're done unless we've ended on a climb node
if ((pathLength >= minPathLength || nSearchCount > 20) &&
pAInode[neighborID]->GetType() != NODE_CLIMB)
{
return MakeRouteFromParents(&nodeParent[0], neighborID);
}
// Clear neighbors
numNeighbors = 0;
numStaleNeighbors = 0;
// Now add in new neighbors, pick links in different order ever time
pAInode[neighborID]->ShuffleLinks();
for (int link=0; link < pAInode[neighborID]->NumLinks();link++)
{
if ( numStaleNeighbors == ARRAYSIZE(pStaleNeighbor) )
{
AssertMsg( 0, "Array overflow" );
return NULL;
}
if ( numNeighbors == ARRAYSIZE(pStaleNeighbor) )
{
AssertMsg( 0, "Array overflow" );
return NULL;
}
CAI_Link* nodeLink = pAInode[neighborID]->GetShuffeledLink(link);
int testID = nodeLink->DestNodeID(neighborID);
// --------------------------------------------------------------------------
// Don't loop
// --------------------------------------------------------------------------
if (closeBS.IsBitSet(testID))
{
continue;
}
// --------------------------------------------------------------------------
// Don't go back to the node I just visited
// --------------------------------------------------------------------------
if (testID == lastID)
{
continue;
}
// --------------------------------------------------------------------------
// Make sure link is valid
// --------------------------------------------------------------------------
if (!IsLinkUsable(nodeLink,neighborID))
{
continue;
}
// --------------------------------------------------------------------------
// If its a stale node add to stale list
// --------------------------------------------------------------------------
if (pAInode[testID]->IsLocked())
{
pStaleNeighbor[numStaleNeighbors]=testID;
numStaleNeighbors++;
}
// --------------------------------------
// Add to list of non-stale neighbors
// --------------------------------------
else
{
pNeighbor[numNeighbors]=testID;
numNeighbors++;
}
}
}
// Failed to get a path of full length, but return what we have
return MakeRouteFromParents(&nodeParent[0], neighborID);
}
//------------------------------------------------------------------------------
// Purpose : Returns true is link us usable by the given NPC from the
// startID node.
//------------------------------------------------------------------------------
bool CAI_Pathfinder::IsLinkUsable(CAI_Link *pLink, int startID)
{
// --------------------------------------------------------------------------
// Skip if link turned off
// --------------------------------------------------------------------------
if (pLink->m_LinkInfo & bits_LINK_OFF)
{
CAI_DynamicLink *pDynamicLink = pLink->m_pDynamicLink;
if ( !pDynamicLink || pDynamicLink->m_strAllowUse == NULL_STRING )
return false;
const char *pszAllowUse = STRING( pDynamicLink->m_strAllowUse );
if ( pDynamicLink->m_bInvertAllow )
{
// Exlude only the specified entity name or classname
if ( GetOuter()->NameMatches(pszAllowUse) || GetOuter()->ClassMatches( pszAllowUse ) )
return false;
}
else
{
// Exclude everything but the allowed entity name or classname
if ( !GetOuter()->NameMatches( pszAllowUse) && !GetOuter()->ClassMatches( pszAllowUse ) )
return false;
}
}
// --------------------------------------------------------------------------
// Get the destination nodeID
// --------------------------------------------------------------------------
int endID = pLink->DestNodeID(startID);
// --------------------------------------------------------------------------
// Make sure I have the ability to do the type of movement specified by the link
// --------------------------------------------------------------------------
int linkMoveTypes = pLink->m_iAcceptedMoveTypes[GetHullType()];
int moveType = ( linkMoveTypes & CapabilitiesGet() );
CAI_Node *pStartNode,*pEndNode;
pStartNode = GetNetwork()->GetNode(startID);
pEndNode = GetNetwork()->GetNode(endID);
if ( (linkMoveTypes & bits_CAP_MOVE_JUMP) && !moveType )
{
CAI_Hint *pStartHint = pStartNode->GetHint();
CAI_Hint *pEndHint = pEndNode->GetHint();
if ( pStartHint && pEndHint )
{
if ( pStartHint->HintType() == HINT_JUMP_OVERRIDE &&
pEndHint->HintType() == HINT_JUMP_OVERRIDE &&
( ( ( pStartHint->GetSpawnFlags() | pEndHint->GetSpawnFlags() ) & SF_ALLOW_JUMP_UP ) || pStartHint->GetAbsOrigin().z > pEndHint->GetAbsOrigin().z ) )
{
if ( !pStartNode->IsLocked() )
{
if ( pStartHint->GetTargetNode() == -1 || pStartHint->GetTargetNode() == endID )
moveType = bits_CAP_MOVE_JUMP;
}
}
}
}
if (!moveType)
{
return false;
}
// --------------------------------------------------------------------------
// Check if NPC has a reason not to use the desintion node
// --------------------------------------------------------------------------
if (GetOuter()->IsUnusableNode(endID, pEndNode->GetHint()))
{
return false;
}
// --------------------------------------------------------------------------
// If a jump make sure the jump is within NPC's legal parameters for jumping
// --------------------------------------------------------------------------
if (moveType == bits_CAP_MOVE_JUMP)
{
if (!GetOuter()->IsJumpLegal(pStartNode->GetPosition(GetHullType()),
pEndNode->GetPosition(GetHullType()),
pEndNode->GetPosition(GetHullType())))
{
return false;
}
}
// --------------------------------------------------------------------------
// If an NPC suggested that this link is stale and I haven't checked it yet
// I should make sure the link is still valid before proceeding
// --------------------------------------------------------------------------
if (pLink->m_LinkInfo & bits_LINK_STALE_SUGGESTED)
{
if (IsLinkStillStale(moveType, pLink))
{
return false;
}
}
return true;
}
//-----------------------------------------------------------------------------
static int NPCBuildFlags( CAI_BaseNPC *pNPC, const Vector &vecOrigin )
{
// If vecOrigin the the npc's position and npc is climbing only climb nodes allowed
if (pNPC->GetLocalOrigin() == vecOrigin && pNPC->GetNavType() == NAV_CLIMB)
{
return bits_BUILD_CLIMB;
}
else if (pNPC->CapabilitiesGet() & bits_CAP_MOVE_FLY)
{
return bits_BUILD_FLY | bits_BUILD_GIVEWAY;
}
else if (pNPC->CapabilitiesGet() & bits_CAP_MOVE_GROUND)
{
int buildFlags = bits_BUILD_GROUND | bits_BUILD_GIVEWAY;
if (pNPC->CapabilitiesGet() & bits_CAP_MOVE_JUMP)
{
buildFlags |= bits_BUILD_JUMP;
}
return buildFlags;
}
return 0;
}
//-----------------------------------------------------------------------------
// Creates a node waypoint
//-----------------------------------------------------------------------------
AI_Waypoint_t* CAI_Pathfinder::CreateNodeWaypoint( Hull_t hullType, int nodeID, int nodeFlags )
{
CAI_Node *pNode = GetNetwork()->GetNode(nodeID);
Navigation_t navType;
switch(pNode->GetType())
{
case NODE_CLIMB:
navType = NAV_CLIMB;
break;
case NODE_AIR:
navType = NAV_FLY;
break;
default:
navType = NAV_GROUND;
break;
}
return new AI_Waypoint_t( pNode->GetPosition(hullType), pNode->GetYaw(), navType, ( bits_WP_TO_NODE | nodeFlags) , nodeID );
}
//-----------------------------------------------------------------------------
// Purpose: Returns a route to a node for the given npc with the given
// build flags
//-----------------------------------------------------------------------------
AI_Waypoint_t* CAI_Pathfinder::RouteToNode(const Vector &vecOrigin, int buildFlags, int nodeID, float goalTolerance)
{
AI_PROFILE_SCOPE( CAI_Pathfinder_RouteToNode );
buildFlags |= NPCBuildFlags( GetOuter(), vecOrigin );
buildFlags &= ~bits_BUILD_GET_CLOSE;
// Check if vecOrigin is already at the smallest node
// FIXME: an equals check is a bit sloppy, this should be a tolerance
const Vector &vecNodePosition = GetNetwork()->GetNode(nodeID)->GetPosition(GetHullType());
if (vecOrigin == vecNodePosition)
{
return CreateNodeWaypoint( GetHullType(), nodeID, bits_WP_TO_GOAL );
}
// Otherwise try to build a local route to the node
AI_Waypoint_t *pResult = BuildLocalRoute(vecOrigin,
vecNodePosition, NULL, bits_WP_TO_NODE, nodeID, buildFlags, goalTolerance);
if ( pResult )
pResult->iNodeID = nodeID;
return pResult;
}
//-----------------------------------------------------------------------------
// Purpose: Returns a route to a node for the given npc with the given
// build flags
//-----------------------------------------------------------------------------
AI_Waypoint_t* CAI_Pathfinder::RouteFromNode(const Vector &vecOrigin, int buildFlags, int nodeID, float goalTolerance)
{
AI_PROFILE_SCOPE( CAI_Pathfinder_RouteFromNode );
buildFlags |= NPCBuildFlags( GetOuter(), vecOrigin );
buildFlags |= bits_BUILD_GET_CLOSE;
// Check if vecOrigin is already at the smallest node
// FIXME: an equals check is a bit sloppy, this should be a tolerance
CAI_Node *pNode = GetNetwork()->GetNode(nodeID);
const Vector &vecNodePosition = pNode->GetPosition(GetHullType());
if (vecOrigin == vecNodePosition)
{
return CreateNodeWaypoint( GetHullType(), nodeID, bits_WP_TO_GOAL );
}
// Otherwise try to build a local route from the node
AI_Waypoint_t* pResult = BuildLocalRoute( vecNodePosition,
vecOrigin, NULL, bits_WP_TO_GOAL, NO_NODE, buildFlags, goalTolerance);
// Handle case of target hanging over edge near climb dismount
if ( !pResult &&
pNode->GetType() == NODE_CLIMB &&
( vecOrigin - vecNodePosition ).Length2DSqr() < 32.0*32.0 &&
GetOuter()->GetMoveProbe()->CheckStandPosition(vecNodePosition, MASK_NPCSOLID_BRUSHONLY ) )
{
pResult = new AI_Waypoint_t( vecOrigin, 0, NAV_GROUND, bits_WP_TO_GOAL, nodeID );
}
return pResult;
}
//-----------------------------------------------------------------------------
// Builds a simple route (no triangulation, no making way)
//-----------------------------------------------------------------------------
AI_Waypoint_t *CAI_Pathfinder::BuildSimpleRoute( Navigation_t navType, const Vector &vStart,
const Vector &vEnd, const CBaseEntity *pTarget, int endFlags, int nodeID,
int nodeTargetType, float flYaw )
{
Assert( navType == NAV_JUMP || navType == NAV_CLIMB ); // this is what this here function is for
// Only allowed to jump to ground nodes
if ((nodeID == NO_NODE) || (GetNetwork()->GetNode(nodeID)->GetType() == nodeTargetType) )
{
AIMoveTrace_t moveTrace;
GetOuter()->GetMoveProbe()->MoveLimit( navType, vStart, vEnd, MASK_NPCSOLID, pTarget, &moveTrace );
// If I was able to make the move, or the vEnd is the
// goal and I'm within tolerance, just move to vEnd
if (!IsMoveBlocked(moveTrace))
{
// It worked so return a route of length one to the endpoint
return new AI_Waypoint_t( vEnd, flYaw, navType, endFlags, nodeID );
}
}
return NULL;
}
//-----------------------------------------------------------------------------
// Builds a complex route (triangulation, making way)
//-----------------------------------------------------------------------------
AI_Waypoint_t *CAI_Pathfinder::BuildComplexRoute( Navigation_t navType, const Vector &vStart,
const Vector &vEnd, const CBaseEntity *pTarget, int endFlags, int nodeID,
int buildFlags, float flYaw, float goalTolerance, float maxLocalNavDistance )
{
AI_PROFILE_SCOPE( CAI_Pathfinder_BuildComplexRoute );
float flTotalDist = ComputePathDistance( navType, vStart, vEnd );
if ( flTotalDist < 0.0625 )
{
return new AI_Waypoint_t( vEnd, flYaw, navType, endFlags, nodeID );
}
unsigned int collideFlags = (buildFlags & bits_BUILD_IGNORE_NPCS) ? MASK_NPCSOLID_BRUSHONLY : MASK_NPCSOLID;
bool bCheckGround = (GetOuter()->CapabilitiesGet() & bits_CAP_SKIP_NAV_GROUND_CHECK) ? false : true;
if ( flTotalDist <= maxLocalNavDistance )
{
AIMoveTrace_t moveTrace;
AI_PROFILE_SCOPE_BEGIN( CAI_Pathfinder_BuildComplexRoute_Direct );
GetOuter()->GetMoveProbe()->MoveLimit( navType, vStart, vEnd, collideFlags, pTarget, (bCheckGround) ? 100 : 0, &moveTrace);
// If I was able to make the move...
if (!IsMoveBlocked(moveTrace))
{
// It worked so return a route of length one to the endpoint
return new AI_Waypoint_t( vEnd, flYaw, navType, endFlags, nodeID );
}
// ...or the vEnd is thegoal and I'm within tolerance, just move to vEnd
if ( (buildFlags & bits_BUILD_GET_CLOSE) &&
(endFlags & bits_WP_TO_GOAL) &&
moveTrace.flDistObstructed <= goalTolerance )
{
return new AI_Waypoint_t( vEnd, flYaw, navType, endFlags, nodeID );
}
AI_PROFILE_SCOPE_END();
// -------------------------------------------------------------------
// Try to triangulate if requested
// -------------------------------------------------------------------
AI_PROFILE_SCOPE_BEGIN( CAI_Pathfinder_BuildComplexRoute_Triangulate );
if (buildFlags & bits_BUILD_TRIANG)
{
if ( !UseStrongOptimizations() || ( GetOuter()->GetState() == NPC_STATE_SCRIPT || GetOuter()->IsCurSchedule( SCHED_SCENE_GENERIC, false ) ) )
{
float flTotalDist = ComputePathDistance( navType, vStart, vEnd );
AI_Waypoint_t *triangRoute = BuildTriangulationRoute(vStart, vEnd, pTarget,
endFlags, nodeID, flYaw, flTotalDist - moveTrace.flDistObstructed, navType);
if (triangRoute)
{
return triangRoute;
}
}
}
AI_PROFILE_SCOPE_END();
// -------------------------------------------------------------------
// Try to giveway if requested
// -------------------------------------------------------------------
if (moveTrace.fStatus == AIMR_BLOCKED_NPC && (buildFlags & bits_BUILD_GIVEWAY))
{
// If I can't get there even ignoring NPCs, don't bother to request a giveway
AIMoveTrace_t moveTrace2;
GetOuter()->GetMoveProbe()->MoveLimit( navType, vStart, vEnd, MASK_NPCSOLID_BRUSHONLY, pTarget, (bCheckGround) ? 100 : 0, &moveTrace2 );
if (!IsMoveBlocked(moveTrace2))
{
// If I can clear the way return a route of length one to the target location
if ( CanGiveWay(vStart, vEnd, moveTrace.pObstruction) )
{
return new AI_Waypoint_t( vEnd, flYaw, navType, endFlags, nodeID );
}
}
}
}
return NULL;
}
//-----------------------------------------------------------------------------
// Purpose: Attempts to build a jump route between vStart
// and vEnd, ignoring entity pTarget
// Input :
// Output : Returns a route if sucessful or NULL if no local route was possible
//-----------------------------------------------------------------------------
AI_Waypoint_t *CAI_Pathfinder::BuildJumpRoute(const Vector &vStart, const Vector &vEnd,
const CBaseEntity *pTarget, int endFlags, int nodeID, int buildFlags, float flYaw)
{
// Only allowed to jump to ground nodes
return BuildSimpleRoute( NAV_JUMP, vStart, vEnd, pTarget,
endFlags, nodeID, NODE_GROUND, flYaw );
}
//-----------------------------------------------------------------------------
// Purpose: Attempts to build a climb route between vStart
// and vEnd, ignoring entity pTarget
// Input :
// Output : Returns a route if sucessful or NULL if no climb route was possible
//-----------------------------------------------------------------------------
AI_Waypoint_t *CAI_Pathfinder::BuildClimbRoute(const Vector &vStart, const Vector &vEnd, const CBaseEntity *pTarget, int endFlags, int nodeID, int buildFlags, float flYaw)
{
// Only allowed to climb to climb nodes
return BuildSimpleRoute( NAV_CLIMB, vStart, vEnd, pTarget,
endFlags, nodeID, NODE_CLIMB, flYaw );
}
//-----------------------------------------------------------------------------
// Purpose: Attempts to build a ground route between vStart
// and vEnd, ignoring entity pTarget the the given tolerance
// Input :
// Output : Returns a route if sucessful or NULL if no ground route was possible
//-----------------------------------------------------------------------------
AI_Waypoint_t *CAI_Pathfinder::BuildGroundRoute(const Vector &vStart, const Vector &vEnd,
const CBaseEntity *pTarget, int endFlags, int nodeID, int buildFlags, float flYaw, float goalTolerance)
{
return BuildComplexRoute( NAV_GROUND, vStart, vEnd, pTarget,
endFlags, nodeID, buildFlags, flYaw, goalTolerance, MAX_LOCAL_NAV_DIST_GROUND[UseStrongOptimizations()] );
}
//-----------------------------------------------------------------------------
// Purpose: Attempts to build a fly route between vStart
// and vEnd, ignoring entity pTarget the the given tolerance
// Input :
// Output : Returns a route if sucessful or NULL if no ground route was possible
//-----------------------------------------------------------------------------
AI_Waypoint_t *CAI_Pathfinder::BuildFlyRoute(const Vector &vStart, const Vector &vEnd,
const CBaseEntity *pTarget, int endFlags, int nodeID, int buildFlags, float flYaw, float goalTolerance)
{
return BuildComplexRoute( NAV_FLY, vStart, vEnd, pTarget,
endFlags, nodeID, buildFlags, flYaw, goalTolerance, MAX_LOCAL_NAV_DIST_FLY[UseStrongOptimizations()] );
}
//-----------------------------------------------------------------------------
// Purpose: Attempts to build a route between vStart and vEnd, requesting the
// pNPCBlocker to get out of the way
// Input :
// Output : Returns a route if sucessful or NULL if giveway failed
//-----------------------------------------------------------------------------
bool CAI_Pathfinder::CanGiveWay( const Vector& vStart, const Vector& vEnd, CBaseEntity *pBlocker)
{
// FIXME: make this a CAI_BaseNPC member function
CAI_BaseNPC *pNPCBlocker = pBlocker->MyNPCPointer();