-
Notifications
You must be signed in to change notification settings - Fork 9
/
bot_job_functions.cpp
3581 lines (2990 loc) · 125 KB
/
bot_job_functions.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)
//
// bot_job_functions.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 "extdll.h"
#include <meta_api.h>
#include "bot.h"
#include "waypoint.h"
#include "bot_func.h"
#include "bot_job_functions.h"
#include "bot_job_think.h"
#include "bot_navigate.h"
#include "bot_weapons.h"
extern chatClass chat; // bot chat stuff
// team data /////////////////////////
extern int RoleStatus[];
extern int team_allies[4];
extern int max_team_players[4];
extern int team_class_limits[4];
extern int spawnAreaWP[4]; // used for tracking the areas where each team spawns
extern int max_teams;
extern bot_t bots[32];
extern bot_weapon_t weapon_defs[MAX_WEAPONS];
// bot settings ////////////////// //TODO: convert bot_allow_humour to bool? [APG]RoboCop[CL]
extern int bot_allow_humour;
extern bool offensive_chatter;
extern bool defensive_chatter;
extern WAYPOINT waypoints[MAX_WAYPOINTS];
extern int num_waypoints; // number of waypoints currently in use
// area defs...
extern AREA areas[MAX_WAYPOINTS];
extern int num_areas;
// This function handles bot behaviour for the JOB_SEEK_WAYPOINT job.
// i.e. the bot can't find a waypoint to move towards, so try to move
// about until the bot can find one again.
int JobSeekWaypoint(bot_t* pBot) {
job_struct* job_ptr = &pBot->job[pBot->currentJob];
// never ignore enemies even though the bot is lost somewhere
if (pBot->enemy.ptr != nullptr) {
BotSetFacing(pBot, pBot->enemy.ptr->v.origin);
BotNavigateWaypointless(pBot);
return JOB_UNDERWAY;
}
// if on a ladder generally climb upwards(think like a spider!)
if (pBot->pEdict->v.movetype == MOVETYPE_FLY) {
// randomly jump off occasionally
if (pBot->f_periodicAlert3 < pBot->f_think_time && random_long(1, 1000) < 101) {
pBot->pEdict->v.button = 0; // in case IN_FORWARD is active
pBot->pEdict->v.button |= IN_JUMP;
pBot->f_pause_time = pBot->f_think_time + 1.0f; // so the jump will work
return JOB_UNDERWAY;
}
TraceResult tr;
UTIL_TraceLine(pBot->pEdict->v.origin, pBot->pEdict->v.origin + Vector(0, 0, 40.0f), dont_ignore_monsters, pBot->pEdict->v.pContainingEntity, &tr);
// if there is space above the bots head climb upwards
if (tr.flFraction >= 1.0f) {
pBot->pEdict->v.idealpitch = 90;
BotChangePitch(pBot->pEdict, 99999.0f);
}
}
// phase zero - try checking for a current waypoint first
if (job_ptr->phase == 0) {
BotFindCurrentWaypoint(pBot);
job_ptr->phase = 1;
return JOB_UNDERWAY;
}
// phase 1 - pick a direction for the bot to wander in
if (job_ptr->phase == 1) {
const Vector newAngle = Vector(0.0f, random_float(-180.0f, 180.0f), 0.0f);
UTIL_MakeVectors(newAngle);
const Vector v_forwards = pBot->pEdict->v.origin + gpGlobals->v_forward * 1000.0f;
// not the same direction the bot is facing already
if (!FInViewCone(v_forwards, pBot->pEdict)) {
job_ptr->origin = v_forwards;
job_ptr->phase = 2;
job_ptr->phase_timer = pBot->f_think_time + random_float(1.5f, 4.0f);
}
else
pBot->f_move_speed = pBot->f_max_speed / 2; // slow down silver!
}
// phase 2 - wander in a straight line for a few seconds
if (job_ptr->phase == 2) {
BotSetFacing(pBot, job_ptr->origin);
BotNavigateWaypointless(pBot);
pBot->pEdict->v.button |= IN_FORWARD; // in case bot is on a ladder
if (pBot->pEdict->v.waterlevel == WL_HEAD_IN_WATER) {
// always swim up if injured to avoid drowning
if (PlayerHealthPercent(pBot->pEdict) < 100)
pBot->f_vertical_speed = pBot->f_max_speed;
else
pBot->f_vertical_speed = 0.0f; // neutral, see what happens
}
// wandered long enough or gotten stuck?
if (job_ptr->phase_timer < pBot->f_think_time || (pBot->f_move_speed > 5.0f && pBot->pEdict->v.velocity.Length() < 0.1f)) {
job_ptr->phase = 0;
return JOB_UNDERWAY;
}
}
return JOB_UNDERWAY;
}
// This function handles bot behaviour for the JOB_GET_UNSTUCK job.
// i.e. the bot has unintentionally stopped moving so try moving in a random
// direction so that it may get unstuck.
int JobGetUnstuck(bot_t* pBot) {
job_struct* job_ptr = &pBot->job[pBot->currentJob];
// phase 0 - look for an open area to move towards
if (job_ptr->phase == 0) {
// if on a ladder jump off
if (pBot->pEdict->v.movetype == MOVETYPE_FLY) {
pBot->pEdict->v.button = 0; // in case IN_FORWARD is active
pBot->pEdict->v.button |= IN_JUMP;
pBot->f_pause_time = pBot->f_think_time + 1.0f; // so the jump will work
job_ptr->phase = 0; // back to square one
}
// try a few random directions, looking for an open area
for (int i = 0; i < 8; i++) {
Vector newAngle = Vector(0.0f, random_float(-180.0f, 180.0f), 0.0f);
UTIL_MakeVectors(newAngle);
// do a trace 300 units ahead of the new view angle to check for sight barriers
const Vector v_forwards = pBot->pEdict->v.origin + gpGlobals->v_forward * 300.0f;
TraceResult tr;
UTIL_TraceLine(pBot->pEdict->v.origin, v_forwards, dont_ignore_monsters, pBot->pEdict->v.pContainingEntity, &tr);
// if the new view angle is clear or a blockage is not too near
if (tr.flFraction >= 1.0f || !VectorsNearerThan(pBot->pEdict->v.origin, tr.vecEndPos, 80.0)) {
job_ptr->origin = tr.vecEndPos; // remember where to go
job_ptr->phase = 1;
job_ptr->phase_timer = pBot->f_think_time + random_float(1.0f, 3.0f);
// WaypointDrawBeam(INDEXENT(1), pBot->pEdict->v.origin,
// job_ptr->origin, 10, 2, 250, 250, 250, 200, 10);
return JOB_UNDERWAY;
}
}
return JOB_TERMINATED; // couldn't find a clear area nearby
}
// phase 1 - wander in a straight line for a second or two
if (job_ptr->phase == 1) {
BotSetFacing(pBot, job_ptr->origin);
pBot->f_move_speed = pBot->f_max_speed;
pBot->f_side_speed = 0.0f;
pBot->f_pause_time = 0.0f;
// in case bot connects with a ladder or movable crate
pBot->pEdict->v.button |= IN_FORWARD;
// jump occasionally, it might help(e.g. over hard to detect obstacles)
if (pBot->f_periodicAlert1 < pBot->f_think_time && random_long(1, 1000) < 334)
pBot->pEdict->v.button |= IN_JUMP;
// wandered long enough?
if (job_ptr->phase_timer < pBot->f_think_time || (pBot->pEdict->v.origin - job_ptr->origin).Length2D() < 20.0f) {
BlacklistJob(pBot, JOB_GET_UNSTUCK, 1.0f); // don't get caught in a loop
return JOB_TERMINATED;
}
}
return JOB_UNDERWAY;
}
// This function handles bot behaviour for the JOB_ROAM job.
// i.e. the bot will roam the map randomly(looking for trouble!).
// This low priority job is meant as a fallback if the bot has no jobs in it's buffer.
// Because it is a failsafe job it should never fail to work properly.
int JobRoam(bot_t* pBot) {
job_struct* job_ptr = &pBot->job[pBot->currentJob];
// make sure the bot has a waypoint destination
if (job_ptr->waypoint == -1) {
// pick a random plain waypoint
job_ptr->waypoint = WaypointFindRandomGoal_R(pBot->pEdict->v.origin, false, 8000.0f, -1, 0);
if (job_ptr->waypoint == -1) {
// this really shouldn't happen(unless the map has no waypoints)
return JOB_TERMINATED;
}
}
// check if the bot has arrived at the waypoint
if (pBot->current_wp == job_ptr->waypoint && VectorsNearerThan(waypoints[job_ptr->waypoint].origin, pBot->pEdict->v.origin, 50.0)) {
// wait for a bit then move on
if (pBot->f_periodicAlert1 < pBot->f_think_time && random_long(1, 1000) < 400)
job_ptr->waypoint = -1;
else {
pBot->f_move_speed = 0.0f;
pBot->f_side_speed = 0.0f;
BotLookAbout(pBot); // keep the bots eyes open
}
}
else {
pBot->goto_wp = job_ptr->waypoint;
if (!BotNavigateWaypoints(pBot, false)) {
// failure is not an option here, so force the selection of a new goal waypoint
job_ptr->waypoint = -1;
}
}
return JOB_UNDERWAY;
}
// This function handles bot behaviour for the JOB_CHAT job.
// i.e. the bot has something personal/humourous to say.
int JobChat(bot_t* pBot) {
job_struct* job_ptr = &pBot->job[pBot->currentJob];
// make the bot pause whilst "typing"
pBot->f_move_speed = 0.0f;
pBot->f_side_speed = 0.0f;
// give the bot time to return to it's waypoint afterwards
pBot->f_current_wp_deadline = pBot->f_think_time + BOT_WP_DEADLINE;
// phase zero - job initialisation
if (job_ptr->phase == 0) {
if (pBot->enemy.ptr == nullptr) {
job_ptr->phase = 1;
// create a delay so the bot can "type"
job_ptr->phase_timer = pBot->f_think_time + 2.0f;
}
}
// end phase - say the message
if (job_ptr->phase == 1 && job_ptr->phase_timer < pBot->f_think_time) {
// just in case!
job_ptr->message[MAX_CHAT_LENGTH - 1] = '\0';
UTIL_HostSay(pBot->pEdict, 0, job_ptr->message);
return JOB_TERMINATED; // job done
}
return JOB_UNDERWAY;
}
// This function handles bot behaviour for the JOB_REPORT job.
// i.e. the bot has something important to say to it's team.
int JobReport(bot_t* pBot) {
job_struct* job_ptr = &pBot->job[pBot->currentJob];
// make the bot pause whilst "typing"
pBot->f_move_speed = 0.0f;
pBot->f_side_speed = 0.0f;
// give the bot time to return to it's waypoint afterwards
pBot->f_current_wp_deadline = pBot->f_think_time + BOT_WP_DEADLINE;
// phase zero - initialisation
if (job_ptr->phase == 0) {
if (pBot->enemy.ptr == nullptr) {
job_ptr->phase = 1;
// create a delay so the bot can "type"
job_ptr->phase_timer = pBot->f_think_time + 2.0f;
}
}
// end phase - say the message
if (job_ptr->phase == 1 && job_ptr->phase_timer < pBot->f_think_time) {
// just in case!
job_ptr->message[MAX_CHAT_LENGTH - 1] = '\0';
UTIL_HostSay(pBot->pEdict, 1, job_ptr->message);
BlacklistJob(pBot, JOB_REPORT, 6.0f); // don't report the same thing too often [APG]RoboCop[CL]
return JOB_TERMINATED; // job done
}
return JOB_UNDERWAY;
}
// This function handles bot behaviour for the JOB_PICKUP_ITEM job.
// This will send a bot after a backpack, medikit, or armor that the bot has seen.
int JobPickUpItem(bot_t* pBot) {
job_struct* job_ptr = &pBot->job[pBot->currentJob];
/* if(pBot->f_periodicAlert3 > pBot->f_think_time)
{
char msg[96] = "";
snprintf(msg, 96, "picking up %s", item_name);
UTIL_HostSay(pBot->pEdict, 0, msg); //DebugMessageOfDoom!
}*/
// stop the bot from taking large route variations during this job
pBot->f_side_route_time = pBot->f_think_time + 5.0f;
pBot->sideRouteTolerance = 400; // very short route changes
// phase zero - go to the waypoint nearest the item
if (job_ptr->phase == 0) {
pBot->goto_wp = job_ptr->waypoint;
if (!BotNavigateWaypoints(pBot, false)) {
BlacklistJob(pBot, JOB_PICKUP_ITEM, random_float(5.0f, 15.0f));
return JOB_TERMINATED;
}
// if the bot has arrived at the waypoint nearest to the seen item
if (pBot->current_wp == job_ptr->waypoint && VectorsNearerThan(waypoints[pBot->current_wp].origin, pBot->pEdict->v.origin, 40.0)) {
// if you can see the item go pick it up, else dump the job
if (BotCanSeeOrigin(pBot, job_ptr->object->v.origin)) {
job_ptr->phase = 1;
job_ptr->phase_timer = pBot->f_think_time + 8.0f;
}
else
return JOB_TERMINATED;
}
}
// phase 1 - try to pick the item up
if (job_ptr->phase == 1) {
// took too long reaching it from the nearest waypoint?
if (job_ptr->phase_timer < pBot->f_think_time) {
BlacklistJob(pBot, JOB_PICKUP_FLAG, random_float(1.0f, 4.0f));
return JOB_TERMINATED;
}
// make sure the item is visible
if (!BotCanSeeOrigin(pBot, job_ptr->object->v.origin))
return JOB_TERMINATED;
BotSetFacing(pBot, job_ptr->object->v.origin);
BotNavigateWaypointless(pBot);
pBot->f_current_wp_deadline = pBot->f_think_time + BOT_WP_DEADLINE;
// if the bot is right on the item (treat the Z axis seperately for best accuracy)
if (pBot->pEdict->v.origin.z > job_ptr->object->v.origin.z - 37.0f && pBot->pEdict->v.origin.z < job_ptr->object->v.origin.z + 40.0f) {
if ((job_ptr->object->v.origin - pBot->pEdict->v.origin).Length2D() < 25.0f) {
// blacklist the job very briefly in case the item can't be picked up
// don't want the bot obssessing about it constantly
BlacklistJob(pBot, JOB_PICKUP_ITEM, 1.0f);
return JOB_TERMINATED; // success
}
}
}
return JOB_UNDERWAY;
}
// This function handles bot behaviour for the JOB_PICKUP_FLAG job.
// i.e. navigate to waypoint nearest to a previously seen flag and then
// try to pick it up.
int JobPickUpFlag(bot_t* pBot) {
job_struct* job_ptr = &pBot->job[pBot->currentJob];
// phase zero - go to the waypoint nearest the flag
if (job_ptr->phase == 0) {
pBot->goto_wp = job_ptr->waypoint;
if (!BotNavigateWaypoints(pBot, false)) {
BlacklistJob(pBot, JOB_PICKUP_FLAG, random_float(8.0f, 12.0f));
return JOB_TERMINATED;
}
// if the bot has arrived at the waypoint nearest to the seen flag
if (pBot->current_wp == job_ptr->waypoint && VectorsNearerThan(waypoints[pBot->current_wp].origin, pBot->pEdict->v.origin, 40.0)) {
job_ptr->phase = 1;
job_ptr->phase_timer = pBot->f_think_time + 8.0f;
}
}
// phase 1 - try to pick the flag up
if (job_ptr->phase == 1) {
// took too long reaching it from the nearest waypoint?
if (job_ptr->phase_timer < pBot->f_think_time) {
BlacklistJob(pBot, JOB_PICKUP_FLAG, random_float(5.0f, 10.0f));
return JOB_TERMINATED;
}
// make sure the flag is visible
if (!BotCanSeeOrigin(pBot, job_ptr->object->v.origin))
return JOB_TERMINATED;
BotSetFacing(pBot, job_ptr->object->v.origin);
BotNavigateWaypointless(pBot);
pBot->f_current_wp_deadline = pBot->f_think_time + BOT_WP_DEADLINE;
// if the bot is right on the flag (treat the Z axis seperately for best accuracy)
if (pBot->pEdict->v.origin.z > job_ptr->object->v.origin.z - 37.0f && pBot->pEdict->v.origin.z < job_ptr->object->v.origin.z + 60.0f) {
if ((job_ptr->object->v.origin - pBot->pEdict->v.origin).Length2D() < 25.0f)
job_ptr->phase = 2;
}
return JOB_UNDERWAY;
}
// phase 2 - can't pick it up, so defend and report the dropped flags location
if (job_ptr->phase == 2) {
// set up a job for defending the dropped flag
job_struct* newJob = InitialiseNewJob(pBot, JOB_DEFEND_FLAG);
if (newJob != nullptr) {
newJob->object = job_ptr->object;
newJob->origin = job_ptr->object->v.origin; // remember where it was seen
SubmitNewJob(pBot, JOB_DEFEND_FLAG, newJob);
}
// used to stop bots on each team over-reporting enemy dropped flags
static float f_flagReportTimeOut[4] = { 0.0f, 0.0f, 0.0f, 0.0f };
// set up a job for reporting the dropped flag's location
if (offensive_chatter && (f_flagReportTimeOut[pBot->current_team] < pBot->f_think_time || f_flagReportTimeOut[pBot->current_team] > pBot->f_think_time + 60.2f)) {
// reset the timeout
f_flagReportTimeOut[pBot->current_team] = pBot->f_think_time + random_float(40.0f, 60.0f);
// UTIL_HostSay(pBot->pEdict, 0, "Found dropped flag"); //DebugMessageOfDoom!
newJob = InitialiseNewJob(pBot, JOB_REPORT);
if (newJob != nullptr) {
const int area = AreaInsideClosest(pBot->pEdict);
if (area != -1) {
switch (pBot->current_team) {
case 0:
snprintf(newJob->message, MAX_CHAT_LENGTH, "Enemy has dropped flag %s", areas[area].namea);
break;
case 1:
snprintf(newJob->message, MAX_CHAT_LENGTH, "Enemy has dropped flag %s", areas[area].nameb);
break;
case 2:
snprintf(newJob->message, MAX_CHAT_LENGTH, "Enemy has dropped flag %s", areas[area].namec);
break;
case 3:
snprintf(newJob->message, MAX_CHAT_LENGTH, "Enemy has dropped flag %s", areas[area].named);
break;
default:
break;
}
SubmitNewJob(pBot, JOB_REPORT, newJob);
}
}
}
BlacklistJob(pBot, JOB_PICKUP_FLAG, 6.0f);
return JOB_TERMINATED; // job done
}
return JOB_UNDERWAY;
}
// This function handles bot behaviour for the JOB_PUSH_BUTTON job.
int JobPushButton(bot_t* pBot) {
job_struct* job_ptr = &pBot->job[pBot->currentJob];
// remember the center of the button
const Vector v_button = VecBModelOrigin(job_ptr->object);
// debugging stuff
// WaypointDrawBeam(INDEXENT(1), pBot->pEdict->v.origin + pBot->pEdict->v.view_ofs,
// v_button, 10, 2, 250, 250, 250, 200, 10);
// phase zero - figure out which button type it is and how to deal with it
if (job_ptr->phase == 0) {
// does the button activate only when something(e.g. a player) bumps into it?
if (job_ptr->object->v.spawnflags & SFLAG_PROXIMITY_BUTTON)
job_ptr->phase = 2;
else
job_ptr->phase = 1; // no, it's just a standard button that you use to activate
job_ptr->phase_timer = pBot->f_think_time + 8.0f;
}
// phase 1 - deal with normal use-to-activate buttons here
if (job_ptr->phase == 1) {
// taken too long getting to the button?
if (job_ptr->phase_timer < pBot->f_think_time) {
BlacklistJob(pBot, JOB_PUSH_BUTTON, 3.0f);
return JOB_TERMINATED;
}
BotSetFacing(pBot, v_button);
pBot->f_current_wp_deadline = pBot->f_think_time + BOT_WP_DEADLINE;
// approach if not near enough to the button
if (!VectorsNearerThan(pBot->pEdict->v.origin, v_button, 50.0)) {
BotNavigateWaypointless(pBot);
}
else {
pBot->f_pause_time = pBot->f_think_time + 0.2f;
const Vector vecStart = pBot->pEdict->v.origin + pBot->pEdict->v.view_ofs;
if (BotInFieldOfView(pBot, v_button - vecStart) < 15) {
pBot->pEdict->v.button = IN_USE;
pBot->f_use_button_time = pBot->f_think_time;
pBot->current_wp = WaypointFindNearest_E(pBot->pEdict, REACHABLE_RANGE, pBot->current_team);
return JOB_TERMINATED; // success
}
}
}
// phase 2 - get fairly close to what must be a proximity button
if (job_ptr->phase == 2) {
// taken too long getting to the button?
if (job_ptr->phase_timer < pBot->f_think_time) {
BlacklistJob(pBot, JOB_PUSH_BUTTON, 3.0f);
return JOB_TERMINATED;
}
BotSetFacing(pBot, v_button);
pBot->f_current_wp_deadline = pBot->f_think_time + BOT_WP_DEADLINE;
// approach if not near enough to the button
if (!VectorsNearerThan(pBot->pEdict->v.origin, v_button, 60.0))
BotNavigateWaypointless(pBot);
else {
job_ptr->phase = 3;
job_ptr->phase_timer = pBot->f_think_time + random_float(0.3f, 0.6f);
}
}
// phase 3 - keep walking into the proximity button for a fraction of a second
// to make sure the bot has pushed up against it
if (job_ptr->phase == 3) {
BotSetFacing(pBot, v_button);
BotNavigateWaypointless(pBot);
// walked into the button long enough?
if (job_ptr->phase_timer < pBot->f_think_time) {
pBot->f_use_button_time = pBot->f_think_time; // assume it got activated
pBot->current_wp = WaypointFindNearest_E(pBot->pEdict, REACHABLE_RANGE, pBot->current_team);
return JOB_TERMINATED; // success
}
}
return JOB_UNDERWAY;
}
// This function handles bot behaviour for the JOB_USE_TELEPORT job.
// i.e. go to a waypoint near the teleport, hop onboard and use the teleport.
// If it doesn't work, forget about it.
int JobUseTeleport(bot_t* pBot) {
job_struct* job_ptr = &pBot->job[pBot->currentJob];
// phase zero - go to the waypoint near the Teleporter
if (job_ptr->phase == 0) {
if (pBot->current_wp == job_ptr->waypoint && VectorsNearerThan(waypoints[pBot->current_wp].origin, pBot->pEdict->v.origin, 30.0)) {
job_ptr->phase = 1;
job_ptr->phase_timer = pBot->f_think_time + 6.0f;
}
else {
pBot->goto_wp = job_ptr->waypoint;
if (!BotNavigateWaypoints(pBot, false)) {
BlacklistJob(pBot, JOB_USE_TELEPORT, random_float(5.0f, 20.0f));
return JOB_TERMINATED;
}
}
}
// phase 1 - go stand on the Teleporter
if (job_ptr->phase == 1) {
// figure out the Teleporters center for maximum accuracy
const Vector TeleportCenter = job_ptr->object->v.absmin + job_ptr->object->v.size / 2;
// is there some reason to abort?
// e.g. took too long, teleport is occupied, or bot is fighting
if (pBot->enemy.ptr != nullptr || job_ptr->phase_timer < pBot->f_think_time || BotAllyAtVector(pBot, TeleportCenter, 70.0, false) != nullptr) {
BlacklistJob(pBot, JOB_USE_TELEPORT, 6.0);
return JOB_TERMINATED;
}
BotSetFacing(pBot, TeleportCenter);
BotNavigateWaypointless(pBot);
const float distance2D = (TeleportCenter - pBot->pEdict->v.origin).Length2D();
// slow down on approach
if (distance2D < 100.0f)
pBot->f_move_speed = pBot->f_max_speed / 3;
// is the bot standing on it? (treat the Z axis seperately for best accuracy)
if (pBot->pEdict->v.origin.z > TeleportCenter.z - 10.0f && pBot->pEdict->v.origin.z < TeleportCenter.z + 90.0f && distance2D < 20.0f) {
// UTIL_BotLogPrintf("%s, waiting at %d, distance %f(2D %f), speed %f, time %f\n",
// pBot->name,
// job_ptr->waypoint,
// (pBot->pEdict->v.origin - job_ptr->object->v.origin).Length(),
// (pBot->pEdict->v.origin - TeleportCenter).Length2D(),
// (pBot->pEdict->v.velocity).Length(),
// pBot->f_think_time);
job_ptr->phase = 2;
job_ptr->phase_timer = pBot->f_think_time + random_float(1.8f, 3.5f);
return JOB_UNDERWAY;
}
}
// phase 2 - wait a few seconds, if nothing happens blacklist the job
// for a short time because the teleporter is not working
if (job_ptr->phase == 2) {
/* if(pBot->f_periodicAlert1 < pBot->f_think_time)
UTIL_BotLogPrintf("%s, waiting at %d, distance %f\n",
pBot->name, job_ptr->waypoint,
(pBot->pEdict->v.origin - job_ptr->object->v.origin).Length());*/
pBot->f_move_speed = 0.0f;
pBot->f_side_speed = 0.0f;
pBot->f_vertical_speed = 0.0f;
pBot->f_pause_time = pBot->f_think_time + 0.3f;
BotLookAbout(pBot);
// has the Teleporter worked?
if (!VectorsNearerThan(pBot->pEdict->v.origin, job_ptr->object->v.origin, 500.0)) {
// UTIL_BotLogPrintf("%s, beamed over via %d, distance %f, time %f\n",
// pBot->name, job_ptr->waypoint,
// (pBot->pEdict->v.origin - job_ptr->object->v.origin).Length(),
// pBot->f_think_time);
job_ptr->phase = 3;
return JOB_UNDERWAY;
}
// is the teleporter non-functional? if so forget about it for a while
if (job_ptr->phase_timer < pBot->f_think_time) {
// recall the memory index of the entrance the bot just tested so the bot
// can then remember it as being non-functional
const int teleIndex = BotRecallTeleportEntranceIndex(pBot, job_ptr->object);
if (teleIndex != -1)
BotForgetTeleportPair(pBot, teleIndex);
// UTIL_BotLogPrintf("%s, teleporter at %d is idle, time %f\n",
// pBot->name, job_ptr->waypoint, pBot->f_think_time);
BlacklistJob(pBot, JOB_USE_TELEPORT, random_float(25.0f, 40.0f));
return JOB_TERMINATED;
}
// no jumping around foo
if (pBot->pEdict->v.button & IN_JUMP)
pBot->pEdict->v.button &= ~IN_JUMP;
}
// phase 3 - find and remember the exit(beneath the bot)
if (job_ptr->phase == 3) {
// recall the index of the entrance the bot just used
int teleIndex = BotRecallTeleportEntranceIndex(pBot, job_ptr->object);
if (teleIndex == -1) {
teleIndex = BotGetFreeTeleportIndex(pBot);
// hopefully this shouldn't happen, but just in case
if (teleIndex == -1) {
// UTIL_BotLogPrintf("%s, doh! -1\n", pBot->name);
return JOB_TERMINATED;
}
}
// find the exit(hopefully!)
const edict_t* teleExit = BotEntityAtPoint("building_teleporter", pBot->pEdict->v.origin, 90.0f);
// look for a waypoint near the exit
if (teleExit != nullptr) {
pBot->current_wp = WaypointFindNearest_E(pBot->pEdict, REACHABLE_RANGE, pBot->current_team);
// welcome space cadet!
if (pBot->current_wp != -1) {
// UTIL_BotLogPrintf("%s, teleported to %d\n", pBot->name, pBot->current_wp);
pBot->telePair[teleIndex].entrance = job_ptr->object;
pBot->telePair[teleIndex].entranceWP = job_ptr->waypoint;
pBot->telePair[teleIndex].exitWP = pBot->current_wp;
}
else
BotForgetTeleportPair(pBot, teleIndex);
}
return JOB_TERMINATED; // regardless of success or failure
}
return JOB_UNDERWAY;
}
// This function handles bot behaviour for the JOB_MAINTAIN_OBJECT job.
// Navigate to a known damaged sentry/dispensor or un-upgraded sentry and give it
// a few whacks with a wrench.
int JobMaintainObject(bot_t* pBot) {
job_struct* job_ptr = &pBot->job[pBot->currentJob];
// phase zero - set a destination waypoint near the object
if (job_ptr->phase == 0) {
job_ptr->waypoint = WaypointFindNearest_E(job_ptr->object, 500.0f, pBot->current_team);
job_ptr->phase = 1;
return JOB_UNDERWAY;
}
// phase 1 - go to the destination waypoint near the object
if (job_ptr->phase == 1) {
// check if the bot has stumbled into the object before it's reached the waypoint
if (VectorsNearerThan(pBot->pEdict->v.origin, job_ptr->object->v.origin, 85.0) && BotCanSeeOrigin(pBot, job_ptr->object->v.origin)) {
job_ptr->phase = 2;
return JOB_UNDERWAY;
}
if (pBot->current_wp == job_ptr->waypoint && VectorsNearerThan(waypoints[pBot->current_wp].origin, pBot->pEdict->v.origin, 40.0))
job_ptr->phase = 2;
else {
pBot->goto_wp = job_ptr->waypoint;
if (!BotNavigateWaypoints(pBot, false)) {
BlacklistJob(pBot, JOB_MAINTAIN_OBJECT, random_float(5.0f, 15.0f));
return JOB_TERMINATED;
}
}
return JOB_UNDERWAY;
}
// phase 2 - check if the object still needs maintenance
if (job_ptr->phase == 2) {
char className[24];
std::strncpy(className, STRING(job_ptr->object->v.classname), 24);
className[23] = '\0';
if (std::strcmp("building_sentrygun", className) == 0) {
bool maintenanceNeeded = false;
if (job_ptr->object->v.health < 100)
maintenanceNeeded = true;
if (job_ptr->object == pBot->sentry_edict && pBot->sentry_ammo < 100)
maintenanceNeeded = true;
else { // sentry needs upgrading?
char modelName[24];
std::strncpy(modelName, STRING(job_ptr->object->v.model), 24);
modelName[23] = '\0';
if (std::strcmp(modelName, "models/sentry3.mdl") != 0)
maintenanceNeeded = true;
}
if (maintenanceNeeded == false)
return JOB_TERMINATED; // maintenance is uneeded
}
else if (std::strcmp("building_dispenser", className) == 0) {
if (job_ptr->object->v.health > 99)
return JOB_TERMINATED; // maintenance is uneeded
}
job_ptr->phase = 3;
job_ptr->phase_timer = pBot->f_think_time + 6.0f;
return JOB_UNDERWAY;
}
// phase 3 - get close to the object
if (job_ptr->phase == 3) {
// having problems getting to the object?
if (job_ptr->phase_timer < pBot->f_think_time || !BotCanSeeOrigin(pBot, job_ptr->object->v.origin)) {
BlacklistJob(pBot, JOB_MAINTAIN_OBJECT, random_float(12.0f, 24.0f));
return JOB_TERMINATED;
}
BotSetFacing(pBot, job_ptr->object->v.origin);
//BotNavigateWaypointless(pBot);
//pBot->f_current_wp_deadline = pBot->f_think_time + BOT_WP_DEADLINE;
if (pBot->enemy.ptr == nullptr && pBot->current_weapon.iId != TF_WEAPON_SPANNER)
UTIL_SelectItem(pBot->pEdict, "tf_weapon_spanner");
if (VectorsNearerThan(pBot->pEdict->v.origin, job_ptr->object->v.origin, 60.0)) {
job_ptr->phase = 4;
job_ptr->phase_timer = pBot->f_think_time + random_float(2.0f, 3.0f);
}
}
// final phase - keep whacking the object for a few seconds
if (job_ptr->phase == 4) {
// abort if the object can't be seen
if (!BotCanSeeOrigin(pBot, job_ptr->object->v.origin))
return JOB_TERMINATED;
if (job_ptr->phase_timer < pBot->f_think_time)
return JOB_TERMINATED; // enough time spent here - job done
BotSetFacing(pBot, job_ptr->object->v.origin);
//BotNavigateWaypointless(pBot);
//pBot->f_current_wp_deadline = pBot->f_think_time + BOT_WP_DEADLINE;
if (pBot->current_weapon.iId != TF_WEAPON_SPANNER)
UTIL_SelectItem(pBot->pEdict, "tf_weapon_spanner");
else
pBot->pEdict->v.button |= IN_ATTACK; // have at thee ya varlet!
}
return JOB_UNDERWAY;
}
// This function handles bot behaviour for the JOB_BUILD_SENTRY job.
// i.e. go to a sentry waypoint and build a sentry there.
int JobBuildSentry(bot_t* pBot) {
job_struct* job_ptr = &pBot->job[pBot->currentJob];
// phase 0, getting into position
if (job_ptr->phase == 0) {
if (pBot->current_wp == job_ptr->waypoint) {
if (!VectorsNearerThan(pBot->pEdict->v.origin, waypoints[job_ptr->waypoint].origin, 20.0)) {
// abort the job if there's a sentry gun here already
if (BotEntityAtPoint("building_sentrygun", waypoints[pBot->current_wp].origin, 300.0f)) {
// UTIL_HostSay(pBot->pEdict, 0, "sentry gun here already"); //DebugMessageOfDoom!
BlacklistJob(pBot, JOB_BUILD_SENTRY, random_float(10.0f, 20.0f));
return JOB_TERMINATED;
}
}
else // the bot has arrived at the waypoint
{
pBot->f_move_speed = 0.0f;
pBot->f_side_speed = 0.0f;
// make sure the bot is facing the right way before continuing
const int aim_index = WaypointFindNearestAiming(waypoints[job_ptr->waypoint].origin);
if (aim_index != -1) {
// WaypointDrawBeam(INDEXENT(1), waypoints[job_ptr->waypoint].origin + Vector(0, 0, 60),
// waypoints[aim_index].origin, 10, 2, 250, 250, 250, 200, 10);
BotSetFacing(pBot, waypoints[aim_index].origin);
const Vector v_aim = waypoints[aim_index].origin - pBot->pEdict->v.origin;
if (BotInFieldOfView(pBot, v_aim) == 0) {
job_ptr->phase = 1;
job_ptr->phase_timer = pBot->f_think_time + 0.5f;
FakeClientCommand(pBot->pEdict, "build", "2", nullptr);
}
return JOB_UNDERWAY;
}
// no waypoint aim indicator found
BlacklistJob(pBot, JOB_BUILD_SENTRY, random_float(10.0f, 20.0f));
return JOB_TERMINATED;
}
}
pBot->goto_wp = job_ptr->waypoint;
if (!BotNavigateWaypoints(pBot, false)) {
BlacklistJob(pBot, JOB_BUILD_SENTRY, random_float(10.0f, 20.0f));
return JOB_TERMINATED;
}
}
// phase 1 - walk backwards for a second whilst trying to start the build
if (job_ptr->phase == 1) {
if (pBot->f_periodicAlertFifth < pBot->f_think_time)
FakeClientCommand(pBot->pEdict, "build", "2", nullptr);
pBot->f_move_speed = -(pBot->f_max_speed / 2);
// find the sentry gun being built(if it is) and remember it's location
edict_t* pent = nullptr;
while ((pent = FIND_ENTITY_BY_CLASSNAME(pent, "building_sentrygun_base")) != nullptr && !FNullEnt(pent)) {
// UTIL_BotLogPrintf("%s: sentry gun base distance %f\n",
// pBot->name, (pBot->pEdict->v.origin - pent->v.origin).Length());
// check that this sentry gun is near to where the bot was
// when it started building
if (!(pent->v.flags & FL_KILLME) && VectorsNearerThan(pBot->pEdict->v.origin, pent->v.origin, 85.0)) {
job_ptr->origin = pent->v.origin;
pBot->f_move_speed = 0.0f;
job_ptr->phase = 2;
job_ptr->phase_timer = pBot->f_think_time + 7.0f;
return JOB_UNDERWAY;
}
}
// waited too long and nothing happened?
if (job_ptr->phase_timer < pBot->f_think_time) {
// don't repeat the job for a while
BlacklistJob(pBot, JOB_BUILD_SENTRY, random_float(20.0f, 40.0f));
// in case bot didn't know it already had a sentry(rare but possible)
FakeClientCommand(pBot->pEdict, "detsentry", nullptr, nullptr);
return JOB_TERMINATED;
}
}
// phase 2 - keep an eye out for the sentry gun once it's built
if (job_ptr->phase == 2) {
if (job_ptr->phase_timer < pBot->f_think_time) {
// find and remember the sentry gun the bot just built
constexpr bool success = false;
edict_t* pent = nullptr;
while ((pent = FIND_ENTITY_BY_CLASSNAME(pent, "building_sentrygun")) != nullptr && !FNullEnt(pent)) {
// UTIL_BotLogPrintf("%s: sentry gun distance %f\n",
// pBot->name, (job_ptr->origin - pent->v.origin).Length());
// check that this sentry gun is near to where the bot was
// when it started building
if (VectorsNearerThan(job_ptr->origin, pent->v.origin, 50.0)) {
pBot->has_sentry = true;
pBot->sentry_edict = pent;
pBot->sentryWaypoint = job_ptr->waypoint;
// rotate the sentry if necessary
if (waypoints[job_ptr->waypoint].flags & W_FL_TFC_SENTRY_180) {
// UTIL_HostSay(pBot->pEdict, 0, "rotating SG"); //DebugMessageOfDoom!
FakeClientCommand(pBot->pEdict, "rotatesentry180", nullptr, nullptr);
pBot->SGRotated = true;
}
job_ptr->phase = 3;
return JOB_UNDERWAY;
}
}
// abort if the bot has been waiting for the sentry gun and it isn't there
if constexpr (success == false) { // don't repeat the job for a while
BlacklistJob(pBot, JOB_BUILD_SENTRY, random_float(20.0f, 40.0f));
return JOB_TERMINATED;
}
}
}
// final phase - report the sentry guns location
if (job_ptr->phase == 3) {
if (defensive_chatter) {
const int bot_area = AreaInsideClosest(pBot->pEdict);
if (bot_area != -1) {
job_struct* newJob = InitialiseNewJob(pBot, JOB_REPORT);
if (newJob != nullptr) {
switch (pBot->current_team) {
case 0:
snprintf(newJob->message, MAX_CHAT_LENGTH, "Sentry gun built: %s", areas[bot_area].namea);
break;
case 1:
snprintf(newJob->message, MAX_CHAT_LENGTH, "Sentry gun built: %s", areas[bot_area].nameb);
break;
case 2:
snprintf(newJob->message, MAX_CHAT_LENGTH, "Sentry gun built: %s", areas[bot_area].namec);
break;
case 3:
snprintf(newJob->message, MAX_CHAT_LENGTH, "Sentry gun built: %s", areas[bot_area].named);
break;
default:
return JOB_TERMINATED;
}
newJob->message[MAX_CHAT_LENGTH - 1] = '\0';
SubmitNewJob(pBot, JOB_REPORT, newJob);
}
}
}
return JOB_TERMINATED; // job done!
}
return JOB_UNDERWAY;
}
// This function handles bot behaviour for the JOB_BUILD_DISPENSER job.
int JobBuildDispenser(bot_t* pBot) {
job_struct* job_ptr = &pBot->job[pBot->currentJob];
// phase 0, getting into position
if (job_ptr->phase == 0) {