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.

248 lines
9.1 KiB

  1. //====== Copyright � 1996-2005, Valve Corporation, All rights reserved. =======
  2. //
  3. // Purpose: Container that allows client & server access to data in player inventories & loadouts
  4. //
  5. //=============================================================================
  6. #ifndef CS_ITEM_INVENTORY_H
  7. #define CS_ITEM_INVENTORY_H
  8. #ifdef _WIN32
  9. #pragma once
  10. #endif
  11. #include "item_inventory.h"
  12. #include "cs_shareddefs.h"
  13. #define LOADOUT_SLOT_USE_BASE_ITEM 0
  14. //===============================================================================================================
  15. // POSITION HANDLING
  16. //===============================================================================================================
  17. // Items at this position have been found, but not presented to the player
  18. #define BACKEND_POSITION_FOR_NEW_ITEM 0
  19. // We store a bag index in the highbits of the inventory position.
  20. // The lowbit stores the position of the item within the bag.
  21. enum inventory_bags_t
  22. {
  23. BAG_ALL_ITEMS = 0,
  24. BAG_BACKPACK = 1,
  25. BAG_TEAM_BASE = 1000,
  26. };
  27. #define MAX_BACKPACK_SLOTS 50
  28. inline uint32 GetBackendPositionFor( int iBag, int iSlot )
  29. {
  30. uint32 iPos = iSlot | (iBag << 16);
  31. return iPos;
  32. }
  33. inline int ExtractBagFromBackendPosition( uint32 iBackendPosition )
  34. {
  35. int iHighbits = iBackendPosition >> 16;
  36. return iHighbits;
  37. }
  38. inline int ExtractBagSlotFromBackendPosition( uint32 iBackendPosition )
  39. {
  40. int iLowbits = iBackendPosition & 0xFFFF;
  41. return iLowbits;
  42. }
  43. /*
  44. inline int GetClassFromLoadoutBag( int iBag )
  45. {
  46. return (iBag - BAG_CLASS_LOADOUT_SCOUT + 1);
  47. }
  48. inline inventory_bags_t GetLoadoutBagForClass( int iClass )
  49. {
  50. return (inventory_bags_t)(BAG_CLASS_LOADOUT_SCOUT + iClass - 1);
  51. }
  52. */
  53. //===============================================================================================================
  54. //-----------------------------------------------------------------------------
  55. // Purpose: A single CS player's inventory.
  56. // On the client, the inventory manager contains contains an instance of this for the local player.
  57. // On the server, each player contains an instance of this.
  58. //-----------------------------------------------------------------------------
  59. class CCSPlayerInventory : public CPlayerInventory
  60. {
  61. DECLARE_CLASS( CCSPlayerInventory, CPlayerInventory );
  62. public:
  63. CCSPlayerInventory();
  64. void SetBag( int iBag ) { m_iBag = iBag; }
  65. int GetBag( void ) { return m_iBag; }
  66. // Returns true if the specified item is allowed to be stored at the specified position
  67. // DOES NOT check to make sure the position is clear. It's just a class / slot validity check.
  68. virtual bool IsItemAllowedInPosition( CScriptCreatedItem *pItem, int iPosition ) { return true; }
  69. // Used to reject items on the backend for inclusion into this inventory.
  70. // Mostly used for division of bags into different in-game inventories.
  71. virtual bool ItemShouldBeIncluded( int iItemPosition );
  72. protected:
  73. // Item movement functions that are called by the inventory manager
  74. #ifdef CLIENT_DLL
  75. // Updates the backpack or loadout for the specified item
  76. // Returns false if the item isn't allowed to be placed in the specified position.
  77. virtual bool MoveItemToPosition( globalindex_t iGlobalIndex, int iPosition );
  78. // Tries to move the specified item into the player's backpack. If this inventory isn't the
  79. // backpack, it'll find the matching player's backpack and move it.
  80. // FAILS if the backpack is full. Returns false in that case.
  81. virtual bool MoveItemToBackpack( globalindex_t iGlobalIndex );
  82. #endif
  83. // Inventory updating, called by the Inventory Manager only. If you want an inventory updated,
  84. // use the SteamRequestX functions in CInventoryManager.
  85. virtual void InventoryReceived( EItemRequestResult eResult, int iItems );
  86. // Debugging
  87. virtual void DumpInventoryToConsole( bool bRoot );
  88. protected:
  89. // The bag position of this inventory, if any (0 == contains all items)
  90. // If this inventory has a bag position, items inside other bags will be ignored.
  91. int m_iBag;
  92. friend class CCSInventoryManager;
  93. // friend class CTFPlayerInventoryLoadout;
  94. };
  95. /*
  96. //-----------------------------------------------------------------------------
  97. // Purpose: Custom TF inventory that manages the player's loadout
  98. //-----------------------------------------------------------------------------
  99. class CTFPlayerInventoryLoadout : public CCSPlayerInventory
  100. {
  101. DECLARE_CLASS( CTFPlayerInventoryLoadout, CCSPlayerInventory );
  102. public:
  103. CTFPlayerInventoryLoadout( void );
  104. // Returns the item in the specified loadout slot for a given class
  105. CScriptCreatedItem *GetItemInLoadout( int iSlot );
  106. // Get the class that this loadout inventory is for
  107. int GetLoadoutClass( void ) { return GetClassFromLoadoutBag(m_iBag); }
  108. // Returns true if the specified item is allowed to be stored at the specified position
  109. // DOES NOT check to make sure the position is clear. It's just a class / slot validity check.
  110. bool IsItemAllowedInPosition( CScriptCreatedItem *pItem, int iPosition );
  111. // Debugging
  112. virtual void DumpInventoryToConsole( bool bRoot );
  113. protected:
  114. // Item movement functions that are called by the inventory manager
  115. #ifdef CLIENT_DLL
  116. // Updates the backpack or loadout for the specified item
  117. // Returns false if the item isn't allowed to be placed in the specified position.
  118. virtual bool MoveItemToPosition( globalindex_t iGlobalIndex, int iPosition );
  119. // Removes any item in a loadout slot. If the slot has a base item,
  120. // the player essentially returns to using that item.
  121. // NOTE: This can fail if the player has no backpack space to contain the equipped item.
  122. bool ClearLoadoutSlot( int iSlot );
  123. #endif
  124. protected:
  125. // Inventory updating, called by the Inventory Manager only. If you want an inventory updated,
  126. // use the SteamRequestX functions in CInventoryManager.
  127. virtual void InventoryReceived( EItemRequestResult eResult, int iItems );
  128. // Derived inventory hooks
  129. virtual void ItemHasBeenUpdated( CScriptCreatedItem *pItem );
  130. virtual void ItemIsBeingRemoved( CScriptCreatedItem *pItem );
  131. private:
  132. // Global indices of the items in our inventory in the loadout slots
  133. globalindex_t m_LoadoutItems[ LOADOUT_POSITION_COUNT ];
  134. friend class CCSInventoryManager;
  135. };
  136. //-----------------------------------------------------------------------------
  137. // Purpose: Custom TF inventory that manages the player's loadout
  138. //-----------------------------------------------------------------------------
  139. class CTFPlayerInventoryBackpack : public CCSPlayerInventory
  140. {
  141. DECLARE_CLASS( CTFPlayerInventoryBackpack, CCSPlayerInventory );
  142. public:
  143. CTFPlayerInventoryBackpack( void );
  144. #ifdef CLIENT_DLL
  145. // Tries to move the specified item into the player's backpack.
  146. // FAILS if the backpack is full. Returns false in that case.
  147. virtual bool MoveItemToBackpack( globalindex_t iGlobalIndex );
  148. protected:
  149. virtual void ItemHasBeenUpdated( CScriptCreatedItem *pItem );
  150. void CalculateNextEmptySlot( void );
  151. #endif
  152. private:
  153. int m_iPredictedEmptySlot;
  154. };
  155. */
  156. //-----------------------------------------------------------------------------
  157. // Purpose:
  158. //-----------------------------------------------------------------------------
  159. class CCSInventoryManager : public CInventoryManager
  160. {
  161. DECLARE_CLASS( CCSInventoryManager, CInventoryManager );
  162. public:
  163. CCSInventoryManager();
  164. virtual void PostInit( void );
  165. #ifdef CLIENT_DLL
  166. // Show the player a pickup screen with any items they've collected recently, if any
  167. bool ShowItemsPickedUp( void );
  168. // Force the player to discard an item to make room for a new item, if they have one
  169. bool CheckForRoomAndForceDiscard( void );
  170. #endif
  171. // Returns the item data for the base item in the loadout slot for a given class
  172. CScriptCreatedItem *GetBaseItemForClass( int iSlot );
  173. void GenerateBaseItems( void );
  174. // Gets the specified inventory for the steam ID
  175. CCSPlayerInventory *GetBagForPlayer( CSteamID &playerID, inventory_bags_t iBag );
  176. // Returns the item in the specified loadout slot for a given class
  177. CScriptCreatedItem *GetItemInLoadoutForClass( int iClass, int iSlot, CSteamID *pID = NULL );
  178. private:
  179. // Base items, returned for slots that the player doesn't have anything in
  180. CScriptCreatedItem m_pBaseLoadoutItems[ LOADOUT_POSITION_COUNT ];
  181. #ifdef CLIENT_DLL
  182. // On the client, we have a single inventory for the local player. Stored here, instead of in the
  183. // local player entity, because players need to access it while not being connected to a server.
  184. public:
  185. virtual void UpdateLocalInventory( void );
  186. CPlayerInventory *GetLocalInventory( void ) { return &m_LocalInventory; }
  187. CCSPlayerInventory *GetLocalTFInventory( void );
  188. // Try and equip the specified item in the specified class's loadout slot
  189. bool EquipItemInLoadout( int iClass, int iSlot, globalindex_t iGlobalIndex );
  190. // Fills out pList with all inventory items that could fit into the specified loadout slot for a given class
  191. int GetAllUsableItemsForSlot( int iClass, int iSlot, CUtlVector<CScriptCreatedItem*> *pList );
  192. private:
  193. CCSPlayerInventory m_LocalInventory;
  194. /*
  195. CTFPlayerInventoryBackpack m_LocalBackpack;
  196. CTFPlayerInventoryLoadout m_LoadoutInventories[TF_CLASS_COUNT];
  197. */
  198. #endif // CLIENT_DLL
  199. };
  200. CCSInventoryManager *CSInventoryManager( void );
  201. #endif // CS_ITEM_INVENTORY_H