diff options
Diffstat (limited to 'source')
-rw-r--r-- | source/Bindings.cpp | 4 | ||||
-rw-r--r-- | source/Inventory.cpp | 8 | ||||
-rw-r--r-- | source/Inventory.h | 12 | ||||
-rw-r--r-- | source/ItemGrid.cpp | 52 | ||||
-rw-r--r-- | source/ItemGrid.h | 21 | ||||
-rw-r--r-- | source/Items/ItemBucket.h | 6 |
6 files changed, 74 insertions, 29 deletions
diff --git a/source/Bindings.cpp b/source/Bindings.cpp index 63d414194..ed48a8563 100644 --- a/source/Bindings.cpp +++ b/source/Bindings.cpp @@ -12978,7 +12978,7 @@ static int tolua_AllToLua_cInventory_AddItems00(lua_State* tolua_S) if (!self) tolua_error(tolua_S,"invalid 'self' in function 'AddItems'", NULL); #endif { - int tolua_ret = (int) self->AddItems(*a_ItemStackList,a_AllowNewStacks); + int tolua_ret = (int) self->AddItems(*a_ItemStackList,a_AllowNewStacks,false); tolua_pushnumber(tolua_S,(lua_Number)tolua_ret); } } @@ -15821,7 +15821,7 @@ static int tolua_AllToLua_cItemGrid_AddItem00(lua_State* tolua_S) if (!self) tolua_error(tolua_S,"invalid 'self' in function 'AddItem'", NULL); #endif { - int tolua_ret = (int) self->AddItem(*a_ItemStack,a_AllowNewStacks); + int tolua_ret = (int) self->AddItem(*a_ItemStack,a_AllowNewStacks, false); tolua_pushnumber(tolua_S,(lua_Number)tolua_ret); } } diff --git a/source/Inventory.cpp b/source/Inventory.cpp index fc52d4621..b56f50ed7 100644 --- a/source/Inventory.cpp +++ b/source/Inventory.cpp @@ -98,7 +98,7 @@ int cInventory::HowManyCanFit(const cItem & a_ItemStack, int a_BeginSlotNum, int -int cInventory::AddItem(const cItem & a_Item, bool a_AllowNewStacks) +int cInventory::AddItem(const cItem & a_Item, bool a_AllowNewStacks, bool a_tryToFillEquippedFirst) { cItem ToAdd(a_Item); int res = 0; @@ -112,7 +112,7 @@ int cInventory::AddItem(const cItem & a_Item, bool a_AllowNewStacks) } } - res += m_HotbarSlots.AddItem(ToAdd, a_AllowNewStacks); + res += m_HotbarSlots.AddItem(ToAdd, a_AllowNewStacks, a_tryToFillEquippedFirst ? m_EquippedSlotNum : -1); ToAdd.m_ItemCount = a_Item.m_ItemCount - res; if (ToAdd.m_ItemCount == 0) { @@ -127,12 +127,12 @@ int cInventory::AddItem(const cItem & a_Item, bool a_AllowNewStacks) -int cInventory::AddItems(cItems & a_ItemStackList, bool a_AllowNewStacks) +int cInventory::AddItems(cItems & a_ItemStackList, bool a_AllowNewStacks, bool a_tryToFillEquippedFirst) { int TotalAdded = 0; for (cItems::iterator itr = a_ItemStackList.begin(); itr != a_ItemStackList.end();) { - int NumAdded = AddItem(*itr, a_AllowNewStacks); + int NumAdded = AddItem(*itr, a_AllowNewStacks, a_tryToFillEquippedFirst); if (itr->m_ItemCount == NumAdded) { itr = a_ItemStackList.erase(itr); diff --git a/source/Inventory.h b/source/Inventory.h index 395ca8982..9ddd00ecd 100644 --- a/source/Inventory.h +++ b/source/Inventory.h @@ -66,17 +66,23 @@ public: /** 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. + 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. Returns the number of items that fit. */ - int AddItem(const cItem & a_ItemStack, bool a_AllowNewStacks = true); + int AddItem(const cItem & a_ItemStack, bool a_AllowNewStacks = true, bool a_tryToFillEquippedFirst = false); /** 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; - if a_AllowNewStacks is set to true, empty slots can be used for the rest + 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. Returns the total number of items that fit. */ - int AddItems(cItems & a_ItemStackList, bool a_AllowNewStacks); + int AddItems(cItems & a_ItemStackList, bool a_AllowNewStacks, bool a_tryToFillEquippedFirst); /// Removes one item out of the currently equipped item stack, returns true if successful, false if empty-handed bool RemoveOneEquippedItem(void); diff --git a/source/ItemGrid.cpp b/source/ItemGrid.cpp index 1154250f1..e4bcec46e 100644 --- a/source/ItemGrid.cpp +++ b/source/ItemGrid.cpp @@ -240,20 +240,51 @@ int cItemGrid::HowManyCanFit(const cItem & a_ItemStack) -int cItemGrid::AddItem(cItem & a_ItemStack, bool a_AllowNewStacks)
+int cItemGrid::AddItemToSlot(const cItem & a_ItemStack, int a_Slot, int a_Num, int a_MaxStack)
+{
+ int PrevCount = 0;
+ if (m_Slots[a_Slot].IsEmpty())
+ {
+ m_Slots[a_Slot] = a_ItemStack;
+ PrevCount = 0;
+ }
+ else
+ {
+ PrevCount = m_Slots[a_Slot].m_ItemCount;
+ }
+ m_Slots[a_Slot].m_ItemCount = std::min(a_MaxStack, PrevCount + a_Num);
+ int toReturn = m_Slots[a_Slot].m_ItemCount - PrevCount;
+ TriggerListeners(a_Slot);
+ return toReturn;
+}
+
+
+
+
+
+int cItemGrid::AddItem(cItem & a_ItemStack, bool a_AllowNewStacks, int a_PrioritarySlot)
{
int NumLeft = a_ItemStack.m_ItemCount;
int MaxStack = ItemHandler(a_ItemStack.m_ItemType)->GetMaxStackSize();
-
+
+ // Try prioritarySlot first:
+ if (
+ (a_PrioritarySlot != -1) &&
+ (
+ m_Slots[a_PrioritarySlot].IsEmpty() ||
+ m_Slots[a_PrioritarySlot].IsStackableWith(a_ItemStack)
+ )
+ )
+ {
+ NumLeft -= AddItemToSlot(a_ItemStack, a_PrioritarySlot, NumLeft, MaxStack);
+ }
+
// Scan existing stacks:
for (int i = m_NumSlots - 1; i >= 0; i--)
{
if (m_Slots[i].IsStackableWith(a_ItemStack))
{
- int PrevCount = m_Slots[i].m_ItemCount;
- m_Slots[i].m_ItemCount = std::min(MaxStack, PrevCount + NumLeft);
- NumLeft -= m_Slots[i].m_ItemCount - PrevCount;
- TriggerListeners(i);
+ NumLeft -= AddItemToSlot(a_ItemStack, i, NumLeft, MaxStack);
}
if (NumLeft <= 0)
{
@@ -271,10 +302,7 @@ int cItemGrid::AddItem(cItem & a_ItemStack, bool a_AllowNewStacks) {
if (m_Slots[i].IsEmpty())
{
- m_Slots[i] = a_ItemStack;
- m_Slots[i].m_ItemCount = std::min(MaxStack, NumLeft);
- NumLeft -= m_Slots[i].m_ItemCount;
- TriggerListeners(i);
+ NumLeft -= AddItemToSlot(a_ItemStack, i, NumLeft, MaxStack);
}
if (NumLeft <= 0)
{
@@ -289,12 +317,12 @@ int cItemGrid::AddItem(cItem & a_ItemStack, bool a_AllowNewStacks) -int cItemGrid::AddItems(cItems & a_ItemStackList, bool a_AllowNewStacks)
+int cItemGrid::AddItems(cItems & a_ItemStackList, bool a_AllowNewStacks, int a_PrioritarySlot)
{
int TotalAdded = 0;
for (cItems::iterator itr = a_ItemStackList.begin(); itr != a_ItemStackList.end();)
{
- int NumAdded = AddItem(*itr, a_AllowNewStacks);
+ int NumAdded = AddItem(*itr, a_AllowNewStacks, a_PrioritarySlot);
if (itr->m_ItemCount == NumAdded)
{
itr = a_ItemStackList.erase(itr);
diff --git a/source/ItemGrid.h b/source/ItemGrid.h index 6b2713c45..45f978530 100644 --- a/source/ItemGrid.h +++ b/source/ItemGrid.h @@ -76,18 +76,24 @@ public: /** 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
+ if a_AllowNewStacks is set to true, empty slots can be used for the rest.
+ If a_PrioritarySlot is set to a positive value, then the corresponding slot will be used in
+ first (if empty or compatible with added items)
+ if a_PrioritarySlot is set to -1, regular order apply
Returns the number of items that fit.
*/
- int AddItem(cItem & a_ItemStack, bool a_AllowNewStacks = true);
+ int AddItem(cItem & a_ItemStack, bool a_AllowNewStacks = true, int a_PrioritarySlot = -1);
/** 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;
- if a_AllowNewStacks is set to true, empty slots can be used for the rest
+ if a_AllowNewStacks is set to true, empty slots can be used for the rest.
+ If a_PrioritarySlot is set to a positive value, then the corresponding slot will be used in
+ first (if empty or compatible with added items)
+ if a_PrioritarySlot is set to -1, regular order apply
Returns the total number of items that fit.
*/
- int AddItems(cItems & a_ItemStackList, bool a_AllowNewStacks = true);
+ int AddItems(cItems & a_ItemStackList, bool a_AllowNewStacks = true, int a_PrioritarySlot = -1);
/** 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.
@@ -160,7 +166,7 @@ public: void RemoveListener(cListener & a_Listener);
// tolua_begin
-
+
protected:
int m_Width;
int m_Height;
@@ -173,6 +179,11 @@ protected: /// Calls all m_Listeners for the specified slot number
void TriggerListeners(int a_SlotNum);
+
+ /** Adds up to a_Num items out of a_ItemStack, as many as can fit, in specified slot
+ Returns the number of items that did fit.
+ */
+ int AddItemToSlot(const cItem & a_ItemStack, int a_Slot, int a_Num, int a_MaxStack);
} ;
// tolua_end
diff --git a/source/Items/ItemBucket.h b/source/Items/ItemBucket.h index 4792fa62d..e4adc98b8 100644 --- a/source/Items/ItemBucket.h +++ b/source/Items/ItemBucket.h @@ -90,7 +90,7 @@ public: // Give new bucket, filled with fluid:
cItem Item(NewItem, 1);
- a_Player->GetInventory().AddItem(Item);
+ a_Player->GetInventory().AddItem(Item, true, true);
// Remove water / lava block
a_Player->GetWorld()->SetBlock(a_BlockX, a_BlockY, a_BlockZ, E_BLOCK_AIR, 0);
@@ -136,7 +136,7 @@ public: return false;
}
cItem Item(E_ITEM_BUCKET, 1);
- if (!a_Player->GetInventory().AddItem(Item))
+ if (!a_Player->GetInventory().AddItem(Item,true,true))
{
return false;
}
@@ -157,4 +157,4 @@ public: return true;
}
-};
\ No newline at end of file +};
|