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.

251 lines
8.2 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. // Author: Michael S. Booth ([email protected]), 2003
  8. #ifndef _BOT_PROFILE_H_
  9. #define _BOT_PROFILE_H_
  10. #pragma warning( disable : 4786 ) // long STL names get truncated in browse info.
  11. #include "bot_constants.h"
  12. #include "bot_util.h"
  13. #include "cs_weapon_parse.h"
  14. enum
  15. {
  16. FirstCustomSkin = 100,
  17. NumCustomSkins = 100,
  18. LastCustomSkin = FirstCustomSkin + NumCustomSkins - 1,
  19. };
  20. //--------------------------------------------------------------------------------------------------------------
  21. /**
  22. * A BotProfile describes the "personality" of a given bot
  23. */
  24. class BotProfile
  25. {
  26. public:
  27. BotProfile( void )
  28. {
  29. m_name = NULL;
  30. m_aggression = 0.0f;
  31. m_skill = 0.0f;
  32. m_teamwork = 0.0f;
  33. m_weaponPreferenceCount = 0;
  34. m_cost = 0;
  35. m_skin = 0;
  36. m_difficultyFlags = 0;
  37. m_voicePitch = 100;
  38. m_reactionTime = 0.3f;
  39. m_attackDelay = 0.0f;
  40. m_teams = TEAM_UNASSIGNED;
  41. m_voiceBank = 0;
  42. m_prefersSilencer = false;
  43. }
  44. ~BotProfile( void )
  45. {
  46. if ( m_name )
  47. delete [] m_name;
  48. }
  49. const char *GetName( void ) const { return m_name; } ///< return bot's name
  50. float GetAggression( void ) const { return m_aggression; }
  51. float GetSkill( void ) const { return m_skill; }
  52. float GetTeamwork( void ) const { return m_teamwork; }
  53. CSWeaponID GetWeaponPreference( int i ) const { return m_weaponPreference[ i ]; }
  54. const char *GetWeaponPreferenceAsString( int i ) const;
  55. int GetWeaponPreferenceCount( void ) const { return m_weaponPreferenceCount; }
  56. bool HasPrimaryPreference( void ) const; ///< return true if this profile has a primary weapon preference
  57. bool HasPistolPreference( void ) const; ///< return true if this profile has a pistol weapon preference
  58. int GetCost( void ) const { return m_cost; }
  59. int GetSkin( void ) const { return m_skin; }
  60. bool IsDifficulty( BotDifficultyType diff ) const; ///< return true if this profile can be used for the given difficulty level
  61. int GetVoicePitch( void ) const { return m_voicePitch; }
  62. float GetReactionTime( void ) const { return m_reactionTime; }
  63. float GetAttackDelay( void ) const { return m_attackDelay; }
  64. int GetVoiceBank() const { return m_voiceBank; }
  65. bool IsValidForTeam( int team ) const;
  66. bool PrefersSilencer() const { return m_prefersSilencer; }
  67. bool InheritsFrom( const char *name ) const;
  68. private:
  69. friend class BotProfileManager; ///< for loading profiles
  70. void Inherit( const BotProfile *parent, const BotProfile *baseline ); ///< copy values from parent if they differ from baseline
  71. char *m_name; ///< the bot's name
  72. float m_aggression; ///< percentage: 0 = coward, 1 = berserker
  73. float m_skill; ///< percentage: 0 = terrible, 1 = expert
  74. float m_teamwork; ///< percentage: 0 = rogue, 1 = complete obeyance to team, lots of comm
  75. enum { MAX_WEAPON_PREFS = 16 };
  76. CSWeaponID m_weaponPreference[ MAX_WEAPON_PREFS ]; ///< which weapons this bot likes to use, in order of priority
  77. int m_weaponPreferenceCount;
  78. int m_cost; ///< reputation point cost for career mode
  79. int m_skin; ///< "skin" index
  80. unsigned char m_difficultyFlags; ///< bits set correspond to difficulty levels this is valid for
  81. int m_voicePitch; ///< the pitch shift for bot chatter (100 = normal)
  82. float m_reactionTime; //< our reaction time in seconds
  83. float m_attackDelay; ///< time in seconds from when we notice an enemy to when we open fire
  84. int m_teams; ///< teams for which this profile is valid
  85. bool m_prefersSilencer; ///< does the bot prefer to use silencers?
  86. int m_voiceBank; ///< Index of the BotChatter.db voice bank this profile uses (0 is the default)
  87. CUtlVector< const BotProfile * > m_templates; ///< List of templates we inherit from
  88. };
  89. typedef CUtlLinkedList<BotProfile *> BotProfileList;
  90. inline bool BotProfile::IsDifficulty( BotDifficultyType diff ) const
  91. {
  92. return (m_difficultyFlags & (1 << diff)) ? true : false;
  93. }
  94. /**
  95. * Copy in data from parent if it differs from the baseline
  96. */
  97. inline void BotProfile::Inherit( const BotProfile *parent, const BotProfile *baseline )
  98. {
  99. if (parent->m_aggression != baseline->m_aggression)
  100. m_aggression = parent->m_aggression;
  101. if (parent->m_skill != baseline->m_skill)
  102. m_skill = parent->m_skill;
  103. if (parent->m_teamwork != baseline->m_teamwork)
  104. m_teamwork = parent->m_teamwork;
  105. if (parent->m_weaponPreferenceCount != baseline->m_weaponPreferenceCount)
  106. {
  107. m_weaponPreferenceCount = parent->m_weaponPreferenceCount;
  108. for( int i=0; i<parent->m_weaponPreferenceCount; ++i )
  109. m_weaponPreference[i] = parent->m_weaponPreference[i];
  110. }
  111. if (parent->m_cost != baseline->m_cost)
  112. m_cost = parent->m_cost;
  113. if (parent->m_skin != baseline->m_skin)
  114. m_skin = parent->m_skin;
  115. if (parent->m_difficultyFlags != baseline->m_difficultyFlags)
  116. m_difficultyFlags = parent->m_difficultyFlags;
  117. if (parent->m_voicePitch != baseline->m_voicePitch)
  118. m_voicePitch = parent->m_voicePitch;
  119. if (parent->m_reactionTime != baseline->m_reactionTime)
  120. m_reactionTime = parent->m_reactionTime;
  121. if (parent->m_attackDelay != baseline->m_attackDelay)
  122. m_attackDelay = parent->m_attackDelay;
  123. if (parent->m_teams != baseline->m_teams)
  124. m_teams = parent->m_teams;
  125. if (parent->m_voiceBank != baseline->m_voiceBank)
  126. m_voiceBank = parent->m_voiceBank;
  127. m_templates.AddToTail( parent );
  128. }
  129. //--------------------------------------------------------------------------------------------------------------
  130. /**
  131. * The BotProfileManager defines the interface to accessing BotProfiles
  132. */
  133. class BotProfileManager
  134. {
  135. public:
  136. BotProfileManager( void );
  137. ~BotProfileManager( void );
  138. void Init( const char *filename, unsigned int *checksum = NULL );
  139. void Reset( void );
  140. /// given a name, return a profile
  141. const BotProfile *GetProfile( const char *name, int team ) const
  142. {
  143. FOR_EACH_LL( m_profileList, it )
  144. {
  145. BotProfile *profile = m_profileList[ it ];
  146. if ( !stricmp( name, profile->GetName() ) && profile->IsValidForTeam( team ) )
  147. return profile;
  148. }
  149. return NULL;
  150. }
  151. /// given a template name and difficulty, return a profile
  152. const BotProfile *GetProfileMatchingTemplate( const char *profileName, int team, BotDifficultyType difficulty ) const
  153. {
  154. FOR_EACH_LL( m_profileList, it )
  155. {
  156. BotProfile *profile = m_profileList[ it ];
  157. if ( !profile->InheritsFrom( profileName ) )
  158. continue;
  159. if ( !profile->IsValidForTeam( team ) )
  160. continue;
  161. if ( !profile->IsDifficulty( difficulty ) )
  162. continue;
  163. if ( UTIL_IsNameTaken( profile->GetName() ) )
  164. continue;
  165. return profile;
  166. }
  167. return NULL;
  168. }
  169. const BotProfileList *GetProfileList( void ) const { return &m_profileList; } ///< return list of all profiles
  170. const BotProfile *GetRandomProfile( BotDifficultyType difficulty, int team, CSWeaponType weaponType ) const; ///< return random unused profile that matches the given difficulty level
  171. const char * GetCustomSkin( int index ); ///< Returns custom skin name at a particular index
  172. const char * GetCustomSkinModelname( int index ); ///< Returns custom skin modelname at a particular index
  173. const char * GetCustomSkinFname( int index ); ///< Returns custom skin filename at a particular index
  174. int GetCustomSkinIndex( const char *name, const char *filename = NULL ); ///< Looks up a custom skin index by name
  175. typedef CUtlVector<char *> VoiceBankList;
  176. const VoiceBankList *GetVoiceBanks( void ) const { return &m_voiceBanks; }
  177. int FindVoiceBankIndex( const char *filename ); ///< return index of the (custom) bot phrase db, inserting it if needed
  178. protected:
  179. BotProfileList m_profileList; ///< the list of all bot profiles
  180. BotProfileList m_templateList; ///< the list of all bot templates
  181. VoiceBankList m_voiceBanks;
  182. char *m_skins[ NumCustomSkins ]; ///< Custom skin names
  183. char *m_skinModelnames[ NumCustomSkins ]; ///< Custom skin modelnames
  184. char *m_skinFilenames[ NumCustomSkins ]; ///< Custom skin filenames
  185. int m_nextSkin; ///< Next custom skin to allocate
  186. };
  187. /// the global singleton for accessing BotProfiles
  188. extern BotProfileManager *TheBotProfiles;
  189. #endif // _BOT_PROFILE_H_