2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2013-05-24 03:30:39 -04:00
|
|
|
#include "ItemGrid.h"
|
2012-08-11 07:51:32 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
namespace Json
|
|
|
|
{
|
|
|
|
class Value;
|
|
|
|
};
|
|
|
|
|
|
|
|
class cClientHandle;
|
|
|
|
class cPlayer;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-05-24 03:30:39 -04:00
|
|
|
// tolua_begin
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2013-05-24 03:30:39 -04:00
|
|
|
/** This class represents the player's inventory
|
|
|
|
The slots are divided into three areas:
|
|
|
|
- armor slots (1 x 4)
|
|
|
|
- inventory slots (9 x 3)
|
|
|
|
- hotbar slots (9 x 1)
|
|
|
|
The generic GetSlot(), SetSlot() and HowManyCanFit() functions take the index of the slots,
|
|
|
|
as if armor slots, inventory slots and then hotbar slots were put one after another.
|
|
|
|
You can use the invArmorOffset, invInventoryOffset and invHotbarOffset constants.
|
|
|
|
*/
|
|
|
|
|
|
|
|
class cInventory :
|
|
|
|
public cItemGrid::cListener
|
|
|
|
{
|
2012-06-14 09:06:06 -04:00
|
|
|
public:
|
2013-05-24 03:30:39 -04:00
|
|
|
|
|
|
|
// Counts and offsets to individual parts of the inventory, as used by GetSlot() / SetSlot() / HowManyCanFit():
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
invArmorCount = 4,
|
2014-08-03 01:56:08 -04:00
|
|
|
invInventoryCount = 9 * 3,
|
|
|
|
invHotbarCount = 9,
|
2013-05-24 03:30:39 -04:00
|
|
|
|
|
|
|
invArmorOffset = 0,
|
|
|
|
invInventoryOffset = invArmorOffset + invArmorCount,
|
|
|
|
invHotbarOffset = invInventoryOffset + invInventoryCount,
|
|
|
|
invNumSlots = invHotbarOffset + invHotbarCount
|
|
|
|
} ;
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2013-05-24 03:30:39 -04:00
|
|
|
// tolua_end
|
|
|
|
|
|
|
|
cInventory(cPlayer & a_Owner);
|
|
|
|
|
2014-03-28 16:35:45 -04:00
|
|
|
virtual ~cInventory() {}
|
|
|
|
|
2013-05-24 03:30:39 -04:00
|
|
|
// tolua_begin
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2014-05-09 17:10:02 -04:00
|
|
|
/** Removes all items from the entire inventory */
|
2013-05-24 03:30:39 -04:00
|
|
|
void Clear(void);
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2014-05-09 17:10:02 -04:00
|
|
|
/** Returns number of items out of a_ItemStack that can fit in the storage */
|
2013-05-24 03:30:39 -04:00
|
|
|
int HowManyCanFit(const cItem & a_ItemStack, bool a_ConsiderEmptySlots);
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2014-05-09 17:10:02 -04:00
|
|
|
/** Returns how many items of the specified type would fit into the slot range specified */
|
2013-05-24 03:30:39 -04:00
|
|
|
int HowManyCanFit(const cItem & a_ItemStack, int a_BeginSlotNum, int a_EndSlotNum, bool a_ConsiderEmptySlots);
|
|
|
|
|
|
|
|
/** Adds as many items out of a_ItemStack as can fit.
|
|
|
|
If a_AllowNewStacks is set to false, only existing stacks can be topped up;
|
|
|
|
if a_AllowNewStacks is set to true, empty slots can be used for the rest.
|
2013-07-03 09:56:11 -04:00
|
|
|
If a_tryToFillEquippedFirst is set to true, the currently equipped slot will be used first (if empty or
|
|
|
|
compatible with added items)
|
|
|
|
if a_tryToFillEquippedFirst is set to false, the regular order applies.
|
2013-05-24 03:30:39 -04:00
|
|
|
Returns the number of items that fit.
|
|
|
|
*/
|
2013-07-03 09:56:11 -04:00
|
|
|
int AddItem(const cItem & a_ItemStack, bool a_AllowNewStacks = true, bool a_tryToFillEquippedFirst = false);
|
2013-05-24 03:30:39 -04:00
|
|
|
|
|
|
|
/** Same as AddItem, but works on an entire list of item stacks.
|
|
|
|
The a_ItemStackList is modified to reflect the leftover items.
|
|
|
|
If a_AllowNewStacks is set to false, only existing stacks can be topped up;
|
2013-07-03 09:56:11 -04:00
|
|
|
if a_AllowNewStacks is set to true, empty slots can be used for the rest.
|
|
|
|
If a_tryToFillEquippedFirst is set to true, the currently equipped slot will be used first (if empty or
|
|
|
|
compatible with added items)
|
|
|
|
if a_tryToFillEquippedFirst is set to false, the regular order applies.
|
2013-05-24 03:30:39 -04:00
|
|
|
Returns the total number of items that fit.
|
|
|
|
*/
|
2013-07-03 09:56:11 -04:00
|
|
|
int AddItems(cItems & a_ItemStackList, bool a_AllowNewStacks, bool a_tryToFillEquippedFirst);
|
2013-05-24 03:30:39 -04:00
|
|
|
|
2014-07-18 17:47:46 -04:00
|
|
|
/** Removes the specified item from the inventory, as many as possible, up to a_ItemStack.m_ItemCount.
|
|
|
|
Returns the number of items that were removed. */
|
2014-07-18 17:11:59 -04:00
|
|
|
int RemoveItem(const cItem & a_ItemStack);
|
|
|
|
|
2014-05-09 17:10:02 -04:00
|
|
|
/** Removes one item out of the currently equipped item stack, returns true if successful, false if empty-handed */
|
2013-05-24 03:30:39 -04:00
|
|
|
bool RemoveOneEquippedItem(void);
|
|
|
|
|
2014-05-09 17:10:02 -04:00
|
|
|
/** Returns the number of items of type a_Item that are stored */
|
2013-05-24 03:30:39 -04:00
|
|
|
int HowManyItems(const cItem & a_Item);
|
|
|
|
|
2014-05-09 17:10:02 -04:00
|
|
|
/** Returns true if there are at least as many items of type a_ItemStack as in a_ItemStack */
|
2013-05-24 03:30:39 -04:00
|
|
|
bool HasItems(const cItem & a_ItemStack);
|
2014-05-09 17:10:02 -04:00
|
|
|
|
|
|
|
/** Sends the equipped item slot to the client */
|
|
|
|
void SendEquippedSlot();
|
2013-05-24 03:30:39 -04:00
|
|
|
|
2014-05-09 17:10:02 -04:00
|
|
|
/** Returns the cItemGrid object representing the armor slots */
|
2013-05-24 03:30:39 -04:00
|
|
|
cItemGrid & GetArmorGrid(void) { return m_ArmorSlots; }
|
|
|
|
|
2014-05-09 17:10:02 -04:00
|
|
|
/** Returns the cItemGrid object representing the main inventory slots */
|
2013-05-24 03:30:39 -04:00
|
|
|
cItemGrid & GetInventoryGrid(void) { return m_InventorySlots; }
|
|
|
|
|
2014-05-09 17:10:02 -04:00
|
|
|
/** Returns the cItemGrid object representing the hotbar slots */
|
2013-05-24 03:30:39 -04:00
|
|
|
cItemGrid & GetHotbarGrid(void) { return m_HotbarSlots; }
|
|
|
|
|
2014-05-09 17:10:02 -04:00
|
|
|
/** Returns the player associated with this inventory */
|
2013-05-24 03:30:39 -04:00
|
|
|
cPlayer & GetOwner(void) { return m_Owner; }
|
|
|
|
|
2014-05-09 17:10:02 -04:00
|
|
|
/** Copies the non-empty slots into a_ItemStacks; preserves the original a_Items contents */
|
2013-05-24 03:30:39 -04:00
|
|
|
void CopyToItems(cItems & a_Items);
|
|
|
|
|
|
|
|
// tolua_end
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2014-05-09 17:10:02 -04:00
|
|
|
/** Returns the player associated with this inventory (const version) */
|
2013-05-24 03:30:39 -04:00
|
|
|
const cPlayer & GetOwner(void) const { return m_Owner; }
|
|
|
|
|
2013-05-19 14:22:37 -04:00
|
|
|
// tolua_begin
|
|
|
|
|
|
|
|
const cItem & GetSlot(int a_SlotNum) const;
|
2013-05-24 03:30:39 -04:00
|
|
|
const cItem & GetArmorSlot(int a_ArmorSlotNum) const;
|
|
|
|
const cItem & GetInventorySlot(int a_InventorySlotNum) const;
|
|
|
|
const cItem & GetHotbarSlot(int a_HotBarSlotNum) const;
|
2012-08-24 03:58:26 -04:00
|
|
|
const cItem & GetEquippedItem(void) const;
|
2013-05-19 14:22:37 -04:00
|
|
|
void SetSlot(int a_SlotNum, const cItem & a_Item);
|
2013-05-24 03:30:39 -04:00
|
|
|
void SetArmorSlot(int a_ArmorSlotNum, const cItem & a_Item);
|
|
|
|
void SetInventorySlot(int a_InventorySlotNum, const cItem & a_Item);
|
|
|
|
void SetHotbarSlot(int a_HotBarSlotNum, const cItem & a_Item);
|
2013-05-19 14:22:37 -04:00
|
|
|
|
|
|
|
void SetEquippedSlotNum(int a_SlotNum);
|
|
|
|
int GetEquippedSlotNum(void) { return m_EquippedSlotNum; }
|
|
|
|
|
2013-07-26 15:50:06 -04:00
|
|
|
/** Adds (or subtracts, if a_AddToCount is negative) to the count of items in the specified slot.
|
|
|
|
If the slot is empty, ignores the call.
|
|
|
|
Returns the new count, or -1 if the slot number is invalid.
|
|
|
|
*/
|
|
|
|
int ChangeSlotCount(int a_SlotNum, int a_AddToCount);
|
|
|
|
|
2014-05-09 17:10:02 -04:00
|
|
|
/** Adds the specified damage to the specified item; deletes the item and returns true if the item broke. */
|
2013-05-19 14:42:42 -04:00
|
|
|
bool DamageItem(int a_SlotNum, short a_Amount);
|
2013-05-19 14:22:37 -04:00
|
|
|
|
2014-05-09 17:10:02 -04:00
|
|
|
/** Adds the specified damage to the currently held item; deletes the item and returns true if the item broke. */
|
2013-05-19 14:22:37 -04:00
|
|
|
bool DamageEquippedItem(short a_Amount = 1);
|
2012-12-21 06:02:31 -05:00
|
|
|
|
2013-05-24 03:30:39 -04:00
|
|
|
const cItem & GetEquippedHelmet (void) const { return m_ArmorSlots.GetSlot(0); }
|
|
|
|
const cItem & GetEquippedChestplate(void) const { return m_ArmorSlots.GetSlot(1); }
|
|
|
|
const cItem & GetEquippedLeggings (void) const { return m_ArmorSlots.GetSlot(2); }
|
|
|
|
const cItem & GetEquippedBoots (void) const { return m_ArmorSlots.GetSlot(3); }
|
2013-05-19 14:22:37 -04:00
|
|
|
|
2013-05-24 05:16:09 -04:00
|
|
|
// tolua_end
|
|
|
|
|
2014-05-09 17:10:02 -04:00
|
|
|
/** Sends the slot contents to the owner */
|
2013-05-24 03:30:39 -04:00
|
|
|
void SendSlot(int a_SlotNum);
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2014-05-09 17:10:02 -04:00
|
|
|
/** Update items (e.g. Maps) */
|
2014-02-17 09:27:12 -05:00
|
|
|
void UpdateItems(void);
|
|
|
|
|
2014-05-09 17:10:02 -04:00
|
|
|
/** Converts an armor slot number into the ID for the EntityEquipment packet */
|
2013-05-24 03:30:39 -04:00
|
|
|
static int ArmorSlotNumToEntityEquipmentID(short a_ArmorSlotNum);
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2013-05-24 03:30:39 -04:00
|
|
|
void SaveToJson(Json::Value & a_Value);
|
|
|
|
bool LoadFromJson(Json::Value & a_Value);
|
2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
protected:
|
2014-07-21 09:19:48 -04:00
|
|
|
bool AddToBar( cItem & a_Item, const int a_Offset, const int a_Size, bool* a_bChangedSlots, int a_Mode = 0);
|
2012-09-20 09:25:54 -04:00
|
|
|
|
2013-05-24 03:30:39 -04:00
|
|
|
cItemGrid m_ArmorSlots;
|
|
|
|
cItemGrid m_InventorySlots;
|
|
|
|
cItemGrid m_HotbarSlots;
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2013-05-19 14:22:37 -04:00
|
|
|
int m_EquippedSlotNum;
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2012-09-20 09:25:54 -04:00
|
|
|
cPlayer & m_Owner;
|
2013-05-24 03:30:39 -04:00
|
|
|
|
2014-10-20 16:55:07 -04:00
|
|
|
/** Returns the ItemGrid and the (grid-local) slot number for a (global) slot number; return nullptr for invalid SlotNum */
|
2013-05-24 03:30:39 -04:00
|
|
|
const cItemGrid * GetGridForSlotNum(int a_SlotNum, int & a_GridSlotNum) const;
|
|
|
|
|
2014-10-20 16:55:07 -04:00
|
|
|
/** Returns the ItemGrid and the (grid-local) slot number for a (global) slot number; return nullptr for invalid SlotNum */
|
2013-05-24 03:30:39 -04:00
|
|
|
cItemGrid * GetGridForSlotNum(int a_SlotNum, int & a_GridSlotNum);
|
|
|
|
|
|
|
|
// cItemGrid::cListener override:
|
|
|
|
virtual void OnSlotChanged(cItemGrid * a_ItemGrid, int a_SlotNum) override;
|
2014-07-17 10:33:09 -04:00
|
|
|
}; // tolua_export
|
2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|