-
Notifications
You must be signed in to change notification settings - Fork 9
/
bot.cpp
4402 lines (3768 loc) · 146 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
// 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.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 "tf_defs.h"
#include <meta_api.h>
#include "list.h"
#include "bot.h"
#include "waypoint.h"
#include "bot_func.h"
#include "bot_job_think.h"
#include "bot_navigate.h"
#include "bot_weapons.h"
#include <sys/stat.h>
#ifdef WIN32
#include <wtypes.h>
#endif
//#include "list.h"
extern List<char*> commanders;
#ifndef __linux__
extern HINSTANCE h_Library;
#else
extern void* h_Library;
#endif
// my global var for checking if we're using metamod or not?
// i.e. should we be a plugin..
bool mr_meta = false;
extern int mod_id;
extern WAYPOINT waypoints[MAX_WAYPOINTS];
extern int num_waypoints; // number of waypoints currently in use
extern edict_t* pent_info_ctfdetect;
static float roleCheckTimer = 30.0f; // set fairly high so players can join first
struct TeamLayout {
List<bot_t*> attackers[4];
List<bot_t*> defenders[4];
List<edict_t*> humanAttackers[4];
List<edict_t*> humanDefenders[4];
int total[4];
};
// 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_weapon_t weapon_defs[MAX_WEAPONS];
// extern int flf_bug_fix;
extern int CheckTeleporterExitTime;
static std::FILE* fp;
int pipeCheckFrame = 15;
extern int debug_engine;
extern bool spawn_check_crash;
extern int spawn_check_crash_count;
extern edict_t* spawn_check_crash_edict;
// bot settings //////////////////
extern int bot_allow_moods;
extern int botskill_lower;
extern int botskill_upper;
extern bool defensive_chatter;
extern bool offensive_chatter;
extern bool observer_mode;
extern bool botdontshoot;
extern bool botdontmove;
extern int bot_chat;
extern int min_bots;
extern int max_bots;
extern int bot_team_balance;
extern bool bot_can_use_teleporter;
extern bool bot_can_build_teleporter;
extern bool bot_xmas;
extern bool g_bot_debug;
extern int spectate_debug; // spectators can trigger debug messages from bots
extern edict_t* clients[32];
// area defs /////////////////
extern AREA areas[MAX_WAYPOINTS];
extern int num_areas;
bool attack[4]; // teams attack
bool defend[4]; // teams defend
// working arrays... these are what are referenced by the bots
// i.e. hold the current available state of points 1..8 for each team
bool blue_av[8];
bool red_av[8];
bool green_av[8];
bool yellow_av[8];
// pre defined msg type.. 64 empty msg command list
msg_com_struct msg_com[MSG_MAX];
// and 64 empty msg's, to be filled with messages to intercept
char msg_msg[64][MSG_MAX];
#define PLAYER_SEARCH_RADIUS 50.0f
//#define FLF_PLAYER_SEARCH_RADIUS 60.0f
#define GETPLAYERAUTHID (*g_engfuncs.pfnGetPlayerAuthId)
bot_t bots[MAX_BOTS]; // max of 32 bots in a game
// this can allow us to track which bots are joining the game for the first time
// and which are joining because they were kicked off due to a map change
bool botJustJoined[MAX_BOTS] = { true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true };
extern chatClass chat; // bot chat stuff
static int number_names = 0;
constexpr unsigned char MAX_BOT_NAMES = 128;
/*enum {
VALVE_MAX_SKINS = 10
};*/
//#define GEARBOX_MAX_SKINS 20
static char bot_names[MAX_BOT_NAMES][BOT_NAME_LEN + 1];
// defend response distance per class
static constexpr float defendMaxRespondDist[] = { 300.0f, 1500.0f, 500.0f, 1500.0f, 800.0f, 1500.0f, 1500.0f, 1500.0f, 1500.0f, 1500.0f };
// const static double double_pi = 3.141592653589793238;
// FUNCTION PROTOTYPES /////////////////
static int BotAssignDefaultSkill();
static void BotFlagSpottedCheck(bot_t* pBot);
static void BotAmmoCheck(bot_t* pBot);
static void BotCombatThink(bot_t* pBot);
static void BotAttackerCheck(bot_t* pBot);
static void BotGrenadeAvoidance(bot_t* pBot);
static void BotRoleCheck(bot_t* pBot);
static bool botVerifyAccess(edict_t* pPlayer);
static void BotComms(bot_t* pBot);
static bool BotChangeRole(bot_t* pBot, const char* cmdLine, const char* from);
static bool BotChangeClass(bot_t* pBot, int iClass, const char* from);
static void BotPickNewClass(bot_t* pBot);
static bool BotChooseCounterClass(bot_t* pBot);
static bool BotDemomanNeededCheck(bot_t* pBot);
static int guessThreatLevel(const bot_t* pBot);
static void BotReportMyFlagDrop(bot_t* pBot);
static void BotEnemyCarrierAlert(bot_t* pBot);
static void BotSenseEnvironment(bot_t* pBot);
static void BotFight(bot_t* pBot);
static void BotSpectatorDebug(bot_t* pBot);
inline edict_t* CREATE_FAKE_CLIENT(const char* netname) {
if (debug_engine) {
fp = UTIL_OpenFoxbotLog();
if (fp != nullptr) {
std::fprintf(fp, "createfakeclient: %s\n", netname);
std::fclose(fp);
}
}
return (*g_engfuncs.pfnCreateFakeClient)(netname);
}
inline char* GET_INFOBUFFER(edict_t* e) {
if (debug_engine) {
fp = UTIL_OpenFoxbotLog();
if (fp != nullptr) {
std::fprintf(fp, "getinfobuffer: %p\n", static_cast<void*>(e));
std::fclose(fp);
}
}
return (*g_engfuncs.pfnGetInfoKeyBuffer)(e);
}
/* this function is unused so far
inline char *GET_INFO_KEY_VALUE( char *infobuffer, char *key )
{
if(debug_engine)
{
fp = UTIL_OpenFoxbotLog();
if(fp != NULL)
{
std::fprintf(fp, "getinfokeyvalue: %s %s\n",infobuffer,key);
std::fclose(fp);
}
}
return (*g_engfuncs.pfnInfoKeyValue)( infobuffer, key );
}*/
inline void SET_CLIENT_KEY_VALUE(const int clientIndex, char* infobuffer, char* key, char* value) {
if (debug_engine) {
fp = UTIL_OpenFoxbotLog();
if (fp != nullptr) {
std::fprintf(fp, "pfnSetClientKeyValue: %s %s\n", key, value);
std::fclose(fp);
}
}
(*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 = reinterpret_cast<LINK_ENTITY_FUNC>(GetProcAddress(h_Library, "player"));
}
if (otherClassName != nullptr) {
(*otherClassName)(pev);
}
}
// This function initializes a bots data to safe values.
// i.e. it clears the bots "mind" before it spawns or respawns
void BotSpawnInit(bot_t* pBot) {
// v1.1.0.8 on xp..(or possibly fast machines)
// had the bot crash if it tried to respawn too soon!
// so put a pause in to stop it pressing 'fire' too soon...
// this should fix it!!
pBot->f_killed_time = gpGlobals->time;
// but it appears to be a bug in valves code, even without bots..
// so not actually needed
pBot->curr_wp_diff = -1;
pBot->f_progressToWaypoint = 4000.0f;
pBot->f_navProblemStartTime = 0.0f;
pBot->routeFailureTally = 0;
pBot->f_current_wp_deadline = 0.0f;
pBot->current_wp = -1;
// Fix by Cheeseh (RCBot)
// float fUpdateTime = gpGlobals->time;
// float fLastRunPlayerMoveTime = gpGlobals->time - 0.1f;
// pBot->msecnum = 0;
// pBot->msecdel = 0.0;
// pBot->msecval = 0.0;
pBot->bot_real_health = 0;
pBot->bot_armor = 0;
pBot->bot_weapons = 0;
pBot->f_blinded_time = 0.0f;
pBot->f_max_speed = CVAR_GET_FLOAT("sv_maxspeed");
pBot->prev_speed = 0.0f; // fake "paused" since bot is NOT stuck
pBot->job[pBot->currentJob].phase = 0;
pBot->f_find_item_time = 0.0f;
pBot->strafe_mod = STRAFE_MOD_NORMAL;
// enemy stuff
pBot->enemy.ptr = nullptr;
pBot->enemy.seenWithFlag = 0;
pBot->enemy.f_firstSeen = gpGlobals->time - 1000.0f;
pBot->enemy.f_lastSeen = pBot->enemy.f_firstSeen;
pBot->enemy.f_seenDistance = 400.0f;
pBot->enemy.lastLocation = Vector(0.0f, 0.0f, 0.0f);
pBot->visEnemyCount = 0;
pBot->visAllyCount = 1; // 1 = the bot itself
pBot->lastAllyVector = Vector(0.0f, 0.0f, 0.0f);
pBot->f_lastAllySeenTime = 0.0f;
pBot->f_alliedMedicSeenTime = 0.0f;
pBot->f_view_change_time = 0.0f;
pBot->f_shoot_time = gpGlobals->time;
pBot->f_primary_charging = -1.0f;
pBot->f_secondary_charging = -1.0f;
pBot->charging_weapon_id = 0;
pBot->tossNade = 0;
pBot->aimDrift = Vector(0.0f, 0.0f, 0.0f);
pBot->f_pause_time = 0.0f;
pBot->f_duck_time = 0.0f;
pBot->bot_has_flag = false;
pBot->scoreAtSpawn = static_cast<int>(pBot->pEdict->v.frags);
pBot->b_use_health_station = false;
pBot->f_use_health_time = 0.0f;
pBot->b_use_HEV_station = false;
pBot->f_use_HEV_time = 0.0f;
pBot->f_use_button_time = 0.0f;
pBot->f_waypoint_drift = 0.0f;
pBot->goto_wp = -1;
pBot->lastgoto_wp = -1;
pBot->f_dontEvadeTime = 0.0f;
pBot->f_enemy_check_time = gpGlobals->time;
pBot->flag_impulse = -1;
pBot->f_soundReactTime = gpGlobals->time + 3.0f;
pBot->disturbedViewAmount = 0;
pBot->f_disturbedViewTime = gpGlobals->time;
pBot->f_disturbedViewYaw = 0.0f;
pBot->f_disturbedViewPitch = 0.0f;
pBot->f_periodicAlertFifth = gpGlobals->time + 0.2f;
pBot->f_periodicAlert1 = gpGlobals->time + 1.0f;
pBot->f_periodicAlert3 = gpGlobals->time + 3.0f;
pBot->f_snipe_time = 0.0f;
pBot->f_disguise_time = gpGlobals->time;
pBot->f_feigningTime = 0.0f;
pBot->disguise_state = DISGUISE_NONE;
pBot->f_spyFeignAmbushTime = gpGlobals->time + static_cast<float>(random_long(20, 30));
pBot->f_side_route_time = gpGlobals->time + RANDOM_FLOAT(0.0f, 60.0f);
// the bots true ammo status can be checked upon later
pBot->ammoStatus = AMMO_WANTED;
/////////////////
// DrEvil vars //
/////////////////
pBot->last_spawn_time = gpGlobals->time;
pBot->nadeType = 0;
pBot->nadePrimed = false;
pBot->primeTime = pBot->lastDistance = 0.0f;
pBot->lastDistanceCheck = pBot->FreezeDelay = 0.0f;
pBot->f_shortcutCheckTime = gpGlobals->time + 2.0f;
pBot->f_roleSayDelay = gpGlobals->time + 10.0f;
pBot->f_discard_time = gpGlobals->time + RANDOM_FLOAT(15.0f, 20.0f);
pBot->f_grenadeScanTime = gpGlobals->time + 0.7f;
pBot->branch_waypoint = -1;
/////////////////////
// end DrEvil vars //
/////////////////////
std::memset(&pBot->current_weapon, 0, sizeof pBot->current_weapon);
std::memset(&pBot->m_rgAmmo, 0, sizeof pBot->m_rgAmmo);
}
//#ifdef WIN32
// FILE* fopen_s(char str[256], const char* text);
//#endif
void BotNameInit() {
char bot_name_filename[256];
UTIL_BuildFileName(bot_name_filename, 255, "foxbot_names.txt", nullptr);
std::FILE* bot_name_fp = std::fopen(bot_name_filename, "r");
if (bot_name_fp != nullptr) {
char name_buffer[80];
while (number_names < MAX_BOT_NAMES && fgets(name_buffer, 80, bot_name_fp) != nullptr) {
unsigned int length = std::strlen(name_buffer);
// remove '\n'
if (length > 0 && name_buffer[length - 1] == '\n') {
name_buffer[length - 1] = '\0';
length--;
}
unsigned int str_index = 0;
while (str_index < length) {
if (name_buffer[str_index] < ' ' || name_buffer[str_index] > '~' || name_buffer[str_index] == '"') {
for (unsigned int index = str_index; index < length; index++)
name_buffer[index] = name_buffer[index + 1];
}
str_index++;
}
if (name_buffer[0] != 0) {
std::strncpy(bot_names[number_names], name_buffer, BOT_NAME_LEN);
bot_names[number_names][BOT_NAME_LEN - 1] = '\0'; // ensure null termination [APG]RoboCop[CL]
number_names++;
}
}
std::fclose(bot_name_fp);
}
}
// This simple function will return a valid, randomly selected bot skill
// in the range from botskill_lower to botskill_upper.
static int BotAssignDefaultSkill() {
if (botskill_lower <= botskill_upper)
return botskill_lower;
return static_cast<int>(random_long(botskill_upper, botskill_lower));
}
void BotPickName(char* name_buffer) {
int index;
// see if a name exists from a kicked bot (if so, reuse it)
for (index = 0; index < MAX_BOTS; index++) {
if (bots[index].is_used == false && bots[index].name[0]) {
std::strcpy(name_buffer, bots[index].name);
// erase the name of the bot whose name we've taken
// this fixes the bug where two bots would be created with the same name
// (sometimes a lower indexed bot would copy a higher indexed bots name)
bots[index].name[0] = 0;
return;
}
}
// it's time to pick a new name at random
int name_index = random_long(1, number_names) - 1; // zero based
bool used = true;
int attempts = 0;
while (used) {
used = false;
// is there a player with this name?
for (index = 1; index <= gpGlobals->maxClients; index++) {
const edict_t* pPlayer = INDEXENT(index);
if (pPlayer && !FNullEnt(pPlayer) && !pPlayer->free && std::strcmp(bot_names[name_index], STRING(pPlayer->v.netname)) == 0) {
used = true;
break;
}
}
if (used) {
++name_index;
if (name_index >= number_names) {
name_index = 0;
++attempts;
}
if (attempts >= 2) {
used = false; // break out of loop even if already used
// Log this event as it normally shouldn't happen
fp = UTIL_OpenFoxbotLog();
if (fp != nullptr) {
std::fprintf(fp, "Ran out of unique bot names to assign.\n");
std::fclose(fp);
}
}
}
}
std::strcpy(name_buffer, bot_names[name_index]);
}
void BotCreate(edict_t* pPlayer, const char* arg1, const char* arg2, const char* arg3, const char* arg4) {
// indicate which models are currently used for random model allocation
/*static bool valve_skin_used[VALVE_MAX_SKINS] = { false, false, false, false, false, false, false, false, false, false };
static bool gearbox_skin_used[GEARBOX_MAX_SKINS] = {
false, false, false, false, false, false, false, false, false, false,
false, false, false, false, false, false, false, false, false, false
};
// store the names of the models...
static const char* valve_bot_skins[VALVE_MAX_SKINS] = { "barney", "gina", "gman", "gordon", "helmet", "hgrunt", "recon", "robo", "scientist", "zombie" };
static const char *gearbox_bot_skins[GEARBOX_MAX_SKINS] = {
"barney", "beret", "cl_suit", "drill", "fassn", "gina", "gman",
"gordon", "grunt", "helmet", "hgrunt", "massn", "otis", "recon",
"recruit", "robo", "scientist", "shepard", "tower", "zombie"
};
// store the player names for each of the models...
static const char* valve_bot_names[VALVE_MAX_SKINS] = { "Barney", "Gina", "G-Man", "Gordon", "Helmet", "H-Grunt", "Recon", "Robo", "Scientist", "Zombie" };
static const char *gearbox_bot_names[GEARBOX_MAX_SKINS] = {
"Barney", "Beret", "Cl_suit", "Drill", "Fassn", "Gina", "G-Man",
"Gordon", "Grunt", "Helmet", "H-Grunt", "Massn", "Otis", "Recon",
"Recruit", "Robo", "Scientist", "Shepard", "Tower", "Zombie"
};*/
char c_skin[BOT_SKIN_LEN + 1];
char c_name[BOT_NAME_LEN + 1];
c_skin[0] = '\0';
c_name[0] = '\0';
int skill;
int i;
// min/max checking...
if (max_bots > MAX_BOTS)
max_bots = MAX_BOTS;
if (max_bots < -1)
max_bots = -1;
if (min_bots > max_bots)
min_bots = max_bots;
if (min_bots < -1)
min_bots = -1;
// count the number of players present
int count = 0;
for (i = 1; i <= MAX_BOTS; i++) ///<
{
char cl_name[128];
cl_name[0] = '\0';
const char* infobuffer = (*g_engfuncs.pfnGetInfoKeyBuffer)(INDEXENT(i));
std::strcpy(cl_name, g_engfuncs.pfnInfoKeyValue(infobuffer, "name"));
if (cl_name[0] != '\0')
count++;
}
// count the number of bots present
int bot_count = 0;
for (i = 0; i < MAX_BOTS; i++) {
if (bots[i].is_used)
bot_count++;
}
if (bot_count >= max_bots && bot_count >= min_bots && max_bots != -1) {
// don't create another bot..
if (pPlayer)
ClientPrint(pPlayer, HUD_PRINTNOTIFY, "Max bots reached... Can't create bot!\n");
if (IS_DEDICATED_SERVER())
std::printf("Max bots reached... Can't create bot!\n");
return;
}
if (mod_id == VALVE_DLL) {
//bool found = false;
//int max_skin_index = 0;
//if (mod_id == VALVE_DLL)
// max_skin_index = VALVE_MAX_SKINS;
// else // must be GEARBOX_DLL
// max_skin_index = GEARBOX_MAX_SKINS;
/*if (arg1 == nullptr || *arg1 == 0) {
bool* pSkinUsed = nullptr;
// pick a random skin
if (mod_id == VALVE_DLL) {
index = random_long(0, VALVE_MAX_SKINS - 1);
pSkinUsed = &valve_skin_used[0];
}
else // must be GEARBOX_DLL
{
index = random_long(0, GEARBOX_MAX_SKINS - 1);
pSkinUsed = &gearbox_skin_used[0];
}
if (pSkinUsed) {
// check if this skin has already been used...
while (pSkinUsed[index] == true) {
index++;
if (index == max_skin_index)
index = 0;
}
pSkinUsed[index] = true;
// check if all skins are now used...
for (i = 0; i < max_skin_index; i++) {
if (pSkinUsed[i] == false)
break;
}
// if all skins are used, reset used to false for next selection
if (i == max_skin_index) {
for (i = 0; i < max_skin_index; i++)
pSkinUsed[i] = false;
}
}
if (mod_id == VALVE_DLL)
std::strcpy(c_skin, valve_bot_skins[index]);
// else // must be GEARBOX_DLL
// std::strcpy( c_skin, gearbox_bot_skins[index] );
}
else {
std::strncpy(c_skin, arg1, BOT_SKIN_LEN - 1);
c_skin[BOT_SKIN_LEN] = 0; // make sure c_skin is null terminated
}*/
//for (i = 0; c_skin[i] != 0; i++)
// c_skin[i] = std::tolower(c_skin[i]); // convert to all lowercase
/*index = 0;
while (!found && index < max_skin_index) {
if (mod_id == VALVE_DLL) {
if (std::strcmp(c_skin, valve_bot_skins[index]) == 0)
found = true;
else
index++;
}
else // must be GEARBOX_DLL
{
if(std::strcmp(c_skin, gearbox_bot_skins[index]) == 0)
found = true;
else index++;
}
}
if (found == true) {
if (arg2 != nullptr && *arg2 != 0) {
std::strncpy(c_name, arg2, BOT_SKIN_LEN - 1);
c_name[BOT_SKIN_LEN] = 0; // make sure c_name is null terminated
}
else {
if (number_names > 0)
BotPickName(c_name);
else if (mod_id == VALVE_DLL)
std::strcpy(c_name, valve_bot_names[index]);
// else // must be GEARBOX_DLL
// std::strcpy( c_name, gearbox_bot_names[index] );
}
}
else {*/
//char dir_name[128];
//char filename[128];
//struct stat stat_str;
//GET_GAME_DIR(dir_name);
/*#ifndef __linux__
snprintf(filename, 127, "%s\\models\\player\\%s", dir_name, c_skin);
#else
snprintf(filename, 127, "%s/models/player/%s", dir_name, c_skin);
#endif
if (stat(filename, &stat_str) != 0) {
#ifndef __linux__
snprintf(filename, 127, "valve\\models\\player\\%s", c_skin);
#else
snprintf(filename, 127, "valve/models/player/%s", c_skin);
#endif
if (stat(filename, &stat_str) != 0) {
char err_msg[80];
snprintf(err_msg, 79, "model \"%s\" is unknown.\n", c_skin);
if (pPlayer)
ClientPrint(pPlayer, HUD_PRINTNOTIFY, err_msg);
if (IS_DEDICATED_SERVER())
std::printf("%s", err_msg);
if (pPlayer)
ClientPrint(pPlayer, HUD_PRINTNOTIFY, "use barney, gina, gman, gordon, helmet, hgrunt,\n");
if (IS_DEDICATED_SERVER())
std::printf("use barney, gina, gman, gordon, helmet, hgrunt,\n");
if (pPlayer)
ClientPrint(pPlayer, HUD_PRINTNOTIFY, " recon, robo, scientist, or zombie\n");
if (IS_DEDICATED_SERVER())
std::printf(" recon, robo, scientist, or zombie\n");
return;
}
}*/
if (arg2 != nullptr && *arg2 != 0) {
std::strncpy(c_name, arg2, 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 {
// copy the name of the model to the bot's name...
std::strncpy(c_name, arg1, BOT_NAME_LEN - 1);
c_name[BOT_NAME_LEN] = '\0'; // make sure c_skin is null terminated
}
}
//}
skill = 0;
if (arg3 != nullptr && *arg3 != 0)
skill = std::atoi(arg3);
if (skill < 1 || skill > 5)
skill = BotAssignDefaultSkill();
}
else {
if (arg3 != nullptr && *arg3 != 0) {
std::strncpy(c_name, arg3, 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
std::strcpy(c_name, "FoxBot");
}
skill = 0;
if (arg4 != nullptr && *arg4 != 0)
skill = std::atoi(arg4);
if (skill < 1 || skill > 5)
skill = BotAssignDefaultSkill();
}
int length = static_cast<int>(std::strlen(c_name));
// remove any illegal characters from the name
for (i = 0; i < length; i++) {
if (c_name[i] < ' ' || c_name[i] > '~') {
// shuffle the remaining chars left (and null)
for (int j = i; j < length; j++)
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");
if (IS_DEDICATED_SERVER())
std::printf("Max. Players reached. Can't create bot!\n");
return;
}
char ptr[256]; // allocate space for message from ClientConnect
int index = 0;
while (index < MAX_BOTS && bots[index].is_used)
++index;
if (index >= MAX_BOTS) {
ClientPrint(pPlayer, HUD_PRINTNOTIFY, "Can't create bot(maximum bots reached).\n");
return;
}
if (IS_DEDICATED_SERVER())
std::printf("Creating bot...\n");
else if (pPlayer)
ClientPrint(pPlayer, HUD_PRINTNOTIFY, "Creating bot...\n");
// clear shit up for clientprintf and clientcommand checking
// (for Admin Mod)only helps if we've just created a bot
// probably never used
i = 0;
while (i < MAX_BOTS && clients[i] != BotEnt)
i++;
if (i < MAX_BOTS)
{
clients[i] = nullptr;
}
// Why put these here?
// Well, admin mod plugin may sneak in as they're connecting
// and before we can add this for checking purposes
bot_t* pBot = &bots[index];
if (botJustJoined[index])
//bzero(pBot, sizeof(bot_t)); // wipe out the bots data
{
*pBot = bot_t{}; // Create a new instance and assign it to pBot
}
pBot->pEdict = BotEnt;
// clear the player info from the previous player who used this edict
// (a fix by Pierre-Marie, found in the HPB forum)
if (BotEnt->pvPrivateData != nullptr) {
FREE_PRIVATE(pBot->pEdict);
pBot->pEdict->pvPrivateData = nullptr;
}
pBot->pEdict->v.frags = 0;
// create the player entity by calling MOD's player function
if (mr_meta)
CALL_GAME_ENTITY(PLID, "player", &BotEnt->v);
else
player(VARS(BotEnt));
//{fp=UTIL_OpenFoxbotLog(); std::fprintf(fp, "-bot connect %x\n",BotEnt); std::fclose(fp); }
MDLL_ClientConnect(BotEnt, c_name, "127.0.0.1", ptr);
spawn_check_crash = true;
spawn_check_crash_count = 0;
spawn_check_crash_edict = BotEnt;
MDLL_ClientPutInServer(BotEnt);
spawn_check_crash = false;
spawn_check_crash_edict = nullptr;
/*i = 0;
while((i < 32) && (clients[i] != NULL))
i++;
if(i < 32)
clients[i] = BotEnt;*/
//{ fp=UTIL_OpenFoxbotLog(); std::fprintf(fp,"e\n"); std::fclose(fp); }
BotEnt->v.flags |= FL_FAKECLIENT;
pBot->is_used = true;
pBot->respawn_state = RESPAWN_IDLE;
pBot->create_time = gpGlobals->time + 1.0f;
//{ fp=UTIL_OpenFoxbotLog(); std::fprintf(fp,"a%f %f\n",pBot->create_time,gpGlobals->time); std::fclose(fp); }
pBot->name[0] = 0; // name not set by server yet
pBot->f_think_time = gpGlobals->time;
std::strcpy(pBot->skin, c_skin);
BotResetJobBuffer(pBot);
pBot->has_sentry = false;
pBot->sentry_edict = nullptr;
pBot->sentryWaypoint = -1;
pBot->SGRotated = false;
pBot->sentry_ammo = 0;
pBot->has_dispenser = false;
pBot->dispenser_edict = nullptr;
pBot->f_dispenserDetTime = 0.0f;
pBot->killer_edict = nullptr;
pBot->killed_edict = nullptr;
pBot->lastEnemySentryGun = nullptr;
pBot->mission = ROLE_ATTACKER;
pBot->lockMission = false;
pBot->tpEntrance = nullptr;
pBot->tpExit = nullptr;
pBot->tpEntranceWP = -1;
pBot->tpExitWP = -1;
pBot->f_safeWeaponTime = gpGlobals->time;
// clear the bots knowledge of any teleporters
for (int teleIndex = 0; teleIndex < MAX_BOT_TELEPORTER_MEMORY; teleIndex++) {
BotForgetTeleportPair(pBot, teleIndex);
}
roleCheckTimer += 4.0f;
// stop the bot from autonomously changing class if
// a class was specified
if (arg2 != nullptr && mod_id == TFC_DLL)
pBot->lockClass = true;
else
pBot->lockClass = false;
pBot->f_suspectSpyTime = 0.0f;
pBot->suspectedSpy = nullptr;
pBot->lastFrameHealth = 70;
pBot->f_injured_time = 0.0f;
pBot->deathsTillClassChange = 4; //(int)random_long(4, 15);
// time to set up the bots personality stuff
if (bot_allow_moods) {
pBot->trait.aggression = static_cast<short>(random_long(1, 1000));
pBot->trait.fairplay = static_cast<short>(random_long(1, 1000));
pBot->trait.faveClass = random_long(1, 9);
pBot->trait.health = static_cast<short>(random_long(0, 3) * 20 + 20);
pBot->trait.humour = static_cast<short>(random_long(0, 100));
if (random_long(1, 30) > 10)
pBot->trait.camper = true;
else
pBot->trait.camper = false;
}
else // make each bot have the same personality traits
{
pBot->trait.aggression = 400;
pBot->trait.fairplay = 600;
pBot->trait.faveClass = 3;
pBot->trait.health = 50;
pBot->trait.humour = 50;
pBot->trait.camper = true;
}
pBot->f_humour_time = pBot->f_think_time + random_float(60.0f, 180.0f);
pBot->sideRouteTolerance = 400; // not willing to branch far initially
pBot->not_started = true; // hasn't joined game yet
pBot->bot_start2 = 1;
pBot->bot_start3 = 0;
pBot->bot_class = -1; // not decided yet
pBot->bot_team = -1; // not decided yet
pBot->current_team = 0; // sane value, will be set properly once bot has spawned
if (botJustJoined[index]) {
pBot->greeting = false; // new bots can always say "hello!"
pBot->newmsg = false;
pBot->message[0] = '\0';
pBot->msgstart[0] = '\0';
}
if (mod_id == TFC_DLL)
pBot->start_action = MSG_TFC_IDLE;
/*else if(mod_id == CSTRIKE_DLL)
pBot->start_action = MSG_CS_IDLE;
else if((mod_id == GEARBOX_DLL) && (pent_info_ctfdetect != NULL))
pBot->start_action = MSG_OPFOR_IDLE;
else if(mod_id == FRONTLINE_DLL)
pBot->start_action = MSG_FLF_IDLE;
else pBot->start_action = 0; // not needed for non-team MODs
*/
//{fp=UTIL_OpenFoxbotLog(); std::fprintf(fp, "-bot d \n",BotEnt); std::fclose(fp); }
BotSpawnInit(pBot);
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 = BOT_PITCH_SPEED;
BotEnt->v.yaw_speed = BOT_YAW_SPEED;
pBot->idle_angle = 0.0f;
pBot->idle_angle_time = 0.0f;
// pBot->lastLiftOrigin = Vector(0.0, 0.0, 0.0);
pBot->bot_skill = skill - 1; // 0 based for array indexes
if (mod_id == TFC_DLL) {
if (arg1 != nullptr && arg1[0] != 0) {
pBot->bot_team = std::atoi(arg1);
if (arg2 != nullptr && arg2[0] != 0) {
pBot->bot_class = std::atoi(arg2);
}
}
}
std::fill_n(pBot->job, JOB_BUFFER_MAX, job_struct());
botJustJoined[index] = false; // the inititation ceremony is over!
}
// This function is responsible for getting bots to spot nearby objects of
// interest and collect or use them.
void BotFindItem(bot_t* pBot) {
// check if the bot can see a flag
BotFlagSpottedCheck(pBot);
edict_t* pPickupEntity = nullptr;
Vector pickup_origin;
Vector entity_origin;
bool can_pickup;
float min_distance;
TraceResult tr;
Vector vecStart;
Vector vecEnd;
int angle_to_entity;
edict_t* pEdict = pBot->pEdict;
job_struct* newJob;
// use a MUCH smaller search radius when waypoints are available
float radius = 400.0f;
if (num_waypoints < 1 || pBot->current_wp == -1)
radius = 600.0f;
// put the search sphere in front of the bot
UTIL_MakeVectors(pBot->pEdict->v.v_angle);
Vector searchCenter = pBot->pEdict->v.origin + gpGlobals->v_forward * 200;