-
Notifications
You must be signed in to change notification settings - Fork 21
/
waypoint.cpp
2940 lines (2257 loc) · 81.1 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
//
// JK_Botti - be more human!
//
// waypoint.cpp
//
#ifndef _WIN32
#include <string.h>
#endif
#ifndef __linux__
#include <io.h>
#endif
#include <fcntl.h>
#ifndef __linux__
#include <sys/stat.h>
#else
#include <string.h>
#include <sys/stat.h>
#endif
#include <malloc.h>
#include <extdll.h>
#include <dllapi.h>
#include <h_export.h>
#include <meta_api.h>
#include "bot.h"
#include "waypoint.h"
#include "bot_weapons.h"
#include "player.h"
#include "zlib/zlib.h"
extern int m_spriteTexture;
// waypoints with information bits (flags)
WAYPOINT waypoints[MAX_WAYPOINTS];
// number of waypoints currently in use
int num_waypoints;
// declare the array of head pointers to the path structures...
PATH paths[MAX_WAYPOINTS];
// time that this waypoint was displayed (while editing)
float wp_display_time[MAX_WAYPOINTS];
// Collect max half of total waypoints
WAYPOINT spawnpoints[MAX_WAYPOINTS / 2];
int num_spawnpoints = 0;
// memory storage for matrix arrays
unsigned short waypoint_matrix_mem_array_shortest_path[MAX_WAYPOINT_MATRIX];
unsigned short waypoint_matrix_mem_array_from_to_array[MAX_WAYPOINT_MATRIX];
// debuging settings
qboolean g_waypoint_on = FALSE;
qboolean g_path_waypoint = FALSE;
//
static Vector block_list[MAX_WAYPOINTS];
static int block_list_endlist = 0;
static const int block_list_size = MAX_WAYPOINTS;
//
qboolean g_waypoint_paths = FALSE; // have any paths been allocated?
qboolean g_auto_waypoint = TRUE;
qboolean g_path_waypoint_enable = TRUE;
qboolean g_waypoint_updated = FALSE;
qboolean g_waypoint_testing = FALSE;
float f_path_time = 0.0;
int g_lifts_added = 0;
unsigned int route_num_waypoints;
unsigned short *shortest_path = NULL;
unsigned short *from_to = NULL;
BOOL wp_matrix_initialized = FALSE;
BOOL wp_matrix_save_on_mapend = FALSE;
//
static qboolean WaypointReachable(const Vector &v_src, const Vector &v_dest, const int reachable_flags);
static void WaypointSlowFloydsStop(void);
static void WaypointRouteInit(qboolean ForceRebuild);
//
static qboolean WaypointIsRouteValid(int src, int dest)
{
if(unlikely(!wp_matrix_initialized) ||
unlikely(from_to == NULL) ||
unlikely(src < 0) ||
unlikely(dest < 0) ||
unlikely(src >= (int)route_num_waypoints) ||
unlikely(dest >= (int)route_num_waypoints))
return(FALSE);
return(TRUE);
}
//
static void WaypointAddBlock(const Vector &origin)
{
if(block_list_endlist == block_list_size)
{
return;
}
block_list[block_list_endlist++] = origin;
}
//
static qboolean WaypointInBlockRadius(const Vector &origin)
{
TraceResult tr;
for(int i = 0; i < block_list_endlist; i++)
{
// if distance is enough, don't block
if((block_list[i] - origin).Length() > 800.0f)
continue;
// within block distance check if visible
UTIL_TraceMove( block_list[i], origin, ignore_monsters, NULL, &tr );
// block visible
if(tr.fStartSolid || tr.fAllSolid || tr.flFraction > 0.999999f)
return(TRUE);
}
return(FALSE);
}
#ifdef _DEBUG
static void WaypointDebug(void)
{
int y = 1, x = 1;
FILE *fp=fopen("jk_botti.txt","a");
fprintf(fp,"WaypointDebug: LINKED LIST ERROR!!!\n");
fclose(fp);
x = x - 1; // x is zero
y = y / x; // cause an divide by zero exception
return;
}
#endif
static inline int GetReachableFlags(int i1, int i2)
{
return((waypoints[i1].flags | waypoints[i2].flags) & (W_FL_LADDER | W_FL_CROUCH));
}
static inline int GetReachableFlags(edict_t * pEntity, int i1)
{
int flags = waypoints[i1].flags & (W_FL_LADDER | W_FL_CROUCH);
if(FNullEnt(pEntity))
return(flags);
if(pEntity->v.flags & FL_DUCKING)
flags |= W_FL_CROUCH;
if(pEntity->v.movetype == MOVETYPE_FLY)
flags |= W_FL_LADDER;
return(flags);
}
static inline int GetReachableFlags(int i1, edict_t * pEntity)
{
return(GetReachableFlags(pEntity, i1));
}
// free the linked list of waypoint path nodes...
static void WaypointFree(void)
{
// no linked list anymore
for(int i=0; i < MAX_WAYPOINTS; i++)
{
memset(&paths[i], 0, sizeof(paths[i]));
}
}
// initialize the waypoint structures...
void WaypointInit(void)
{
int i;
// have any waypoint path nodes been allocated yet?
if (g_waypoint_paths)
WaypointFree(); // must free previously allocated path memory
shortest_path = NULL;
from_to = NULL;
wp_matrix_initialized = FALSE;
memset(waypoints, 0, sizeof(waypoints));
for (i=0; i < MAX_WAYPOINTS; i++)
{
wp_display_time[i] = 0.0;
memset(&paths[i], 0, sizeof(paths[i])); // no paths allocated yet
}
f_path_time = 0.0; // reset waypoint path display time
num_waypoints = 0;
for(i = 0; i < 32; i++)
players[i].last_waypoint = -1;
memset(&spawnpoints, 0, sizeof(spawnpoints));
num_spawnpoints = 0;
memset(block_list, 0, sizeof(block_list));
block_list_endlist = 0;
g_lifts_added = 0;
}
static void WaypointAddPath(short int add_index, short int path_index)
{
int i;
if((waypoints[add_index].flags | waypoints[path_index].flags) & W_FL_DELETED)
{
UTIL_ConsolePrintf("%s", "Tried to add path to deleted waypoint!");
return;
}
PATH &p = paths[add_index];
for(i = 0; i < p.last_idx_used; i++)
{
if(p.index[i] == -1 || p.index[i] == path_index)
{
p.index[i] = path_index;
return;
}
#ifdef _DEBUG
count++;
if (count > 100) WaypointDebug();
#endif
}
if(i >= MAX_PATH_INDEX)
return;
p.index[p.last_idx_used++] = path_index;
}
static void WaypointDeletePath(short int path_index, short int del_index)
{
int i;
PATH &p = paths[path_index];
for(i = 0; i < p.last_idx_used; i++)
{
if(p.index[i] == del_index)
p.index[i] = -1;
#ifdef _DEBUG
count++;
if (count > 100) WaypointDebug();
#endif
}
}
static void WaypointDeletePath(short int del_index)
{
int i;
// search all paths for this index...
for (i=0; i < num_waypoints; i++)
WaypointDeletePath(i, del_index);
}
// 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(int &path_index, int waypoint_index)
{
PATH &p = paths[waypoint_index];
while(++path_index < p.last_idx_used)
if(p.index[path_index] != -1)
return(p.index[path_index]);
return -1;
}
// calculate all the paths to this waypoint
static void WaypointMakePathsWith(int index)
{
// calculate all the paths to this new waypoint
if(!g_path_waypoint_enable)
return;
for (int i=0; i < num_waypoints; i++)
{
if (i == index)
continue; // skip the waypoint that was just added
if (waypoints[i].flags & (W_FL_DELETED | W_FL_AIMING))
continue; // skip any aiming waypoints
// don't allow paths TO lift-end
if(!(waypoints[i].flags & W_FL_LIFT_END))
{
// check if the waypoint is reachable from the new one (one-way)
if ( WaypointReachable(waypoints[index].origin, waypoints[i].origin, GetReachableFlags(index, i)))
{
WaypointAddPath(index, i);
}
}
// don't allow paths TO lift-end
if(!(waypoints[index].flags & W_FL_LIFT_END))
{
// check if the new one is reachable from the waypoint (other way)
if ( WaypointReachable(waypoints[i].origin, waypoints[index].origin, GetReachableFlags(i, index)))
{
WaypointAddPath(i, index);
}
}
}
}
// Called when map-objects spawn
void CollectMapSpawnItems(edict_t *pSpawn)
{
const char *classname = (const char *)STRING(pSpawn->v.classname);
int flags = 0;
int i;
Vector SpawnOrigin;
int itemflag = 0;
static Vector CheckOffset;
static int wall_count, wall_updated;
static BOOL offset_set = FALSE;
static const Vector offsets[8] = { Vector(60, 0, 0), Vector(0, 60, 0), Vector(-60, 0, 0), Vector(0, -60, 0), Vector(42, 42, 0), Vector(42, -42, 0), Vector(-42, 42, 0), Vector(-42, -42, 0) };
if(num_spawnpoints >= MAX_WAYPOINTS / 2)
return;
Vector EntOrigin = pSpawn->v.origin;
if (strncmp("item_health", classname, 11) == 0)
flags |= W_FL_HEALTH;
else if (strncmp("item_battery", classname, 12) == 0)
flags |= W_FL_ARMOR;
else if ((itemflag = GetAmmoItemFlag(classname)) != 0)
flags |= W_FL_AMMO;
else if ((itemflag = GetWeaponItemFlag(classname)) != 0 && (pSpawn->v.owner == NULL))
flags |= W_FL_WEAPON;
else if (strcmp("item_longjump", classname) == 0)
flags |= W_FL_LONGJUMP;
else
{
// check for func_recharge and func_healthcharger
if(strncmp("func_recharge", classname, 13) == 0 || strncmp("func_healthcharger", classname, 18) == 0)
{
flags |= (classname[5] == 'r') ? W_FL_ARMOR : W_FL_HEALTH;
if(offset_set)
{
// check if there is room around charger
EntOrigin = VecBModelOrigin(pSpawn) + CheckOffset;
}
else
{
wall_count = 0;
wall_updated = 0;
// check if there is room around charger
for(int i = 0; i < 8; i++)
{
CheckOffset = offsets[i];
offset_set = TRUE;
CollectMapSpawnItems(pSpawn);
offset_set = FALSE;
}
//UTIL_ConsolePrintf("Total %d waypoints add around %s and %d waypoints updated with new flags\n", wall_count, classname, wall_updated);
return;
}
}
else
return;
}
// Trace item on ground
TraceResult tr;
UTIL_TraceHull( EntOrigin + Vector(0, 0, 36 / 2), EntOrigin - Vector( 0, 0, 10240 ),
ignore_monsters, head_hull, pSpawn->v.pContainingEntity, &tr );
//stuck in world?
if(tr.fStartSolid || tr.fAllSolid)
{
//UTIL_ConsolePrintf("stuck in world\n");
return;
}
SpawnOrigin = tr.vecEndPos - Vector(0, 0, 36 / 2) + Vector(0, 0, 72 / 2);
// Check if there is enough space to stand
TraceResult tr2;
UTIL_TraceHull( SpawnOrigin, SpawnOrigin + Vector(0, 0, 1),
ignore_monsters, human_hull, pSpawn->v.pContainingEntity, &tr2 );
if(tr2.fStartSolid || tr2.flFraction < 1.0f)
{
// Not enough space, make this duck waypoint
SpawnOrigin = tr.vecEndPos;
flags |= W_FL_CROUCH;
//UTIL_ConsolePrintf("adding crouch waypoint\n");
}
// Check if any spawnpoints near already (distance 50.0)
for(i = 0; i < num_spawnpoints; i++)
{
if((flags & W_FL_CROUCH) != (spawnpoints[i].flags & W_FL_CROUCH))
continue;
if((spawnpoints[i].origin - SpawnOrigin).Length() < 50.0)
{
spawnpoints[i].flags |= flags;
spawnpoints[i].itemflags |= itemflag;
wall_updated++;
return;
}
}
// Add new
spawnpoints[num_spawnpoints].origin = SpawnOrigin;
spawnpoints[num_spawnpoints].flags = flags | W_FL_SPAWNADD;
spawnpoints[num_spawnpoints].itemflags = itemflag;
num_spawnpoints++;
wall_count++;
}
//
void WaypointAddSpawnObjects(void)
{
int index;
int i,j;
qboolean done;
int count = 0;
int flags_updated = 0;
int itemflags_updated = 0;
if (!g_auto_waypoint || num_spawnpoints == 0)
return;
for(i = 0; i < num_spawnpoints; i++)
{
done = FALSE;
// Check if there is waypoint near already
for(j = 0; j < num_waypoints; j++) {
if (!(waypoints[j].flags & (W_FL_DELETED | W_FL_AIMING)))
{
if((waypoints[j].origin - spawnpoints[i].origin).Length() < 50.0)
{
// just add flags and return
if(waypoints[j].flags != spawnpoints[i].flags)
flags_updated++;
if(waypoints[j].itemflags != spawnpoints[i].itemflags)
itemflags_updated++;
waypoints[j].flags |= spawnpoints[i].flags;
waypoints[j].itemflags |= spawnpoints[i].itemflags;
done = TRUE;
}
}
}
if(done)
continue;
index = 0;
// find the next available slot for the new waypoint...
while (index < num_waypoints)
{
if (waypoints[index].flags & W_FL_DELETED)
break;
index++;
}
if (index >= MAX_WAYPOINTS) // out of space
break;
waypoints[index].flags = spawnpoints[i].flags;
waypoints[index].itemflags = spawnpoints[i].itemflags;
// store the origin (location) of this waypoint
waypoints[index].origin = spawnpoints[i].origin;
// set the time that this waypoint was originally displayed...
wp_display_time[index] = 0.0;
// increment total number of waypoints if adding at end of array...
if (index == num_waypoints)
num_waypoints++;
count++;
// calculate all the paths to this new waypoint
WaypointMakePathsWith(index);
}
if(count || flags_updated || itemflags_updated)
{
UTIL_ConsolePrintf("Added total %d map-object based waypoints and updated flags for %d!\n", count, flags_updated + itemflags_updated);
g_waypoint_updated = !!count;
}
num_spawnpoints = 0;
memset(&spawnpoints, 0, sizeof(spawnpoints));
if(g_lifts_added || count)
{
WaypointSlowFloydsStop();
shortest_path = NULL;
from_to = NULL;
wp_matrix_initialized = FALSE;
WaypointRouteInit(TRUE);
}
}
//
void WaypointAddLift(edict_t * pent, const Vector &start, const Vector &end)
{
int index_start, index_end;
if(end.z - start.z < 16.0f)
return;
TraceResult tr;
Vector origin = VecBModelOrigin(pent);
Vector move = end - start;
// test if this is trap
Vector stand_start = Vector(origin.x, origin.y, pent->v.absmax.z + 72/2);
Vector stand_end = stand_start + move;
UTIL_TraceDuck(stand_start, stand_end, ignore_monsters, NULL, &tr);
if(tr.fAllSolid || tr.fStartSolid || tr.flFraction < 1.0f)
return;
// Detect if waypoints already exists for this lift
for(int i = 0; i < num_waypoints; i++)
{
if(waypoints[i].flags & W_FL_DELETED)
continue;
//check start point
if(waypoints[i].flags & W_FL_LIFT_START)
{
if((stand_start - waypoints[i].origin).Length() < 100.0f)
return;
}
//check end point
if(waypoints[i].flags & W_FL_LIFT_END)
{
if((stand_end - waypoints[i].origin).Length() < 100.0f)
return;
}
}
// Get two free waypoint indexes
index_start = 0;
index_end = 0;
// find the next available slot for the new waypoint...
while (index_start < num_waypoints)
{
if (waypoints[index_start].flags & W_FL_DELETED)
break;
index_start++;
}
if (index_start >= MAX_WAYPOINTS) // out of space
return;
// increment total number of waypoints if adding at end of array...
if (index_start == num_waypoints)
num_waypoints++;
// find the next available slot for the new waypoint...
while (index_end < num_waypoints)
{
if ((waypoints[index_end].flags & W_FL_DELETED) && index_end != index_start)
break;
index_end++;
}
if (index_end >= MAX_WAYPOINTS) // out of space
return;
// increment total number of waypoints if adding at end of array...
if (index_end == num_waypoints)
num_waypoints++;
// Add waypoints (W_FL_LIFT_START -> W_FL_LIFT_END)
waypoints[index_start].origin = stand_start;
waypoints[index_start].flags = W_FL_LIFT_START;
waypoints[index_start].itemflags = 0;
waypoints[index_end].origin = stand_end;
waypoints[index_end].flags = W_FL_LIFT_END;
waypoints[index_end].itemflags = 0;
// Add one way path
WaypointAddPath(index_start, index_end);
// Make path links with start point
WaypointMakePathsWith(index_start);
// Move lift to upper position and make path links with end point
SET_ORIGIN(pent, end);
WaypointMakePathsWith(index_end);
SET_ORIGIN(pent, start);
// done
g_lifts_added++;
g_waypoint_updated = TRUE;
if(0)
{
UTIL_ConsolePrintf("%s\n", STRING(pent->v.classname));
UTIL_ConsolePrintf(" - start : %4.1f, %4.1f, %4.1f", start.x, start.y, start.z);
UTIL_ConsolePrintf(" - end : %4.1f, %4.1f, %4.1f", end.x, end.y, end.z);
UTIL_ConsolePrintf(" - move : %4.1f, %4.1f, %4.1f", move.x, move.y, move.z);
UTIL_ConsolePrintf(" - origin: %4.1f, %4.1f, %4.1f\n", origin.x, origin.y, origin.z);
}
}
// find the nearest waypoint to the source postition and return the index
int WaypointFindNearest(const Vector &v_origin, const Vector &v_offset, edict_t *pEntity, float range, qboolean b_traceline)
{
int index, min_index;
float distance;
float min_distance;
TraceResult tr;
if (num_waypoints < 1)
return -1;
// find the nearest waypoint...
min_index = -1;
min_distance = 99999.0;
for (index=0; index < num_waypoints; index++)
{
if (waypoints[index].flags & W_FL_DELETED)
continue; // skip any deleted waypoints
if (waypoints[index].flags & W_FL_AIMING)
continue; // skip any aiming waypoints
distance = (waypoints[index].origin - v_origin).Length();
if ((distance < min_distance) && (distance < range))
{
// if waypoint is visible from source position...
if(b_traceline)
UTIL_TraceLine( v_origin + v_offset, waypoints[index].origin, ignore_monsters, pEntity->v.pContainingEntity, &tr );
else
UTIL_TraceMove( v_origin + v_offset, waypoints[index].origin, ignore_monsters, pEntity->v.pContainingEntity, &tr );
if (tr.flFraction > 0.999999f)
{
min_index = index;
min_distance = distance;
}
}
}
return min_index;
}
//
int WaypointFindNearestGoal(edict_t *pEntity, int src, int flags, int itemflags, int exclude[], float range, const Vector *pv_src)
{
int index, min_index;
float distance, min_distance;
if (num_waypoints < 1)
return -1;
// find the nearest waypoint with the matching flags...
min_index = -1;
if(pv_src != NULL)
{
JKASSERT(src != -1);
min_distance = 99999;
}
else
min_distance = WAYPOINT_UNREACHABLE;
for (index=0; index < num_waypoints; index++)
{
if (index == src)
continue; // skip the source waypoint
if (waypoints[index].flags & (W_FL_DELETED | W_FL_AIMING))
continue; // skip any deleted && aiming waypoints
if (flags && !(waypoints[index].flags & flags))
continue; // skip this waypoint if the flags don't match
if (itemflags && !(waypoints[index].itemflags & itemflags))
continue; // skip this weapoint if no match for itemflags
if (exclude != NULL)
{
int exclude_index = 0;
while (exclude[exclude_index])
{
if (index == exclude[exclude_index])
break; // found a match, break out of while loop
exclude_index++;
}
if (index == exclude[exclude_index])
continue; // skip any index that matches exclude list
}
if (waypoints[index].flags & (W_FL_LONGJUMP | W_FL_HEALTH | W_FL_ARMOR | W_FL_AMMO | W_FL_WEAPON))
{
edict_t *wpt_item = WaypointFindItem(index);
if ((wpt_item == NULL) || (wpt_item->v.effects & EF_NODRAW) || (wpt_item->v.frame > 0))
continue;
}
if (pv_src != NULL)
distance = (waypoints[index].origin - *pv_src).Length();
else
distance = WaypointDistanceFromTo(src, index);
if (range > 0.0f && distance >= range)
continue;
if (distance < min_distance)
{
min_index = index;
min_distance = distance;
}
}
return min_index;
}
//
int WaypointFindRandomGoal(int *out_indexes, int max_indexes, edict_t *pEntity, int flags, int itemflags, int exclude[])
{
int index;
int * indexes;
int count = 0;
if (num_waypoints < 1 || max_indexes <= 0 || out_indexes == NULL)
return 0;
indexes = (int*)alloca(sizeof(int)*num_waypoints);
// find all the waypoints with the matching flags...
for (index=0; index < num_waypoints; index++)
{
if (waypoints[index].flags & (W_FL_DELETED | W_FL_AIMING))
continue; // skip any deleted&aiming waypoints
if (flags && !(waypoints[index].flags & flags))
continue; // skip this waypoint if the flags don't match
if (itemflags && !(waypoints[index].itemflags & itemflags))
continue; // skip this weapoint if no match for itemflags
if (exclude != NULL)
{
int exclude_index = 0;
while (exclude[exclude_index])
{
if (index == exclude[exclude_index])
break; // found a match, break out of while loop
exclude_index++;
}
if (index == exclude[exclude_index])
continue; // skip any index that matches exclude list
}
if (waypoints[index].flags & (W_FL_LONGJUMP | W_FL_HEALTH | W_FL_ARMOR | W_FL_AMMO | W_FL_WEAPON))
{
edict_t *wpt_item = WaypointFindItem(index);
if ((wpt_item == NULL) || (wpt_item->v.effects & EF_NODRAW) || (wpt_item->v.frame > 0))
continue;
}
if (count < num_waypoints)
{
indexes[count] = index;
count++;
}
}
if (count == 0) // no matching waypoints found
return 0;
if (count <= max_indexes)
{
// simplest case, we just enough or less indexes to give
for(int i = 0; i < count; i++)
out_indexes[i] = indexes[i];
return(count);
}
// we have extra.. take randomly
int out_count = 0;
for(int i = 0; i < max_indexes; i++)
out_indexes[out_count] = indexes[RANDOM_LONG2(0, count - 1)];
return max_indexes;
}
// find the nearest waypoint to the source postition and return the index
int WaypointFindRunawayPath(int runner, int enemy)
{
int index, max_index;
float runner_distance;
float enemy_distance;
float max_difference;
float difference;
TraceResult tr;
if (num_waypoints < 1)
return -1;
// find the nearest waypoint...
max_difference = 0.0;
max_index = -1;
for (index=0; index < num_waypoints; index++)
{
if (waypoints[index].flags & (W_FL_DELETED|W_FL_AIMING))
continue; // skip any deleted&aiming waypoints
runner_distance = WaypointDistanceFromTo(runner, index);
enemy_distance = WaypointDistanceFromTo(runner, index);
if(runner_distance == WAYPOINT_MAX_DISTANCE)
continue;
difference = enemy_distance - runner_distance;
if(difference > max_difference)
{
max_difference = difference;
max_index = index;
}
}
return max_index;
}
//
#if 0
static int WaypointFindNearestAiming(Vector v_origin)
{
int index;
int min_index = -1;
float min_distance = 99999.0;
float distance;
if (num_waypoints < 1)
return -1;
// search for nearby aiming waypoint...
for (index=0; index < num_waypoints; index++)
{
if (waypoints[index].flags & W_FL_DELETED)
continue; // skip any deleted waypoints
if (!(waypoints[index].flags & W_FL_AIMING))
continue; // skip any NON aiming waypoints
distance = (v_origin - waypoints[index].origin).Length();
if ((distance < min_distance) && (distance < 40))
{
min_index = index;
min_distance = distance;
}
}
return min_index;
}
#endif
//
static void WaypointDrawBeam(edict_t *pEntity, const Vector &start, const Vector &end, int width,
int noise, int red, int green, int blue, int brightness, int speed)
{
// should waypoints be visible?
if(!g_waypoint_on)
return;
MESSAGE_BEGIN(FNullEnt(pEntity)?MSG_ALL:MSG_ONE, SVC_TEMPENTITY, NULL, pEntity);
WRITE_BYTE( TE_BEAMPOINTS);
WRITE_COORD(start.x);
WRITE_COORD(start.y);
WRITE_COORD(start.z);
WRITE_COORD(end.x);
WRITE_COORD(end.y);
WRITE_COORD(end.z);
WRITE_SHORT( m_spriteTexture );
WRITE_BYTE( 1 ); // framestart
WRITE_BYTE( 10 ); // framerate
WRITE_BYTE( 10 ); // life in 0.1's
WRITE_BYTE( width ); // width
WRITE_BYTE( noise ); // noise
WRITE_BYTE( red ); // r, g, b
WRITE_BYTE( green ); // r, g, b
WRITE_BYTE( blue ); // r, g, b
WRITE_BYTE( brightness ); // brightness
WRITE_BYTE( speed ); // speed
MESSAGE_END();
}
//
static void WaypointSearchItems(edict_t *pEntity, const Vector &v_origin, int wpt_index)
{
edict_t *pent = NULL;
float radius = 40;
TraceResult tr;
float distance;
float min_distance;
char item_name[64];
char nearest_name[64];
edict_t *nearest_pent;
int itemflag;
nearest_name[0] = 0; // null out nearest_name string
nearest_pent = NULL;
min_distance = 99999.0;