forked from Bots-United/jk_botti
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.cpp
2937 lines (2361 loc) · 88.4 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
//
// JK_Botti - be more human!
//
// bot.cpp
//
#ifndef _WIN32
#include <string.h>
#endif
#include <malloc.h>
#include <extdll.h>
#include <dllapi.h>
#include <h_export.h>
#include <meta_api.h>
#include "bot.h"
#include "bot_func.h"
#include "waypoint.h"
#include "bot_weapons.h"
#include "bot_skill.h"
#include "bot_config_init.h"
#include <sys/types.h>
#include <sys/stat.h>
extern WAYPOINT waypoints[MAX_WAYPOINTS];
extern int num_waypoints; // number of waypoints currently in use
extern int default_bot_skill;
extern int bot_add_level_tag;
extern int bot_chat_percent;
extern int bot_taunt_percent;
extern int bot_whine_percent;
extern int bot_endgame_percent;
extern int bot_logo_percent;
extern int bot_chat_tag_percent;
extern int bot_chat_drop_percent;
extern int bot_chat_swap_percent;
extern int bot_chat_lower_percent;
extern qboolean b_random_color;
extern qboolean debug_minmax;
extern qboolean g_in_intermission;
extern bot_chat_t bot_chat[MAX_BOT_CHAT];
extern bot_chat_t bot_whine[MAX_BOT_CHAT];
extern int bot_chat_count;
extern int bot_whine_count;
extern int recent_bot_chat[];
extern int recent_bot_whine[];
extern qboolean checked_teamplay;
extern qboolean is_team_play;
extern float bot_think_spf;
extern int team_balancetype;
extern char *team_blockedlist;
extern char g_team_list[TEAMPLAY_TEAMLISTLENGTH];
extern char g_team_names[MAX_TEAMS][MAX_TEAMNAME_LENGTH];
extern int g_team_scores[MAX_TEAMS];
extern int g_num_teams;
extern qboolean g_team_limit;
extern int number_skins;
extern skin_t bot_skins[MAX_SKINS];
bot_t bots[32]; // max of 32 bots in a game
qboolean b_observer_mode = FALSE;
qboolean b_botdontshoot = FALSE;
//
static qboolean BotLowHealth( bot_t &pBot )
{
return(pBot.pEdict->v.health + 0.8f * pBot.pEdict->v.armorvalue < VALVE_MAX_NORMAL_HEALTH * 0.5f);
}
//
void BotKick(bot_t &pBot)
{
char cmd[64];
if(pBot.userid <= 0) // user id filled in yet?
pBot.userid = GETPLAYERUSERID(pBot.pEdict);
JKASSERT(pBot.is_used == FALSE);
JKASSERT(FNullEnt(pBot.pEdict));
JKASSERT(pBot.userid <= 0);
safevoid_snprintf(cmd, sizeof(cmd), "kick # %d\n", pBot.userid);
SERVER_COMMAND(cmd); // kick the bot using 'kick # <userid>'
SERVER_EXECUTE();
}
//
static void BotSpawnInit( bot_t &pBot )
{
pBot.bot_think_time = -1.0;
pBot.f_last_think_time = gpGlobals->time - 0.1;
pBot.f_recoil = 0;
pBot.v_prev_origin = Vector(9999.0, 9999.0, 9999.0);
pBot.f_speed_check_time = gpGlobals->time;
pBot.waypoint_origin = Vector(0, 0, 0);
pBot.f_waypoint_time = 0.0;
pBot.curr_waypoint_index = -1;
pBot.prev_waypoint_index[0] = -1;
pBot.prev_waypoint_index[1] = -1;
pBot.prev_waypoint_index[2] = -1;
pBot.prev_waypoint_index[3] = -1;
pBot.prev_waypoint_index[4] = -1;
pBot.f_random_waypoint_time = gpGlobals->time;
pBot.waypoint_goal = -1;
pBot.wpt_goal_type = WPT_GOAL_NONE;
pBot.f_waypoint_goal_time = 0.0;
pBot.prev_waypoint_distance = 0.0;
pBot.f_last_item_found = 0.0;
pBot.pTrackSoundEdict = NULL;
pBot.f_track_sound_time = 0.0;
memset(pBot.exclude_points, 0, sizeof(pBot.exclude_points));
pBot.b_on_ground = 0;
pBot.b_on_ladder = 0;
pBot.b_in_water = 0;
pBot.b_ducking = 0;
pBot.b_has_enough_ammo_for_good_weapon = 0;
pBot.b_low_health = 0;
pBot.f_last_time_attacked = 0;
pBot.eagle_secondary_state = 0;
pBot.blinded_time = 0.0;
pBot.f_max_speed = CVAR_GET_FLOAT("sv_maxspeed");
pBot.f_prev_speed = 0.0; // fake "paused" since bot is NOT stuck
pBot.f_find_item = 0.0;
pBot.b_not_maxspeed = FALSE;
pBot.ladder_dir = LADDER_UNKNOWN;
pBot.f_start_use_ladder_time = 0.0;
pBot.f_end_use_ladder_time = 0.0;
pBot.waypoint_top_of_ladder = FALSE;
pBot.f_wall_check_time = 0.0;
pBot.f_wall_on_right = 0.0;
pBot.f_wall_on_left = 0.0;
pBot.f_dont_avoid_wall_time = 0.0;
pBot.f_look_for_waypoint_time = 0.0;
pBot.f_jump_time = 0.0;
pBot.f_drop_check_time = 0.0;
// pick a wander direction (50% of the time to the left, 50% to the right)
pBot.wander_dir = (RANDOM_LONG2(1, 100) <= 50) ? WANDER_LEFT : WANDER_RIGHT;
pBot.f_move_direction = 1;
pBot.f_exit_water_time = 0.0;
pBot.pBotEnemy = NULL;
pBot.f_bot_see_enemy_time = gpGlobals->time;
pBot.v_bot_see_enemy_origin = Vector(-99999,-99999,-99999);
pBot.f_next_find_visible_sound_enemy_time = 0.0f;
pBot.f_duck_time = 0.0;
pBot.f_random_jump_time = 0.0;
pBot.f_random_jump_duck_time = 0.0;
pBot.f_random_jump_duck_end = 0.0;
pBot.f_random_duck_time = 0.0;
pBot.prev_random_type = 0;
pBot.f_sniper_aim_time = 0.0;
pBot.b_longjump_do_jump = FALSE;
pBot.b_longjump = FALSE;
pBot.f_combat_longjump = 0.0;
pBot.f_longjump_time = 0.0;
pBot.b_combat_longjump = FALSE;
pBot.f_shoot_time = gpGlobals->time;
pBot.f_primary_charging = -1.0;
pBot.f_secondary_charging = -1.0;
pBot.charging_weapon_id = 0;
pBot.f_grenade_search_time = 0.0;
pBot.f_grenade_found_time = 0.0;
pBot.current_weapon_index = -1;
pBot.current_opt_distance = 99999.0;
pBot.f_pause_time = 0.0;
pBot.f_sound_update_time = 0.0;
pBot.b_see_tripmine = FALSE;
pBot.b_shoot_tripmine = FALSE;
pBot.v_tripmine = Vector(0,0,0);
pBot.tripmine_edict = NULL;
pBot.b_use_health_station = FALSE;
pBot.f_use_health_time = 0.0;
pBot.b_use_HEV_station = FALSE;
pBot.f_use_HEV_time = 0.0;
pBot.b_use_button = FALSE;
pBot.f_use_button_time = 0;
pBot.b_lift_moving = FALSE;
pBot.v_use_target = Vector(0,0,0);
pBot.b_spray_logo = FALSE;
pBot.f_reaction_target_time = 0.0;
pBot.b_set_special_shoot_angle = FALSE;
pBot.f_special_shoot_angle = 0.0;
pBot.f_weaponchange_time = 0.0;
pBot.f_pause_look_time = 0.0;
pBot.f_current_hearing_sensitivity = -1;
memset(&(pBot.current_weapon), 0, sizeof(pBot.current_weapon));
memset(&(pBot.m_rgAmmo), 0, sizeof(pBot.m_rgAmmo));
}
static void BotPickLogo(bot_t &pBot)
{
qboolean used;
int logo_index;
int check_count;
int index;
pBot.logo_name[0] = 0;
if (num_logos == 0)
return;
logo_index = RANDOM_LONG2(1, num_logos) - 1; // zero based
// check make sure this name isn't used
used = TRUE;
check_count = 0;
while ((used) && (check_count < MAX_BOT_LOGOS))
{
used = FALSE;
for (index=0; index < 32; index++)
{
if (bots[index].is_used)
{
if (strcmp(bots[index].logo_name, bot_logos[logo_index]) == 0)
{
used = TRUE;
}
}
}
if (used)
logo_index++;
if (logo_index == MAX_BOT_LOGOS)
logo_index = 0;
check_count++;
}
safe_strcopy(pBot.logo_name, sizeof(pBot.logo_name), bot_logos[logo_index]);
}
static void BotSprayLogo(bot_t &pBot)
{
edict_t * pEntity = pBot.pEdict;
char *logo_name = pBot.logo_name;
int index;
TraceResult pTrace;
Vector v_src, v_dest;
v_src = pEntity->v.origin + pEntity->v.view_ofs;
v_dest = v_src + UTIL_AnglesToForward(pEntity->v.v_angle) * 80;
UTIL_TraceMove( v_src, v_dest, ignore_monsters, pEntity->v.pContainingEntity, &pTrace );
index = DECAL_INDEX(logo_name);
if (index < 0)
return;
if ((pTrace.pHit) && (pTrace.flFraction < 1.0f))
{
if (pTrace.pHit->v.solid != SOLID_BSP)
return;
MESSAGE_BEGIN( MSG_BROADCAST, SVC_TEMPENTITY );
if (index > 255)
{
WRITE_BYTE( TE_WORLDDECALHIGH);
index -= 256;
}
else
WRITE_BYTE( TE_WORLDDECAL );
WRITE_COORD( pTrace.vecEndPos.x );
WRITE_COORD( pTrace.vecEndPos.y );
WRITE_COORD( pTrace.vecEndPos.z );
WRITE_BYTE( index );
MESSAGE_END();
EMIT_SOUND_DYN2(pEntity, CHAN_VOICE, "player/sprayer.wav", 1.0, ATTN_NORM, 0, 100);
}
}
static void BotPickName( char *name_buffer, int sizeof_name_buffer )
{
int name_index, index;
qboolean used;
edict_t *pPlayer;
int attempts = 0;
name_index = RANDOM_LONG2(1, number_names) - 1; // zero based
// check make sure this name isn't used
used = TRUE;
while (used)
{
used = FALSE;
for (index = 1; index <= gpGlobals->maxClients; index++)
{
pPlayer = INDEXENT(index);
if (pPlayer && !pPlayer->free && !FBitSet(pPlayer->v.flags, FL_PROXY))
{
const char * netname = STRING(pPlayer->v.netname);
//check if bot name is [lvlX]name format...
if (strncmp(netname, "[lvl", 4) == 0 && netname[4] >= '1' && netname[4] <= '5' && netname[5] == ']')
netname += 6;
if (strcmp(bot_names[name_index], netname) == 0)
{
used = TRUE;
break;
}
}
}
if (used)
{
name_index++;
if (name_index == number_names)
name_index = 0;
attempts++;
if (attempts == number_names)
used = FALSE; // break out of loop even if already used
}
}
safe_strcopy(name_buffer, sizeof_name_buffer, bot_names[name_index]);
}
//
static qboolean TeamInTeamBlockList(const char * teamname)
{
if(!team_blockedlist)
team_blockedlist = strdup("");
// make a copy because strtok is destructive
int slen = strlen(team_blockedlist);
char * blocklist = (char*)alloca(slen+1);
memcpy(blocklist, team_blockedlist, slen+1);
char *pName = strtok( blocklist, ";" );
while ( pName != NULL && *pName)
{
if(!stricmp(teamname, pName))
{
//UTIL_ConsolePrintf("in block list: %s", pName);
return(TRUE);
}
pName = strtok( NULL, ";" );
}
return(FALSE);
}
//
char * GetSpecificTeam(char * teamstr, size_t slen, qboolean get_smallest, qboolean get_largest, qboolean only_count_bots)
{
if(!!get_smallest + !!get_largest != 1)
return(NULL);
// get team
int find_index = -1;
int find_count = get_smallest ? 9999 : -1;
for(int i = 0; i < MAX_TEAMS; i++)
{
if(*g_team_names[i] && !TeamInTeamBlockList(g_team_names[i]))
{
int count = 0;
char teamname[MAX_TEAMNAME_LENGTH];
// collect player counts for team
for(int j = 1; j <= gpGlobals->maxClients; j++)
{
edict_t * pClient = INDEXENT(j);
// skip unactive clients
if(!pClient || pClient->free || FNullEnt(pClient) || GETPLAYERUSERID(pClient) <= 0 || STRING(pClient->v.netname)[0] == 0)
continue;
// skip bots?
if(only_count_bots && UTIL_GetBotIndex(pClient) != -1)
continue;
// match team
if(!stricmp(UTIL_GetTeam(pClient, teamname, sizeof(teamname)), g_team_names[i]))
{
count++;
}
}
if(get_smallest)
{
// smaller?
if(count < find_count)
{
find_count = count;
find_index = i;
}
}
else
{
// larger?
if(count > find_count)
{
find_count = count;
find_index = i;
}
}
}
}
// got it?
if(find_index != -1)
{
if(get_largest && find_count == 0)
return(NULL);
safe_strcopy(teamstr, slen, g_team_names[find_index]);
return(teamstr);
}
return(NULL);
}
//
static int GetTeamIndex( const char *pTeamName )
{
if ( pTeamName && *pTeamName != 0 )
{
// try to find existing team
for ( int tm = 0; tm < g_num_teams; tm++ )
{
if ( !stricmp( g_team_names[tm], pTeamName ) )
return tm;
}
}
return -1; // No match
}
//
static void RecountTeams(void)
{
if(!is_team_play)
return;
// Construct teams list
char teamlist[TEAMPLAY_TEAMLISTLENGTH];
char *pName;
// loop through all teams, recounting everything
g_num_teams = 0;
// Copy all of the teams from the teamlist
// make a copy because strtok is destructive
safe_strcopy(teamlist, sizeof(teamlist), g_team_list);
pName = teamlist;
pName = strtok( pName, ";" );
while ( pName != NULL && *pName )
{
if ( GetTeamIndex( pName ) < 0 )
{
safe_strcopy(g_team_names[g_num_teams], sizeof(g_team_names[g_num_teams]), pName);
g_num_teams++;
if(g_num_teams == MAX_TEAMS)
break;
}
pName = strtok( NULL, ";" );
}
if ( g_num_teams < 2 )
{
g_num_teams = 0;
g_team_limit = FALSE;
}
// Sanity check
memset( g_team_scores, 0, sizeof(g_team_scores) );
// loop through all clients
for ( int i = 1; i <= gpGlobals->maxClients; i++ )
{
edict_t * plr = INDEXENT(i);
if(!plr || plr->free || FNullEnt(plr) || GETPLAYERUSERID(plr) <= 0 || STRING(plr->v.netname)[0] == 0)
continue;
char teamname[MAX_TEAMNAME_LENGTH];
const char *pTeamName;
pTeamName = UTIL_GetTeam(plr, teamname, sizeof(teamname));
// try add to existing team
int tm = GetTeamIndex( pTeamName );
if ( tm < 0 ) // no team match found
{
if ( !g_team_limit && g_num_teams < MAX_TEAMS)
{
// add to new team
tm = g_num_teams;
g_num_teams++;
g_team_scores[tm] = 0;
safe_strcopy(g_team_names[tm], sizeof(g_team_names[tm]), pTeamName);
}
}
if ( tm >= 0 )
{
g_team_scores[tm] += (int)plr->v.frags;
}
}
}
//
void BotCheckTeamplay(void)
{
float f_team_play = CVAR_GET_FLOAT("mp_teamplay"); // teamplay enabled?
if (f_team_play > 0.0f)
is_team_play = TRUE;
else
is_team_play = FALSE;
checked_teamplay = TRUE;
// get team list, exactly as in teamplay_gamerules.cpp
if(is_team_play)
{
safe_strcopy(g_team_list, sizeof(g_team_list), CVAR_GET_STRING("mp_teamlist"));
edict_t *pWorld = INDEXENT(0);
if ( pWorld && pWorld->v.team )
{
if ( CVAR_GET_FLOAT("mp_teamoverride") != 0.0f )
{
const char *pTeamList = STRING(pWorld->v.team);
if ( pTeamList && *pTeamList )
{
safe_strcopy(g_team_list, sizeof(g_team_list), pTeamList);
}
}
}
// Has the server set teams
g_team_limit = ( *g_team_list != 0 );
RecountTeams();
}
else
{
g_team_list[0] = 0;
g_team_limit = FALSE;
memset(g_team_names, 0, sizeof(g_team_names));
}
}
//
void BotCreate( const char *skin, const char *name, int skill, int top_color, int bottom_color, int cfg_bot_index )
{
edict_t *BotEnt;
char c_skin[BOT_SKIN_LEN];
char c_name[BOT_NAME_LEN];
char balanceskin[MAX_TEAMNAME_LENGTH];
int index;
int i, j, length;
qboolean found = FALSE;
qboolean got_skill_arg = FALSE;
char c_topcolor[4], c_bottomcolor[4];
int max_skin_index;
max_skin_index = number_skins;
// balance teams, ignore input skin
if(is_team_play && team_balancetype >= 1 && g_team_limit)
{
RecountTeams();
// get smallest team
if(GetSpecificTeam(balanceskin, sizeof(balanceskin), TRUE, FALSE, FALSE))
{
if (skin != NULL && skin[0] != '\0')
UTIL_ConsolePrintf("[warning] Teambalance overriding input model '%s' with '%s'", skin, balanceskin);
skin = balanceskin;
}
}
if (skin == NULL || skin[0] == '\0')
{
index = RANDOM_LONG2(0, number_skins-1);
// check if this skin has already been used...
while (bot_skins[index].skin_used == TRUE)
{
index++;
if (index == max_skin_index)
index = 0;
}
bot_skins[index].skin_used = TRUE;
// check if all skins are now used...
for (i = 0; i < max_skin_index; i++)
{
if (bot_skins[i].skin_used == 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++)
bot_skins[i].skin_used = FALSE;
}
safe_strcopy(c_skin, sizeof(c_skin), bot_skins[index].model_name);
}
else
{
safe_strcopy(c_skin, sizeof(c_skin), skin);
}
for (i = 0; c_skin[i] != 0; i++)
c_skin[i] = tolower( c_skin[i] ); // convert to all lowercase
// check existance of player model file only on listenserver, dedicated server doesn't _need_ model.
if(!IS_DEDICATED_SERVER())
{
index = 0;
while ((!found) && (index < max_skin_index))
{
if (strcmp(c_skin, bot_skins[index].model_name) == 0)
found = TRUE;
else
index++;
}
if (found == FALSE)
{
char dir_name[128];
char filename[256];
struct stat stat_str;
GetGameDir(dir_name);
safevoid_snprintf(filename, sizeof(filename), "%s/models/player/%s", dir_name, c_skin);
if (stat(filename, &stat_str) != 0)
{
safevoid_snprintf(filename, sizeof(filename), "valve/models/player/%s", c_skin);
if (stat(filename, &stat_str) != 0)
{
UTIL_ConsolePrintf("model \"%s\" is unknown.\n", c_skin );
UTIL_ConsolePrintf("use barney, gina, gman, gordon, helmet, hgrunt,\n");
UTIL_ConsolePrintf(" recon, robo, scientist, or zombie\n");
return;
}
}
}
}
if ((name != NULL) && (*name != 0))
{
safe_strcopy(c_name, sizeof(c_name), name);
}
else
{
if (number_names > 0)
BotPickName( c_name, sizeof(c_name) );
else
{
// copy the name of the model to the bot's name...
safe_strcopy(c_name, sizeof(c_name), c_skin);
}
}
if (skill >= 1 && skill <= 5)
{
got_skill_arg = TRUE;
}
if (skill < 1 || skill > 5)
{
skill = default_bot_skill;
got_skill_arg = FALSE;
}
if ((top_color < 0) || (top_color > 255))
top_color = RANDOM_LONG2(0, 255);
if ((bottom_color < 0) || (bottom_color > 255))
bottom_color = RANDOM_LONG2(0, 255);
safevoid_snprintf(c_topcolor, sizeof(c_topcolor), "%d", top_color);
safevoid_snprintf(c_bottomcolor, sizeof(c_bottomcolor), "%d", bottom_color);
length = strlen(c_name);
// remove any illegal characters from name...
for (i = 0; i < length; i++)
{
if ((c_name[i] <= ' ') || (c_name[i] > '~') ||
(c_name[i] == '"'))
{
for (j = i; j < length; j++) // shuffle chars left (and null)
c_name[j] = c_name[j+1];
length--;
}
}
//
// Bug fix: remove "(1)" tags from name, HLDS adds "(1)" on duplicated names (and "(2), (3), ...")
//
if(c_name[0] == '(' && (c_name[1] >= '1' && c_name[1] <= '9') && c_name[2] == ')')
{
length = strlen(&c_name[3]) + 1; // str+null
for(i = 0; i < length; i++)
c_name[i] = (&c_name[3])[i];
}
//
// Bug fix: remove [lvlX] tags always
//
if(!strncmp(c_name, "[lvl", 4) && (c_name[4] >= '1' && c_name[4] <= '5') && c_name[5] == ']')
{
// Read skill from config file name
if(!got_skill_arg)
skill = c_name[4] - '0';
length = strlen(&c_name[6]) + 1; // str+null
for(i = 0; i < length; i++)
c_name[i] = (&c_name[6])[i];
}
//
if(bot_add_level_tag)
{
char tmp[sizeof(c_name)];
safevoid_snprintf(tmp, sizeof(tmp), "[lvl%d]%s", skill, c_name);
tmp[sizeof(tmp)-1] = 0; // make sure c_name is null terminated
strcpy(c_name, tmp);
c_name[sizeof(c_name)-1] = 0; // make sure c_name is null terminated
}
if(UTIL_GetBotCount() < 32)
BotEnt = (*g_engfuncs.pfnCreateFakeClient)( c_name );
else
BotEnt = NULL;
if (FNullEnt( BotEnt ))
{
UTIL_ConsolePrintf("%s\n", "Max. Players reached. Can't create bot!");
}
else
{
char ptr[128]; // allocate space for message from ClientConnect
char *infobuffer;
int clientIndex;
int index;
UTIL_ConsolePrintf("Creating bot...\n");
index = 0;
while ((bots[index].is_used) && (index < 32))
index++;
if (index == 32)
{
UTIL_ConsolePrintf("Can't create bot!\n");
return;
}
// create the player entity by calling MOD's player function
// (from LINK_ENTITY_TO_CLASS for player object)
CALL_GAME_ENTITY (PLID, "player", VARS(BotEnt));
infobuffer = GET_INFOKEYBUFFER( BotEnt );
clientIndex = ENTINDEX( BotEnt );
// is this a MOD that supports model colors AND it's not teamplay?
SET_CLIENT_KEYVALUE( clientIndex, infobuffer, "model", c_skin );
if (top_color != -1)
SET_CLIENT_KEYVALUE( clientIndex, infobuffer, "topcolor", c_topcolor );
if (bottom_color != -1)
SET_CLIENT_KEYVALUE( clientIndex, infobuffer, "bottomcolor", c_bottomcolor );
// JK_Botti fix, call our own ClientConnect first.
jkbotti_ClientConnect( BotEnt, c_name, "::::local:jk_botti", ptr );
MDLL_ClientConnect( BotEnt, c_name, "127.0.0.1", ptr );
// JK_Botti fix, call our own ClientPutInServer first.
jkbotti_ClientPutInServer( BotEnt );
MDLL_ClientPutInServer( BotEnt );
BotEnt->v.flags |= FL_THIRDPARTYBOT | FL_FAKECLIENT;
// initialize all the variables for this bot...
bot_t &pBot = bots[index];
memset(&pBot, 0, sizeof(pBot));
pBot.is_used = TRUE;
pBot.userid = 0;
pBot.cfg_bot_index = cfg_bot_index;
pBot.f_create_time = gpGlobals->time;
pBot.name[0] = 0; // name not set by server yet
safe_strcopy(pBot.skin, sizeof(pBot.skin), c_skin);
pBot.top_color = top_color;
pBot.bottom_color = bottom_color;
pBot.pEdict = BotEnt;
BotPickLogo(pBot);
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;
// these should REALLY be MOD dependant...
BotEnt->v.pitch_speed = 270; // slightly faster than HLDM of 225
BotEnt->v.yaw_speed = 250; // slightly faster than HLDM of 210
pBot.idle_angle = 0.0;
pBot.idle_angle_time = 0.0;
pBot.chat_percent = bot_chat_percent;
pBot.taunt_percent = bot_taunt_percent;
pBot.whine_percent = bot_whine_percent;
pBot.endgame_percent = bot_endgame_percent;
pBot.chat_tag_percent = bot_chat_tag_percent;
pBot.chat_drop_percent = bot_chat_drop_percent;
pBot.chat_swap_percent = bot_chat_swap_percent;
pBot.chat_lower_percent = bot_chat_lower_percent;
pBot.logo_percent = bot_logo_percent;
pBot.f_strafe_direction = 0.0; // not strafing
pBot.f_strafe_time = 0.0;
pBot.bot_skill = skill - 1; // 0 based for array indexes
pBot.weapon_skill = skill;
pBot.b_bot_say = FALSE;
pBot.f_bot_say = 0.0;
pBot.bot_say_msg[0] = 0;
pBot.f_bot_chat_time = gpGlobals->time;
pBot.b_bot_endgame = FALSE;
// use system wide timer for connection times
// bot will stay 30-160 minutes
pBot.stay_time = 60 * (double)RANDOM_FLOAT2(30, 160);
// bot has been already here for 10%-50% of the total stay time
pBot.connect_time = UTIL_GetSecs() - pBot.stay_time * (double)RANDOM_FLOAT2(0.2, 0.8);
}
}
//
void BotReplaceConnectionTime(const char * name, float * timeslot)
{
for(int i = 0; i < 32; i++)
{
bot_t &pBot = bots[i];
// find bot by name
if(strcmp(pBot.name, name) != 0)
continue;
double current_time = UTIL_GetSecs();
// check if stay time has been exceeded
if(current_time - pBot.connect_time > pBot.stay_time || current_time - pBot.connect_time <= 0)
{
// use system wide timer for connection times
// bot will stay 30-160 minutes
pBot.stay_time = 60 * (double)RANDOM_FLOAT2(30, 160);
// bot has been already here for 20%-80% of the total stay time
pBot.connect_time = current_time - pBot.stay_time * (double)RANDOM_FLOAT2(0.2, 0.8);
}
*timeslot = (float)(current_time - pBot.connect_time);
break;
}
}
//
static int BotInFieldOfView(bot_t &pBot, const Vector & dest)
{
// find angles from source to destination...
Vector entity_angles = UTIL_VecToAngles( dest );
// make yaw angle 0 to 360 degrees if negative...
if (entity_angles.y < 0)
entity_angles.y += 360;
// get bot's current view angle...
float view_angle = pBot.pEdict->v.v_angle.y;
// make view angle 0 to 360 degrees if negative...
if (view_angle < 0)
view_angle += 360;
// return the absolute value of angle to destination entity
// zero degrees means straight ahead, 45 degrees to the left or
// 45 degrees to the right is the limit of the normal view angle
// rsm - START angle bug fix
int angle = abs((int)view_angle - (int)entity_angles.y);
if (angle > 180)
angle = 360 - angle;
return angle;
// rsm - END
}
static qboolean 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->v.pContainingEntity, &tr );