forked from ValveSoftware/source-sdk-2013
-
Notifications
You must be signed in to change notification settings - Fork 165
Expand file tree
/
Copy pathNextBotPlayer.h
More file actions
941 lines (745 loc) · 28.2 KB
/
NextBotPlayer.h
File metadata and controls
941 lines (745 loc) · 28.2 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
// NextBotPlayer.h
// A CBasePlayer bot based on the NextBot technology
// Author: Michael Booth, November 2005
//========= Copyright Valve Corporation, All rights reserved. ============//
#ifndef _NEXT_BOT_PLAYER_H_
#define _NEXT_BOT_PLAYER_H_
#include "cbase.h"
#include "gameinterface.h"
#include "NextBot.h"
#include "Path/NextBotPathFollow.h"
//#include "NextBotPlayerBody.h"
#include "NextBotBehavior.h"
#include "in_buttons.h"
extern ConVar NextBotPlayerStop;
extern ConVar NextBotPlayerWalk;
extern ConVar NextBotPlayerCrouch;
extern ConVar NextBotPlayerMove;
//--------------------------------------------------------------------------------------------------
/**
* Instantiate a NextBot derived from CBasePlayer and spawn it into the environment.
* Assumes class T is derived from CBasePlayer, and has the following method that
* creates a new entity of type T and returns it:
*
* static CBasePlayer *T::AllocatePlayerEntity( edict_t *pEdict, const char *playerName )
*
*/
template < typename T >
T * NextBotCreatePlayerBot( const char *name, bool bReportFakeClient = true )
{
/*
if ( UTIL_ClientsInGame() >= gpGlobals->maxClients )
{
Msg( "CreatePlayerBot: Failed - server is full (%d/%d clients).\n", UTIL_ClientsInGame(), gpGlobals->maxClients );
return NULL;
}
*/
// This is a "back door" for allocating a custom player bot entity when
// the engine calls ClientPutInServer (from CreateFakeClient)
ClientPutInServerOverride( T::AllocatePlayerEntity );
// create the bot and spawn it into the environment
edict_t *botEdict = engine->CreateFakeClientEx( name, bReportFakeClient );
// close the "back door"
ClientPutInServerOverride( NULL );
if ( botEdict == NULL )
{
Msg( "CreatePlayerBot: Unable to create bot %s - CreateFakeClient() returned NULL.\n", name );
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( "CreatePlayerBot: Could not Instance() from the bot edict.\n" );
return NULL;
}
bot->SetPlayerName( name );
// flag this as a fakeclient (bot)
bot->ClearFlags();
bot->AddFlag( FL_CLIENT | FL_FAKECLIENT );
return bot;
}
//--------------------------------------------------------------------------------------------------
/**
* Interface to access player input buttons.
* Unless a duration is given, each button is released at the start of the next frame.
* The release methods allow releasing a button before its duration has elapsed.
*/
class INextBotPlayerInput
{
public:
virtual void PressFireButton( float duration = -1.0f ) = 0;
virtual void ReleaseFireButton( void ) = 0;
virtual void PressAltFireButton( float duration = -1.0f ) = 0;
virtual void ReleaseAltFireButton( void ) = 0;
virtual void PressMeleeButton( float duration = -1.0f ) = 0;
virtual void ReleaseMeleeButton( void ) = 0;
virtual void PressSpecialFireButton( float duration = -1.0f ) = 0;
virtual void ReleaseSpecialFireButton( void ) = 0;
virtual void PressUseButton( float duration = -1.0f ) = 0;
virtual void ReleaseUseButton( void ) = 0;
virtual void PressReloadButton( float duration = -1.0f ) = 0;
virtual void ReleaseReloadButton( void ) = 0;
virtual void PressForwardButton( float duration = -1.0f ) = 0;
virtual void ReleaseForwardButton( void ) = 0;
virtual void PressBackwardButton( float duration = -1.0f ) = 0;
virtual void ReleaseBackwardButton( void ) = 0;
virtual void PressLeftButton( float duration = -1.0f ) = 0;
virtual void ReleaseLeftButton( void ) = 0;
virtual void PressRightButton( float duration = -1.0f ) = 0;
virtual void ReleaseRightButton( void ) = 0;
virtual void PressJumpButton( float duration = -1.0f ) = 0;
virtual void ReleaseJumpButton( void ) = 0;
virtual void PressCrouchButton( float duration = -1.0f ) = 0;
virtual void ReleaseCrouchButton( void ) = 0;
virtual void PressWalkButton( float duration = -1.0f ) = 0;
virtual void ReleaseWalkButton( void ) = 0;
virtual void SetButtonScale( float forward, float right ) = 0;
};
//--------------------------------------------------------------------------------------------------
/**
* Drive a CBasePlayer-derived entity via NextBot logic
*/
template < typename PlayerType >
class NextBotPlayer : public PlayerType, public INextBot, public INextBotPlayerInput
{
public:
DECLARE_CLASS( NextBotPlayer, PlayerType );
NextBotPlayer( void );
virtual ~NextBotPlayer();
virtual void Spawn( void );
virtual void OnRestore( void );
virtual void SetSpawnPoint( CBaseEntity *spawnPoint ); // define place in environment where bot will (re)spawn
virtual CBaseEntity *EntSelectSpawnPoint( void );
virtual void PhysicsSimulate( void );
virtual bool IsNetClient( void ) const { return false; } // Bots should return FALSE for this, they can't receive NET messages
virtual bool IsFakeClient( void ) const { return true; }
virtual bool IsBot( void ) const { return true; }
virtual INextBot *MyNextBotPointer( void ) { return this; }
// this is valid because the templatized PlayerType must be derived from CBasePlayer, which is derived from CBaseCombatCharacter
virtual CBaseCombatCharacter *GetEntity( void ) const { return ( PlayerType * )this; }
virtual bool IsRemovedOnReset( void ) const { return false; } // remove this bot when the NextBot manager calls Reset
virtual bool IsDormantWhenDead( void ) const { return true; } // should this player-bot continue to update itself when dead (respawn logic, etc)
// allocate a bot and bind it to the edict
static CBasePlayer *AllocatePlayerEntity( edict_t *edict, const char *playerName );
//------------------------------------------------------------------------
// utility methods
float GetDistanceBetween( CBaseEntity *other ) const; // return distance between us and the given entity
bool IsDistanceBetweenLessThan( CBaseEntity *other, float range ) const; // return true if distance between is less than the given value
bool IsDistanceBetweenGreaterThan( CBaseEntity *other, float range ) const; // return true if distance between is greater than the given value
float GetDistanceBetween( const Vector &target ) const; // return distance between us and the given entity
bool IsDistanceBetweenLessThan( const Vector &target, float range ) const; // return true if distance between is less than the given value
bool IsDistanceBetweenGreaterThan( const Vector &target, float range ) const; // return true if distance between is greater than the given value
//------------------------------------------------------------------------
// INextBotPlayerInput
virtual void PressFireButton( float duration = -1.0f );
virtual void ReleaseFireButton( void );
virtual void PressAltFireButton( float duration = -1.0f );
virtual void ReleaseAltFireButton( void );
virtual void PressMeleeButton( float duration = -1.0f );
virtual void ReleaseMeleeButton( void );
virtual void PressSpecialFireButton( float duration = -1.0f );
virtual void ReleaseSpecialFireButton( void );
virtual void PressUseButton( float duration = -1.0f );
virtual void ReleaseUseButton( void );
virtual void PressReloadButton( float duration = -1.0f );
virtual void ReleaseReloadButton( void );
virtual void PressForwardButton( float duration = -1.0f );
virtual void ReleaseForwardButton( void );
virtual void PressBackwardButton( float duration = -1.0f );
virtual void ReleaseBackwardButton( void );
virtual void PressLeftButton( float duration = -1.0f );
virtual void ReleaseLeftButton( void );
virtual void PressRightButton( float duration = -1.0f );
virtual void ReleaseRightButton( void );
virtual void PressJumpButton( float duration = -1.0f );
virtual void ReleaseJumpButton( void );
virtual void PressCrouchButton( float duration = -1.0f );
virtual void ReleaseCrouchButton( void );
virtual void PressWalkButton( float duration = -1.0f );
virtual void ReleaseWalkButton( void );
virtual void SetButtonScale( float forward, float right );
//------------------------------------------------------------------------
// Event hooks into NextBot system
virtual int OnTakeDamage_Alive( const CTakeDamageInfo &info );
virtual int OnTakeDamage_Dying( const CTakeDamageInfo &info );
virtual void Event_Killed( const CTakeDamageInfo &info );
virtual void HandleAnimEvent( animevent_t *event );
virtual void OnNavAreaChanged( CNavArea *enteredArea, CNavArea *leftArea ); // invoked (by UpdateLastKnownArea) when we enter a new nav area (or it is reset to NULL)
virtual void Touch( CBaseEntity *other );
virtual void Weapon_Equip( CBaseCombatWeapon *weapon ); // for OnPickUp
virtual void Weapon_Drop( CBaseCombatWeapon *weapon, const Vector *target, const Vector *velocity ); // for OnDrop
virtual void OnMainActivityComplete( Activity newActivity, Activity oldActivity );
virtual void OnMainActivityInterrupted( Activity newActivity, Activity oldActivity );
//------------------------------------------------------------------------
bool IsAbleToAutoCenterOnLadders( void ) const;
virtual void AvoidPlayers( CUserCmd *pCmd ) { } // some game types allow players to pass through each other, this method pushes them apart
public:
// begin INextBot ------------------------------------------------------------------------------------------------------------------
virtual void Update( void ); // (EXTEND) update internal state
protected:
int m_inputButtons; // this is still needed to guarantee each button press is captured at least once
int m_prevInputButtons;
CountdownTimer m_fireButtonTimer;
CountdownTimer m_meleeButtonTimer;
CountdownTimer m_specialFireButtonTimer;
CountdownTimer m_useButtonTimer;
CountdownTimer m_reloadButtonTimer;
CountdownTimer m_forwardButtonTimer;
CountdownTimer m_backwardButtonTimer;
CountdownTimer m_leftButtonTimer;
CountdownTimer m_rightButtonTimer;
CountdownTimer m_jumpButtonTimer;
CountdownTimer m_crouchButtonTimer;
CountdownTimer m_walkButtonTimer;
CountdownTimer m_buttonScaleTimer;
IntervalTimer m_burningTimer; // how long since we were last burning
float m_forwardScale;
float m_rightScale;
CHandle< CBaseEntity > m_spawnPointEntity;
};
template < typename PlayerType >
inline void NextBotPlayer< PlayerType >::SetSpawnPoint( CBaseEntity *spawnPoint )
{
m_spawnPointEntity = spawnPoint;
}
template < typename PlayerType >
inline CBaseEntity *NextBotPlayer< PlayerType >::EntSelectSpawnPoint( void )
{
if ( m_spawnPointEntity != NULL )
return m_spawnPointEntity;
return BaseClass::EntSelectSpawnPoint();
}
template < typename PlayerType >
inline float NextBotPlayer< PlayerType >::GetDistanceBetween( CBaseEntity *other ) const
{
return (this->GetAbsOrigin() - other->GetAbsOrigin()).Length();
}
template < typename PlayerType >
inline bool NextBotPlayer< PlayerType >::IsDistanceBetweenLessThan( CBaseEntity *other, float range ) const
{
return (this->GetAbsOrigin() - other->GetAbsOrigin()).IsLengthLessThan( range );
}
template < typename PlayerType >
inline bool NextBotPlayer< PlayerType >::IsDistanceBetweenGreaterThan( CBaseEntity *other, float range ) const
{
return (this->GetAbsOrigin() - other->GetAbsOrigin()).IsLengthGreaterThan( range );
}
template < typename PlayerType >
inline float NextBotPlayer< PlayerType >::GetDistanceBetween( const Vector &target ) const
{
return (this->GetAbsOrigin() - target).Length();
}
template < typename PlayerType >
inline bool NextBotPlayer< PlayerType >::IsDistanceBetweenLessThan( const Vector &target, float range ) const
{
return (this->GetAbsOrigin() - target).IsLengthLessThan( range );
}
template < typename PlayerType >
inline bool NextBotPlayer< PlayerType >::IsDistanceBetweenGreaterThan( const Vector &target, float range ) const
{
return (this->GetAbsOrigin() - target).IsLengthGreaterThan( range );
}
template < typename PlayerType >
inline void NextBotPlayer< PlayerType >::PressFireButton( float duration )
{
m_inputButtons |= IN_ATTACK;
m_fireButtonTimer.Start( duration );
}
template < typename PlayerType >
inline void NextBotPlayer< PlayerType >::ReleaseFireButton( void )
{
m_inputButtons &= ~IN_ATTACK;
m_fireButtonTimer.Invalidate();
}
template < typename PlayerType >
inline void NextBotPlayer< PlayerType >::PressAltFireButton( float duration )
{
PressMeleeButton( duration );
}
template < typename PlayerType >
inline void NextBotPlayer< PlayerType >::ReleaseAltFireButton( void )
{
ReleaseMeleeButton();
}
template < typename PlayerType >
inline void NextBotPlayer< PlayerType >::PressMeleeButton( float duration )
{
m_inputButtons |= IN_ATTACK2;
m_meleeButtonTimer.Start( duration );
}
template < typename PlayerType >
inline void NextBotPlayer< PlayerType >::ReleaseMeleeButton( void )
{
m_inputButtons &= ~IN_ATTACK2;
m_meleeButtonTimer.Invalidate();
}
template < typename PlayerType >
inline void NextBotPlayer< PlayerType >::PressSpecialFireButton( float duration )
{
m_inputButtons |= IN_ATTACK3;
m_specialFireButtonTimer.Start( duration );
}
template < typename PlayerType >
inline void NextBotPlayer< PlayerType >::ReleaseSpecialFireButton( void )
{
m_inputButtons &= ~IN_ATTACK3;
m_specialFireButtonTimer.Invalidate();
}
template < typename PlayerType >
inline void NextBotPlayer< PlayerType >::PressUseButton( float duration )
{
m_inputButtons |= IN_USE;
m_useButtonTimer.Start( duration );
}
template < typename PlayerType >
inline void NextBotPlayer< PlayerType >::ReleaseUseButton( void )
{
m_inputButtons &= ~IN_USE;
m_useButtonTimer.Invalidate();
}
template < typename PlayerType >
inline void NextBotPlayer< PlayerType >::PressReloadButton( float duration )
{
m_inputButtons |= IN_RELOAD;
m_reloadButtonTimer.Start( duration );
}
template < typename PlayerType >
inline void NextBotPlayer< PlayerType >::ReleaseReloadButton( void )
{
m_inputButtons &= ~IN_RELOAD;
m_reloadButtonTimer.Invalidate();
}
template < typename PlayerType >
inline void NextBotPlayer< PlayerType >::PressJumpButton( float duration )
{
m_inputButtons |= IN_JUMP;
m_jumpButtonTimer.Start( duration );
}
template < typename PlayerType >
inline void NextBotPlayer< PlayerType >::ReleaseJumpButton( void )
{
m_inputButtons &= ~IN_JUMP;
m_jumpButtonTimer.Invalidate();
}
template < typename PlayerType >
inline void NextBotPlayer< PlayerType >::PressCrouchButton( float duration )
{
m_inputButtons |= IN_DUCK;
m_crouchButtonTimer.Start( duration );
}
template < typename PlayerType >
inline void NextBotPlayer< PlayerType >::ReleaseCrouchButton( void )
{
m_inputButtons &= ~IN_DUCK;
m_crouchButtonTimer.Invalidate();
}
template < typename PlayerType >
inline void NextBotPlayer< PlayerType >::PressWalkButton( float duration )
{
m_inputButtons |= IN_SPEED;
m_walkButtonTimer.Start( duration );
}
template < typename PlayerType >
inline void NextBotPlayer< PlayerType >::ReleaseWalkButton( void )
{
m_inputButtons &= ~IN_SPEED;
m_walkButtonTimer.Invalidate();
}
template < typename PlayerType >
inline void NextBotPlayer< PlayerType >::PressForwardButton( float duration )
{
m_inputButtons |= IN_FORWARD;
m_forwardButtonTimer.Start( duration );
}
template < typename PlayerType >
inline void NextBotPlayer< PlayerType >::ReleaseForwardButton( void )
{
m_inputButtons &= ~IN_FORWARD;
m_forwardButtonTimer.Invalidate();
}
template < typename PlayerType >
inline void NextBotPlayer< PlayerType >::PressBackwardButton( float duration )
{
m_inputButtons |= IN_BACK;
m_backwardButtonTimer.Start( duration );
}
template < typename PlayerType >
inline void NextBotPlayer< PlayerType >::ReleaseBackwardButton( void )
{
m_inputButtons &= ~IN_BACK;
m_backwardButtonTimer.Invalidate();
}
template < typename PlayerType >
inline void NextBotPlayer< PlayerType >::PressLeftButton( float duration )
{
m_inputButtons |= IN_MOVELEFT;
m_leftButtonTimer.Start( duration );
}
template < typename PlayerType >
inline void NextBotPlayer< PlayerType >::ReleaseLeftButton( void )
{
m_inputButtons &= ~IN_MOVELEFT;
m_leftButtonTimer.Invalidate();
}
template < typename PlayerType >
inline void NextBotPlayer< PlayerType >::PressRightButton( float duration )
{
m_inputButtons |= IN_MOVERIGHT;
m_rightButtonTimer.Start( duration );
}
template < typename PlayerType >
inline void NextBotPlayer< PlayerType >::ReleaseRightButton( void )
{
m_inputButtons &= ~IN_MOVERIGHT;
m_rightButtonTimer.Invalidate();
}
template < typename PlayerType >
inline void NextBotPlayer< PlayerType >::SetButtonScale( float forward, float right )
{
m_forwardScale = forward;
m_rightScale = right;
m_buttonScaleTimer.Start( 0.01 );
}
//-----------------------------------------------------------------------------------------------------
template < typename PlayerType >
inline NextBotPlayer< PlayerType >::NextBotPlayer( void )
{
m_prevInputButtons = 0;
m_inputButtons = 0;
m_burningTimer.Invalidate();
m_spawnPointEntity = NULL;
}
//-----------------------------------------------------------------------------------------------------
template < typename PlayerType >
inline NextBotPlayer< PlayerType >::~NextBotPlayer()
{
}
//-----------------------------------------------------------------------------------------------------
template < typename PlayerType >
inline void NextBotPlayer< PlayerType >::Spawn( void )
{
engine->SetFakeClientConVarValue( this->edict(), "cl_autohelp", "0" );
m_prevInputButtons = m_inputButtons = 0;
m_fireButtonTimer.Invalidate();
m_meleeButtonTimer.Invalidate();
m_specialFireButtonTimer.Invalidate();
m_useButtonTimer.Invalidate();
m_reloadButtonTimer.Invalidate();
m_forwardButtonTimer.Invalidate();
m_backwardButtonTimer.Invalidate();
m_leftButtonTimer.Invalidate();
m_rightButtonTimer.Invalidate();
m_jumpButtonTimer.Invalidate();
m_crouchButtonTimer.Invalidate();
m_walkButtonTimer.Invalidate();
m_buttonScaleTimer.Invalidate();
m_forwardScale = m_rightScale = 0.04;
m_burningTimer.Invalidate();
// reset first, because Spawn() may access various interfaces
INextBot::Reset();
BaseClass::Spawn();
}
//-----------------------------------------------------------------------------------------------------
template < typename PlayerType >
inline void NextBotPlayer< PlayerType >::OnRestore( void )
{
engine->SetFakeClientConVarValue( this->edict(), "cl_autohelp", "0" );
m_prevInputButtons = m_inputButtons = 0;
m_fireButtonTimer.Invalidate();
m_meleeButtonTimer.Invalidate();
m_specialFireButtonTimer.Invalidate();
m_useButtonTimer.Invalidate();
m_reloadButtonTimer.Invalidate();
m_forwardButtonTimer.Invalidate();
m_backwardButtonTimer.Invalidate();
m_leftButtonTimer.Invalidate();
m_rightButtonTimer.Invalidate();
m_jumpButtonTimer.Invalidate();
m_crouchButtonTimer.Invalidate();
m_walkButtonTimer.Invalidate();
m_buttonScaleTimer.Invalidate();
m_forwardScale = m_rightScale = 0.04;
m_burningTimer.Invalidate();
// reset first, because Spawn() may access various interfaces
INextBot::Reset();
BaseClass::OnRestore();
}
//-----------------------------------------------------------------------------------------------------
inline void _NextBot_BuildUserCommand( CUserCmd *cmd, const QAngle &viewangles, float forwardmove, float sidemove, float upmove, int buttons, byte impulse )
{
Q_memset( cmd, 0, sizeof( CUserCmd ) );
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 );
}
//-----------------------------------------------------------------------------------------------------
template < typename PlayerType >
inline void NextBotPlayer< PlayerType >::PhysicsSimulate( void )
{
VPROF( "NextBotPlayer::PhysicsSimulate" );
// Make sure not to simulate this guy twice per frame
if ( PlayerType::m_nSimulationTick == gpGlobals->tickcount )
{
return;
}
if ( engine->IsPaused() )
{
// We're paused - don't add new commands
PlayerType::PhysicsSimulate();
return;
}
if ( ( IsDormantWhenDead() && PlayerType::m_lifeState == LIFE_DEAD ) || NextBotStop.GetBool() )
{
// death animation complete - nothing left to do except let PhysicsSimulate run PreThink etc
PlayerType::PhysicsSimulate();
return;
}
int inputButtons;
//
// Update bot behavior
//
if ( BeginUpdate() )
{
Update();
// build button bits
if ( !m_fireButtonTimer.IsElapsed() )
m_inputButtons |= IN_ATTACK;
if ( !m_meleeButtonTimer.IsElapsed() )
m_inputButtons |= IN_ATTACK2;
if ( !m_specialFireButtonTimer.IsElapsed() )
m_inputButtons |= IN_ATTACK3;
if ( !m_useButtonTimer.IsElapsed() )
m_inputButtons |= IN_USE;
if ( !m_reloadButtonTimer.IsElapsed() )
m_inputButtons |= IN_RELOAD;
if ( !m_forwardButtonTimer.IsElapsed() )
m_inputButtons |= IN_FORWARD;
if ( !m_backwardButtonTimer.IsElapsed() )
m_inputButtons |= IN_BACK;
if ( !m_leftButtonTimer.IsElapsed() )
m_inputButtons |= IN_MOVELEFT;
if ( !m_rightButtonTimer.IsElapsed() )
m_inputButtons |= IN_MOVERIGHT;
if ( !m_jumpButtonTimer.IsElapsed() )
m_inputButtons |= IN_JUMP;
if ( !m_crouchButtonTimer.IsElapsed() )
m_inputButtons |= IN_DUCK;
if ( !m_walkButtonTimer.IsElapsed() )
m_inputButtons |= IN_SPEED;
m_prevInputButtons = m_inputButtons;
inputButtons = m_inputButtons;
EndUpdate();
}
else
{
// HACK: Smooth out body animations
GetBodyInterface()->Update();
// keep buttons pressed between Update() calls (m_prevInputButtons),
// and include any button presses that occurred this tick (m_inputButtons).
inputButtons = m_prevInputButtons | m_inputButtons;
}
//
// Convert NextBot locomotion and posture into
// player commands
//
IBody *body = GetBodyInterface();
ILocomotion *mover = GetLocomotionInterface();
if ( body->IsActualPosture( IBody::CROUCH ) )
{
inputButtons |= IN_DUCK;
}
float forwardSpeed = 0.0f;
float strafeSpeed = 0.0f;
float verticalSpeed = ( m_inputButtons & IN_JUMP ) ? mover->GetRunSpeed() : 0.0f;
if ( inputButtons & IN_FORWARD )
{
forwardSpeed = mover->GetRunSpeed();
}
else if ( inputButtons & IN_BACK )
{
forwardSpeed = -mover->GetRunSpeed();
}
if ( inputButtons & IN_MOVELEFT )
{
strafeSpeed = -mover->GetRunSpeed();
}
else if ( inputButtons & IN_MOVERIGHT )
{
strafeSpeed = mover->GetRunSpeed();
}
if ( NextBotPlayerWalk.GetBool() )
{
inputButtons |= IN_SPEED;
}
if ( NextBotPlayerCrouch.GetBool() )
{
inputButtons |= IN_DUCK;
}
if ( !m_buttonScaleTimer.IsElapsed() )
{
forwardSpeed = mover->GetRunSpeed() * m_forwardScale;
strafeSpeed = mover->GetRunSpeed() * m_rightScale;
}
if ( !NextBotPlayerMove.GetBool() )
{
inputButtons &= ~(IN_FORWARD | IN_BACK | IN_MOVELEFT | IN_MOVERIGHT | IN_JUMP );
forwardSpeed = 0.0f;
strafeSpeed = 0.0f;
verticalSpeed = 0.0f;
}
QAngle angles = this->EyeAngles();
#ifdef TERROR
if ( IsStunned() )
{
inputButtons &= ~(IN_FORWARD | IN_BACK | IN_MOVELEFT | IN_MOVERIGHT | IN_JUMP | IN_DUCK );
}
// "Look" in the direction we're climbing/stumbling etc. We can't do anything anyway, and it
// keeps motion extraction working.
if ( IsRenderYawOverridden() && IsMotionControlledXY( GetMainActivity() ) )
{
angles[YAW] = GetOverriddenRenderYaw();
}
#endif
// construct a "command" to move the player
CUserCmd userCmd;
_NextBot_BuildUserCommand( &userCmd, angles, forwardSpeed, strafeSpeed, verticalSpeed, inputButtons, 0 );
AvoidPlayers( &userCmd );
// allocate a new command and add it to the player's list of command to process
this->ProcessUsercmds( &userCmd, 1, 1, 0, false );
m_inputButtons = 0;
// actually execute player commands and do player physics
PlayerType::PhysicsSimulate();
}
//----------------------------------------------------------------------------------------------------------
template < typename PlayerType >
inline void NextBotPlayer< PlayerType >::OnNavAreaChanged( CNavArea *enteredArea, CNavArea *leftArea )
{
// propagate into NextBot responders
INextBotEventResponder::OnNavAreaChanged( enteredArea, leftArea );
BaseClass::OnNavAreaChanged( enteredArea, leftArea );
}
//----------------------------------------------------------------------------------------------------------
template < typename PlayerType >
inline void NextBotPlayer< PlayerType >::Touch( CBaseEntity *other )
{
if ( ShouldTouch( other ) )
{
// propagate touch into NextBot event responders
trace_t result;
result = this->GetTouchTrace();
OnContact( other, &result );
}
BaseClass::Touch( other );
}
//----------------------------------------------------------------------------------------------------------
template < typename PlayerType >
inline void NextBotPlayer< PlayerType >::Weapon_Equip( CBaseCombatWeapon *weapon )
{
#ifdef TERROR
// TODO: Reimplement GetDroppingPlayer() into GetLastOwner()
OnPickUp( weapon, weapon->GetDroppingPlayer() );
#else
OnPickUp( weapon, NULL );
#endif
BaseClass::Weapon_Equip( weapon );
}
//----------------------------------------------------------------------------------------------------------
template < typename PlayerType >
inline void NextBotPlayer< PlayerType >::Weapon_Drop( CBaseCombatWeapon *weapon, const Vector *target, const Vector *velocity )
{
OnDrop( weapon );
BaseClass::Weapon_Drop( weapon, target, velocity );
}
//--------------------------------------------------------------------------------------------------------
template < typename PlayerType >
inline void NextBotPlayer< PlayerType >::OnMainActivityComplete( Activity newActivity, Activity oldActivity )
{
#ifdef TERROR
BaseClass::OnMainActivityComplete( newActivity, oldActivity );
#endif
OnAnimationActivityComplete( oldActivity );
}
//--------------------------------------------------------------------------------------------------------
template < typename PlayerType >
inline void NextBotPlayer< PlayerType >::OnMainActivityInterrupted( Activity newActivity, Activity oldActivity )
{
#ifdef TERROR
BaseClass::OnMainActivityInterrupted( newActivity, oldActivity );
#endif
OnAnimationActivityInterrupted( oldActivity );
}
//----------------------------------------------------------------------------------------------------------
template < typename PlayerType >
inline void NextBotPlayer< PlayerType >::Update( void )
{
// don't spend CPU updating if this Survivor is dead
if ( ( this->IsAlive() || !IsDormantWhenDead() ) && !NextBotPlayerStop.GetBool() )
{
INextBot::Update();
}
}
//----------------------------------------------------------------------------------------------------------
template < typename PlayerType >
inline bool NextBotPlayer< PlayerType >::IsAbleToAutoCenterOnLadders( void ) const
{
const ILocomotion *locomotion = GetLocomotionInterface();
return locomotion && locomotion->IsAbleToAutoCenterOnLadder();
}
//----------------------------------------------------------------------------------------------------------
template < typename PlayerType >
inline int NextBotPlayer< PlayerType >::OnTakeDamage_Alive( const CTakeDamageInfo &info )
{
if ( info.GetDamageType() & DMG_BURN )
{
if ( !m_burningTimer.HasStarted() || m_burningTimer.IsGreaterThen( 1.0f ) )
{
// emit ignite event periodically as long as we are burning
OnIgnite();
m_burningTimer.Start();
}
}
// propagate event to components
OnInjured( info );
return BaseClass::OnTakeDamage_Alive( info );
}
//----------------------------------------------------------------------------------------------------------
template < typename PlayerType >
inline int NextBotPlayer< PlayerType >::OnTakeDamage_Dying( const CTakeDamageInfo &info )
{
if ( info.GetDamageType() & DMG_BURN )
{
if ( !m_burningTimer.HasStarted() || m_burningTimer.IsGreaterThen( 1.0f ) )
{
// emit ignite event periodically as long as we are burning
OnIgnite();
m_burningTimer.Start();
}
}
// propagate event to components
OnInjured( info );
return BaseClass::OnTakeDamage_Dying( info );
}
//----------------------------------------------------------------------------------------------------------
template < typename PlayerType >
inline void NextBotPlayer< PlayerType >::Event_Killed( const CTakeDamageInfo &info )
{
// propagate event to my components
OnKilled( info );
BaseClass::Event_Killed( info );
}
//----------------------------------------------------------------------------------------------------------
template < typename PlayerType >
inline void NextBotPlayer< PlayerType >::HandleAnimEvent( animevent_t *event )
{
// propagate event to components
OnAnimationEvent( event );
BaseClass::HandleAnimEvent( event );
}
#endif // _NEXT_BOT_PLAYER_H_