1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-01-12 02:28:11 +02:00

Unified CGameHandler::bulkMoveArtifacts and CGameHandler::bulkSwapArtifacts to one

This commit is contained in:
SoundSSGood 2022-11-10 19:09:37 +02:00
parent ad47a7573c
commit 30db38c0fe
3 changed files with 67 additions and 83 deletions

View File

@ -3917,7 +3917,7 @@ bool CGameHandler::moveArtifact(const ArtifactLocation &al1, const ArtifactLocat
(si32)dst.getHolderArtSet()->artifactsInBackpack.size() + GameConstants::BACKPACK_START)));
}
auto hero = boost::get<ConstTransitivePtr<CGHeroInstance>>(dst.artHolder);
if (ArtifactUtils::checkSpellbookIsNeeded(hero, srcArtifact->artType->id, dst.slot))
if(ArtifactUtils::checkSpellbookIsNeeded(hero, srcArtifact->artType->id, dst.slot))
giveHeroNewArtifact(hero, VLC->arth->objects[ArtifactID::SPELLBOOK], ArtifactPosition::SPELLBOOK);
MoveArtifact ma(&src, &dst);
@ -3925,98 +3925,86 @@ bool CGameHandler::moveArtifact(const ArtifactLocation &al1, const ArtifactLocat
return true;
}
bool CGameHandler::bulkMoveArtifacts(ObjectInstanceID srcHero, ObjectInstanceID dstHero)
bool CGameHandler::bulkMoveArtifacts(ObjectInstanceID srcHero, ObjectInstanceID dstHero, bool swap)
{
// Make sure exchange is even possible between the two heroes.
if (!isAllowedExchange(srcHero, dstHero))
if(!isAllowedExchange(srcHero, dstHero))
COMPLAIN_RET("That heroes cannot make any exchange!");
auto psrcHero = getHero(srcHero);
auto pdstHero = getHero(dstHero);
BulkMoveArtifacts ma(static_cast<ConstTransitivePtr<CGHeroInstance>>(psrcHero),
static_cast<ConstTransitivePtr<CGHeroInstance>>(pdstHero), false);
auto slots = &ma.artsPack0;
// Temporary fitting set for artifacts. Used to select available slots before sending data.
CArtifactFittingSet ArtFittingSet(pdstHero->bearerType());
ArtFittingSet.artifactsInBackpack = pdstHero->artifactsInBackpack;
ArtFittingSet.artifactsWorn = pdstHero->artifactsWorn;
BulkMoveArtifacts ma(static_cast<ConstTransitivePtr<CGHeroInstance>>(psrcHero),
static_cast<ConstTransitivePtr<CGHeroInstance>>(pdstHero), swap);
auto & slotsSrcDst = ma.artsPack0;
auto & slotsDstSrc = ma.artsPack1;
// Move over artifacts that are worn
for(auto & artInfo : psrcHero->artifactsWorn)
if(swap)
{
if(ArtifactUtils::isArtRemovable(artInfo))
auto moveArtsWorn = [this](const CGHeroInstance * srcHero, const CGHeroInstance * dstHero,
std::vector<BulkMoveArtifacts::LinkedSlots> & slots) -> void
{
for(auto & artifact : srcHero->artifactsWorn)
{
if(artifact.second.locked)
continue;
if(!ArtifactUtils::isArtRemovable(artifact))
continue;
slots.push_back(BulkMoveArtifacts::LinkedSlots(artifact.first, artifact.first));
if(ArtifactUtils::checkSpellbookIsNeeded(dstHero, artifact.second.getArt()->artType->id, artifact.first))
giveHeroNewArtifact(dstHero, VLC->arth->objects[ArtifactID::SPELLBOOK], ArtifactPosition::SPELLBOOK);
}
};
// Move over artifacts that are worn srcHero -> dstHero
moveArtsWorn(psrcHero, pdstHero, slotsSrcDst);
// Move over artifacts that are worn dstHero -> srcHero
moveArtsWorn(pdstHero, psrcHero, slotsDstSrc);
// Move over artifacts that are in backpack srcHero -> dstHero
for(auto & slotInfo : psrcHero->artifactsInBackpack)
{
auto slot = psrcHero->getArtPos(slotInfo.artifact);
slotsSrcDst.push_back(BulkMoveArtifacts::LinkedSlots(slot, slot));
}
// Move over artifacts that are in backpack dstHero -> srcHero
for(auto & slotInfo : pdstHero->artifactsInBackpack)
{
auto slot = pdstHero->getArtPos(slotInfo.artifact);
slotsDstSrc.push_back(BulkMoveArtifacts::LinkedSlots(slot, slot));
}
}
else
{
auto & slots = ma.artsPack0;
// Temporary fitting set for artifacts. Used to select available slots before sending data.
CArtifactFittingSet ArtFittingSet(pdstHero->bearerType());
ArtFittingSet.artifactsInBackpack = pdstHero->artifactsInBackpack;
ArtFittingSet.artifactsWorn = pdstHero->artifactsWorn;
auto moveArtifact = [this, &ArtFittingSet, &slots](const CArtifactInstance * artifact,
ArtifactPosition srcSlot, const CGHeroInstance * pdstHero) -> void
{
auto artifact = psrcHero->getArt(artInfo.first);
auto dstSlot = ArtifactUtils::getArtifactDstPosition(artifact, &ArtFittingSet, pdstHero->bearerType());
ArtFittingSet.putArtifact(dstSlot,
static_cast<ConstTransitivePtr<CArtifactInstance>>(psrcHero->getArt(artInfo.first)));
slots->push_back(BulkMoveArtifacts::LinkedSlots(artInfo.first, dstSlot));
ArtFittingSet.putArtifact(dstSlot, static_cast<ConstTransitivePtr<CArtifactInstance>>(artifact));
slots.push_back(BulkMoveArtifacts::LinkedSlots(srcSlot, dstSlot));
if(ArtifactUtils::checkSpellbookIsNeeded(pdstHero, artifact->artType->id, dstSlot))
giveHeroNewArtifact(pdstHero, VLC->arth->objects[ArtifactID::SPELLBOOK], ArtifactPosition::SPELLBOOK);
}
}
// Move over artifacts that are in backpack
for(auto & slotInfo : psrcHero->artifactsInBackpack)
{
auto artifact = psrcHero->getArt(psrcHero->getArtPos(slotInfo.artifact));
auto dstSlot = ArtifactUtils::getArtifactDstPosition(artifact, &ArtFittingSet, pdstHero->bearerType());
ArtFittingSet.putArtifact(dstSlot, static_cast<ConstTransitivePtr<CArtifactInstance>>(slotInfo.artifact));
slots->push_back(BulkMoveArtifacts::LinkedSlots(psrcHero->getArtPos(slotInfo.artifact), dstSlot));
if(ArtifactUtils::checkSpellbookIsNeeded(pdstHero, artifact->artType->id, dstSlot))
giveHeroNewArtifact(pdstHero, VLC->arth->objects[ArtifactID::SPELLBOOK], ArtifactPosition::SPELLBOOK);
}
sendAndApply(&ma);
return true;
}
};
bool CGameHandler::bulkSwapArtifacts(ObjectInstanceID leftHero, ObjectInstanceID rightHero)
{
// Make sure exchange is even possible between the two heroes.
if(!isAllowedExchange(leftHero, rightHero))
COMPLAIN_RET("That heroes cannot make any exchange!");
auto pleftHero = getHero(leftHero);
auto prightHero = getHero(rightHero);
BulkMoveArtifacts ma(static_cast<ConstTransitivePtr<CGHeroInstance>>(pleftHero),
static_cast<ConstTransitivePtr<CGHeroInstance>>(prightHero), true);
ma.srcArtHolder = static_cast<ConstTransitivePtr<CGHeroInstance>>(prightHero);
ma.dstArtHolder = static_cast<ConstTransitivePtr<CGHeroInstance>>(pleftHero);
auto & slotsLeftRight = ma.artsPack0;
auto & slotsRightLeft = ma.artsPack1;
auto moveArtsWorn = [this](const CGHeroInstance * srcHero, const CGHeroInstance * dstHero,
std::vector<BulkMoveArtifacts::LinkedSlots> & slots) -> void
{
for(auto & artifact : srcHero->artifactsWorn)
// Move over artifacts that are worn
for(auto & artInfo : psrcHero->artifactsWorn)
{
if(artifact.second.locked)
continue;
if(!ArtifactUtils::isArtRemovable(artifact))
continue;
slots.push_back(BulkMoveArtifacts::LinkedSlots(artifact.first, artifact.first));
if(ArtifactUtils::checkSpellbookIsNeeded(dstHero, artifact.second.getArt()->artType->id, artifact.first))
giveHeroNewArtifact(dstHero, VLC->arth->objects[ArtifactID::SPELLBOOK], ArtifactPosition::SPELLBOOK);
if(ArtifactUtils::isArtRemovable(artInfo))
{
moveArtifact(psrcHero->getArt(artInfo.first), artInfo.first, pdstHero);
}
}
// Move over artifacts that are in backpack
for(auto & slotInfo : psrcHero->artifactsInBackpack)
{
moveArtifact(psrcHero->getArt(psrcHero->getArtPos(slotInfo.artifact)), psrcHero->getArtPos(slotInfo.artifact), pdstHero);
}
};
// Move over artifacts that are worn leftHero -> rightHero
moveArtsWorn(pleftHero, prightHero, slotsLeftRight);
// Move over artifacts that are worn rightHero -> leftHero
moveArtsWorn(prightHero, pleftHero, slotsRightLeft);
// Move over artifacts that are in backpack leftHero -> rightHero
for(auto & slotInfo : pleftHero->artifactsInBackpack)
{
auto slot = pleftHero->getArtPos(slotInfo.artifact);
slotsLeftRight.push_back(BulkMoveArtifacts::LinkedSlots(slot, slot));
}
// Move over artifacts that are in backpack rightHero -> leftHero
for(auto & slotInfo : prightHero->artifactsInBackpack)
{
auto slot = prightHero->getArtPos(slotInfo.artifact);
slotsRightLeft.push_back(BulkMoveArtifacts::LinkedSlots(slot, slot));
}
sendAndApply(&ma);
return true;

View File

@ -181,8 +181,7 @@ public:
void putArtifact(const ArtifactLocation &al, const CArtifactInstance *a) override;
void removeArtifact(const ArtifactLocation &al) override;
bool moveArtifact(const ArtifactLocation & al1, const ArtifactLocation & al2) override;
bool bulkMoveArtifacts(ObjectInstanceID srcHero, ObjectInstanceID dstHero);
bool bulkSwapArtifacts(ObjectInstanceID leftHero, ObjectInstanceID rightHero);
bool bulkMoveArtifacts(ObjectInstanceID srcHero, ObjectInstanceID dstHero, bool swap);
void synchronizeArtifactHandlerLists();
void showCompInfo(ShowInInfobox * comp) override;

View File

@ -184,10 +184,7 @@ bool BulkExchangeArtifacts::applyGh(CGameHandler * gh)
{
const CGHeroInstance * pSrcHero = gh->getHero(srcHero);
throwOnWrongPlayer(gh, pSrcHero->getOwner());
if(swap)
return gh->bulkSwapArtifacts(srcHero, dstHero);
else
return gh->bulkMoveArtifacts(srcHero, dstHero);
return gh->bulkMoveArtifacts(srcHero, dstHero, swap);
}
bool AssembleArtifacts::applyGh(CGameHandler * gh)