-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbot.cpp
5638 lines (4555 loc) · 167 KB
/
bot.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.cpp
//
////////////////////////////////////////////////////////////////////////////////////////////////
#if defined(WIN32)
#pragma warning(disable: 4005 91)
#endif
#include "defines.h"
#include "extdll.h"
#include "util.h"
#include "cbase.h"
#include "bot.h"
#include "bot_func.h"
#include "bot_manager.h"
#include "waypoint.h"
#include "bot_weapons.h"
#include <sys/types.h>
#include <sys/stat.h>
#ifndef __linux__
extern HINSTANCE h_Library;
#else
extern void *h_Library;
#endif
extern bool is_dedicated_server;
extern bool g_debug_bot_on;
extern edict_t* g_debug_bot;
//extern int team_class_limits[4]; // this might be useful
extern char bot_whine[MAX_BOT_WHINE][81];
extern int whine_count;
static FILE *fp;
bot_t *bots = nullptr; // no bots in a game yet
float bot_t::harakiri_moment = 0.0;
extern int debug_engine;
extern int recent_bot_whine[5];
int number_names = 0;
botname_t bot_names[MAX_BOT_NAMES]; // array of bot names read from external file
// few function prototypes used in this file
bool IsEntityInSphere(const char* entity_name, edict_t *pEdict, float radius);
//int BotInFieldOfView(bot_t *pBot, Vector dest);
bool BotEntityIsVisible( bot_t *pBot, Vector dest );
void BotFindItem( bot_t *pBot );
bool IsBleeding(edict_t *pPatient);
bool BotHealTeammate(bot_t *pBot);
bool BotCheckMidAir(edict_t *pEdict);
void BotCheckSkillSystem();
void BotSelectBackupWeapon(bot_t *pBot);
void BotSelectKnife(bot_t *pBot);
inline edict_t *CREATE_FAKE_CLIENT( const char *netname )
{
return (*g_engfuncs.pfnCreateFakeClient)( netname );
}
inline char *GET_INFOBUFFER( edict_t *e )
{
return (*g_engfuncs.pfnGetInfoKeyBuffer)( e );
}
inline char *GET_INFO_KEY_VALUE( char *infobuffer, char *key )
{
return (g_engfuncs.pfnInfoKeyValue( infobuffer, key ));
}
inline void SET_CLIENT_KEY_VALUE( int clientIndex, char *infobuffer,
char *key, char *value )
{
(*g_engfuncs.pfnSetClientKeyValue)( clientIndex, infobuffer, key, value );
}
// this is the LINK_ENTITY_TO_CLASS function that creates a player (bot)
void player( entvars_t *pev )
{
static LINK_ENTITY_FUNC otherClassName = nullptr;
if (otherClassName == nullptr)
otherClassName = (LINK_ENTITY_FUNC)GetProcAddress(h_Library, "player");
if (otherClassName != nullptr)
{
(*otherClassName)(pev);
}
}
bot_t::bot_t()
{
is_used = false;
respawn_state =0;
pEdict = nullptr;
need_to_initialize = false;
name[0] = '\0';
clan_tag = 0;
not_started = 0;
start_action = 0;
kick_time = 0.0f;
create_time = 0.0f;
bot_skill = 0;
aim_skill = 0;
bot_team = NO_VAL;
bot_class = NO_VAL;
pcust_class = nullptr;
bot_skin = NO_VAL;
bot_behaviour = 0;
idle_angle = 0.0f;
round_end = false;
move_speed = SPEED_NO; // probably move it to spawninit()
bot_spawn_time = 0.0f;
// killer_edict = NULL;
weapon_status = 0;
claymore_slot = NO_VAL;
bot_fa_skill = 0;
bot_bandages = 0; // not sure about this it should probably be in spawninit()
main_weapon = NO_VAL;
backup_weapon = NO_VAL;
forced_usage = 0;
grenade_slot = NO_VAL;
main_no_ammo = true;
backup_no_ammo = true;
take_main_mags = 0; // both most probably to spawninit() as well
take_backup_mags= 0;
BotSpawnInit();
prev_wpt_index.print();
}
/*
* sets nearly all bot variables to initial/default value
* needed after the bot has been killed or when the bot is joining the game
*/
void bot_t::BotSpawnInit()
{
#ifdef _DEBUG
HudNotify("Bot Spawn Init\n", this);
// NEW CODE 094 (remove it)
if (strlen(name) > 1)
{
if (g_debug_bot_on && (g_debug_bot == pEdict) || !g_debug_bot_on)
{
char dm[256];
sprintf(dm, "%s is respawning\n", name);
UTIL_DebugInFile(dm);
}
}
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
#endif
bot_tasks = 0;
bot_subtasks = 0;
bot_needs = 0;
prev_time = gpGlobals->time;
v_prev_origin = Vector(9999.0f, 9999.0f, 9999.0f);
f_dontmove_time = gpGlobals->time;
pItem = nullptr;
f_face_item_time = 0.0f;
wpt_origin = g_vecZero;//Vector(0, 0, 0);
prev_wpt_origin = g_vecZero;//Vector(0, 0, 0);
f_waypoint_time = gpGlobals->time;
curr_wpt_index = -1;
prev_wpt_index.clear();
crowded_wpt_index = -1;
//pBot->waypoint_goal = -1;
//pBot->f_waypoint_goal_time = 0.0;
//pBot->waypoint_near_flag = FALSE;
//pBot->waypoint_flag_origin = Vector(0, 0, 0);
prev_wpt_distance = 9999.0f;
wpt_wait_time = 0.0f; // must be 0.0 otherwise bot use it right after respawn
wpt_action_time = 0.0f; // must be 0.0 otherwise bot use it right after respawn
f_face_wpt = 0.0f;
clear_path();
curr_path_index = -1;
prev_path_index = -1;
opposite_path_dir = FALSE;
patrol_path_wpt = -1;
msecnum = 0;
msecdel = 0.0f;
msecval = 0.0f;
bot_health = 0;
bot_armor = 0;
bot_weapons = 0;
blinded_time = 0.0f;
f_max_speed = CVAR_GET_FLOAT("sv_maxspeed");
prev_speed = 0; // fake "paused" since bot is NOT stuck
f_strafe_direction = 0.0f;
f_strafe_time = 0.0f;
hide_time = -10.0f; // must be - bot test if (ht+5 < globtime)
ladder_dir = LADDER_UNKNOWN;
f_start_ladder_time = 0.0f;
waypoint_top_of_ladder = FALSE;
end_wpt_index = -1;
f_wall_check_time = 0.0f;
f_wall_on_right = 0.0f;
f_wall_on_left = 0.0f;
f_dont_avoid_wall_time = 0.0f;
f_dont_look_for_waypoint_time = 0.0f;
//pBot->f_jump_time = 0.0f;
f_duckjump_time = 0.0f;
SetDontCheckStuck(); // prevents false running of unstuck when spawning
f_stuck_time = 0.0f;
changed_direction = 0;
f_check_deathfall = 0.0f;
// pick a wander direction (50% of the time to the left, 50% to the right)
if (RANDOM_LONG(1, 100) <= 50)
wander_dir = SIDE_LEFT;
else
wander_dir = SIDE_RIGHT;
pBotEnemy = nullptr;
f_bot_see_enemy_time = 0.0f; // set it to zero to prevent "area clear" after spawn (at least for now)
f_bot_find_enemy_time = gpGlobals->time;
pBotPrevEnemy = nullptr;
f_bot_wait_for_enemy_time = gpGlobals->time;
v_last_enemy_position = g_vecZero;//Vector(0, 0, 0);
f_prev_enemy_dist = 0.0f;
f_reaction_time = 0.0f;
pBotUser = nullptr;
f_bot_use_time = 0.0f;
// b_bot_say_killed = FALSE;
// f_bot_say_killed = 0.0;
aim_index[0] = -1;
aim_index[1] = -1;
aim_index[2] = -1;
aim_index[3] = -1;
curr_aim_index = -1;
f_aim_at_target_time = 0.0;
f_check_aim_time = 0.0;
targeting_stop = 0;
weapon_action = W_READY;
// weapon status is initialized just once at bot creation so we must clear these manually
RemoveWeaponStatus(WS_PRESSRELOAD);
RemoveWeaponStatus(WS_INVALID);
RemoveWeaponStatus(WS_NOTEMPTYMAG);
RemoveWeaponStatus(WS_MERGEMAGS1);
RemoveWeaponStatus(WS_MERGEMAGS2);
RemoveWeaponStatus(WS_CANTBIPOD);
f_shoot_time = gpGlobals->time;
f_fullauto_time = -1.0;
f_reload_time = 0.0;
ClearPauseTime();// f_pause_time = 0.0;
f_sound_update_time = 0.0;
f_look_for_ground_items = 0.0;
v_ground_item = g_vecZero;//Vector(0,0,0);
f_use_clay_time = 0.0;
clay_action = ALTW_NOTUSED;
//pBot->b_use_button = FALSE;
//pBot->f_use_button_time = 0;
//pBot->b_lift_moving = FALSE;
//pBot->b_use_capture = FALSE;
//pBot->f_use_capture_time = 0.0;
//pBot->pCaptureEdict = NULL;
v_door = g_vecZero;//Vector(0, 0, 0);
f_bipod_time = 0.0;
chute_action_time = 0.0;
bot_prev_health = 100;
bandage_time = 0.0;
f_medic_treat_time = 0.0;
f_cant_prone = 0.0;
f_go_prone_time = 0.0;
search_closest_time = 0.0;
speak_time = 0.0;
text_msg_time = 0.0;
prev_msg = 0;
grenade_time = 0.0;
grenade_action = ALTW_NOTUSED;
secondary_active = FALSE;
sniping_time = 0.0;
SetTask(TASK_CHECKAMMO); // set this task so the bot has to check it right after spawn
check_ammunition_time = gpGlobals->time + 2.0f; // don't check it during spawning
f_combat_advance_time = gpGlobals->time;
f_overide_advance_time = gpGlobals->time;
f_check_stance_time = gpGlobals->time;
f_stance_changed_time = 0.0f;
memset(&(current_weapon), 0, sizeof(current_weapon));
memset(&(curr_rgAmmo), 0, sizeof(curr_rgAmmo));// array of current amount of mags
harakiri = false;
#ifdef _DEBUG
curr_aim_offset = g_vecZero;//Vector(0, 0, 0);
target_aim_offset = g_vecZero;//Vector(0, 0, 0);
is_forced = false;
forced_stance = BOT_STANDING;
#endif
}
/*
* reads names from external file and puts them to global array of bot names
*/
void BotNameInit()
{
char bot_name_filename[256];
char name_buffer[80];
UTIL_MarineBotFileName(bot_name_filename, "marine_dog-tags.txt", nullptr);
FILE* bot_name_fp = fopen(bot_name_filename, "r");
if (bot_name_fp != nullptr)
{
while ((number_names < MAX_BOT_NAMES) && (fgets(name_buffer, 80, bot_name_fp) != nullptr))
{
int length = strlen(name_buffer);
// handle commented lines in the file (skip them)
if (name_buffer[0] == '#')
continue;
if ((length > 0) && (name_buffer[length-1] == '\n'))
{
name_buffer[length-1] = 0; // remove '\n'
length--;
}
int str_index = 0;
while (str_index < length)
{
if ((name_buffer[str_index] < ' ') || (name_buffer[str_index] > '~') ||
(name_buffer[str_index] == '"'))
{
for (int index = str_index; index < length; index++)
name_buffer[index] = name_buffer[index+1];
}
str_index++;
}
if (name_buffer[0] != 0)
{
// name can only be long up to 28 chars due to the '[MB]' sign
strncpy(bot_names[number_names].name, name_buffer, BOT_NAME_LEN - 4);
number_names++;
}
}
fclose(bot_name_fp);
}
}
/*
* pick any free (not used) name from global array of bot names
* adds Marine Bot sign/tag if needed (allowed by .cfg variable)
*/
void BotPickName( char *name_buffer )
{
int attempts = 0;
int name_index = RANDOM_LONG(1, number_names) - 1; // zero based
// check make sure this name isn't used
bool used = TRUE;
while (used)
{
// is there another bot using this name?
if (bot_names[name_index].is_used)
{
name_index++;
if (name_index == number_names)
name_index = 0;
attempts++;
}
// otherwise this name should be free, but we need to test all existing clients
else
{
// check all existing clients for this name
for (int index = 1; index <= gpGlobals->maxClients; index++)
{
edict_t *pPlayer = INDEXENT(index);
if (pPlayer && !pPlayer->free)
{
// check if the name is part of this client's name
if (strstr(STRING(pPlayer->v.netname), bot_names[name_index].name) != nullptr)
{
// we found that another bot uses this name so set correct flag
bot_names[name_index].is_used = TRUE;
}
}
}
// this name must really be free so use it
if (bot_names[name_index].is_used == FALSE)
used = FALSE;
}
// break out of loop even if already used
if (attempts == number_names)
used = FALSE;
}
// if needed insert the '[MB]' tag right before the name
if (externals.GetRichNames())
{
// insert Marine Bot tag before the name
strcpy(name_buffer, "[MB]");
// and add the standard name behing the tag
strcat(name_buffer, bot_names[name_index].name);
// this name is beeing used from now
bot_names[name_index].is_used = TRUE;
}
// otherwise use only the name
else
{
strcpy(name_buffer, bot_names[name_index].name);
bot_names[name_index].is_used = TRUE;
}
}
// for new config system
bool BotCreate( edict_t *pPlayer, const std::string &s_arg1, const std::string &s_arg2,
const std::string &s_arg3, const std::string &s_arg4, const std::string &s_arg5)
{
return BotCreate( pPlayer, s_arg1.c_str(), s_arg2.c_str(),
s_arg3.c_str(), s_arg4.c_str(), s_arg5.c_str());
}
/*
* puts bot in the game
* sets all values if corresponding arguments are valid otherwise are left untouched
* for further proccessing (methods in bot_start.cpp generates them)
* arg1 is team
* arg2 is class
* arg3 is skin
* arg4 is skill level
* arg5 is name
*/
bool BotCreate( edict_t *pPlayer, const char *arg1, const char *arg2,
const char *arg3, const char *arg4, const char *arg5)
{
char c_name[BOT_NAME_LEN+1];
bool found = FALSE;
// general initialization
c_name[0] = 0;
int skill = 1;
if (mod_id == FIREARMS_DLL)
{
skill = 0;
if ((arg4 != nullptr) && (*arg4 != 0))
skill = atoi(arg4);
if ((skill < 1) || (skill > 5))
{
// if there is a random skill request then generate the skill number
if (externals.GetRandomSkill())
skill = RANDOM_LONG(1, 5);
// otherwise use default skill
else
skill = externals.GetSpawnSkill();
}
if ((arg5 != nullptr) && (*arg5 != 0))
{
strncpy( c_name, arg5, BOT_NAME_LEN-1 );
c_name[BOT_NAME_LEN] = 0; // make sure c_name is null terminated
}
else
{
if (number_names > 0)
BotPickName( c_name );
else
strcpy(c_name, "[MB]marine");
}
}
int length = strlen(c_name);
// remove any illegal characters from name
for (int i = 0; i < length; i++)
{
if ((c_name[i] < ' ') || (c_name[i] > '~') ||
(c_name[i] == '"'))
{
for (int j = i; j < length; j++) // shuffle chars left (and null)
c_name[j] = c_name[j+1];
length--;
}
}
edict_t* BotEnt = CREATE_FAKE_CLIENT(c_name);
if (FNullEnt( BotEnt ))
{
if (pPlayer)
{
ClientPrint( pPlayer, HUD_PRINTNOTIFY, "Max. Players reached. Can't create bot!\n");
return FALSE;
}
}
else
{
char ptr[128]; // allocate space for message from ClientConnect
PrintOutput(pPlayer, "Creating MarineBot...\n", MType::msg_null);
int index = 0;
while ((bots[index].is_used) && (index < MAX_CLIENTS))
index++;
if (index == MAX_CLIENTS)
{
PrintOutput(pPlayer, "Can't create MarineBot server is full!\n", MType::msg_null);
return FALSE;
}
// create the player entity by calling MOD's player function
// (from LINK_ENTITY_TO_CLASS for player object)
// kick & rejoin bug - a fix by Pierre-Marie Baty
FREE_PRIVATE (BotEnt);
BotEnt->pvPrivateData = nullptr;
BotEnt->v.frags = 0;
// a fix by Pierre-Marie Baty end
player( VARS(BotEnt) );
char* infobuffer = GET_INFOBUFFER(BotEnt);
const int clientIndex = ENTINDEX(BotEnt);
SET_CLIENT_KEY_VALUE( clientIndex, infobuffer, "model", "gina" );
ClientConnect( BotEnt, c_name, "127.0.0.1", ptr );
BotEnt->v.flags |= FL_FAKECLIENT;
// Pieter van Dijk - use instead of DispatchSpawn() - Hip Hip Hurray!
ClientPutInServer( BotEnt );
// original position, but I've moved it above the client put in server because we need
// to know if it is bot or not right in that method
//BotEnt->v.flags |= FL_FAKECLIENT;
// initialize all the variables for this bot
bot_t* pBot = &bots[index];
pBot->is_used = TRUE;
pBot->respawn_state = RESPAWN_IDLE;
pBot->create_time = gpGlobals->time;
pBot->name[0] = 0; // name not set by server yet
pBot->clan_tag = 0; // no clan tag yet
pBot->pEdict = BotEnt;
pBot->not_started = 1; // hasn't joined game yet
if (mod_id == FIREARMS_DLL)
pBot->start_action = MSG_FA_IDLE;
else
pBot->start_action = 0; // not needed for non-team MODs
pBot->BotSpawnInit();
pBot->need_to_initialize = FALSE; // don't need to initialize yet
BotEnt->v.idealpitch = BotEnt->v.v_angle.x;
BotEnt->v.ideal_yaw = BotEnt->v.v_angle.y;
BotEnt->v.pitch_speed = 20;
BotEnt->v.yaw_speed = 20;
pBot->idle_angle = 0.0;
pBot->bot_skill = skill - 1; // 0 based for array indexes
pBot->aim_skill = skill - 1; // use the same value we used for bot_skill
pBot->bot_team = -1;
pBot->bot_class = -1;
pBot->bot_skin = -1;
pBot->main_weapon = NO_VAL;
pBot->backup_weapon = NO_VAL;
pBot->grenade_slot = NO_VAL;
pBot->claymore_slot = NO_VAL;
pBot->weapon_status = 0;
pBot->bot_behaviour = 0;
pBot->bot_fa_skill = 0;
pBot->round_end = FALSE;
pBot->pcust_class = nullptr;
for (int ii=0; ii<ROUTE_LENGTH; ++ii) { // NOTE: This is code by kota@ - I'm not going to use it, because it looks like he simply recreated the same thing that botman used for paths. Will remove it one day.
pBot->point_list[ii] = -1; // Because I don't like that system in FA. For deathmatch it's great, but FA is different
}
if (mod_id == FIREARMS_DLL)
{
if ((arg1 != nullptr) && (arg1[0] != 0))
{
pBot->bot_team = atoi(arg1);
if ((arg2 != nullptr) && (arg2[0] != 0))
{
pBot->bot_class = atoi(arg2);
if ((arg3 != nullptr) && (*arg3 != 0))
{
pBot->bot_skin = atoi(arg3);
}
}
}
}
}
return TRUE;
}
/*
* returns true is there's the entity in given range we've search for
*/
bool IsEntityInSphere(const char* entity_name, edict_t *pEdict, float radius)
{
edict_t *pent = nullptr;
while ((pent = UTIL_FindEntityInSphere(pent, pEdict->v.origin, radius )) != nullptr)
{
char item_name[64];
strcpy(item_name, STRING(pent->v.classname));
if (strcmp(entity_name, item_name) == 0)
return TRUE;
}
return FALSE;
}
/*
* returns true if the target object is fully visible
*/
bool BotEntityIsVisible( bot_t *pBot, const Vector dest )
{
TraceResult tr;
// trace a line from bot's eyes to destination...
UTIL_TraceLine( pBot->pEdict->v.origin + pBot->pEdict->v.view_ofs,
dest, ignore_monsters, pBot->pEdict, &tr );
// check if line of sight to object is not blocked (i.e. visible)
if (tr.flFraction >= 1.0f)
return TRUE;
else
return FALSE;
}
/*
* scans the surrounding for certain items/objects
*/
void BotFindItem( bot_t *pBot )
{
edict_t *pent = nullptr;
char item_name[40];
Vector vecStart, vecEnd;
int angle_to_entity;
edict_t *pEdict = pBot->pEdict;
while ((pent = UTIL_FindEntityInSphere( pent, pEdict->v.origin, FIND_ITEM_RADIUS )) != nullptr)
{
strcpy(item_name, STRING(pent->v.classname));
if (strncmp("item_", item_name, 5) == 0)
{
// look for placed claymores
if (strcmp("item_claymore", item_name) == 0)
{
// bot already knows about this claymore
// must be first otherwise we will "spot" the same mine more then once
if (pBot->v_ground_item == pent->v.origin)
continue;
// we shouldn't avoid the mine we've just set - its not active yet
if (pBot->f_use_clay_time > gpGlobals->time)
continue;
vecStart = pEdict->v.origin + pEdict->v.view_ofs;
vecEnd = pent->v.origin;
angle_to_entity = InFieldOfView(pEdict, vecEnd - vecStart);
if (angle_to_entity > 45)
continue;
// can the bot see this object
if (BotEntityIsVisible(pBot, vecEnd))
{
pBot->v_ground_item = vecEnd;
// the best bots don't fail to spot it much, but worse bots
// have higher chance not to see this mine
const int chance_to_miss = 95 - (pBot->bot_skill * 5);
// like if the bot didn't see it
if (RANDOM_LONG(1, 100) > chance_to_miss)
{
pBot->SetTask(TASK_CLAY_IGNORE);
if (botdebugger.IsDebugActions())
HudNotify("***Ignoring this claymore -> like if didn't see it\n", pBot);
}
// bot spotted this claymore
else
{
// let others know about it
pBot->BotSpeak(SAY_CLAYMORE_FOUND);
// set the evade flag
pBot->SetTask(TASK_CLAY_EVADE);
if (botdebugger.IsDebugActions())
{
HudNotify("***Found this claymore -> evading\n", pBot);
#ifdef _DEBUG
//@@@@@@@@@@@@@
//ALERT(at_console, "***Found this claymore -> evading gTime=%0.3f\n", gpGlobals->time);
#endif
}
// we don't need to look for other objects
break;
}
}
}
// look for grenades thrown at bot
else if ((strcmp("item_frag", item_name) == 0) ||
(strcmp("item_concussion", item_name) == 0) ||
(strcmp("item_stg24", item_name) == 0))
{
// this grenade is not thrown (ie. still in players inventory)
if ((pent->v.solid == SOLID_NOT) && (pent->v.movetype == MOVETYPE_FOLLOW))
continue;
const int grenade_team = UTIL_GetTeam(pent);
const int bot_team = UTIL_GetTeam(pEdict);
// ignore grenades thrown by your teammate or unknown grenades
// even if it might save lifes in certain cases (ie. TKs)
if ((grenade_team == bot_team) || (grenade_team == TEAM_NONE))
continue;
vecStart = pEdict->v.origin + pEdict->v.view_ofs;
vecEnd = pent->v.origin;
angle_to_entity = InFieldOfView(pEdict, vecEnd - vecStart);
if (angle_to_entity > 45)
continue;
if (BotEntityIsVisible(pBot, vecEnd))
{
pBot->BotSpeak(SAY_GRENADE_IN);
break;
}
}
}
// handle thrown grenades in older versions or grenades fired from GLs
else if (UTIL_IsOldVersion() || (strcmp("weapon_m79", item_name) == 0) ||
(strcmp("weapon_m203", item_name) == 0) ||
(strcmp("weapon_gp25", item_name) == 0))
{
if ((strcmp("weapon_frag", item_name) == 0) ||
(strcmp("weapon_flashbang", item_name) == 0) ||
(strcmp("weapon_stg24", item_name) == 0) ||
(strcmp("grenade", item_name) == 0) ||
(strcmp("weapon_m203", item_name) == 0) ||
(strcmp("weapon_m79", item_name) == 0) ||
(strcmp("weapon_gp25", item_name) == 0))
{
if ((pent->v.solid == SOLID_NOT) && (pent->v.movetype == MOVETYPE_FOLLOW))
continue;
const int grenade_team = UTIL_GetTeam(pent);
const int bot_team = UTIL_GetTeam(pEdict);
if ((grenade_team == bot_team) || (grenade_team == TEAM_NONE))
continue;
vecStart = pEdict->v.origin + pEdict->v.view_ofs;
vecEnd = pent->v.origin;
angle_to_entity = InFieldOfView(pEdict, vecEnd - vecStart);
if (angle_to_entity > 45)
continue;
if (BotEntityIsVisible(pBot, vecEnd))
{
pBot->BotSpeak(SAY_GRENADE_IN);
break;
}
}
}
} // end the while
}
/*
* picks the best message for current situation passed by message type argument
*/
void bot_t::BotSpeak(int msg_type, const char *target)
{
// use say command only if allowed to do so
if (externals.GetDontChat())
return;
// we don't want to spam the game
if ((text_msg_time + 1.0f > gpGlobals->time) || (speak_time + 1.0f > gpGlobals->time))
return;
// if the bot said exactly this message in last 3 seconds then don't say it again
if ((prev_msg == msg_type) && (text_msg_time + 3.0f > gpGlobals->time))
return;
// store the time the bot said something
text_msg_time = gpGlobals->time;
const int choice = RANDOM_LONG(1, 100);
char msg[128];
char name[BOT_NAME_LEN];
name[0] = '\0';
switch (msg_type)
{
case SAY_GRENADE_IN:
if (choice < 50)
UTIL_TeamSay(pEdict, "GRENADE!");
else
UTIL_TeamSay(pEdict, "GRENADE! Take cover");
break;
case SAY_GRENADE_OUT:
if (choice < 50)
UTIL_TeamSay(pEdict, "Fire in the hole!");
else
UTIL_TeamSay(pEdict, "Frag out!");
break;
case SAY_CLAYMORE_FOUND:
if (choice < 50)
UTIL_TeamSay(pEdict, "Claymore spotted! Watch your steps");
else
UTIL_TeamSay(pEdict, "There's a mine at my position. Watch out!");
// to prevent spamming the game
text_msg_time = gpGlobals->time + 1.0f;
break;
case SAY_MEDIC_HELP_YOU:
UTIL_HumanizeTheName(target, name);
sprintf(msg, "Hey, %s, stay still and I'll treat you", name);
UTIL_TeamSay(pEdict, msg);
text_msg_time = gpGlobals->time + 1.0f;
break;
case SAY_MEDIC_CANT_HELP:
UTIL_HumanizeTheName(target, name);
if (choice < 50)
sprintf(msg, "Sorry, %s, I can't help you", name);
else
sprintf(msg, "I can't help you %s", name);
UTIL_TeamSay(pEdict, msg);
text_msg_time = gpGlobals->time + 1.0f;
break;
case SAY_CEASE_FIRE:
UTIL_HumanizeTheName(target, name);
if (choice < 50)
sprintf(msg, "Cease fire, %s", name);
else
sprintf(msg, "Hey, %s, cease fire!", name);
UTIL_TeamSay(pEdict, msg);
text_msg_time = gpGlobals->time + 1.0f;
break;
default:
break;
}
// remember the message that has to be said, to prevent spamming the game
prev_msg = msg_type;
}
///////////////////////////////////////////////////////////////////////////////
//
// NOTE: Marine Bot and MedicGuild are partners so if you work on your own bot
// and your bot and MedicGiuld aren't partners remove the part of the code
// marked by >MedicGuild< and all connection to it!
//
///////////////////////////////////////////////////////////////////////////////
// >MedicGuild START<
/*
* checks for medic skills and if bot uses one sets a MedicGuild tag behind bot's name
* or it removes the tag if bot has no medic skill
*/
void bot_t::CheckMedicGuildTag()
{
// set MedicGuild name tag if bot uses one of medic skills
if ((bot_fa_skill & (FAID | MEDIC)) && !(clan_tag & NAMETAG_MEDGUILD))
{
char change_name[32];
clan_tag |= NAMETAG_MEDGUILD;
// if the MedicGuild sign isn't set yet change bot's name
if (strstr(name, "}+{") == nullptr)
{
strcpy(change_name, name);
strcat(change_name, "}+{");
player(VARS(pEdict));
char* infobuffer = GET_INFOBUFFER(pEdict);
const int clientIndex = ENTINDEX(pEdict);
SET_CLIENT_KEY_VALUE(clientIndex, infobuffer, "name", change_name);
// update bot's name with this new name
strcpy(name, change_name);
}
}
// remove the tag if bot isn't medic
if (!(clan_tag & NAMETAG_DONE) && !(bot_fa_skill & (FAID | MEDIC)))
{
char change_name[32];