Index: source/Bindings.cpp =================================================================== --- source/Bindings.cpp (révision 1640) +++ source/Bindings.cpp (copie de travail) @@ -12978,7 +12978,7 @@ 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 @@ 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); } } Index: source/Inventory.cpp =================================================================== --- source/Inventory.cpp (révision 1640) +++ source/Inventory.cpp (copie de travail) @@ -98,7 +98,7 @@ -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 @@ } } - 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) { @@ -125,14 +125,12 @@ - - -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); Index: source/ItemGrid.cpp =================================================================== --- source/ItemGrid.cpp (révision 1640) +++ source/ItemGrid.cpp (copie de travail) @@ -238,22 +238,47 @@ +int cItemGrid::AddItemToSlot(cItem & a_ItemStack, int a_Slot, int a_NumLeft, 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_NumLeft); + int toReturn = m_Slots[a_Slot].m_ItemCount - PrevCount; + TriggerListeners(a_Slot); + return toReturn; +} - -int cItemGrid::AddItem(cItem & a_ItemStack, bool a_AllowNewStacks) +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 +296,7 @@ { 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 +311,12 @@ -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); Index: source/Inventory.h =================================================================== --- source/Inventory.h (révision 1640) +++ source/Inventory.h (copie de travail) @@ -66,17 +66,23 @@ /** 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_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); Index: source/Items/ItemBucket.h =================================================================== --- source/Items/ItemBucket.h (révision 1640) +++ source/Items/ItemBucket.h (copie de travail) @@ -90,7 +90,7 @@ // 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 @@ 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 @@ return true; } -}; \ No newline at end of file +}; Index: source/ItemGrid.h =================================================================== --- source/ItemGrid.h (révision 1640) +++ source/ItemGrid.h (copie de travail) @@ -77,17 +77,23 @@ /** 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_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_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,8 +166,15 @@ void RemoveListener(cListener & a_Listener); // tolua_begin - + protected: + /** Adds as many items out of a_ItemStack as can fit in specified slot + Returns the number of items that fit. + */ + + int AddItemToSlot(cItem & a_ItemStack, int a_Slot, int a_Num, int a_MaxStack); + +protected: int m_Width; int m_Height; int m_NumSlots; // m_Width * m_Height, for easier validity checking in the access functions