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.

287 lines
8.9 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #ifndef AI_NAMESPACES_H
  8. #define AI_NAMESPACES_H
  9. class CStringRegistry;
  10. #if defined( _WIN32 )
  11. #pragma once
  12. #endif
  13. #define MAX_STRING_INDEX 9999
  14. const int GLOBAL_IDS_BASE = 1000000000; // decimal for debugging readability
  15. //-----------------------------------------------------------------------------
  16. inline bool AI_IdIsGlobal( int id ) { return ( id >= GLOBAL_IDS_BASE || id == -1 ); }
  17. inline bool AI_IdIsLocal( int id ) { return ( id < GLOBAL_IDS_BASE || id == -1 ); }
  18. inline int AI_RemapToGlobal( int id ) { return ( id != -1 ) ? id + GLOBAL_IDS_BASE : -1; }
  19. inline int AI_RemapFromGlobal( int id ) { return ( id != -1 ) ? id - GLOBAL_IDS_BASE : -1; }
  20. inline int AI_MakeGlobal( int id ) { return AI_IdIsLocal( id ) ? AI_RemapToGlobal( id ) : id; }
  21. //-----------------------------------------------------------------------------
  22. // CAI_GlobalNamespace
  23. //
  24. // Purpose: Symbol table for all symbols across a given namespace, a
  25. // bi-directional mapping of "text" to global ID
  26. //
  27. class CAI_GlobalNamespace
  28. {
  29. public:
  30. CAI_GlobalNamespace();
  31. ~CAI_GlobalNamespace();
  32. void Clear();
  33. void AddSymbol( const char *pszSymbol, int symbolID );
  34. int NextGlobalBase() const;
  35. const char *IdToSymbol( int symbolID ) const;
  36. int SymbolToId( const char *pszSymbol ) const;
  37. private:
  38. CStringRegistry * m_pSymbols;
  39. int m_NextGlobalBase;
  40. };
  41. //-----------------------------------------------------------------------------
  42. // CAI_LocalIdSpace
  43. //
  44. // Purpose: Maps per class IDs to global IDs, so that various classes can use
  45. // the same integer in local space to represent different globally
  46. // unique integers. Used for schedules, tasks, conditions and squads
  47. //
  48. class CAI_LocalIdSpace
  49. {
  50. public:
  51. CAI_LocalIdSpace( bool fIsRoot = false );
  52. bool Init( CAI_GlobalNamespace *pGlobalNamespace, CAI_LocalIdSpace *pParentIDSpace = NULL );
  53. bool IsGlobalBaseSet() const { return ( m_globalBase != -1 ); }
  54. bool AddSymbol( const char *pszSymbol, int localId, const char *pszDebugSymbolType = "", const char *pszDebugOwner = "" );
  55. int GlobalToLocal( int globalID ) const;
  56. int LocalToGlobal( int localID ) const;
  57. CAI_GlobalNamespace *GetGlobalNamespace() { return m_pGlobalNamespace; }
  58. const CAI_GlobalNamespace *GetGlobalNamespace() const { return m_pGlobalNamespace; }
  59. private:
  60. bool IsLocalBaseSet() const { return ( m_localBase != MAX_STRING_INDEX ); }
  61. int GetLocalBase() const { return m_localBase; }
  62. int GetGlobalBase() const { return m_globalBase; }
  63. int GetLocalTop() const { return m_localTop; }
  64. int GetGlobalTop() const { return m_globalTop; }
  65. bool SetLocalBase( int newBase );
  66. // --------------------------------
  67. int m_globalBase;
  68. int m_localBase;
  69. int m_localTop;
  70. int m_globalTop;
  71. CAI_LocalIdSpace * m_pParentIDSpace;
  72. CAI_GlobalNamespace * m_pGlobalNamespace;
  73. };
  74. //-----------------------------------------------------------------------------
  75. //
  76. // Namespaces used by CAI_BaseNPC
  77. //
  78. //-----------------------------------------------------------------------------
  79. class CAI_GlobalScheduleNamespace
  80. {
  81. public:
  82. void Clear()
  83. {
  84. m_ScheduleNamespace.Clear();
  85. m_TaskNamespace.Clear();
  86. m_ConditionNamespace.Clear();
  87. }
  88. void AddSchedule( const char *pszSchedule, int scheduleID );
  89. const char *ScheduleIdToSymbol( int scheduleID ) const;
  90. int ScheduleSymbolToId( const char *pszSchedule ) const;
  91. void AddTask( const char *pszTask, int taskID );
  92. const char *TaskIdToSymbol( int taskID ) const;
  93. int TaskSymbolToId( const char *pszTask ) const;
  94. void AddCondition( const char *pszCondition, int conditionID );
  95. const char *ConditionIdToSymbol( int conditionID ) const;
  96. int ConditionSymbolToId( const char *pszCondition ) const;
  97. int NumConditions() const;
  98. private:
  99. friend class CAI_ClassScheduleIdSpace;
  100. CAI_GlobalNamespace m_ScheduleNamespace;
  101. CAI_GlobalNamespace m_TaskNamespace;
  102. CAI_GlobalNamespace m_ConditionNamespace;
  103. };
  104. //-------------------------------------
  105. class CAI_ClassScheduleIdSpace
  106. {
  107. public:
  108. CAI_ClassScheduleIdSpace( bool fIsRoot = false )
  109. : m_ScheduleIds( fIsRoot ),
  110. m_TaskIds( fIsRoot ),
  111. m_ConditionIds( fIsRoot )
  112. {
  113. }
  114. bool Init( const char *pszClassName, CAI_GlobalScheduleNamespace *pGlobalNamespace, CAI_ClassScheduleIdSpace *pParentIDSpace = NULL );
  115. const char *GetClassName() const { return m_pszClassName; }
  116. bool IsGlobalBaseSet() const;
  117. bool AddSchedule( const char *pszSymbol, int localId, const char *pszDebugOwner = "" );
  118. int ScheduleGlobalToLocal( int globalID ) const;
  119. int ScheduleLocalToGlobal( int localID ) const;
  120. bool AddTask( const char *pszSymbol, int localId, const char *pszDebugOwner = "" );
  121. int TaskGlobalToLocal( int globalID ) const;
  122. int TaskLocalToGlobal( int localID ) const;
  123. bool AddCondition( const char *pszSymbol, int localId, const char *pszDebugOwner = "" );
  124. int ConditionGlobalToLocal( int globalID ) const;
  125. int ConditionLocalToGlobal( int localID ) const;
  126. private:
  127. const char * m_pszClassName;
  128. CAI_LocalIdSpace m_ScheduleIds;
  129. CAI_LocalIdSpace m_TaskIds;
  130. CAI_LocalIdSpace m_ConditionIds;
  131. };
  132. //-----------------------------------------------------------------------------
  133. inline void CAI_GlobalScheduleNamespace::AddSchedule( const char *pszSchedule, int scheduleID )
  134. {
  135. m_ScheduleNamespace.AddSymbol( pszSchedule, scheduleID);
  136. }
  137. inline const char *CAI_GlobalScheduleNamespace::ScheduleIdToSymbol( int scheduleID ) const
  138. {
  139. return m_ScheduleNamespace.IdToSymbol( scheduleID );
  140. }
  141. inline int CAI_GlobalScheduleNamespace::ScheduleSymbolToId( const char *pszSchedule ) const
  142. {
  143. return m_ScheduleNamespace.SymbolToId( pszSchedule );
  144. }
  145. inline void CAI_GlobalScheduleNamespace::AddTask( const char *pszTask, int taskID )
  146. {
  147. m_TaskNamespace.AddSymbol( pszTask, taskID);
  148. }
  149. inline const char *CAI_GlobalScheduleNamespace::TaskIdToSymbol( int taskID ) const
  150. {
  151. return m_TaskNamespace.IdToSymbol( taskID );
  152. }
  153. inline int CAI_GlobalScheduleNamespace::TaskSymbolToId( const char *pszTask ) const
  154. {
  155. return m_TaskNamespace.SymbolToId( pszTask );
  156. }
  157. inline void CAI_GlobalScheduleNamespace::AddCondition( const char *pszCondition, int conditionID )
  158. {
  159. m_ConditionNamespace.AddSymbol( pszCondition, conditionID);
  160. }
  161. inline const char *CAI_GlobalScheduleNamespace::ConditionIdToSymbol( int conditionID ) const
  162. {
  163. return m_ConditionNamespace.IdToSymbol( conditionID );
  164. }
  165. inline int CAI_GlobalScheduleNamespace::ConditionSymbolToId( const char *pszCondition ) const
  166. {
  167. return m_ConditionNamespace.SymbolToId( pszCondition );
  168. }
  169. inline int CAI_GlobalScheduleNamespace::NumConditions() const
  170. {
  171. return m_ConditionNamespace.NextGlobalBase() - GLOBAL_IDS_BASE;
  172. }
  173. inline bool CAI_ClassScheduleIdSpace::Init( const char *pszClassName, CAI_GlobalScheduleNamespace *pGlobalNamespace, CAI_ClassScheduleIdSpace *pParentIDSpace )
  174. {
  175. m_pszClassName = pszClassName;
  176. return ( m_ScheduleIds.Init( &pGlobalNamespace->m_ScheduleNamespace, ( pParentIDSpace ) ? &pParentIDSpace->m_ScheduleIds : NULL ) &&
  177. m_TaskIds.Init( &pGlobalNamespace->m_TaskNamespace, ( pParentIDSpace ) ? &pParentIDSpace->m_TaskIds : NULL ) &&
  178. m_ConditionIds.Init( &pGlobalNamespace->m_ConditionNamespace, ( pParentIDSpace ) ? &pParentIDSpace->m_ConditionIds : NULL ) );
  179. }
  180. //-----------------------------------------------------------------------------
  181. inline bool CAI_ClassScheduleIdSpace::IsGlobalBaseSet() const
  182. {
  183. return m_ScheduleIds.IsGlobalBaseSet();
  184. }
  185. inline bool CAI_ClassScheduleIdSpace::AddSchedule( const char *pszSymbol, int localId, const char *pszDebugOwner )
  186. {
  187. return m_ScheduleIds.AddSymbol( pszSymbol, localId, "schedule", pszDebugOwner );
  188. }
  189. inline int CAI_ClassScheduleIdSpace::ScheduleGlobalToLocal( int globalID ) const
  190. {
  191. return m_ScheduleIds.GlobalToLocal( globalID );
  192. }
  193. inline int CAI_ClassScheduleIdSpace::ScheduleLocalToGlobal( int localID ) const
  194. {
  195. return m_ScheduleIds.LocalToGlobal( localID );
  196. }
  197. inline bool CAI_ClassScheduleIdSpace::AddTask( const char *pszSymbol, int localId, const char *pszDebugOwner )
  198. {
  199. return m_TaskIds.AddSymbol( pszSymbol, localId, "task", pszDebugOwner );
  200. }
  201. inline int CAI_ClassScheduleIdSpace::TaskGlobalToLocal( int globalID ) const
  202. {
  203. return m_TaskIds.GlobalToLocal( globalID );
  204. }
  205. inline int CAI_ClassScheduleIdSpace::TaskLocalToGlobal( int localID ) const
  206. {
  207. return m_TaskIds.LocalToGlobal( localID );
  208. }
  209. inline bool CAI_ClassScheduleIdSpace::AddCondition( const char *pszSymbol, int localId, const char *pszDebugOwner )
  210. {
  211. return m_ConditionIds.AddSymbol( pszSymbol, localId, "condition", pszDebugOwner );
  212. }
  213. inline int CAI_ClassScheduleIdSpace::ConditionGlobalToLocal( int globalID ) const
  214. {
  215. return m_ConditionIds.GlobalToLocal( globalID );
  216. }
  217. inline int CAI_ClassScheduleIdSpace::ConditionLocalToGlobal( int localID ) const
  218. {
  219. return m_ConditionIds.LocalToGlobal( localID );
  220. }
  221. //=============================================================================
  222. #endif // AI_NAMESPACES_H