-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbot_navigate.cpp
3872 lines (3085 loc) · 116 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
///////////////////////////////////////////////////////////////////////////////////////////////
//
// -- GNU -- open source
// Please read and agree to the mb_gnu_license.txt file
// (the file is located in the marine_bot source folder)
// before editing or distributing this source code.
// This source code is free for use under the rules of the GNU General Public License.
// For more information goto:: http://www.gnu.org/licenses/
//
// credits to - valve, botman.
//
// Marine Bot - code by Frank McNeil, Kota@, Mav, Shrike.
//
// (http://marinebot.xf.cz)
//
//
// bot_navigate.cpp
//
////////////////////////////////////////////////////////////////////////////////////////////////
// added by kota@
#if defined(WIN32)
#pragma warning(disable:4786)
#endif
#if defined(WIN32)
#pragma warning(disable: 4005 91)
#endif
#include <cstring>
#include "extdll.h"
#include "util.h"
#include "cbase.h"
#include <cstdio>
#include <sys/types.h>
#include <cmath>
#include "bot.h"
#include "bot_func.h"
#include "bot_manager.h"
#include "waypoint.h"
#include "bot_weapons.h"
// few function prototypes used in this file
void UpdateWptHistory(bot_t *pBot);
Vector GenerateOrigin(int wpt_index);
int ForwardPathMove(bot_t *pBot, W_PATH *p, int &path_next_wpt, bool &path_end);
int BackwardPathMove(bot_t *pBot, W_PATH *p, int &path_next_wpt, bool &path_end);
void AssignPathBasedBehaviour(bot_t *pBot, int wpt_index);
//int getNextWptInPath(bot_t *pBot); // not used
bool TraceForward(bot_t *pBot, bool check_head, bool check_feet, int dist, TraceResult *tr);
bool TraceJumpUp(bot_t *pBot, int height);
void BotFixIdealPitch(edict_t *pEdict)
{
// check for wrap around of angle
if (pEdict->v.idealpitch > 180)
pEdict->v.idealpitch -= 360;
if (pEdict->v.idealpitch < -180)
pEdict->v.idealpitch += 360;
}
float BotChangePitch( bot_t *pBot, float speed )
{
edict_t *pEdict = pBot->pEdict;
float current_180; // current +/- 180 degrees
const float bipod_top_limit = 20.0;
const float bipod_bottom_limit = 45.0;
// turn from the current v_angle pitch to the idealpitch by selecting
// the quickest way to turn to face that direction
float current = pEdict->v.v_angle.x;
const float ideal = pEdict->v.idealpitch;
// is bot using bipod so limit his turn angles
if (pBot->IsTask(TASK_BIPOD))
{
// in pEdict->v.vuser1 is stored v_view angle when player started using bipod
// (ie the direction player faced when he used "bipod" command)
// is current angle bigger than top limit angle (while the bot is looking upwards)
// OR is current angle bigger than bottom limit angle (while the bot is looking downwards)
if (((std::fabs(current) > bipod_top_limit) && (current < 0)) ||
((std::fabs(current) > bipod_bottom_limit) && (current > 0)))
current = pEdict->v.vuser1.x; // reset to original value
}
// find the difference in the current and ideal angle
const float diff = fabs(static_cast<double>(current) - ideal);
// check if the bot is already facing the idealpitch direction
if (diff <= 1)
return diff; // return number of degrees turned
// 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)
// we should make some difference between bots if the bot is trying to face an enemy
// ie. we need to slow down worse bots more than better bots
if (pBot->IsSubTask(ST_FACEENEMY))
{
// so we just take some constant and multiply it by the skill
// which means no difference for best bots because skill is zero based variable
const float speed_tweak = 4 * pBot->bot_skill;
// and then we can modify the turning speed
speed = speed - speed_tweak;
}
// 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 speed; // return number of degrees turned
}
void BotFixIdealYaw(edict_t *pEdict)
{
// check for wrap around of angle
if (pEdict->v.ideal_yaw > 180)
pEdict->v.ideal_yaw -= 360;
if (pEdict->v.ideal_yaw < -180)
pEdict->v.ideal_yaw += 360;
}
float BotChangeYaw( bot_t *pBot, float speed )
{
edict_t *pEdict = pBot->pEdict;
float current_180; // current +/- 180 degrees
// turn from the current v_angle yaw to the ideal_yaw by selecting
// the quickest way to turn to face that direction
float current = pEdict->v.v_angle.y;
const float ideal = pEdict->v.ideal_yaw;
if (pBot->IsTask(TASK_BIPOD))
{
const float bipod_limit = 45.0;
// in pEdict->v.vuser1 is stored v_view angle when player started using bipod
// (ie the direction player looked when he used "bipod" command)
// is bot trying to turn more than bipod allows
if (fabs(static_cast<double>(pEdict->v.vuser1.y) - current) > bipod_limit)
{
current = pEdict->v.vuser1.y;
}
}
if (pBot->IsSubTask(ST_FACEENEMY))
{
const float speed_tweak = 4 * pBot->bot_skill;
speed = speed - speed_tweak;
}
const float diff = fabs(static_cast<double>(current) - ideal);
// the bot is basically facing the direction we wanted so we can remove the flag
if (diff < 4)
{
// we do it only here, because yaw is the more important part
// in making the bot reactions human like
pBot->RemoveSubTask(ST_FACEENEMY);
}
if (diff <= 1)
{
return diff;
}
if (diff < speed)
speed = diff;
if ((current >= 0) && (ideal >= 0))
{
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;
}
if (current > 180)
current -= 360;
if (current < -180)
current += 360;
pEdict->v.v_angle.y = current;
return speed; // return number of degrees turned
}
/*
* remove oldest history record and move all others by one (to make free slot for new wpt)
*/
void UpdateWptHistory(bot_t *pBot)
{
// don't update history if nothing changed
if (pBot->prev_wpt_index.get() == pBot->curr_wpt_index)
{
return;
}
pBot->prev_wpt_index.push(pBot->curr_wpt_index);
pBot->prev_wpt_origin = pBot->wpt_origin;
}
/*
* creates fake waypoint origin somewhere inside its range
* to make bots moves more random (human like)
* real waypoint origin stays untouched
* we are working only in planar dimension (ie ingoring z-coord)
*/
Vector GenerateOrigin(int wpt_index)
{
// if really small range OR the waypoint is cross OR ladder OR door waypoint use real origin
if ((waypoints[wpt_index].range <= 15) ||
(waypoints[wpt_index].flags & (W_FL_CROSS | W_FL_LADDER | W_FL_DOOR | W_FL_DOORUSE)))
{
return waypoints[wpt_index].origin;
}
Vector new_origin = waypoints[wpt_index].origin;
// get the range out of this wpt
const float wpt_range = waypoints[wpt_index].range;
// make the new x,y position
float d_x = RANDOM_FLOAT(1, wpt_range - 1);
float d_y = RANDOM_FLOAT(1, wpt_range - 1);
// in 50% of time use also negative values for fake position
if (RANDOM_LONG(1, 100) < 50)
d_x *= (float) -1.0;
if (RANDOM_LONG(1, 100) < 50)
d_y *= (float) -1.0;
// change waypoint x and y value
new_origin.x += d_x;
new_origin.y += d_y;
// return the new fake origin
return new_origin;
}
/*
* goes through given path and returns pointer to given waypoint node
* returns NULL if the waypoint isn't in this path
*/
/*
CAN'T USE IT NOW - NEED A NEW HEADER FILE THAT WILL HOLD PATH STRUCTURE
W_PATH *GetWaypointFromPath (bot_t *pBot, int wpt_index, int path_index)
{
// set it to head node
W_PATH *pcur_path_node = w_paths[path_index];
// go through the path node by node until the waypoint stored in one of those nodes do
// match given waypoint
while (pcur_path_node)
{
// we need to test if the bot already used this path node or not
// to handle situations when there is the same waypoint used more than once in one path
if ((pcur_path_node->wpt_index == wpt_index) &&
(pcur_path_node != pBot->curr_path_node))
{
// return a pointer to a path node that holds the waypoint we've searched
return pcur_path_node;
}
pcur_path_node = pcur_path_node->next;
}
return NULL;
}
*/
/* kota@
* this is the standard path move from Bot OnPath()
* I move it into separate function for easy understanding and manipulation
* Function set path_next_wpt, path_end.
* Function return status like Bot OnPath().
* 0 - need continue run Bot OnPath()
* 1 - return Bot OnPath() with the same status.
*/
int ForwardPathMove(bot_t *pBot, W_PATH *p, int &path_next_wpt, bool &path_end)
{
// is standard path move (start -> end)
// if the path ends AND is only one way path
if ((p->next == nullptr) && (w_paths[pBot->curr_path_index]->flags & P_FL_WAY_ONE))
{
// search for waypoints around
const int next_wpt = WaypointSearchNewInRange(pBot, p->wpt_index, pBot->curr_path_index);
// is any new waypoint in range
if (next_wpt != -1)
{
pBot->curr_wpt_index = next_wpt;
pBot->wpt_origin = waypoints[next_wpt].origin;
pBot->f_waypoint_time = gpGlobals->time;
if (botdebugger.IsDebugPaths())
{
char fpmsg[128];
sprintf(fpmsg, "\n***Reached the end of one-way path! - path #%d | curr_wpt #%d | dir is (start->end) | next wpt #%d\n\n",
pBot->curr_path_index + 1, p->wpt_index + 1, pBot->curr_wpt_index + 1);
HudNotify(fpmsg);
}
return 1;
}
// otherwise bot reached end of one-way path and didn't find any waypoint
else
{
// set some wait time
pBot->wpt_wait_time = gpGlobals->time + RANDOM_FLOAT(30.0, 120.0);
// bot still has a waypoint
pBot->f_waypoint_time = gpGlobals->time;
if (botdebugger.IsDebugPaths())
{
char fpemsg[128];
sprintf(fpemsg, "\n\n<<BUG IN WAYPOINTS>>Reached the end of one-way path, but didn't find wpt! - path #%d | last_wpt #%d\n\n\n",
pBot->curr_path_index + 1, p->wpt_index + 1);
HudNotify(fpemsg);
}
return -1;
}
}
// if the path ends AND it's two way path use the path in opposite direction
else if ((p->next == nullptr) && (w_paths[pBot->curr_path_index]->flags & P_FL_WAY_TWO))
{
// if path ends with goback and similar "turnback" waypoints
if (waypoints[pBot->curr_wpt_index].flags & PATH_TURNBACK &&
WaypointGetPriority(pBot->curr_wpt_index, pBot->bot_team) != 0)
{
if (p->prev != nullptr)
{
// change the direction bot had
pBot->opposite_path_dir = TRUE;
// set next waypoint - the previous one in llist
path_next_wpt = p->prev->wpt_index;
}
// REMOVE THIS - ONLY TESTING
#ifdef _DEBUG
else
{
ALERT(at_console, "(<<OBSERVE INFO>>)What's the bot doing when this happens?\n");
UTIL_DebugDev("start->end | two-way | wpt==goback/ammobox/use | prev == NULL",
pBot->curr_wpt_index, pBot->curr_path_index);
}
#endif
}
// otherwise try to find any new waypoint and path
else
{
const int next_wpt = WaypointSearchNewInRange(pBot, p->wpt_index, pBot->curr_path_index);
if (next_wpt != -1)
{
pBot->curr_wpt_index = next_wpt;
pBot->wpt_origin = waypoints[next_wpt].origin;
pBot->f_waypoint_time = gpGlobals->time;
if (botdebugger.IsDebugPaths())
{
char fpmsg[128];
sprintf(fpmsg, "\n***Reached the end of path! - path #%d | curr_wpt #%d | dir is (start->end) | next wpt #%d\n\n",
pBot->curr_path_index + 1, p->wpt_index + 1, pBot->curr_wpt_index + 1);
HudNotify(fpmsg);
}
return 1;
}
// otherwise wait there for a while and then turn back
else
{
pBot->wpt_wait_time = gpGlobals->time + RANDOM_FLOAT(15.0, 60.0);
if (p->prev != nullptr)
{
pBot->opposite_path_dir = TRUE;
path_next_wpt = p->prev->wpt_index;
}
if (botdebugger.IsDebugPaths())
{
char fpemsg[128];
sprintf(fpemsg, "\n\n<<BUG IN WAYPOINTS>>Reached the end of this path, but didn't find next wpt! - path #%d | curr_wpt #%d\n\n\n",
pBot->curr_path_index + 1, p->wpt_index + 1);
HudNotify(fpemsg);
}
return -1;
}
}
}
// if the path ends AND it's a patrol path use the path in opposite direction
else if ((p->next == nullptr) && (w_paths[pBot->curr_path_index]->flags & P_FL_WAY_PATROL))
{
// if path ends with these waypoints do nothing (further code does all we need)
if (waypoints[pBot->curr_wpt_index].flags & PATH_TURNBACK &&
WaypointGetPriority(pBot->curr_wpt_index, pBot->bot_team) != 0)
;
// otherwise if it does NOT end with "turnbacks" check
// if there is a chance to leave this path
else if (RANDOM_LONG(1, 100) < 15)
{
const int next_wpt = WaypointSearchNewInRange(pBot, p->wpt_index, pBot->curr_path_index);
if (next_wpt != -1)
{
pBot->curr_wpt_index = next_wpt;
pBot->wpt_origin = waypoints[next_wpt].origin;
pBot->f_waypoint_time = gpGlobals->time;
if (botdebugger.IsDebugPaths())
{
char ppmsg[128];
sprintf(ppmsg, "\n***Reached patrol path end! - path #%d | curr_wpt #%d | dir is (start->end) | next wpt #%d\n\n",
pBot->curr_path_index + 1, p->wpt_index + 1, pBot->curr_wpt_index + 1);
HudNotify(ppmsg);
}
return 1;
}
}
pBot->opposite_path_dir = TRUE;
path_next_wpt = p->prev->wpt_index;
}
// otherwise get next waypoint to continue to
else
{
if (p->next != nullptr)
{
path_next_wpt = p->next->wpt_index;
// we didn't reached end yet
path_end = FALSE;
}
}
return 0;
}
/* kota@
* this is the standart path move from Bot OnPath().
* I move it into separate function for easy understanding and manipulation
* Function set path_next_wpt, path_end.
* Function return status like Bot OnPath().
* 0 - need continue run Bot OnPath()
* 1 - return Bot OnPath() with the same status.
* -1 - error , return Bot OnPath() with the same status.
*/
int BackwardPathMove(bot_t *pBot, W_PATH *p, int &path_next_wpt, bool &path_end)
{
// if bot gets to one way path
if (w_paths[pBot->curr_path_index]->flags & P_FL_WAY_ONE)
{
#ifdef _DEBUG
ALERT(at_console, "***DAMN there is a problem: Get on One-way path #%d in opposite direction (RESET)\n", pBot->curr_path_index + 1);
UTIL_DebugDev("ERROR (Bad wpts): Bot entered One-way path from opposite direction!!!",
pBot->curr_wpt_index, pBot->curr_path_index);
#endif
pBot->opposite_path_dir = FALSE;
return -1;
}
// if reached path start change back to standard path moves
if ((p->prev == nullptr) && (w_paths[pBot->curr_path_index]->flags & P_FL_WAY_TWO))
{
// if path ends with goback and similar "turnback" waypoints
if (waypoints[pBot->curr_wpt_index].flags & PATH_TURNBACK &&
WaypointGetPriority(pBot->curr_wpt_index, pBot->bot_team) != 0)
{
if (p->next != nullptr)
{
// change the direction bot had
pBot->opposite_path_dir = FALSE;
// set next waypoint - the new one in llist
path_next_wpt = p->next->wpt_index;
}
}
// otherwise try to find any new waypoint and path
else
{
const int next_wpt = WaypointSearchNewInRange(pBot, p->wpt_index, pBot->curr_path_index);
if (next_wpt != -1)
{
pBot->curr_wpt_index = next_wpt;
pBot->wpt_origin = waypoints[next_wpt].origin;
pBot->f_waypoint_time = gpGlobals->time;
if (botdebugger.IsDebugPaths())
{
char bpmsg[128];
sprintf(bpmsg, "\n***Reached path start! - path #%d | curr_wpt #%d | dir is (end->start) | next wpt #%d\n\n",
pBot->curr_path_index + 1, p->wpt_index + 1, pBot->curr_wpt_index + 1);
HudNotify(bpmsg);
}
// clear this flag before return
pBot->opposite_path_dir = FALSE;
return 1;
}
// otherwise wait there for a while and then turn back
else
{
pBot->wpt_wait_time = gpGlobals->time + RANDOM_FLOAT(15.0, 60.0);
if (p->next != nullptr)
{
pBot->opposite_path_dir = FALSE;
path_next_wpt = p->next->wpt_index;
}
if (botdebugger.IsDebugPaths())
{
char bpemsg[128];
sprintf(bpemsg, "\n\n<<BUG IN WAYPOINTS>>Reached the end of this path, but didn't find next wpt! - path #%d | curr_wpt #%d\n\n\n",
pBot->curr_path_index + 1, p->wpt_index + 1);
HudNotify(bpemsg);
}
return -1;
}
}
}
// if reached path start change back to standard path moves
else if ((p->prev == nullptr) && (w_paths[pBot->curr_path_index]->flags & P_FL_WAY_PATROL))
{
// if path ends with a turback do nothing (further code does all we need)
if (waypoints[pBot->curr_wpt_index].flags & PATH_TURNBACK &&
WaypointGetPriority(pBot->curr_wpt_index, pBot->bot_team) != 0)
;
// otherwise if does NOT end with "turnbacks" check
// if there is a chance to leave this path
else if (RANDOM_LONG(1, 100) < 25)
{
const int next_wpt = WaypointSearchNewInRange(pBot, p->wpt_index, pBot->curr_path_index);
if (next_wpt != -1)
{
pBot->curr_wpt_index = next_wpt;
pBot->wpt_origin = waypoints[next_wpt].origin;
pBot->f_waypoint_time = gpGlobals->time;
if (botdebugger.IsDebugPaths())
{
char ppmsg[128];
sprintf(ppmsg, "\n***Reached patrol path start! - path #%d | curr_wpt #%d | dir is (end->start) | next wpt #%d\n\n",
pBot->prev_path_index + 1, p->wpt_index + 1, pBot->curr_wpt_index + 1);
HudNotify(ppmsg);
}
// clear this flag before return
pBot->opposite_path_dir = FALSE;
return 1;
}
}
// change the direction bot had
pBot->opposite_path_dir = FALSE;
// set next waypoint - the new one in llist
path_next_wpt = p->next->wpt_index;
}
// otherwise get next (this time from previous node in llist) waypoint to head to
else
{
if (p->prev != nullptr)
{
path_next_wpt = p->prev->wpt_index;
// we didn't reached end yet
path_end = FALSE;
}
}
return 0;
}
/*
* checks current path flags and sets correct behaviour based on it
*/
void AssignPathBasedBehaviour(bot_t *pBot, int wpt_index = -1)
{
if (pBot->curr_path_index == -1 || w_paths[pBot->curr_path_index] == nullptr)
return;
// if actual path is PATROL path store last waypoint index (to return to it after combat)
if (w_paths[pBot->curr_path_index]->flags & P_FL_WAY_PATROL)
pBot->patrol_path_wpt = wpt_index;
// if actual path isn't PATROL AND the index was set clear it
else if ((pBot->patrol_path_wpt != -1) && !(w_paths[pBot->curr_path_index]->flags & P_FL_WAY_PATROL))
pBot->patrol_path_wpt = -1;
// set avoid enemy task if the path forces the bot to do so
if (!pBot->IsTask(TASK_AVOID_ENEMY) && (w_paths[pBot->curr_path_index]->flags & P_FL_MISC_AVOID))
{
pBot->SetTask(TASK_AVOID_ENEMY);
// keep these messages as alert at console
// so the user can turn them on/off using the developer 1 command
// these are just additional info
if (botdebugger.IsDebugPaths())
ALERT(at_console, "<<PATHS>>Bot %s will AVOID all enemies that aren't in current weapon range (path #%d)\n",
pBot->name, pBot->curr_path_index + 1);
}
// remove avoid enemy task if the path is a "common path"
if (pBot->IsTask(TASK_AVOID_ENEMY) && !(w_paths[pBot->curr_path_index]->flags & P_FL_MISC_AVOID))
{
pBot->RemoveTask(TASK_AVOID_ENEMY);
if (botdebugger.IsDebugPaths())
ALERT(at_console, "<<PATHS>>Bot %s will no longer AVOID enemies! Any visible foe can be a target again (path #%d)\n",
pBot->name, pBot->curr_path_index + 1);
}
// set ignore enemy task if the path forces the bot to do so
if (!pBot->IsTask(TASK_IGNORE_ENEMY) && (w_paths[pBot->curr_path_index]->flags & P_FL_MISC_IGNORE))
{
pBot->SetTask(TASK_IGNORE_ENEMY);
if (botdebugger.IsDebugPaths())
ALERT(at_console, "<<PATHS>>Bot %s will IGNORE most enemies now (path %d)\n", pBot->name, pBot->curr_path_index + 1);
}
// remove ignore enemy task if the path is a "common path"
if (pBot->IsTask(TASK_IGNORE_ENEMY) && !(w_paths[pBot->curr_path_index]->flags & P_FL_MISC_IGNORE))
{
pBot->RemoveTask(TASK_IGNORE_ENEMY);
if (botdebugger.IsDebugPaths())
ALERT(at_console, "<<PATHS>>Bot %s will no longer IGNORE enemies! Any visible foe can be a target again (path #%d)\n",
pBot->name, pBot->curr_path_index + 1);
}
return;
}
/*
* path navigation (also handles a path change)
* returns 1 = successful path based navigation,
* 0 = something went wrong but keep path navigation (ie don't try standard wpt-to-wpt navigation),
* -1 = error (try standard waypoint-to-waypoint navigation)
*/
int BotOnPath(bot_t *pBot)
{
int path_next_wpt = -1;
bool found = FALSE;
bool found2 = FALSE;
bool path_end = TRUE;
W_PATH *p = nullptr;
// get pointer to path node that holds current waypoint (ie. find current waypoint in path)
if ((p = FindPointInPath (pBot->curr_wpt_index, pBot->curr_path_index)) != nullptr)
// CAN'T USE NOW
//if ((p = GetWaypointFromPath (pBot, pBot->curr_wpt_index, pBot->curr_path_index)) != NULL)
{
found = TRUE;
}
// current waypoint isn't in current path (ie bot must have changed path)
else
{
// check all paths for current waypoint
if (CheckPossiblePath(pBot, pBot->curr_wpt_index))
{
if ((p = FindPointInPath (pBot->curr_wpt_index, pBot->curr_path_index)) != nullptr )
// CAN'T USE NOW
// if ((p = GetWaypointFromPath (pBot, pBot->curr_wpt_index, pBot->curr_path_index)) != NULL )
{
found2 = TRUE;
}
}
}
// we found nothing ie current waypoint isn't in any path
if (p == nullptr)
{
// update path history
pBot->prev_path_index = pBot->curr_path_index;
pBot->curr_path_index = -1; // clear current path index
return -1;
}
// is standard path move (start -> end)
if ((pBot->opposite_path_dir == FALSE) && (path_next_wpt == -1))
{
const int forward_status = ForwardPathMove(pBot, p, path_next_wpt, path_end);
if (forward_status != 0)
{
return forward_status;
}
}
// is opposite direction path move (end -> start)
if ((pBot->opposite_path_dir) && (path_next_wpt == -1))
{
const int backward_status = BackwardPathMove(pBot, p, path_next_wpt, path_end);
if (backward_status != 0)
{
return backward_status;
}
}
// current waypoint has been found in one path AND we also found next waypoint
// in this path to continue to
if (((found) || (found2)) && (path_next_wpt != -1))
{
// is the current waypoint a goback wpt and NOT end of the path
if (IsFlagSet(W_FL_GOBACK, pBot->curr_wpt_index, pBot->bot_team) && !path_end)
{
// clear opposite_dir flag and set next waypoint in llist
if ((pBot->opposite_path_dir) && (p->next != nullptr))
{
pBot->opposite_path_dir = FALSE;
path_next_wpt = p->next->wpt_index;
}
// otherwise set opposite direction flag and use prev node waypoint
else if ((pBot->opposite_path_dir == FALSE) && (p->prev != nullptr))
{
pBot->opposite_path_dir = TRUE;
path_next_wpt = p->prev->wpt_index;
}
}
// set next path waypoint as a waypoint to head towards
pBot->curr_wpt_index = path_next_wpt;
// generate next waypoint fake origin
pBot->wpt_origin = GenerateOrigin(path_next_wpt);
pBot->f_waypoint_time = gpGlobals->time;
// set this time to prevent unwanted search for different waypoint
// while bot needs to do bigger turning at current wpt
// (ie if new waypoint is NOT in his view cone)
if (FInViewCone(&pBot->wpt_origin, pBot->pEdict) == FALSE)
pBot->f_face_wpt = gpGlobals->time + 0.6f;
// set correct behaviour based on the path flags
AssignPathBasedBehaviour(pBot, path_next_wpt);
if (botdebugger.IsDebugPaths())
{
char pmsg[128];
if (pBot->opposite_path_dir)
sprintf(pmsg, "<<PATHS>>current path is #%d | current waypoint is #%d | path direction is (end->start)\n",
pBot->curr_path_index + 1, pBot->curr_wpt_index + 1);
else
sprintf(pmsg, "<<PATHS>>current path is #%d | current waypoint is #%d | path direction is (start->end)\n",
pBot->curr_path_index + 1, pBot->curr_wpt_index + 1);
HudNotify(pmsg);
}
return 1;
}
#ifdef _DEBUG
char msg[80];
sprintf(msg, "BotOnPath()->unknown event | bot.opp_dir %d\n", pBot->opposite_path_dir);
UTIL_DebugDev(msg, pBot->curr_wpt_index, pBot->curr_path_index);
#endif
return -1;
}
/*
* find the nearest next waypoint from the current waypoint
*/
bool BotFindWaypoint(bot_t *pBot, bool ladder)
{
int next_waypoint = -1;
// NOTE: As of now this cannot happen!!!
// It should probably be changed to check for movetype == MOVETYPE_FLY
// and scrap the bool ladder variable altogether.
if (ladder)
{
next_waypoint = FindRightLadderWpt(pBot);
}
else
{
// look for nearby waypoints
next_waypoint = WaypointFindAvailable(pBot);
}
// is there at least one
if (next_waypoint != -1)
{
// update visited wpts history; prevents the loop effect
UpdateWptHistory(pBot);
pBot->curr_wpt_index = next_waypoint;
// generate next waypoint fake origin
pBot->wpt_origin = GenerateOrigin(next_waypoint);
pBot->f_waypoint_time = gpGlobals->time;
// is new wpt NOT in view cone so set some time to allow bigger turn
if (FInViewCone(&pBot->wpt_origin, pBot->pEdict) == FALSE)
pBot->f_face_wpt = gpGlobals->time + 0.6f;
return TRUE;
}
return FALSE;
}
/*
* head towards to your waypoint if close enough find next one
*/
bool BotHeadTowardWaypoint( bot_t *pBot )
{
float wpt_distance, wpt_range;
bool in_wpt_range; // when bot reaches the wpt range
bool past_the_wpt; // if bot run past current wpt set this and then double check if really reach its range
bool is_next_wpt; // do we have next wpt to head to
int unreachable_wpt_index = -1; // prevents the bot to take the same wpt where he got stuck before
bool use_only_planar; // in cases when we need to test just the 2D distance to waypoint (eg. tiny wpt range) NEW CODE 094
char dbgmsg[128]; // allows printing feedback for waypointer who's testing his work
edict_t *pEdict = pBot->pEdict;
// forget on any waypoint that is unreachable for longer time NEW CODE 094 (prev code)
// don't apply this when waiting or doing any action
//if ((pBot->f_waypoint_time + 5.0 < gpGlobals->time) &&
// (pBot->wpt_wait_time > 0.0) && (pBot->wpt_wait_time < gpGlobals->time) &&
// (pBot->wpt_action_time > 0.0) && (pBot->wpt_action_time < gpGlobals->time))
// forget on any waypoint that is unreachable for longer time
if (pBot->f_waypoint_time + 5.0f < gpGlobals->time)// NEW CODE 094
{
#ifdef _DEBUG
//@@@@@@@@@@@@@@@@
if (botdebugger.IsDebugStuck())
{
//sprintf(dbgmsg, "(nav.cpp) f_wpt_time=%3.f < globTIME=%3.f for wpt #%d ---- !!GENERAL!! !!GENERAL!! !!GENERAL!!\n",
// pBot->f_waypoint_time, gpGlobals->time, pBot->curr_wpt_index+1);
//HudNotify(dbgmsg);
}
#endif
// NEW CODE 094
// if the bot was stuck in last 5 seconds AND
// it's NOT soon after battle, because the bot can move far away from
// his waypoint while in battle so we must allow him to reset navigation
// if he isn't able to reach his waypoint anymore
// for example if he fell from roof during battle
if ((pBot->f_stuck_time + 5.0f >= gpGlobals->time) &&
(pBot->f_bot_see_enemy_time + 8.0f < gpGlobals->time) &&
(pBot->f_bot_wait_for_enemy_time + 8.0f < gpGlobals->time))
{
if (pBot->IsNeed(NEED_RESETNAVIG) == false)
{
// then update the waypoint time to compensate the time he lost trying to free self
pBot->f_waypoint_time = gpGlobals->time;
// and prepare the navigation reset
pBot->SetNeed(NEED_RESETNAVIG);
if (botdebugger.IsDebugPaths() || botdebugger.IsDebugStuck() || botdebugger.IsDebugWaypoints())
{
sprintf(dbgmsg, "<<WARNING>>%s was stuck while heading to waypoint #%d -> setting extra time to reach it!\n",
pBot->name, pBot->curr_wpt_index + 1);
HudNotify(dbgmsg, pBot);
}
}
else
{
// the bot already had 10 seconds to reach current waypoint
// but give him another 10 seconds...
if (pBot->f_waypoint_time + 10.0f < gpGlobals->time)
{
// if the bot is still stuck while trying to reach this waypoint
// then mark current waypoint as unreachable waypoint to allow resetting navigation
unreachable_wpt_index = pBot->curr_wpt_index;
if (botdebugger.IsDebugPaths() || botdebugger.IsDebugStuck() || botdebugger.IsDebugWaypoints())
{
sprintf(dbgmsg, "<<WARNING>>%s's extra stuck time is over & still can't reach the wpt -> going to RESET NAVIG!\n",
pBot->name);
HudNotify(dbgmsg, pBot);
}
}