-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathNextBotBehavior.h
1936 lines (1605 loc) · 70.4 KB
/
NextBotBehavior.h
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
// NextBotBehaviorEngine.h
// Behavioral system constructed from Actions
// Author: Michael Booth, April 2006
//========= Copyright Valve Corporation, All rights reserved. ============//
#ifndef _BEHAVIOR_ENGINE_H_
#define _BEHAVIOR_ENGINE_H_
#include "fmtstr.h"
#include "NextBotEventResponderInterface.h"
#include "NextBotContextualQueryInterface.h"
#include "NextBotDebug.h"
#include "tier0/vprof.h"
//#define DEBUG_BEHAVIOR_MEMORY
extern ConVar NextBotDebugHistory;
/**
* Notes:
*
* By using return results to cause transitions, we ensure the atomic-ness
* of these transitions. For instance, it is not possible to change to a
* new Action and continue execution of code in the current Action.
*
* Creation and deletion of Actions during transitions allows passing of
* type-safe arguments between Actions via constructors.
*
* Events are propagated to each Action in the hierarchy. If an
* action is suspended for another action, it STILL RECEIVES EVENTS
* that are not handled by the events "above it" in the suspend stack.
* In other words, the active Action gets the first response, and if it
* returns CONTINUE, the Action buried beneath it can process it,
* and so on deeper into the stack of suspended Actions.
*
* About events:
* It is not possible to have event handlers instantaneously change
* state upon return due to out-of-order and recurrence issues, not
* to mention deleting the state out from under itself. Therefore,
* events return DESIRED results, and the highest priority result
* is executed at the next Update().
*
* About buried Actions causing SUSPEND_FOR results:
* If a buried Action reacts to an event by returning a SUSPEND_FOR,
* the new interrupting Action is put at the TOP of the stack, burying
* whatever Action was there.
*
*/
// forward declaration
template < typename Actor > class Action;
/**
* The possible consequences of an Action
*/
enum ActionResultType
{
CONTINUE, // continue executing this action next frame - nothing has changed
CHANGE_TO, // change actions next frame
SUSPEND_FOR, // put the current action on hold for the new action
DONE, // this action has finished, resume suspended action
SUSTAIN, // for use with event handlers - a way to say "It's important to keep doing what I'm doing"
};
//----------------------------------------------------------------------------------------------
/**
* Actions and Event processors return results derived from this class.
* Do not assemble this yourself - use the Continue(), ChangeTo(), Done(), and SuspendFor()
* methods within Action.
*/
template < typename Actor >
struct IActionResult
{
IActionResult( ActionResultType type = CONTINUE, Action< Actor > *action = NULL, const char *reason = NULL )
{
m_type = type;
m_action = action;
m_reason = reason;
}
bool IsDone( void ) const
{
return ( m_type == DONE );
}
bool IsContinue( void ) const
{
return ( m_type == CONTINUE );
}
bool IsRequestingChange( void ) const
{
return ( m_type == CHANGE_TO || m_type == SUSPEND_FOR || m_type == DONE );
}
const char *GetTypeName( void ) const
{
switch ( m_type )
{
case CHANGE_TO: return "CHANGE_TO";
case SUSPEND_FOR: return "SUSPEND_FOR";
case DONE: return "DONE";
case SUSTAIN: return "SUSTAIN";
default:
case CONTINUE: return "CONTINUE";
}
}
ActionResultType m_type;
Action< Actor > *m_action;
const char *m_reason;
};
//----------------------------------------------------------------------------------------------
/**
* When an Action is executed it returns this result.
* Do not assemble this yourself - use the Continue(), ChangeTo(), Done(), and SuspendFor()
* methods within Action.
*/
template < typename Actor >
struct ActionResult : public IActionResult< Actor >
{
// this is derived from IActionResult to ensure that ActionResult and EventDesiredResult cannot be silently converted
ActionResult( ActionResultType type = CONTINUE, Action< Actor > *action = NULL, const char *reason = NULL ) : IActionResult< Actor >( type, action, reason ) { }
};
//----------------------------------------------------------------------------------------------
/**
* When an event is processed, it returns this DESIRED result,
* which may or MAY NOT happen, depending on other event results
* that occur simultaneously.
* Do not assemble this yourself - use the TryContinue(), TryChangeTo(), TryDone(), TrySustain(),
* and TrySuspendFor() methods within Action.
*/
enum EventResultPriorityType
{
RESULT_NONE, // no result
RESULT_TRY, // use this result, or toss it out, either is ok
RESULT_IMPORTANT, // try extra-hard to use this result
RESULT_CRITICAL // this result must be used - emit an error if it can't be
};
template < typename Actor >
struct EventDesiredResult : public IActionResult< Actor >
{
EventDesiredResult( ActionResultType type = CONTINUE, Action< Actor > *action = NULL, EventResultPriorityType priority = RESULT_TRY, const char *reason = NULL ) : IActionResult< Actor >( type, action, reason )
{
m_priority = priority;
}
EventResultPriorityType m_priority;
};
//-------------------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------------------
/**
* A Behavior is the root of an Action hierarchy as well as its container/manager.
* Instantiate a Behavior with the root Action of your behavioral system, and
* call Behavior::Update() to drive it.
*/
template < typename Actor >
class Behavior : public INextBotEventResponder, public IContextualQuery
{
public:
DECLARE_CLASS( Behavior, INextBotEventResponder );
Behavior( Action< Actor > *initialAction, const char *name = "" ) : m_name( "%s", name )
{
m_action = initialAction;
m_me = NULL;
}
virtual ~Behavior()
{
if ( m_me && m_action )
{
// allow all currently active Actions to end
m_action->InvokeOnEnd( m_me, this, NULL );
m_me = NULL;
}
// dig down to the bottom of the action stack and delete
// that, so we don't leak action memory since action
// destructors intentionally don't delete actions
// "buried" underneath them.
Action< Actor > *bottomAction;
for( bottomAction = m_action; bottomAction && bottomAction->m_buriedUnderMe; bottomAction = bottomAction->m_buriedUnderMe )
;
if ( bottomAction )
{
delete bottomAction;
}
// delete any dead Actions
m_deadActionVector.PurgeAndDeleteElements();
}
/**
* Reset this Behavior with the given Action. If this Behavior
* was already running, this will delete all current Actions and
* restart the Behavior with the new one.
*/
void Reset( Action< Actor > *action )
{
if ( m_me && m_action )
{
// allow all currently active Actions to end
m_action->InvokeOnEnd( m_me, this, NULL );
m_me = NULL;
}
// find "bottom" action (see comment in destructor)
Action< Actor > *bottomAction;
for( bottomAction = m_action; bottomAction && bottomAction->m_buriedUnderMe; bottomAction = bottomAction->m_buriedUnderMe )
;
if ( bottomAction )
{
delete bottomAction;
}
// delete any dead Actions
m_deadActionVector.PurgeAndDeleteElements();
m_action = action;
}
/**
* Return true if this Behavior contains no actions
*/
bool IsEmpty( void ) const
{
return m_action == NULL;
}
/**
* Execute this Behavior
*/
void Update( Actor *me, float interval )
{
if ( me == NULL || IsEmpty() )
{
return;
}
m_me = me;
m_action = m_action->ApplyResult( me, this, m_action->InvokeUpdate( me, this, interval ) );
if ( m_action && me->IsDebugging( NEXTBOT_BEHAVIOR ) )
{
CFmtStr msg;
me->DisplayDebugText( msg.sprintf( "%s: %s", GetName(), m_action->DebugString() ) );
}
// delete any dead Actions
m_deadActionVector.PurgeAndDeleteElements();
}
/**
* If this Behavior has not been Update'd in a long time,
* call Resume() to let the system know its internal state may
* be out of date.
*/
void Resume( Actor *me )
{
if ( me == NULL || IsEmpty() )
{
return;
}
m_action = m_action->ApplyResult( me, this, m_action->OnResume( me, NULL ) );
if ( m_action && me->IsDebugging( NEXTBOT_BEHAVIOR ) )
{
CFmtStr msg;
me->DisplayDebugText( msg.sprintf( "%s: %s", GetName(), m_action->DebugString() ) );
}
}
/**
* Use this method to destroy Actions used by this Behavior.
* We cannot delete Actions in-line since Action updates can potentially
* invoke event responders which will then use potentially deleted
* Action pointers, causing memory corruption.
* Instead, we will collect the dead Actions and delete them at the
* end of Update().
*/
void DestroyAction( Action< Actor > *dead )
{
m_deadActionVector.AddToTail( dead );
}
const char *GetName( void ) const
{
return m_name;
}
// INextBotEventResponder propagation ----------------------------------------------------------------------
virtual INextBotEventResponder *FirstContainedResponder( void ) const
{
return m_action;
}
virtual INextBotEventResponder *NextContainedResponder( INextBotEventResponder *current ) const
{
return NULL;
}
// IContextualQuery propagation ----------------------------------------------------------------------------
virtual QueryResultType ShouldPickUp( const INextBot *me, CBaseEntity *item ) const // if the desired item was available right now, should we pick it up?
{
QueryResultType result = ANSWER_UNDEFINED;
if ( m_action )
{
// find innermost child action
Action< Actor > *action;
for( action = m_action; action->m_child; action = action->m_child )
;
// work our way through our containers
while( action && result == ANSWER_UNDEFINED )
{
Action< Actor > *containingAction = action->m_parent;
// work our way up the stack
while( action && result == ANSWER_UNDEFINED )
{
result = action->ShouldPickUp( me, item );
action = action->GetActionBuriedUnderMe();
}
action = containingAction;
}
}
return result;
}
virtual QueryResultType ShouldHurry( const INextBot *me ) const // are we in a hurry?
{
QueryResultType result = ANSWER_UNDEFINED;
if ( m_action )
{
// find innermost child action
Action< Actor > *action;
for( action = m_action; action->m_child; action = action->m_child )
;
// work our way through our containers
while( action && result == ANSWER_UNDEFINED )
{
Action< Actor > *containingAction = action->m_parent;
// work our way up the stack
while( action && result == ANSWER_UNDEFINED )
{
result = action->ShouldHurry( me );
action = action->GetActionBuriedUnderMe();
}
action = containingAction;
}
}
return result;
}
virtual QueryResultType ShouldRetreat( const INextBot *me ) const // is it time to retreat?
{
QueryResultType result = ANSWER_UNDEFINED;
if ( m_action )
{
// find innermost child action
Action< Actor > *action;
for( action = m_action; action->m_child; action = action->m_child )
;
// work our way through our containers
while( action && result == ANSWER_UNDEFINED )
{
Action< Actor > *containingAction = action->m_parent;
// work our way up the stack
while( action && result == ANSWER_UNDEFINED )
{
result = action->ShouldRetreat( me );
action = action->GetActionBuriedUnderMe();
}
action = containingAction;
}
}
return result;
}
virtual QueryResultType ShouldAttack( const INextBot *me, const CKnownEntity *them ) const // should we attack "them"?
{
QueryResultType result = ANSWER_UNDEFINED;
if ( m_action )
{
// find innermost child action
Action< Actor > *action;
for( action = m_action; action->m_child; action = action->m_child )
;
// work our way through our containers
while( action && result == ANSWER_UNDEFINED )
{
Action< Actor > *containingAction = action->m_parent;
// work our way up the stack
while( action && result == ANSWER_UNDEFINED )
{
result = action->ShouldAttack( me, them );
action = action->GetActionBuriedUnderMe();
}
action = containingAction;
}
}
return result;
}
virtual QueryResultType IsHindrance( const INextBot *me, CBaseEntity *blocker ) const // return true if we should wait for 'blocker' that is across our path somewhere up ahead.
{
QueryResultType result = ANSWER_UNDEFINED;
if ( m_action )
{
// find innermost child action
Action< Actor > *action;
for( action = m_action; action->m_child; action = action->m_child )
;
// work our way through our containers
while( action && result == ANSWER_UNDEFINED )
{
Action< Actor > *containingAction = action->m_parent;
// work our way up the stack
while( action && result == ANSWER_UNDEFINED )
{
result = action->IsHindrance( me, blocker );
action = action->GetActionBuriedUnderMe();
}
action = containingAction;
}
}
return result;
}
virtual Vector SelectTargetPoint( const INextBot *me, const CBaseCombatCharacter *subject ) const // given a subject, return the world space position we should aim at
{
Vector result = vec3_origin;
if ( m_action )
{
// find innermost child action
Action< Actor > *action;
for( action = m_action; action->m_child; action = action->m_child )
;
// work our way through our containers
while( action && result == vec3_origin )
{
Action< Actor > *containingAction = action->m_parent;
// work our way up the stack
while( action && result == vec3_origin )
{
result = action->SelectTargetPoint( me, subject );
action = action->GetActionBuriedUnderMe();
}
action = containingAction;
}
}
return result;
}
/**
* Allow bot to approve of positions game movement tries to put him into.
* This is most useful for bots derived from CBasePlayer that go through
* the player movement system.
*/
virtual QueryResultType IsPositionAllowed( const INextBot *me, const Vector &pos ) const
{
QueryResultType result = ANSWER_UNDEFINED;
if ( m_action )
{
// find innermost child action
Action< Actor > *action;
for( action = m_action; action->m_child; action = action->m_child )
;
// work our way through our containers
while( action && result == ANSWER_UNDEFINED )
{
Action< Actor > *containingAction = action->m_parent;
// work our way up the stack
while( action && result == ANSWER_UNDEFINED )
{
result = action->IsPositionAllowed( me, pos );
action = action->GetActionBuriedUnderMe();
}
action = containingAction;
}
}
return result;
}
virtual const CKnownEntity *SelectMoreDangerousThreat( const INextBot *me, const CBaseCombatCharacter *subject, const CKnownEntity *threat1, const CKnownEntity *threat2 ) const // return the more dangerous of the two threats, or NULL if we have no opinion
{
const CKnownEntity *result = NULL;
if ( m_action )
{
// find innermost child action
Action< Actor > *action;
for( action = m_action; action->m_child; action = action->m_child )
;
// work our way through our containers
while( action && result == NULL )
{
Action< Actor > *containingAction = action->m_parent;
// work our way up the stack
while( action && result == NULL )
{
result = action->SelectMoreDangerousThreat( me, subject, threat1, threat2 );
action = action->GetActionBuriedUnderMe();
}
action = containingAction;
}
}
return result;
}
private:
Action< Actor > *m_action;
#define MAX_NAME_LENGTH 32
CFmtStrN< MAX_NAME_LENGTH > m_name;
Actor *m_me;
CUtlVector< Action< Actor > * > m_deadActionVector; // completed Actions pending deletion
};
//----------------------------------------------------------------------------------------------
/**
* Something an Actor does.
* Actions can contain Actions, representing the precise context of the Actor's behavior.
* A system of Actions is contained within a Behavior, which acts as the manager
* of the Action system.
*/
template < typename Actor >
class Action : public INextBotEventResponder, public IContextualQuery
{
public:
DECLARE_CLASS( Action, INextBotEventResponder );
Action( void );
virtual ~Action();
virtual const char *GetName( void ) const = 0; // return name of this action
virtual bool IsNamed( const char *name ) const; // return true if given name matches the name of this Action
virtual const char *GetFullName( void ) const; // return a temporary string showing the full lineage of this one action
Actor *GetActor( void ) const; // return the Actor performing this Action (valid just before OnStart() is invoked)
//-----------------------------------------------------------------------------------------
/**
* Try to start the Action. Result is immediately processed,
* which can cause an immediate transition, another OnStart(), etc.
* An Action can count on each OnStart() being followed (eventually) with an OnEnd().
*/
virtual ActionResult< Actor > OnStart( Actor *me, Action< Actor > *priorAction ) { return Continue(); }
/**
* Do the work of the Action. It is possible for Update to not be
* called between a given OnStart/OnEnd pair due to immediate transitions.
*/
virtual ActionResult< Actor > Update( Actor *me, float interval ) { return Continue(); }
// Invoked when an Action is ended for any reason
virtual void OnEnd( Actor *me, Action< Actor > *nextAction ) { }
/*
* When an Action is suspended by a new action.
* Note that only CONTINUE and DONE are valid results. All other results will
* be considered as a CONTINUE.
*/
virtual ActionResult< Actor > OnSuspend( Actor *me, Action< Actor > *interruptingAction ) { return Continue(); }
// When an Action is resumed after being suspended
virtual ActionResult< Actor > OnResume( Actor *me, Action< Actor > *interruptingAction ) { return Continue(); }
/**
* To cause a state change, use these methods to create an ActionResult to
* return from OnStart, Update, or OnResume.
*/
ActionResult< Actor > Continue( void ) const;
ActionResult< Actor > ChangeTo( Action< Actor > *action, const char *reason = NULL ) const;
ActionResult< Actor > SuspendFor( Action< Actor > *action, const char *reason = NULL ) const;
ActionResult< Actor > Done( const char *reason = NULL ) const;
// create and return an Action to start as sub-action within this Action when it starts
virtual Action< Actor > *InitialContainedAction( Actor *me ) { return NULL; }
//-----------------------------------------------------------------------------------------
/**
* Override the event handler methods below to respond to events that occur during this Action
* NOTE: These are identical to the events in INextBotEventResponder with the addition
* of an actor argument and a return result. Their translators are located in the private area
* below.
*/
virtual EventDesiredResult< Actor > OnLeaveGround( Actor *me, CBaseEntity *ground ) { return TryContinue(); }
virtual EventDesiredResult< Actor > OnLandOnGround( Actor *me, CBaseEntity *ground ) { return TryContinue(); }
virtual EventDesiredResult< Actor > OnContact( Actor *me, CBaseEntity *other, CGameTrace *result = NULL ) { return TryContinue(); }
virtual EventDesiredResult< Actor > OnMoveToSuccess( Actor *me, const Path *path ) { return TryContinue(); }
virtual EventDesiredResult< Actor > OnMoveToFailure( Actor *me, const Path *path, MoveToFailureType reason ) { return TryContinue(); }
virtual EventDesiredResult< Actor > OnStuck( Actor *me ) { return TryContinue(); }
virtual EventDesiredResult< Actor > OnUnStuck( Actor *me ) { return TryContinue(); }
virtual EventDesiredResult< Actor > OnPostureChanged( Actor *me ) { return TryContinue(); }
virtual EventDesiredResult< Actor > OnAnimationActivityComplete( Actor *me, int activity ) { return TryContinue(); }
virtual EventDesiredResult< Actor > OnAnimationActivityInterrupted( Actor *me, int activity ) { return TryContinue(); }
virtual EventDesiredResult< Actor > OnAnimationEvent( Actor *me, animevent_t *event ) { return TryContinue(); }
virtual EventDesiredResult< Actor > OnIgnite( Actor *me ) { return TryContinue(); }
virtual EventDesiredResult< Actor > OnInjured( Actor *me, const CTakeDamageInfo &info ) { return TryContinue(); }
virtual EventDesiredResult< Actor > OnKilled( Actor *me, const CTakeDamageInfo &info ) { return TryContinue(); }
virtual EventDesiredResult< Actor > OnOtherKilled( Actor *me, CBaseCombatCharacter *victim, const CTakeDamageInfo &info ) { return TryContinue(); }
virtual EventDesiredResult< Actor > OnSight( Actor *me, CBaseEntity *subject ) { return TryContinue(); }
virtual EventDesiredResult< Actor > OnLostSight( Actor *me, CBaseEntity *subject ) { return TryContinue(); }
virtual EventDesiredResult< Actor > OnSound( Actor *me, CBaseEntity *source, const Vector &pos, KeyValues *keys ) { return TryContinue(); }
virtual EventDesiredResult< Actor > OnSpokeConcept( Actor *me, CBaseCombatCharacter *who, AIConcept_t concept, AI_Response *response ) { return TryContinue(); }
virtual EventDesiredResult< Actor > OnWeaponFired( Actor *me, CBaseCombatCharacter *whoFired, CBaseCombatWeapon *weapon ) { return TryContinue(); }
virtual EventDesiredResult< Actor > OnNavAreaChanged( Actor *me, CNavArea *newArea, CNavArea *oldArea ) { return TryContinue(); }
virtual EventDesiredResult< Actor > OnModelChanged( Actor *me ) { return TryContinue(); }
virtual EventDesiredResult< Actor > OnPickUp( Actor *me, CBaseEntity *item, CBaseCombatCharacter *giver ) { return TryContinue(); }
virtual EventDesiredResult< Actor > OnDrop( Actor *me, CBaseEntity *item ) { return TryContinue(); }
virtual EventDesiredResult< Actor > OnActorEmoted( Actor *me, CBaseCombatCharacter *emoter, int emote ) { return TryContinue(); }
virtual EventDesiredResult< Actor > OnCommandAttack( Actor *me, CBaseEntity *victim ) { return TryContinue(); }
virtual EventDesiredResult< Actor > OnCommandApproach( Actor *me, const Vector &pos, float range ) { return TryContinue(); }
virtual EventDesiredResult< Actor > OnCommandApproach( Actor *me, CBaseEntity *goal ) { return TryContinue(); }
virtual EventDesiredResult< Actor > OnCommandRetreat( Actor *me, CBaseEntity *threat, float range ) { return TryContinue(); }
virtual EventDesiredResult< Actor > OnCommandPause( Actor *me, float duration ) { return TryContinue(); }
virtual EventDesiredResult< Actor > OnCommandResume( Actor *me ) { return TryContinue(); }
virtual EventDesiredResult< Actor > OnCommandString( Actor *me, const char *command ) { return TryContinue(); }
virtual EventDesiredResult< Actor > OnShoved( Actor *me, CBaseEntity *pusher ) { return TryContinue(); }
virtual EventDesiredResult< Actor > OnBlinded( Actor *me, CBaseEntity *blinder ) { return TryContinue(); }
virtual EventDesiredResult< Actor > OnTerritoryContested( Actor *me, int territoryID ) { return TryContinue(); }
virtual EventDesiredResult< Actor > OnTerritoryCaptured( Actor *me, int territoryID ) { return TryContinue(); }
virtual EventDesiredResult< Actor > OnTerritoryLost( Actor *me, int territoryID ) { return TryContinue(); }
virtual EventDesiredResult< Actor > OnWin( Actor *me ) { return TryContinue(); }
virtual EventDesiredResult< Actor > OnLose( Actor *me ) { return TryContinue(); }
#ifdef DOTA_SERVER_DLL
virtual EventDesiredResult< Actor > OnCommandMoveTo( Actor *me, const Vector &pos ) { return TryContinue(); }
virtual EventDesiredResult< Actor > OnCommandMoveToAggressive( Actor *me, const Vector &pos ) { return TryContinue(); }
virtual EventDesiredResult< Actor > OnCommandAttack( Actor *me, CBaseEntity *victim, bool bDeny ) { return TryContinue(); }
virtual EventDesiredResult< Actor > OnCastAbilityNoTarget( Actor *me, CDOTABaseAbility *ability ) { return TryContinue(); }
virtual EventDesiredResult< Actor > OnCastAbilityOnPosition( Actor *me, CDOTABaseAbility *ability, const Vector &pos ) { return TryContinue(); }
virtual EventDesiredResult< Actor > OnCastAbilityOnTarget( Actor *me, CDOTABaseAbility *ability, CBaseEntity *target ) { return TryContinue(); }
virtual EventDesiredResult< Actor > OnDropItem( Actor *me, const Vector &pos, CBaseEntity *item ) { return TryContinue(); }
virtual EventDesiredResult< Actor > OnPickupItem( Actor *me, CBaseEntity *item ) { return TryContinue(); }
virtual EventDesiredResult< Actor > OnPickupRune( Actor *me, CBaseEntity *item ) { return TryContinue(); }
virtual EventDesiredResult< Actor > OnStop( Actor *me ) { return TryContinue(); }
virtual EventDesiredResult< Actor > OnFriendThreatened( Actor *me, CBaseEntity *friendly, CBaseEntity *threat ) { return TryContinue(); }
virtual EventDesiredResult< Actor > OnCancelAttack( Actor *me, CBaseEntity *pTarget ) { return TryContinue(); }
virtual EventDesiredResult< Actor > OnDominated( Actor *me ) { return TryContinue(); }
virtual EventDesiredResult< Actor > OnWarped( Actor *me, Vector vStartPos ) { return TryContinue(); }
#endif
/**
* Event handlers must return one of these.
*/
EventDesiredResult< Actor > TryContinue( EventResultPriorityType priority = RESULT_TRY ) const;
EventDesiredResult< Actor > TryChangeTo( Action< Actor > *action, EventResultPriorityType priority = RESULT_TRY, const char *reason = NULL ) const;
EventDesiredResult< Actor > TrySuspendFor( Action< Actor > *action, EventResultPriorityType priority = RESULT_TRY, const char *reason = NULL ) const;
EventDesiredResult< Actor > TryDone( EventResultPriorityType priority = RESULT_TRY, const char *reason = NULL ) const;
EventDesiredResult< Actor > TryToSustain( EventResultPriorityType priority = RESULT_TRY, const char *reason = NULL ) const;
//-----------------------------------------------------------------------------------------
Action< Actor > *GetActiveChildAction( void ) const;
Action< Actor > *GetParentAction( void ) const; // the Action that I'm running inside of
bool IsSuspended( void ) const; // return true if we are currently suspended for another Action
const char *DebugString( void ) const; // return a temporary string describing the current action stack for debugging
/**
* Sometimes we want to pass through other NextBots. OnContact() will always
* be invoked, but collision resolution can be skipped if this
* method returns false.
*/
virtual bool IsAbleToBlockMovementOf( const INextBot *botInMotion ) const { return true; }
// INextBotEventResponder propagation ----------------------------------------------------------------------
virtual INextBotEventResponder *FirstContainedResponder( void ) const;
virtual INextBotEventResponder *NextContainedResponder( INextBotEventResponder *current ) const;
private:
/**
* These macros are used below to translate INextBotEventResponder event methods
* into Action event handler methods
*/
#define PROCESS_EVENT( METHOD ) \
{ \
if ( !m_isStarted ) \
return; \
\
Action< Actor > *_action = this; \
EventDesiredResult< Actor > _result; \
\
while( _action ) \
{ \
if ( m_actor && (m_actor->IsDebugging(NEXTBOT_EVENTS) || NextBotDebugHistory.GetBool())) \
{ \
m_actor->DebugConColorMsg( NEXTBOT_EVENTS, Color( 100, 100, 100, 255 ), "%3.2f: %s:%s: %s received EVENT %s\n", gpGlobals->curtime, m_actor->GetDebugIdentifier(), m_behavior->GetName(), _action->GetFullName(), #METHOD ); \
} \
_result = _action->METHOD( m_actor ); \
if ( !_result.IsContinue() ) \
break; \
_action = _action->GetActionBuriedUnderMe(); \
} \
\
if ( _action ) \
{ \
if ( m_actor && _result.IsRequestingChange() && (m_actor->IsDebugging(NEXTBOT_BEHAVIOR) || NextBotDebugHistory.GetBool()) ) \
{ \
m_actor->DebugConColorMsg( NEXTBOT_BEHAVIOR, Color( 255, 255, 0, 255 ), "%3.2f: %s:%s: ", gpGlobals->curtime, m_actor->GetDebugIdentifier(), m_behavior->GetName() ); \
m_actor->DebugConColorMsg( NEXTBOT_BEHAVIOR, Color( 255, 255, 255, 255 ), "%s ", _action->GetFullName() ); \
m_actor->DebugConColorMsg( NEXTBOT_BEHAVIOR, Color( 255, 255, 0, 255 ), "reponded to EVENT %s with ", #METHOD ); \
m_actor->DebugConColorMsg( NEXTBOT_BEHAVIOR, Color( 255, 0, 0, 255 ), "%s %s ", _result.GetTypeName(), _result.m_action ? _result.m_action->GetName() : "" ); \
m_actor->DebugConColorMsg( NEXTBOT_BEHAVIOR, Color( 0, 255, 0, 255 ), "%s\n", _result.m_reason ? _result.m_reason : "" ); \
} \
\
_action->StorePendingEventResult( _result, #METHOD ); \
} \
\
INextBotEventResponder::METHOD(); \
}
#define PROCESS_EVENT_WITH_1_ARG( METHOD, ARG1 ) \
{ \
if ( !m_isStarted ) \
return; \
\
Action< Actor > *_action = this; \
EventDesiredResult< Actor > _result; \
\
while( _action ) \
{ \
if ( m_actor && (m_actor->IsDebugging(NEXTBOT_EVENTS) || NextBotDebugHistory.GetBool()) ) \
{ \
m_actor->DebugConColorMsg( NEXTBOT_EVENTS, Color( 100, 100, 100, 255 ), "%3.2f: %s:%s: %s received EVENT %s\n", gpGlobals->curtime, m_actor->GetDebugIdentifier(), m_behavior->GetName(), _action->GetFullName(), #METHOD ); \
} \
_result = _action->METHOD( m_actor, ARG1 ); \
if ( !_result.IsContinue() ) \
break; \
_action = _action->GetActionBuriedUnderMe(); \
} \
\
if ( _action ) \
{ \
if ( m_actor && (m_actor->IsDebugging(NEXTBOT_BEHAVIOR) || NextBotDebugHistory.GetBool()) && _result.IsRequestingChange() && _action ) \
{ \
m_actor->DebugConColorMsg( NEXTBOT_BEHAVIOR, Color( 255, 255, 0, 255 ), "%3.2f: %s:%s: ", gpGlobals->curtime, m_actor->GetDebugIdentifier(), m_behavior->GetName() ); \
m_actor->DebugConColorMsg( NEXTBOT_BEHAVIOR, Color( 255, 255, 255, 255 ), "%s ", _action->GetFullName() ); \
m_actor->DebugConColorMsg( NEXTBOT_BEHAVIOR, Color( 255, 255, 0, 255 ), "reponded to EVENT %s with ", #METHOD ); \
m_actor->DebugConColorMsg( NEXTBOT_BEHAVIOR, Color( 255, 0, 0, 255 ), "%s %s ", _result.GetTypeName(), _result.m_action ? _result.m_action->GetName() : "" ); \
m_actor->DebugConColorMsg( NEXTBOT_BEHAVIOR, Color( 0, 255, 0, 255 ), "%s\n", _result.m_reason ? _result.m_reason : "" ); \
} \
\
_action->StorePendingEventResult( _result, #METHOD ); \
} \
\
INextBotEventResponder::METHOD( ARG1 ); \
}
#define PROCESS_EVENT_WITH_2_ARGS( METHOD, ARG1, ARG2 ) \
{ \
if ( !m_isStarted ) \
return; \
\
Action< Actor > *_action = this; \
EventDesiredResult< Actor > _result; \
\
while( _action ) \
{ \
if ( m_actor && (m_actor->IsDebugging(NEXTBOT_EVENTS) || NextBotDebugHistory.GetBool()) ) \
{ \
m_actor->DebugConColorMsg( NEXTBOT_EVENTS, Color( 100, 100, 100, 255 ), "%3.2f: %s:%s: %s received EVENT %s\n", gpGlobals->curtime, m_actor->GetDebugIdentifier(), m_behavior->GetName(), _action->GetFullName(), #METHOD ); \
} \
_result = _action->METHOD( m_actor, ARG1, ARG2 ); \
if ( !_result.IsContinue() ) \
break; \
_action = _action->GetActionBuriedUnderMe(); \
} \
\
if ( _action ) \
{ \
if ( m_actor && (m_actor->IsDebugging(NEXTBOT_BEHAVIOR) || NextBotDebugHistory.GetBool()) && _result.IsRequestingChange() && _action ) \
{ \
m_actor->DebugConColorMsg( NEXTBOT_BEHAVIOR, Color( 255, 255, 0, 255 ), "%3.2f: %s:%s: ", gpGlobals->curtime, m_actor->GetDebugIdentifier(), m_behavior->GetName() ); \
m_actor->DebugConColorMsg( NEXTBOT_BEHAVIOR, Color( 255, 255, 255, 255 ), "%s ", _action->GetFullName() ); \
m_actor->DebugConColorMsg( NEXTBOT_BEHAVIOR, Color( 255, 255, 0, 255 ), "reponded to EVENT %s with ", #METHOD ); \
m_actor->DebugConColorMsg( NEXTBOT_BEHAVIOR, Color( 255, 0, 0, 255 ), "%s %s ", _result.GetTypeName(), _result.m_action ? _result.m_action->GetName() : "" ); \
m_actor->DebugConColorMsg( NEXTBOT_BEHAVIOR, Color( 0, 255, 0, 255 ), "%s\n", _result.m_reason ? _result.m_reason : "" ); \
} \
\
_action->StorePendingEventResult( _result, #METHOD ); \
} \
\
INextBotEventResponder::METHOD( ARG1, ARG2 ); \
}
#define PROCESS_EVENT_WITH_3_ARGS( METHOD, ARG1, ARG2, ARG3 ) \
{ \
if ( !m_isStarted ) \
return; \
\
Action< Actor > *_action = this; \
EventDesiredResult< Actor > _result; \
\
while( _action ) \
{ \
if ( m_actor && (m_actor->IsDebugging(NEXTBOT_EVENTS) || NextBotDebugHistory.GetBool()) ) \
{ \
m_actor->DebugConColorMsg( NEXTBOT_EVENTS, Color( 100, 100, 100, 255 ), "%3.2f: %s:%s: %s received EVENT %s\n", gpGlobals->curtime, m_actor->GetDebugIdentifier(), m_behavior->GetName(), _action->GetFullName(), #METHOD ); \
} \
_result = _action->METHOD( m_actor, ARG1, ARG2, ARG3 ); \
if ( !_result.IsContinue() ) \
break; \
_action = _action->GetActionBuriedUnderMe(); \
} \
\
if ( _action ) \
{ \
if ( m_actor && (m_actor->IsDebugging(NEXTBOT_BEHAVIOR) || NextBotDebugHistory.GetBool()) && _result.IsRequestingChange() && _action ) \
{ \
m_actor->DebugConColorMsg( NEXTBOT_BEHAVIOR, Color( 255, 255, 0, 255 ), "%3.2f: %s:%s: ", gpGlobals->curtime, m_actor->GetDebugIdentifier(), m_behavior->GetName() ); \
m_actor->DebugConColorMsg( NEXTBOT_BEHAVIOR, Color( 255, 255, 255, 255 ), "%s ", _action->GetFullName() ); \
m_actor->DebugConColorMsg( NEXTBOT_BEHAVIOR, Color( 255, 255, 0, 255 ), "reponded to EVENT %s with ", #METHOD ); \
m_actor->DebugConColorMsg( NEXTBOT_BEHAVIOR, Color( 255, 0, 0, 255 ), "%s %s ", _result.GetTypeName(), _result.m_action ? _result.m_action->GetName() : "" ); \
m_actor->DebugConColorMsg( NEXTBOT_BEHAVIOR, Color( 0, 255, 0, 255 ), "%s\n", _result.m_reason ? _result.m_reason : "" ); \
} \
\
_action->StorePendingEventResult( _result, #METHOD ); \
} \
\
INextBotEventResponder::METHOD( ARG1, ARG2, ARG3 ); \
}
/**
* Translate incoming events into Action events
* DO NOT OVERRIDE THESE METHODS
*/
virtual void OnLeaveGround( CBaseEntity *ground ) { PROCESS_EVENT_WITH_1_ARG( OnLeaveGround, ground ); }
virtual void OnLandOnGround( CBaseEntity *ground ) { PROCESS_EVENT_WITH_1_ARG( OnLandOnGround, ground ); }
virtual void OnContact( CBaseEntity *other, CGameTrace *result ) { PROCESS_EVENT_WITH_2_ARGS( OnContact, other, result ); }
virtual void OnMoveToSuccess( const Path *path ) { PROCESS_EVENT_WITH_1_ARG( OnMoveToSuccess, path ); }
virtual void OnMoveToFailure( const Path *path, MoveToFailureType reason ) { PROCESS_EVENT_WITH_2_ARGS( OnMoveToFailure, path, reason ); }
virtual void OnStuck( void ) { PROCESS_EVENT( OnStuck ); }
virtual void OnUnStuck( void ) { PROCESS_EVENT( OnUnStuck ); }
virtual void OnPostureChanged( void ) { PROCESS_EVENT( OnPostureChanged ); }
virtual void OnAnimationActivityComplete( int activity ) { PROCESS_EVENT_WITH_1_ARG( OnAnimationActivityComplete, activity ); }
virtual void OnAnimationActivityInterrupted( int activity ) { PROCESS_EVENT_WITH_1_ARG( OnAnimationActivityInterrupted, activity ); }
virtual void OnAnimationEvent( animevent_t *event ) { PROCESS_EVENT_WITH_1_ARG( OnAnimationEvent, event ); }
virtual void OnIgnite( void ) { PROCESS_EVENT( OnIgnite ); }
virtual void OnInjured( const CTakeDamageInfo &info ) { PROCESS_EVENT_WITH_1_ARG( OnInjured, info ); }
virtual void OnKilled( const CTakeDamageInfo &info ) { PROCESS_EVENT_WITH_1_ARG( OnKilled, info ); }
virtual void OnOtherKilled( CBaseCombatCharacter *victim, const CTakeDamageInfo &info ) { PROCESS_EVENT_WITH_2_ARGS( OnOtherKilled, victim, info ); }
virtual void OnSight( CBaseEntity *subject ) { PROCESS_EVENT_WITH_1_ARG( OnSight, subject ); }
virtual void OnLostSight( CBaseEntity *subject ) { PROCESS_EVENT_WITH_1_ARG( OnLostSight, subject ); }
virtual void OnSound( CBaseEntity *source, const Vector &pos, KeyValues *keys ) { PROCESS_EVENT_WITH_3_ARGS( OnSound, source, pos, keys ); }
virtual void OnSpokeConcept( CBaseCombatCharacter *who, AIConcept_t concept, AI_Response *response ) { PROCESS_EVENT_WITH_3_ARGS( OnSpokeConcept, who, concept, response ); }
virtual void OnWeaponFired( CBaseCombatCharacter *whoFired, CBaseCombatWeapon *weapon ) { PROCESS_EVENT_WITH_2_ARGS( OnWeaponFired, whoFired, weapon ); }
virtual void OnNavAreaChanged( CNavArea *newArea, CNavArea *oldArea ) { PROCESS_EVENT_WITH_2_ARGS( OnNavAreaChanged, newArea, oldArea ); }
virtual void OnModelChanged( void ) { PROCESS_EVENT( OnModelChanged ); }
virtual void OnPickUp( CBaseEntity *item, CBaseCombatCharacter *giver ) { PROCESS_EVENT_WITH_2_ARGS( OnPickUp, item, giver ); }
virtual void OnDrop( CBaseEntity *item ) { PROCESS_EVENT_WITH_1_ARG( OnDrop, item ); }
virtual void OnActorEmoted( CBaseCombatCharacter *emoter, int emote ) { PROCESS_EVENT_WITH_2_ARGS( OnActorEmoted, emoter, emote ); }
virtual void OnCommandAttack( CBaseEntity *victim ) { PROCESS_EVENT_WITH_1_ARG( OnCommandAttack, victim ); }
virtual void OnCommandApproach( const Vector &pos, float range ) { PROCESS_EVENT_WITH_2_ARGS( OnCommandApproach, pos, range ); }
virtual void OnCommandApproach( CBaseEntity *goal ) { PROCESS_EVENT_WITH_1_ARG( OnCommandApproach, goal ); }
virtual void OnCommandRetreat( CBaseEntity *threat, float range ) { PROCESS_EVENT_WITH_2_ARGS( OnCommandRetreat, threat, range ); }
virtual void OnCommandPause( float duration ) { PROCESS_EVENT_WITH_1_ARG( OnCommandPause, duration ); }
virtual void OnCommandResume( void ) { PROCESS_EVENT( OnCommandResume ); }
virtual void OnCommandString( const char *command ) { PROCESS_EVENT_WITH_1_ARG( OnCommandString, command ); }
virtual void OnShoved( CBaseEntity *pusher ) { PROCESS_EVENT_WITH_1_ARG( OnShoved, pusher ); }
virtual void OnBlinded( CBaseEntity *blinder ) { PROCESS_EVENT_WITH_1_ARG( OnBlinded, blinder ); }
virtual void OnTerritoryContested( int territoryID ) { PROCESS_EVENT_WITH_1_ARG( OnTerritoryContested, territoryID ); }
virtual void OnTerritoryCaptured( int territoryID ) { PROCESS_EVENT_WITH_1_ARG( OnTerritoryCaptured, territoryID ); }
virtual void OnTerritoryLost( int territoryID ) { PROCESS_EVENT_WITH_1_ARG( OnTerritoryLost, territoryID ); }
virtual void OnWin( void ) { PROCESS_EVENT( OnWin ); }
virtual void OnLose( void ) { PROCESS_EVENT( OnLose ); }
#ifdef DOTA_SERVER_DLL
virtual void OnCommandMoveTo( const Vector &pos ) { PROCESS_EVENT_WITH_1_ARG( OnCommandMoveTo, pos ); }
virtual void OnCommandMoveToAggressive( const Vector &pos ) { PROCESS_EVENT_WITH_1_ARG( OnCommandMoveToAggressive, pos ); }
virtual void OnCommandAttack( CBaseEntity *victim, bool bDeny ) { PROCESS_EVENT_WITH_2_ARGS( OnCommandAttack, victim, bDeny ); }
virtual void OnCastAbilityNoTarget( CDOTABaseAbility *ability ) { PROCESS_EVENT_WITH_1_ARG( OnCastAbilityNoTarget, ability ); }
virtual void OnCastAbilityOnPosition( CDOTABaseAbility *ability, const Vector &pos ) { PROCESS_EVENT_WITH_2_ARGS( OnCastAbilityOnPosition, ability, pos ); }
virtual void OnCastAbilityOnTarget( CDOTABaseAbility *ability, CBaseEntity *target ) { PROCESS_EVENT_WITH_2_ARGS( OnCastAbilityOnTarget, ability, target ); }
virtual void OnDropItem( const Vector &pos, CBaseEntity *item ) { PROCESS_EVENT_WITH_2_ARGS( OnDropItem, pos, item ); }
virtual void OnPickupItem( CBaseEntity *item ) { PROCESS_EVENT_WITH_1_ARG( OnPickupItem, item ); }
virtual void OnPickupRune( CBaseEntity *item ) { PROCESS_EVENT_WITH_1_ARG( OnPickupRune, item ); }
virtual void OnStop() { PROCESS_EVENT( OnStop ); }
virtual void OnFriendThreatened( CBaseEntity *friendly, CBaseEntity *threat ) { PROCESS_EVENT_WITH_2_ARGS( OnFriendThreatened, friendly, threat ); }
virtual void OnCancelAttack( CBaseEntity *pTarget ) { PROCESS_EVENT_WITH_1_ARG( OnCancelAttack, pTarget ); }
virtual void OnDominated() { PROCESS_EVENT( OnDominated ); }
virtual void OnWarped( Vector vStartPos ) { PROCESS_EVENT_WITH_1_ARG( OnWarped, vStartPos ); }
#endif
friend class Behavior< Actor>; // the containing Behavior class
Behavior< Actor > *m_behavior; // the Behavior this Action is part of
Action< Actor > *m_parent; // the Action that contains us
Action< Actor > *m_child; // the ACTIVE Action we contain, top of the stack. Use m_buriedUnderMe, m_coveringMe on the child to traverse to other suspended children
Action< Actor > *m_buriedUnderMe; // the Action just "under" us in the stack that we will resume to when we finish
Action< Actor > *m_coveringMe; // the Action just "above" us in the stack that will resume to us when it finishes
Actor *m_actor; // only valid after OnStart()
mutable EventDesiredResult< Actor > m_eventResult; // set by event handlers
bool m_isStarted; // Action doesn't start until OnStart() is invoked
bool m_isSuspended; // are we suspended for another Action
Action< Actor > *GetActionBuriedUnderMe( void ) const // return Action just "under" us that we will resume to when we finish
{
return m_buriedUnderMe;
}
Action< Actor > *GetActionCoveringMe( void ) const // return Action just "above" us that will resume to us when it finishes
{
return m_coveringMe;
}
/**
* If any Action buried underneath me has either exited
* or is changing to a different Action, we're "out of scope"
*/
bool IsOutOfScope( void ) const
{
for( Action< Actor > *under = GetActionBuriedUnderMe(); under; under = under->GetActionBuriedUnderMe() )
{
if ( under->m_eventResult.m_type == CHANGE_TO ||
under->m_eventResult.m_type == DONE )
{
return true;
}
}
return false;
}
/**
* Process any pending events with the stack. This is called
* by the active Action on the top of the stack, and walks