Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
4.0 kB
3
Indexable
Never
bool CHARACTER::CanOpenChest(LPITEM item, int openCount)
{
    const CSpecialItemGroup* pGroup = ITEM_MANAGER::instance().GetSpecialItemGroup(item->GetVnum());
    if (!pGroup || pGroup->IsEmpty())
    {
        sys_err("OpenChest: cannot find special item group %d", item->GetVnum());
        return false;
    }

    std::vector<int> idxes;
    int n = pGroup->GetMultiIndex2(idxes);
    for (int i = 0; i < n; i++)
    {
        int idx = idxes[i];
        DWORD dwVnum = pGroup->GetVnum(idx);
        DWORD dwCountPerChest = pGroup->GetCount(idx);
        BYTE bPercent = pGroup->GetMyPercent(idx);
        DWORD per = (openCount * bPercent) / 100;
        DWORD totalItemCount = dwCountPerChest * per;

        if (totalItemCount > 0)
        {
            // Item'in proto bilgilerini al
            LPITEM pItem = ITEM_MANAGER::instance().CreateItem(dwVnum, totalItemCount);
            if (!pItem)
            {
                ChatPacket(CHAT_TYPE_INFO, "Item creation failed for Vnum: %u.", dwVnum);
                return false;
            }

            bool hasSpace = false;

            // Stacklenebilir itemler için kontrol
            if (pItem->IsStackable() && !IS_SET(pItem->GetAntiFlag(), ITEM_ANTIFLAG_STACK))
            {
                DWORD requiredCount = totalItemCount;

                // Mevcut stack'leri kontrol et
                for (int i = 0; i < INVENTORY_MAX_NUM; ++i)
                {
                    LPITEM existingItem = GetInventoryItem(i);
                    if (existingItem && existingItem->GetVnum() == dwVnum && existingItem->IsStackable())
                    {
                        DWORD currentCount = existingItem->GetCount();
                        if (currentCount < ITEM_MAX_COUNT)
                        {
                            DWORD freeSpace = ITEM_MAX_COUNT - currentCount;
                            if (freeSpace >= requiredCount)
                            {
                                hasSpace = true;
                                break;
                            }
                            else
                            {
                                requiredCount -= freeSpace;
                            }
                        }
                    }
                }

                // Eğer hala yer yoksa boş slotlarda yer aramaya devam et
                if (!hasSpace)
                {
                    for (int i = 0; i < INVENTORY_MAX_NUM; ++i)
                    {
                        if (!GetInventoryItem(i))
                        {
                            if (requiredCount <= ITEM_MAX_COUNT)
                            {
                                hasSpace = true;
                                break;
                            }
                            else
                            {
                                requiredCount -= ITEM_MAX_COUNT;
                            }
                        }
                    }
                }
            }

            // Stacklenemeyen itemler için kontrol
            if (!hasSpace) 
            {
                for (int i = 0; i < INVENTORY_MAX_NUM; ++i)
                {
                    LPITEM existingItem = GetInventoryItem(i);

                    // Eğer item stacklenemiyorsa veya slotta item varsa, dolu kabul et
                    if (existingItem && (!existingItem->IsStackable() || IS_SET(existingItem->GetAntiFlag(), ITEM_ANTIFLAG_STACK)))
                    {
                        continue;  // Bu slot dolu kabul edilir
                    }

                    // Boş slot bulundu
                    if (!existingItem)
                    {
                        hasSpace = true;
                        break;
                    }
                }
            }

            if (!hasSpace)
            {
                ChatPacket(CHAT_TYPE_INFO, "Not enough space in inventory for item: %u.", dwVnum);
                return false;
            }
        }
    }
    return true;
}
Leave a Comment