示例#1
0
文件: goods.cpp 项目: mildrock/dummy
UINT16 CGoods::UseRateBox(DT_ITEM_DATA_LIST_CLI2& stBoxItemInfo)
{
    SGoodsProp*	pstGoodsProp =  GetGoodsProp();
    if(NULL == pstGoodsProp)
    {
        RETURN_OTHER_ERR;
    }

    stBoxItemInfo.byItemNum = 0;

    SItemComposeProp* pstItemComposeProp = CItemComposePropMgr::Instance()->GetProp(pstGoodsProp->wKindID);
    if(NULL == pstItemComposeProp)
    {
        SYS_CRITICAL(_SDT("[%s: %d]: pstItemComposeProp is NULL!"), MSG_MARK);
        RETURN_OTHER_ERR;
    }

    CItemRateMap& mapComposeItem = pstItemComposeProp->mapComposeItem;
    if(0 == mapComposeItem.size())
    {
        SYS_CRITICAL(_SDT("[%s: %d]: mapComposeItem[%d] is 0!"), MSG_MARK, pstGoodsProp->wKindID);
        RETURN_OTHER_ERR;
    }

    //计算随机值
    CRandomVec vecRandom;
    for(CItemRateMapItr itr = mapComposeItem.begin(); itr != mapComposeItem.end(); itr++)
    {
        vecRandom.push_back(itr->second.wRate);
    }

    //随机
    INT32 nRandomHitIdx = GetRandomIdxMustHit(vecRandom);
    if(RANDOM_UNHIT == nRandomHitIdx)
    {
        //随机不到物品也算使用成功
        return ERR_GOODS_USE::ID_SUCCESS;
    }

    stBoxItemInfo.byItemNum = 0;
    UINT8 byIdx = 0;
    for(CItemRateMapItr itr = mapComposeItem.begin(); itr != mapComposeItem.end(); itr++, byIdx++)
    {
        if(byIdx == nRandomHitIdx)
        {
            //若是系统可以直接使用的物品,则使用
            if(CItemMgr::Instance()->SysUseGoods(m_poOwner, itr->first, itr->second.wItemNum))
            {
                CItemMgr::Instance()->GetDT_ITEM_DATA_CLI2(itr->first, itr->second.wItemNum, stBoxItemInfo.astItemList[stBoxItemInfo.byItemNum++]);
            }
            else
            {
                UINT16 wItemKindID = itr->first;
                ECreateItemRet eRet;

                CItem* poItem = CItemMgr::Instance()->CreateItemIntoBag(m_poOwner, wItemKindID, itr->second.wItemNum, eRet, 0, CRecordMgr::EAIT_BOX, m_stDT_GOODS_DATA.wKindID );
                if(NULL == poItem)
                {
                    if(ECIR_FAILED_BAG_FULL == eRet)
                    {
                        return ERR_GOODS_USE::ID_BAG_FULL;
                    }
                    else
                    {
                        //异常
                        SYS_CRITICAL(_SDT("[%s: %d]: CreateItemIntoBag failed[%d]!"), MSG_MARK, wItemKindID);
                        RETURN_OTHER_ERR;
                    }
                }

                //内部礼包,返回的是礼包的物品
                SGoodsProp* pstGoodsProp = CGoodsPropMgr::Instance()->GetPropByPrimaryKey(itr->first);
                if((pstGoodsProp) && (EGMK_INNERBOX == pstGoodsProp->byMainKindID))
                {
                    CItemMgr::Instance()->GetDT_ITEM_DATA_CLI2(poItem->GetItemKindID(), 1, stBoxItemInfo.astItemList[stBoxItemInfo.byItemNum++]);
                }
                else
                {
                    CItemMgr::Instance()->GetDT_ITEM_DATA_CLI2(itr->first, itr->second.wItemNum, stBoxItemInfo.astItemList[stBoxItemInfo.byItemNum++]);
                }
            }

            break; //只爆出1个物品
        }
    }

    return ERR_GOODS_USE::ID_SUCCESS;
}
示例#2
0
文件: goods.cpp 项目: mildrock/dummy
UINT16 CGoods::UseBox(DT_ITEM_DATA_LIST_CLI2& stBoxItemInfo)
{
    SGoodsProp*	pstGoodsProp =  GetGoodsProp();
    if(NULL == pstGoodsProp)
    {
        RETURN_OTHER_ERR;
    }

    SItemComposeProp* pstItemComposeProp = CItemComposePropMgr::Instance()->GetProp(pstGoodsProp->wKindID);
    if(NULL == pstItemComposeProp)
    {
        SYS_CRITICAL(_SDT("[%s: %d]: pstItemComposeProp is NULL!"), MSG_MARK);
        RETURN_OTHER_ERR;
    }

    CItemRateMap& mapComposeItem = pstItemComposeProp->mapComposeItem;
    if(0 == mapComposeItem.size())
    {
        SYS_CRITICAL(_SDT("[%s: %d]: mapComposeItem[%d] is 0!"), MSG_MARK, pstGoodsProp->wKindID);
        RETURN_OTHER_ERR;
    }

    INT32 nGap = (INT32)(m_poOwner->GetBag().GetIdleSize()) - (INT32)(mapComposeItem.size());
    if(nGap < 0)
    {
        //判断背包中是否存在该物品,若存在则可少使用一个格子
        CBag& oBag = m_poOwner->GetBag();
        for(CItemRateMapItr itr = mapComposeItem.begin(); itr != mapComposeItem.end(); itr++)
        {
            //只有物品有效,装备叠加数为1,则只找Goods
            if(oBag.GetGoodsItem(itr->first))
            {
                nGap++;
            }
        }
    }

    //背包容量不足
    if(nGap < 0)
    {
        return ERR_GOODS_USE::ID_BAG_FULL;
    }

    for(CItemRateMapItr itr = mapComposeItem.begin(); itr != mapComposeItem.end(); itr++)
    {
        //若是系统可以直接使用的物品,则使用
        if(CItemMgr::Instance()->SysUseGoods(m_poOwner, itr->first, itr->second.wItemNum))
        {
            CItemMgr::Instance()->GetDT_ITEM_DATA_CLI2(itr->first, itr->second.wItemNum, stBoxItemInfo.astItemList[stBoxItemInfo.byItemNum]);
        }
        else
        {
            ECreateItemRet eRet;
            CItem* poItem = CItemMgr::Instance()->CreateItemIntoBag(m_poOwner, itr->first, itr->second.wItemNum, eRet, 0, CRecordMgr::EAIT_BOX, m_stDT_GOODS_DATA.wKindID);
            if(NULL == poItem)
            {
                //异常
                SYS_CRITICAL(_SDT("[%s: %d]: CreateItemIntoBag failed[%d]!"), MSG_MARK, itr->first);
            }
            //装备没有叠加数,可以直接poEquip->GetDT_EQUIP_DATA_CLI(stEquip);
            if(EIK_EQUIP == poItem->GetItemKind())
            {
                CEquip* poEquip = dynamic_cast<CEquip*>(poItem);
                if(NULL == poEquip)
                {
                    //异常
                    continue;
                }
                stBoxItemInfo.astItemList[stBoxItemInfo.byItemNum].byItemType = EIK_EQUIP;
                DT_EQUIP_DATA_CLI& stEquip = stBoxItemInfo.astItemList[stBoxItemInfo.byItemNum].stItemInfo.stEquip ;
                poEquip->GetDT_EQUIP_DATA_CLI(stEquip);
            }
            else
            {
                //内部礼包,返回的是礼包的物品
                SGoodsProp* pstGoodsProp = CGoodsPropMgr::Instance()->GetPropByPrimaryKey(itr->first);
                if((pstGoodsProp) && (EGMK_INNERBOX == pstGoodsProp->byMainKindID))
                {
                    CItemMgr::Instance()->GetDT_ITEM_DATA_CLI2(poItem->GetItemKindID(), 1, stBoxItemInfo.astItemList[stBoxItemInfo.byItemNum]);
                }
                else
                {
                    CItemMgr::Instance()->GetDT_ITEM_DATA_CLI2(itr->first, itr->second.wItemNum, stBoxItemInfo.astItemList[stBoxItemInfo.byItemNum]);
                }
            }
        }

        stBoxItemInfo.byItemNum++;
    }

    return ERR_GOODS_USE::ID_SUCCESS;
}