-
Notifications
You must be signed in to change notification settings - Fork 19
/
bot.cpp
4434 lines (3755 loc) · 153 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
/**
* RealBot : Artificial Intelligence
* Version : Work In Progress
* Author : Stefan Hendriks
* Url : http://realbot.bots-united.com
**
* DISCLAIMER
*
* History, Information & Credits:
* RealBot is based partially upon the HPB-Bot Template #3 by Botman
* Thanks to Ditlew (NNBot), Pierre Marie Baty (RACCBOT), Tub (RB AI PR1/2/3)
* Greg Slocum & Shivan (RB V1.0), Botman (HPB-Bot) and Aspirin (JOEBOT). And
* everybody else who helped me with this project.
* Storage of Visibility Table using BITS by Cheesemonster.
*
* Some portions of code are from other bots, special thanks (and credits) go
* to (in no specific order):
*
* Pierre Marie Baty
* Count - Floyd
*
* !! BOTS-UNITED FOREVER !!
*
* This project is open-source, it is protected under the GPL license;
* By using this source-code you agree that you will ALWAYS release the
* source-code with your project.
*
**/
/*
//=========================================================
// Returns if enemy can be shoot through some obstacle
//=========================================================
bool CBaseBot::IsShootableThruObstacle(Vector vecDest)
{
if (!WeaponShootsThru(m_iCurrentWeapon))
return FALSE;
Vector vecSrc = EyePosition();
Vector vecDir = (vecDest - vecSrc).Normalize(); // 1 unit long
Vector vecPoint = g_vecZero;
int iThickness = 0;
int iHits = 0;
edict_t *pentIgnore = pev->pContainingEntity;
TraceResult tr;
UTIL_TraceLine(vecSrc, vecDest, ignore_monsters, ignore_glass, pentIgnore, &tr);
while (tr.flFraction != 1.0 && iHits < 3)
{
iHits++;
iThickness++;
vecPoint = tr.vecEndPos + vecDir;
while (POINT_CONTENTS(vecPoint) == CONTENTS_SOLID && iThickness < 64)
{
vecPoint = vecPoint + vecDir;
iThickness++;
}
UTIL_TraceLine(vecPoint, vecDest, ignore_monsters, ignore_glass, pentIgnore, &tr);
}
if (iHits < 3 && iThickness < 64)
{
if (LengthSquared(vecDest - vecPoint) < 12544)
return TRUE;
}
return FALSE;
}
*/
#include <string.h>
#include <extdll.h>
#include <dllapi.h>
#include <meta_api.h>
#include <entity_state.h>
#include "bot.h"
#include "bot_weapons.h"
#include "bot_func.h"
#include "game.h"
#include "NodeMachine.h"
#include "ChatEngine.h"
#include <sys/types.h>
#include <sys/stat.h>
extern edict_t *pHostEdict;
extern int mod_id;
extern bool internet_play;
extern cGame Game;
extern cNodeMachine NodeMachine;
extern cChatEngine ChatEngine;
extern int counterstrike;
//static FILE *fp;
extern bool autoskill;
/* Radio issue
Credit by Ditlew (NNBOT - Rest In Peace) */
bool radio_message = false;
char *message = (char *) malloc(64 * sizeof(char));
char radio_messenger[30];
// random boundries
extern int random_max_skill;
extern int random_min_skill;
cBot bots[32]; // max of 32 bots in a game
// External added variables
extern bool end_round; // End round
#ifndef _WIN32
#define _snprintf snprintf
#endif
cBot::cBot() {
pBotHostage = NULL;
fMoveToNodeTime = -1;
clearHostages();
}
/******************************************************************************
Function purpose: Initializes bot vars on spawn
******************************************************************************/
void cBot::SpawnInit() {
rprint_trace("SpawnInit()", "START");
// ------------------------
// TIMERS
// ------------------------
fUpdateTime = gpGlobals->time;
fLastRunPlayerMoveTime = gpGlobals->time - 0.1f;
fButtonTime = gpGlobals->time;
fChatTime = gpGlobals->time + RANDOM_FLOAT(0.5, 5);
fMemoryTime = gpGlobals->time;
fDoRadio = gpGlobals->time;
float freezeTimeCVAR = CVAR_GET_FLOAT("mp_freezetime");
fNotStuckTime = gpGlobals->time + freezeTimeCVAR + 0.5f;
f_shoot_wait_time = gpGlobals->time;
f_goback_time = gpGlobals->time;
f_may_jump_time = gpGlobals->time;
fCheckHostageStatusTimer = gpGlobals->time;
f_defuse = gpGlobals->time;
f_allow_keypress = gpGlobals->time;
f_use_timer = gpGlobals->time;
f_light_time = gpGlobals->time;
f_sec_weapon = gpGlobals->time;
f_prim_weapon = gpGlobals->time;
f_gren_time = gpGlobals->time;
f_walk_time = gpGlobals->time;
f_hear_time = gpGlobals->time;
freezeTime = gpGlobals->time - 1;
f_cover_time = gpGlobals->time;
f_c4_time = gpGlobals->time;
f_update_weapon_time = gpGlobals->time;
f_follow_time = gpGlobals->time;
f_jump_time = 0.0;
f_hold_duck = gpGlobals->time;
f_camp_time = gpGlobals->time;
f_wait_time = gpGlobals->time;
f_bot_see_enemy_time = gpGlobals->time;
f_bot_find_enemy_time = gpGlobals->time;
f_shoot_time = gpGlobals->time;
fMoveToNodeTime = -1;
nodeTimeIncreasedAmount = 0;
distanceMovedTimer = gpGlobals->time;
distanceMoved = 0;
fBlindedTime = gpGlobals->time;
f_console_timer = gpGlobals->time + RANDOM_FLOAT(0.1, 0.9);
fWanderTime = gpGlobals->time;
f_strafe_time = gpGlobals->time;
// Personality Related (these gets changed when loading personality file)
fpXOffset = 0.0;
fpYOffset = 0.0;
fpZOffset = 0.0;
fpMinReactTime = 0.0;
fpMaxReactTime = 0.0;
// ------------------------
// POINTERS
// ------------------------
pButtonEdict = NULL;
pBotHostage = NULL;
clearHostages();
pEnemyEdict = NULL;
// chat
memset(chChatSentence, 0, sizeof(chChatSentence));
// ------------------------
// INTEGERS
// ------------------------
iGoalNode = -1;
goalIndex = -1;
iPreviousGoalNode = -1;
iCloseNode = -1;
iDiedNode = -1;
iTeam = -1;
bot_class = -1;
i_camp_style = 0;
iPrimaryWeapon = -1;
iSecondaryWeapon = -1;
zoomed = ZOOM_NONE;
play_rounds = RANDOM_LONG(Game.GetMinPlayRounds(), Game.GetMaxPlayRounds());
bot_health = 0;
prev_health = 0;
bot_armor = 0;
bot_weapons = 0;
bot_use_special = 0 + RANDOM_LONG(0, 2);
console_nr = 0;
pathIndex = -1;
iPathFlags = PATH_DANGER;
// Smarter Stuck stuff
iDuckTries = 0;
iJumpTries = 0;
// ------------------------
// BOOLEANS
// ------------------------
vip = UTIL_IsVip(pEdict);
bWalkKnife = false;
buy_ammo_primary = true;
buy_ammo_secondary = true;
buy_primary = (Game.bPistols ? false : true); //30/07/04: Josh, handle the pistols only mode
buy_secondary = (Game.bPistols ? true : false);
buy_armor = false;
buy_defusekit = false;
bFirstOutOfSight = false;
buy_grenade = false;
buy_smokegrenade = false;
buy_flashbang = 0;
if (RANDOM_LONG(0, 100) < ipWalkWithKnife) {
bWalkKnife = true;
}
if (UTIL_GetTeam(pEdict) == 1) {
if (RANDOM_LONG(0, 100) < ipBuyDefuseKit) {
buy_defusekit = true;
}
}
if (RANDOM_LONG(0, 100) < ipBuyGrenade) {
buy_grenade = true;
}
// 31.08.04 Frashman added Support for Smoke Grenade
if (RANDOM_LONG(0, 100) < ipBuySmokeGren) {
buy_smokegrenade = true;
}
if (RANDOM_LONG(0, 100) < ipBuyFlashBang) {
buy_flashbang = 2;
}
if (RANDOM_LONG(0, 100) < 15 || Game.bPistols)
buy_secondary = true;
// ------------------------
// HUD
// ------------------------
bHUD_C4_plantable = false; // Get's init'ed anyway... // BERKED
// ------------------------
// FLOATS
// ------------------------
f_strafe_speed = 0.0;
f_max_speed = CVAR_GET_FLOAT("sv_maxspeed");
// ------------------------
// VECTORS
// ------------------------
prevOrigin = Vector(9999.0, 9999.0, 9999.0);
lastSeenEnemyVector = Vector(0, 0, 0);
vEar = Vector(9999, 9999, 9999);
// ------------------------
// CHAR
// ------------------------
arg1[0] = 0;
arg2[0] = 0;
arg3[0] = 0;
memset(&(current_weapon), 0, sizeof(current_weapon));
memset(&(m_rgAmmo), 0, sizeof(m_rgAmmo));
rprint_trace("SpawnInit()", "END");
}
/******************************************************************************
Function purpose: Initializes bot vars on new round
******************************************************************************/
void cBot::NewRound() {
rprint_trace("NewRound()", "START");
// ------------------------
// TIMERS
// ------------------------
fUpdateTime = gpGlobals->time;
fLastRunPlayerMoveTime = gpGlobals->time;
fCheckHostageStatusTimer = gpGlobals->time;
fButtonTime = gpGlobals->time;
fChatTime = gpGlobals->time + RANDOM_FLOAT(0.5, 5);
fMemoryTime = gpGlobals->time;
fDoRadio = gpGlobals->time;
float freezeTimeCVAR = CVAR_GET_FLOAT("mp_freezetime");
fNotStuckTime = gpGlobals->time + freezeTimeCVAR + 0.5f;
f_shoot_wait_time = gpGlobals->time;
f_goback_time = gpGlobals->time;
f_may_jump_time = gpGlobals->time;
f_defuse = gpGlobals->time;
f_allow_keypress = gpGlobals->time;
f_use_timer = gpGlobals->time;
f_light_time = gpGlobals->time;
f_sec_weapon = gpGlobals->time;
f_prim_weapon = gpGlobals->time;
f_gren_time = gpGlobals->time;
f_walk_time = gpGlobals->time;
f_hear_time = gpGlobals->time;
freezeTime = gpGlobals->time - 1;
f_cover_time = gpGlobals->time;
f_c4_time = gpGlobals->time;
f_update_weapon_time = gpGlobals->time;
f_follow_time = gpGlobals->time;
f_jump_time = 0.0;
f_hold_duck = gpGlobals->time - 1;
f_camp_time = gpGlobals->time;
f_wait_time = gpGlobals->time;
f_bot_see_enemy_time = gpGlobals->time;
f_bot_find_enemy_time = gpGlobals->time;
f_shoot_time = gpGlobals->time;
fMoveToNodeTime = -1;
nodeTimeIncreasedAmount = 0;
distanceMovedTimer = gpGlobals->time;
distanceMoved = 0;
fBlindedTime = gpGlobals->time;
f_console_timer = gpGlobals->time + RANDOM_FLOAT(0.1, 0.9);
fWanderTime = gpGlobals->time;
f_strafe_time = gpGlobals->time;
// ------------------------
// POINTERS
// ------------------------
pButtonEdict = NULL;
pBotHostage = NULL;
clearHostages();
pEnemyEdict = NULL;
// ------------------------
// INTEGERS
// ------------------------
i_camp_style = 0;
iPrimaryWeapon = -1;
iSecondaryWeapon = -1;
zoomed = ZOOM_NONE;
bot_health = 0;
prev_health = 0;
bot_armor = 0;
// bot_weapons = 0; // <- stefan: prevent from buying new stuff every round!
console_nr = 0;
pathIndex = -1;
iGoalNode = -1;
goalIndex = -1;
iPreviousGoalNode = -1;
iCloseNode = -1;
// Smarter Stuck stuff
iDuckTries = 0;
iJumpTries = 0;
if (RANDOM_LONG(0, 100) < ipFearRate)
iPathFlags = PATH_DANGER;
else
iPathFlags = PATH_NONE;
// ------------------------
// BOOLEANS
// ------------------------
// chat
memset(chChatSentence, 0, sizeof(chChatSentence));
vip = UTIL_IsVip(pEdict);
// Every round consider
bWalkKnife = false;
if (RANDOM_LONG(0, 100) < ipWalkWithKnife)
bWalkKnife = true;
// Buying
buy_ammo_primary = true;
buy_ammo_secondary = true;
buy_primary = (Game.bPistols ? false : true);
buy_grenade = false;
buy_smokegrenade = false;
buy_flashbang = 0;
buy_secondary = (Game.bPistols ? true : false);
buy_armor = false;
buy_defusekit = false;
if (UTIL_GetTeam(pEdict) == 1)
if (RANDOM_LONG(0, 100) < ipBuyDefuseKit)
buy_defusekit = true;
if (RANDOM_LONG(0, 100) < ipBuyArmour)
buy_armor = true;
if (RANDOM_LONG(0, 100) < ipBuyGrenade)
buy_grenade = true;
if (RANDOM_LONG(0, 100) < ipBuySmokeGren)
buy_smokegrenade = true;
if (RANDOM_LONG(0, 100) < ipBuyFlashBang)
buy_flashbang = 2;
bFirstOutOfSight = false;
f_strafe_speed = 0.0;
// ------------------------
// VECTORS
// ------------------------
prevOrigin = Vector(9999.0, 9999.0, 9999.0);
lastSeenEnemyVector = Vector(0, 0, 0);
vEar = Vector(9999, 9999, 9999);
// ------------------------
// CHAR
// ------------------------
arg1[0] = 0;
arg2[0] = 0;
arg3[0] = 0;
// initalize a few other stuff
NodeMachine.path_clear(iBotIndex);
iPathFlags = PATH_NONE;
played_rounds++;
// hello dudes
if (played_rounds == 1) {
// do some chatting
if (RANDOM_LONG(0, 100) < (ipChatRate + 10)) {
// we should say something now?
int iMax = -1;
for (int tc = 0; tc < 50; tc++) {
if (ChatEngine.ReplyBlock[98].sentence[tc][0] != '\0')
iMax++;
}
int the_c = RANDOM_LONG(0, iMax);
if (the_c > -1 && iMax > -1) {
char chSentence[80];
memset(chSentence, 0, sizeof(chSentence));
sprintf(chSentence, "%s ",
ChatEngine.ReplyBlock[98].sentence[the_c]);
PrepareChat(chSentence);
}
}
}
clearHostages();
clearHostageToRescueTarget();
rprint("NewRound", "Initialization new round finished");
}
/******************************************************************************
Function purpose: Returns a random chat sentence and stores it into 'sentence'
******************************************************************************/
void cBot::PrepareChat(char sentence[128]) {
if (Game.iProducedSentences <= Game.iMaxSentences) {
// makes bot chat away
fChatTime = gpGlobals->time + RANDOM_FLOAT(0.1, 2.0);
strcpy(chChatSentence, sentence); // copy this
Game.iProducedSentences++;
}
}
/******************************************************************************
Function purpose: Return reaction time based upon skill
******************************************************************************/
float cBot::ReactionTime(int iSkill) {
float time = RANDOM_FLOAT(fpMinReactTime, fpMaxReactTime);
if (Game.messageVerbosity > 1) {
char msg[255];
sprintf(msg, "minReactTime %f, maxReactTime %f, skill %d, results into %f", fpMinReactTime, fpMaxReactTime, iSkill, time);
rprint_trace("ReactionTime()", msg);
}
return time;
}
/******************************************************************************
Function purpose: Finds a (new) enemy
******************************************************************************/
int cBot::FindEnemy() {
// When on ladder, do not search for enemies
if (isOnLadder())
return -1;
// When blinded we cannot search for enemies
if (fBlindedTime > gpGlobals->time)
return -1;
float fNearestDistance = 9999; // Nearest distance
edict_t *pNewEnemy = NULL; // New enemy found
// SEARCH PLAYERS FOR ENEMIES
for (int i = 1; i <= gpGlobals->maxClients; i++) {
edict_t *pPlayer = INDEXENT(i);
// skip invalid players and skip self (i.e. this bot)
if ((pPlayer) && (!pPlayer->free) && (pPlayer != pEdict)) {
// skip this player if not alive (i.e. dead or dying)
if (!IsAlive(pPlayer))
continue;
Vector vVecEnd = pPlayer->v.origin + pPlayer->v.view_ofs;
// if bot can see the player...
if (FInViewCone(&vVecEnd, pEdict) && FVisible(vVecEnd, pEdict)) {
int player_team = UTIL_GetTeam(pPlayer);
int bot_team = UTIL_GetTeam(pEdict);
if (player_team == bot_team)
continue; // do not target teammates
// Its not a friend, track enemy
float fDistance =
(pPlayer->v.origin - pEdict->v.origin).Length();
bool bCanSee = true;
// The further away, the less chance we see this enemy
//if (RANDOM_FLOAT(0,1.0) < (fDistance/4096))
// bCanSee=false;
if (CarryWeaponType() == SNIPER)
bCanSee = true;
if (fDistance < fNearestDistance && bCanSee) {
fNearestDistance = fDistance;
pNewEnemy = pPlayer;
}
continue;
}
} // valid player
} // FOR
// We found a new enemy & the new enemy is different then previous pointer
if (pNewEnemy && pNewEnemy != pEnemyEdict) {
int iCurrentNode = determineCurrentNode();
// Add 'contact' data
if (iCurrentNode > -1) {
NodeMachine.contact(iCurrentNode, UTIL_GetTeam(pEdict));
}
// We have a reaction time to this new enemy
rememberEnemyFound();
f_shoot_time = gpGlobals->time + ReactionTime(bot_skill);
pEnemyEdict = pNewEnemy; // Update pointer
// We did not have an enemy before
if (pEnemyEdict == NULL) {
rprint_trace("FindEnemy()", "Found new enemy");
// RADIO: When we found a NEW enemy but NOT via a friend
if (FUNC_DoRadio(this)) {
UTIL_BotRadioMessage(this, 3, "2", "");
}
// We found a new enemy
return 0;
} else {
// we found an enemy that is newer/more dangerous then previous
rprint_trace("FindEnemy()", "Found 'newer' enemy");
return 3;
}
}
// nothing found
return -1; // return result
}
void cBot::rememberEnemyFound() {
f_bot_find_enemy_time = gpGlobals->time + REMEMBER_ENEMY_TIME;
}
/******************************************************************************
Function purpose: Sets vHead to aim at vector
******************************************************************************/
void cBot::setHeadAiming(Vector vTarget) {
vHead = vTarget;
}
/**
* Returns true / false wether enemy is alive.
* @return
*/
bool cBot::isEnemyAlive() {
return IsAlive(pEnemyEdict);
}
bool cBot::isSeeingEnemy() {
if (!hasEnemy()) {
this->rprint("canSeeEnemy called without having enemy?");
return false;
}
if (isBlindedByFlashbang()) {
return false;
}
Vector vBody = pEnemyEdict->v.origin;
Vector vHead = pEnemyEdict->v.origin + pEnemyEdict->v.view_ofs;
bool bodyInFOV = FInViewCone(&vBody, pEdict) && FVisible(vBody, pEdict);
bool headInFOV = FInViewCone(&vHead, pEdict) && FVisible(vHead, pEdict);
if (bodyInFOV || headInFOV) {
return true;
}
return false;
}
/******************************************************************************
Function purpose: Aims at enemy, only when valid. Based upon skill how it 'aims'
******************************************************************************/
void cBot::AimAtEnemy() {
if (!hasEnemy())
return;
// We cannot see our enemy? -> bail out
if (isSeeingEnemy()) {
setHeadAiming(lastSeenEnemyVector); // look at last known vector of enemy
return;
}
// ------------------------ we can see enemy -------------------------
float fDistance;
// Distance to enemy
fDistance = (pEnemyEdict->v.origin - pEdict->v.origin).Length() + 1; // +1 to make sure we never divide by zero
// factor in distance, the further away the more deviation - which is based on skill
int skillReversed = (10 - bot_skill) + 1;
float fScale = 0.5 + (fDistance / (64 *
skillReversed)); // a good skilled bot is less impacted by distance than a bad skilled bot
if (CarryWeaponType() == SNIPER) fScale *= 0.80; // sniping improves aiming
// Set target here
Vector vTarget;
if (bot_skill <= 1)
vTarget = pEnemyEdict->v.origin + pEnemyEdict->v.view_ofs * RANDOM_FLOAT(-0.5, 1.1); // aim for the head
else if (bot_skill > 1 && bot_skill < 4)
vTarget = pEnemyEdict->v.origin +
pEnemyEdict->v.view_ofs * RANDOM_FLOAT(-2.5, 2.5); // aim for the head more fuzzy
else
vTarget = pEnemyEdict->v.origin; // aim for body
// Based upon how far, we make this fuzzy
float fDx, fDy, fDz;
fDx = fDy = fDz = ((bot_skill + 1) * fScale);
// Example 1:
// Super skilled bot (bot_skill 1), with enemy of 2048 units away. Results into:
// skillReversed = (10 - 0 + 1) == 11
// fScale = 2048 / (128 * 11) -> 2048 / 1408 => 1.454545
// fd* = 0.5 + 1 * 1,95
// Example 2, less skilled bot (skill = 3) same enemy
// skillReversed = (10 - 3 + 1) == 8
// fScale = 2048 / (128 * 8) -> 2048 / 1024 => 2
// fd* = 3 * 2
vTarget = vTarget + Vector(
RANDOM_FLOAT(-fDx, fDx),
RANDOM_FLOAT(-fDy, fDy),
RANDOM_FLOAT(-fDz, fDz)
);
// Add Offset
fDx = fpXOffset;
fDy = fpYOffset;
fDz = fpZOffset;
// increase offset with personality x,y,z offsets randomly
vTarget = vTarget + Vector(
RANDOM_FLOAT(-fDx, fDx),
RANDOM_FLOAT(-fDy, fDy),
RANDOM_FLOAT(-fDz, fDz)
);
if (isHoldingGrenadeOrFlashbang()) {
// aim a bit higher
vTarget = vTarget + Vector(0, 0, 50);
}
setHeadAiming(vTarget);
}
bool cBot::isBlindedByFlashbang() const {
return fBlindedTime > gpGlobals->time;
}
bool cBot::isHoldingGrenadeOrFlashbang() const {
return current_weapon.iId == CS_WEAPON_HEGRENADE || current_weapon.iId == CS_WEAPON_FLASHBANG;
}
/******************************************************************************
Function purpose: Perform fighting actions
******************************************************************************/
void cBot::FightEnemy() {
// We can see our enemy
if (!isBlindedByFlashbang() && isSeeingEnemy()) {
// GET OUT OF CAMP MODE
if (f_camp_time > gpGlobals->time) {
f_camp_time = gpGlobals->time;
}
// Next time our enemy gets out of sight, it will be the 'first' time
// of all 'frame times'.
bFirstOutOfSight = false;
// Remember last seen enemy position
lastSeenEnemyVector = pEnemyEdict->v.origin; // last seen enemy position
// FIXME: Fix the darn zoom bug
// zoom in with sniper gun
if (CarryWeaponType() == SNIPER) {
if (zoomed < ZOOM_TWICE && f_allow_keypress < gpGlobals->time) {
UTIL_BotPressKey(this, IN_ATTACK2);
f_allow_keypress = gpGlobals->time + 0.7;
zoomed++;
if (zoomed > ZOOM_TWICE)
zoomed = ZOOM_NONE;
}
} else if (FUNC_BotHoldsZoomWeapon(this)) {
if (zoomed < ZOOM_ONCE && f_allow_keypress < gpGlobals->time) {
UTIL_BotPressKey(this, IN_ATTACK2);
f_allow_keypress = gpGlobals->time + 0.7;
zoomed++;
}
}
// NOT blinded by flashbang, try to find cover?
if (f_cover_time < gpGlobals->time) {
// COVER: Not taking cover now, fight using fightstyles.
// when vip, we always take cover.
if (vip) {
// Camp, take cover, etc.
BOT_DecideTakeCover(this);
if (FUNC_DoRadio(this)) {
UTIL_BotRadioMessage(this, 3, "3", ""); // need backup
}
} else {
// DECIDE: Should we take cover or not.
if (FUNC_ShouldTakeCover(this)) {
FindCover();
}
}
} else {
}
// Keep timer updated for enemy
f_bot_find_enemy_time = gpGlobals->time + REMEMBER_ENEMY_TIME;
}
else // ---- CANNOT SEE ENEMY
{
if (f_bot_find_enemy_time < gpGlobals->time) {
pEnemyEdict = NULL;
lastSeenEnemyVector = Vector(0, 0, 0);
rprint_trace("FightEnemy()", "Lost enemy out of sight, forgetting path and goal");
forgetPath();
forgetGoal();
} else {
// When we have the enemy for the first time out of sight
// we calculate a path to the last seen position
if (!bFirstOutOfSight) {
rprint_trace("FightEnemy()", "Enemy out of sight, calculating path towards it.");
// Only change path when we update our information here
int iGoal = NodeMachine.getClosestNode(lastSeenEnemyVector, NODE_ZONE, pEdict);
if (iGoal > -1) {
setGoalNode(iGoal);
forgetPath();
}
bFirstOutOfSight = true;
} else {
if (!hasGoal()) {
rprint("Enemy out of sight and no goal, forgetting enemy");
forgetEnemy();
}
}
}
} // visible
}
void cBot::pickWeapon(int weaponId) {
UTIL_SelectItem(pEdict, UTIL_GiveWeaponName(weaponId));
f_c4_time = gpGlobals->time - 1; // reset C4 timer data
// give Counter-Strike time to switch weapon (animation, update state, etc)
f_update_weapon_time = gpGlobals->time + 0.7;
}
bool cBot::ownsFavoritePrimaryWeapon() {
return hasFavoritePrimaryWeaponPreference() && isOwningWeapon(ipFavoPriWeapon);
}
bool cBot::ownsFavoriteSecondaryWeapon() {
return hasFavoriteSecondaryWeaponPreference() && isOwningWeapon(ipFavoSecWeapon);
}
/**
* Returns true if bot has weapon (id) in possession
* @param weaponId
* @return
*/
bool cBot::isOwningWeapon(int weaponId) {
return bot_weapons & (1 << weaponId);
}
/**
* Returns true if bot carries weapon right now
* @param weaponId
* @return
*/
bool cBot::isHoldingWeapon(int weaponId) {
return (current_weapon.iId == weaponId);
}
bool cBot::hasFavoritePrimaryWeaponPreference() {
return ipFavoPriWeapon > -1;
}
bool cBot::hasFavoriteSecondaryWeaponPreference() {
return ipFavoSecWeapon > -1;
}
bool cBot::canAfford(int price) {
return this->bot_money > price;
}
/******************************************************************************
Function purpose: Based upon several events pick the best weapon
******************************************************************************/
void cBot::PickBestWeapon() {
// does Timer allow to change weapon? (only when f_update_weapon_time < gpGlobals->time
if (f_update_weapon_time > gpGlobals->time)
return;
// Distance to enemy
float fDistance = func_distance(pEdict->v.origin, lastSeenEnemyVector);
float knifeDistance = 300;
// ----------------------------
// In this function all we do is decide what weapon to pick
// if we don't pick another weapon the current weapon is okay
// ----------------------------
// First we handle situations which are bad, no matter the distance
// or any other circumstance.
// BAD: Carrying C4 or knife
if (CarryWeapon(CS_WEAPON_C4) || // carrying C4
(CarryWeapon(CS_WEAPON_KNIFE) && fDistance > knifeDistance)) { // carrying knife and too far
if (hasPrimaryWeaponEquiped()) {
pickWeapon(iPrimaryWeapon);
return;
} else if (hasSecondaryWeaponEquiped()) {
pickWeapon(iSecondaryWeapon);
return;
}
}
// At this point we do not update weapon information. And we did not 'switch back' to primary / secondary
if (hasEnemy() && !isSeeingEnemy()) {
// decision to pull HE grenade
if (isOwningWeapon(CS_WEAPON_HEGRENADE) && // we have a grenade
func_distance(pEdict->v.origin, lastSeenEnemyVector) < 900 && // we are close
func_distance(pEdict->v.origin, lastSeenEnemyVector) > 200 && // but not to close
RANDOM_LONG(0, 100) < 10 && // only randomly we pick a grenade in the heat of the battle
current_weapon.iId != CS_WEAPON_HEGRENADE && current_weapon.iId != CS_WEAPON_FLASHBANG &&
f_gren_time + 15 < gpGlobals->time) // and dont hold it yet
{
UTIL_SelectItem(pEdict, "weapon_hegrenade"); // select grenade
f_wait_time = gpGlobals->time + 1; // wait 1 second (stand still 1 sec)
f_gren_time =
gpGlobals->time + (1.0 + RANDOM_FLOAT(0.5, 1.5)); // and determine how long we should hold it
zoomed = ZOOM_NONE; // Counter-Strike resets zooming when choosing another weapon
return;
}
// OR we pull a flashbang?
if (isOwningWeapon(CS_WEAPON_FLASHBANG) && // we have a grenade
func_distance(pEdict->v.origin, lastSeenEnemyVector) < 200 && // we are close
func_distance(pEdict->v.origin, lastSeenEnemyVector) > 300 && // but not to close
RANDOM_LONG(0, 100) < 15 && // only randomly we pick a grenade in the heat of the battle
current_weapon.iId != CS_WEAPON_FLASHBANG && current_weapon.iId != CS_WEAPON_HEGRENADE &&
f_gren_time + 15 < gpGlobals->time) // and dont hold it yet
{
UTIL_SelectItem(pEdict, "weapon_flashbang"); // select grenade
f_wait_time = gpGlobals->time + 1; // wait 1 second (stand still 1 sec)
f_gren_time =
gpGlobals->time + (1.0 + RANDOM_FLOAT(0.5, 1.5)); // and determine how long we should hold it
zoomed = ZOOM_NONE; // Counter-Strike resets zooming when choosing another weapon
return;
}
}
// When we are here, we did not decide to switch to grenade/flashbang. Now we look
// if the bot has to reload or switch weapon based upon ammo.
// ----------------------------------------
// More complex bad things that can happen:
// ----------------------------------------
int iTotalAmmo = current_weapon.iAmmo1;
int iCurrentAmmo = current_weapon.iClip;
//char msg[80];
//sprintf(msg, "BOT: ICLIP %d, TOTALAMMO %d\n", iCurrentAmmo, iTotalAmmo);
// Clip is out of ammo
if (iCurrentAmmo < 1
&& (CarryWeaponType() == PRIMARY || CarryWeaponType() == SECONDARY)) {
// Camp, take cover, etc.
BOT_DecideTakeCover(this);
// We still have ammo!
if (iTotalAmmo > 0) {
UTIL_BotPressKey(this, IN_RELOAD);
f_update_weapon_time = gpGlobals->time + 0.7; // update timer
return;
} else {
// Thanks to dstruct2k for easy ctrl-c/v, i optimized the code
// a bit though. Btw, distance 600 is too far for slashing :)
// at here the bot does not have ammo of the current weapon, so
// switch to another weapon.
if (iPrimaryWeapon > -1 && // we have a primary
current_weapon.iId != iPrimaryWeapon && // that's not the current, empty gun
func_distance(pEdict->v.origin, lastSeenEnemyVector) > 300) // and we are not close enough to knife
{
// select primary weapon
UTIL_SelectItem(pEdict, UTIL_GiveWeaponName(iPrimaryWeapon)); // select the primary
return;
} else {
if (iSecondaryWeapon > -1 && current_weapon.iId != iSecondaryWeapon &&
// that's not the current, empty gun
func_distance(pEdict->v.origin, lastSeenEnemyVector) > 300) // and we are not close enough to knife
{
UTIL_SelectItem(pEdict, UTIL_GiveWeaponName(iSecondaryWeapon)); // select the secondary
return;
} else {
if (isOwningWeapon(CS_WEAPON_KNIFE) && // we have a knife (for non-knife maps)
!isHoldingWeapon(CS_WEAPON_KNIFE)) // but we do not carry it
{
UTIL_SelectItem(pEdict, "weapon_knife");
return;
}
}
} // end if
} // no ammo
}
}
/******************************************************************************
Function purpose: Fire weapon (burst; or do not fire when not allowed)
******************************************************************************/
void cBot::FireWeapon() {
// We may not shoot!
if (f_shoot_time > gpGlobals->time ||
f_update_weapon_time > gpGlobals->time)
return;
if (!isSeeingEnemy()) {
return;
}
// ------------------------------------------------------------
float fDistance = 50;