-
Notifications
You must be signed in to change notification settings - Fork 5
/
bot_combat.cpp
2626 lines (2302 loc) · 87 KB
/
bot_combat.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
// ####################################
// # #
// # Ping of Death - Bot #
// # by #
// # Markus Klinge aka Count Floyd #
// # #
// ####################################
//
// Started from the HPB-Bot Alpha Source
// by Botman so Credits for a lot of the basic
// HL Server/Client Stuff goes to him
//
// bot_combat.cpp
//
// Does Enemy Sensing (spotting), combat movement and firing weapons
#include "bot_globals.h"
#ifdef __linux__
#define strncpy_s strncpy
#endif
int NumTeammatesNearPos(bot_t* pBot, Vector vecPosition, int iRadius)
{
int iCount = 0;
float fDistance;
edict_t* pEdict = pBot->pEdict;
int i;
Vector vPlayerOrigin; // KWo - 19.10.2008
if (g_b_cv_ffa) // KWo - 05.10.2006
return 0;
for (i = 0; i < gpGlobals->maxClients; i++)
{
if (!(clients[i].iFlags & CLIENT_USED)
|| !(clients[i].iFlags & CLIENT_ALIVE)
|| (clients[i].iTeam != pBot->bot_team || g_b_cv_ffa) // KWo - 05.10.2006
|| clients[i].pEdict == pEdict)
continue;
fDistance = (clients[i].vOrigin - vecPosition).Length();
if (fDistance < iRadius)
{
vPlayerOrigin = clients[i].pEdict->v.origin; // KWo - 19.10.2008
if (FVisible(vPlayerOrigin, pBot->pEdict)) // KWo - 19.10.2008
iCount++;
}
}
return iCount;
}
int NumEnemiesNearPos(bot_t* pBot, Vector vecPosition, int iRadius)
{
int iCount = 0;
float fDistance;
int i;
Vector vPlayerOrigin; // KWo - 19.10.2008
for (i = 0; i < gpGlobals->maxClients; i++)
{
if (!(clients[i].iFlags & CLIENT_USED)
|| !(clients[i].iFlags & CLIENT_ALIVE)
|| clients[i].iTeam == pBot->bot_team && !g_b_cv_ffa) // KWo - 05.10.2006
continue;
fDistance = (clients[i].vOrigin - vecPosition).Length();
if (fDistance < iRadius)
{
vPlayerOrigin = clients[i].pEdict->v.origin; // KWo - 19.10.2008
if (FVisible(vPlayerOrigin, pBot->pEdict)) // KWo - 19.10.2008
iCount++;
}
}
return iCount;
}
bool BotEnemyIsVisible(bot_t* pBot, edict_t* pEnemy) // KWo - 27.01.2008
{
TraceResult tr;
int RenderFx; // KWo - 22.03.2008
int RenderMode; // KWo - 22.03.2008
int EntEnemyIndex; // KWo - 17.01.2011
int EnemyWeapon; // KWo - 17.01.2011
Vector RenderColor; // KWo - 22.03.2008
float RenderAmount; // KWo - 22.03.2008
float LightLevel; // KWo - 23.03.2008
bool SemiTransparent = false; // KWo - 22.03.2008
bool EnemyWeaponIsGun = false;// KWo - 17.01.2011
if (FNullEnt(pEnemy))
return false;
if (!FStrEq(STRING(pEnemy->v.classname), "player"))
return false;
// Can't see the target entity if blinded or smoked...
if ((pBot->pEdict->v.origin - pEnemy->v.origin).Length() > pBot->f_view_distance) // KWo - 14.09.2008
return false;
// KWo - 22.03.2008 - added invisibility check
RenderFx = pEnemy->v.renderfx;
RenderMode = pEnemy->v.rendermode;
RenderColor = pEnemy->v.rendercolor;
RenderAmount = pEnemy->v.renderamt;
EntEnemyIndex = ENTINDEX(pEnemy) - 1; // 17.01.2011
if (EntEnemyIndex >= 0 && EntEnemyIndex < gpGlobals->maxClients) // 17.01.2011
{
EnemyWeapon = clients[EntEnemyIndex].iCurrentWeaponId; // 17.01.2011
EnemyWeaponIsGun = WeaponIsPistol(EnemyWeapon) || WeaponIsPrimaryGun(EnemyWeapon); // 17.01.2011
}
if ((RenderFx == kRenderFxExplode || pEnemy->v.effects & EF_NODRAW) // 17.01.2011
&& (!(pEnemy->v.oldbuttons & IN_ATTACK) || !EnemyWeaponIsGun)) // kRenderFxExplode is always invisible even for mode kRenderNormal
return false;
else if ((RenderFx == kRenderFxExplode || pEnemy->v.effects & EF_NODRAW)
&& pEnemy->v.oldbuttons & IN_ATTACK && EnemyWeaponIsGun)
SemiTransparent = true;
else if (RenderFx != kRenderFxHologram && RenderFx != kRenderFxExplode
&& RenderMode != kRenderNormal) // kRenderFxHologram is always visible no matter what is the mode
{
if (RenderFx == kRenderFxGlowShell)
{
if (RenderAmount <= 20.0f && RenderColor.x <= 20
&& RenderColor.y <= 20 && RenderColor.z <= 20)
{
if (!(pEnemy->v.oldbuttons & IN_ATTACK) || !EnemyWeaponIsGun)
{
return false;
}
else
{
SemiTransparent = true;
}
}
else if (RenderAmount <= 60.0f && RenderColor.x <= 60
&& RenderColor.y <= 60 && RenderColor.z <= 60)
SemiTransparent = true;
}
else
{
if (RenderAmount <= 20)
{
if (!(pEnemy->v.oldbuttons & IN_ATTACK) || !EnemyWeaponIsGun)
{
return false;
}
else
{
SemiTransparent = true;
}
}
else if (RenderAmount <= 60)
SemiTransparent = true;
}
}
// KWo - 23.03.2008 - added darkness check
LightLevel = UTIL_IlluminationOf(pEnemy);
if (!pBot->bUsesNVG && (LightLevel < 3.0f && g_f_cv_skycolor > 50.0f || LightLevel < 25.0f && g_f_cv_skycolor <= 50.0f)
&& !(pEnemy->v.effects & EF_DIMLIGHT) /* && (!g_bIsOldCS15) */ && (!(pEnemy->v.oldbuttons & IN_ATTACK) || !EnemyWeaponIsGun)) // 17.01.2011
{
return false;
}
else if ((LightLevel < 10.0f && g_f_cv_skycolor > 50.0f || LightLevel < 30.0f && g_f_cv_skycolor <= 50.0f
|| pEnemy->v.oldbuttons & IN_ATTACK && EnemyWeaponIsGun)
&& !pBot->bUsesNVG && !(pEnemy->v.effects & EF_DIMLIGHT))
{
SemiTransparent = true; // in this case we can notice the enemy, but not so good...
}
// trace a line from bot's eyes to the destination...
TRACE_LINE(GetGunPosition(pBot->pEdict), GetGunPosition(pEnemy), dont_ignore_monsters, pBot->pEdict, &tr);
if (tr.flFraction <= 1.0f && tr.pHit == pEnemy && (pBot->bUsesNVG || !SemiTransparent)) // KWo - 22.03.2008
return true;
TRACE_LINE(GetGunPosition(pBot->pEdict), pEnemy->v.origin, dont_ignore_monsters, pBot->pEdict, &tr);
if (tr.flFraction <= 1.0f && tr.pHit == pEnemy) // KWo - 24.02.2008
return true;
return false;
}
bool BotFindEnemy(bot_t* pBot)
{
// Returns true if an Enemy can be seen
// FIXME: Bot should lock onto the best shoot position for
// a target instead of going through all of them everytime
static Vector vecEnd;
static float distance; // KWo - 22.10.2006
static float distance2; // KWo - 22.10.2006
static float FrDistLastEn; // KWo - 08.04.2010
static float nearestdistance;
static edict_t* pNewEnemy;
static edict_t* pNewEnemy2;
static edict_t* pPlayer;
static edict_t* pEdict;
static bot_t* pFriendlyBot;
static Vector vecVisible; // KWo - 10.10.2006
static Vector vecVisible2; // KWo - 10.10.2006
static Vector vecVisible3; // KWo - 08.01.2012
static unsigned char cHit; // KWo - 10.10.2006
static unsigned char cHit2; // KWo - 10.10.2006
static unsigned char cHit3; // KWo - 08.01.2012
static char szBotEnemyModelName[64]; // KWo - 12.08.2007
static int i, j;
static int iEnemyIndex;
static int iShootThruFreq;
static Vector vecRandom; // KWo - 27.01.2008
static Vector vecOrg;
// We're blind and can't see anything !!
// if (pBot->f_blind_time > gpGlobals->time) // Moved to BotSetConditions function - KWo 17.06.2018
// return (FALSE);
if (!FNullEnt(pBot->pBotEnemy) && pBot->iStates & STATE_SEEINGENEMY
&& g_i_botthink_index % 4 != g_iFrameCounter % 4) // KWo - 30.05.2010
{
pBot->iAimFlags |= AIM_ENEMY;
pBot->iStates &= ~STATE_SUSPECTENEMY; // KWo - 09.02.2007
pBot->bShootThruHeard = FALSE; // KWo - 10.07.2008
pBot->bShootThruSeen = FALSE; // KWo - 10.07.2008
return true;
}
else if (FNullEnt(pBot->pBotEnemy) && pBot->f_bot_see_enemy_time + 5.0f > gpGlobals->time) // KWo - 21.01.2008
{
pBot->iAimFlags |= AIM_LASTENEMY;
pBot->iStates |= STATE_SUSPECTENEMY;
}
pEdict = pBot->pEdict;
distance = 0.0f;
distance2 = 0.0f;
nearestdistance = pBot->f_view_distance;
pNewEnemy = NULL;
pNewEnemy2 = NULL;
vecVisible = g_vecZero;
vecVisible2 = g_vecZero;
vecVisible3 = g_vecZero; // KWo - 08.01.2012
cHit = 0;
cHit2 = 0;
cHit3 = 0; // KWo - 08.01.2012
iEnemyIndex = -1;
pBot->ucVisibility = 0;
// Setup Potentially Visible Set for this Bot
vecOrg = GetGunPosition(pEdict);
if (pEdict->v.flags & FL_DUCKING)
vecOrg = vecOrg + (VEC_HULL_MIN - VEC_DUCK_HULL_MIN);
// unsigned char *pvs = ENGINE_SET_PVS ((float *) &vecOrg);
if (!FNullEnt(pBot->pBotEnemy)) // KWo - 10.10.2006
{
pPlayer = pBot->pBotEnemy;
iEnemyIndex = ENTINDEX(pPlayer) - 1; // KWo - 14.03.2010
if (iEnemyIndex >= 0 && iEnemyIndex < gpGlobals->maxClients)
{
if (IsAlive(pPlayer) || clients[iEnemyIndex].fDeathTime >= gpGlobals->time) // KWo 15.03.2010
{
if (pBot->bot_team != clients[iEnemyIndex].iTeam || g_b_cv_ffa
|| pBot->pBotEnemy == pEdict->v.dmg_inflictor) // KWo - 19.01.2011
{
vecEnd = GetGunPosition(pPlayer);
if (FInViewCone(&vecEnd, pEdict) && (pPlayer->v.origin - pEdict->v.origin).Length() <= nearestdistance) // KWo - 08.09.2009
{
if (FBoxVisible(pBot, pPlayer, &vecVisible, &cHit)) // KWo - 25.03,2007
{
// the old enemy is still visible
pNewEnemy = pPlayer;
vecRandom = Vector(RANDOM_FLOAT(-BotAimTab[pBot->bot_skill / 20].fAim_X, BotAimTab[pBot->bot_skill / 20].fAim_X),
RANDOM_FLOAT(-BotAimTab[pBot->bot_skill / 20].fAim_Y, BotAimTab[pBot->bot_skill / 20].fAim_Y),
RANDOM_FLOAT(-BotAimTab[pBot->bot_skill / 20].fAim_Z, BotAimTab[pBot->bot_skill / 20].fAim_Z)); // KWo - 27.01.2008
pBot->vecVisPos = vecVisible + vecRandom; // KWo - 25.01.2008
// pBot->vecEnemy = vecVisible;
distance = (pPlayer->v.origin - pEdict->v.origin).Length(); // KWo - 22.10.2006
pBot->ucVisibility = cHit;
}
}
}
}
}
}
// the old enemy is no longer visible or it was farer away than 400 units
if (FNullEnt(pNewEnemy)
|| distance > 400.0f && g_i_botthink_index % 4 == g_iFrameCounter % 4) // KWo - 11.01.2012
{
// search the world for enemies...
for (i = 0; i < gpGlobals->maxClients; i++)
{
if (!(clients[i].iFlags & CLIENT_USED)
|| !(clients[i].iFlags & CLIENT_ALIVE) && clients[i].fDeathTime <= gpGlobals->time // 15.03.2010
|| clients[i].iTeam == pBot->bot_team && !g_b_cv_ffa // KWo - 05.10.2006
|| clients[i].pEdict == pEdict)
continue;
pPlayer = clients[i].pEdict;
// Let the Engine check if this Player is potentially visible
vecEnd = GetGunPosition(pPlayer);
if (!FInViewCone(&vecEnd, pEdict))
continue;
distance2 = (pPlayer->v.origin - pEdict->v.origin).Length();
memset(szBotEnemyModelName, 0, sizeof szBotEnemyModelName);
#ifdef _WIN32
strncpy_s(szBotEnemyModelName, sizeof szBotEnemyModelName, INFOKEY_VALUE(GET_INFOKEYBUFFER(pPlayer), "model"), sizeof szBotEnemyModelName - 1); // KWo - 12.08.2007
#else
strncpy(szBotEnemyModelName, (INFOKEY_VALUE(GET_INFOKEYBUFFER(pPlayer), "model")), sizeof(szBotEnemyModelName)); // KWo - 12.08.2007
#endif
if ((distance2 >= nearestdistance
|| !FNullEnt(pNewEnemy) && distance > 0.0f && (distance2 >= 0.9f * distance
|| pBot->f_bot_see_new_enemy_time + 0.5f >= gpGlobals->time))
&& (!(g_iMapType & MAP_AS) || strcmp("vip", szBotEnemyModelName) != 0)) // KWo - 05.09.2009
continue;
if (!FBoxVisible(pBot, pPlayer, &vecVisible3, &cHit3)) // KWo - 08.01.2012
continue;
// if (!ENGINE_CHECK_VISIBILITY (pPlayer, pvs))
// continue;
if (distance2 < nearestdistance
&& (FNullEnt(pNewEnemy) || distance == 0.0f
|| distance2 < 0.9f * distance && pBot->f_bot_see_new_enemy_time + 0.5f < gpGlobals->time)
|| g_iMapType & MAP_AS && strcmp("vip", szBotEnemyModelName) == 0) // KWo - 29.04.2008
{
nearestdistance = distance2;
pNewEnemy2 = pPlayer;
cHit2 = cHit3; // KWo - 08.01.2012
vecVisible2 = vecVisible3; // KWo - 08.01.2012
pBot->f_bot_see_new_enemy_time = gpGlobals->time; // KWo - 29.04.2008
pBot->fChangeAimDirectionTime = gpGlobals->time + 1.0f;
iEnemyIndex = i; // KWo - 14.03.2010
// On Assassination Maps target VIP first !
if (g_iMapType & MAP_AS
&& strcmp("vip", szBotEnemyModelName) == 0) // Is VIP ? - KWo - 12.08.2007
break;
}
}
if ((FNullEnt(pNewEnemy) || distance > 400.0f) && !FNullEnt(pNewEnemy2))
{
pNewEnemy = pNewEnemy2;
pBot->ucVisibility = cHit2;
vecRandom = Vector(RANDOM_FLOAT(-BotAimTab[pBot->bot_skill / 20].fAim_X, BotAimTab[pBot->bot_skill / 20].fAim_X),
RANDOM_FLOAT(-BotAimTab[pBot->bot_skill / 20].fAim_Y, BotAimTab[pBot->bot_skill / 20].fAim_Y),
RANDOM_FLOAT(-BotAimTab[pBot->bot_skill / 20].fAim_Z, BotAimTab[pBot->bot_skill / 20].fAim_Z)); // KWo - 27.01.2008
pBot->vecLastEnemyOrigin = vecVisible2 + vecRandom; // KWo - 27.01.2008
pBot->vecEnemy = vecVisible2 + vecRandom; // KWo - 27.01.2008
pBot->vecVisPos = vecVisible2 + vecRandom; // KWo - 27.01.2008
vecVisible = vecVisible2;
}
/*
if (FNullEnt (pNewEnemy)) // KWo - 05.09.2009
pBot->fEnemyUpdateTime = gpGlobals->time + 0.1f;
*/
/*
else if (!(FNullEnt (pNewEnemy)) && (!FNullEnt (pNewEnemy2)) && (distance > nearestdistance + 50.0f))
{
if (GetShootingConeDeviation (pNewEnemy2, &pEdict->v.origin) >= 0.8f)
{
pNewEnemy = pNewEnemy2;
pBot->ucVisibility = cHit2;
pBot->vecEnemy = vecVisible2;
vecVisible = vecVisible2;
}
}
*/
}
if (pNewEnemy)
{
pBot->fEnemyUpdateTime = gpGlobals->time + 0.02f; // KWo - 05.09.2009 (not checked currently in the code)
pBot->fLastSeenEnOrgUpdateTime = gpGlobals->time + 1.0f; // KWo - 13.07.2008
g_bBotsCanPause = TRUE;
pBot->iAimFlags |= AIM_ENEMY;
// pBot->iAimFlags |= AIM_LASTENEMY; // KWo - 27.01.2008
pBot->iStates |= STATE_SEEINGENEMY; // KWo - 10.07.2008
pBot->iStates &= ~STATE_SUSPECTENEMY; // KWo - 09.02.2007
pBot->bShootThruHeard = FALSE; // KWo - 10.07.2008
pBot->bShootThruSeen = FALSE; // KWo - 10.07.2008
// Now alarm all Teammates who see this Bot &
// don't have an actual Enemy of the Bots Enemy
// Should simulate human players seeing a Teammate firing
// In this case he should take our enemy as his own suspected enemy...
if (pEdict->v.oldbuttons & IN_ATTACK && !g_b_cv_ffa) // KWo - 13.07.2008
{
for (j = 0; j < gpGlobals->maxClients; j++)
{
if (!bots[j].is_used
|| bots[j].bDead
|| bots[j].bot_team != pBot->bot_team
|| bots[j].pEdict == pEdict)
continue;
pFriendlyBot = &bots[j];
// pFriendlyBot = UTIL_GetBotPointer (clients[j].pEdict);
if (pFriendlyBot != NULL)
{
FrDistLastEn = 9999.0f;
if (pFriendlyBot->vecLastEnemyOrigin != g_vecZero) // KWo - 08.04.2010
FrDistLastEn = (pFriendlyBot->pEdict->v.origin - pFriendlyBot->vecLastEnemyOrigin).Length();
if (pFriendlyBot->f_bot_see_enemy_time + 4.0f < gpGlobals->time
&& (pFriendlyBot->f_heard_sound_time + 4.0f < gpGlobals->time
&& FNullEnt(pFriendlyBot->pLastEnemy)
|| FrDistLastEn < (pFriendlyBot->pEdict->v.origin - pBot->vecLastEnemyOrigin).Length())
&& FNullEnt(pFriendlyBot->pBotEnemy))// KWo - 08.04.2010
{
if (FInViewCone(&pEdict->v.origin, pFriendlyBot->pEdict))
{
if (FVisible(pEdict->v.origin, pFriendlyBot->pEdict))
{
pFriendlyBot->pLastEnemy = pNewEnemy;
pFriendlyBot->vecLastEnemyOrigin = pBot->vecLastEnemyOrigin;
pFriendlyBot->f_heard_sound_time = gpGlobals->time;
pFriendlyBot->iStates |= STATE_SUSPECTENEMY;
pFriendlyBot->iStates |= STATE_HEARINGENEMY;
pFriendlyBot->iAimFlags |= AIM_LASTENEMY;
pFriendlyBot->bShootThruHeard = FALSE;
pFriendlyBot->bShootThruSeen = FALSE;
}
}
}
}
}
}
// keep track of when we last saw an enemy
if (pNewEnemy == pBot->pBotEnemy)
{
// Zero out reaction time
pBot->f_actual_reaction_time = 0.0f;
pBot->pLastEnemy = pNewEnemy;
// pBot->vecLastEnemyOrigin = vecVisible; // KWo - 10.10.2006
// pBot->vecVisPos = vecVisible; // KWo - 25.01.2008 // KWo - 08.01.2012
pBot->pBotUser = NULL; // don't follow user when enemy found
pBot->f_bot_see_enemy_time = gpGlobals->time;
return true;
}
else
{
if (pBot->f_bot_see_enemy_time + 4.0f < gpGlobals->time
&& (pEdict->v.weapons & 1 << CS_WEAPON_C4
|| BotHasHostage(pBot) || !FNullEnt(pBot->pBotUser))
&& !g_b_cv_ffa && g_b_cv_radio && RANDOM_LONG(1, 100) < 20
&& (pBot->bot_team == TEAM_CS_COUNTER && g_iAliveCTs > 1
|| pBot->bot_team == TEAM_CS_TERRORIST && g_iAliveTs > 1)) // KWo - 06.03.2010
BotPlayRadioMessage(pBot, RADIO_ENEMYSPOTTED);
pBot->f_enemy_surprise_time = gpGlobals->time + pBot->f_actual_reaction_time;
pBot->f_bot_see_enemy_time = gpGlobals->time;
// UTIL_ServerPrint("[DEBUG] Bot %s got surprised by a new enemy.\n", pBot->name);
// Zero out reaction time
pBot->f_actual_reaction_time = 0.0f;
pBot->pBotEnemy = pNewEnemy;
pBot->pLastEnemy = pNewEnemy;
vecRandom = Vector(RANDOM_FLOAT(-BotAimTab[pBot->bot_skill / 20].fAim_X, BotAimTab[pBot->bot_skill / 20].fAim_X),
RANDOM_FLOAT(-BotAimTab[pBot->bot_skill / 20].fAim_Y, BotAimTab[pBot->bot_skill / 20].fAim_Y),
RANDOM_FLOAT(-BotAimTab[pBot->bot_skill / 20].fAim_Z, BotAimTab[pBot->bot_skill / 20].fAim_Z)); // KWo - 27.01.2008
pBot->vecLastEnemyOrigin = vecVisible + vecRandom; // KWo - 27.01.2008
pBot->vecEnemy = vecVisible + vecRandom; // KWo - 27.01.2008
pBot->vecVisPos = vecVisible + vecRandom; // KWo - 27.01.2008
pBot->fEnemyReachableTimer = 0.0f;
pBot->pBotUser = NULL; // don't follow user when enemy found
return true;
}
}
else if (!FNullEnt(pBot->pBotEnemy))
{
pNewEnemy = pBot->pBotEnemy;
pBot->pLastEnemy = pNewEnemy;
iEnemyIndex = ENTINDEX(pNewEnemy) - 1; // KWo - 14.03.2010
if (iEnemyIndex >= 0 && iEnemyIndex < gpGlobals->maxClients)
{
if (!IsAlive(pNewEnemy) && clients[iEnemyIndex].fDeathTime <= gpGlobals->time
|| IsAlive(pNewEnemy) && clients[iEnemyIndex].fDeathTime > gpGlobals->time
|| pEdict->v.dmgtime + 1.5f <= gpGlobals->time && pEdict->v.dmg_inflictor == pBot->pBotEnemy
&& clients[iEnemyIndex].iTeam == pBot->bot_team && !g_b_cv_ffa) // KWo - 18.01.2011
{
pBot->iStates &= ~STATE_SEEINGENEMY; // KWo - 10.07.2008
pBot->pBotEnemy = NULL;
return false;
}
}
// If no Enemy visible check if last one shootable thru Wall
// KWo - 17.06.2018 - uncommented back the check below...
if (pBot->fShootThruSeenCheckTime < gpGlobals->time) // KWo - 23.03.2008
{
iShootThruFreq = BotAimTab[pBot->bot_skill / 20].iSeenShootThruProb;
if (g_b_cv_shootthruwalls && RANDOM_LONG(1, 100) <= iShootThruFreq
&& WeaponShootsThru(pBot->current_weapon.iId)
&& pBot->f_bot_see_enemy_time + 0.6f > gpGlobals->time) // KWo - 08.01.2012
{
pBot->fShootThruSeenCheckTime = gpGlobals->time + 0.2f; // KWo - 23.03.2008
if (vecVisible == g_vecZero) // KWo - 17.06.2018
{
if (IsShootableThruObstacle(pEdict, pNewEnemy->v.origin)) // KWo - 23.03.2008
pBot->bShootThruSeen = TRUE;
else
pBot->bShootThruSeen = FALSE;
}
else
pBot->bShootThruSeen = FALSE;
}
else
{
pBot->bShootThruSeen = FALSE;
}
}
if (pBot->bShootThruSeen) // KWo - 05.05.2007
{
pBot->iStates |= STATE_SUSPECTENEMY; // KWo - 02.02.2007
pBot->iAimFlags |= AIM_LASTENEMY;
pBot->iAimFlags |= AIM_ENEMY; // KWo - 10.07.2008 - very important for work this function!!!
pBot->pLastEnemy = pNewEnemy;
return true;
}
return false;
}
return false;
}
Vector BotBodyTarget(edict_t* pBotEnemy, bot_t* pBot)
{
// Returns the aiming Vector for an Enemy
static int iWeapId;
static int iEnemyIndex; // KWo - 18.01.2011
static Vector target;
static unsigned char ucVis;
static edict_t* pEdict;
static float fDistance;
static Vector vecVel;
static Vector vecRandom;
static bool bBotUsesSniper; // KWo - 14.10.2006
static bool bBotUsesPistol; // KWo - 23.10.2006
static bool bBotUsesAssaultSniper; // KWo - 09.02.2008
static bool bBotUsesSubmachine; // KWo - 09.02.2008
static bool bBotUsesRifle; // KWo - 09.02.2008
static bool bEnemyTeamnate; // KWo - 18.01.2011
static float fRandomFactor;
if (pBot->f_blind_time > gpGlobals->time) // KWo - 04.10.2009
return pBot->vecLastEnemyOrigin;
if (pBot->fEnemyOriginUpdateTime < gpGlobals->time) // KWo - 04.10.2009
{
vecRandom = Vector(RANDOM_FLOAT(-BotAimTab[pBot->bot_skill / 20].fAim_X, BotAimTab[pBot->bot_skill / 20].fAim_X),
RANDOM_FLOAT(-BotAimTab[pBot->bot_skill / 20].fAim_Y, BotAimTab[pBot->bot_skill / 20].fAim_Y),
RANDOM_FLOAT(-BotAimTab[pBot->bot_skill / 20].fAim_Z, BotAimTab[pBot->bot_skill / 20].fAim_Z));
pBot->vecEnemyRandomOffset = vecRandom;
pBot->fEnemyOriginUpdateTime = gpGlobals->time + 0.5f;
}
else
vecRandom = pBot->vecEnemyRandomOffset;
iWeapId = pBot->current_weapon.iId;
ucVis = pBot->ucVisibility;
pEdict = pBot->pEdict;
fDistance = (pBotEnemy->v.origin - pEdict->v.origin).Length();
// Compensate both the enemy's and the bot's own velocity
vecVel = 1.0f * pBot->fTimeFrameInterval /* - gpGlobals->frametime */ * pBotEnemy->v.velocity - 1.0f * pBot->fTimeFrameInterval * pBot->pEdict->v.velocity;
// bots now don't update target all time, but every 0.5f sec (for skill 100 it's 0.0f)
// so randomize target will not affect bot crosshair all frames - it's more realistic than in PB2.5f,
// but still we didn't find better way to make stupid bots aiming worse... - KWo - 11.03.2006
iEnemyIndex = ENTINDEX(pBotEnemy) - 1; // KWo - 18.01.2011
bEnemyTeamnate = FALSE; // KWo - 18.01.2011
if (iEnemyIndex >= 0 && iEnemyIndex < gpGlobals->time) // KWo - 18.01.2011
{
if (pBot->bot_team == clients[iEnemyIndex].iTeam && !g_b_cv_ffa)
bEnemyTeamnate = TRUE;
}
bBotUsesSniper = BotUsesSniper(pBot); // KWo - 14.10.2006
bBotUsesPistol = WeaponIsPistol(iWeapId); // KWo - 23.10.2006
bBotUsesAssaultSniper = WeaponIsAssualtSniper(iWeapId); // KWo - 09.02.2008
bBotUsesSubmachine = WeaponIsSubmachineGun(iWeapId); // KWo - 09.02.2008
bBotUsesRifle = WeaponIsRifle(iWeapId); // KWo - 09.02.2008
if (!bBotUsesSniper)
{
vecVel = vecVel * pBot->fTimeFrameInterval;
// No Up/Down Compensation
vecVel.z = 0.0f;
}
else
vecVel = g_vecZero;
// Waist Visible ?
if (ucVis & WAIST_VISIBLE)
{
// Use Waist as Target for big distances
if (fDistance > 1200.0f + 4.0f * pBot->bot_skill && !bBotUsesSniper && !bBotUsesAssaultSniper
|| bEnemyTeamnate) // KWo - 18.01.2011
ucVis &= ~HEAD_VISIBLE;
if ((bBotUsesRifle && !bBotUsesAssaultSniper || iWeapId == CS_WEAPON_M3
|| iWeapId == CS_WEAPON_XM1014 || iWeapId == CS_WEAPON_M249) && fDistance > 1200.0f + 4.0f * pBot->bot_skill) // KWo - 09.02.2008
ucVis &= ~HEAD_VISIBLE;
}
// If we only suspect an Enemy behind a Wall take the worst Skill
if ((pBot->iStates & STATE_SUSPECTENEMY && !(pBot->iStates & STATE_SEEINGENEMY) && pBot->bShootThruSeen
|| pBot->iStates & STATE_SEEINGENEMY && pBot->f_bot_see_enemy_time + 5.0f > gpGlobals->time
&& pBot->f_bot_see_enemy_time < gpGlobals->time)
&& !FNullEnt(pBotEnemy)) // KWo - 08.01.2012
{
if ((fDistance < 2000.0f || BotUsesSniper(pBot))
/* && ( (pBot->fLastHeardEnOrgUpdateTime < gpGlobals->time) */
&& pBot->fLastSeenEnOrgUpdateTime < gpGlobals->time
&& pBot->f_bot_see_enemy_time + 5.0f > gpGlobals->time) // KWo - 14.10.2011
{
fRandomFactor = 2.0f * (gpGlobals->time - pBot->f_bot_see_enemy_time + 1.0f); // KWo - 11.07.2008
if (fRandomFactor < 2.0f) // KWo - 11.07.2008
fRandomFactor = 2.0f;
else if (fRandomFactor > 8.0f)
fRandomFactor = 8.0f;
target = pBot->vecLastEnemyOrigin; // KWo - 13.08.2008
if (pBot->fLastHeardEnOrgUpdateTime < gpGlobals->time
&& pBot->fLastSeenEnOrgUpdateTime < gpGlobals->time
&& pBot->f_bot_see_enemy_time + 1.0f < gpGlobals->time) // KWo - 08.01.2012
{
// target = pBotEnemy->v.origin;
// target.x = target.x + RANDOM_FLOAT (pBotEnemy->v.mins.x * fRandomFactor, pBotEnemy->v.maxs.x * fRandomFactor);
// target.y = target.y + RANDOM_FLOAT (pBotEnemy->v.mins.y * fRandomFactor, pBotEnemy->v.maxs.y * fRandomFactor);
pBot->fLastSeenEnOrgUpdateTime = gpGlobals->time + 1.0f;
}
pBot->vecLastEnemyOrigin = target;
pBot->vecEnemy = target; // KWo - 11.07.2008
pBot->vecVisPos = pBot->vecLastEnemyOrigin; // KWo - 10.07.2008
pBot->ucVisibility = WAIST_VISIBLE; // KWo - 10.07.2008
vecRandom = g_vecZero; // KWo - 12.09.2011
// if (pHostEdict)
// {
// if ((pHostEdict->v.origin - pEdict->v.origin).Length() < 20.0f)
// Vector start, Vector end, int life, int width, int noise, int red, int green, int blue, int brightness, int speed
// UTIL_DrawBeam(GetGunPosition(pEdict), target, 10, 50, 0, 0, 0, 255, 255, 0);
// }
}
else
{
target = pBot->vecLastEnemyOrigin;
vecRandom = g_vecZero; // KWo - 12.09.2011
}
}
else if (pBot->iStates & STATE_SEEINGENEMY && !FNullEnt(pBotEnemy))
{
if (ucVis & HEAD_VISIBLE && ucVis & WAIST_VISIBLE)
{
if (RANDOM_LONG(1, 100) <= BotAimTab[pBot->bot_skill / 20].iHeadShot_Frequency)
{
target = GetGunPosition(pBotEnemy); // aim for the head
// the idea taken rom YapB
if (fDistance < 3000.0f && fDistance > 2 * MIN_BURST_DISTANCE) // KWo - 09.04.2010
{
if (bBotUsesSniper)
target.z += 3.5f; // 3.5
else if (bBotUsesAssaultSniper)
target.z += 4.5f; // 2.5
else if (bBotUsesPistol)
target.z += 6.5f; // 4.5
else if (bBotUsesSubmachine)
target.z += 5.5f; // 3.0
else if (bBotUsesRifle)
target.z += 5.5f; // 3.0
else if (iWeapId == CS_WEAPON_M249)
target.z += 2.5f;
else if (iWeapId == CS_WEAPON_XM1014 || iWeapId == CS_WEAPON_M3)
target.z += 10.5f; // 1.5
}
else if (fDistance > MIN_BURST_DISTANCE && fDistance <= 2 * MIN_BURST_DISTANCE) // KWo - 09.04.2010
{
if (bBotUsesSniper)
target.z += 3.5f;
else if (bBotUsesAssaultSniper)
target.z += 3.5f; // - 1.0
else if (bBotUsesPistol)
target.z += 6.5f; // 4.5
else if (bBotUsesSubmachine)
target.z += 3.5f; // 1.0
else if (bBotUsesRifle)
target.z += 1.0f; // -1.0
else if (iWeapId == CS_WEAPON_M249)
target.z -= 2.0f; // -2.0
else if (iWeapId == CS_WEAPON_XM1014 || iWeapId == CS_WEAPON_M3)
target.z += 10.0f; // 1.0
}
else if (fDistance < MIN_BURST_DISTANCE) // KWo - 09.04.2010
{
if (bBotUsesSniper)
target.z += 4.5f;
else if (bBotUsesAssaultSniper)
target.z -= 5.0f;
else if (bBotUsesPistol)
target.z += 4.5f;
else if (bBotUsesSubmachine)
target.z -= 4.5f;
else if (bBotUsesRifle)
target.z -= 4.5f;
else if (iWeapId == CS_WEAPON_M249)
target.z -= 6.0f;
else if (iWeapId == CS_WEAPON_XM1014 || iWeapId == CS_WEAPON_M3)
target.z -= 5.0f;
}
target.z -= 0.5f * (100 - pBot->bot_skill); // 16.0f * fSkillMin; KWo - 11.03.2006
}
else
{
target = pBotEnemy->v.origin + Vector(0.0f, 0.0f, 3.0f); // aim for the chest
}
}
else if (ucVis & HEAD_VISIBLE)
{
target = GetGunPosition(pBotEnemy); // aim for the head
// the idea taken rom YapB
if (true) // just to have the same identation like above...
{
if (fDistance < 3000.0f && fDistance > 2 * MIN_BURST_DISTANCE) // KWo - 09.04.2010
{
if (bBotUsesSniper)
target.z += 5.5f; // 3.5
else if (bBotUsesAssaultSniper)
target.z += 4.5f; // 2.5
else if (bBotUsesPistol)
target.z += 6.5f; // 4.5
else if (bBotUsesSubmachine)
target.z += 5.5f; // 3.0
else if (bBotUsesRifle)
target.z += 5.5f; // 3.0
else if (iWeapId == CS_WEAPON_M249)
target.z += 2.5f;
else if (iWeapId == CS_WEAPON_XM1014 || iWeapId == CS_WEAPON_M3)
target.z += 10.5f; // 1.5
}
else if (fDistance > MIN_BURST_DISTANCE && fDistance <= 2 * MIN_BURST_DISTANCE) // KWo - 09.04.2010
{
if (bBotUsesSniper)
target.z += 3.5f;
else if (bBotUsesAssaultSniper)
target.z += 3.5f; // - 1.0
else if (bBotUsesPistol)
target.z += 6.5f; // 4.5
else if (bBotUsesSubmachine)
target.z += 3.5f; // 1.0
else if (bBotUsesRifle)
target.z += 1.0f; // -1.0
else if (iWeapId == CS_WEAPON_M249)
target.z -= 2.0f; // -2.0
else if (iWeapId == CS_WEAPON_XM1014 || iWeapId == CS_WEAPON_M3)
target.z += 10.0f; // 1.0
}
else if (fDistance < MIN_BURST_DISTANCE) // KWo - 09.04.2010
{
if (bBotUsesSniper)
target.z += 3.5f;
else if (bBotUsesAssaultSniper)
target.z -= 5.0f;
else if (bBotUsesPistol)
target.z += 4.5f;
else if (bBotUsesSubmachine)
target.z -= 4.5f;
else if (bBotUsesRifle)
target.z -= 4.5f;
else if (iWeapId == CS_WEAPON_M249)
target.z -= 6.0f;
else if (iWeapId == CS_WEAPON_XM1014 || iWeapId == CS_WEAPON_M3)
target.z -= 5.0f;
}
target.z -= 0.5f * (100 - pBot->bot_skill); // 16.0f * fSkillMin; KWo - 11.03.2006
}
}
else if (ucVis & WAIST_VISIBLE)
{
target = pBotEnemy->v.origin + Vector(0.0, 0.0, 3.0f); // aim for the chest
}
else if (ucVis & CUSTOM_VISIBLE) // aim for custom part
target = pBot->vecVisPos; // KWo - 25.01.2008
// Something went wrong - use last enemy origin
else
{
// assert (pBot == NULL);
target = pBot->vecLastEnemyOrigin;
vecRandom = g_vecZero; // KWo - 12.09.2011
}
}
else // KWo - 25.01.2008
{
target = pBot->vecLastEnemyOrigin;
vecRandom = g_vecZero; // KWo - 12.09.2011
}
if (g_i_cv_aim_type != 5)
target = target + vecVel + vecRandom; // KWo - 11.03.2006
else
target = target + vecRandom; // KWo - 11.03.2006
pBot->vecEnemy = target;
pBot->vecLastEnemyOrigin = target; // KWo - 25.01.2008
return target;
}
bool WeaponShootsThru(int iId)
{
// Returns if Weapon can pierce thru a wall
int i = 0;
while (cs_weapon_select[i].iId)
{
if (cs_weapon_select[i].iId == iId)
return cs_weapon_select[i].bShootsThru;
i++;
}
return false;
}
bool WeaponIsSniper(int iId)
{
if (iId == CS_WEAPON_AWP || iId == CS_WEAPON_G3SG1
|| iId == CS_WEAPON_SCOUT || iId == CS_WEAPON_SG550)
return true;
return false;
}
bool WeaponIsRifle(int iId)
{
if (iId == CS_WEAPON_AK47 || iId == CS_WEAPON_M4A1
|| iId == CS_WEAPON_GALIL || iId == CS_WEAPON_FAMAS
|| WeaponIsAssualtSniper(iId))
return true;
return false;
}
bool WeaponIsAssualtSniper(int iId)
{
if (iId == CS_WEAPON_AUG || iId == CS_WEAPON_SG552)
return true;
return false;
}
bool WeaponIsSubmachineGun(int iId)
{
return iId == CS_WEAPON_MP5NAVY || iId == CS_WEAPON_TMP
|| iId == CS_WEAPON_P90 || iId == CS_WEAPON_MAC10
|| iId == CS_WEAPON_UMP45;
}
bool WeaponIsPrimaryGun(int iId)
{
return WeaponIsSubmachineGun(iId) || WeaponIsSniper(iId)
|| WeaponIsRifle(iId) || iId == CS_WEAPON_M3
|| iId == CS_WEAPON_XM1014 || iId == CS_WEAPON_M249;
}
bool WeaponIsPistol(int iId)
{
if (iId == CS_WEAPON_USP || iId == CS_WEAPON_GLOCK18
|| iId == CS_WEAPON_DEAGLE || iId == CS_WEAPON_P228
|| iId == CS_WEAPON_ELITE || iId == CS_WEAPON_FIVESEVEN)
return true;
return false;
}
bool WeaponIsNade(int iId) // KWo - 15.01.2007
{
return iId == CS_WEAPON_HEGRENADE || iId == CS_WEAPON_SMOKEGRENADE
|| iId == CS_WEAPON_FLASHBANG;
}
bool BotUsesRifle(bot_t* pBot)
{
if (WeaponIsRifle(pBot->current_weapon.iId)
|| WeaponIsSniper(pBot->current_weapon.iId))
return true;
return false;
}
bool BotUsesSniper(bot_t* pBot)
{
if (WeaponIsSniper(pBot->current_weapon.iId))
return true;
return false;
}
bool BotUsesSubmachineGun(bot_t* pBot) // KWo - 20.10.2006 (from yapb)
{
return pBot->current_weapon.iId == CS_WEAPON_MP5NAVY || pBot->current_weapon.iId == CS_WEAPON_TMP
|| pBot->current_weapon.iId == CS_WEAPON_P90 || pBot->current_weapon.iId == CS_WEAPON_MAC10
|| pBot->current_weapon.iId == CS_WEAPON_UMP45;
}
bool BotHasPrimaryWeapon(bot_t* pBot)
{
bot_weapon_select_t* pSelect = &cs_weapon_select[7];
int iWeapons = pBot->pEdict->v.weapons;
// loop through all the weapons until terminator is found...
while (pSelect->iId)
{
// is the bot carrying this weapon?
if (iWeapons & 1 << pSelect->iId)
return true;
pSelect++;
}
return false;
}
bool BotHasSecondaryWeapon(bot_t* pBot) // KWo - 18.01.2011
{
bot_weapon_select_t* pSelect = &cs_weapon_select[1];
int iWeapons = pBot->pEdict->v.weapons;
int iCount = 1;
// loop through all the weapons until terminator is found...
while (pSelect->iId && iCount < 7)
{
// is the bot carrying this weapon?
if (iWeapons & 1 << pSelect->iId)
return true;
iCount++;
pSelect++;
}
return false;
}
bool BotHasSniperWeapon(bot_t* pBot) // KWo - 27.02.2007
{
int iWeapons = pBot->pEdict->v.weapons;
if (iWeapons & 1 << CS_WEAPON_SG550 || iWeapons & 1 << CS_WEAPON_G3SG1
|| iWeapons & 1 << CS_WEAPON_AWP || iWeapons & 1 << CS_WEAPON_SCOUT)
return true;
return false;
}
bool BotHasRifleWeapon(bot_t* pBot) // KWo - 27.02.2007
{
int iWeapons = pBot->pEdict->v.weapons;
if (iWeapons & 1 << CS_WEAPON_AK47 || iWeapons & 1 << CS_WEAPON_SG552
|| iWeapons & 1 << CS_WEAPON_M4A1 || iWeapons & 1 << CS_WEAPON_GALIL
|| iWeapons & 1 << CS_WEAPON_FAMAS || iWeapons & 1 << CS_WEAPON_AUG)
return true;
return false;
}
bool BotHasSubmachineGun(bot_t* pBot) // KWo - 19.05.2010
{
int iWeapons = pBot->pEdict->v.weapons;
if (iWeapons & 1 << CS_WEAPON_MP5NAVY || iWeapons & 1 << CS_WEAPON_TMP
|| iWeapons & 1 << CS_WEAPON_P90 || iWeapons & 1 << CS_WEAPON_MAC10
|| iWeapons & 1 << CS_WEAPON_UMP45)
return true;
return false;
}
bool BotHasCampWeapon(bot_t* pBot) // KWo - 19.05.2010
{
if (BotHasSubmachineGun(pBot) || BotHasRifleWeapon(pBot) || BotHasSniperWeapon(pBot))
return true;
return false;
}