-
Notifications
You must be signed in to change notification settings - Fork 21
/
bot_navigate.cpp
2362 lines (1835 loc) · 72 KB
/
bot_navigate.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!
//
// bot_navigate.cpp
//
#ifndef _WIN32
#include <string.h>
#endif
#include <extdll.h>
#include <dllapi.h>
#include <h_export.h>
#include <meta_api.h>
#include "bot.h"
#include "bot_func.h"
#include "bot_weapons.h"
#include "waypoint.h"
#include "bot_skill.h"
#include "bot_weapons.h"
#include "bot_sound.h"
extern WAYPOINT waypoints[MAX_WAYPOINTS];
extern int num_waypoints; // number of waypoints currently in use
extern qboolean is_team_play;
extern qboolean checked_teamplay;
extern bot_weapon_t weapon_defs[MAX_WEAPONS];
// SET THIS UP BASED ON MOD!!!
const int max_drop_height = 800;
// returns the number of degrees left to turn toward ideal pitch...
float BotChangePitch( bot_t &pBot, float speed )
{
edict_t *pEdict = pBot.pEdict;
float ideal;
float current;
float current_180; // current +/- 180 degrees
float diff;
// turn from the current v_angle pitch to the idealpitch by selecting
// the quickest way to turn to face that direction
current = pEdict->v.v_angle.x;
ideal = pEdict->v.idealpitch;
// find the difference in the current and ideal angle
diff = fabs(current - ideal);
speed = speed * pBot.f_frame_time; // angles per second
// check if difference is less than the max degrees per turn
if (diff < speed)
speed = diff; // just need to turn a little bit (less than max)
// here we have four cases, both angle positive, one positive and
// the other negative, one negative and the other positive, or
// both negative. handle each case separately...
if ((current >= 0) && (ideal >= 0)) // both positive
{
if (current > ideal)
current -= speed;
else
current += speed;
}
else if ((current >= 0) && (ideal < 0))
{
current_180 = current - 180;
if (current_180 > ideal)
current += speed;
else
current -= speed;
}
else if ((current < 0) && (ideal >= 0))
{
current_180 = current + 180;
if (current_180 > ideal)
current += speed;
else
current -= speed;
}
else // (current < 0) && (ideal < 0) both negative
{
if (current > ideal)
current -= speed;
else
current += speed;
}
// check for wrap around of angle...
if (current > 180)
current -= 360;
if (current < -180)
current += 360;
pEdict->v.v_angle.x = current;
return diff; // return number of degrees left to turn
}
// returns the number of degrees left to turn toward ideal yaw...
float BotChangeYaw( bot_t &pBot, float speed )
{
edict_t *pEdict = pBot.pEdict;
float ideal;
float current;
float current_180; // current +/- 180 degrees
float diff;
// turn from the current v_angle yaw to the ideal_yaw by selecting
// the quickest way to turn to face that direction
current = pEdict->v.v_angle.y;
ideal = pEdict->v.ideal_yaw;
// find the difference in the current and ideal angle
diff = fabs(current - ideal);
// speed that we can turn during this frame...
speed = speed * pBot.f_frame_time;
// check if difference is less than the max degrees per turn
if (diff < speed)
speed = diff; // just need to turn a little bit (less than max)
// here we have four cases, both angle positive, one positive and
// the other negative, one negative and the other positive, or
// both negative. handle each case separately...
if ((current >= 0) && (ideal >= 0)) // both positive
{
if (current > ideal)
current -= speed;
else
current += speed;
}
else if ((current >= 0) && (ideal < 0))
{
current_180 = current - 180;
if (current_180 > ideal)
current += speed;
else
current -= speed;
}
else if ((current < 0) && (ideal >= 0))
{
current_180 = current + 180;
if (current_180 > ideal)
current += speed;
else
current -= speed;
}
else // (current < 0) && (ideal < 0) both negative
{
if (current > ideal)
current -= speed;
else
current += speed;
}
// check for wrap around of angle...
if (current > 180)
current -= 360;
if (current < -180)
current += 360;
pEdict->v.v_angle.y = current;
return diff; // return number of degrees left to turn
}
static qboolean BotFindWaypoint( bot_t &pBot )
{
int index, select_index;
int path_index = -1;
float distance, min_distance[3];
int min_index[3];
edict_t *pEdict = pBot.pEdict;
for (index=0; index < 3; index++)
{
min_distance[index] = 99999.0;
min_index[index] = -1;
}
index = WaypointFindPath(path_index, pBot.curr_waypoint_index);
while (index != -1)
{
// if index is not a current or recent previous waypoint...
if ((index != pBot.curr_waypoint_index) &&
(index != pBot.prev_waypoint_index[0]) &&
(index != pBot.prev_waypoint_index[1]) &&
(index != pBot.prev_waypoint_index[2]) &&
(index != pBot.prev_waypoint_index[3]) &&
(index != pBot.prev_waypoint_index[4]))
{
// find the distance from the bot to this waypoint
distance = (pEdict->v.origin - waypoints[index].origin).Length();
if (distance < min_distance[0])
{
min_distance[2] = min_distance[1];
min_index[2] = min_index[1];
min_distance[1] = min_distance[0];
min_index[1] = min_index[0];
min_distance[0] = distance;
min_index[0] = index;
}
else if (distance < min_distance [1])
{
min_distance[2] = min_distance[1];
min_index[2] = min_index[1];
min_distance[1] = distance;
min_index[1] = index;
}
else if (distance < min_distance[2])
{
min_distance[2] = distance;
min_index[2] = index;
}
}
// find the next path to a waypoint
index = WaypointFindPath(path_index, pBot.curr_waypoint_index);
}
select_index = -1;
// about 20% of the time choose a waypoint at random
// (don't do this any more often than every 10 seconds)
if ((RANDOM_LONG2(1, 100) <= 20) &&
(pBot.f_random_waypoint_time <= gpGlobals->time))
{
pBot.f_random_waypoint_time = gpGlobals->time + 10.0;
if (min_index[2] != -1)
index = RANDOM_LONG2(0, 2);
else if (min_index[1] != -1)
index = RANDOM_LONG2(0, 1);
else if (min_index[0] != -1)
index = 0;
else
return FALSE; // no waypoints found!
select_index = min_index[index];
}
else
{
// use the closest waypoint that has been recently used
select_index = min_index[0];
}
if (select_index != -1) // was a waypoint found?
{
pBot.prev_waypoint_index[4] = pBot.prev_waypoint_index[3];
pBot.prev_waypoint_index[3] = pBot.prev_waypoint_index[2];
pBot.prev_waypoint_index[2] = pBot.prev_waypoint_index[1];
pBot.prev_waypoint_index[1] = pBot.prev_waypoint_index[0];
pBot.prev_waypoint_index[0] = pBot.curr_waypoint_index;
pBot.curr_waypoint_index = select_index;
pBot.waypoint_origin = waypoints[select_index].origin;
pBot.f_waypoint_time = gpGlobals->time;
return TRUE;
}
return FALSE; // couldn't find a waypoint
}
static void BotEvaluateGoal( bot_t &pBot )
{
edict_t *pEdict = pBot.pEdict;
// we're dying! Forget about our goal
if (pBot.waypoint_goal != -1 && pEdict->v.health <= 25 && pBot.wpt_goal_type != WPT_GOAL_HEALTH)
{
//if(pBot.waypoint_goal != -1)
// UTIL_ConsolePrintf("[%s] Dying, forget goal: %d -> %d", pBot.name, pBot.waypoint_goal, -1);
pBot.f_waypoint_goal_time = 0;
pBot.waypoint_goal = -1;
}
}
//
static int BotGetSoundWaypoint( bot_t &pBot, edict_t *pTrackSoundEdict, edict_t ** pNewTrackSoundEdict )
{
edict_t *pEdict = pBot.pEdict;
int index = -1;
int iSound;
CSound *pCurrentSound;
float mindistance;
edict_t *pMinDistanceEdict = NULL;
mindistance = WAYPOINT_UNREACHABLE;
// walk through active sound linked list
for(iSound = CSoundEnt::ActiveList(); iSound != SOUNDLIST_EMPTY; iSound = pCurrentSound->m_iNext)
{
pCurrentSound = CSoundEnt::SoundPointerForIndex( iSound );
if(!pCurrentSound)
continue;
// ignore sounds created by bot itself
if(pCurrentSound->m_iBotOwner == (&pBot - &bots[0]))
continue;
// we want specific waypoint?
if(!FNullEnt(pTrackSoundEdict) && pCurrentSound->m_pEdict != pTrackSoundEdict)
continue;
// is sound too far away? (bot cannot hear)
float s_distance = (pCurrentSound->m_vecOrigin - pEdict->v.origin).Length();
if(s_distance > pCurrentSound->m_iVolume * skill_settings[pBot.bot_skill].hearing_sensitivity)
continue;
int temp_index = WaypointFindNearest(pCurrentSound->m_vecOrigin, pEdict, REACHABLE_RANGE, TRUE);
// get distance
if (temp_index > -1 && temp_index < num_waypoints)
{
float distance;
// use alternative way in case bot doesn't have current waypoint
if(pBot.curr_waypoint_index == -1)
distance = (pEdict->v.origin - waypoints[temp_index].origin).Length();
else
distance = WaypointDistanceFromTo(pBot.curr_waypoint_index, temp_index);
if (distance < mindistance)
{
mindistance = distance;
index = temp_index;
pMinDistanceEdict = FNullEnt(pCurrentSound->m_pEdict)?NULL:pCurrentSound->m_pEdict;
}
}
}
if(pNewTrackSoundEdict)
*pNewTrackSoundEdict = pMinDistanceEdict;
return index;
}
//
qboolean BotUpdateTrackSoundGoal( bot_t &pBot )
{
if(pBot.wpt_goal_type != WPT_GOAL_TRACK_SOUND)
return FALSE;
// check track-sound-time if we should stop tracking
// check if target has been erased
// check our state -> do we want to keep tracking
if((pBot.f_track_sound_time <= 0.0f && pBot.f_track_sound_time < gpGlobals->time) ||
!pBot.b_has_enough_ammo_for_good_weapon || pBot.b_low_health)
{
if(pBot.waypoint_goal != -1)
{
//if(!pBot.b_has_enough_ammo_for_good_weapon || pBot.b_low_health)
// UTIL_ConsolePrintf("[%s] Dropped sound tracking goal because(%d -> %d): out of ammo/health", pBot.name, pBot.waypoint_goal, -1);
//if(pBot.f_track_sound_time <= 0.0f && pBot.f_track_sound_time < gpGlobals->time)
// UTIL_ConsolePrintf("[%s] Dropped sound tracking goal because(%d -> %d): end of tracking time", pBot.name, pBot.waypoint_goal, -1);
}
pBot.waypoint_goal = -1;
pBot.wpt_goal_type = WPT_GOAL_NONE;
pBot.pTrackSoundEdict = NULL;
pBot.f_track_sound_time = -1;
return FALSE;
}
// get waypoint close to sound of track-sound-edict
edict_t* pNew = NULL;
int waypoint = BotGetSoundWaypoint( pBot, pBot.pTrackSoundEdict, &pNew );
if(FNullEnt(pBot.pTrackSoundEdict) && !FNullEnt(pNew))
pBot.pTrackSoundEdict = pNew;
// update waypoint_goal
//if(pBot.waypoint_goal != waypoint)
// UTIL_ConsolePrintf("[%s] Updated sound tracking goal waypoint: %d -> %d", pBot.name, pBot.waypoint_goal, waypoint);
pBot.waypoint_goal = waypoint;
return TRUE;
}
static void BotFindWaypointGoal( bot_t &pBot )
{
int index = -1;
edict_t *pEdict = pBot.pEdict;
bot_weapon_select_t *pSelect = &weapon_select[0];
if (pEdict->v.health * RANDOM_FLOAT2(0.9f, 1.0f/0.9f) < VALVE_MAX_NORMAL_HEALTH * 0.25f)
{
// look for health if we're pretty dead
index = WaypointFindNearestGoal(pEdict, pBot.curr_waypoint_index, W_FL_HEALTH);
if(index == -1)
index = WaypointFindRandomGoal(pEdict, W_FL_HEALTH);
if (index != -1)
{
pBot.wpt_goal_type = WPT_GOAL_HEALTH;
pBot.waypoint_goal = index;
}
goto exit;
}
if (pBot.pBotEnemy == NULL)
{
int goal_type = 0;
int ammoflags;
int weaponflags;
float mindistance = WAYPOINT_UNREACHABLE;
edict_t* pTrackSoundEdict = NULL;
// only if not engaging
// find these if have good weapon and no low health
if(!pBot.b_low_health && pBot.b_has_enough_ammo_for_good_weapon)
{
// find nearest interesting sound that isn't visible
int temp_index = BotGetSoundWaypoint(pBot, NULL, &pTrackSoundEdict);
if(temp_index != -1)
{
if(RANDOM_FLOAT2(0.0, 1.0) < 0.5f)
{
float track_time = RANDOM_FLOAT2(skill_settings[pBot.bot_skill].track_sound_time_min, skill_settings[pBot.bot_skill].track_sound_time_max);
// don't try get other objects
pBot.wpt_goal_type = WPT_GOAL_TRACK_SOUND;
pBot.waypoint_goal = temp_index;
pBot.pTrackSoundEdict = pTrackSoundEdict;
pBot.f_track_sound_time = gpGlobals->time + track_time;
index = temp_index;
goto exit;
}
else
{
float distance = WaypointDistanceFromTo(pBot.curr_waypoint_index, temp_index);
if (distance < mindistance)
{
mindistance = distance;
index = temp_index;
goal_type = WPT_GOAL_TRACK_SOUND;
}
}
}
// want health?
if (pEdict->v.health < VALVE_MAX_NORMAL_HEALTH * 0.95)
{
// find nearest health
int temp_index = WaypointFindNearestGoal(pEdict, pBot.curr_waypoint_index, W_FL_HEALTH, pBot.exclude_points);
if(temp_index == -1)
temp_index = WaypointFindRandomGoal(pEdict, W_FL_HEALTH, pBot.exclude_points);
// get distance
if (temp_index > -1 && temp_index < num_waypoints)
{
float distance = WaypointDistanceFromTo(pBot.curr_waypoint_index, temp_index);
if (distance < mindistance)
{
mindistance = distance;
index = temp_index;
goal_type = WPT_GOAL_HEALTH;
}
}
}
// want health?
if (pEdict->v.armorvalue < VALVE_MAX_NORMAL_BATTERY * 0.95)
{
// find nearest armor
int temp_index = WaypointFindNearestGoal(pEdict, pBot.curr_waypoint_index, W_FL_ARMOR, pBot.exclude_points);
if(temp_index == -1)
temp_index = WaypointFindRandomGoal(pEdict, W_FL_ARMOR, pBot.exclude_points);
// get distance
if (temp_index > -1 && temp_index < num_waypoints)
{
float distance = WaypointDistanceFromTo(pBot.curr_waypoint_index, temp_index);
if (distance < mindistance)
{
mindistance = distance;
index = temp_index;
goal_type = WPT_GOAL_ARMOR;
}
}
}
}
if (!pBot.b_longjump && skill_settings[pBot.bot_skill].can_longjump)
{
// find a longjump module
int temp_index = WaypointFindNearestGoal(pEdict, pBot.curr_waypoint_index, W_FL_LONGJUMP, pBot.exclude_points);
if(temp_index == -1)
temp_index = WaypointFindRandomGoal(pEdict, W_FL_LONGJUMP, pBot.exclude_points);
// get distance
if (temp_index > -1 && temp_index < num_waypoints)
{
float distance = WaypointDistanceFromTo(pBot.curr_waypoint_index, temp_index);
if (distance < mindistance)
{
mindistance = distance;
index = temp_index;
goal_type = WPT_GOAL_ITEM;
}
}
}
// find new weapon if only have shitty weapons or running out of ammo on all weapons
if (BotGetGoodWeaponCount(pBot, 1)==0 || !pBot.b_has_enough_ammo_for_good_weapon)
{
// find weapons that bot can use
int select_index = -1;
int weaponflags = 0;
// collect item flags of desired weapons
while(pSelect[++select_index].iId)
{
if(pSelect[select_index].avoid_this_gun)
continue;
if(!BotCanUseWeapon(pBot, pSelect[select_index]))
continue;
weaponflags |= pSelect[select_index].waypoint_flag;
}
if(weaponflags)
{
int temp_index = WaypointFindNearestGoal(pEdict, pBot.curr_waypoint_index, W_FL_WEAPON, weaponflags, pBot.exclude_points);
if(temp_index == -1)
temp_index = WaypointFindRandomGoal(pEdict, W_FL_WEAPON, weaponflags, pBot.exclude_points);
if (temp_index != -1)
{
float distance = WaypointDistanceFromTo(pBot.curr_waypoint_index, temp_index);
if (distance < mindistance)
{
index = temp_index;
mindistance = distance;
goal_type = WPT_GOAL_WEAPON;
}
}
}
}
// get flags for ammo that are low, primarily ammo that can we can use with current weapons
else if (((ammoflags = BotGetLowAmmoFlags(pBot, &(weaponflags=0), TRUE)) != 0 && weaponflags != 0) ||
((ammoflags = BotGetLowAmmoFlags(pBot, &(weaponflags=0), FALSE)) != 0 && weaponflags != 0))
{
int flags = (ammoflags ? W_FL_AMMO : 0) | (weaponflags ? W_FL_WEAPON : 0);
// find ammo for that we don't have enough yet
int temp_index = WaypointFindNearestGoal(pEdict, pBot.curr_waypoint_index, flags, ammoflags|weaponflags, pBot.exclude_points);
if(temp_index == -1)
temp_index = WaypointFindRandomGoal(pEdict, flags, ammoflags|weaponflags, pBot.exclude_points);
if (temp_index != -1)
{
float distance = WaypointDistanceFromTo(pBot.curr_waypoint_index, temp_index);
if (distance < mindistance)
{
index = temp_index;
mindistance = distance;
goal_type = WPT_GOAL_AMMO;
}
}
}
if(index != -1)
{
pBot.wpt_goal_type = goal_type;
pBot.waypoint_goal = index;
if(goal_type != WPT_GOAL_TRACK_SOUND)
{
pBot.pTrackSoundEdict = NULL;
pBot.f_track_sound_time = -1;
}
else
{
float track_time = RANDOM_FLOAT2(skill_settings[pBot.bot_skill].track_sound_time_min, skill_settings[pBot.bot_skill].track_sound_time_max);
pBot.pTrackSoundEdict = pTrackSoundEdict;
pBot.f_track_sound_time = gpGlobals->time + track_time;
}
goto exit;
}
}
else if(!FNullEnt(pBot.pBotEnemy))
{
// find a waypoint near our enemy
index = WaypointFindNearest(pBot.pBotEnemy, 1024);
if (index != -1)
{
pBot.wpt_goal_type = WPT_GOAL_ENEMY;
pBot.waypoint_goal = index;
pBot.pTrackSoundEdict = NULL;
pBot.f_track_sound_time = -1;
goto exit;
}
}
// we couldn't find ANYTHING, go somewhere random
if (index == -1)
{
index = WaypointFindRandomGoal(pEdict, 0);
if (index != -1)
{
pBot.wpt_goal_type = WPT_GOAL_LOCATION;
pBot.waypoint_goal = index;
pBot.pTrackSoundEdict = NULL;
pBot.f_track_sound_time = -1;
goto exit;
}
}
exit:
if (index != -1)
{
#if 0
switch (pBot.wpt_goal_type)
{
case WPT_GOAL_HEALTH:
UTIL_ConsolePrintf("[%s] %s", pBot.name, "I am going for some health!\n");
break;
case WPT_GOAL_ARMOR:
UTIL_ConsolePrintf("[%s] %s", pBot.name, "I am going for some armor!\n");
break;
case WPT_GOAL_WEAPON:
UTIL_ConsolePrintf("[%s] %s", pBot.name, "I am going for a weapon!\n");
break;
case WPT_GOAL_AMMO:
UTIL_ConsolePrintf("[%s] %s", pBot.name, "I am going for some ammo!\n");
break;
case WPT_GOAL_ITEM:
UTIL_ConsolePrintf("[%s] %s", pBot.name, "I am going for an item!\n");
break;
case WPT_GOAL_TRACK_SOUND:
UTIL_ConsolePrintf("[%s] %s", pBot.name, "I am tracking a sound!\n");
break;
case WPT_GOAL_ENEMY:
UTIL_ConsolePrintf("[%s] %s", pBot.name, "I am tracking/engaging an enemy!\n");
break;
case WPT_GOAL_LOCATION:
UTIL_ConsolePrintf("[%s] %s", pBot.name, "I have a random location goal!\n");
break;
default:
UTIL_ConsolePrintf("[%s] %s", pBot.name, "I have an unknown goal!\n");
break;
}
#endif
}
}
qboolean BotHeadTowardWaypoint( bot_t &pBot )
{
int i;
Vector v_src, v_dest;
TraceResult tr;
int index;
qboolean status;
float waypoint_distance, min_distance;
qboolean touching;
edict_t *pEdict = pBot.pEdict;
// check if the bot has been trying to get to this waypoint for a while...
if ((pBot.f_waypoint_time + 5.0) < gpGlobals->time)
{
pBot.curr_waypoint_index = -1; // forget about this waypoint
pBot.waypoint_goal = -1; // also forget about a goal
}
// no goal, no goal time
if ((pBot.waypoint_goal == -1) && (pBot.f_waypoint_goal_time > gpGlobals->time + 2) &&
(pBot.f_waypoint_goal_time != 0.0))
pBot.f_waypoint_goal_time = 0.0;
// find new waypoint for sound we are tracking
if(pBot.wpt_goal_type == WPT_GOAL_TRACK_SOUND)
{
BotUpdateTrackSoundGoal(pBot);
}
// check if we need to find a waypoint...
if (pBot.curr_waypoint_index == -1)
{
pBot.waypoint_top_of_ladder = FALSE;
// did we just come off of a ladder or are we underwater?
if (((pBot.f_end_use_ladder_time + 2.0) > gpGlobals->time) || pBot.b_in_water)
{
// find the nearest visible waypoint
i = WaypointFindNearest(pEdict, REACHABLE_RANGE);
}
else
{
// find the nearest reachable waypoint
i = WaypointFindReachable(pEdict, REACHABLE_RANGE);
}
if (i == -1)
{
pBot.curr_waypoint_index = -1;
return FALSE;
}
pBot.curr_waypoint_index = i;
pBot.waypoint_origin = waypoints[i].origin;
pBot.f_waypoint_time = gpGlobals->time;
}
else
{
if(pBot.b_longjump && skill_settings[pBot.bot_skill].can_longjump)
{
Vector vecToWpt = waypoints[pBot.curr_waypoint_index].origin - pEdict->v.origin;
float max_lj_distance = LONGJUMP_DISTANCE * (800 / CVAR_GET_FLOAT("sv_gravity"));
// longjump toward the waypoint
// we have to be out of water, on the ground, able to longjump, far enough away to longjump, and
// facing pretty close to the waypoint
if(!pBot.b_in_water && pBot.b_on_ground && !pBot.b_ducking && !pBot.b_on_ladder &&
vecToWpt.Length() >= max_lj_distance * 0.6 &&
pEdict->v.velocity.Length() > 50 &&
DotProduct(UTIL_AnglesToForward(pEdict->v.v_angle), vecToWpt.Normalize()) > cos(deg2rad(10)))
{
// trace a hull toward the current waypoint the distance of a longjump (depending on gravity)
UTIL_TraceHull(
UTIL_GetOrigin(pEdict),
UTIL_GetOrigin(pEdict) + vecToWpt.Normalize() * max_lj_distance,
dont_ignore_monsters, head_hull, pEdict, &tr
);
// make sure it's clear
if (tr.flFraction > 0.999999f)
{
// trace another hull straight down so we can get ground level here
UTIL_TraceHull(tr.vecEndPos, tr.vecEndPos - Vector(0,0,8192), dont_ignore_monsters, head_hull, pEdict, &tr);
// make sure the point we found is about level with
if (waypoints[pBot.curr_waypoint_index].origin.z - tr.vecEndPos.z < 52)
{
// actually do the longjump
pEdict->v.button |= IN_DUCK;
pBot.b_longjump_do_jump = TRUE;
// recognize we'll be in the air for a second most likely
pBot.f_longjump_time = gpGlobals->time + 1.0;
//UTIL_ConsolePrintf("%s doing longjump! -- wp\n", STRING(pEdict->v.netname));
}
}
}
}
// skip this part if bot is trying to get out of water...
if (pBot.f_exit_water_time < gpGlobals->time)
{
// check if we can still see our target waypoint...
v_src = pEdict->v.origin + pEdict->v.view_ofs;
v_dest = waypoints[pBot.curr_waypoint_index].origin;
// trace a line from bot's eyes to destination...
UTIL_TraceMove( v_src, v_dest, ignore_monsters,
pEdict->v.pContainingEntity, &tr );
// check if line of sight to object is blocked (i.e. not visible)
if (tr.flFraction < 1.0f)
{
// did we just come off of a ladder or are we under water?
if (((pBot.f_end_use_ladder_time + 2.0) > gpGlobals->time) || pBot.b_in_water)
{
// find the nearest visible waypoint
i = WaypointFindNearest(pEdict, REACHABLE_RANGE);
}
else
{
// find the nearest reachable waypoint
i = WaypointFindReachable(pEdict, REACHABLE_RANGE);
}
if (i == -1)
{
pBot.curr_waypoint_index = -1;
return FALSE;
}
pBot.curr_waypoint_index = i;
pBot.waypoint_origin = waypoints[i].origin;
pBot.f_waypoint_time = gpGlobals->time;
}
}
}
// find the distance to the target waypoint
waypoint_distance = (pEdict->v.origin - pBot.waypoint_origin).Length();
// set the minimum distance from waypoint to be considered "touching" it
min_distance = 50.0;
// if this is a crouch waypoint, bot must be fairly close...
if (waypoints[pBot.curr_waypoint_index].flags & W_FL_JUMP)
min_distance = 25.0;
if (waypoints[pBot.curr_waypoint_index].flags & W_FL_CROUCH)
min_distance = 20.0;
// if this is a ladder waypoint, bot must be fairly close to get on ladder
if (waypoints[pBot.curr_waypoint_index].flags & W_FL_LADDER)
min_distance = 20.0;
// if this is a lift-start waypoint
if (waypoints[pBot.curr_waypoint_index].flags & W_FL_LIFT_START)
min_distance = 32.0;
// if this is a lift-end waypoint, bot must be very close
if (waypoints[pBot.curr_waypoint_index].flags & W_FL_LIFT_END)
min_distance = 20.0;
// if item waypoint, go close
if (waypoints[pBot.curr_waypoint_index].flags & (W_FL_LONGJUMP | W_FL_HEALTH | W_FL_ARMOR | W_FL_AMMO | W_FL_WEAPON))
min_distance = 20.0;
// if trying to get out of water, need to get very close to waypoint...
if (pBot.f_exit_water_time >= gpGlobals->time)
min_distance = 20.0;
touching = FALSE;
// did the bot run past the waypoint? (prevent the loop-the-loop problem)
if ((pBot.prev_waypoint_distance > 1.0f) &&
(waypoint_distance > pBot.prev_waypoint_distance))
touching = TRUE;
// are we close enough to a target waypoint...
if (waypoint_distance <= min_distance)
touching = TRUE;
// save current distance as previous
pBot.prev_waypoint_distance = waypoint_distance;
if (touching)
{
qboolean waypoint_found = FALSE;
pBot.prev_waypoint_distance = 0.0;
// reeval our goal
BotEvaluateGoal( pBot );
// check if the waypoint is a door waypoint
if (waypoints[pBot.curr_waypoint_index].flags & W_FL_DOOR)
{
pBot.f_dont_avoid_wall_time = gpGlobals->time + 5.0;
}
// check if the next waypoint is a jump waypoint...
if (waypoints[pBot.curr_waypoint_index].flags & W_FL_JUMP)
{
pEdict->v.button |= IN_JUMP; // jump here
}
// check if the bot has reached the goal waypoint...
if (pBot.curr_waypoint_index == pBot.waypoint_goal)
{
// if this waypoint has an item, make sure we get it
if (waypoints[pBot.waypoint_goal].flags & (W_FL_LONGJUMP| W_FL_HEALTH | W_FL_ARMOR | W_FL_AMMO | W_FL_WEAPON))
{
pBot.pBotPickupItem = WaypointFindItem(pBot.waypoint_goal);
pBot.f_find_item = gpGlobals->time + 0.2;
pBot.f_last_item_found = gpGlobals->time;
}
if (pBot.pBotEnemy != NULL)
pBot.f_waypoint_goal_time = gpGlobals->time + 0.25;
else // a little delay time, since we'll touch the waypoint before we actually get what it has
pBot.f_waypoint_goal_time = gpGlobals->time + 0.25;
// don't pick same object too often
if(pBot.wpt_goal_type == WPT_GOAL_HEALTH ||
pBot.wpt_goal_type == WPT_GOAL_ARMOR ||
pBot.wpt_goal_type == WPT_GOAL_WEAPON ||
pBot.wpt_goal_type == WPT_GOAL_AMMO ||
pBot.wpt_goal_type == WPT_GOAL_ITEM)
{
for(int i = EXCLUDE_POINTS_COUNT - 1; i > 0; i--)
pBot.exclude_points[i] = pBot.exclude_points[i-1];
pBot.exclude_points[0] = pBot.waypoint_goal;
}
//UTIL_ConsolePrintf("[%s] Reach goal: %d -> %d", pBot.name, pBot.waypoint_goal, -1);
pBot.waypoint_goal = -1; // forget this goal waypoint
pBot.wpt_goal_type = WPT_GOAL_NONE;
}
// test special case of bot underwater and close to surface...
if (pBot.b_in_water)
{
Vector v_src, v_dest;
TraceResult tr;
int contents;
// trace a line straight up 100 units...
v_src = pEdict->v.origin;
v_dest = v_src;
v_dest.z = v_dest.z + 100.0;
// trace a line to destination...
UTIL_TraceMove( v_src, v_dest, ignore_monsters,
pEdict->v.pContainingEntity, &tr );
if (tr.flFraction > 0.999999f)
{
// find out what the contents is of the end of the trace...
contents = POINT_CONTENTS( tr.vecEndPos );
// check if the trace endpoint is in open space...
if (contents == CONTENTS_EMPTY)
{
// find the nearest visible waypoint
i = WaypointFindNearest(tr.vecEndPos, pEdict, 100);
if (i != -1)
{
waypoint_found = TRUE;
pBot.curr_waypoint_index = i;
pBot.waypoint_origin = waypoints[i].origin;
pBot.f_waypoint_time = gpGlobals->time;
// keep trying to exit water for next 3 seconds
pBot.f_exit_water_time = gpGlobals->time + 3.0;
}
}
}
}
// if the bot doesn't have a goal waypoint then pick one...
if ((pBot.waypoint_goal == -1 || pBot.pBotEnemy != NULL) &&
(pBot.f_waypoint_goal_time < gpGlobals->time))
{