forked from hitmen047/Source-PlusPlus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAI_ResponseSystem.cpp
3404 lines (2849 loc) · 78.4 KB
/
AI_ResponseSystem.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
//=============================================================================//
#include "cbase.h"
#include "SoundEmitterSystem/isoundemittersystembase.h"
#include "AI_ResponseSystem.h"
#include "igamesystem.h"
#include "AI_Criteria.h"
#include <KeyValues.h>
#include "filesystem.h"
#include "utldict.h"
#include "ai_speech.h"
#include "tier0/icommandline.h"
#include <ctype.h>
#include "sceneentity.h"
#include "isaverestore.h"
#include "utlbuffer.h"
#include "stringpool.h"
#include "fmtstr.h"
#include "multiplay_gamerules.h"
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
ConVar rr_debugresponses( "rr_debugresponses", "0", FCVAR_NONE, "Show verbose matching output (1 for simple, 2 for rule scoring). If set to 3, it will only show response success/failure for npc_selected NPCs." );
ConVar rr_debugrule( "rr_debugrule", "", FCVAR_NONE, "If set to the name of the rule, that rule's score will be shown whenever a concept is passed into the response rules system.");
ConVar rr_dumpresponses( "rr_dumpresponses", "0", FCVAR_NONE, "Dump all response_rules.txt and rules (requires restart)" );
static CUtlSymbolTable g_RS;
inline static char *CopyString( const char *in )
{
if ( !in )
return NULL;
int len = Q_strlen( in );
char *out = new char[ len + 1 ];
Q_memcpy( out, in, len );
out[ len ] = 0;
return out;
}
#pragma pack(1)
class Matcher
{
public:
Matcher()
{
valid = false;
isnumeric = false;
notequal = false;
usemin = false;
minequals = false;
usemax = false;
maxequals = false;
maxval = 0.0f;
minval = 0.0f;
token = UTL_INVAL_SYMBOL;
rawtoken = UTL_INVAL_SYMBOL;
}
void Describe( void )
{
if ( !valid )
{
DevMsg( " invalid!\n" );
return;
}
char sz[ 128 ];
sz[ 0] = 0;
int minmaxcount = 0;
if ( usemin )
{
Q_snprintf( sz, sizeof( sz ), ">%s%.3f", minequals ? "=" : "", minval );
minmaxcount++;
}
if ( usemax )
{
char sz2[ 128 ];
Q_snprintf( sz2, sizeof( sz2 ), "<%s%.3f", maxequals ? "=" : "", maxval );
if ( minmaxcount > 0 )
{
Q_strncat( sz, " and ", sizeof( sz ), COPY_ALL_CHARACTERS );
}
Q_strncat( sz, sz2, sizeof( sz ), COPY_ALL_CHARACTERS );
minmaxcount++;
}
if ( minmaxcount >= 1 )
{
DevMsg( " matcher: %s\n", sz );
return;
}
if ( notequal )
{
DevMsg( " matcher: !=%s\n", GetToken() );
return;
}
DevMsg( " matcher: ==%s\n", GetToken() );
}
float maxval;
float minval;
bool valid : 1; //1
bool isnumeric : 1; //2
bool notequal : 1; //3
bool usemin : 1; //4
bool minequals : 1; //5
bool usemax : 1; //6
bool maxequals : 1; //7
void SetToken( char const *s )
{
token = g_RS.AddString( s );
}
char const *GetToken()
{
if ( token.IsValid() )
{
return g_RS.String( token );
}
return "";
}
void SetRaw( char const *raw )
{
rawtoken = g_RS.AddString( raw );
}
char const *GetRaw()
{
if ( rawtoken.IsValid() )
{
return g_RS.String( rawtoken );
}
return "";
}
private:
CUtlSymbol token;
CUtlSymbol rawtoken;
};
struct Response
{
DECLARE_SIMPLE_DATADESC();
Response()
{
type = RESPONSE_NONE;
value = NULL;
weight.SetFloat( 1.0f );
depletioncount = 0;
first = false;
last = false;
}
Response( const Response& src )
{
weight = src.weight;
type = src.type;
value = CopyString( src.value );
depletioncount = src.depletioncount;
first = src.first;
last = src.last;
}
Response& operator =( const Response& src )
{
if ( this == &src )
return *this;
weight = src.weight;
type = src.type;
value = CopyString( src.value );
depletioncount = src.depletioncount;
first = src.first;
last = src.last;
return *this;
}
~Response()
{
delete[] value;
}
ResponseType_t GetType() { return (ResponseType_t)type; }
char *value; // fixed up value spot // 4
float16 weight; // 6
byte depletioncount; // 7
byte type : 6; // 8
byte first : 1; //
byte last : 1; //
};
struct ResponseGroup
{
DECLARE_SIMPLE_DATADESC();
ResponseGroup()
{
// By default visit all nodes before repeating
m_bSequential = false;
m_bNoRepeat = false;
m_bEnabled = true;
m_nCurrentIndex = 0;
m_bDepleteBeforeRepeat = true;
m_nDepletionCount = 1;
m_bHasFirst = false;
m_bHasLast = false;
}
ResponseGroup( const ResponseGroup& src )
{
int c = src.group.Count();
for ( int i = 0; i < c; i++ )
{
group.AddToTail( src.group[ i ] );
}
rp = src.rp;
m_bDepleteBeforeRepeat = src.m_bDepleteBeforeRepeat;
m_nDepletionCount = src.m_nDepletionCount;
m_bHasFirst = src.m_bHasFirst;
m_bHasLast = src.m_bHasLast;
m_bSequential = src.m_bSequential;
m_bNoRepeat = src.m_bNoRepeat;
m_bEnabled = src.m_bEnabled;
m_nCurrentIndex = src.m_nCurrentIndex;
}
ResponseGroup& operator=( const ResponseGroup& src )
{
if ( this == &src )
return *this;
int c = src.group.Count();
for ( int i = 0; i < c; i++ )
{
group.AddToTail( src.group[ i ] );
}
rp = src.rp;
m_bDepleteBeforeRepeat = src.m_bDepleteBeforeRepeat;
m_nDepletionCount = src.m_nDepletionCount;
m_bHasFirst = src.m_bHasFirst;
m_bHasLast = src.m_bHasLast;
m_bSequential = src.m_bSequential;
m_bNoRepeat = src.m_bNoRepeat;
m_bEnabled = src.m_bEnabled;
m_nCurrentIndex = src.m_nCurrentIndex;
return *this;
}
bool HasUndepletedChoices() const
{
if ( !m_bDepleteBeforeRepeat )
return true;
int c = group.Count();
for ( int i = 0; i < c; i++ )
{
if ( group[ i ].depletioncount != m_nDepletionCount )
return true;
}
return false;
}
void MarkResponseUsed( int idx )
{
if ( !m_bDepleteBeforeRepeat )
return;
if ( idx < 0 || idx >= group.Count() )
{
Assert( 0 );
return;
}
group[ idx ].depletioncount = m_nDepletionCount;
}
void ResetDepletionCount()
{
if ( !m_bDepleteBeforeRepeat )
return;
++m_nDepletionCount;
}
void Reset()
{
ResetDepletionCount();
SetEnabled( true );
SetCurrentIndex( 0 );
m_nDepletionCount = 1;
for ( int i = 0; i < group.Count(); ++i )
{
group[ i ].depletioncount = 0;
}
}
bool HasUndepletedFirst( int& index )
{
index = -1;
if ( !m_bDepleteBeforeRepeat )
return false;
int c = group.Count();
for ( int i = 0; i < c; i++ )
{
Response *r = &group[ i ];
if ( ( r->depletioncount != m_nDepletionCount ) && r->first )
{
index = i;
return true;
}
}
return false;
}
bool HasUndepletedLast( int& index )
{
index = -1;
if ( !m_bDepleteBeforeRepeat )
return false;
int c = group.Count();
for ( int i = 0; i < c; i++ )
{
Response *r = &group[ i ];
if ( ( r->depletioncount != m_nDepletionCount ) && r->last )
{
index = i;
return true;
}
}
return false;
}
bool ShouldCheckRepeats() const { return m_bDepleteBeforeRepeat; }
int GetDepletionCount() const { return m_nDepletionCount; }
bool IsSequential() const { return m_bSequential; }
void SetSequential( bool seq ) { m_bSequential = seq; }
bool IsNoRepeat() const { return m_bNoRepeat; }
void SetNoRepeat( bool norepeat ) { m_bNoRepeat = norepeat; }
bool IsEnabled() const { return m_bEnabled; }
void SetEnabled( bool enabled ) { m_bEnabled = enabled; }
int GetCurrentIndex() const { return m_nCurrentIndex; }
void SetCurrentIndex( byte idx ) { m_nCurrentIndex = idx; }
CUtlVector< Response > group;
AI_ResponseParams rp;
bool m_bEnabled;
byte m_nCurrentIndex;
// Invalidation counter
byte m_nDepletionCount;
// Use all slots before repeating any
bool m_bDepleteBeforeRepeat : 1;
bool m_bHasFirst : 1;
bool m_bHasLast : 1;
bool m_bSequential : 1;
bool m_bNoRepeat : 1;
};
struct Criteria
{
Criteria()
{
name = NULL;
value = NULL;
weight.SetFloat( 1.0f );
required = false;
}
Criteria& operator =(const Criteria& src )
{
if ( this == &src )
return *this;
name = CopyString( src.name );
value = CopyString( src.value );
weight = src.weight;
required = src.required;
matcher = src.matcher;
int c = src.subcriteria.Count();
for ( int i = 0; i < c; i++ )
{
subcriteria.AddToTail( src.subcriteria[ i ] );
}
return *this;
}
Criteria(const Criteria& src )
{
name = CopyString( src.name );
value = CopyString( src.value );
weight = src.weight;
required = src.required;
matcher = src.matcher;
int c = src.subcriteria.Count();
for ( int i = 0; i < c; i++ )
{
subcriteria.AddToTail( src.subcriteria[ i ] );
}
}
~Criteria()
{
delete[] name;
delete[] value;
}
bool IsSubCriteriaType() const
{
return ( subcriteria.Count() > 0 ) ? true : false;
}
char *name;
char *value;
float16 weight;
bool required;
Matcher matcher;
// Indices into sub criteria
CUtlVector< unsigned short > subcriteria;
};
struct Rule
{
Rule()
{
m_bMatchOnce = false;
m_bEnabled = true;
m_szContext = NULL;
m_bApplyContextToWorld = false;
}
Rule& operator =( const Rule& src )
{
if ( this == &src )
return *this;
int i;
int c;
c = src.m_Criteria.Count();
for ( i = 0; i < c; i++ )
{
m_Criteria.AddToTail( src.m_Criteria[ i ] );
}
c = src.m_Responses.Count();
for ( i = 0; i < c; i++ )
{
m_Responses.AddToTail( src.m_Responses[ i ] );
}
SetContext( src.m_szContext );
m_bMatchOnce = src.m_bMatchOnce;
m_bEnabled = src.m_bEnabled;
m_bApplyContextToWorld = src.m_bApplyContextToWorld;
return *this;
}
Rule( const Rule& src )
{
int i;
int c;
c = src.m_Criteria.Count();
for ( i = 0; i < c; i++ )
{
m_Criteria.AddToTail( src.m_Criteria[ i ] );
}
c = src.m_Responses.Count();
for ( i = 0; i < c; i++ )
{
m_Responses.AddToTail( src.m_Responses[ i ] );
}
SetContext( src.m_szContext );
m_bMatchOnce = src.m_bMatchOnce;
m_bEnabled = src.m_bEnabled;
m_bApplyContextToWorld = src.m_bApplyContextToWorld;
}
~Rule()
{
delete[] m_szContext;
}
void SetContext( const char *context )
{
delete[] m_szContext;
m_szContext = CopyString( context );
}
const char *GetContext( void ) const { return m_szContext; }
bool IsEnabled() const { return m_bEnabled; }
void Disable() { m_bEnabled = false; }
bool IsMatchOnce() const { return m_bMatchOnce; }
bool IsApplyContextToWorld() const { return m_bApplyContextToWorld; }
// Indices into underlying criteria and response dictionaries
CUtlVector< unsigned short > m_Criteria;
CUtlVector< unsigned short> m_Responses;
char *m_szContext;
bool m_bApplyContextToWorld : 1;
bool m_bMatchOnce : 1;
bool m_bEnabled : 1;
};
#pragma pack()
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
abstract_class CResponseSystem : public IResponseSystem
{
public:
CResponseSystem();
~CResponseSystem();
// IResponseSystem
virtual bool FindBestResponse( const AI_CriteriaSet& set, AI_Response& response, IResponseFilter *pFilter = NULL );
virtual void GetAllResponses( CUtlVector<AI_Response *> *pResponses );
virtual void Release() = 0;
virtual void DumpRules();
virtual void Precache();
virtual void PrecacheResponses( bool bEnable )
{
m_bPrecache = bEnable;
}
bool ShouldPrecache() { return m_bPrecache; }
bool IsCustomManagable() { return m_bCustomManagable; }
void Clear();
void DumpDictionary( const char *pszName );
protected:
virtual const char *GetScriptFile( void ) = 0;
void LoadRuleSet( const char *setname );
void ResetResponseGroups();
float LookForCriteria( const AI_CriteriaSet &criteriaSet, int iCriteria );
float RecursiveLookForCriteria( const AI_CriteriaSet &criteriaSet, Criteria *pParent );
public:
void CopyRuleFrom( Rule *pSrcRule, int iRule, CResponseSystem *pCustomSystem );
void CopyCriteriaFrom( Rule *pSrcRule, Rule *pDstRule, CResponseSystem *pCustomSystem );
void CopyResponsesFrom( Rule *pSrcRule, Rule *pDstRule, CResponseSystem *pCustomSystem );
void CopyEnumerationsFrom( CResponseSystem *pCustomSystem );
//private:
struct Enumeration
{
float value;
};
struct ResponseSearchResult
{
ResponseSearchResult()
{
group = NULL;
action = NULL;
}
ResponseGroup *group;
Response *action;
};
inline bool ParseToken( void )
{
if ( m_bUnget )
{
m_bUnget = false;
return true;
}
if ( m_ScriptStack.Count() <= 0 )
{
Assert( 0 );
return false;
}
m_ScriptStack[ 0 ].currenttoken = engine->ParseFile( m_ScriptStack[ 0 ].currenttoken, token, sizeof( token ) );
m_ScriptStack[ 0 ].tokencount++;
return m_ScriptStack[ 0 ].currenttoken != NULL ? true : false;
}
inline void Unget()
{
m_bUnget = true;
}
inline bool TokenWaiting( void )
{
if ( m_ScriptStack.Count() <= 0 )
{
Assert( 0 );
return false;
}
const char *p = m_ScriptStack[ 0 ].currenttoken;
if ( !p )
{
Error( "AI_ResponseSystem: Unxpected TokenWaiting() with NULL buffer in %p", m_ScriptStack[ 0 ].name );
return false;
}
while ( *p && *p!='\n')
{
// Special handler for // comment blocks
if ( *p == '/' && *(p+1) == '/' )
return false;
if ( !isspace( *p ) || isalnum( *p ) )
return true;
p++;
}
return false;
}
void ParseOneResponse( const char *responseGroupName, ResponseGroup& group );
void ParseInclude( CStringPool &includedFiles );
void ParseResponse( void );
void ParseCriterion( void );
void ParseRule( void );
void ParseEnumeration( void );
int ParseOneCriterion( const char *criterionName );
bool Compare( const char *setValue, Criteria *c, bool verbose = false );
bool CompareUsingMatcher( const char *setValue, Matcher& m, bool verbose = false );
void ComputeMatcher( Criteria *c, Matcher& matcher );
void ResolveToken( Matcher& matcher, char *token, size_t bufsize, char const *rawtoken );
float LookupEnumeration( const char *name, bool& found );
int FindBestMatchingRule( const AI_CriteriaSet& set, bool verbose );
float ScoreCriteriaAgainstRule( const AI_CriteriaSet& set, int irule, bool verbose = false );
float RecursiveScoreSubcriteriaAgainstRule( const AI_CriteriaSet& set, Criteria *parent, bool& exclude, bool verbose /*=false*/ );
float ScoreCriteriaAgainstRuleCriteria( const AI_CriteriaSet& set, int icriterion, bool& exclude, bool verbose = false );
bool GetBestResponse( ResponseSearchResult& result, Rule *rule, bool verbose = false, IResponseFilter *pFilter = NULL );
bool ResolveResponse( ResponseSearchResult& result, int depth, const char *name, bool verbose = false, IResponseFilter *pFilter = NULL );
int SelectWeightedResponseFromResponseGroup( ResponseGroup *g, IResponseFilter *pFilter );
void DescribeResponseGroup( ResponseGroup *group, int selected, int depth );
void DebugPrint( int depth, const char *fmt, ... );
void LoadFromBuffer( const char *scriptfile, const char *buffer, CStringPool &includedFiles );
void GetCurrentScript( char *buf, size_t buflen );
int GetCurrentToken() const;
void SetCurrentScript( const char *script );
bool IsRootCommand();
void PushScript( const char *scriptfile, unsigned char *buffer );
void PopScript(void);
void ResponseWarning( const char *fmt, ... );
CUtlDict< ResponseGroup, short > m_Responses;
CUtlDict< Criteria, short > m_Criteria;
CUtlDict< Rule, short > m_Rules;
CUtlDict< Enumeration, short > m_Enumerations;
char token[ 1204 ];
bool m_bUnget;
bool m_bPrecache;
bool m_bCustomManagable;
struct ScriptEntry
{
unsigned char *buffer;
FileNameHandle_t name;
const char *currenttoken;
int tokencount;
};
CUtlVector< ScriptEntry > m_ScriptStack;
friend class CDefaultResponseSystemSaveRestoreBlockHandler;
friend class CResponseSystemSaveRestoreOps;
};
BEGIN_SIMPLE_DATADESC( Response )
// DEFINE_FIELD( type, FIELD_INTEGER ),
// DEFINE_ARRAY( value, FIELD_CHARACTER ),
// DEFINE_FIELD( weight, FIELD_FLOAT ),
DEFINE_FIELD( depletioncount, FIELD_CHARACTER ),
// DEFINE_FIELD( first, FIELD_BOOLEAN ),
// DEFINE_FIELD( last, FIELD_BOOLEAN ),
END_DATADESC()
BEGIN_SIMPLE_DATADESC( ResponseGroup )
// DEFINE_FIELD( group, FIELD_UTLVECTOR ),
// DEFINE_FIELD( rp, FIELD_EMBEDDED ),
// DEFINE_FIELD( m_bDepleteBeforeRepeat, FIELD_BOOLEAN ),
DEFINE_FIELD( m_nDepletionCount, FIELD_CHARACTER ),
// DEFINE_FIELD( m_bHasFirst, FIELD_BOOLEAN ),
// DEFINE_FIELD( m_bHasLast, FIELD_BOOLEAN ),
// DEFINE_FIELD( m_bSequential, FIELD_BOOLEAN ),
// DEFINE_FIELD( m_bNoRepeat, FIELD_BOOLEAN ),
DEFINE_FIELD( m_bEnabled, FIELD_BOOLEAN ),
DEFINE_FIELD( m_nCurrentIndex, FIELD_CHARACTER ),
END_DATADESC()
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
CResponseSystem::CResponseSystem()
{
token[0] = 0;
m_bUnget = false;
m_bPrecache = true;
m_bCustomManagable = false;
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
CResponseSystem::~CResponseSystem()
{
}
//-----------------------------------------------------------------------------
// Purpose:
// Output : char const
//-----------------------------------------------------------------------------
void CResponseSystem::GetCurrentScript( char *buf, size_t buflen )
{
Assert( buf );
buf[ 0 ] = 0;
if ( m_ScriptStack.Count() <= 0 )
return;
if ( filesystem->String( m_ScriptStack[ 0 ].name, buf, buflen ) )
{
return;
}
buf[ 0 ] = 0;
}
void CResponseSystem::PushScript( const char *scriptfile, unsigned char *buffer )
{
ScriptEntry e;
e.name = filesystem->FindOrAddFileName( scriptfile );
e.buffer = buffer;
e.currenttoken = (char *)e.buffer;
e.tokencount = 0;
m_ScriptStack.AddToHead( e );
}
void CResponseSystem::PopScript(void)
{
Assert( m_ScriptStack.Count() >= 1 );
if ( m_ScriptStack.Count() <= 0 )
return;
m_ScriptStack.Remove( 0 );
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CResponseSystem::Clear()
{
m_Responses.RemoveAll();
m_Criteria.RemoveAll();
m_Rules.RemoveAll();
m_Enumerations.RemoveAll();
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : *name -
// found -
// Output : float
//-----------------------------------------------------------------------------
float CResponseSystem::LookupEnumeration( const char *name, bool& found )
{
int idx = m_Enumerations.Find( name );
if ( idx == m_Enumerations.InvalidIndex() )
{
found = false;
return 0.0f;
}
found = true;
return m_Enumerations[ idx ].value;
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : matcher -
//-----------------------------------------------------------------------------
void CResponseSystem::ResolveToken( Matcher& matcher, char *token, size_t bufsize, char const *rawtoken )
{
if ( rawtoken[0] != '[' )
{
Q_strncpy( token, rawtoken, bufsize );
return;
}
// Now lookup enumeration
bool found = false;
float f = LookupEnumeration( rawtoken, found );
if ( !found )
{
Q_strncpy( token, rawtoken, bufsize );
ResponseWarning( "No such enumeration '%s'\n", token );
return;
}
Q_snprintf( token, bufsize, "%f", f );
}
static bool AppearsToBeANumber( char const *token )
{
if ( atof( token ) != 0.0f )
return true;
char const *p = token;
while ( *p )
{
if ( *p != '0' )
return false;
p++;
}
return true;
}
void CResponseSystem::ComputeMatcher( Criteria *c, Matcher& matcher )
{
const char *s = c->value;
if ( !s )
{
matcher.valid = false;
return;
}
const char *in = s;
char token[ 128 ];
char rawtoken[ 128 ];
token[ 0 ] = 0;
rawtoken[ 0 ] = 0;
int n = 0;
bool gt = false;
bool lt = false;
bool eq = false;
bool nt = false;
bool done = false;
while ( !done )
{
switch( *in )
{
case '>':
{
gt = true;
Assert( !lt ); // Can't be both
}
break;
case '<':
{
lt = true;
Assert( !gt ); // Can't be both
}
break;
case '=':
{
eq = true;
}
break;
case ',':
case '\0':
{
rawtoken[ n ] = 0;
n = 0;
// Convert raw token to real token in case token is an enumerated type specifier
ResolveToken( matcher, token, sizeof( token ), rawtoken );
// Fill in first data set
if ( gt )
{
matcher.usemin = true;
matcher.minequals = eq;
matcher.minval = (float)atof( token );
matcher.isnumeric = true;
}
else if ( lt )
{
matcher.usemax = true;
matcher.maxequals = eq;
matcher.maxval = (float)atof( token );
matcher.isnumeric = true;
}
else
{
if ( *in == ',' )
{
// If there's a comma, this better have been a less than or a gt key
Assert( 0 );
}
matcher.notequal = nt;
matcher.isnumeric = AppearsToBeANumber( token );
}
gt = lt = eq = nt = false;
if ( !(*in) )
{
done = true;
}
}
break;
case '!':
nt = true;
break;
default:
rawtoken[ n++ ] = *in;
break;
}
in++;
}
matcher.SetToken( token );
matcher.SetRaw( rawtoken );
matcher.valid = true;
}
bool CResponseSystem::CompareUsingMatcher( const char *setValue, Matcher& m, bool verbose /*=false*/ )
{
if ( !m.valid )
return false;
float v = (float)atof( setValue );
if ( setValue[0] == '[' )