forked from ValveSoftware/halflife
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnodes.cpp
More file actions
3657 lines (3102 loc) · 101 KB
/
nodes.cpp
File metadata and controls
3657 lines (3102 loc) · 101 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 (c) 1996-2001, Valve LLC. All rights reserved.
*
* This product contains software technology licensed from Id
* Software, Inc. ("Id Technology"). Id Technology (c) 1996 Id Software, Inc.
* All Rights Reserved.
*
* This source code contains proprietary and confidential information of
* Valve LLC and its suppliers. Access to this code is restricted to
* persons who have executed a written SDK license with Valve. Any access,
* use or distribution of this code by or to any unlicensed person is illegal.
*
****/
//=========================================================
// nodes.cpp - AI node tree stuff.
//=========================================================
#include "extdll.h"
#include "util.h"
#include "cbase.h"
#include "monsters.h"
#include "nodes.h"
#include "animation.h"
#include "doors.h"
#if !defined ( _WIN32 )
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h> // mkdir
#endif
#define HULL_STEP_SIZE 16// how far the test hull moves on each step
#define NODE_HEIGHT 8 // how high to lift nodes off the ground after we drop them all (make stair/ramp mapping easier)
// to help eliminate node clutter by level designers, this is used to cap how many other nodes
// any given node is allowed to 'see' in the first stage of graph creation "LinkVisibleNodes()".
#define MAX_NODE_INITIAL_LINKS 128
#define MAX_NODES 1024
extern DLL_GLOBAL edict_t *g_pBodyQueueHead;
Vector VecBModelOrigin( entvars_t* pevBModel );
CGraph WorldGraph;
LINK_ENTITY_TO_CLASS( info_node, CNodeEnt );
LINK_ENTITY_TO_CLASS( info_node_air, CNodeEnt );
#ifdef _LINUX
#include <unistd.h>
#define CreateDirectory(p, n) mkdir(p, 0777)
#endif
//=========================================================
// CGraph - InitGraph - prepares the graph for use. Frees any
// memory currently in use by the world graph, NULLs
// all pointers, and zeros the node count.
//=========================================================
void CGraph :: InitGraph( void)
{
// Make the graph unavailable
//
m_fGraphPresent = FALSE;
m_fGraphPointersSet = FALSE;
m_fRoutingComplete = FALSE;
// Free the link pool
//
if ( m_pLinkPool )
{
free ( m_pLinkPool );
m_pLinkPool = NULL;
}
// Free the node info
//
if ( m_pNodes )
{
free ( m_pNodes );
m_pNodes = NULL;
}
if ( m_di )
{
free ( m_di );
m_di = NULL;
}
// Free the routing info.
//
if ( m_pRouteInfo )
{
free ( m_pRouteInfo );
m_pRouteInfo = NULL;
}
if (m_pHashLinks)
{
free(m_pHashLinks);
m_pHashLinks = NULL;
}
// Zero node and link counts
//
m_cNodes = 0;
m_cLinks = 0;
m_nRouteInfo = 0;
m_iLastActiveIdleSearch = 0;
m_iLastCoverSearch = 0;
}
//=========================================================
// CGraph - AllocNodes - temporary function that mallocs a
// reasonable number of nodes so we can build the path which
// will be saved to disk.
//=========================================================
int CGraph :: AllocNodes ( void )
{
// malloc all of the nodes
WorldGraph.m_pNodes = (CNode *)calloc ( sizeof ( CNode ), MAX_NODES );
// could not malloc space for all the nodes!
if ( !WorldGraph.m_pNodes )
{
ALERT ( at_aiconsole, "**ERROR**\nCouldn't malloc %d nodes!\n", WorldGraph.m_cNodes );
return FALSE;
}
return TRUE;
}
//=========================================================
// CGraph - LinkEntForLink - sometimes the ent that blocks
// a path is a usable door, in which case the monster just
// needs to face the door and fire it. In other cases, the
// monster needs to operate a button or lever to get the
// door to open. This function will return a pointer to the
// button if the monster needs to hit a button to open the
// door, or returns a pointer to the door if the monster
// need only use the door.
//
// pNode is the node the monster will be standing on when it
// will need to stop and trigger the ent.
//=========================================================
entvars_t* CGraph :: LinkEntForLink ( CLink *pLink, CNode *pNode )
{
edict_t *pentSearch;
edict_t *pentTrigger;
entvars_t *pevTrigger;
entvars_t *pevLinkEnt;
TraceResult tr;
pevLinkEnt = pLink->m_pLinkEnt;
if ( !pevLinkEnt )
return NULL;
pentSearch = NULL;// start search at the top of the ent list.
if ( FClassnameIs ( pevLinkEnt, "func_door" ) || FClassnameIs ( pevLinkEnt, "func_door_rotating" ) )
{
///!!!UNDONE - check for TOGGLE or STAY open doors here. If a door is in the way, and is
// TOGGLE or STAY OPEN, even monsters that can't open doors can go that way.
if ( ( pevLinkEnt->spawnflags & SF_DOOR_USE_ONLY ) )
{// door is use only, so the door is all the monster has to worry about
return pevLinkEnt;
}
while ( 1 )
{
pentTrigger = FIND_ENTITY_BY_TARGET ( pentSearch, STRING( pevLinkEnt->targetname ) );// find the button or trigger
if ( FNullEnt( pentTrigger ) )
{// no trigger found
// right now this is a problem among auto-open doors, or any door that opens through the use
// of a trigger brush. Trigger brushes have no models, and don't show up in searches. Just allow
// monsters to open these sorts of doors for now.
return pevLinkEnt;
}
pentSearch = pentTrigger;
pevTrigger = VARS( pentTrigger );
if ( FClassnameIs(pevTrigger, "func_button") || FClassnameIs(pevTrigger, "func_rot_button" ) )
{// only buttons are handled right now.
// trace from the node to the trigger, make sure it's one we can see from the node.
// !!!HACKHACK Use bodyqueue here cause there are no ents we really wish to ignore!
UTIL_TraceLine ( pNode->m_vecOrigin, VecBModelOrigin( pevTrigger ), ignore_monsters, g_pBodyQueueHead, &tr );
if ( VARS(tr.pHit) == pevTrigger )
{// good to go!
return VARS( tr.pHit );
}
}
}
}
else
{
ALERT ( at_aiconsole, "Unsupported PathEnt:\n'%s'\n", STRING ( pevLinkEnt->classname ) );
return NULL;
}
}
//=========================================================
// CGraph - HandleLinkEnt - a brush ent is between two
// nodes that would otherwise be able to see each other.
// Given the monster's capability, determine whether
// or not the monster can go this way.
//=========================================================
int CGraph :: HandleLinkEnt ( int iNode, entvars_t *pevLinkEnt, int afCapMask, NODEQUERY queryType )
{
edict_t *pentWorld;
CBaseEntity *pDoor;
TraceResult tr;
if ( !m_fGraphPresent || !m_fGraphPointersSet )
{// protect us in the case that the node graph isn't available
ALERT ( at_aiconsole, "Graph not ready!\n" );
return FALSE;
}
if ( FNullEnt ( pevLinkEnt ) )
{
ALERT ( at_aiconsole, "dead path ent!\n" );
return TRUE;
}
pentWorld = NULL;
// func_door
if ( FClassnameIs( pevLinkEnt, "func_door" ) || FClassnameIs( pevLinkEnt, "func_door_rotating" ) )
{// ent is a door.
pDoor = ( CBaseEntity::Instance( pevLinkEnt ) );
if ( ( pevLinkEnt->spawnflags & SF_DOOR_USE_ONLY ) )
{// door is use only.
if ( ( afCapMask & bits_CAP_OPEN_DOORS ) )
{// let monster right through if he can open doors
return TRUE;
}
else
{
// monster should try for it if the door is open and looks as if it will stay that way
if ( pDoor->GetToggleState()== TS_AT_TOP && ( pevLinkEnt->spawnflags & SF_DOOR_NO_AUTO_RETURN ) )
{
return TRUE;
}
return FALSE;
}
}
else
{// door must be opened with a button or trigger field.
// monster should try for it if the door is open and looks as if it will stay that way
if ( pDoor->GetToggleState() == TS_AT_TOP && ( pevLinkEnt->spawnflags & SF_DOOR_NO_AUTO_RETURN ) )
{
return TRUE;
}
if ( ( afCapMask & bits_CAP_OPEN_DOORS ) )
{
if ( !( pevLinkEnt->spawnflags & SF_DOOR_NOMONSTERS ) || queryType == NODEGRAPH_STATIC )
return TRUE;
}
return FALSE;
}
}
// func_breakable
else if ( FClassnameIs( pevLinkEnt, "func_breakable" ) && queryType == NODEGRAPH_STATIC )
{
return TRUE;
}
else
{
ALERT ( at_aiconsole, "Unhandled Ent in Path %s\n", STRING( pevLinkEnt->classname ) );
return FALSE;
}
return FALSE;
}
#if 0
//=========================================================
// FindNearestLink - finds the connection (line) nearest
// the given point. Returns FALSE if fails, or TRUE if it
// has stuffed the index into the nearest link pool connection
// into the passed int pointer, and a BOOL telling whether or
// not the point is along the line into the passed BOOL pointer.
//=========================================================
int CGraph :: FindNearestLink ( const Vector &vecTestPoint, int *piNearestLink, BOOL *pfAlongLine )
{
int i, j;// loops
int iNearestLink;// index into the link pool, this is the nearest node at any time.
float flMinDist;// the distance of of the nearest case so far
float flDistToLine;// the distance of the current test case
BOOL fCurrentAlongLine;
BOOL fSuccess;
//float flConstant;// line constant
Vector vecSpot1, vecSpot2;
Vector2D vec2Spot1, vec2Spot2, vec2TestPoint;
Vector2D vec2Normal;// line normal
Vector2D vec2Line;
TraceResult tr;
iNearestLink = -1;// prepare for failure
fSuccess = FALSE;
flMinDist = 9999;// anything will be closer than this
// go through all of the nodes, and each node's connections
int cSkip = 0;// how many links proper pairing allowed us to skip
int cChecked = 0;// how many links were checked
for ( i = 0 ; i < m_cNodes ; i++ )
{
vecSpot1 = m_pNodes[ i ].m_vecOrigin;
if ( m_pNodes[ i ].m_cNumLinks <= 0 )
{// this shouldn't happen!
ALERT ( at_aiconsole, "**Node %d has no links\n", i );
continue;
}
for ( j = 0 ; j < m_pNodes[ i ].m_cNumLinks ; j++ )
{
/*
!!!This optimization only works when the node graph consists of properly linked pairs.
if ( INodeLink ( i, j ) <= i )
{
// since we're going through the nodes in order, don't check
// any connections whose second node is lower in the list
// than the node we're currently working with. This eliminates
// redundant checks.
cSkip++;
continue;
}
*/
vecSpot2 = PNodeLink ( i, j )->m_vecOrigin;
// these values need a little attention now and then, or sometimes ramps cause trouble.
if ( fabs ( vecSpot1.z - vecTestPoint.z ) > 48 && fabs ( vecSpot2.z - vecTestPoint.z ) > 48 )
{
// if both endpoints of the line are 32 units or more above or below the monster,
// the monster won't be able to get to them, so we do a bit of trivial rejection here.
// this may change if monsters are allowed to jump down.
//
// !!!LATER: some kind of clever X/Y hashing should be used here, too
continue;
}
// now we have two endpoints for a line segment that we've not already checked.
// since all lines that make it this far are within -/+ 32 units of the test point's
// Z Plane, we can get away with doing the point->line check in 2d.
cChecked++;
vec2Spot1 = vecSpot1.Make2D();
vec2Spot2 = vecSpot2.Make2D();
vec2TestPoint = vecTestPoint.Make2D();
// get the line normal.
vec2Line = ( vec2Spot1 - vec2Spot2 ).Normalize();
vec2Normal.x = -vec2Line.y;
vec2Normal.y = vec2Line.x;
if ( DotProduct ( vec2Line, ( vec2TestPoint - vec2Spot1 ) ) > 0 )
{// point outside of line
flDistToLine = ( vec2TestPoint - vec2Spot1 ).Length();
fCurrentAlongLine = FALSE;
}
else if ( DotProduct ( vec2Line, ( vec2TestPoint - vec2Spot2 ) ) < 0 )
{// point outside of line
flDistToLine = ( vec2TestPoint - vec2Spot2 ).Length();
fCurrentAlongLine = FALSE;
}
else
{// point inside line
flDistToLine = fabs( DotProduct ( vec2TestPoint - vec2Spot2, vec2Normal ) );
fCurrentAlongLine = TRUE;
}
if ( flDistToLine < flMinDist )
{// just found a line nearer than any other so far
UTIL_TraceLine ( vecTestPoint, SourceNode( i, j ).m_vecOrigin, ignore_monsters, g_pBodyQueueHead, &tr );
if ( tr.flFraction != 1.0 )
{// crap. can't see the first node of this link, try to see the other
UTIL_TraceLine ( vecTestPoint, DestNode( i, j ).m_vecOrigin, ignore_monsters, g_pBodyQueueHead, &tr );
if ( tr.flFraction != 1.0 )
{// can't use this link, cause can't see either node!
continue;
}
}
fSuccess = TRUE;// we know there will be something to return.
flMinDist = flDistToLine;
iNearestLink = m_pNodes [ i ].m_iFirstLink + j;
*piNearestLink = m_pNodes[ i ].m_iFirstLink + j;
*pfAlongLine = fCurrentAlongLine;
}
}
}
/*
if ( fSuccess )
{
WRITE_BYTE(MSG_BROADCAST, SVC_TEMPENTITY);
WRITE_BYTE(MSG_BROADCAST, TE_SHOWLINE);
WRITE_COORD(MSG_BROADCAST, m_pNodes[ m_pLinkPool[ iNearestLink ].m_iSrcNode ].m_vecOrigin.x );
WRITE_COORD(MSG_BROADCAST, m_pNodes[ m_pLinkPool[ iNearestLink ].m_iSrcNode ].m_vecOrigin.y );
WRITE_COORD(MSG_BROADCAST, m_pNodes[ m_pLinkPool[ iNearestLink ].m_iSrcNode ].m_vecOrigin.z + NODE_HEIGHT);
WRITE_COORD(MSG_BROADCAST, m_pNodes[ m_pLinkPool[ iNearestLink ].m_iDestNode ].m_vecOrigin.x );
WRITE_COORD(MSG_BROADCAST, m_pNodes[ m_pLinkPool[ iNearestLink ].m_iDestNode ].m_vecOrigin.y );
WRITE_COORD(MSG_BROADCAST, m_pNodes[ m_pLinkPool[ iNearestLink ].m_iDestNode ].m_vecOrigin.z + NODE_HEIGHT);
}
*/
ALERT ( at_aiconsole, "%d Checked\n", cChecked );
return fSuccess;
}
#endif
int CGraph::HullIndex( const CBaseEntity *pEntity )
{
if ( pEntity->pev->movetype == MOVETYPE_FLY)
return NODE_FLY_HULL;
if ( pEntity->pev->mins == Vector( -12, -12, 0 ) )
return NODE_SMALL_HULL;
else if ( pEntity->pev->mins == VEC_HUMAN_HULL_MIN )
return NODE_HUMAN_HULL;
else if ( pEntity->pev->mins == Vector ( -32, -32, 0 ) )
return NODE_LARGE_HULL;
// ALERT ( at_aiconsole, "Unknown Hull Mins!\n" );
return NODE_HUMAN_HULL;
}
int CGraph::NodeType( const CBaseEntity *pEntity )
{
if ( pEntity->pev->movetype == MOVETYPE_FLY)
{
if (pEntity->pev->waterlevel != 0)
{
return bits_NODE_WATER;
}
else
{
return bits_NODE_AIR;
}
}
return bits_NODE_LAND;
}
// Sum up graph weights on the path from iStart to iDest to determine path length
float CGraph::PathLength( int iStart, int iDest, int iHull, int afCapMask )
{
float distance = 0;
int iNext;
int iMaxLoop = m_cNodes;
int iCurrentNode = iStart;
int iCap = CapIndex( afCapMask );
while (iCurrentNode != iDest)
{
if (iMaxLoop-- <= 0)
{
ALERT( at_console, "Route Failure\n" );
return 0;
}
iNext = NextNodeInRoute( iCurrentNode, iDest, iHull, iCap );
if (iCurrentNode == iNext)
{
//ALERT(at_aiconsole, "SVD: Can't get there from here..\n");
return 0;
}
int iLink;
HashSearch(iCurrentNode, iNext, iLink);
if (iLink < 0)
{
ALERT(at_console, "HashLinks is broken from %d to %d.\n", iCurrentNode, iDest);
return 0;
}
CLink &link = Link(iLink);
distance += link.m_flWeight;
iCurrentNode = iNext;
}
return distance;
}
// Parse the routing table at iCurrentNode for the next node on the shortest path to iDest
int CGraph::NextNodeInRoute( int iCurrentNode, int iDest, int iHull, int iCap )
{
int iNext = iCurrentNode;
int nCount = iDest+1;
char *pRoute = m_pRouteInfo + m_pNodes[ iCurrentNode ].m_pNextBestNode[iHull][iCap];
// Until we decode the next best node
//
while (nCount > 0)
{
char ch = *pRoute++;
//ALERT(at_aiconsole, "C(%d)", ch);
if (ch < 0)
{
// Sequence phrase
//
ch = -ch;
if (nCount <= ch)
{
iNext = iDest;
nCount = 0;
//ALERT(at_aiconsole, "SEQ: iNext/iDest=%d\n", iNext);
}
else
{
//ALERT(at_aiconsole, "SEQ: nCount + ch (%d + %d)\n", nCount, ch);
nCount = nCount - ch;
}
}
else
{
//ALERT(at_aiconsole, "C(%d)", *pRoute);
// Repeat phrase
//
if (nCount <= ch+1)
{
iNext = iCurrentNode + *pRoute;
if (iNext >= m_cNodes) iNext -= m_cNodes;
else if (iNext < 0) iNext += m_cNodes;
nCount = 0;
//ALERT(at_aiconsole, "REP: iNext=%d\n", iNext);
}
else
{
//ALERT(at_aiconsole, "REP: nCount - ch+1 (%d - %d+1)\n", nCount, ch);
nCount = nCount - ch - 1;
}
pRoute++;
}
}
return iNext;
}
//=========================================================
// CGraph - FindShortestPath
//
// accepts a capability mask (afCapMask), and will only
// find a path usable by a monster with those capabilities
// returns the number of nodes copied into supplied array
//=========================================================
int CGraph :: FindShortestPath ( int *piPath, int iStart, int iDest, int iHull, int afCapMask)
{
int iVisitNode;
int iCurrentNode;
int iNumPathNodes;
int iHullMask;
if ( !m_fGraphPresent || !m_fGraphPointersSet )
{// protect us in the case that the node graph isn't available or built
ALERT ( at_aiconsole, "Graph not ready!\n" );
return FALSE;
}
if ( iStart < 0 || iStart > m_cNodes )
{// The start node is bad?
ALERT ( at_aiconsole, "Can't build a path, iStart is %d!\n", iStart );
return FALSE;
}
if (iStart == iDest)
{
piPath[0] = iStart;
piPath[1] = iDest;
return 2;
}
// Is routing information present.
//
if (m_fRoutingComplete)
{
int iCap = CapIndex( afCapMask );
iNumPathNodes = 0;
piPath[iNumPathNodes++] = iStart;
iCurrentNode = iStart;
int iNext;
//ALERT(at_aiconsole, "GOAL: %d to %d\n", iStart, iDest);
// Until we arrive at the destination
//
while (iCurrentNode != iDest)
{
iNext = NextNodeInRoute( iCurrentNode, iDest, iHull, iCap );
if (iCurrentNode == iNext)
{
//ALERT(at_aiconsole, "SVD: Can't get there from here..\n");
return 0;
break;
}
if (iNumPathNodes >= MAX_PATH_SIZE)
{
//ALERT(at_aiconsole, "SVD: Don't return the entire path.\n");
break;
}
piPath[iNumPathNodes++] = iNext;
iCurrentNode = iNext;
}
//ALERT( at_aiconsole, "SVD: Path with %d nodes.\n", iNumPathNodes);
}
else
{
CQueuePriority queue;
switch( iHull )
{
case NODE_SMALL_HULL:
iHullMask = bits_LINK_SMALL_HULL;
break;
case NODE_HUMAN_HULL:
iHullMask = bits_LINK_HUMAN_HULL;
break;
case NODE_LARGE_HULL:
iHullMask = bits_LINK_LARGE_HULL;
break;
case NODE_FLY_HULL:
iHullMask = bits_LINK_FLY_HULL;
break;
}
// Mark all the nodes as unvisited.
//
int i;
for ( i = 0; i < m_cNodes; i++)
{
m_pNodes[ i ].m_flClosestSoFar = -1.0;
}
m_pNodes[ iStart ].m_flClosestSoFar = 0.0;
m_pNodes[ iStart ].m_iPreviousNode = iStart;// tag this as the origin node
queue.Insert( iStart, 0.0 );// insert start node
while ( !queue.Empty() )
{
// now pull a node out of the queue
float flCurrentDistance;
iCurrentNode = queue.Remove(flCurrentDistance);
// For straight-line weights, the following Shortcut works. For arbitrary weights,
// it doesn't.
//
if (iCurrentNode == iDest) break;
CNode *pCurrentNode = &m_pNodes[ iCurrentNode ];
for ( i = 0 ; i < pCurrentNode->m_cNumLinks ; i++ )
{// run through all of this node's neighbors
iVisitNode = INodeLink ( iCurrentNode, i );
if ( ( m_pLinkPool[ m_pNodes[ iCurrentNode ].m_iFirstLink + i ].m_afLinkInfo & iHullMask ) != iHullMask )
{// monster is too large to walk this connection
//ALERT ( at_aiconsole, "fat ass %d/%d\n",m_pLinkPool[ m_pNodes[ iCurrentNode ].m_iFirstLink + i ].m_afLinkInfo, iMonsterHull );
continue;
}
// check the connection from the current node to the node we're about to mark visited and push into the queue
if ( m_pLinkPool[ m_pNodes[ iCurrentNode ].m_iFirstLink + i ].m_pLinkEnt != NULL )
{// there's a brush ent in the way! Don't mark this node or put it into the queue unless the monster can negotiate it
if ( !HandleLinkEnt ( iCurrentNode, m_pLinkPool[ m_pNodes[ iCurrentNode ].m_iFirstLink + i ].m_pLinkEnt, afCapMask, NODEGRAPH_STATIC ) )
{// monster should not try to go this way.
continue;
}
}
float flOurDistance = flCurrentDistance + m_pLinkPool[ m_pNodes[ iCurrentNode ].m_iFirstLink + i].m_flWeight;
if ( m_pNodes[ iVisitNode ].m_flClosestSoFar < -0.5
|| flOurDistance < m_pNodes[ iVisitNode ].m_flClosestSoFar - 0.001 )
{
m_pNodes[iVisitNode].m_flClosestSoFar = flOurDistance;
m_pNodes[iVisitNode].m_iPreviousNode = iCurrentNode;
queue.Insert ( iVisitNode, flOurDistance );
}
}
}
if ( m_pNodes[iDest].m_flClosestSoFar < -0.5 )
{// Destination is unreachable, no path found.
return 0;
}
// the queue is not empty
// now we must walk backwards through the m_iPreviousNode field, and count how many connections there are in the path
iCurrentNode = iDest;
iNumPathNodes = 1;// count the dest
while ( iCurrentNode != iStart )
{
iNumPathNodes++;
iCurrentNode = m_pNodes[ iCurrentNode ].m_iPreviousNode;
}
iCurrentNode = iDest;
for ( i = iNumPathNodes - 1 ; i >= 0 ; i-- )
{
piPath[ i ] = iCurrentNode;
iCurrentNode = m_pNodes [ iCurrentNode ].m_iPreviousNode;
}
}
#if 0
if (m_fRoutingComplete)
{
// This will draw the entire path that was generated for the monster.
for ( int i = 0 ; i < iNumPathNodes - 1 ; i++ )
{
MESSAGE_BEGIN( MSG_BROADCAST, SVC_TEMPENTITY );
WRITE_BYTE( TE_SHOWLINE);
WRITE_COORD( m_pNodes[ piPath[ i ] ].m_vecOrigin.x );
WRITE_COORD( m_pNodes[ piPath[ i ] ].m_vecOrigin.y );
WRITE_COORD( m_pNodes[ piPath[ i ] ].m_vecOrigin.z + NODE_HEIGHT );
WRITE_COORD( m_pNodes[ piPath[ i + 1 ] ].m_vecOrigin.x );
WRITE_COORD( m_pNodes[ piPath[ i + 1 ] ].m_vecOrigin.y );
WRITE_COORD( m_pNodes[ piPath[ i + 1 ] ].m_vecOrigin.z + NODE_HEIGHT );
MESSAGE_END();
}
}
#endif
#if 0 // MAZE map
MESSAGE_BEGIN( MSG_BROADCAST, SVC_TEMPENTITY );
WRITE_BYTE( TE_SHOWLINE);
WRITE_COORD( m_pNodes[ 4 ].m_vecOrigin.x );
WRITE_COORD( m_pNodes[ 4 ].m_vecOrigin.y );
WRITE_COORD( m_pNodes[ 4 ].m_vecOrigin.z + NODE_HEIGHT );
WRITE_COORD( m_pNodes[ 9 ].m_vecOrigin.x );
WRITE_COORD( m_pNodes[ 9 ].m_vecOrigin.y );
WRITE_COORD( m_pNodes[ 9 ].m_vecOrigin.z + NODE_HEIGHT );
MESSAGE_END();
#endif
return iNumPathNodes;
}
inline ULONG Hash(void *p, int len)
{
CRC32_t ulCrc;
CRC32_INIT(&ulCrc);
CRC32_PROCESS_BUFFER(&ulCrc, p, len);
return CRC32_FINAL(ulCrc);
}
void inline CalcBounds(int &Lower, int &Upper, int Goal, int Best)
{
int Temp = 2*Goal - Best;
if (Best > Goal)
{
Lower = max(0, Temp);
Upper = Best;
}
else
{
Upper = min(255, Temp);
Lower = Best;
}
}
// Convert from [-8192,8192] to [0, 255]
//
inline int CALC_RANGE(int x, int lower, int upper)
{
return NUM_RANGES*(x-lower)/((upper-lower+1));
}
void inline UpdateRange(int &minValue, int &maxValue, int Goal, int Best)
{
int Lower, Upper;
CalcBounds(Lower, Upper, Goal, Best);
if (Upper < maxValue) maxValue = Upper;
if (minValue < Lower) minValue = Lower;
}
void CGraph :: CheckNode(Vector vecOrigin, int iNode)
{
// Have we already seen this point before?.
//
if (m_di[iNode].m_CheckedEvent == m_CheckedCounter) return;
m_di[iNode].m_CheckedEvent = m_CheckedCounter;
float flDist = ( vecOrigin - m_pNodes[ iNode ].m_vecOriginPeek ).Length();
if ( flDist < m_flShortest )
{
TraceResult tr;
// make sure that vecOrigin can trace to this node!
UTIL_TraceLine ( vecOrigin, m_pNodes[ iNode ].m_vecOriginPeek, ignore_monsters, 0, &tr );
if ( tr.flFraction == 1.0 )
{
m_iNearest = iNode;
m_flShortest = flDist;
UpdateRange(m_minX, m_maxX, CALC_RANGE(vecOrigin.x, m_RegionMin[0], m_RegionMax[0]), m_pNodes[iNode].m_Region[0]);
UpdateRange(m_minY, m_maxY, CALC_RANGE(vecOrigin.y, m_RegionMin[1], m_RegionMax[1]), m_pNodes[iNode].m_Region[1]);
UpdateRange(m_minZ, m_maxZ, CALC_RANGE(vecOrigin.z, m_RegionMin[2], m_RegionMax[2]), m_pNodes[iNode].m_Region[2]);
// From maxCircle, calculate maximum bounds box. All points must be
// simultaneously inside all bounds of the box.
//
m_minBoxX = CALC_RANGE(vecOrigin.x - flDist, m_RegionMin[0], m_RegionMax[0]);
m_maxBoxX = CALC_RANGE(vecOrigin.x + flDist, m_RegionMin[0], m_RegionMax[0]);
m_minBoxY = CALC_RANGE(vecOrigin.y - flDist, m_RegionMin[1], m_RegionMax[1]);
m_maxBoxY = CALC_RANGE(vecOrigin.y + flDist, m_RegionMin[1], m_RegionMax[1]);
m_minBoxZ = CALC_RANGE(vecOrigin.z - flDist, m_RegionMin[2], m_RegionMax[2]);
m_maxBoxZ = CALC_RANGE(vecOrigin.z + flDist, m_RegionMin[2], m_RegionMax[2]);
}
}
}
//=========================================================
// CGraph - FindNearestNode - returns the index of the node nearest
// the given vector -1 is failure (couldn't find a valid
// near node )
//=========================================================
int CGraph :: FindNearestNode ( const Vector &vecOrigin, CBaseEntity *pEntity )
{
return FindNearestNode( vecOrigin, NodeType( pEntity ) );
}
int CGraph :: FindNearestNode ( const Vector &vecOrigin, int afNodeTypes )
{
int i;
TraceResult tr;
if ( !m_fGraphPresent || !m_fGraphPointersSet )
{// protect us in the case that the node graph isn't available
ALERT ( at_aiconsole, "Graph not ready!\n" );
return -1;
}
// Check with the cache
//
ULONG iHash = (CACHE_SIZE-1) & Hash((void *)(const float *)vecOrigin, sizeof(vecOrigin));
if (m_Cache[iHash].v == vecOrigin)
{
//ALERT(at_aiconsole, "Cache Hit.\n");
return m_Cache[iHash].n;
}
else
{
//ALERT(at_aiconsole, "Cache Miss.\n");
}
// Mark all points as unchecked.
//
m_CheckedCounter++;
if (m_CheckedCounter == 0)
{
for (int i = 0; i < m_cNodes; i++)
{
m_di[i].m_CheckedEvent = 0;
}
m_CheckedCounter++;
}
m_iNearest = -1;
m_flShortest = 999999.0; // just a big number.
// If we can find a visible point, then let CalcBounds set the limits, but if
// we have no visible point at all to start with, then don't restrict the limits.
//
#if 1
m_minX = 0; m_maxX = 255;
m_minY = 0; m_maxY = 255;
m_minZ = 0; m_maxZ = 255;
m_minBoxX = 0; m_maxBoxX = 255;
m_minBoxY = 0; m_maxBoxY = 255;
m_minBoxZ = 0; m_maxBoxZ = 255;
#else
m_minBoxX = CALC_RANGE(vecOrigin.x - flDist, m_RegionMin[0], m_RegionMax[0]);
m_maxBoxX = CALC_RANGE(vecOrigin.x + flDist, m_RegionMin[0], m_RegionMax[0]);
m_minBoxY = CALC_RANGE(vecOrigin.y - flDist, m_RegionMin[1], m_RegionMax[1]);
m_maxBoxY = CALC_RANGE(vecOrigin.y + flDist, m_RegionMin[1], m_RegionMax[1]);
m_minBoxZ = CALC_RANGE(vecOrigin.z - flDist, m_RegionMin[2], m_RegionMax[2]);
m_maxBoxZ = CALC_RANGE(vecOrigin.z + flDist, m_RegionMin[2], m_RegionMax[2])
CalcBounds(m_minX, m_maxX, CALC_RANGE(vecOrigin.x, m_RegionMin[0], m_RegionMax[0]), m_pNodes[m_iNearest].m_Region[0]);
CalcBounds(m_minY, m_maxY, CALC_RANGE(vecOrigin.y, m_RegionMin[1], m_RegionMax[1]), m_pNodes[m_iNearest].m_Region[1]);
CalcBounds(m_minZ, m_maxZ, CALC_RANGE(vecOrigin.z, m_RegionMin[2], m_RegionMax[2]), m_pNodes[m_iNearest].m_Region[2]);
#endif
int halfX = (m_minX+m_maxX)/2;
int halfY = (m_minY+m_maxY)/2;
int halfZ = (m_minZ+m_maxZ)/2;
int j;
for (i = halfX; i >= m_minX; i--)
{
for (j = m_RangeStart[0][i]; j <= m_RangeEnd[0][i]; j++)
{
if (!(m_pNodes[m_di[j].m_SortedBy[0]].m_afNodeInfo & afNodeTypes)) continue;
int rgY = m_pNodes[m_di[j].m_SortedBy[0]].m_Region[1];
if (rgY > m_maxBoxY) break;
if (rgY < m_minBoxY) continue;
int rgZ = m_pNodes[m_di[j].m_SortedBy[0]].m_Region[2];
if (rgZ < m_minBoxZ) continue;
if (rgZ > m_maxBoxZ) continue;
CheckNode(vecOrigin, m_di[j].m_SortedBy[0]);
}
}
for (i = max(m_minY,halfY+1); i <= m_maxY; i++)
{
for (j = m_RangeStart[1][i]; j <= m_RangeEnd[1][i]; j++)
{
if (!(m_pNodes[m_di[j].m_SortedBy[1]].m_afNodeInfo & afNodeTypes)) continue;
int rgZ = m_pNodes[m_di[j].m_SortedBy[1]].m_Region[2];
if (rgZ > m_maxBoxZ) break;
if (rgZ < m_minBoxZ) continue;
int rgX = m_pNodes[m_di[j].m_SortedBy[1]].m_Region[0];
if (rgX < m_minBoxX) continue;
if (rgX > m_maxBoxX) continue;
CheckNode(vecOrigin, m_di[j].m_SortedBy[1]);
}
}
for (i = min(m_maxZ,halfZ); i >= m_minZ; i--)
{
for (j = m_RangeStart[2][i]; j <= m_RangeEnd[2][i]; j++)
{
if (!(m_pNodes[m_di[j].m_SortedBy[2]].m_afNodeInfo & afNodeTypes)) continue;
int rgX = m_pNodes[m_di[j].m_SortedBy[2]].m_Region[0];
if (rgX > m_maxBoxX) break;
if (rgX < m_minBoxX) continue;
int rgY = m_pNodes[m_di[j].m_SortedBy[2]].m_Region[1];
if (rgY < m_minBoxY) continue;
if (rgY > m_maxBoxY) continue;
CheckNode(vecOrigin, m_di[j].m_SortedBy[2]);
}
}
for (i = max(m_minX,halfX+1); i <= m_maxX; i++)
{
for (j = m_RangeStart[0][i]; j <= m_RangeEnd[0][i]; j++)
{
if (!(m_pNodes[m_di[j].m_SortedBy[0]].m_afNodeInfo & afNodeTypes)) continue;
int rgY = m_pNodes[m_di[j].m_SortedBy[0]].m_Region[1];
if (rgY > m_maxBoxY) break;
if (rgY < m_minBoxY) continue;
int rgZ = m_pNodes[m_di[j].m_SortedBy[0]].m_Region[2];
if (rgZ < m_minBoxZ) continue;
if (rgZ > m_maxBoxZ) continue;
CheckNode(vecOrigin, m_di[j].m_SortedBy[0]);
}
}