-
Notifications
You must be signed in to change notification settings - Fork 2
/
bot.cpp
2711 lines (2159 loc) · 82.5 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
//
// HPB bot - botman's High Ping Bastard bot
//
// (http://planethalflife.com/botman/)
//
// bot.cpp
//
#ifndef _WIN32
#include <string.h>
#endif
#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 <sys/types.h>
#include <sys/stat.h>
extern edict_t *clients[32];
extern int mod_id;
extern WAYPOINT waypoints[MAX_WAYPOINTS];
extern int num_waypoints; // number of waypoints currently in use
extern int default_bot_skill;
extern int bot_strafe_percent;
extern int bot_chat_percent;
extern int bot_taunt_percent;
extern int bot_whine_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 bool b_random_color;
extern edict_t *pent_info_ctfdetect;
extern int bot_reaction_time;
extern int IsDedicatedServer;
extern int holywars_gamemode;
extern int max_team_players[4];
extern int team_class_limits[4];
extern int team_allies[4];
extern int max_teams;
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 bool checked_teamplay;
extern bool is_team_play;
extern int number_skins;
extern skin_t bot_skins[MAX_SKINS];
static FILE *fp;
#define PLAYER_SEARCH_RADIUS 40.0
#define FLF_PLAYER_SEARCH_RADIUS 60.0
#define MAX_BOT_NAMES 100
int number_names = 0;
char bot_names[MAX_BOT_NAMES][BOT_NAME_LEN+1];
#define MAX_BOT_LOGOS 100
int num_logos = 0;
char bot_logos[MAX_BOT_LOGOS][16];
bot_t bots[32]; // max of 32 bots in a game
bool b_observer_mode = FALSE;
bool b_botdontshoot = FALSE;
// how often (out of 1000 times) the bot will pause, based on bot skill
float pause_frequency[5] = {4, 7, 10, 15, 20};
float pause_time[5][2] = {
{0.2, 0.5}, {0.5, 1.0}, {0.7, 1.3}, {1.0, 1.7}, {1.2, 2.0}};
void BotSpawnInit( bot_t *pBot )
{
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->f_waypoint_goal_time = 0.0;
pBot->waypoint_near_flag = FALSE;
pBot->waypoint_flag_origin = Vector(0, 0, 0);
pBot->prev_waypoint_distance = 0.0;
pBot->weapon_points[0] = 0;
pBot->weapon_points[1] = 0;
pBot->weapon_points[2] = 0;
pBot->weapon_points[3] = 0;
pBot->weapon_points[4] = 0;
pBot->weapon_points[5] = 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->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)
if (RANDOM_LONG(1, 100) <= 50)
pBot->wander_dir = WANDER_LEFT;
else
pBot->wander_dir = WANDER_RIGHT;
pBot->f_exit_water_time = 0.0;
pBot->pBotEnemy = NULL;
pBot->f_bot_see_enemy_time = gpGlobals->time;
pBot->f_bot_find_enemy_time = gpGlobals->time;
pBot->f_aim_tracking_time = 0.0;
pBot->f_aim_x_angle_delta = 0.0;
pBot->f_aim_y_angle_delta = 0.0;
pBot->pBotUser = NULL;
pBot->f_bot_use_time = 0.0;
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->enemy_attack_count = 0;
pBot->f_duck_time = 0.0;
pBot->f_sniper_aim_time = 0.0;
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_gren_throw_time = -1.0;
pBot->f_gren_check_time = 0.0;
pBot->b_grenade_primed = FALSE;
pBot->grenade_type = 0;
pBot->f_grenade_search_time = 0.0;
pBot->f_grenade_found_time = 0.0;
pBot->f_medic_check_time = 0.0;
pBot->f_medic_pause_time = 0.0;
pBot->f_medic_yell_time = 0.0;
pBot->f_pause_time = 0.0;
pBot->f_sound_update_time = 0.0;
pBot->bot_has_flag = FALSE;
pBot->b_see_tripmine = FALSE;
pBot->b_shoot_tripmine = FALSE;
pBot->v_tripmine = Vector(0,0,0);
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->b_use_capture = FALSE;
pBot->f_use_capture_time = 0.0;
pBot->pCaptureEdict = NULL;
pBot->b_spray_logo = FALSE;
pBot->f_engineer_build_time = 0.0;
pBot->b_build_sentrygun = FALSE;
pBot->b_build_dispenser = FALSE;
pBot->f_other_sentry_time = 0.0;
pBot->b_upgrade_sentry = FALSE;
pBot->f_medic_check_health_time = 0.0;
pBot->f_reaction_target_time = 0.0;
memset(&(pBot->current_weapon), 0, sizeof(pBot->current_weapon));
memset(&(pBot->m_rgAmmo), 0, sizeof(pBot->m_rgAmmo));
}
void BotNameInit( void )
{
FILE *bot_name_fp;
char bot_name_filename[256];
int str_index;
char name_buffer[80];
int length, index;
UTIL_BuildFileName(bot_name_filename, "HPB_bot_names.txt", NULL);
bot_name_fp = fopen(bot_name_filename, "r");
if (bot_name_fp != NULL)
{
while ((number_names < MAX_BOT_NAMES) &&
(fgets(name_buffer, 80, bot_name_fp) != NULL))
{
length = strlen(name_buffer);
if (name_buffer[length-1] == '\n')
{
name_buffer[length-1] = 0; // remove '\n'
length--;
}
str_index = 0;
while (str_index < length)
{
if ((name_buffer[str_index] < ' ') || (name_buffer[str_index] > '~') ||
(name_buffer[str_index] == '"'))
for (index=str_index; index < length; index++)
name_buffer[index] = name_buffer[index+1];
str_index++;
}
if (name_buffer[0] != 0)
{
strncpy(bot_names[number_names], name_buffer, BOT_NAME_LEN);
number_names++;
}
}
fclose(bot_name_fp);
}
}
void BotPickName( char *name_buffer )
{
int name_index, index;
bool used;
edict_t *pPlayer;
int attempts = 0;
name_index = RANDOM_LONG(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)
{
if (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 == number_names)
used = FALSE; // break out of loop even if already used
}
}
strcpy(name_buffer, bot_names[name_index]);
}
void BotCreate( edict_t *pPlayer, const char *arg1, const char *arg2,
const char *arg3, const char *arg4, const char *arg5 )
{
edict_t *BotEnt;
bot_t *pBot;
char c_skin[BOT_SKIN_LEN+1];
char c_name[BOT_NAME_LEN+1];
int skill;
int index;
int i, j, length;
bool found = FALSE;
int top_color, bottom_color;
char c_topcolor[4], c_bottomcolor[4];
top_color = -1;
bottom_color = -1;
if ((mod_id == VALVE_DLL) ||
((mod_id == GEARBOX_DLL) && (pent_info_ctfdetect == NULL)) ||
(mod_id == HOLYWARS_DLL) || (mod_id == DMC_DLL))
{
int max_skin_index;
max_skin_index = number_skins;
if ((arg1 == NULL) || (*arg1 == 0))
{
index = RANDOM_LONG(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;
}
strcpy(c_skin, bot_skins[index].model_name);
}
else
{
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] = tolower( c_skin[i] ); // convert to all lowercase
index = 0;
while ((!found) && (index < max_skin_index))
{
if (strcmp(c_skin, bot_skins[index].model_name) == 0)
found = TRUE;
else
index++;
}
if (found == TRUE)
{
if ((arg2 != NULL) && (*arg2 != 0))
{
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
strcpy(c_name, bot_skins[index].bot_name);
}
}
else
{
char dir_name[32];
char filename[128];
struct stat stat_str;
GetGameDir(dir_name);
sprintf(filename, "%s/models/player/%s", dir_name, c_skin);
if (stat(filename, &stat_str) != 0)
{
sprintf(filename, "valve/models/player/%s", c_skin);
if (stat(filename, &stat_str) != 0)
{
char err_msg[80];
sprintf( err_msg, "model \"%s\" is unknown.\n", c_skin );
if (pPlayer)
ClientPrint(pPlayer, HUD_PRINTNOTIFY, err_msg );
if (IsDedicatedServer)
printf(err_msg);
if (pPlayer)
ClientPrint(pPlayer, HUD_PRINTNOTIFY,
"use barney, gina, gman, gordon, helmet, hgrunt,\n");
if (IsDedicatedServer)
printf("use barney, gina, gman, gordon, helmet, hgrunt,\n");
if (pPlayer)
ClientPrint(pPlayer, HUD_PRINTNOTIFY,
" recon, robo, scientist, or zombie\n");
if (IsDedicatedServer)
printf(" recon, robo, scientist, or zombie\n");
return;
}
}
if ((arg2 != NULL) && (*arg2 != 0))
{
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...
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 != NULL) && (*arg3 != 0))
skill = atoi(arg3);
if ((skill < 1) || (skill > 5))
skill = default_bot_skill;
if ((arg4 != NULL) && (*arg4 != 0))
top_color = atoi(arg4);
if ((top_color < 0) || (top_color > 255))
top_color = -1;
else
sprintf(c_topcolor, "%d", top_color);
if ((arg5 != NULL) && (*arg5 != 0))
bottom_color = atoi(arg5);
if ((bottom_color < 0) || (bottom_color > 255))
bottom_color = -1;
else
sprintf(c_bottomcolor, "%d", bottom_color);
if ((top_color == -1) && (bottom_color == -1) && (b_random_color))
{
top_color = RANDOM_LONG(0, 255);
sprintf(c_topcolor, "%d", top_color);
bottom_color = RANDOM_LONG(0, 255);
sprintf(c_bottomcolor, "%d", bottom_color);
}
}
else
{
if ((arg3 != NULL) && (*arg3 != 0))
{
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
strcpy(c_name, "Bot");
}
skill = 0;
if ((arg4 != NULL) && (*arg4 != 0))
skill = atoi(arg4);
if ((skill < 1) || (skill > 5))
skill = default_bot_skill;
}
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--;
}
}
BotEnt = (*g_engfuncs.pfnCreateFakeClient)( c_name );
if (FNullEnt( BotEnt ))
{
if (pPlayer)
ClientPrint( pPlayer, HUD_PRINTNOTIFY, "Max. Players reached. Can't create HPB bot!\n");
}
else
{
char ptr[128]; // allocate space for message from ClientConnect
char *infobuffer;
int clientIndex;
int index;
if (IsDedicatedServer)
printf("Creating HPB bot...\n");
else if (pPlayer)
ClientPrint( pPlayer, HUD_PRINTNOTIFY, "Creating HPB bot...\n");
index = 0;
while ((bots[index].is_used) && (index < 32))
index++;
if (index == 32)
{
ClientPrint( pPlayer, HUD_PRINTNOTIFY, "Can't create HPB 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 );
if (!checked_teamplay) // check for team play...
BotCheckTeamplay();
// is this a MOD that supports model colors AND it's not teamplay?
if (((mod_id == VALVE_DLL) || (mod_id == DMC_DLL) ||
(mod_id == GEARBOX_DLL) || (mod_id == HOLYWARS_DLL)) &&
(is_team_play == FALSE))
{
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 );
}
else // other mods
SET_CLIENT_KEYVALUE( clientIndex, infobuffer, "model", "" ); // bugfix, thanks Whistler
if (mod_id == CSTRIKE_DLL)
{
SET_CLIENT_KEYVALUE( clientIndex, infobuffer, "rate", "3500.000000");
SET_CLIENT_KEYVALUE( clientIndex, infobuffer, "cl_updaterate", "20");
SET_CLIENT_KEYVALUE( clientIndex, infobuffer, "cl_lw", "1");
SET_CLIENT_KEYVALUE( clientIndex, infobuffer, "cl_lc", "1");
SET_CLIENT_KEYVALUE( clientIndex, infobuffer, "tracker", "0");
SET_CLIENT_KEYVALUE( clientIndex, infobuffer, "cl_dlmax", "128");
SET_CLIENT_KEYVALUE( clientIndex, infobuffer, "lefthand", "1");
SET_CLIENT_KEYVALUE( clientIndex, infobuffer, "friends", "0");
SET_CLIENT_KEYVALUE( clientIndex, infobuffer, "dm", "0");
SET_CLIENT_KEYVALUE( clientIndex, infobuffer, "ah", "1");
}
MDLL_ClientConnect( BotEnt, c_name, "127.0.0.1", ptr );
// HPB_bot metamod fix - START
// we have to do the ClientPutInServer() hook's job ourselves since calling the MDLL_
// function calls directly the gamedll one, and not ours. You are allowed to call this
// an "awful hack".
while ((i < 32) && (clients[i] != NULL))
i++;
if (i < 32)
clients[i] = BotEnt; // store this clients edict in the clients array
// HPB_bot metamod fix - END
// Pieter van Dijk - use instead of DispatchSpawn() - Hip Hip Hurray!
MDLL_ClientPutInServer( BotEnt );
BotEnt->v.flags |= FL_THIRDPARTYBOT;
// initialize all the variables for this bot...
pBot = &bots[index];
pBot->is_used = TRUE;
pBot->respawn_state = RESPAWN_IDLE;
pBot->f_create_time = gpGlobals->time;
pBot->name[0] = 0; // name not set by server yet
pBot->bot_money = 0;
pBot->sentrygun_waypoint = -1;
pBot->dispenser_waypoint = -1;
pBot->sentrygun_level = 0;
pBot->dispenser_built = 0;
strcpy(pBot->skin, c_skin);
pBot->top_color = top_color;
pBot->bottom_color = bottom_color;
pBot->pEdict = BotEnt;
BotPickLogo(pBot);
pBot->not_started = 1; // hasn't joined game yet
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
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->warmup = 0; // for Front Line Force
pBot->idle_angle = 0.0;
pBot->idle_angle_time = 0.0;
pBot->round_end = 0;
pBot->defender = 0;
pBot->strafe_percent = bot_strafe_percent;
pBot->chat_percent = bot_chat_percent;
pBot->taunt_percent = bot_taunt_percent;
pBot->whine_percent = bot_whine_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->reaction_time = bot_reaction_time;
pBot->f_start_vote_time = gpGlobals->time + RANDOM_LONG(120, 600);
pBot->vote_in_progress = FALSE;
pBot->f_vote_time = 0.0;
pBot->bot_skill = skill - 1; // 0 based for array indexes
pBot->bot_team = -1;
pBot->bot_class = -1;
if ((mod_id == TFC_DLL) || (mod_id == CSTRIKE_DLL) ||
((mod_id == GEARBOX_DLL) && (pent_info_ctfdetect != NULL)) ||
(mod_id == FRONTLINE_DLL))
{
if ((arg1 != NULL) && (arg1[0] != 0))
{
pBot->bot_team = atoi(arg1);
if ((arg2 != NULL) && (arg2[0] != 0))
{
pBot->bot_class = atoi(arg2);
}
}
}
}
}
int BotInFieldOfView(bot_t *pBot, 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
}
bool BotEntityIsVisible( bot_t *pBot, 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 );
// check if line of sight to object is not blocked (i.e. visible)
if (tr.flFraction >= 1.0)
return TRUE;
else
return FALSE;
}
void BotLogoInit(void)
{
FILE *bot_logo_fp;
char bot_logo_filename[256];
char logo_buffer[80];
int length, index;
#ifdef _WIN32
HANDLE h_logo;
char dir_name[32];
char decal_filename[256];
DWORD dwDummy;
struct stat stat_str;
wadinfo_t wad_header;
lumpinfo_t lump_info;
GetGameDir(dir_name);
sprintf(decal_filename, "%s/decals.wad", dir_name);
if (stat(decal_filename, &stat_str) != 0)
{
strcpy(decal_filename, "valve/decals.wad");
if (stat(decal_filename, &stat_str) != 0)
return; // file not found
}
h_logo = CreateFile(decal_filename, GENERIC_READ, FILE_SHARE_READ,
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
if (h_logo == INVALID_HANDLE_VALUE)
return; // can't open file
if (!ReadFile(h_logo, &wad_header, sizeof(wadinfo_t), &dwDummy, NULL))
{
CloseHandle(h_logo); // can't read wad header
return;
}
if (SetFilePointer(h_logo, wad_header.infotableofs, NULL, FILE_BEGIN) == 0)
{
CloseHandle(h_logo);
return; // can't seek to lump info table
}
for (index=0; index < wad_header.numlumps; index++)
{
if (!ReadFile(h_logo, &lump_info, sizeof(lumpinfo_t), &dwDummy, NULL))
{
CloseHandle(h_logo); // can't read lump info
return;
}
if (strncmp(lump_info.name, "{__", 3) == 0)
{
strcpy(bot_logos[num_logos], lump_info.name);
num_logos++;
}
if (strncmp(lump_info.name, "__", 2) == 0)
{
strcpy(bot_logos[num_logos], lump_info.name);
num_logos++;
}
}
CloseHandle(h_logo);
#endif
UTIL_BuildFileName(bot_logo_filename, "HPB_bot_logo.cfg", NULL);
bot_logo_fp = fopen(bot_logo_filename, "r");
if (bot_logo_fp != NULL)
{
while ((num_logos < MAX_BOT_LOGOS) &&
(fgets(logo_buffer, 80, bot_logo_fp) != NULL))
{
length = strlen(logo_buffer);
if (logo_buffer[length-1] == '\n')
{
logo_buffer[length-1] = 0; // remove '\n'
length--;
}
if (logo_buffer[0] != 0)
{
strncpy(bot_logos[num_logos], logo_buffer, 16);
num_logos++;
}
}
fclose(bot_logo_fp);
}
}
void BotPickLogo(bot_t *pBot)
{
bool used;
int logo_index;
int check_count;
int index;
pBot->logo_name[0] = 0;
if (num_logos == 0)
return;
logo_index = RANDOM_LONG(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++;
}
strcpy(pBot->logo_name, bot_logos[logo_index]);
}
void BotSprayLogo(edict_t *pEntity, char *logo_name)
{
int index;
TraceResult pTrace;
Vector v_src, v_dest;
MAKE_VECTORS(pEntity->v.v_angle);
v_src = pEntity->v.origin + pEntity->v.view_ofs;
v_dest = v_src + gpGlobals->v_forward * 80;
UTIL_TraceLine( 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.0))
{
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);
}
}
void BotFindItem( bot_t *pBot )
{
edict_t *pent = NULL;
edict_t *pPickupEntity = NULL;
Vector pickup_origin;
Vector entity_origin;
float radius = 500;
bool can_pickup;
float min_distance;
char item_name[40];
TraceResult tr;
Vector vecStart;
Vector vecEnd;
int angle_to_entity;
edict_t *pEdict = pBot->pEdict;
pBot->pBotPickupItem = NULL;
// use a MUCH smaller search radius when waypoints are available
if ((num_waypoints > 0) && (pBot->curr_waypoint_index != -1))
radius = 100.0;
else
radius = 500.0;
min_distance = radius + 1.0;
while ((pent = UTIL_FindEntityInSphere( pent, pEdict->v.origin, radius )) != NULL)
{
can_pickup = FALSE; // assume can't use it until known otherwise
strcpy(item_name, STRING(pent->v.classname));
// see if this is a "func_" type of entity (func_button, etc.)...
if (strncmp("func_", item_name, 5) == 0)
{
// BModels have 0,0,0 for origin so must use VecBModelOrigin...
entity_origin = VecBModelOrigin(pent);
vecStart = pEdict->v.origin + pEdict->v.view_ofs;
vecEnd = entity_origin;
angle_to_entity = BotInFieldOfView( pBot, vecEnd - vecStart );
// check if entity is outside field of view (+/- 45 degrees)