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.

269 lines
11 KiB

  1. //====== Copyright � 1996-2005, Valve Corporation, All rights reserved. =======
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================
  6. #ifndef BUY_PRESETS_H
  7. #define BUY_PRESETS_H
  8. #ifdef _WIN32
  9. #pragma once
  10. #endif
  11. #define USE_BUY_PRESETS 1
  12. /**
  13. * CLASS STRUCTURE:
  14. * Buy presets are managed by TheBuyPresetManager, which has a list of BuyPreset objects.
  15. * Each BuyPreset has a list of fallback WeaponSets. Each WeaponSet is a complete set of
  16. * weapons and equipment that could be purchased by a buy preset.
  17. *
  18. * BUYING:
  19. * When purchasing a buy preset, the fallback WeaponSets are considered in order from first
  20. * to last. When one is deemed to be purchasable, the buy commands are generated and
  21. * processing stops.
  22. *
  23. * EDITING:
  24. * When editing buy presets, TheBuyPresetManager maintains a second list of BuyPresets, to
  25. * allow an all-or-nothing undo system. The editing is done on two main VGUI menus: a
  26. * list of the four main buy presets (CBuyPresetEditMainMenu), and a menu showing the
  27. * fallbacks for a single preset (CBuyPresetEditOutfitListMenu).
  28. *
  29. * Presets and fallbacks can be copied, created, deleted, etc from these pages. From the
  30. * fallback menu, a wizard can edit the contents of a fallback, specifying primary weapon,
  31. * pistol, and equipment.
  32. */
  33. #include "weapon_csbase.h"
  34. #include <keyvalues.h>
  35. #include <utlvector.h>
  36. class BuyPreset;
  37. class CCSWeaponInfo;
  38. //--------------------------------------------------------------------------------------------------------------
  39. enum BuyPresetStringSizes
  40. {
  41. BUY_PRESET_COMMAND_LEN = 256,
  42. MaxBuyPresetName = 64,
  43. MaxBuyPresetImageFname = 64,
  44. };
  45. enum AmmoSizeType
  46. {
  47. AMMO_PERCENT,
  48. AMMO_CLIPS,
  49. AMMO_ROUNDS
  50. };
  51. enum { NUM_PRESETS = 4 };
  52. //--------------------------------------------------------------------------------------------------------------
  53. /**
  54. * A BuyPresetWeapon stores the mp.dll version of the WeaponID, the increment for buying ammo (clips, rounds, etc),
  55. * the number of increments to buy, etc. This is the basic info for buying a weapon that is present in an
  56. * item set.
  57. */
  58. class BuyPresetWeapon
  59. {
  60. public:
  61. BuyPresetWeapon();
  62. explicit BuyPresetWeapon( CSWeaponID weaponID );
  63. //BuyPresetWeapon& operator= (const BuyPresetWeapon& other); // default implementation should work
  64. const wchar_t* GetName() const { return m_name; } ///< Returns the display name corresponding to the weapon ID
  65. CSWeaponID GetCSWeaponID() const { return m_weaponID; } ///< Returns the WEAPON_* weapon ID
  66. void SetWeaponID( CSWeaponID weaponID ); ///< Sets the WEAPON_* weapon ID (and resets ammo, etc)
  67. void SetAmmoType( AmmoSizeType ammoType ) { m_ammoType = ammoType; } ///< Sets the ammo type - percent (unused), clips, or rounds (unused)
  68. void SetAmmoAmount( int ammoAmount ) { m_ammoAmount = ammoAmount; } ///< Sets the amount of ammo, in units relevant to the ammo type
  69. void SetFillAmmo( bool fill ) { m_fillAmmo = fill; } ///< Sets whether the weapon's ammo should be topped off
  70. AmmoSizeType GetAmmoType() const { return m_ammoType; } ///< Returns ammo type - percent (unused), clips, or rounds (unused)
  71. int GetAmmoAmount() const { return m_ammoAmount; } ///< Returns the amount of ammo, in units relevant to the ammo type
  72. bool GetFillAmmo() const { return m_fillAmmo; } ///< Returns true if the weapon's ammo should be topped off
  73. protected:
  74. const wchar_t *m_name;
  75. CSWeaponID m_weaponID;
  76. AmmoSizeType m_ammoType;
  77. int m_ammoAmount;
  78. bool m_fillAmmo;
  79. };
  80. typedef CUtlVector< BuyPresetWeapon > BuyPresetWeaponList;
  81. //--------------------------------------------------------------------------------------------------------------
  82. /**
  83. * A WeaponSet is part of a buy preset. Each buy preset is composed of a series of (fallback) WeaponSets.
  84. * The WeaponSet contains a snapshot of a set of weapons and equipment that should be bought.
  85. */
  86. class WeaponSet
  87. {
  88. public:
  89. WeaponSet();
  90. void GetCurrent( int& cost, WeaponSet& ws ) const; ///< Generates a WeaponSet containing only items that would be bought right now
  91. void GetFromScratch( int& cost, WeaponSet& ws ) const; ///< Generates a WeaponSet containing everything that would be bought from scratch
  92. void GenerateBuyCommands( char command[BUY_PRESET_COMMAND_LEN] ) const; ///< Generates a list of commands to buy the current WeaponSet.
  93. int FullCost() const; ///< Calculates the full cost of the WeaponSet, including all optional items
  94. void Reset( void );
  95. const BuyPresetWeapon& GetPrimaryWeapon() const { return m_primaryWeapon; }
  96. const BuyPresetWeapon& GetSecondaryWeapon() const { return m_secondaryWeapon; }
  97. BuyPresetWeapon m_primaryWeapon;
  98. BuyPresetWeapon m_secondaryWeapon;
  99. // Equipment --------------------------------
  100. int m_armor;
  101. bool m_helmet;
  102. bool m_smokeGrenade;
  103. bool m_HEGrenade;
  104. int m_flashbangs;
  105. bool m_defuser;
  106. bool m_nightvision;
  107. };
  108. typedef CUtlVector< WeaponSet > WeaponSetList;
  109. //--------------------------------------------------------------------------------------------------------------
  110. /**
  111. * The BuyPreset is a complete representation of a buy preset. Essentially, it consists of the preset name,
  112. * whether or not the preset is used for autobuy, and a list of fallback WeaponSets.
  113. */
  114. class BuyPreset
  115. {
  116. public:
  117. BuyPreset();
  118. ~BuyPreset();
  119. BuyPreset(const BuyPreset& other);
  120. void SetName( const wchar_t *name );
  121. const wchar_t * GetName() const { return m_name; }
  122. void Parse( KeyValues *data ); ///< Populates data from a KeyValues structure, for loading
  123. void Save( KeyValues *data ); ///< Fills in a KeyValues structure with data, for saving
  124. int GetNumSets() const { return m_weaponList.Count(); } ///< Returns the number of fallback WeaponSets
  125. const WeaponSet * GetSet( int index ) const; ///< Returns the specified fallback WeaponSet, or NULL if it doesn't exist
  126. int FullCost() const; ///< Calculates the full cost of the preset, which is exactly the full cost of the first fallback WeaponSet
  127. // editing functions --------------------
  128. void DeleteSet( int index ); ///< Deletes a fallback
  129. void SwapSet( int firstIndex, int secondIndex ); ///< Switches the order of two fallbacks
  130. void ReplaceSet( int index, const WeaponSet& weaponSet ); ///< Replaces a fallback with the supplied WeaponSet
  131. private:
  132. wchar_t m_name[MaxBuyPresetName];
  133. WeaponSetList m_weaponList;
  134. };
  135. typedef CUtlVector< BuyPreset > BuyPresetList;
  136. //--------------------------------------------------------------------------------------------------------------
  137. /**
  138. * The BuyPresetManager singleton manages saving/loading/buying the individual BuyPresets. It also tracks the
  139. * ownership of some items that aren't explicitly known by the client (defuser, nightvision).
  140. *
  141. * Two sets of BuyPresets are maintained - the live set used for purchasing, and a set that is acted upon for
  142. * editing. This provides the basic save changes/abandon changes ability when editing presets.
  143. */
  144. class BuyPresetManager
  145. {
  146. public:
  147. BuyPresetManager();
  148. void Save();
  149. void PurchasePreset( int presetIndex ); ///< Generates and sends buy commands to buy a specific preset
  150. int GetNumPresets() { VerifyLoadedTeam(); return m_presets.Count(); }
  151. const BuyPreset * GetPreset( int index ) const; ///< Returns the specified "live" preset, or NULL if it doesn't exist
  152. void SetPreset( int index, const BuyPreset *preset );
  153. void SetPresets( const BuyPresetList& presets ) { m_presets = presets; }
  154. void SetEditPresets( const BuyPresetList& presets ) { m_editPresets = presets; }
  155. int GetNumEditPresets() const { return m_editPresets.Count(); }
  156. BuyPreset * GetEditPreset( int index ); ///< Returns the specified editing preset, or NULL if it doesn't exist
  157. const CUtlVector< BuyPreset >& GetEditPresets() const { return m_editPresets; }
  158. void ResetEditPresets() { m_editPresets = m_presets; } ///< Resets the editing presets to the live presets (e.g. when starting editing)
  159. void ResetEditToDefaults( void ); ///< Loads the default presets into the editing presets (e.g. when hitting the "Use Defaults" button)
  160. void GetCurrentLoadout( WeaponSet *weaponSet );
  161. private:
  162. BuyPresetList m_presets; ///< Current (live) presets
  163. BuyPresetList m_editPresets; ///< BuyPresets used when editing
  164. int m_loadedTeam;
  165. void VerifyLoadedTeam( void );
  166. };
  167. //--------------------------------------------------------------------------------------------------------------
  168. extern BuyPresetManager *TheBuyPresets;
  169. //--------------------------------------------------------------------------------------------------------------
  170. /**
  171. * Functions for controlling the display of the various buy preset editing menus. Opening one closes any
  172. * others open.
  173. */
  174. enum { REOPEN_YES, REOPEN_NO, REOPEN_DONTCHANGE }; ///< Determines if we need to re-open the buy menu when done editing.
  175. void ShowBuyPresetMainMenu( bool runUpdate, int reopenBuyMenu ); ///< Open the main preset editing menu
  176. void ShowBuyPresetEditMenu( int presetIndex ); ///< Open the menu for editing the list of fallbacks for an individual preset
  177. //--------------------------------------------------------------------------------------------------------------
  178. /**
  179. * Constructs a list of weapons that will satisfy the any unfinished weapon-specific career tasks:
  180. * 1. If there are no unfinished career tasks, the source list is returned
  181. * 2. If there are unfinished career tasks, all weapons that can be used for the tasks are added
  182. * to the front of the list. This list of career weapons is sorted according to the order of
  183. * weapons in the source list, so that if any weapons in the source list can be used for career
  184. * tasks, they will be.
  185. */
  186. BuyPresetWeaponList CareerWeaponList( const BuyPresetWeaponList& source, bool isPrimary, CSWeaponID currentClientID );
  187. //--------------------------------------------------------------------------------------------------------------
  188. /**
  189. * Returns the number of clips that will have to be bought for the specified BuyPresetWeapon
  190. */
  191. class CCSWeaponInfo;
  192. int CalcClipsNeeded( const BuyPresetWeapon *pWeapon, const CCSWeaponInfo *pInfo, const int ammo[MAX_AMMO_TYPES] );
  193. //--------------------------------------------------------------------------------------------------------------
  194. /**
  195. * Fills the ammo array with the ammo currently owned by the local player
  196. */
  197. void FillClientAmmo( int ammo[MAX_AMMO_TYPES] );
  198. //--------------------------------------------------------------------------------------------------------------
  199. /**
  200. * Returns true if the client can currently buy the weapon according to class/gamemode restrictions. Returns
  201. * true also if the player owns the weapon already.
  202. */
  203. bool CanBuyWeapon( CSWeaponID currentPrimaryID, CSWeaponID currentSecondaryID, CSWeaponID weaponID );
  204. //--------------------------------------------------------------------------------------------------------------
  205. /**
  206. * Returns the display name for a weapon, based on it's weaponID
  207. */
  208. const wchar_t* WeaponIDToDisplayName( CSWeaponID weaponID );
  209. //--------------------------------------------------------------------------------------------------------------
  210. // If our weapon is NONE, we want to be able to buy up to this many clips for the current gun we're holding
  211. enum { NUM_CLIPS_FOR_CURRENT = 4 };
  212. #if USE_BUY_PRESETS
  213. extern ConVar cl_buy_favorite_quiet;
  214. extern ConVar cl_buy_favorite_nowarn;
  215. #endif // USE_BUY_PRESETS
  216. #endif // BUY_PRESETS_H