1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-11-29 23:07:48 +02:00

manageBackpackArtifacts

This commit is contained in:
SoundSSGood
2024-09-26 13:47:26 +03:00
parent f816c3306b
commit e22d15b1d8
7 changed files with 89 additions and 27 deletions

View File

@@ -2655,22 +2655,66 @@ bool CGameHandler::bulkMoveArtifacts(const PlayerColor & player, ObjectInstanceI
return true;
}
bool CGameHandler::scrollBackpackArtifacts(const PlayerColor & player, const ObjectInstanceID heroID, bool left)
bool CGameHandler::manageBackpackArtifacts(const PlayerColor & player, const ObjectInstanceID heroID, const ManageBackpackArtifacts::ManageCmd & sortType)
{
const auto artSet = getArtSet(heroID);
COMPLAIN_RET_FALSE_IF(artSet == nullptr, "scrollBackpackArtifacts: wrong hero's ID");
COMPLAIN_RET_FALSE_IF(artSet == nullptr, "manageBackpackArtifacts: wrong hero's ID");
BulkMoveArtifacts bma(player, heroID, heroID, false);
const auto backpackEnd = ArtifactPosition(ArtifactPosition::BACKPACK_START + artSet->artifactsInBackpack.size() - 1);
if(backpackEnd > ArtifactPosition::BACKPACK_START)
const auto makeSortBackpackRequest = [artSet, &bma](const std::function<int32_t(const ArtSlotInfo&)> & getSortId)
{
if(left)
bma.artsPack0.push_back(BulkMoveArtifacts::LinkedSlots(backpackEnd, ArtifactPosition::BACKPACK_START));
else
bma.artsPack0.push_back(BulkMoveArtifacts::LinkedSlots(ArtifactPosition::BACKPACK_START, backpackEnd));
sendAndApply(&bma);
std::map<int32_t, std::vector<BulkMoveArtifacts::LinkedSlots>> packsSorted;
ArtifactPosition backpackSlot = ArtifactPosition::BACKPACK_START;
for(const auto & backpackSlotInfo : artSet->artifactsInBackpack)
packsSorted.try_emplace(getSortId(backpackSlotInfo)).first->second.emplace_back(backpackSlot++, ArtifactPosition::PRE_FIRST);
for(auto & [sortId, pack] : packsSorted)
{
// Each pack of artifacts is also sorted by ArtifactID
std::sort(pack.begin(), pack.end(), [artSet](const auto & slots0, const auto & slots1) -> bool
{
return artSet->getArt(slots0.srcPos)->getTypeId().num > artSet->getArt(slots1.srcPos)->getTypeId().num;
});
bma.artsPack0.insert(bma.artsPack0.end(), pack.begin(), pack.end());
}
backpackSlot = ArtifactPosition::BACKPACK_START;
for(auto & slots : bma.artsPack0)
slots.dstPos = backpackSlot++;
};
if(sortType == ManageBackpackArtifacts::ManageCmd::SORT_BY_SLOT)
{
makeSortBackpackRequest([](const ArtSlotInfo & inf) -> int32_t
{
return inf.getArt()->artType->getPossibleSlots().at(ArtBearer::HERO).front().num;
});
}
else if(sortType == ManageBackpackArtifacts::ManageCmd::SORT_BY_COST)
{
makeSortBackpackRequest([](const ArtSlotInfo & inf) -> int32_t
{
return inf.getArt()->artType->getPrice();
});
}
else if(sortType == ManageBackpackArtifacts::ManageCmd::SORT_BY_CLASS)
{
makeSortBackpackRequest([](const ArtSlotInfo & inf) -> int32_t
{
return inf.getArt()->artType->aClass;
});
}
else
{
const auto backpackEnd = ArtifactPosition(ArtifactPosition::BACKPACK_START + artSet->artifactsInBackpack.size() - 1);
if(backpackEnd > ArtifactPosition::BACKPACK_START)
{
if(sortType == ManageBackpackArtifacts::ManageCmd::SCROLL_LEFT)
bma.artsPack0.emplace_back(backpackEnd, ArtifactPosition::BACKPACK_START);
else
bma.artsPack0.emplace_back(ArtifactPosition::BACKPACK_START, backpackEnd);
}
}
sendAndApply(&bma);
return true;
}