12 #include "json/json.h"
22 m_InventorySlots(9, 3),
64 UNUSED(a_ConsiderEmptySlots);
65 if ((a_BeginSlotNum < 0) || (a_BeginSlotNum >=
invNumSlots))
67 LOGWARNING(
"%s: Bad BeginSlotNum, got %d, there are %d slots; correcting to 0.", __FUNCTION__, a_BeginSlotNum,
invNumSlots - 1);
70 if ((a_EndSlotNum < 0) || (a_EndSlotNum >=
invNumSlots))
75 if (a_BeginSlotNum > a_EndSlotNum)
77 std::swap(a_BeginSlotNum, a_EndSlotNum);
82 for (
int i = a_BeginSlotNum; i <= a_EndSlotNum; i++)
89 else if (Slot.
IsEqual(a_ItemStack))
143 if (!a_AllowNewStacks)
168 for (cItems::iterator itr = a_ItemStackList.begin(); itr != a_ItemStackList.end();)
170 char NumAdded =
AddItem(*itr, a_AllowNewStacks);
171 if (itr->m_ItemCount == NumAdded)
173 itr = a_ItemStackList.erase(itr);
177 itr->m_ItemCount -= NumAdded;
180 TotalAdded += NumAdded;
200 cItem Temp(a_ItemStack);
253 if (EquippedItem.IsEmpty())
260 cItem ItemsToAdd = a_Item;
261 if (EquippedItem.IsEqual(ItemsToAdd))
263 auto AmountToAdd = std::min(
static_cast<char>(ItemsToAdd.
GetMaxStackSize() - EquippedItem.m_ItemCount), ItemsToAdd.
m_ItemCount);
265 EquippedItem.m_ItemCount += AmountToAdd;
277 if (!a_TryOtherSlots)
283 return AddItem(ItemsToAdd) + ItemsAdded;
317 LOGWARNING(
"%s: requesting an invalid slot index: %d out of %d. Ignoring.", __FUNCTION__, a_SlotNum,
invNumSlots - 1);
325 LOGWARNING(
"%s(%d): requesting an invalid itemgrid. Ignoring.", __FUNCTION__, a_SlotNum);
328 Grid->
SetSlot(GridSlotNum, a_Item);
394 LOGWARNING(
"%s: requesting an invalid slot index: %d out of %d. Returning the first inventory slot instead.", __FUNCTION__, a_SlotNum,
invNumSlots - 1);
402 LOGWARNING(
"%s(%d): requesting an invalid ItemGrid, returning the first inventory slot instead.", __FUNCTION__, a_SlotNum);
405 return Grid->
GetSlot(GridSlotNum);
414 if ((a_ArmorSlotNum < 0) || (a_ArmorSlotNum >=
invArmorCount))
416 LOGWARNING(
"%s: requesting an invalid slot index: %d out of %d. Returning the first one instead", __FUNCTION__, a_ArmorSlotNum,
invArmorCount - 1);
430 LOGWARNING(
"%s: requesting an invalid slot index: %d out of %d. Returning the first one instead", __FUNCTION__, a_InventorySlotNum,
invInventoryCount - 1);
444 LOGWARNING(
"%s: requesting an invalid slot index: %d out of %d. Returning the first one instead", __FUNCTION__, a_SlotNum,
invHotbarCount - 1);
476 LOGWARNING(
"%s: requesting invalid slot index: %d out of %d. Setting 0 instead.", __FUNCTION__, a_SlotNum,
invHotbarCount - 1);
504 LOGWARNING(
"%s: invalid slot number, expected 0 .. %d, got %d; ignoring", __FUNCTION__,
invNumSlots, a_SlotNum);
518 LOGWARNING(
"%s: requesting an invalid slot index: %d out of %d", __FUNCTION__, a_SlotNum,
invNumSlots - 1);
530 LOGWARNING(
"%s(%d, %d): requesting an invalid grid, ignoring.", __FUNCTION__, a_SlotNum, a_Amount);
613 switch (a_ArmorSlotNum)
620 LOGWARN(
"%s: invalid armor slot number: %d", __FUNCTION__, a_ArmorSlotNum);
634 int MaxStackSize = cItemHandler::GetItemHandler(a_Item.
m_ItemType)->GetMaxStackSize();
635 for (
int i = 0; i < a_Size; i++)
638 (m_Slots[i + a_Offset].m_ItemType == a_Item.
m_ItemType) &&
639 (m_Slots[i + a_Offset].m_ItemCount < MaxStackSize) &&
640 (m_Slots[i + a_Offset].m_ItemDamage == a_Item.
m_ItemDamage)
643 int NumFree = MaxStackSize - m_Slots[i + a_Offset].m_ItemCount;
648 m_Slots[i + a_Offset].m_ItemCount += a_Item.
m_ItemCount;
650 a_bChangedSlots[i + a_Offset] =
true;
656 m_Slots[i + a_Offset].m_ItemCount += (char)NumFree;
658 a_bChangedSlots[i + a_Offset] =
true;
667 for (
int i = 0; (i < a_Size) && (a_Item.
m_ItemCount > 0); i++)
669 if (m_Slots[i + a_Offset].m_ItemType == -1)
671 m_Slots[i + a_Offset] = a_Item;
673 a_bChangedSlots[i + a_Offset] =
true;
703 Json::Value EmptyItemJson;
704 EmptyItem.
GetJson(EmptyItemJson);
705 for (
int i = 0; i < 5; i++)
707 a_Value.append(EmptyItemJson);
713 Json::Value JSON_Item;
715 a_Value.append(JSON_Item);
721 Json::Value JSON_Item;
723 a_Value.append(JSON_Item);
729 Json::Value JSON_Item;
731 a_Value.append(JSON_Item);
735 Json::Value JSON_Item;
737 a_Value.append(JSON_Item);
748 for (Json::Value::iterator itr = a_Value.begin(); itr != a_Value.end(); ++itr, SlotIdx++)
783 a_GridSlotNum = a_SlotNum;
789 a_GridSlotNum = a_SlotNum;
795 a_GridSlotNum = a_SlotNum;
812 a_GridSlotNum = a_SlotNum;
818 a_GridSlotNum = a_SlotNum;
824 a_GridSlotNum = a_SlotNum;
849 World->BroadcastEntityEquipment(
881 ASSERT(!
"Unknown ItemGrid calling OnSlotChanged()");
void LOGWARNING(std::string_view a_Format, const Args &... args)
bool IsArmor(short a_ItemType)
Utilities to allow casting a cWorld to one of its interfaces without including World....
void SendInventorySlot(char a_WindowID, short a_SlotNum, const cItem &a_Item)
bool IsTicking(void) const
Returns true if the entity is valid and ticking.
cWorld * GetWorld(void) const
cClientHandle * GetClientHandle(void) const
void AddKnownItem(const cItem &a_Item)
Adds an Item to the list of known items.
char AddItem(const cItem &a_ItemStack, bool a_AllowNewStacks=true)
Adds as many items out of a_ItemStack as can fit.
const cItem & GetHotbarSlot(int a_HotBarSlotNum) const
Returns current item in a_ArmorSlotNum in hotbar slots.
bool LoadFromJson(Json::Value &a_Value)
void UpdateItems(void)
Update items (e.g.
virtual void OnSlotChanged(cItemGrid *a_ItemGrid, int a_SlotNum) override
Called whenever a slot changes.
cItemGrid m_InventorySlots
cInventory(cPlayer &a_Owner)
void CopyToItems(cItems &a_Items)
Copies the non-empty slots into a_ItemStacks; preserves the original a_Items contents.
static int ArmorSlotNumToEntityEquipmentID(short a_ArmorSlotNum)
Converts an armor slot number into the ID for the EntityEquipment packet.
bool HasItems(const cItem &a_ItemStack)
Returns true if there are at least as many items of type a_ItemStack as in a_ItemStack.
bool DamageEquippedItem(short a_Amount=1)
Adds the specified damage to the currently held item; deletes the item and returns true if the item b...
const cItem & GetArmorSlot(int a_ArmorSlotNum) const
Returns current item in a_ArmorSlotNum in armor slots.
char AddItems(cItems &a_ItemStackList, bool a_AllowNewStacks)
Same as AddItem, but works on an entire list of item stacks.
cItem * FindItem(const cItem &a_RecipeItem)
Finds an item based on ItemType and ItemDamage (<- defines the itemType, too)
void SetEquippedSlotNum(int a_SlotNum)
Sets equiped item to the a_SlotNum slot number.
void SaveToJson(Json::Value &a_Value)
void SendSlot(int a_SlotNum)
Sends the slot contents to the owner.
void SetSlot(int a_SlotNum, const cItem &a_Item)
Puts a_Item item in a_SlotNum slot number.
const cItem & GetSlot(int a_SlotNum) const
Returns current item in a_SlotNum slot.
void SendEquippedSlot()
Sends the equipped item slot to the client.
char ChangeSlotCount(int a_SlotNum, char a_AddToCount)
Adds (or subtracts, if a_AddToCount is negative) to the count of items in the specified slot.
bool AddToBar(cItem &a_Item, const int a_Offset, const int a_Size, bool *a_bChangedSlots, int a_Mode=0)
void SetShieldSlot(const cItem &a_Item)
Sets current item in shield slot.
int HowManyItems(const cItem &a_Item)
Returns the number of items of type a_Item that are stored.
void SetInventorySlot(int a_InventorySlotNum, const cItem &a_Item)
Puts a_Item item in a_InventorySlotNum slot number in inventory slots.
void Clear(void)
Removes all items from the entire inventory.
int ReplaceOneEquippedItem(const cItem &a_Item, bool a_TryOtherSlots=true)
Removes one item from the the current equipped item stack, and attempts to add the specified item sta...
bool DamageItem(int a_SlotNum, short a_Amount)
Adds the specified damage to the specified item; deletes the item and returns true if the item broke.
const cItem & GetInventorySlot(int a_InventorySlotNum) const
Returns current item in a_ArmorSlotNum in inventory slots.
void SetArmorSlot(int a_ArmorSlotNum, const cItem &a_Item)
Puts a_Item item in a_ArmorSlotNum slot number in armor slots.
int GetEquippedSlotNum(void)
Returns slot number of equiped item.
void SetHotbarSlot(int a_HotBarSlotNum, const cItem &a_Item)
Puts a_Item item in a_HotBarSlotNum slot number in hotbar slots.
const cItemGrid * GetGridForSlotNum(int a_SlotNum, int &a_GridSlotNum) const
Returns the ItemGrid and the (grid-local) slot number for a (global) slot number; return nullptr for ...
bool RemoveOneEquippedItem(void)
Removes one item out of the currently equipped item stack, returns true if successful,...
const cItem & GetEquippedItem(void) const
Returns current equiped item.
int HowManyCanFit(const cItem &a_ItemStack, bool a_ConsiderEmptySlots=true)
Returns number of items out of a_ItemStack that can fit in the storage.
int RemoveItem(const cItem &a_ItemStack)
Removes the specified item from the inventory, as many as possible, up to a_ItemStack....
const cItem & GetShieldSlot() const
Returns current item in shield slot.
void SetEquippedItem(const cItem &a_Item)
Sets current item in the equipped hotbar slot.
const cItemHandler & GetHandler(void) const
Returns the cItemHandler responsible for this item type.
void GetJson(Json::Value &a_OutValue) const
Saves the item data into JSON representation.
char GetMaxStackSize(void) const
Returns the maximum amount of stacked items of this type.
bool IsEmpty(void) const
Returns true if the item represents an empty stack - either the type is invalid, or count is zero.
bool IsEqual(const cItem &a_Item) const
This class bridges a vector of cItem for safe access via Lua.
char ChangeSlotCount(int a_SlotNum, char a_AddToCount)
Adds (or subtracts, if a_AddToCount is negative) to the count of items in the specified slot.
void SetSlot(int a_X, int a_Y, const cItem &a_Item)
cItem * FindItem(const cItem &a_RecipeItem)
Finds an item based on ItemType and ItemDamage (<- defines the itemType, too)
const cItem & GetSlot(int a_X, int a_Y) const
void Clear(void)
Sets all items as empty.
void EmptySlot(int a_X, int a_Y)
int GetNumSlots(void) const
void AddListener(cListener &a_Listener)
Adds a callback that gets called whenever a slot changes.
int HowManyItems(const cItem &a_Item)
Returns the number of items of type a_Item that are stored.
char RemoveItem(const cItem &a_ItemStack)
Removes the specified item from the grid, as many as possible, up to a_ItemStack.m_ItemCount.
bool DamageItem(int a_SlotNum, short a_Amount)
Adds the specified damage to the specified item; returns true if the item broke (but the item is left...
char AddItem(cItem &a_ItemStack, bool a_AllowNewStacks=true, int a_PrioritySlot=-1)
Adds as many items out of a_ItemStack as can fit.
void CopyToItems(cItems &a_Items) const
Copies the contents into a cItems object; preserves the original a_Items contents.
virtual void OnUpdate(cWorld *a_World, cPlayer *a_Player, const cItem &a_Item) const
Called every tick while the item is on the player's inventory (used by maps, for example) - For now,...
static bool CanPlaceArmorInSlot(int a_SlotNum, const cItem &a_Item)
virtual void BroadcastEntityEquipment(const cEntity &a_Entity, short a_SlotNum, const cItem &a_Item, const cClientHandle *a_Exclude=nullptr) override