Counter Strike : Global Offensive Source Code
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.

311 lines
7.9 KiB

  1. //===== Copyright � 1996-2005, Valve Corporation, All rights reserved. ======//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. //===========================================================================//
  8. #include "rrbase.h"
  9. #include <tier2/interval.h>
  10. /*
  11. #include "AI_Criteria.h"
  12. #include "ai_speech.h"
  13. #include <keyvalues.h>
  14. #include "engine/IEngineSound.h"
  15. */
  16. // memdbgon must be the last include file in a .cpp file!!!
  17. #include <tier0/memdbgon.h>
  18. using namespace ResponseRules;
  19. //-----------------------------------------------------------------------------
  20. // Purpose:
  21. //-----------------------------------------------------------------------------
  22. CRR_Response::CRR_Response() : m_fMatchScore(0)
  23. {
  24. m_Type = ResponseRules::RESPONSE_NONE;
  25. m_szResponseName[0] = 0;
  26. m_szMatchingRule[0]=0;
  27. m_szContext = NULL;
  28. m_bApplyContextToWorld = false;
  29. }
  30. //-----------------------------------------------------------------------------
  31. //-----------------------------------------------------------------------------
  32. CRR_Response::CRR_Response( const CRR_Response &from ) : m_fMatchScore(0)
  33. {
  34. // Assert( (void*)(&m_Type) == (void*)this );
  35. Invalidate();
  36. memcpy( this, &from, sizeof(*this) );
  37. m_szContext = NULL;
  38. SetContext( from.m_szContext );
  39. m_bApplyContextToWorld = from.m_bApplyContextToWorld;
  40. }
  41. //-----------------------------------------------------------------------------
  42. CRR_Response &CRR_Response::operator=( const CRR_Response &from )
  43. {
  44. // Assert( (void*)(&m_Type) == (void*)this );
  45. Invalidate();
  46. memcpy( this, &from, sizeof(*this) );
  47. m_szContext = NULL;
  48. SetContext( from.m_szContext );
  49. m_bApplyContextToWorld = from.m_bApplyContextToWorld;
  50. return *this;
  51. }
  52. //-----------------------------------------------------------------------------
  53. // Purpose:
  54. //-----------------------------------------------------------------------------
  55. CRR_Response::~CRR_Response()
  56. {
  57. if (m_szContext)
  58. delete[] m_szContext;
  59. }
  60. void CRR_Response::Invalidate()
  61. {
  62. if (m_szContext)
  63. {
  64. delete[] m_szContext;
  65. m_szContext = NULL;
  66. }
  67. m_Type = ResponseRules::RESPONSE_NONE;
  68. m_szResponseName[0] = 0;
  69. // not really necessary:
  70. /*
  71. m_szMatchingRule[0]=0;
  72. m_szContext = NULL;
  73. m_bApplyContextToWorld = false;
  74. */
  75. }
  76. // please do not new or delete CRR_Responses.
  77. void CRR_Response::operator delete(void* p)
  78. {
  79. AssertMsg(false, "DO NOT new or delete CRR_Response s.");
  80. free(p);
  81. }
  82. //-----------------------------------------------------------------------------
  83. // Purpose:
  84. // Input : *response -
  85. // *criteria -
  86. //-----------------------------------------------------------------------------
  87. void CRR_Response::Init( ResponseType_t type, const char *responseName, const ResponseParams& responseparams, const char *ruleName, const char *applyContext, bool bApplyContextToWorld )
  88. {
  89. m_Type = type;
  90. Q_strncpy( m_szResponseName, responseName, sizeof( m_szResponseName ) );
  91. // Copy underlying criteria
  92. Q_strncpy( m_szMatchingRule, ruleName ? ruleName : "NULL", sizeof( m_szMatchingRule ) );
  93. m_Params = responseparams;
  94. SetContext( applyContext );
  95. m_bApplyContextToWorld = bApplyContextToWorld;
  96. }
  97. //-----------------------------------------------------------------------------
  98. // Purpose: Debug-print the response. You can optionally pass in the criteria
  99. // used to come up with this response (usually present in the calling function)
  100. // if you want to print that as well. DO NOT store the entire criteria set in
  101. // CRR_Response just to make this debug print cleaner.
  102. //-----------------------------------------------------------------------------
  103. void CRR_Response::Describe( const CriteriaSet *pDebugCriteria )
  104. {
  105. if ( pDebugCriteria )
  106. {
  107. DevMsg( "Search criteria:\n" );
  108. pDebugCriteria->Describe();
  109. }
  110. if ( m_szMatchingRule[ 0 ] )
  111. {
  112. DevMsg( "Matched rule '%s', ", m_szMatchingRule );
  113. }
  114. if ( m_szContext )
  115. {
  116. DevMsg( "Contexts to set '%s' on %s, ", m_szContext, m_bApplyContextToWorld ? "world" : "speaker" );
  117. }
  118. DevMsg( "response %s = '%s'\n", DescribeResponse( (ResponseType_t)m_Type ), m_szResponseName );
  119. }
  120. //-----------------------------------------------------------------------------
  121. // Purpose:
  122. // Output : char const
  123. //-----------------------------------------------------------------------------
  124. void CRR_Response::GetName( char *buf, size_t buflen ) const
  125. {
  126. Q_strncpy( buf, m_szResponseName, buflen );
  127. }
  128. //-----------------------------------------------------------------------------
  129. // Purpose:
  130. // Output : char const
  131. //-----------------------------------------------------------------------------
  132. void CRR_Response::GetResponse( char *buf, size_t buflen ) const
  133. {
  134. GetName( buf, buflen );
  135. }
  136. //-----------------------------------------------------------------------------
  137. // Purpose:
  138. // Input : type -
  139. // Output : char const
  140. //-----------------------------------------------------------------------------
  141. const char *CRR_Response::DescribeResponse( ResponseType_t type )
  142. {
  143. if ( (int)type < 0 || (int)type >= ResponseRules::NUM_RESPONSES )
  144. {
  145. Assert( 0 );
  146. return "???CRR_Response bogus index";
  147. }
  148. switch( type )
  149. {
  150. default:
  151. {
  152. Assert( 0 );
  153. }
  154. // Fall through
  155. case ResponseRules::RESPONSE_NONE:
  156. return "RESPONSE_NONE";
  157. case ResponseRules::RESPONSE_SPEAK:
  158. return "RESPONSE_SPEAK";
  159. case ResponseRules::RESPONSE_SENTENCE:
  160. return "RESPONSE_SENTENCE";
  161. case ResponseRules::RESPONSE_SCENE:
  162. return "RESPONSE_SCENE";
  163. case ResponseRules::RESPONSE_RESPONSE:
  164. return "RESPONSE_RESPONSE";
  165. case ResponseRules::RESPONSE_PRINT:
  166. return "RESPONSE_PRINT";
  167. case ResponseRules::RESPONSE_ENTITYIO:
  168. return "RESPONSE_ENTITYIO";
  169. }
  170. return "RESPONSE_NONE";
  171. }
  172. /*
  173. //-----------------------------------------------------------------------------
  174. // Purpose:
  175. //-----------------------------------------------------------------------------
  176. void CRR_Response::Release()
  177. {
  178. delete this;
  179. }
  180. */
  181. //-----------------------------------------------------------------------------
  182. // Purpose:
  183. // Output : soundlevel_t
  184. //-----------------------------------------------------------------------------
  185. soundlevel_t CRR_Response::GetSoundLevel() const
  186. {
  187. if ( m_Params.flags & ResponseParams::RG_SOUNDLEVEL )
  188. {
  189. return (soundlevel_t)m_Params.soundlevel;
  190. }
  191. return SNDLVL_TALKING;
  192. }
  193. float CRR_Response::GetRespeakDelay( void ) const
  194. {
  195. if ( m_Params.flags & ResponseParams::RG_RESPEAKDELAY )
  196. {
  197. interval_t temp;
  198. m_Params.respeakdelay.ToInterval( temp );
  199. return RandomInterval( temp );
  200. }
  201. return 0.0f;
  202. }
  203. float CRR_Response::GetWeaponDelay( void ) const
  204. {
  205. if ( m_Params.flags & ResponseParams::RG_WEAPONDELAY )
  206. {
  207. interval_t temp;
  208. m_Params.weapondelay.ToInterval( temp );
  209. return RandomInterval( temp );
  210. }
  211. return 0.0f;
  212. }
  213. bool CRR_Response::GetSpeakOnce( void ) const
  214. {
  215. if ( m_Params.flags & ResponseParams::RG_SPEAKONCE )
  216. {
  217. return true;
  218. }
  219. return false;
  220. }
  221. bool CRR_Response::ShouldntUseScene( void ) const
  222. {
  223. return ( m_Params.flags & ResponseParams::RG_DONT_USE_SCENE ) != 0;
  224. }
  225. bool CRR_Response::ShouldBreakOnNonIdle( void ) const
  226. {
  227. return ( m_Params.flags & ResponseParams::RG_STOP_ON_NONIDLE ) != 0;
  228. }
  229. int CRR_Response::GetOdds( void ) const
  230. {
  231. if ( m_Params.flags & ResponseParams::RG_ODDS )
  232. {
  233. return m_Params.odds;
  234. }
  235. return 100;
  236. }
  237. float CRR_Response::GetDelay() const
  238. {
  239. if ( m_Params.flags & ResponseParams::RG_DELAYAFTERSPEAK )
  240. {
  241. interval_t temp;
  242. m_Params.delay.ToInterval( temp );
  243. return RandomInterval( temp );
  244. }
  245. return 0.0f;
  246. }
  247. float CRR_Response::GetPreDelay() const
  248. {
  249. if ( m_Params.flags & ResponseParams::RG_DELAYBEFORESPEAK )
  250. {
  251. interval_t temp;
  252. m_Params.predelay.ToInterval( temp );
  253. return RandomInterval( temp );
  254. }
  255. return 0.0f;
  256. }
  257. //-----------------------------------------------------------------------------
  258. // Purpose: Sets context string
  259. // Output : void
  260. //-----------------------------------------------------------------------------
  261. void CRR_Response::SetContext( const char *context )
  262. {
  263. if (m_szContext)
  264. {
  265. delete[] m_szContext;
  266. m_szContext = NULL;
  267. }
  268. if ( context )
  269. {
  270. int len = Q_strlen( context );
  271. m_szContext = new char[ len + 1 ];
  272. Q_memcpy( m_szContext, context, len );
  273. m_szContext[ len ] = 0;
  274. }
  275. }