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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
|
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
// $NoKeywords: $
//=============================================================================//
//
// Author: Michael S. Booth ([email protected]), 2003
//
// NOTE: The CS Bot code uses Doxygen-style comments. If you run Doxygen over this code, it will
// auto-generate documentation. Visit www.doxygen.org to download the system for free.
//
#ifndef BOT_H
#define BOT_H
#include "cbase.h"
#include "in_buttons.h"
#include "movehelper_server.h"
#include "mathlib/mathlib.h"
#include "bot_manager.h"
#include "bot_util.h"
#include "bot_constants.h"
#include "nav_mesh.h"
#include "gameinterface.h"
#include "weapon_csbase.h"
#include "shared_util.h"
#include "util.h"
#include "shareddefs.h"
#include "tier0/vprof.h"
class BotProfile;
extern bool AreBotsAllowed();
//--------------------------------------------------------------------------------------------------------
// BOTPORT: Convert everything to assume "origin" means "feet"
//
// Utility function to get "centroid" or center of player or player equivalent
//
inline Vector GetCentroid( const CBaseEntity *player )
{
Vector centroid = player->GetAbsOrigin();
const Vector &mins = player->WorldAlignMins();
const Vector &maxs = player->WorldAlignMaxs();
centroid.z += (maxs.z - mins.z)/2.0f;
//centroid.z += HalfHumanHeight;
return centroid;
}
CBasePlayer* ClientPutInServerOverride_Bot( edict_t *pEdict, const char *playername );
/// @todo Remove this nasty hack - CreateFakeClient() calls CBot::Spawn, which needs the profile
extern const BotProfile *g_botInitProfile;
extern int g_botInitTeam;
extern int g_nClientPutInServerOverrides;
//--------------------------------------------------------------------------------------------------------
template < class T > T * CreateBot( const BotProfile *profile, int team )
{
if ( !AreBotsAllowed() )
return NULL;
if ( UTIL_ClientsInGame() >= gpGlobals->maxClients )
{
CONSOLE_ECHO( "Unable to create bot: Server is full (%d/%d clients).\n", UTIL_ClientsInGame(), gpGlobals->maxClients );
return NULL;
}
// set the bot's name
char botName[64];
UTIL_ConstructBotNetName( botName, 64, profile );
// This is a backdoor we use so when the engine calls ClientPutInServer (from CreateFakeClient),
// expecting the game to make an entity for the fake client, we can make our special bot class
// instead of a CCSPlayer.
g_nClientPutInServerOverrides = 0;
ClientPutInServerOverride( ClientPutInServerOverride_Bot );
// get an edict for the bot
// NOTE: This will ultimately invoke CBot::Spawn(), so set the profile now
g_botInitProfile = profile;
g_botInitTeam = team;
edict_t *botEdict = engine->CreateFakeClient( botName );
ClientPutInServerOverride( NULL );
Assert( g_nClientPutInServerOverrides == 1 );
if ( botEdict == NULL )
{
CONSOLE_ECHO( "Unable to create bot: CreateFakeClient() returned null.\n" );
return NULL;
}
// create an instance of the bot's class and bind it to the edict
T *bot = dynamic_cast< T * >( CBaseEntity::Instance( botEdict ) );
if ( bot == NULL )
{
Assert( false );
Error( "Could not allocate and bind entity to bot edict.\n" );
return NULL;
}
bot->ClearFlags();
bot->AddFlag( FL_CLIENT | FL_FAKECLIENT );
return bot;
}
//----------------------------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------------------------
/**
* The base bot class from which bots for specific games are derived
* A template is needed here because the CBot class must be derived from CBasePlayer,
* but also may need to be derived from a more specific player class, such as CCSPlayer
*/
template < class PlayerType >
class CBot : public PlayerType
{
public:
DECLARE_CLASS( CBot, PlayerType );
CBot( void ); ///< constructor initializes all values to zero
virtual ~CBot();
virtual bool Initialize( const BotProfile *profile, int team ); ///< (EXTEND) prepare bot for action
unsigned int GetID( void ) const { return m_id; } ///< return bot's unique ID
virtual bool IsBot( void ) const { return true; }
virtual bool IsNetClient( void ) const { return false; } // Bots should return FALSE for this, they can't receive NET messages
virtual void Spawn( void ); ///< (EXTEND) spawn the bot into the game
virtual void Upkeep( void ) = 0; ///< lightweight maintenance, invoked frequently
virtual void Update( void ) = 0; ///< heavyweight algorithms, invoked less often
virtual void Run( void );
virtual void Walk( void );
virtual bool IsRunning( void ) const { return m_isRunning; }
virtual void Crouch( void );
virtual void StandUp( void );
bool IsCrouching( void ) const { return m_isCrouching; }
void PushPostureContext( void ); ///< push the current posture context onto the top of the stack
void PopPostureContext( void ); ///< restore the posture context to the next context on the stack
virtual void MoveForward( void );
virtual void MoveBackward( void );
virtual void StrafeLeft( void );
virtual void StrafeRight( void );
#define MUST_JUMP true
virtual bool Jump( bool mustJump = false ); ///< returns true if jump was started
bool IsJumping( void ); ///< returns true if we are in the midst of a jump
float GetJumpTimestamp( void ) const { return m_jumpTimestamp; } ///< return time last jump began
virtual void ClearMovement( void ); ///< zero any MoveForward(), Jump(), etc
const Vector &GetViewVector( void ); ///< return the actual view direction
//------------------------------------------------------------------------------------
// Weapon interface
//
virtual void UseEnvironment( void );
virtual void PrimaryAttack( void );
virtual void ClearPrimaryAttack( void );
virtual void TogglePrimaryAttack( void );
virtual void SecondaryAttack( void );
virtual void Reload( void );
float GetActiveWeaponAmmoRatio( void ) const; ///< returns ratio of ammo left to max ammo (1 = full clip, 0 = empty)
bool IsActiveWeaponClipEmpty( void ) const; ///< return true if active weapon has any empty clip
bool IsActiveWeaponOutOfAmmo( void ) const; ///< return true if active weapon has no ammo at all
bool IsActiveWeaponRecoilHigh( void ) const; ///< return true if active weapon's bullet spray has become large and inaccurate
bool IsUsingScope( void ); ///< return true if looking thru weapon's scope
//------------------------------------------------------------------------------------
// Event hooks
//
/// invoked when injured by something (EXTEND) - returns the amount of damage inflicted
virtual int OnTakeDamage( const CTakeDamageInfo &info )
{
return PlayerType::OnTakeDamage( info );
}
/// invoked when killed (EXTEND)
virtual void Event_Killed( const CTakeDamageInfo &info )
{
PlayerType::Event_Killed( info );
}
bool IsEnemy( CBaseEntity *ent ) const; ///< returns TRUE if given entity is our enemy
int GetEnemiesRemaining( void ) const; ///< return number of enemies left alive
int GetFriendsRemaining( void ) const; ///< return number of friends left alive
bool IsPlayerFacingMe( CBasePlayer *enemy ) const; ///< return true if player is facing towards us
bool IsPlayerLookingAtMe( CBasePlayer *enemy, float cosTolerance = 0.9f ) const; ///< returns true if other player is pointing right at us
bool IsLookingAtPosition( const Vector &pos, float angleTolerance = 20.0f ) const; ///< returns true if looking (roughly) at given position
bool IsLocalPlayerWatchingMe( void ) const; ///< return true if local player is observing this bot
void PrintIfWatched( PRINTF_FORMAT_STRING const char *format, ... ) const; ///< output message to console if we are being watched by the local player
virtual void UpdatePlayer( void ); ///< update player physics, movement, weapon firing commands, etc
virtual void BuildUserCmd( CUserCmd& cmd, const QAngle& viewangles, float forwardmove, float sidemove, float upmove, int buttons, byte impulse );
virtual void SetModel( const char *modelName );
int Save( CSave &save ) const { return 0; }
int Restore( CRestore &restore ) const { return 0; }
virtual void Think( void ) { }
const BotProfile *GetProfile( void ) const { return m_profile; } ///< return our personality profile
virtual bool ClientCommand( const CCommand &args ); ///< Do a "client command" - useful for invoking menu choices, etc.
virtual int Cmd_Argc( void ); ///< Returns the number of tokens in the command string
virtual char *Cmd_Argv( int argc ); ///< Retrieves a specified token
private:
CUtlVector< char * > m_args;
protected:
const BotProfile *m_profile; ///< the "personality" profile of this bot
private:
friend class CBotManager;
unsigned int m_id; ///< unique bot ID
CUserCmd m_userCmd;
bool m_isRunning; ///< run/walk mode
bool m_isCrouching; ///< true if crouching (ducking)
float m_forwardSpeed;
float m_strafeSpeed;
float m_verticalSpeed;
int m_buttonFlags; ///< bitfield of movement buttons
float m_jumpTimestamp; ///< time when we last began a jump
Vector m_viewForward; ///< forward view direction (only valid when GetViewVector() is used)
/// the PostureContext represents the current settings of walking and crouching
struct PostureContext
{
bool isRunning;
bool isCrouching;
};
enum { MAX_POSTURE_STACK = 8 };
PostureContext m_postureStack[ MAX_POSTURE_STACK ];
int m_postureStackIndex; ///< index of top of stack
void ResetCommand( void );
//byte ThrottledMsec( void ) const;
protected:
virtual float GetMoveSpeed( void ); ///< returns current movement speed (for walk/run)
};
//-----------------------------------------------------------------------------------------------------------
//-----------------------------------------------------------------------------------------------------------
//
// Inlines
//
//--------------------------------------------------------------------------------------------------------------
template < class T >
inline void CBot<T>::SetModel( const char *modelName )
{
BaseClass::SetModel( modelName );
}
//-----------------------------------------------------------------------------------------------------------
template < class T >
inline float CBot<T>::GetMoveSpeed( void )
{
return this->MaxSpeed();
}
//-----------------------------------------------------------------------------------------------------------
template < class T >
inline void CBot<T>::Run( void )
{
m_isRunning = true;
}
//-----------------------------------------------------------------------------------------------------------
template < class T >
inline void CBot<T>::Walk( void )
{
m_isRunning = false;
}
//-----------------------------------------------------------------------------------------------------------
template < class T >
inline bool CBot<T>::IsActiveWeaponRecoilHigh( void ) const
{
const QAngle &angles = const_cast< CBot<T> * >( this )->GetPunchAngle();
const float highRecoil = -1.5f;
return (angles.x < highRecoil);
}
//-----------------------------------------------------------------------------------------------------------
template < class T >
inline void CBot<T>::PushPostureContext( void )
{
if (m_postureStackIndex == MAX_POSTURE_STACK)
{
PrintIfWatched( "PushPostureContext() overflow error!\n" );
return;
}
m_postureStack[ m_postureStackIndex ].isRunning = m_isRunning;
m_postureStack[ m_postureStackIndex ].isCrouching = m_isCrouching;
++m_postureStackIndex;
}
//-----------------------------------------------------------------------------------------------------------
template < class T >
inline void CBot<T>::PopPostureContext( void )
{
if (m_postureStackIndex == 0)
{
PrintIfWatched( "PopPostureContext() underflow error!\n" );
m_isRunning = true;
m_isCrouching = false;
return;
}
--m_postureStackIndex;
m_isRunning = m_postureStack[ m_postureStackIndex ].isRunning;
m_isCrouching = m_postureStack[ m_postureStackIndex ].isCrouching;
}
//-----------------------------------------------------------------------------------------------------------
template < class T >
inline bool CBot<T>::IsPlayerFacingMe( CBasePlayer *other ) const
{
Vector toOther = other->GetAbsOrigin() - this->GetAbsOrigin();
Vector otherForward;
AngleVectors( other->EyeAngles() + other->GetPunchAngle(), &otherForward );
if (DotProduct( otherForward, toOther ) < 0.0f)
return true;
return false;
}
//-----------------------------------------------------------------------------------------------------------
template < class T >
inline bool CBot<T>::IsPlayerLookingAtMe( CBasePlayer *other, float cosTolerance ) const
{
Vector toOther = other->GetAbsOrigin() - this->GetAbsOrigin();
toOther.NormalizeInPlace();
Vector otherForward;
AngleVectors( other->EyeAngles() + other->GetPunchAngle(), &otherForward );
// other player must be pointing nearly right at us to be "looking at" us
if (DotProduct( otherForward, toOther ) < -cosTolerance)
return true;
return false;
}
//-----------------------------------------------------------------------------------------------------------
template < class T >
inline const Vector &CBot<T>::GetViewVector( void )
{
AngleVectors( this->EyeAngles() + this->GetPunchAngle(), &m_viewForward );
return m_viewForward;
}
//-----------------------------------------------------------------------------------------------------------
template < class T >
inline bool CBot<T>::IsLookingAtPosition( const Vector &pos, float angleTolerance ) const
{
// forced to do this since many methods in CBaseEntity are not const, but should be
CBot< T > *me = const_cast< CBot< T > * >( this );
Vector to = pos - me->EyePosition();
QAngle idealAngles;
VectorAngles( to, idealAngles );
QAngle viewAngles = me->EyeAngles();
float deltaYaw = AngleNormalize( idealAngles.y - viewAngles.y );
float deltaPitch = AngleNormalize( idealAngles.x - viewAngles.x );
if (fabs( deltaYaw ) < angleTolerance && abs( deltaPitch ) < angleTolerance)
return true;
return false;
}
//--------------------------------------------------------------------------------------------------------------
template < class PlayerType >
inline CBot< PlayerType >::CBot( void )
{
// the profile will be attached after this instance is constructed
m_profile = NULL;
// assign this bot a unique ID
static unsigned int nextID = 1;
// wraparound (highly unlikely)
if (nextID == 0)
++nextID;
m_id = nextID;
++nextID;
m_postureStackIndex = 0;
}
//--------------------------------------------------------------------------------------------------------------
template < class PlayerType >
inline CBot< PlayerType >::~CBot( void )
{
}
//--------------------------------------------------------------------------------------------------------------
/**
* Prepare bot for action
*/
template < class PlayerType >
inline bool CBot< PlayerType >::Initialize( const BotProfile *profile, int team )
{
m_profile = profile;
return true;
}
//--------------------------------------------------------------------------------------------------------------
template < class PlayerType >
inline void CBot< PlayerType >::Spawn( void )
{
// initialize the bot (thus setting its profile)
if (m_profile == NULL)
Initialize( g_botInitProfile, g_botInitTeam );
// let the base class set some things up
PlayerType::Spawn();
// Make sure everyone knows we are a bot
this->AddFlag( FL_CLIENT | FL_FAKECLIENT );
// Bots use their own thinking mechanism
this->SetThink( NULL );
m_isRunning = true;
m_isCrouching = false;
m_postureStackIndex = 0;
m_jumpTimestamp = 0.0f;
// Command interface variable initialization
ResetCommand();
}
/*
//--------------------------------------------------------------------------------------------------------------
template < class PlayerType >
inline void CBot< PlayerType >::BotThink( void )
{
float g_flBotFullThinkInterval = 1.0 / 15.0; // full AI at lower frequency (was 10 in GoldSrc)
Upkeep();
if (gpGlobals->curtime >= m_flNextFullBotThink)
{
m_flNextFullBotThink = gpGlobals->curtime + g_flBotFullThinkInterval;
ResetCommand();
Update();
}
UpdatePlayer();
}
*/
//--------------------------------------------------------------------------------------------------------------
template < class PlayerType >
inline void CBot< PlayerType >::MoveForward( void )
{
m_forwardSpeed = GetMoveSpeed();
SETBITS( m_buttonFlags, IN_FORWARD );
// make mutually exclusive
CLEARBITS( m_buttonFlags, IN_BACK );
}
//--------------------------------------------------------------------------------------------------------------
template < class PlayerType >
inline void CBot< PlayerType >::MoveBackward( void )
{
m_forwardSpeed = -GetMoveSpeed();
SETBITS( m_buttonFlags, IN_BACK );
// make mutually exclusive
CLEARBITS( m_buttonFlags, IN_FORWARD );
}
//--------------------------------------------------------------------------------------------------------------
template < class PlayerType >
inline void CBot< PlayerType >::StrafeLeft( void )
{
m_strafeSpeed = -GetMoveSpeed();
SETBITS( m_buttonFlags, IN_MOVELEFT );
// make mutually exclusive
CLEARBITS( m_buttonFlags, IN_MOVERIGHT );
}
//--------------------------------------------------------------------------------------------------------------
template < class PlayerType >
inline void CBot< PlayerType >::StrafeRight( void )
{
m_strafeSpeed = GetMoveSpeed();
SETBITS( m_buttonFlags, IN_MOVERIGHT );
// make mutually exclusive
CLEARBITS( m_buttonFlags, IN_MOVELEFT );
}
//--------------------------------------------------------------------------------------------------------------
template < class PlayerType >
inline bool CBot< PlayerType >::Jump( bool mustJump )
{
if (IsJumping() || IsCrouching())
return false;
if (!mustJump)
{
const float minJumpInterval = 0.9f; // 1.5f;
if (gpGlobals->curtime - m_jumpTimestamp < minJumpInterval)
return false;
}
// still need sanity check for jumping frequency
const float sanityInterval = 0.3f;
if (gpGlobals->curtime - m_jumpTimestamp < sanityInterval)
return false;
// jump
SETBITS( m_buttonFlags, IN_JUMP );
m_jumpTimestamp = gpGlobals->curtime;
return true;
}
//--------------------------------------------------------------------------------------------------------------
/**
* Zero any MoveForward(), Jump(), etc
*/
template < class PlayerType >
void CBot< PlayerType >::ClearMovement( void )
{
m_forwardSpeed = 0.0;
m_strafeSpeed = 0.0;
m_verticalSpeed = 100.0; // stay at the top of water, so we don't drown. TODO: swim logic
m_buttonFlags &= ~(IN_FORWARD | IN_BACK | IN_LEFT | IN_RIGHT | IN_JUMP);
}
//--------------------------------------------------------------------------------------------------------------
/**
* Returns true if we are in the midst of a jump
*/
template < class PlayerType >
inline bool CBot< PlayerType >::IsJumping( void )
{
// if long time after last jump, we can't be jumping
if (gpGlobals->curtime - m_jumpTimestamp > 3.0f)
return false;
// if we just jumped, we're still jumping
if (gpGlobals->curtime - m_jumpTimestamp < 0.9f) // 1.0f
return true;
// a little after our jump, we're jumping until we hit the ground
if (FBitSet( this->GetFlags(), FL_ONGROUND ))
return false;
return true;
}
//--------------------------------------------------------------------------------------------------------------
template < class PlayerType >
inline void CBot< PlayerType >::Crouch( void )
{
m_isCrouching = true;
}
//--------------------------------------------------------------------------------------------------------------
template < class PlayerType >
inline void CBot< PlayerType >::StandUp( void )
{
m_isCrouching = false;
}
//--------------------------------------------------------------------------------------------------------------
template < class PlayerType >
inline void CBot< PlayerType >::UseEnvironment( void )
{
SETBITS( m_buttonFlags, IN_USE );
}
//--------------------------------------------------------------------------------------------------------------
template < class PlayerType >
inline void CBot< PlayerType >::PrimaryAttack( void )
{
SETBITS( m_buttonFlags, IN_ATTACK );
}
//--------------------------------------------------------------------------------------------------------------
template < class PlayerType >
inline void CBot< PlayerType >::ClearPrimaryAttack( void )
{
CLEARBITS( m_buttonFlags, IN_ATTACK );
}
//--------------------------------------------------------------------------------------------------------------
template < class PlayerType >
inline void CBot< PlayerType >::TogglePrimaryAttack( void )
{
if (FBitSet( m_buttonFlags, IN_ATTACK ))
{
CLEARBITS( m_buttonFlags, IN_ATTACK );
}
else
{
SETBITS( m_buttonFlags, IN_ATTACK );
}
}
//--------------------------------------------------------------------------------------------------------------
template < class PlayerType >
inline void CBot< PlayerType >::SecondaryAttack( void )
{
SETBITS( m_buttonFlags, IN_ATTACK2 );
}
//--------------------------------------------------------------------------------------------------------------
template < class PlayerType >
inline void CBot< PlayerType >::Reload( void )
{
SETBITS( m_buttonFlags, IN_RELOAD );
}
//--------------------------------------------------------------------------------------------------------------
/**
* Returns ratio of ammo left to max ammo (1 = full clip, 0 = empty)
*/
template < class PlayerType >
inline float CBot< PlayerType >::GetActiveWeaponAmmoRatio( void ) const
{
CWeaponCSBase *weapon = this->GetActiveCSWeapon();
if (weapon == NULL)
return 0.0f;
// weapons with no ammo are always full
if (weapon->Clip1() < 0)
return 1.0f;
return (float)weapon->Clip1() / (float)weapon->GetMaxClip1();
}
//--------------------------------------------------------------------------------------------------------------
/**
* Return true if active weapon has an empty clip
*/
template < class PlayerType >
inline bool CBot< PlayerType >::IsActiveWeaponClipEmpty( void ) const
{
CWeaponCSBase *gun = this->GetActiveCSWeapon();
if (gun && gun->Clip1() == 0)
return true;
return false;
}
//--------------------------------------------------------------------------------------------------------------
/**
* Return true if active weapon has no ammo at all
*/
template < class PlayerType >
inline bool CBot< PlayerType >::IsActiveWeaponOutOfAmmo( void ) const
{
CWeaponCSBase *weapon = this->GetActiveCSWeapon();
if (weapon == NULL)
return true;
return !weapon->HasAnyAmmo();
}
//--------------------------------------------------------------------------------------------------------------
/**
* Return true if looking thru weapon's scope
*/
template < class PlayerType >
inline bool CBot< PlayerType >::IsUsingScope( void )
{
// if our field of view is less than 90, we're looking thru a scope (maybe only true for CS...)
if (this->GetFOV() < this->GetDefaultFOV())
return true;
return false;
}
//--------------------------------------------------------------------------------------------------------------
/**
* Fill in a CUserCmd with our data
*/
template < class PlayerType >
inline void CBot< PlayerType >::BuildUserCmd( CUserCmd& cmd, const QAngle& viewangles, float forwardmove, float sidemove, float upmove, int buttons, byte impulse )
{
Q_memset( &cmd, 0, sizeof( cmd ) );
cmd.command_number = gpGlobals->tickcount;
cmd.forwardmove = forwardmove;
cmd.sidemove = sidemove;
cmd.upmove = upmove;
cmd.buttons = buttons;
cmd.impulse = impulse;
VectorCopy( viewangles, cmd.viewangles );
cmd.random_seed = random->RandomInt( 0, 0x7fffffff );
}
//--------------------------------------------------------------------------------------------------------------
/**
* Update player physics, movement, weapon firing commands, etc
*/
template < class PlayerType >
inline void CBot< PlayerType >::UpdatePlayer( void )
{
if (m_isCrouching)
{
SETBITS( m_buttonFlags, IN_DUCK );
}
else if (!m_isRunning)
{
SETBITS( m_buttonFlags, IN_SPEED );
}
if ( this->IsEFlagSet(EFL_BOT_FROZEN) )
{
m_buttonFlags = 0; // Freeze.
m_forwardSpeed = 0;
m_strafeSpeed = 0;
m_verticalSpeed = 0;
}
// Fill in a CUserCmd with our data
this->BuildUserCmd( m_userCmd, this->EyeAngles(), m_forwardSpeed, m_strafeSpeed, m_verticalSpeed, m_buttonFlags, 0 );
// Save off the CUserCmd to execute later
this->ProcessUsercmds( &m_userCmd, 1, 1, 0, false );
}
//--------------------------------------------------------------------------------------------------------------
template < class PlayerType >
inline void CBot< PlayerType >::ResetCommand( void )
{
m_forwardSpeed = 0.0;
m_strafeSpeed = 0.0;
m_verticalSpeed = 100.0; // stay at the top of water, so we don't drown. TODO: swim logic
m_buttonFlags = 0;
}
//--------------------------------------------------------------------------------------------------------------
/*
template < class PlayerType >
inline byte CBot< PlayerType >::ThrottledMsec( void ) const
{
int iNewMsec;
// Estimate Msec to use for this command based on time passed from the previous command
iNewMsec = (int)( (gpGlobals->curtime - m_flPreviousCommandTime) * 1000 );
if (iNewMsec > 255) // Doh, bots are going to be slower than they should if this happens.
iNewMsec = 255; // Upgrade that CPU or use less bots!
return (byte)iNewMsec;
}
*/
//--------------------------------------------------------------------------------------------------------------
/**
* Do a "client command" - useful for invoking menu choices, etc.
*/
template < class PlayerType >
inline bool CBot< PlayerType >::ClientCommand( const CCommand &args )
{
// Remove old args
int i;
for ( i=0; i<m_args.Count(); ++i )
{
delete[] m_args[i];
}
m_args.RemoveAll();
// parse individual args
const char *cmd = args.GetCommandString();
while (1)
{
// skip whitespace up to a /n
while (*cmd && *cmd <= ' ' && *cmd != '\n')
{
cmd++;
}
if (*cmd == '\n')
{ // a newline seperates commands in the buffer
cmd++;
break;
}
if (!*cmd)
break;
cmd = SharedParse (cmd);
if (!cmd)
break;
m_args.AddToTail( CloneString( SharedGetToken() ) );
}
// and pass to the base class
return PlayerType::ClientCommand( args );
}
//--------------------------------------------------------------------------------------------------------------
/**
* Returns the number of tokens in the command string
*/
template < class PlayerType >
inline int CBot< PlayerType >::Cmd_Argc()
{
return m_args.Count();
}
//--------------------------------------------------------------------------------------------------------------
/**
* Retrieves a specified token
*/
template < class PlayerType >
inline char * CBot< PlayerType >::Cmd_Argv( int argc )
{
if ( argc < 0 || argc >= m_args.Count() )
return NULL;
return m_args[argc];
}
//--------------------------------------------------------------------------------------------------------------
/**
* Returns TRUE if given entity is our enemy
*/
template < class PlayerType >
inline bool CBot< PlayerType >::IsEnemy( CBaseEntity *ent ) const
{
// only Players (real and AI) can be enemies
if (!ent->IsPlayer())
return false;
// corpses are no threat
if (!ent->IsAlive())
return false;
CBasePlayer *player = static_cast<CBasePlayer *>( ent );
// if they are on our team, they are our friends
if (player->GetTeamNumber() == this->GetTeamNumber())
return false;
// yep, we hate 'em
return true;
}
//--------------------------------------------------------------------------------------------------------------
/**
* Return number of enemies left alive
*/
template < class PlayerType >
inline int CBot< PlayerType >::GetEnemiesRemaining( void ) const
{
int count = 0;
for ( int i = 1; i <= gpGlobals->maxClients; ++i )
{
CBaseEntity *player = UTIL_PlayerByIndex( i );
if (player == NULL)
continue;
if (!IsEnemy( player ))
continue;
if (!player->IsAlive())
continue;
count++;
}
return count;
}
//--------------------------------------------------------------------------------------------------------------
/**
* Return number of friends left alive
*/
template < class PlayerType >
inline int CBot< PlayerType >::GetFriendsRemaining( void ) const
{
int count = 0;
for ( int i = 1; i <= gpGlobals->maxClients; ++i )
{
CBaseEntity *player = UTIL_PlayerByIndex( i );
if (player == NULL)
continue;
if (IsEnemy( player ))
continue;
if (!player->IsAlive())
continue;
if (player == static_cast<CBaseEntity *>( const_cast<CBot *>( this ) ))
continue;
count++;
}
return count;
}
//--------------------------------------------------------------------------------------------------------------
/**
* Return true if the local player is currently in observer mode watching this bot.
*/
template < class PlayerType >
inline bool CBot< PlayerType >::IsLocalPlayerWatchingMe( void ) const
{
if ( engine->IsDedicatedServer() )
return false;
CBasePlayer *player = UTIL_GetListenServerHost();
if ( player == NULL )
return false;
if ( cv_bot_debug_target.GetInt() > 0 )
{
return this->entindex() == cv_bot_debug_target.GetInt();
}
if ( player->IsObserver() || !player->IsAlive() )
{
if ( const_cast< CBot< PlayerType > * >(this) == player->GetObserverTarget() )
{
switch( player->GetObserverMode() )
{
case OBS_MODE_IN_EYE:
case OBS_MODE_CHASE:
return true;
}
}
}
return false;
}
//--------------------------------------------------------------------------------------------------------------
/**
* Output message to console if we are being watched by the local player
*/
template < class PlayerType >
inline void CBot< PlayerType >::PrintIfWatched( PRINTF_FORMAT_STRING const char *format, ... ) const
{
if (cv_bot_debug.GetInt() == 0)
{
return;
}
if ((IsLocalPlayerWatchingMe() && (cv_bot_debug.GetInt() == 1 || cv_bot_debug.GetInt() == 3)) ||
(cv_bot_debug.GetInt() == 2 || cv_bot_debug.GetInt() == 4))
{
va_list varg;
char buffer[ CBotManager::MAX_DBG_MSG_SIZE ];
const char *name = const_cast< CBot< PlayerType > * >( this )->GetPlayerName();
va_start( varg, format );
vsprintf( buffer, format, varg );
va_end( varg );
// prefix the console message with the bot's name (this can be NULL if bot was just added)
ClientPrint( UTIL_GetListenServerHost(),
HUD_PRINTCONSOLE,
UTIL_VarArgs( "%s: %s",
(name) ? name : "(NULL netname)", buffer ) );
TheBots->AddDebugMessage( buffer );
}
}
//-----------------------------------------------------------------------------------------------------------
//-----------------------------------------------------------------------------------------------------------
extern void InstallBotControl( void );
extern void RemoveBotControl( void );
extern void Bot_ServerCommand( void );
extern void Bot_RegisterCvars( void );
extern bool IsSpotOccupied( CBaseEntity *me, const Vector &pos ); // if a player is at the given spot, return true
extern const Vector *FindNearbyHidingSpot( CBaseEntity *me, const Vector &pos, float maxRange = 1000.0f, bool isSniper = false, bool useNearest = false );
extern const Vector *FindRandomHidingSpot( CBaseEntity *me, Place place, bool isSniper = false );
extern const Vector *FindNearbyRetreatSpot( CBaseEntity *me, const Vector &start, float maxRange = 1000.0f, int avoidTeam = 0 );
#endif // BOT_H
|