1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-24 22:14:36 +02:00

callback functions and cleaning up unused code

This commit is contained in:
SoundSSGood 2022-11-06 23:41:29 +02:00
parent 363e461c3e
commit d0895e30ef
5 changed files with 28 additions and 111 deletions

View File

@ -181,6 +181,20 @@ bool CCallback::assembleArtifacts (const CGHeroInstance * hero, ArtifactPosition
return true;
}
bool CCallback::bulkMoveArtifacts(ObjectInstanceID srcHero, ObjectInstanceID dstHero)
{
BulkExchangeArtifacts bma(srcHero, dstHero, false);
sendRequest(&bma);
return true;
}
bool CCallback::bulkSwapArtifacts(ObjectInstanceID leftHero, ObjectInstanceID rightHero)
{
BulkExchangeArtifacts bma(leftHero, rightHero, true);
sendRequest(&bma);
return true;
}
bool CCallback::buildBuilding(const CGTownInstance *town, BuildingID buildingID)
{
if(town->tempOwner!=player)

View File

@ -94,6 +94,11 @@ public:
virtual int bulkSplitStack(ObjectInstanceID armyId, SlotID srcSlot, int howMany = 1) = 0;
virtual int bulkSmartSplitStack(ObjectInstanceID armyId, SlotID srcSlot) = 0;
virtual int bulkMergeStacks(ObjectInstanceID armyId, SlotID srcSlot) = 0;
// Moves all artifacts from one hero to another
virtual bool bulkMoveArtifacts(ObjectInstanceID srcHero, ObjectInstanceID dstHero) = 0;
virtual bool bulkSwapArtifacts(ObjectInstanceID leftHero, ObjectInstanceID rightHero) = 0;
};
class CBattleCallback : public IBattleCallback, public CPlayerBattleCallback
@ -151,6 +156,8 @@ public:
bool dismissHero(const CGHeroInstance * hero) override;
bool swapArtifacts(const ArtifactLocation &l1, const ArtifactLocation &l2) override;
bool assembleArtifacts(const CGHeroInstance * hero, ArtifactPosition artifactSlot, bool assemble, ArtifactID assembleTo) override;
bool bulkMoveArtifacts(ObjectInstanceID srcHero, ObjectInstanceID dstHero) override;
bool bulkSwapArtifacts(ObjectInstanceID leftHero, ObjectInstanceID rightHero) override;
bool buildBuilding(const CGTownInstance *town, BuildingID buildingID) override;
void recruitCreatures(const CGDwelling * obj, const CArmedInstance * dst, CreatureID ID, ui32 amount, si32 level=-1) override;
bool dismissCreature(const CArmedInstance *obj, SlotID stackPos) override;

View File

