-
Notifications
You must be signed in to change notification settings - Fork 9
/
waypoint.cpp
5522 lines (4751 loc) · 170 KB
/
waypoint.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
// This is an open source non-commercial project. Dear PVS-Studio, please check it.
// PVS-Studio Static Code Analyzer for C, C++, C#, and Java: http://www.viva64.com
//
// FoXBot - AI Bot for Halflife's Team Fortress Classic
//
// (http://foxbot.net)
//
// waypoint.cpp
//
// Copyright (C) 2003 - Tom "Redfox" Simpson
//
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
//
// See the GNU General Public License for more details at:
// http://www.gnu.org/copyleft/gpl.html
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//
#include <cstring>
#ifndef __linux__
#include <io.h>
#else
#include <unistd.h>
#endif
#include <sys/stat.h>
#include "extdll.h"
#include "util.h"
#include <enginecallback.h>
#include "bot.h"
#include "waypoint.h"
// linked list class.
#include "list.h"
List<char*> commanders;
int last_area = -1;
bool area_on_last;
// Rocket jumping points
// The array to hold all the RJ waypoints, and the team they belong to.
int RJPoints[MAXRJWAYPOINTS][2];
extern int mod_id;
extern int m_spriteTexture;
extern bool botcamEnabled;
// my externs
extern bool blue_av[8];
extern bool red_av[8];
extern bool green_av[8];
extern bool yellow_av[8];
extern bool attack[4]; // teams attack
extern bool defend[4]; // teams defend
// tracks which waypoint types have been loaded for the current map, team by team
static int wp_type_exists[4];
// waypoints with information bits (flags)
WAYPOINT waypoints[MAX_WAYPOINTS];
// number of waypoints currently in use in the current map
int num_waypoints;
// waypoint author
char waypoint_author[256];
// declare the array of head pointers to the path structures...
// basically an array of linked lists
PATH* paths[MAX_WAYPOINTS];
// time that this waypoint was displayed (while editing)
float wp_display_time[MAX_WAYPOINTS];
static bool g_waypoint_paths = false; // have any paths been allocated?
bool g_waypoint_cache = false;
bool g_waypoint_on = false;
bool g_auto_waypoint = false;
bool g_path_waypoint = false;
bool g_find_waypoint = false;
long g_find_wp = 0;
bool g_path_connect = true;
bool g_path_oneway = false;
bool g_path_twoway = false;
static Vector last_waypoint;
static float f_path_time = 0.0f;
static float a_display_time[MAX_WAYPOINTS];
bool g_area_def;
int wpt1;
int wpt2;
// area defs...
AREA areas[MAX_WAYPOINTS];
int num_areas;
static bool is_junction[MAX_WAYPOINTS];
static unsigned int route_num_waypoints;
unsigned int* shortest_path[4] = { nullptr, nullptr, nullptr, nullptr };
unsigned int* from_to[4] = { nullptr, nullptr, nullptr, nullptr };
static std::FILE* fp;
// FUNCTION PROTOTYPES
static void WaypointFloyds(unsigned int* shortest_path, unsigned int* from_to);
static void WaypointRouteInit();
static bool WaypointLoadVersion4(FILE* bfp, int number_of_waypoints);
static bool WaypointDeleteAimArtifact(const edict_t* pEntity);
void WaypointDebug() {
fp = UTIL_OpenFoxbotLog();
if (fp != nullptr) {
std::fprintf(fp, "WaypointDebug: LINKED LIST ERROR!!!\n");
std::fclose(fp);
}
// int x = x - 1; // x is zero
// int y = y / x; // cause an divide by zero exception
//return;
}
// free the linked list of waypoint path nodes...
void WaypointFree() {
for (PATH *&i : paths) {
#ifdef _DEBUG
int count = 0;
#endif
if (i) {
PATH* p = i;
while (p) // free the linked list
{
PATH* p_next = p->next; // save the link to next
std::free(p);
p = p_next;
#ifdef _DEBUG
count++;
if (count > 1000)
WaypointDebug();
#endif
}
i = nullptr;
}
}
}
// initialize the waypoint structures...
void WaypointInit() {
int i;
// have any waypoint path nodes been allocated yet?
if (g_waypoint_paths)
WaypointFree(); // must free previously allocated path memory
for (i = 0; i < 4; i++) {
// if (shortest_path[i] != NULL)
std::free(shortest_path[i]);
// if (from_to[i] != NULL)
std::free(from_to[i]);
}
// erase the name of the waypoint files author
for (i = 0; i < 255; i++)
waypoint_author[i] = '\0';
for (i = 0; i < MAX_WAYPOINTS; i++) {
waypoints[i].flags = 0;
waypoints[i].script_flags = 0;
waypoints[i].origin = Vector(0, 0, 0);
wp_display_time[i] = 0.0f;
paths[i] = nullptr; // no paths allocated yet
a_display_time[i] = 0.0f;
areas[i].flags = 0;
areas[i].a = Vector(0, 0, 0);
areas[i].b = Vector(0, 0, 0);
areas[i].c = Vector(0, 0, 0);
areas[i].d = Vector(0, 0, 0);
areas[i].namea[0] = '\0';
areas[i].nameb[0] = '\0';
areas[i].namec[0] = '\0';
areas[i].named[0] = '\0';
is_junction[i] = false;
}
f_path_time = 0.0f; // reset waypoint path display time
num_waypoints = 0;
num_areas = 0;
last_waypoint = Vector(0, 0, 0);
for (i = 0; i < 4; i++) {
shortest_path[i] = nullptr;
from_to[i] = nullptr;
}
}
// add a path from one waypoint (add_index) to another (path_index)...
// Returns false on memory allocation failure.
int WaypointAddPath(const short int add_index, const short int path_index) {
// don't do it if its greater than max distance
if ((waypoints[add_index].origin - waypoints[path_index].origin).Length() > REACHABLE_RANGE)
return true;
PATH* p = paths[add_index];
PATH* prev = nullptr;
int i;
#ifdef _DEBUG
int count = 0;
#endif
// find an empty slot for new path_index...
while (p != nullptr) {
i = 0;
while (i < MAX_PATH_INDEX) {
// don't add the path if its already there?!
if (p->index[i] == path_index)
return true;
if (p->index[i] == -1) {
p->index[i] = path_index;
return true;
}
i++;
}
prev = p; // save the previous node in linked list
p = p->next; // go to next node in linked list
#ifdef _DEBUG
count++;
if (count > 100)
WaypointDebug();
#endif
}
// hmmm, should we only do this if prev->next !=NULL
// nope loop only stops when complete...
// ignore this unless future probs arise!
p = static_cast<PATH*>(std::malloc(sizeof(PATH)));
if (p == nullptr) {
ALERT(at_error, "FoXBot - Error, memory allocation failed for waypoint path!");
UTIL_BotLogPrintf("Memory allocation failed for waypoint path!\n");
return false;
}
p->index[0] = path_index;
p->index[1] = -1;
p->index[2] = -1;
p->index[3] = -1;
p->next = nullptr;
if (prev != nullptr)
prev->next = p; // link new node into existing list
if (paths[add_index] == nullptr)
paths[add_index] = p; // save head point if necessary
return true;
}
// delete all paths to this waypoint index...
void WaypointDeletePath(const short int del_index) {
int i;
// search all paths for this index...
for (int index = 0; index < num_waypoints; index++) {
PATH* p = paths[index];
#ifdef _DEBUG
int count = 0;
#endif
// search linked list for del_index...
while (p != nullptr) {
i = 0;
while (i < MAX_PATH_INDEX) {
if (p->index[i] == del_index) {
p->index[i] = -1; // unassign this path
}
i++;
}
p = p->next; // go to next node in linked list
#ifdef _DEBUG
count++;
if (count > 100)
WaypointDebug();
#endif
}
}
}
// delete a path from a waypoint (path_index) to another waypoint
// (del_index)...
void WaypointDeletePath(const short int path_index, const short int del_index) {
PATH* p = paths[path_index];
int i;
#ifdef _DEBUG
int count = 0;
#endif
// search linked list for del_index...
while (p != nullptr) {
i = 0;
while (i < MAX_PATH_INDEX) {
if (p->index[i] == del_index) {
p->index[i] = -1; // unassign this path
}
i++;
}
p = p->next; // go to next node in linked list
#ifdef _DEBUG
count++;
if (count > 100)
WaypointDebug();
#endif
}
}
// find a path from the current waypoint. (pPath MUST be NULL on the
// initial call. subsequent calls will return other paths if they exist.)
int WaypointFindPath(PATH** pPath, int* path_index, const int waypoint_index, const int team) {
int index;
if (*pPath == nullptr) {
*pPath = paths[waypoint_index];
*path_index = 0;
}
if (*path_index == MAX_PATH_INDEX) {
*path_index = 0;
*pPath = (*pPath)->next; // go to next node in linked list
}
#ifdef _DEBUG
int count = 0;
#endif
while (*pPath != nullptr) {
while (*path_index < MAX_PATH_INDEX) {
if ((*pPath)->index[*path_index] != -1) // found a path?
{
// save the return value
index = (*pPath)->index[*path_index];
// skip this path if next waypoint is team specific and
// NOT this team
if (team != -1 && waypoints[index].flags & W_FL_TEAM_SPECIFIC && (waypoints[index].flags & W_FL_TEAM) != team) {
(*path_index)++;
continue;
}
// set up stuff for subsequent calls...
(*path_index)++;
return index;
}
(*path_index)++;
}
*path_index = 0;
*pPath = (*pPath)->next; // go to next node in linked list
#ifdef _DEBUG
count++;
if (count > 100)
WaypointDebug();
#endif
}
return -1;
}
// find the index of the nearest waypoint to the indicated player
// (-1 if not found)
// This function also checks that the waypoint is visible to the specified entity.
int WaypointFindNearest_E(const edict_t* pEntity, const float range, const int team) {
int min_index = -1;
double min_distance_squared = static_cast<double>(range * range) + 0.1;
TraceResult tr;
// bit field of waypoint types to ignore
static constexpr WPT_INT32 ignoreFlags = 0 + (W_FL_DELETED | W_FL_AIMING);
// find the nearest waypoint...
for (int i = 0; i < num_waypoints; i++) {
// skip waypoints we don't want to consider
if (waypoints[i].flags & ignoreFlags)
continue;
// skip this waypoint if it's team specific and teams don't match
if (team != -1 && waypoints[i].flags & W_FL_TEAM_SPECIFIC && (waypoints[i].flags & W_FL_TEAM) != team)
continue;
const Vector distance = waypoints[i].origin - pEntity->v.origin;
const double distance_squared = static_cast<double>(distance.x * distance.x) + static_cast<double>(distance.y * distance.y) + static_cast<double>(distance.z * distance.z);
if (distance_squared < min_distance_squared) {
// if waypoint is visible from current position
// (even behind head)...
UTIL_TraceLine(pEntity->v.origin + pEntity->v.view_ofs, waypoints[i].origin, ignore_monsters, pEntity->v.pContainingEntity, &tr);
if (tr.flFraction >= 1.0f) {
min_index = i;
min_distance_squared = distance_squared;
}
}
}
return min_index;
}
// pick an origin, and find the nearest waypoint to it
// This wont check for visibility.
int WaypointFindNearest_V(const Vector& v_src, const float range, const int team) {
if (num_waypoints < 1)
return -1;
int min_index = -1;
float min_distance = range;
// bit field of waypoint types to ignore
static constexpr WPT_INT32 ignoreFlags = 0 + (W_FL_DELETED | W_FL_AIMING);
// find the nearest waypoint...
for (int index = 0; index < num_waypoints; index++) {
// skip waypoints we don't want to consider
if (waypoints[index].flags & ignoreFlags)
continue;
// skip this waypoint if it's team specific and teams don't match
if (team != -1 && waypoints[index].flags & W_FL_TEAM_SPECIFIC && (waypoints[index].flags & W_FL_TEAM) != team)
continue;
const float distance = (waypoints[index].origin - v_src).Length();
if (distance < min_distance) {
min_index = index;
min_distance = distance;
}
}
return min_index;
}
// Find the nearest waypoint to the source postition and return the index of
// that waypoint.
// If pEntity is not NULL then the waypoint found must be visible to that entity.
// Also, you can specify waypoint flags that you wish to exclude from the search.
int WaypointFindNearest_S(const Vector& v_src, const edict_t* pEntity, const float range, const int team, const WPT_INT32 ignore_flags) {
int min_index = -1;
double min_distance_squared = static_cast<double>(range * range) + 0.1;
TraceResult tr;
// find the nearest waypoint...
for (int index = 0; index < num_waypoints; index++) {
// skip deleted waypoints
if (waypoints[index].flags & W_FL_DELETED || waypoints[index].flags & W_FL_AIMING)
continue;
// skip waypoints we should ignore
if (waypoints[index].flags & ignore_flags)
continue;
// skip this waypoint if it's team specific and teams don't match
if (team != -1 && waypoints[index].flags & W_FL_TEAM_SPECIFIC && (waypoints[index].flags & W_FL_TEAM) != team)
continue;
// square the Manhattan distance
const Vector distance = waypoints[index].origin - v_src;
const double distance_squared = static_cast<double>(distance.x * distance.x) + static_cast<double>(distance.y * distance.y) + static_cast<double>(distance.z * distance.z);
// if it's the nearest found so far
if (distance_squared < min_distance_squared) {
// check if the waypoint is visible from
// the source position or a specified entity
if (pEntity != nullptr)
UTIL_TraceLine(v_src, waypoints[index].origin, dont_ignore_monsters, pEntity->v.pContainingEntity, &tr);
else
UTIL_TraceLine(waypoints[index].origin, v_src, ignore_monsters, nullptr, &tr);
// it is visible, so store it
if (tr.flFraction >= 1.0f) {
min_index = index;
min_distance_squared = distance_squared;
}
}
}
return min_index;
}
// This function is like WaypointFindNearest() but you can specify the
// ideal kind of distance you want for the waypoint returned.
// If no waypoint of suitable distance was found it may return a nearer
// waypoint it found instead.
// If chooseRandom is true then the search starts from a random waypoint.
// Returns -1 if no waypoint was found that is in visible range.
int WaypointFindInRange(const Vector& v_src, const float min_range, const float max_range, const int team, const bool chooseRandom) {
TraceResult tr;
int nextBestWP = -1; // just in case one wasn't found in ideal range
int i = 0;
// pick a random waypoint to start searching from if requested to
if (chooseRandom)
i = RANDOM_LONG(0, num_waypoints - 1);
// bit field of waypoint types to ignore
static constexpr WPT_INT32 ignoreFlags = 0 + (W_FL_DELETED | W_FL_AIMING);
// start the search
for (int waypoints_checked = 0; waypoints_checked < num_waypoints; waypoints_checked++, i++) {
// wrap the search if it exceeds the number of available waypoints
if (i >= num_waypoints)
i = 0;
// skip waypoints we don't want to consider
if (waypoints[i].flags & ignoreFlags)
continue;
// skip this waypoint if it's team specific and teams don't match
if (team != -1 && waypoints[i].flags & W_FL_TEAM_SPECIFIC && (waypoints[i].flags & W_FL_TEAM) != team)
continue;
const float distance = (waypoints[i].origin - v_src).Length();
// if this waypoint is within range
if (distance < max_range) {
UTIL_TraceLine(waypoints[i].origin, v_src, ignore_monsters, nullptr, &tr);
// if the source is visible from this waypoint
if (tr.flFraction >= 1.0f) {
// a cool laser effect (for debugging purposes)
// WaypointDrawBeam(INDEXENT(1), waypoints[i].origin,
// v_src, 10, 2, 50, 250, 50, 200, 10);
// it's within the ideal range, purr-fect
if (distance > min_range)
return i;
// remember this waypoint as a backup
nextBestWP = i;
}
}
}
// no waypoint was found in ideal range, use the backup instead
if (nextBestWP != -1)
return nextBestWP;
return -1; // none found
}
// find the goal nearest to the source waypoint matching the "flags" bits
// and within range and return the index of that waypoint...
int WaypointFindNearestGoal(const int srcWP, const int team, int range, const WPT_INT32 flags) {
// sanity check
if (num_waypoints < 1 || srcWP < 0 || srcWP >= num_waypoints) {
// UTIL_BotLogPrintf("bad srcWP\n");
return -1;
}
int min_index = -1;
// find the nearest waypoint with the matching flags...
for (int index = 0; index < num_waypoints; index++) {
if (flags == 0) // was a plain waypoint with no flags requested?
{
if (waypoints[index].flags != 0)
continue;
}
else if (!(waypoints[index].flags & flags))
continue; // skip this waypoint if the flags don't match
// skip any deleted waypoints or aiming waypoints
if (waypoints[index].flags & W_FL_DELETED || waypoints[index].flags & W_FL_AIMING)
continue;
// Skip unavailable waypoints.
if (!WaypointAvailable(index, team))
continue;
if (index == srcWP)
continue; // skip the source waypoint
const int distance = WaypointDistanceFromTo(srcWP, index, team);
if (distance < range && distance > 0) {
min_index = index;
range = distance;
}
}
return min_index;
}
#if FALSE // this function is currently unused
// find the goal nearest to the source position (v_src) matching the
// "flags" bits and return the index of that waypoint...
// Returns -1 if one wasn't found
int WaypointFindNearestGoal_R(Vector v_src, edict_t* pEntity,
const float range, const int team, const WPT_INT32 flags)
{
// sanity check
if (num_waypoints < 1)
return -1;
int index;
int min_index = -1;
float distance;
float min_distance = range;
// find the nearest waypoint with the matching flags...
for (index = 0; index < num_waypoints; index++)
{
if (flags == 0) // was a plain waypoint with no flags requested?
{
if (waypoints[index].flags != 0)
continue;
}
else if (!(waypoints[index].flags & flags))
continue; // skip this waypoint if the flags don't match
// skip any deleted waypoints or aiming waypoints
if (waypoints[index].flags & W_FL_DELETED
|| waypoints[index].flags & W_FL_AIMING)
continue;
// Skip unavailable waypoints.
if (!WaypointAvailable(index, team))
continue;
distance = (waypoints[index].origin - v_src).Length();
if (distance < min_distance)
{
min_index = index;
min_distance = distance;
}
}
return min_index;
}
#endif
// find a random goal matching the "flags" bits and return the index of
// that waypoint...
// If source_WP is not -1 then this will make sure a path exists to the
// waypoint it returns.
int WaypointFindRandomGoal(const int source_WP, const int team, const WPT_INT32 flags) {
if (num_waypoints < 1 || source_WP < 0 || source_WP >= num_waypoints)
return -1;
static int indexes[50]; // made static for speed reasons
int count = 0;
// start from a random waypoint in case there are more matching waypoints
// than this function is ready to keep a list of
int index = RANDOM_LONG(0, num_waypoints - 1);
// find all the waypoints with the matching flags...
for (int i = 0; i < num_waypoints; i++, index++) {
// wrap the search if it exceeds the number of available waypoints
if (index >= num_waypoints)
index = 0;
if (flags == 0) // was a plain waypoint with no flags requested?
{
if (waypoints[index].flags != 0)
continue;
}
else if (!(waypoints[index].flags & flags))
continue; // skip this waypoint if the flags don't match
// skip any deleted or aiming waypoints
if (waypoints[index].flags & W_FL_DELETED || waypoints[index].flags & W_FL_AIMING)
continue;
// need to skip wpt if not available to the bots team
// also - make sure a route exists to this waypoint
if (!WaypointAvailable(index, team) || WaypointRouteFromTo(source_WP, index, team) == -1)
continue;
// found a suitable waypoint
indexes[count] = index;
++count;
if (count >= 50)
break; // we have filled the list
}
if (count == 0) // no matching waypoints found
return -1;
index = RANDOM_LONG(1, count) - 1;
return indexes[index];
}
// find a random goal within range matching the "flags" bits and return
// the index of that waypoint...
// This function will make sure a path exists to the waypoint it returns.
int WaypointFindRandomGoal_D(const int source_WP, const int team, const int range, const WPT_INT32 flags) {
if (num_waypoints < 1 || source_WP < 0 || source_WP >= num_waypoints)
return -1;
static int indexes[50]; // made static for speed reasons
int count = 0;
// start from a random waypoint in case there are more matching waypoints
// than this function is ready to keep a list of
int index = RANDOM_LONG(0, num_waypoints - 1);
// find all the waypoints with the matching flags...
for (int i = 0; i < num_waypoints; i++, index++) {
// wrap the search if it exceeds the number of available waypoints
if (index >= num_waypoints)
index = 0;
if (flags == 0) // was a plain waypoint with no flags requested?
{
if (waypoints[index].flags != 0)
continue;
}
else if (!(waypoints[index].flags & flags))
continue; // skip this waypoint if the flags don't match
// skip any deleted or aiming waypoints
if (waypoints[index].flags & W_FL_DELETED || waypoints[index].flags & W_FL_AIMING)
continue;
if (index == source_WP)
continue; // we're looking for a new waypoint, duh!
const int routeDistance = WaypointDistanceFromTo(source_WP, index, team);
// need to skip wpt if not available to the bots team
// or it's too far away
if (routeDistance == -1 || routeDistance > range || !WaypointAvailable(index, team))
continue;
// found a suitable waypoint
indexes[count] = index;
++count;
if (count >= 50)
break; // we have filled the list
}
if (count == 0) // no matching waypoints found
return -1;
index = RANDOM_LONG(1, count) - 1;
return indexes[index];
}
// find a random goal within a range of a position (v_src) matching the
// "flags" bits and return the index of that waypoint...
int WaypointFindRandomGoal_R(const Vector& v_src, const bool checkVisibility, const float range, const int team, const WPT_INT32 flags) {
if (num_waypoints < 1)
return -1;
static int indexes[50]; // made static for speed reasons
int count = 0;
// start from a random waypoint in case there are more matching waypoints
// than this function is ready to remember and choose from
int index = RANDOM_LONG(0, num_waypoints - 1);
TraceResult tr;
// find all the waypoints with the matching flags...
for (int i = 0; i < num_waypoints; i++, index++) {
// wrap the search if it exceeds the number of available waypoints
if (index >= num_waypoints)
index = 0;
if (flags == 0) // was a plain waypoint with no flags requested?
{
if (waypoints[index].flags != 0)
continue;
}
else if (!(waypoints[index].flags & flags))
continue; // skip this waypoint if the flags don't match
// skip any deleted or aiming waypoints
if (waypoints[index].flags & W_FL_DELETED || waypoints[index].flags & W_FL_AIMING)
continue;
// need to skip wpt if not available to bots team
if (!WaypointAvailable(index, team))
continue;
if (VectorsNearerThan(waypoints[index].origin, v_src, range)) {
if (checkVisibility)
UTIL_TraceLine(v_src, waypoints[index].origin, ignore_monsters, nullptr, &tr);
if (!checkVisibility || tr.flFraction >= 1.0f) {
indexes[count] = index;
++count;
if (count >= 50)
break; // we have filled the list
}
}
}
if (count == 0) // no matching waypoints found
return -1;
index = RANDOM_LONG(1, count) - 1;
return indexes[index];
}
// This function attempts to find a randomly selected detpack waypoint
// that is suitable for setting a detpack on.
// It will return -1 on failure to find such a waypoint.
int WaypointFindDetpackGoal(const int srcWP, const int team) {
if (num_waypoints < 1 || srcWP == -1)
return -1;
int WP_list[10];
int total = 0;
// find all the waypoints with the matching flags...
for (int index = 0; index < num_waypoints; index++) {
if (!(waypoints[index].flags & W_FL_TFC_DETPACK_CLEAR) && !(waypoints[index].flags & W_FL_TFC_DETPACK_SEAL))
continue; // skip this waypoint if the flags don't match
// skip any deleted or aiming waypoints
if (waypoints[index].flags & W_FL_DELETED || waypoints[index].flags & W_FL_AIMING)
continue;
// skip it if not available to the bots team
// also - make sure a route exists to this waypoint
if (!WaypointAvailable(index, team) || WaypointRouteFromTo(srcWP, index, team) == -1)
continue;
WP_list[total] = index;
++total;
if (total > 8)
break;
}
// pick waypoints out of the list randomly until we find a suitable one
if (total > 0) {
while (total > 0) {
// pick a waypoint from the list at random
const int rand_index = RANDOM_LONG(0, total - 1);
// check if it needs clearing
if (waypoints[WP_list[rand_index]].flags & W_FL_TFC_DETPACK_CLEAR && DetpackClearIsBlocked(WP_list[rand_index]))
return WP_list[rand_index];
// check if it needs sealing
if (waypoints[WP_list[rand_index]].flags & W_FL_TFC_DETPACK_SEAL && DetpackSealIsClear(WP_list[rand_index]))
return WP_list[rand_index];
--total; // shrink the list
// this detpack waypoint is unsuitable
// replace it with the last waypoint in the list
WP_list[rand_index] = WP_list[total];
}
}
return -1;
}
// This function returns true if a specified W_FL_TFC_DETPACK_CLEAR waypoint
// needs detpacking to blast it open.
bool DetpackClearIsBlocked(const int index) {
TraceResult tr;
// start checking
const PATH* p = paths[index];
int path_total = 0;
while (p != nullptr) {
for (const short i : p->index) {
// test for an obstruction
if (i != -1) {
++path_total;
UTIL_TraceLine(waypoints[index].origin, waypoints[i].origin, ignore_monsters, nullptr, &tr);
if (tr.flFraction < 1.0f)
return true; // a path is blocked by something
}
}
p = p->next; // go to next node in linked list
}
// return true only if no blockages were found
// and there was less than two paths from the waypoint
// this test is done to maintain backwards compatibility with old maps
// from before Foxbot 0.76 that only used one type of detpack waypoint tag
if (path_total < 2)
return true;
return false; // waypoint is unsuitable for detpacking
}
// This function returns true if a specified W_FL_TFC_DETPACK_SEAL waypoint
// needs detpacking to blast it closed.
bool DetpackSealIsClear(const int index) {
TraceResult tr;
// start checking
const PATH* p = paths[index];
while (p != nullptr) {
for (const short i : p->index) {
// test for an obstruction
if (i != -1) {
UTIL_TraceLine(waypoints[index].origin, waypoints[i].origin, ignore_monsters, nullptr, &tr);
if (tr.flFraction < 1.0f)
return false; // a path is blocked by something
}
}
p = p->next; // go to next node in linked list
}
// all paths are clear - waypoint is suitable for detpacking
return true;
}
// find the nearest "special" aiming waypoint (for sniper aiming)...
// Returns -1 if one wasn't found
int WaypointFindNearestAiming(const Vector& r_v_origin) {
if (num_waypoints < 1)
return -1;
int min_index = -1;
float min_distance = 200.0f;
// search for nearby aiming waypoint...
for (int index = 0; index < num_waypoints; index++) {
if ((waypoints[index].flags & W_FL_AIMING) == 0)
continue; // skip any NON aiming waypoints
if (waypoints[index].flags & W_FL_DELETED)
continue; // skip any deleted waypoints
// ignore distant waypoints(most aim waypoints will be too far away)
if (!VectorsNearerThan(r_v_origin, waypoints[index].origin, 200.0))
continue;
// find the nearest of the near
const float distance = (r_v_origin - waypoints[index].origin).Length();
if (distance < min_distance) {
min_index = index;
min_distance = distance;
}
}