Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

239 lines
5.4 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================//
  6. #ifndef AI_CRITERIA_H
  7. #define AI_CRITERIA_H
  8. #ifdef _WIN32
  9. #pragma once
  10. #endif
  11. #include "tier1/utlrbtree.h"
  12. #include "tier1/utlsymbol.h"
  13. #include "interval.h"
  14. #include "mathlib/compressed_vector.h"
  15. extern const char *SplitContext( const char *raw, char *key, int keylen, char *value, int valuelen, float *duration );
  16. class AI_CriteriaSet
  17. {
  18. public:
  19. AI_CriteriaSet();
  20. AI_CriteriaSet( const AI_CriteriaSet& src );
  21. ~AI_CriteriaSet();
  22. void AppendCriteria( const char *criteria, const char *value = "", float weight = 1.0f );
  23. void RemoveCriteria( const char *criteria );
  24. void Describe();
  25. int GetCount() const;
  26. int FindCriterionIndex( const char *name ) const;
  27. const char *GetName( int index ) const;
  28. const char *GetValue( int index ) const;
  29. float GetWeight( int index ) const;
  30. private:
  31. struct CritEntry_t
  32. {
  33. CritEntry_t() :
  34. criterianame( UTL_INVAL_SYMBOL ),
  35. weight( 0.0f )
  36. {
  37. value[ 0 ] = 0;
  38. }
  39. CritEntry_t( const CritEntry_t& src )
  40. {
  41. criterianame = src.criterianame;
  42. value[ 0 ] = 0;
  43. weight = src.weight;
  44. SetValue( src.value );
  45. }
  46. CritEntry_t& operator=( const CritEntry_t& src )
  47. {
  48. if ( this == &src )
  49. return *this;
  50. criterianame = src.criterianame;
  51. weight = src.weight;
  52. SetValue( src.value );
  53. return *this;
  54. }
  55. static bool LessFunc( const CritEntry_t& lhs, const CritEntry_t& rhs )
  56. {
  57. return Q_stricmp( lhs.criterianame.String(), rhs.criterianame.String() ) < 0 ? true : false;
  58. }
  59. void SetValue( char const *str )
  60. {
  61. if ( !str )
  62. {
  63. value[ 0 ] = 0;
  64. }
  65. else
  66. {
  67. Q_strncpy( value, str, sizeof( value ) );
  68. }
  69. }
  70. // We use CUtlRBTree CopyFrom() in ctor, so CritEntry_t must be POD. If you add
  71. // CUtlString or something then you must change AI_CriteriaSet copy ctor.
  72. CUtlSymbol criterianame;
  73. char value[ 64 ];
  74. float weight;
  75. };
  76. CUtlRBTree< CritEntry_t, short > m_Lookup;
  77. };
  78. #pragma pack(1)
  79. template<typename T>
  80. struct response_interval_t
  81. {
  82. T start;
  83. T range;
  84. interval_t &ToInterval( interval_t &dest ) const { dest.start = start; dest.range = range; return dest; }
  85. void FromInterval( const interval_t &from ) { start = from.start; range = from.range; }
  86. float Random() const { interval_t temp = { start, range }; return RandomInterval( temp ); }
  87. };
  88. typedef response_interval_t<float16_with_assign> responseparams_interval_t;
  89. struct AI_ResponseParams
  90. {
  91. DECLARE_SIMPLE_DATADESC();
  92. enum
  93. {
  94. RG_DELAYAFTERSPEAK = (1<<0),
  95. RG_SPEAKONCE = (1<<1),
  96. RG_ODDS = (1<<2),
  97. RG_RESPEAKDELAY = (1<<3),
  98. RG_SOUNDLEVEL = (1<<4),
  99. RG_DONT_USE_SCENE = (1<<5),
  100. RG_STOP_ON_NONIDLE = (1<<6),
  101. RG_WEAPONDELAY = (1<<7),
  102. RG_DELAYBEFORESPEAK = (1<<8),
  103. };
  104. AI_ResponseParams()
  105. {
  106. flags = 0;
  107. odds = 100;
  108. delay.start = 0;
  109. delay.range = 0;
  110. respeakdelay.start = 0;
  111. respeakdelay.range = 0;
  112. weapondelay.start = 0;
  113. weapondelay.range = 0;
  114. soundlevel = 0;
  115. predelay.start = 0;
  116. predelay.range = 0;
  117. }
  118. responseparams_interval_t delay; //4
  119. responseparams_interval_t respeakdelay; //8
  120. responseparams_interval_t weapondelay; //12
  121. short odds; //14
  122. short flags; //16
  123. byte soundlevel; //17
  124. responseparams_interval_t predelay; //21
  125. };
  126. #pragma pack()
  127. //-----------------------------------------------------------------------------
  128. // Purpose: Generic container for a response to a match to a criteria set
  129. // This is what searching for a response returns
  130. //-----------------------------------------------------------------------------
  131. enum ResponseType_t
  132. {
  133. RESPONSE_NONE = 0,
  134. RESPONSE_SPEAK,
  135. RESPONSE_SENTENCE,
  136. RESPONSE_SCENE,
  137. RESPONSE_RESPONSE, // A reference to another response by name
  138. RESPONSE_PRINT,
  139. NUM_RESPONSES,
  140. };
  141. class AI_Response
  142. {
  143. public:
  144. DECLARE_SIMPLE_DATADESC();
  145. AI_Response();
  146. AI_Response( const AI_Response &from );
  147. ~AI_Response();
  148. AI_Response &operator=( const AI_Response &from );
  149. void Release();
  150. const char * GetNamePtr() const;
  151. const char * GetResponsePtr() const;
  152. const AI_ResponseParams *GetParams() const { return &m_Params; }
  153. ResponseType_t GetType() const { return (ResponseType_t)m_Type; }
  154. soundlevel_t GetSoundLevel() const;
  155. float GetRespeakDelay() const;
  156. float GetWeaponDelay() const;
  157. bool GetSpeakOnce() const;
  158. bool ShouldntUseScene( ) const;
  159. bool ShouldBreakOnNonIdle( void ) const;
  160. int GetOdds() const;
  161. float GetDelay() const;
  162. float GetPreDelay() const;
  163. void SetContext( const char *context );
  164. const char * GetContext( void ) const { return m_szContext.Length() ? m_szContext.Get() : NULL; }
  165. bool IsApplyContextToWorld( void ) { return m_bApplyContextToWorld; }
  166. void Describe();
  167. const AI_CriteriaSet* GetCriteria();
  168. void Init( ResponseType_t type,
  169. const char *responseName,
  170. const AI_CriteriaSet& criteria,
  171. const AI_ResponseParams& responseparams,
  172. const char *matchingRule,
  173. const char *applyContext,
  174. bool bApplyContextToWorld );
  175. static const char *DescribeResponse( ResponseType_t type );
  176. enum
  177. {
  178. MAX_RESPONSE_NAME = 64,
  179. MAX_RULE_NAME = 64
  180. };
  181. private:
  182. byte m_Type;
  183. char m_szResponseName[ MAX_RESPONSE_NAME ];
  184. char m_szMatchingRule[ MAX_RULE_NAME ];
  185. // The initial criteria to which we are responsive
  186. AI_CriteriaSet *m_pCriteria;
  187. AI_ResponseParams m_Params;
  188. CUtlString m_szContext;
  189. bool m_bApplyContextToWorld;
  190. };
  191. #endif // AI_CRITERIA_H