1
0

Fixed issues with pickups.

* Now when picking up max. stack amount is checked.
* Added cInventory::AddItemAnyAmount() which will not fail if it cannot add all items (it will just modify amount)
* If there is no space in inventory and picking up stacked items, it will try to fill stacks already in inventory, partially picking up the item.
* When closing inventory player will drop any items it's currently 'dragging'

git-svn-id: http://mc-server.googlecode.com/svn/trunk@1008 0a769ca7-a7f5-676a-18bf-c427514a06d6
This commit is contained in:
sebi.noreply@gmail.com 2012-10-24 12:48:25 +00:00
parent 5c5463c335
commit e33e9111ab
5 changed files with 52 additions and 8 deletions

View File

@ -89,6 +89,36 @@ bool cInventory::AddItem( cItem & a_Item )
bool cInventory::AddItemAnyAmount( cItem & a_Item )
{
bool ChangedSlots[c_NumSlots];
memset( ChangedSlots, false, c_NumSlots * sizeof( bool ) );
char StartCount = a_Item.m_ItemCount;
if( a_Item.m_ItemCount > 0 ) AddToBar( a_Item, c_HotOffset, c_HotSlots, ChangedSlots, 0 );
if( a_Item.m_ItemCount > 0 ) AddToBar( a_Item, c_MainOffset, c_MainSlots, ChangedSlots, 0 );
if( a_Item.m_ItemCount > 0 ) AddToBar( a_Item, c_HotOffset, c_HotSlots, ChangedSlots, 2 );
if( a_Item.m_ItemCount > 0 ) AddToBar( a_Item, c_MainOffset, c_MainSlots, ChangedSlots, 2 );
if (a_Item.m_ItemCount == StartCount)
return false;
for (unsigned int i = 0; i < c_NumSlots; i++)
{
if (ChangedSlots[i])
{
LOGD("cInventory::AddItemAnyAmount(): Item was added to %i ID:%i Count:%i", i, m_Slots[i].m_ItemID, m_Slots[i].m_ItemCount);
SendSlot(i);
}
}
return true;
}
// TODO: Right now if you dont have enough items, the items you did have are removed, and the function returns false anyway
bool cInventory::RemoveItem( cItem & a_Item )
{
@ -347,11 +377,12 @@ bool cInventory::AddToBar( cItem & a_Item, const int a_Offset, const int a_Size,
// Fill already present stacks
if( a_Mode < 2 )
{
int MaxStackSize = cItemHandler::GetItemHandler(a_Item.m_ItemType)->GetMaxStackSize();
for(int i = 0; i < a_Size; i++)
{
if( m_Slots[i + a_Offset].m_ItemID == a_Item.m_ItemID && m_Slots[i + a_Offset].m_ItemCount < 64 && m_Slots[i + a_Offset].m_ItemHealth == a_Item.m_ItemHealth )
if( m_Slots[i + a_Offset].m_ItemType == a_Item.m_ItemType && m_Slots[i + a_Offset].m_ItemCount < MaxStackSize && m_Slots[i + a_Offset].m_ItemHealth == a_Item.m_ItemHealth )
{
int NumFree = 64 - m_Slots[i + a_Offset].m_ItemCount;
int NumFree = MaxStackSize - m_Slots[i + a_Offset].m_ItemCount;
if( NumFree >= a_Item.m_ItemCount )
{
@ -377,7 +408,7 @@ bool cInventory::AddToBar( cItem & a_Item, const int a_Offset, const int a_Size,
// If we got more left, find first empty slot
for(int i = 0; i < a_Size && a_Item.m_ItemCount > 0; i++)
{
if( m_Slots[i + a_Offset].m_ItemID == -1 )
if( m_Slots[i + a_Offset].m_ItemType == -1 )
{
m_Slots[i + a_Offset] = a_Item;
a_Item.m_ItemCount = 0;

View File

@ -31,6 +31,7 @@ public:
int GetSlotCountForType( int a_Type );
bool AddItem( cItem & a_Item ); //tolua_export
bool AddItemAnyAmount( cItem & a_Item ); //tolua_export
bool RemoveItem( cItem & a_Item ); //tolua_export
void SaveToJson(Json::Value & a_Value);

View File

@ -256,12 +256,16 @@ bool cPickup::CollectedBy( cPlayer* a_Dest )
return false;
}
if (a_Dest->GetInventory().AddItem(*m_Item))
if (a_Dest->GetInventory().AddItemAnyAmount(*m_Item))
{
m_World->BroadcastCollectPickup(*this, *a_Dest);
m_bCollected = true;
m_Timer = 0;
if( m_Item->m_ItemCount != 0 ) {
cItems Pickup;
Pickup.push_back(cItem(*m_Item));
m_World->SpawnItemPickups(Pickup, m_Pos.x, m_Pos.y, m_Pos.z);
}
return true;
}

View File

@ -443,6 +443,11 @@ void cPlayer::CloseWindow(char a_WindowType)
if (m_CurrentWindow == m_InventoryWindow)
{
// The inventory window must not be closed and must not be even sent a close packet
if (IsDraggingItem()) // But we need to check if player is holding anything
{
LOGD("Player holds item in inventory window! Dropping it...");
TossItem(true, GetDraggingItem().m_ItemCount);
}
return;
}
@ -791,10 +796,12 @@ void cPlayer::TossItem(
cItem & Item = GetDraggingItem();
if (!Item.IsEmpty())
{
char OriginalItemAmount = Item.m_ItemCount;
Item.m_ItemCount = MIN(OriginalItemAmount, a_Amount);
Drops.push_back(Item);
if (Item.m_ItemCount > a_Amount)
if (OriginalItemAmount > a_Amount)
{
Item.m_ItemCount -= (char)a_Amount;
Item.m_ItemCount = OriginalItemAmount - (char)a_Amount;
}
else
{

View File

@ -138,11 +138,12 @@ void cSlotArea::Clicked(cPlayer & a_Player, int a_SlotNum, bool a_IsRightClick,
}
}
SetSlot(a_SlotNum, a_Player, Slot);
if (bAsync)
{
m_ParentWindow.BroadcastWholeWindow();
}
SetSlot(a_SlotNum, a_Player, Slot);
}