forked from borzh/botrix
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.cpp
More file actions
2481 lines (2115 loc) · 94.5 KB
/
bot.cpp
File metadata and controls
2481 lines (2115 loc) · 94.5 KB
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
#include <stdio.h>
#include <math.h>
#include <good/string.h>
#include <good/string_buffer.h>
#include "clients.h"
#include "chat.h"
#include "waypoint_navigator.h"
#include "server_plugin.h"
#include "type2string.h"
#include "bot.h"
#include "in_buttons.h"
#include "public/irecipientfilter.h"
// memdbgon must be the last include file in a .cpp file!!!
#include "public/tier0/memdbgon.h"
//----------------------------------------------------------------------------------------------------------------
extern char* szMainBuffer;
extern int iMainBufferSize;
//----------------------------------------------------------------------------------------------------------------
bool CBot::bAssumeUnknownWeaponManual = false;
TBotIntelligence CBot::iMinIntelligence = EBotFool;
TBotIntelligence CBot::iMaxIntelligence = EBotPro;
TTeam CBot::iDefaultTeam = 0;
TClass CBot::iDefaultClass = -1;
int CBot::iChangeClassRound = 0;
TFightStrategyFlags CBot::iDefaultFightStrategy = FFightStrategyRunAwayIfNear | FFightStrategyComeCloserIfFar;
float CBot::fNearDistanceSqr = SQR(200);
float CBot::fFarDistanceSqr = SQR(500);
float CBot::fInvalidWaypointSuicideTime = 10.0f;
float CBot::m_fTimeIntervalCheckUsingMachines = 0.5f;
int CBot::m_iCheckEntitiesPerFrame = 4;
//----------------------------------------------------------------------------------------------------------------
CBot::CBot( edict_t* pEdict, TBotIntelligence iIntelligence, TClass iClass ):
CPlayer(pEdict, true), m_pController( CBotrixPlugin::pBotManager->GetBotController(m_pEdict) ),
m_iIntelligence(iIntelligence), m_iClass(iClass), m_iClassChange(0),
r( rand()&0xFF ), g( rand()&0xFF ), b( rand()&0xFF ),
m_aNearPlayers(CPlayers::Size()), m_aSeenEnemies(CPlayers::Size()),
m_aEnemies(CPlayers::Size()), m_aAllies(CPlayers::Size()),
m_cAttackDuckRangeSqr(0, SQR(400)),
m_bTest(false), m_bCommandAttack(true), m_bCommandPaused(false), m_bCommandStopped(false),
#if defined(DEBUG) || defined(_DEBUG)
m_bDebugging(true),
#else
m_bDebugging(false),
#endif
m_bDontBreakObjects(false), m_bDontThrowObjects(false),
m_bFeatureAttackDuckEnabled(iIntelligence < EBotNormal), m_bFeatureWeaponCheck(true)
{
m_aPickedItems.reserve(16);
for ( TItemType i=0; i < EItemTypeTotal; ++i )
{
int iSize = CItems::GetItems(i).size() >> 4;
if ( iSize == 0 )
iSize = 2;
m_aNearItems[i].reserve( iSize );
m_aNearestItems[i].reserve( 16 );
}
}
//----------------------------------------------------------------------------------------------------------------
void CBot::ConsoleCommand( const char* szCommand )
{
BotMessage( "%s -> Executing command '%s'.", GetName(), szCommand );
CBotrixPlugin::pServerPluginHelpers->ClientCommand(m_pEdict, szCommand);
}
//----------------------------------------------------------------------------------------------------------------
// Class used to send say messages to server.
//----------------------------------------------------------------------------------------------------------------
class UserRecipientFilter: public IRecipientFilter
{
public:
UserRecipientFilter(int iUserIndex): m_iUserIndex(iUserIndex) {}
virtual bool IsReliable() const { return true; }
virtual bool IsInitMessage() const { return false; }
virtual int GetRecipientCount() const { return 1; }
virtual int GetRecipientIndex( int ) const { return m_iUserIndex;}
protected:
int m_iUserIndex;
};
//----------------------------------------------------------------------------------------------------------------
void CBot::Say( bool bTeamOnly, const char* szFormat, ... )
{
static char szBotBuffer[2048];
good::string_buffer sBuffer(szBotBuffer, 2048, false);
if ( bTeamOnly )
sBuffer << "(TEAM) " << GetName() << ": ";
int iSize = sBuffer.size();
va_list vaList;
va_start(vaList, szFormat);
vsprintf(&szBotBuffer[iSize], szFormat, vaList);
va_end(vaList);
//CBotrixPlugin::pServerPluginHelpers->ClientCommand(m_pEdict, szBotBuffer);
#ifdef BOTRIX_SEND_BOT_CHAT
for ( int i = 0; i <= CPlayers::GetPlayersCount(); i++ )
{
CPlayer* pPlayer = CPlayers::Get(i);
if ( !pPlayer || pPlayer->IsBot() )
continue;
if ( bTeamOnly && (pPlayer->GetTeam() != 0) && (pPlayer->GetTeam() != GetTeam()) )
continue;
UserRecipientFilter user( pPlayer->GetIndex() + 1 );
bf_write* bf = CBotrixPlugin::pEngineServer->UserMessageBegin(&user, 3);
if ( bf )
{
bf->WriteByte( m_iIndex );
bf->WriteString( szBotBuffer );
bf->WriteByte( true );
}
CBotrixPlugin::pEngineServer->MessageEnd();
}
// Echo to server console.
if ( CBotrixPlugin::pEngineServer->IsDedicatedServer() )
Msg( "%s", szBotBuffer );
#endif
CBotrixPlugin::instance->GenerateSayEvent(m_pEdict, &szBotBuffer[iSize], bTeamOnly);
}
//----------------------------------------------------------------------------------------------------------------
void CBot::TestWaypoints( TWaypointId iFrom, TWaypointId iTo )
{
BASSERT( CWaypoints::IsValid(iFrom) && CWaypoints::IsValid(iTo), CPlayers::KickBot(this) );
CWaypoint& wFrom = CWaypoints::Get(iFrom);
Vector vSetOrigin = wFrom.vOrigin;
vSetOrigin.z -= CMod::iPlayerEyeLevel; // Make bot appear on the ground (waypoints are at eye level).
m_pController->SetAbsOrigin(vSetOrigin);
iCurrentWaypoint = iFrom;
m_iDestinationWaypoint = iTo;
m_bDontAttack = m_bDestinationChanged = m_bNeedMove = m_bLastNeedMove = m_bUseNavigatorToMove = m_bTest = true;
}
//----------------------------------------------------------------------------------------------------------------
void CBot::AddWeapon( const char* szWeaponName )
{
m_pController->SetActiveWeapon(szWeaponName);
TWeaponId iId = WeaponSearch(szWeaponName);
if ( CWeapon::IsValid(iId) ) // As if grabbed a weapon: add default bullets and weapon.
m_aWeapons[iId].AddWeapon();
else
WeaponCheckCurrent(true);
}
//----------------------------------------------------------------------------------------------------------------
void CBot::Activated()
{
CPlayer::Activated();
BotMessage( "%s -> Activated.", GetName() );
}
//----------------------------------------------------------------------------------------------------------------
void CBot::Respawned()
{
CPlayer::Respawned();
BotMessage( "%s -> Respawned.", GetName() );
if ( iChangeClassRound && m_iClassChange )
m_iClassChange--;
#ifdef BOTRIX_CHAT
m_iPrevChatMate = m_iPrevTalk = -1;
m_bTalkStarted = false;
m_cChat.iSpeaker = m_iIndex;
m_iObjective = EBotChatUnknown;
m_bPerformingRequest = false;
#endif
m_cCmd.Reset();
m_cCmd.viewangles = m_pController->GetLocalAngles();
m_cNavigator.Stop();
//m_vLastVelocity.x = m_vLastVelocity.y = m_vLastVelocity.z = 0.0f;
m_fPrevThinkTime = m_fStuckCheckTime = 0.0f;
for ( TItemType i=0; i < EItemTypeTotal; ++i )
{
m_iNextNearItem[i] = 0;
m_aNearItems[i].clear();
m_aNearestItems[i].clear();
}
m_aAvoidAreas.clear();
m_iNextCheckPlayer = 0;
m_aNearPlayers.reset();
m_aSeenEnemies.reset();
m_pCurrentEnemy = NULL;
// Don't clear picked items, as bot still know which were taken previously.
m_iCurrentPickedItem = 0;
// Set default flags.
m_bDontAttack = m_bFlee = m_bNeedSetWeapon = m_bNeedReload = m_bAttackDuck = false;
m_bNeedAim = m_bUseSideLook = false;
m_bDontAttack = m_bDestinationChanged = m_bNeedMove = m_bLastNeedMove = m_bUseNavigatorToMove = m_bTest;
m_bPathAim = m_bLockNavigatorMove = m_bLockMove = false;
m_bMoveFailure = false;
m_bStuck = m_bNeedCheckStuck = m_bStuckBreakObject = m_bStuckUsePhyscannon = false;
m_bStuckTryingSide = m_bStuckTryGoLeft = m_bStuckGotoCurrent = m_bStuckGotoPrevious = m_bRepeatWaypointAction = false;
m_bLadderMove = m_bNeedStop = m_bNeedDuck = m_bNeedWalk = m_bNeedSprint = false;
m_bNeedFlashlight = m_bUsingFlashlight = false;
m_bNeedUse = m_bAlreadyUsed = m_bUsingHealthMachine = false;
m_bNeedAttack = m_bNeedAttack2 = m_bNeedJumpDuck = m_bNeedJump = false;
m_bInvalidWaypointStart = false;
m_fNextDrawNearObjectsTime = 0.0f;
// Check near items (skip objects).
Vector vFoot = m_pController->GetLocalOrigin();
for ( TItemType iType = 0; iType < EItemTypeObject; ++iType )
{
good::vector<TItemIndex>& aNear = m_aNearItems[iType];
good::vector<TItemIndex>& aNearest = m_aNearestItems[iType];
const good::vector<CItem>& aItems = CItems::GetItems(iType);
if ( aItems.size() == 0)
continue;
for ( TItemIndex i = 0; i < (int)aItems.size(); ++i )
{
const CItem* pItem = &aItems[i];
if ( pItem->IsFree() || !pItem->IsOnMap() ) // Item is picked or broken.
continue;
Vector vOrigin = pItem->CurrentPosition();
float fDistSqr = vFoot.DistToSqr( vOrigin );
if ( fDistSqr <= pItem->fPickupDistanceSqr )
aNearest.push_back(i);
else if ( fDistSqr <= CMod::iNearItemMaxDistanceSqr ) // Item is close.
aNear.push_back(i);
}
}
// Get default weapons.
if ( m_bFeatureWeaponCheck )
{
CWeapons::GetRespawnWeapons( m_aWeapons, m_pPlayerInfo->GetTeamIndex(), m_iClass );
WeaponsScan();
if ( !CWeapon::IsValid(m_iWeapon) )
WeaponCheckCurrent(true);
}
else
m_iMeleeWeapon = m_iPhyscannon = m_iBestWeapon = m_iWeapon = EWeaponIdInvalid;
}
//----------------------------------------------------------------------------------------------------------------
void CBot::Dead()
{
BotDebug( "%s -> Dead.", GetName() );
CPlayer::Dead();
if ( m_bTest )
CPlayers::KickBot(this);
}
//----------------------------------------------------------------------------------------------------------------
void CBot::PlayerDisconnect( int iPlayerIndex, CPlayer* pPlayer )
{
m_aNearPlayers.reset(iPlayerIndex);
m_aSeenEnemies.reset(iPlayerIndex);
if ( pPlayer == m_pCurrentEnemy )
EraseCurrentEnemy();
}
//----------------------------------------------------------------------------------------------------------------
#ifdef BOTRIX_CHAT
void CBot::ReceiveChatRequest( const CBotChat& cRequest )
{
BASSERT( (cRequest.iDirectedTo == m_iIndex) && (cRequest.iSpeaker != -1), return);
if (iChatMate == -1)
iChatMate = cRequest.iSpeaker;
CBotChat cResponse(EBotChatUnknown, m_iIndex, cRequest.iSpeaker); // TODO: m_cChat.
if ( iChatMate == cRequest.iSpeaker )
{
// If conversation is not started, decide whether to help teammate or not (random).
if ( !m_bTalkStarted )
{
m_bTalkStarted = true;
m_iPrevTalk = -1;
m_bHelpingMate = rand()&1;
if ( !m_bHelpingMate ) // Force to help admin of the server.
{
CPlayer* pSpeaker = CPlayers::Get(cRequest.iSpeaker);
if ( !pSpeaker->IsBot() )
{
CClient* pClient = (CClient*)pSpeaker;
if ( FLAG_ALL_SET_OR_0(FCommandAccessBot, pClient->iCommandAccessFlags) )
m_bHelpingMate = true;
}
}
}
if ( m_iPrevTalk == -1 ) // This is a request from other player, generate response.
{
// We want to know what bot can answer.
const good::vector<TBotChat>& aPossibleAnswers = CChat::PossibleAnswers(cRequest.iBotChat);
if ( aPossibleAnswers.size() == 1 )
cResponse.iBotChat = aPossibleAnswers[0];
else if ( aPossibleAnswers.size() > 0 )
{
bool bAffirmativeNegativeAnswer = good::find(aPossibleAnswers.begin(), aPossibleAnswers.end(), EBotChatAffirm) != aPossibleAnswers.end();
if ( bAffirmativeNegativeAnswer )
{
bool bYesNoAnswer = good::find(aPossibleAnswers.begin(), aPossibleAnswers.end(), EBotChatAffirmative) != aPossibleAnswers.end();
cResponse.iBotChat = bYesNoAnswer ? ( (rand()&1) ? EBotChatAffirmative : EBotChatAffirm ) : EBotChatAffirm;
if ( m_bHelpingMate )
StartPerformingChatRequest(cRequest);
else
cResponse.iBotChat += 1; // Negate request (EBotChatAffirmative + 1 = EBotChatNegative).
}
else
cResponse.iBotChat = aPossibleAnswers[ rand() % aPossibleAnswers.size() ];
}
}
else // This is a response to this bot's request.
{
// TODO: this part is not implemented yet.
switch ( cRequest.iBotChat )
{
case EBotChatAffirm:
case EBotChatAffirmative:
case EBotChatNegative:
case EBotChatNegate:
case EBotChatBusy:
// Ask another bot?
default:;
}
}
if ( cRequest.iBotChat == EBotChatBye )
{
m_iPrevChatMate = iChatMate;
m_iPrevTalk = EBotChatBye;
iChatMate = -1;
}
}
else if ( cRequest.iBotChat == EBotChatBye )
{
if ( (m_iPrevTalk != EBotChatBye) || (m_iPrevChatMate != cRequest.iSpeaker) )
cResponse.iBotChat = EBotChatBye;
}
else
cResponse.iBotChat = EBotChatBusy;
// Generate chat.
if ( cResponse.iBotChat != -1 )
{
const good::string& sText = CChat::ChatToText(cResponse);
Say(false, sText.c_str());
}
}
//----------------------------------------------------------------------------------------------------------------
void CBot::StartPerformingChatRequest( const CBotChat& cRequest )
{
m_bPerformingRequest = true;
m_iObjective = cRequest.iBotChat;
switch ( m_iObjective )
{
case EBotChatStop:
m_bNeedMove = false;
m_bRequestTimeout = true;
m_fEndTalkActionTime = CBotrixPlugin::fTime + 30.0f;
break;
case EBotChatCome:
case EBotChatFollow:
case EBotChatAttack:
case EBotChatNoKill:
case EBotChatSitDown:
case EBotChatStandUp:
case EBotChatJump:
case EBotChatLeave:
default:
BASSERT(false); // Should not enter.
}
}
//----------------------------------------------------------------------------------------------------------------
void CBot::EndPerformingChatRequest( bool bSayGoodbye )
{
m_bPerformingRequest = m_bRequestTimeout = false;
if ( bSayGoodbye )
{
m_cChat.cMap.clear();
m_cChat.iBotChat = EBotChatBye;
if ( rand()&1 )
{
m_cChat.iDirectedTo = iChatMate;
m_cChat.cMap.push_back( CChatVarValue(CChat::iPlayerVar, 0, iChatMate) );
}
else
m_cChat.iDirectedTo = -1;
bool bTeam = ( CPlayers::Get(iChatMate)->GetTeam() == GetTeam() );
Say( bTeam, CChat::ChatToText(m_cChat).c_str() );
}
switch ( m_iObjective )
{
case EBotChatStop:
m_bNeedMove = true;
break;
case EBotChatCome:
case EBotChatFollow:
case EBotChatAttack:
case EBotChatNoKill:
case EBotChatSitDown:
case EBotChatStandUp:
case EBotChatJump:
case EBotChatLeave:
default:
BASSERT(false); // Should not enter.
}
}
#endif // BOTRIX_CHAT
//----------------------------------------------------------------------------------------------------------------
void CBot::PreThink()
{
//#define DRAW_BEAM_TO_0_0_0
#if defined(DEBUG) && defined(DRAW_BEAM_TO_0_0_0)
static float fBeamTo000 = 0.0f;
if ( CBotrixPlugin::fTime > fBeamTo000 )
{
fBeamTo000 = CBotrixPlugin::fTime + 0.5f;
CUtil::DrawBeam(GetHead(), CUtil::vZero, 5, 0.6f, 0xff, 0xff, 0xff);
}
#endif
if ( m_bCommandPaused )
return;
if ( CWaypoints::Size() <= 3 )
{
BLOG_W( "Please create more waypoints, so I could move. I am paused." );
m_bCommandPaused = true;
return;
}
Vector vPrevOrigin = m_vHead;
int iPrevCurrWaypoint = iCurrentWaypoint;
CPlayer::PreThink(); // Will invalidate current waypoint if away.
if ( CWaypoint::IsValid(iCurrentWaypoint) )
m_bInvalidWaypointStart = false;
else if ( m_bAlive && fInvalidWaypointSuicideTime )
{
if ( m_bInvalidWaypointStart )
{
if ( CBotrixPlugin::fTime >= m_fInvalidWaypointEnd ) // TODO: config.
{
BLOG_E( "%s -> Staying away from waypoints too long, suiciding.", GetName() );
ConsoleCommand("kill");
}
}
else
{
m_bInvalidWaypointStart = true;
m_fInvalidWaypointEnd = CBotrixPlugin::fTime + CBot::fInvalidWaypointSuicideTime;
}
return;
}
// Reset bot's command.
m_cCmd.forwardmove = 0;
m_cCmd.sidemove = 0;
m_cCmd.upmove = 0;
m_cCmd.buttons = 0;
m_cCmd.impulse = 0;
if ( m_bAlive ) // Update near objects, items, players and current weapon.
UpdateWorld();
if ( !m_bTest )
Think(); // Mod's think.
if ( m_bAlive )
{
PerformMove( iPrevCurrWaypoint, vPrevOrigin );
if ( m_bNeedMove && m_bUseNavigatorToMove && m_cNavigator.SearchEnded() )
m_cNavigator.DrawPath(r, g, b, m_vHead);
// Show near items in white and nearest in red.
if ( m_bDebugging )
{
static const float fDrawNearObjectsTime = 0.1f;
if ( CBotrixPlugin::fTime > m_fNextDrawNearObjectsTime )
{
m_fNextDrawNearObjectsTime = CBotrixPlugin::fTime + fDrawNearObjectsTime;
for ( TItemType iType=0; iType < EItemTypeTotal; ++iType)
{
const good::vector<CItem>& aItems = CItems::GetItems(iType);
for ( int i=0; i < m_aNearItems[iType].size(); ++i) // Draw near items with white color.
{
ICollideable* pCollide = aItems[ m_aNearItems[iType][i] ].pEdict->GetCollideable();
CUtil::DrawBox(pCollide->GetCollisionOrigin(), pCollide->OBBMins(), pCollide->OBBMaxs(),
fDrawNearObjectsTime, 0xFF, 0xFF, 0xFF, pCollide->GetCollisionAngles());
}
for ( int i=0; i < m_aNearestItems[iType].size(); ++i) // Draw nearest items with red color.
{
ICollideable* pCollide = aItems[ m_aNearestItems[iType][i] ].pEdict->GetCollideable();
CUtil::DrawBox(pCollide->GetCollisionOrigin(), pCollide->OBBMins(), pCollide->OBBMaxs(),
fDrawNearObjectsTime, 0xFF, 0x00, 0x00, pCollide->GetCollisionAngles());
}
}
}
}
}
m_pController->RunPlayerMove(&m_cCmd);
#ifdef BOTRIX_SOURCE_ENGINE_2006
m_pController->PostClientMessagesSent();
#endif
m_fPrevThinkTime = CBotrixPlugin::fTime;
// Bot created for testing and couldn't find path or reached destination and finished using health/armor machine.
if ( m_bTest && ( /*m_bMoveFailure || */(!m_bNeedMove && !m_bNeedUse) ) )
CPlayers::KickBot(this);
}
//================================================================================================================
// CBot virtual protected methods.
//================================================================================================================
void CBot::CurrentWaypointJustChanged()
{
if ( m_bNeedMove && m_bUseNavigatorToMove )
{
if ( m_cNavigator.SearchEnded() )
{
if ( iCurrentWaypoint == iNextWaypoint ) // Bot becomes closer to next waypoint in path.
{
if ( CWaypoint::IsValid(m_iAfterNextWaypoint) )
{
// Set forward look to next waypoint in path.
m_vForward = CWaypoints::Get(m_iAfterNextWaypoint).vOrigin;
// Don't change look while using actions.
if ( !m_bEnemyAim && !m_bPathAim )
{
m_bNeedAim = true;
m_vLook = m_vForward;
m_fEndAimTime = CBotrixPlugin::fTime + GetEndLookTime();
}
}
}
else if ( CWaypoint::IsValid(iNextWaypoint) ) // Something went wrong (bot falls or pushed?).
{
BLOG_W("%s -> Invalid current waypoint %d (should be %d).", GetName(), iCurrentWaypoint, iNextWaypoint);
m_bMoveFailure = true;
iCurrentWaypoint = EWaypointIdInvalid;
m_cNavigator.Stop();
}
else
{
// Can't touch last waypoint, just stop.
m_bNeedMove = false;
m_cNavigator.Stop();
}
}
else
m_bDestinationChanged = true; // Search not ended, but waypoint changed (?), restart search from current waypoint.
}
}
//----------------------------------------------------------------------------------------------------------------
bool CBot::DoWaypointAction()
{
if ( m_bEnemyAim || !m_bNeedMove || !m_bUseNavigatorToMove )
return false;
BASSERT( CWaypoint::IsValid(iCurrentWaypoint), return false );
CWaypoint& w = CWaypoints::Get(iCurrentWaypoint);
if ( m_bAlreadyUsed )
{
// When coming back to current waypoint (if stucked) don't use it again, but make this variable false at next waypoint.
m_bAlreadyUsed = FLAG_SOME_SET(FWaypointHealthMachine | FWaypointArmorMachine, w.iFlags) != 0;
}
else
{
// Check if need health/armor.
if ( FLAG_SOME_SET(FWaypointHealthMachine, w.iFlags) )
{
m_iLastHealthArmor = m_pPlayerInfo->GetHealth();
m_bNeedUse = m_iLastHealthArmor < m_pPlayerInfo->GetMaxHealth();
m_bUsingHealthMachine = true;
}
else if ( FLAG_SOME_SET(FWaypointArmorMachine, w.iFlags) )
{
m_iLastHealthArmor = m_pPlayerInfo->GetArmorValue();
m_bNeedUse = m_iLastHealthArmor < CMod::iPlayerMaxArmor;
m_bUsingHealthMachine = false;
}
if ( m_bNeedUse )
{
QAngle angNeeded;
CWaypoint::GetFirstAngle(angNeeded, w.iArgument);
CUtil::NormalizeAngle(angNeeded.x);
CUtil::NormalizeAngle(angNeeded.y);
AngleVectors(angNeeded, &m_vForward);
m_vForward *= 1000.0f;
m_vForward += m_vHead;
m_vLook = m_vForward;
m_fStartActionTime = m_fEndAimTime = CBotrixPlugin::fTime + GetEndLookTime();
m_fEndActionTime = m_fStartActionTime + m_fTimeIntervalCheckUsingMachines;
m_bLockNavigatorMove = m_bPathAim = m_bNeedAim = true;
}
}
// To stop, bot must not start from current waypoint.
//if ( CWaypoint::IsValid(iPrevWaypoint) && (iCurrentWaypoint != iPrevWaypoint) &&
// FLAG_SOME_SET(FWaypointStop, CWaypoints::Get(iCurrentWaypoint).iFlags) )
// m_bNeedStop = true;
m_bNeedStop = FLAG_SOME_SET(FWaypointStop, CWaypoints::Get(iCurrentWaypoint).iFlags);
return m_bNeedUse;
}
//----------------------------------------------------------------------------------------------------------------
void CBot::ApplyPathFlags()
{
GoodAssert( m_bNeedMove );
//BotMessage("%s -> Waypoint %d", m_pPlayerInfo->GetName(), iCurrentWaypoint);
// Release buttons and locks.
m_bNeedFlashlight = m_bAlreadyUsed = m_bNeedUse = false;
m_bNeedJumpDuck = m_bNeedJump = m_bNeedAttack = m_bNeedAttack2 = m_bNeedSprint = m_bNeedDuck = m_bNeedWalk = false;
m_bLockNavigatorMove = m_bPathAim = false;
m_bPathAim = false;
if ( CWaypoint::IsValid(iNextWaypoint) )
{
CWaypoint& wNext = CWaypoints::Get(iNextWaypoint);
m_vDestination = wNext.vOrigin;
if ( iCurrentWaypoint == iNextWaypoint ) // Happens when bot stucks.
{
if ( CWaypoint::IsValid(m_iAfterNextWaypoint) )
m_vForward = CWaypoints::Get(m_iAfterNextWaypoint).vOrigin;
else
m_vForward = wNext.vOrigin;
}
else
{
m_vForward = wNext.vOrigin;
CWaypointPath* pCurrentPath = CWaypoints::GetPath(iCurrentWaypoint, iNextWaypoint);
BASSERT( pCurrentPath, return );
m_bLadderMove = FLAG_ALL_SET(FPathLadder, pCurrentPath->iFlags);
if ( FLAG_ALL_SET(FPathStop, pCurrentPath->iFlags) )
m_bNeedStop = true;
if ( FLAG_ALL_SET(FPathSprint, pCurrentPath->iFlags) )
m_bNeedSprint = true;
if ( FLAG_ALL_SET(FPathFlashlight, pCurrentPath->iFlags) )
m_bNeedFlashlight = true;
if ( FLAG_ALL_SET(FPathCrouch, pCurrentPath->iFlags) &&
!FLAG_ALL_SET(FPathJump, pCurrentPath->iFlags) )
m_bNeedDuck = true; // Crouch only when not jumping.
// Dont use path aim if aim is locked.
m_bPathAim = !m_bEnemyAim && FLAG_SOME_SET( FPathJump | FPathBreak | FPathSprint | FPathLadder | FPathStop, pCurrentPath->iFlags);
}
}
if ( m_bPathAim )
{
m_bNeedAim = true;
m_vLook = m_vForward; // Always look to next waypoint when aim is locked.
m_fEndAimTime = CBotrixPlugin::fTime + GetEndLookTime();
}
}
//----------------------------------------------------------------------------------------------------------------
void CBot::DoPathAction()
{
BASSERT( CWaypoint::IsValid(iCurrentWaypoint), return );
if ( CWaypoint::IsValid(iNextWaypoint) && (iCurrentWaypoint != iNextWaypoint) )
{
CWaypointPath* pCurrentPath = CWaypoints::GetPath(iCurrentWaypoint, iNextWaypoint);
BASSERT( pCurrentPath, return );
if ( FLAG_SOME_SET(FPathBreak, pCurrentPath->iFlags) )
m_bNeedAttack = true;
if ( FLAG_SOME_SET(FPathJump | FPathTotem, pCurrentPath->iFlags) )
m_bNeedJump = true;
if ( FLAG_SOME_SET(FPathCrouch | FPathJump, pCurrentPath->iFlags) || FLAG_SOME_SET(FPathTotem, pCurrentPath->iFlags) )
m_bNeedJumpDuck = true;
if ( m_bNeedAttack || m_bNeedJump || m_bNeedJumpDuck )
{
int iStartTime = GET_1ST_BYTE(pCurrentPath->iArgument); // Action (jump/ jump with duck / attack) start time in deciseconds at first byte.
int iDuration = GET_2ND_BYTE(pCurrentPath->iArgument); // Action duration (duck for jump / attack for break) in deciseconds at second byte.
if (iDuration == 0)
iDuration = 10; // Duration 1 second by default. Good for either duck while jumping or attack (to break something).
m_fStartActionTime = CBotrixPlugin::fTime + (iStartTime / 10.0f);
m_fEndActionTime = CBotrixPlugin::fTime + ( (iStartTime + iDuration) / 10.0f);
}
}
m_bRepeatWaypointAction = false;
}
//----------------------------------------------------------------------------------------------------------------
void CBot::PickItem( const CItem& cItem, TItemType iEntityType, TItemIndex iIndex )
{
switch ( iEntityType )
{
case EItemTypeHealth:
BotMessage("%s -> Picked %s. Health now %d.", GetName(), cItem.pItemClass->sClassName.c_str(), m_pPlayerInfo->GetHealth());
break;
case EItemTypeArmor:
BotMessage("%s -> Picked %s. Armor now %d.", GetName(), cItem.pItemClass->sClassName.c_str(), m_pPlayerInfo->GetArmorValue());
break;
case EItemTypeWeapon:
if ( m_bFeatureWeaponCheck )
{
TWeaponId iWeapon = CWeapons::AddWeapon(cItem.pItemClass, m_aWeapons);
if ( CWeapon::IsValid(iWeapon) )
{
CWeaponWithAmmo& cWeapon = m_aWeapons[iWeapon];
BotMessage( "%s -> Picked weapon %s (%d/%d, %d/%d).", GetName(), cItem.pItemClass->sClassName.c_str(),
cWeapon.Bullets(CWeapon::PRIMARY), cWeapon.ExtraBullets(CWeapon::PRIMARY),
cWeapon.Bullets(CWeapon::SECONDARY), cWeapon.ExtraBullets(CWeapon::SECONDARY) );
WeaponChoose();
}
else if ( CMod::aClassNames.size() )
BLOG_W( "%s -> Picked weapon %s, but there is no such weapon for class %s.", GetName(),
cItem.pItemClass->sClassName.c_str(), CTypeToString::ClassToString(m_iClass).c_str() );
else
BLOG_W( "%s -> Picked weapon %s, but there is no such weapon.", GetName(), cItem.pItemClass->sClassName.c_str() );
}
break;
case EItemTypeAmmo:
if ( m_bFeatureWeaponCheck )
{
if ( CWeapons::AddAmmo(cItem.pItemClass, m_aWeapons) )
{
BotMessage( "%s -> Picked ammo %s.", GetName(), cItem.pItemClass->sClassName.c_str() );
WeaponChoose();
}
else
BLOG_W("%s -> Picked ammo %s, but bot doesn't have that weapon.", GetName(), cItem.pItemClass->sClassName.c_str() );
}
break;
case EItemTypeObject:
BotMessage("%s -> Breaked object %s", GetName(), cItem.pItemClass->sClassName.c_str());
break;
default:
BASSERT(false);
}
if ( iEntityType != EItemTypeObject )
{
CPickedItem cPickedItem( iEntityType, iIndex, CBotrixPlugin::fTime );
// If item is not respawnable (or just bad configuration), force to not to search for it again right away, but in 1 minute at least.
// TODO: check if item is respawnable.
cPickedItem.fRemoveTime += /*FLAG_ALL_SET_OR_0(FEntityRespawnable, cItem.iFlags) ? cItem.pItemClass->GetArgument() : */60.0f;
m_aPickedItems.push_back( cPickedItem );
}
}
//================================================================================================================
// CBot protected methods.
//================================================================================================================
#ifdef BOTRIX_CHAT
void CBot::Speak( bool bTeamSay )
{
TPlayerIndex iPlayerIndex = m_cChat.iDirectedTo;
if ( iPlayerIndex == EPlayerIndexInvalid )
iPlayerIndex = m_iPrevChatMate;
if ( iPlayerIndex != EPlayerIndexInvalid )
m_cChat.cMap.push_back( CChatVarValue(CChat::iPlayerVar, 0, iPlayerIndex) );
const good::string& sText = CChat::ChatToText(m_cChat);
BLOG_D( "%s: %s %s", GetName(), bTeamSay? "say_team" : "say", sText.c_str() );
Say( bTeamSay, sText.c_str() );
}
#else
void CBot::Speak( bool ) {}
#endif
//----------------------------------------------------------------------------------------------------------------
bool CBot::IsVisible( CPlayer* pPlayer, bool bViewCone ) const
{
// Check PVS first. Get visible clusters from player's position.
Vector vAim(pPlayer->GetHead());
CUtil::SetPVSForVector(m_vHead);
if ( !CUtil::IsVisiblePVS(vAim) )
return false;
// First check if other player is in bot's view cone.
static const float fFovHorizontal = 70.0f; // Giving 140 degree of view horizontally.
static const float fFovVertical = fFovHorizontal * 3/4; // Normal monitor has 4:3 aspect ratio.
if ( bViewCone ) // Check view cone.
{
vAim -= m_vHead; // vAim contains vector to enemy relative from bot.
QAngle angPlayer;
VectorAngles(vAim, angPlayer);
CUtil::GetAngleDifference(angPlayer, m_cCmd.viewangles, angPlayer);
bViewCone = ( (-fFovHorizontal <= angPlayer.x) && (angPlayer.x <= fFovHorizontal) &&
(-fFovVertical <= angPlayer.y) && (angPlayer.y <= fFovVertical) );
}
else
bViewCone = true; // Assume enemy is in view cone.
// CUtil::IsVisible( m_vHead, vAim, FVisibilityAll );
return bViewCone ? CUtil::IsVisible( m_vHead, pPlayer->GetEdict() ) : false;
}
//----------------------------------------------------------------------------------------------------------------
float CBot::GetEndLookTime()
{
// angle 180 degrees = aAimSpeed time -> Y time = angle X * aAimSpeed / 180 degrees
// angle X = Y time
//static const float aAimSpeed[EBotIntelligenceTotal] = { 1.20f, 0.85f, 0.67f, 0.57f, 1.42f };
static const int iAngDiff = 40; // 30 degrees to make time difference in bot's aim.
static const int iEndAimSize = 180/iAngDiff + 1;
static const float aEndAimTime[EBotIntelligenceTotal][iEndAimSize] =
{
// 40 80 120 160 180
{ 0.40f, 0.55f, 0.70f, 0.85f, 1.00f },
{ 0.30f, 0.42f, 0.54f, 0.66f, 0.80f },
{ 0.20f, 0.30f, 0.40f, 0.50f, 0.60f },
{ 0.15f, 0.20f, 0.25f, 0.30f, 0.35f },
{ 0.10f, 0.10f, 0.15f, 0.20f, 0.25f },
};
Vector vDestinationAim( m_vLook );
vDestinationAim -= m_vHead; // Get abs vector.
QAngle angNeeded;
VectorAngles(vDestinationAim, angNeeded);
CUtil::GetAngleDifference(m_cCmd.viewangles, angNeeded, angNeeded);
int iPitch = abs( (int)angNeeded.x );
int iYaw = abs( (int)angNeeded.y );
iPitch /= iAngDiff;
iYaw /= iAngDiff;
BASSERT( (iPitch <= iEndAimSize) && (iYaw <= iEndAimSize), return 0.0f );
if ( iPitch < iYaw)
iPitch = iYaw; // iPitch = MAX2( iPitch, iYaw );
float fAimTime = aEndAimTime[m_iIntelligence][iPitch];
BASSERT( fAimTime < 2.0f, return 2.0f );
return fAimTime;
//return CBotrixPlugin::fTime + iPitch * aAimSpeed[m_iIntelligence] / 180.0f;
}
//----------------------------------------------------------------------------------------------------------------
void CBot::WeaponCheckCurrent( bool bAddToBotWeapons )
{
// Check weapon bot has in hands.
const char* szCurrentWeapon = m_pPlayerInfo->GetWeaponName();
if ( !szCurrentWeapon )
return;
GoodAssert( WeaponSearch(szCurrentWeapon) == EWeaponIdInvalid );
good::string sCurrentWeapon( szCurrentWeapon );
TWeaponId iId = CWeapons::GetIdFromWeaponName( sCurrentWeapon );
if ( iId == EWeaponIdInvalid )
{
// Add weapon class first.
BLOG_W( "%s -> Adding new weapon class %s.", GetName(), szCurrentWeapon );
CItemClass cWeaponClass;
cWeaponClass.fPickupDistanceSqr = CMod::iPlayerRadius << 4; // 4*player's radius.
cWeaponClass.fPickupDistanceSqr *= cWeaponClass.fPickupDistanceSqr;
// Don't set engine name so mod will think that there is no such weapon in this map.
//cWeaponClass.szEngineName = szCurrentWeapon;
cWeaponClass.sClassName = szCurrentWeapon;
const CItemClass* pClass = CItems::AddItemClassFor( EItemTypeWeapon, cWeaponClass );
// Add new weapon to default weapons.
BLOG_W( "%s -> Adding new weapon %s, %s.", GetName(), szCurrentWeapon,
bAssumeUnknownWeaponManual ? "melee" : "ranged" );
CWeapon* pNewWeapon = new CWeapon();
pNewWeapon->pWeaponClass = pClass;
// Make it usable by all classes and teams.
pNewWeapon->iClass = -1;
pNewWeapon->iTeam = -1;
pNewWeapon->fDamage[0] = 0.01; // DamagePerSecond() will be low to not select unknown weapons by default.
// Make it has infinite ammo.
pNewWeapon->iAttackBullets[CWeapon::PRIMARY] = 0;
if ( bAssumeUnknownWeaponManual )
{
pNewWeapon->iType = EWeaponMelee;
pNewWeapon->fShotTime[0] = 0.2f; // Manual weapon: 5 times in a second.
}
else
{
pNewWeapon->iType = EWeaponRifle;
pNewWeapon->iClipSize[CWeapon::PRIMARY] = 1;
pNewWeapon->iDefaultAmmo[CWeapon::PRIMARY] = 1;
pNewWeapon->fShotTime[0] = 0.01f; // This will make bot always press attack button.
}
CWeaponWithAmmo cNewWeapon(pNewWeapon);
iId = CWeapons::Add(cNewWeapon);
}
if ( bAddToBotWeapons )
{
const CWeaponWithAmmo& cNewWeapon = CWeapons::Get(iId);
m_aWeapons.push_back( cNewWeapon );
iId = m_aWeapons.size()-1;
m_aWeapons[iId].AddWeapon();
if ( !CWeapon::IsValid(m_iMeleeWeapon) && cNewWeapon.IsMelee() )
m_iMeleeWeapon = iId;
else if ( !CWeapon::IsValid(m_iPhyscannon) && cNewWeapon.IsPhysics() )
m_iPhyscannon = iId;
else if ( !CWeapon::IsValid(m_iBestWeapon) && cNewWeapon.IsRanged() )