@ -872,41 +872,6 @@ std::function<void()> CExchangeController::onMoveArmyToRight()
return [&]() { moveArmy(true); };
}
void CExchangeController::swapArtifacts(ArtifactPosition slot)
{
bool leftHasArt = !left->isPositionFree(slot);
bool rightHasArt = !right->isPositionFree(slot);
if(!leftHasArt && !rightHasArt)
return;
ArtifactLocation leftLocation = ArtifactLocation(left, slot);
ArtifactLocation rightLocation = ArtifactLocation(right, slot);
if(leftHasArt && !left->artifactsWorn.at(slot).artifact->canBePutAt(rightLocation, true))
return;
if(rightHasArt && !right->artifactsWorn.at(slot).artifact->canBePutAt(leftLocation, true))
return;
if(leftHasArt)
{
if(rightHasArt)
{
auto art = right->getArt(slot);
cb->swapArtifacts(leftLocation, rightLocation);
cb->swapArtifacts(ArtifactLocation(right, right->getArtPos(art)), leftLocation);
}
else
cb->swapArtifacts(leftLocation, rightLocation);
}
else
{
cb->swapArtifacts(rightLocation, leftLocation);
}
}
std::vector<CArtifactInstance *> getBackpackArts(const CGHeroInstance * hero)
{
std::vector<CArtifactInstance *> result;
@ -955,56 +920,13 @@ std::vector<HeroArtifact> CExchangeController::moveCompositeArtsToBackpack()
return artPositions;
}
void CExchangeController::swapArtifacts()
{
for(int i = ArtifactPosition::HEAD; i < ArtifactPosition::AFTER_LAST; i++)
{
if(vstd::contains(unmovablePositions, i))
continue;
swapArtifacts(ArtifactPosition(i));
}
auto leftHeroBackpack = getBackpackArts(left);
auto rightHeroBackpack = getBackpackArts(right);
for(auto leftArt : leftHeroBackpack)
{
cb->swapArtifacts(
ArtifactLocation(left, left->getArtPos(leftArt)),
ArtifactLocation(right, ArtifactPosition(GameConstants::BACKPACK_START)));
}
for(auto rightArt : rightHeroBackpack)
{
cb->swapArtifacts(
ArtifactLocation(right, right->getArtPos(rightArt)),
ArtifactLocation(left, ArtifactPosition(GameConstants::BACKPACK_START)));
}
}
std::function<void()> CExchangeController::onSwapArtifacts()
{
return [&]()
{
GsThread::run([=]
{
// it is not possible directly exchange composite artifacts like Angelic Alliance and Armor of Damned
auto compositeArtLocations = moveCompositeArtsToBackpack();
swapArtifacts();
for(HeroArtifact artLocation : compositeArtLocations)
{
auto target = artLocation.hero == left ? right : left;
auto currentPos = target->getArtPos(artLocation.artifact);
cb->swapArtifacts(
ArtifactLocation(target, currentPos),
ArtifactLocation(target, artLocation.artPosition));
}
view->redraw();
cb->bulkSwapArtifacts(left->id, right->id);
});
};
}
@ -1161,19 +1083,7 @@ void CExchangeController::moveArtifacts(bool leftToRight)
GsThread::run([=]
{
while(vstd::contains_if(source->artifactsWorn, isArtRemovable))
{
auto art = std::find_if(source->artifactsWorn.begin(), source->artifactsWorn.end(), isArtRemovable);
moveArtifact(source, target, art->first);
}
while(!source->artifactsInBackpack.empty())
{
moveArtifact(source, target, source->getArtPos(source->artifactsInBackpack.begin()->artifact));
}
view->redraw();
cb->bulkMoveArtifacts(source->id, target->id);
});
}
@ -1184,24 +1094,10 @@ void CExchangeController::moveArtifact(
{
auto artifact = source->getArt(srcPosition);
auto srcLocation = ArtifactLocation(source, srcPosition);
auto dstLocation = ArtifactLocation(target,
ArtifactUtils::getArtifactDstPosition(source->getArt(srcPosition), target, target->bearerType()));
for(auto slot : artifact->artType->possibleSlots.at(target->bearerType()))
{
auto existingArtifact = target->getArt(slot);
auto existingArtInfo = target->getSlot(slot);
ArtifactLocation destLocation(target, slot);
if(!existingArtifact
&& (!existingArtInfo || !existingArtInfo->locked)
&& artifact->canBePutAt(destLocation))
{
cb->swapArtifacts(srcLocation, ArtifactLocation(target, slot));
return;
}
}
cb->swapArtifacts(srcLocation, ArtifactLocation(target, ArtifactPosition(GameConstants::BACKPACK_START)));
cb->swapArtifacts(srcLocation, dstLocation);
}
CExchangeWindow::CExchangeWindow(ObjectInstanceID hero1, ObjectInstanceID hero2, QueryID queryID)

View File

@ -324,9 +324,7 @@ private:
void moveArtifacts(bool leftToRight);
void moveArtifact(const CGHeroInstance * source, const CGHeroInstance * target, ArtifactPosition srcPosition);
void moveStack(const CGHeroInstance * source, const CGHeroInstance * target, SlotID sourceSlot);
void swapArtifacts(ArtifactPosition artPosition);
std::vector<HeroArtifact> moveCompositeArtsToBackpack();
void swapArtifacts();
};
class CExchangeWindow : public CStatusbarWindow, public CGarrisonHolder, public CWindowWithArtifacts

View File

@ -319,6 +319,7 @@ void registerTypesClientPacks2(Serializer &s)
s.template registerType<CArtifactOperationPack, MoveArtifact>();
s.template registerType<CArtifactOperationPack, AssembledArtifact>();
s.template registerType<CArtifactOperationPack, DisassembledArtifact>();
s.template registerType<CArtifactOperationPack, BulkMoveArtifacts>();
s.template registerType<CPackForClient, SaveGameClient>();
s.template registerType<CPackForClient, PlayerMessageClient>();
@ -358,6 +359,7 @@ void registerTypesServerPacks(Serializer &s)
s.template registerType<CPackForServer, BulkMergeStacks>();
s.template registerType<CPackForServer, BulkSmartSplitStack>();
s.template registerType<CPackForServer, BulkMoveArmy>();
s.template registerType<CPackForServer, BulkExchangeArtifacts>();
}
template<typename Serializer>