270 lines
8.0 KiB
C++
270 lines
8.0 KiB
C++
// PORT: AVA2 ENABLE_MULTI_CHEST; see docs/FEATURE-AVA2-CHEST.md for adaptations.
|
|
void CHARACTER::OpenChest(LPITEM item, WORD openCount)
|
|
{
|
|
// Validate the client supplied position before entering AVA2's batch reward path.
|
|
if (!item || item->GetOwner()!=this || !openCount || item->GetType()!=ITEM_GIFTBOX ||
|
|
item->IsEquipped() || item->IsExchanging() || item->isLocked() ||
|
|
IsDead() || IsStun() || IsWarping() || IsObserverMode() || !CanHandleItem() ||
|
|
GetExchange() || GetMyShop() || GetShopOwner() || IsOpenSafebox() || IsCubeOpen()) return;
|
|
|
|
for(int i=0;i<ITEM_LIMIT_MAX_NUM;++i)
|
|
if(item->GetLimitType(i)==LIMIT_LEVEL && GetLevel()<item->GetLimitValue(i)) return;
|
|
if(CArenaManager::instance().IsLimitedItem(GetMapIndex(),item->GetVnum())) return;
|
|
const DWORD vnum=item->GetVnum();
|
|
if(((vnum>51500 && vnum<52000) || (vnum>=50255 && vnum<=50260)) && !DragonSoul_IsQualified()) return;
|
|
if(openCount > item->GetCount())
|
|
openCount = item->GetCount();
|
|
|
|
if(openCount > 1000)
|
|
openCount =1000;
|
|
|
|
std::map<DWORD, DWORD> dwItemVnums;
|
|
const WORD openCountCache = openCount;
|
|
if (GiveItemFromSpecialItemGroupNew(item->GetVnum(), openCount))
|
|
{
|
|
if(openCount != 0)
|
|
{
|
|
ChatPacket(CHAT_TYPE_COMMAND, "chest_opened %u", unsigned(openCountCache-openCount));
|
|
item->SetCount(item->GetCount() - (openCountCache-openCount));
|
|
}
|
|
else
|
|
item->SetCount(item->GetCount() - openCountCache);
|
|
}
|
|
else
|
|
ChatPacket(CHAT_TYPE_COMMAND, "chest_error full");
|
|
}
|
|
bool CHARACTER::AutoGiveItemChest(LPITEM item, DWORD& itemCount, std::map<LPITEM, WORD>& vecUpdateItems)
|
|
{
|
|
int cell = -1;
|
|
if (item->IsDragonSoul())
|
|
cell = GetEmptyDragonSoulInventory(item);
|
|
else
|
|
cell = GetEmptyInventory(item->GetSize());
|
|
|
|
if (cell == -1)
|
|
return false;
|
|
|
|
const DWORD itemVnum = item->GetVnum();
|
|
WORD wCount = item->GetCount();
|
|
|
|
if (item->IsStackable() && item->GetType() != ITEM_BLEND)
|
|
{
|
|
for (auto it = vecUpdateItems.begin(); it != vecUpdateItems.end(); ++it)
|
|
{
|
|
LPITEM item2 = it->first;
|
|
if (itemVnum == item2->GetVnum())
|
|
{
|
|
bool same=true;
|
|
for(int j=0;j<ITEM_SOCKET_MAX_NUM;++j)
|
|
if(item2->GetSocket(j)!=item->GetSocket(j)) same=false;
|
|
if(!same) continue;
|
|
const DWORD item2Count = item2->GetCount();
|
|
const WORD bCount2 = MIN(ITEM_MAX_COUNT - item2Count, wCount);
|
|
if (bCount2 > 0)
|
|
{
|
|
wCount -= bCount2;
|
|
item2->SetCount(item2Count + bCount2);
|
|
if (wCount == 0)
|
|
{
|
|
itemCount = 0;
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
{
|
|
for (int i = 0; i < INVENTORY_MAX_NUM; ++i)
|
|
{
|
|
LPITEM item2 = GetInventoryItem(i);
|
|
if (!item2)
|
|
continue;
|
|
if (item2->GetVnum() == itemVnum)
|
|
{
|
|
BYTE j;
|
|
for (j = 0; j < ITEM_SOCKET_MAX_NUM; ++j)
|
|
if (item2->GetSocket(j) != item->GetSocket(j))
|
|
break;
|
|
if (j != ITEM_SOCKET_MAX_NUM)
|
|
continue;
|
|
const DWORD item2Count = item2->GetCount();
|
|
const WORD bCount2 = MIN(ITEM_MAX_COUNT - item2Count, wCount);
|
|
if (bCount2 > 0)
|
|
{
|
|
const auto itItem = vecUpdateItems.find(item2);
|
|
if(itItem == vecUpdateItems.end())
|
|
vecUpdateItems.emplace(item2, item2Count);
|
|
wCount -= bCount2;
|
|
item2->SetUpdateStatus(true);
|
|
item2->SetCount(item2Count + bCount2);
|
|
if (wCount == 0)
|
|
{
|
|
itemCount = 0;
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
if (wCount > 0)
|
|
{
|
|
item->SetCount(wCount);
|
|
if (item->IsDragonSoul())
|
|
item->AddToCharacter(this, TItemPos(DRAGON_SOUL_INVENTORY, cell));
|
|
else
|
|
item->AddToCharacter(this, TItemPos(INVENTORY, cell));
|
|
|
|
if(item->IsStackable())
|
|
{
|
|
const auto itItem = vecUpdateItems.find(item);
|
|
if(itItem == vecUpdateItems.end())
|
|
vecUpdateItems.emplace(item, wCount);
|
|
}
|
|
itemCount = wCount;
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
bool CHARACTER::GiveItemFromSpecialItemGroupNew(DWORD dwGroupNum, WORD& loopCount)
|
|
{
|
|
const CSpecialItemGroup* pGroup = ITEM_MANAGER::instance().GetSpecialItemGroup(dwGroupNum);
|
|
if (!pGroup)
|
|
{
|
|
sys_err("cannot find special item group %d", dwGroupNum);
|
|
return false;
|
|
}
|
|
std::map<DWORD, DWORD> vecItemCounts;
|
|
std::map<LPITEM, WORD> vecUpdateItems;
|
|
const WORD constCount = loopCount;
|
|
WORD workCount = loopCount;
|
|
|
|
int returnCount = 5;
|
|
|
|
for (DWORD j = 0; j < constCount; ++j)
|
|
{
|
|
if (returnCount <= 0 || workCount <= 0)
|
|
break;
|
|
std::vector <int> idxes;
|
|
const int n = pGroup->GetMultiIndex(idxes);
|
|
|
|
bool isOkey = false;
|
|
|
|
for (int i = 0; i < n; i++)
|
|
{
|
|
if (returnCount <= 0 || (workCount <= 0 && !isOkey))
|
|
break;
|
|
|
|
const int idx = idxes[i];
|
|
const DWORD dwVnum = pGroup->GetVnum(idx);
|
|
const DWORD dwEarnCount = pGroup->GetCount(idx);
|
|
DWORD dwCount = pGroup->GetCount(idx);
|
|
|
|
switch (dwVnum)
|
|
{
|
|
case CSpecialItemGroup::GOLD:
|
|
PointChange(POINT_GOLD, dwCount);
|
|
if(isOkey == false)
|
|
{
|
|
isOkey = true;
|
|
workCount -= 1;
|
|
}
|
|
break;
|
|
case CSpecialItemGroup::POISON:
|
|
AttackedByPoison(NULL);
|
|
if(isOkey == false)
|
|
{
|
|
isOkey = true;
|
|
workCount -= 1;
|
|
}
|
|
break;
|
|
case CSpecialItemGroup::EXP:
|
|
PointChange(POINT_EXP, dwCount);
|
|
if(isOkey == false)
|
|
{
|
|
isOkey = true;
|
|
workCount -= 1;
|
|
}
|
|
break;
|
|
case CSpecialItemGroup::MOB:
|
|
{
|
|
LPCHARACTER ch = CHARACTER_MANAGER::instance().SpawnMob(dwCount, GetMapIndex(), GetX() + number(-500, 500), GetY() + number(-500, 500), 0, true, -1);
|
|
if (ch)
|
|
ch->SetAggressive();
|
|
|
|
if(isOkey == false)
|
|
{
|
|
isOkey = true;
|
|
workCount -= 1;
|
|
}
|
|
}
|
|
break;
|
|
case CSpecialItemGroup::MOB_GROUP:
|
|
CHARACTER_MANAGER::instance().SpawnGroup(dwCount, GetMapIndex(), GetX() - number(300, 500), GetY() - number(300, 500), GetX() + number(300, 500), GetY() + number(300, 500), NULL, true);
|
|
if(isOkey == false)
|
|
{
|
|
isOkey = true;
|
|
workCount -= 1;
|
|
}
|
|
break;
|
|
case CSpecialItemGroup::SLOW:
|
|
AddAffect(AFFECT_SLOW, POINT_MOV_SPEED, -(int)dwCount, AFF_SLOW, 300, 0, true);
|
|
if(isOkey == false)
|
|
{
|
|
isOkey = true;
|
|
workCount -= 1;
|
|
}
|
|
break;
|
|
case CSpecialItemGroup::DRAIN_HP:
|
|
{
|
|
int iDropHP = GetMaxHP() * dwCount / 100;
|
|
iDropHP = MIN(iDropHP, GetHP() - 1);
|
|
PointChange(POINT_HP, -iDropHP);
|
|
if(isOkey == false)
|
|
{
|
|
isOkey = true;
|
|
workCount -= 1;
|
|
}
|
|
}
|
|
break;
|
|
default:
|
|
{
|
|
// The selected base has 200-count stacks and byte item-count packets.
|
|
// AVA2 rewards larger than a stack must be split, never silently clamped by CreateItem.
|
|
DWORD remaining=dwEarnCount;
|
|
while(remaining) {
|
|
LPITEM item=ITEM_MANAGER::Instance().CreateItem(dwVnum,MIN(remaining,ITEM_MAX_COUNT));
|
|
if(!item) { --returnCount; break; }
|
|
const DWORD created=item->GetCount();
|
|
DWORD count=created;
|
|
if(!created || !AutoGiveItemChest(item,count,vecUpdateItems)) {
|
|
M2_DESTROY_ITEM(item); --returnCount; break;
|
|
}
|
|
if(!isOkey) { isOkey=true; --workCount; }
|
|
vecItemCounts[dwVnum]+=created;
|
|
remaining-=MIN(remaining,created); // Non-stackable count=1; MAKECOUNT prototypes may create a larger minimum.
|
|
if(!count) M2_DESTROY_ITEM(item);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
for (auto it = vecUpdateItems.begin(); it != vecUpdateItems.end(); ++it)
|
|
{
|
|
LPITEM item = it->first;
|
|
item->SetUpdateStatus(false);
|
|
if (item->GetCount() != it->second)
|
|
item->UpdatePacket();
|
|
|
|
if (item->GetType() == ITEM_QUEST || item->GetType() == ITEM_MATERIAL)
|
|
quest::CQuestManager::instance().PickupItem (GetPlayerID(), item);
|
|
}
|
|
|
|
for (auto it = vecItemCounts.begin(); it != vecItemCounts.end(); ++it)
|
|
ChatPacket(CHAT_TYPE_COMMAND, "chest_reward %u %u", it->first, it->second);
|
|
|
|
loopCount = workCount;
|
|
return (workCount != constCount);
|
|
}
|