1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-11-24 08:32:34 +02:00

cleanup & fix build & sonarcloud fixes

This commit is contained in:
SoundSSGood 2024-01-27 23:48:11 +02:00
parent c6ca6ad835
commit d16632b54c
16 changed files with 78 additions and 136 deletions

View File

@ -38,20 +38,3 @@ CArtifactsOfHeroAltar::~CArtifactsOfHeroAltar()
{
putBackPickedArtifact();
}
void CArtifactsOfHeroAltar::scrollBackpack(int offset)
{
CArtifactsOfHeroBase::scrollBackpackForArtSet(offset, *curHero);
redraw();
}
void CArtifactsOfHeroAltar::pickUpArtifact(CArtPlace & artPlace)
{
LOCPLINT->cb->swapArtifacts(ArtifactLocation(curHero->id, artPlace.slot),
ArtifactLocation(curHero->id, ArtifactPosition::TRANSITION_POS));
}
void CArtifactsOfHeroAltar::swapArtifacts(const ArtifactLocation & srcLoc, const ArtifactLocation & dstLoc)
{
LOCPLINT->cb->swapArtifacts(srcLoc, dstLoc);
}

View File

@ -16,11 +16,6 @@
class CArtifactsOfHeroAltar : public CArtifactsOfHeroBase
{
public:
std::set<const CArtifactInstance*> artifactsOnAltar;
CArtifactsOfHeroAltar(const Point & position);
~CArtifactsOfHeroAltar();
void scrollBackpack(int offset) override;
void pickUpArtifact(CArtPlace & artPlace);
void swapArtifacts(const ArtifactLocation & srcLoc, const ArtifactLocation & dstLoc);
};

View File

@ -41,17 +41,6 @@ CArtifactsOfHeroBackpack::CArtifactsOfHeroBackpack()
initAOHbackpack(visibleCapacityMax, backpackCap < 0 || visibleCapacityMax < backpackCap);
}
void CArtifactsOfHeroBackpack::swapArtifacts(const ArtifactLocation & srcLoc, const ArtifactLocation & dstLoc)
{
LOCPLINT->cb->swapArtifacts(srcLoc, dstLoc);
}
void CArtifactsOfHeroBackpack::pickUpArtifact(CArtPlace & artPlace)
{
LOCPLINT->cb->swapArtifacts(ArtifactLocation(curHero->id, artPlace.slot),
ArtifactLocation(curHero->id, ArtifactPosition::TRANSITION_POS));
}
void CArtifactsOfHeroBackpack::scrollBackpack(int offset)
{
if(backpackListBox)
@ -60,7 +49,7 @@ void CArtifactsOfHeroBackpack::scrollBackpack(int offset)
auto slot = ArtifactPosition::BACKPACK_START + backpackPos;
for(auto artPlace : backpack)
{
setSlotData(artPlace, slot, *curHero);
setSlotData(artPlace, slot);
slot = slot + 1;
}
redraw();
@ -188,9 +177,9 @@ void CArtifactsOfHeroQuickBackpack::setHero(const CGHeroInstance * hero)
initAOHbackpack(requiredSlots, false);
auto artPlace = backpack.begin();
for(auto & art : filteredArts)
setSlotData(*artPlace++, curHero->getSlotByInstance(art.second), *curHero);
setSlotData(*artPlace++, curHero->getSlotByInstance(art.second));
for(auto & art : filteredScrolls)
setSlotData(*artPlace++, curHero->getSlotByInstance(art.second), *curHero);
setSlotData(*artPlace++, curHero->getSlotByInstance(art.second));
}
}
@ -215,5 +204,5 @@ void CArtifactsOfHeroQuickBackpack::swapSelected()
break;
}
if(backpackLoc.slot != ArtifactPosition::PRE_FIRST && filterBySlot != ArtifactPosition::PRE_FIRST && curHero)
swapArtifacts(backpackLoc, ArtifactLocation(curHero->id, filterBySlot));
LOCPLINT->cb->swapArtifacts(backpackLoc, ArtifactLocation(curHero->id, filterBySlot));
}

View File

@ -24,8 +24,6 @@ class CArtifactsOfHeroBackpack : public CArtifactsOfHeroBase
public:
CArtifactsOfHeroBackpack(size_t slotsColumnsMax, size_t slotsRowsMax);
CArtifactsOfHeroBackpack();
void swapArtifacts(const ArtifactLocation & srcLoc, const ArtifactLocation & dstLoc);
void pickUpArtifact(CArtPlace & artPlace);
void scrollBackpack(int offset) override;
void updateBackpackSlots() override;
size_t getActiveSlotRowsNum();

View File

@ -123,7 +123,7 @@ void CArtifactsOfHeroBase::setHero(const CGHeroInstance * hero)
for(auto slot : artWorn)
{
setSlotData(slot.second, slot.first, *curHero);
setSlotData(slot.second, slot.first);
}
scrollBackpack(0);
}
@ -134,16 +134,10 @@ const CGHeroInstance * CArtifactsOfHeroBase::getHero() const
}
void CArtifactsOfHeroBase::scrollBackpack(int offset)
{
scrollBackpackForArtSet(offset, *curHero);
redraw();
}
void CArtifactsOfHeroBase::scrollBackpackForArtSet(int offset, const CArtifactSet & artSet)
{
// offset==-1 => to left; offset==1 => to right
using slotInc = std::function<ArtifactPosition(ArtifactPosition&)>;
auto artsInBackpack = static_cast<int>(artSet.artifactsInBackpack.size());
auto artsInBackpack = static_cast<int>(curHero->artifactsInBackpack.size());
auto scrollingPossible = artsInBackpack > backpack.size();
slotInc inc_straight = [](ArtifactPosition & slot) -> ArtifactPosition
@ -170,7 +164,7 @@ void CArtifactsOfHeroBase::scrollBackpackForArtSet(int offset, const CArtifactSe
auto slot = ArtifactPosition(ArtifactPosition::BACKPACK_START + backpackPos);
for(auto artPlace : backpack)
{
setSlotData(artPlace, slot, artSet);
setSlotData(artPlace, slot);
slot = inc(slot);
}
@ -179,6 +173,8 @@ void CArtifactsOfHeroBase::scrollBackpackForArtSet(int offset, const CArtifactSe
leftBackpackRoll->block(!scrollingPossible);
if(rightBackpackRoll)
rightBackpackRoll->block(!scrollingPossible);
redraw();
}
void CArtifactsOfHeroBase::markPossibleSlots(const CArtifactInstance * art, bool assumeDestRemoved)
@ -235,7 +231,7 @@ void CArtifactsOfHeroBase::updateBackpackSlots()
void CArtifactsOfHeroBase::updateSlot(const ArtifactPosition & slot)
{
setSlotData(getArtPlace(slot), slot, *curHero);
setSlotData(getArtPlace(slot), slot);
}
const CArtifactInstance * CArtifactsOfHeroBase::getPickedArtifact()
@ -256,7 +252,7 @@ void CArtifactsOfHeroBase::addGestureCallback(CArtPlace::ClickFunctor callback)
}
}
void CArtifactsOfHeroBase::setSlotData(ArtPlacePtr artPlace, const ArtifactPosition & slot, const CArtifactSet & artSet)
void CArtifactsOfHeroBase::setSlotData(ArtPlacePtr artPlace, const ArtifactPosition & slot)
{
// Spurious call from artifactMoved in attempt to update hidden backpack slot
if(!artPlace && ArtifactUtils::isSlotBackpack(slot))
@ -265,7 +261,7 @@ void CArtifactsOfHeroBase::setSlotData(ArtPlacePtr artPlace, const ArtifactPosit
}
artPlace->slot = slot;
if(auto slotInfo = artSet.getSlot(slot))
if(auto slotInfo = curHero->getSlot(slot))
{
artPlace->lockSlot(slotInfo->locked);
artPlace->setArtifact(slotInfo->artifact);
@ -278,7 +274,7 @@ void CArtifactsOfHeroBase::setSlotData(ArtPlacePtr artPlace, const ArtifactPosit
arts.insert(std::pair(combinedArt, 0));
for(const auto part : combinedArt->getConstituents())
{
if(artSet.hasArt(part->getId(), false))
if(curHero->hasArt(part->getId(), false))
arts.at(combinedArt)++;
}
}

View File

@ -69,6 +69,5 @@ protected:
virtual void init(CHeroArtPlace::ClickFunctor lClickCallback, CHeroArtPlace::ClickFunctor showPopupCallback,
const Point & position, BpackScrollFunctor scrollCallback);
// Assigns an artifacts to an artifact place depending on it's new slot ID
virtual void setSlotData(ArtPlacePtr artPlace, const ArtifactPosition & slot, const CArtifactSet & artSet);
virtual void scrollBackpackForArtSet(int offset, const CArtifactSet & artSet);
virtual void setSlotData(ArtPlacePtr artPlace, const ArtifactPosition & slot);
};

View File

@ -50,15 +50,3 @@ CArtifactsOfHeroKingdom::~CArtifactsOfHeroKingdom()
{
putBackPickedArtifact();
}
void CArtifactsOfHeroKingdom::swapArtifacts(const ArtifactLocation & srcLoc, const ArtifactLocation & dstLoc)
{
LOCPLINT->cb->swapArtifacts(srcLoc, dstLoc);
}
void CArtifactsOfHeroKingdom::pickUpArtifact(CArtPlace & artPlace)
{
LOCPLINT->cb->swapArtifacts(ArtifactLocation(curHero->id, artPlace.slot),
ArtifactLocation(curHero->id, ArtifactPosition::TRANSITION_POS));
}

View File

@ -20,9 +20,8 @@ VCMI_LIB_NAMESPACE_END
class CArtifactsOfHeroKingdom : public CArtifactsOfHeroBase
{
public:
CArtifactsOfHeroKingdom() = delete;
CArtifactsOfHeroKingdom(ArtPlaceMap ArtWorn, std::vector<ArtPlacePtr> Backpack,
std::shared_ptr<CButton> leftScroll, std::shared_ptr<CButton> rightScroll);
~CArtifactsOfHeroKingdom();
void swapArtifacts(const ArtifactLocation & srcLoc, const ArtifactLocation & dstLoc);
void pickUpArtifact(CArtPlace & artPlace);
};

View File

@ -30,14 +30,3 @@ CArtifactsOfHeroMain::~CArtifactsOfHeroMain()
{
putBackPickedArtifact();
}
void CArtifactsOfHeroMain::swapArtifacts(const ArtifactLocation & srcLoc, const ArtifactLocation & dstLoc)
{
LOCPLINT->cb->swapArtifacts(srcLoc, dstLoc);
}
void CArtifactsOfHeroMain::pickUpArtifact(CArtPlace & artPlace)
{
LOCPLINT->cb->swapArtifacts(ArtifactLocation(curHero->id, artPlace.slot),
ArtifactLocation(curHero->id, ArtifactPosition::TRANSITION_POS));
}

View File

@ -22,6 +22,4 @@ class CArtifactsOfHeroMain : public CArtifactsOfHeroBase
public:
CArtifactsOfHeroMain(const Point & position);
~CArtifactsOfHeroMain();
void swapArtifacts(const ArtifactLocation & srcLoc, const ArtifactLocation & dstLoc);
void pickUpArtifact(CArtPlace & artPlace);
};

View File

@ -28,12 +28,12 @@ CArtifactsOfHeroMarket::CArtifactsOfHeroMarket(const Point & position)
void CArtifactsOfHeroMarket::scrollBackpack(int offset)
{
CArtifactsOfHeroBase::scrollBackpackForArtSet(offset, *curHero);
CArtifactsOfHeroBase::scrollBackpack(offset);
// We may have highlight on one of backpack artifacts
if(selectArtCallback)
{
for(auto & artPlace : backpack)
for(const auto & artPlace : backpack)
{
if(artPlace->isSelected())
{
@ -42,5 +42,4 @@ void CArtifactsOfHeroMarket::scrollBackpack(int offset)
}
}
}
redraw();
}

View File

@ -33,6 +33,8 @@
#include "../../lib/networkPacks/ArtifactLocation.h"
#include "../../lib/CConfigHandler.h"
#include "../../CCallback.h"
void CWindowWithArtifacts::addSet(CArtifactsOfHeroPtr artSet)
{
artSets.emplace_back(artSet);
@ -81,40 +83,14 @@ const CArtifactInstance * CWindowWithArtifacts::getPickedArtifact()
void CWindowWithArtifacts::clickPressedArtPlaceHero(CArtifactsOfHeroBase & artsInst, CArtPlace & artPlace, const Point & cursorPosition)
{
const auto artSetWeak = findAOHbyRef(artsInst);
assert(artSetWeak.has_value());
const auto artSet = findAOHbyRef(artsInst);
assert(artSet.has_value());
if(artPlace.isLocked())
return;
const auto checkSpecialArts = [](const CGHeroInstance * hero, CArtPlace & artPlace, bool isTrade) -> bool
{
if(artPlace.getArt()->getTypeId() == ArtifactID::SPELLBOOK)
{
GH.windows().createAndPushWindow<CSpellWindow>(hero, LOCPLINT, LOCPLINT->battleInt.get());
return false;
}
if(artPlace.getArt()->getTypeId() == ArtifactID::CATAPULT)
{
// The Catapult must be equipped
LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[312],
std::vector<std::shared_ptr<CComponent>>(1, std::make_shared<CComponent>(ComponentType::ARTIFACT, ArtifactID(ArtifactID::CATAPULT))));
return false;
}
if(isTrade)
{
if(!artPlace.getArt()->artType->isTradable())
{
LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[21],
std::vector<std::shared_ptr<CComponent>>(1, std::make_shared<CComponent>(ComponentType::ARTIFACT, artPlace.getArt()->getTypeId())));
return false;
}
}
return true;
};
std::visit(
[checkSpecialArts, this, &artPlace](auto artSetWeak) -> void
[this, &artPlace](auto artSetWeak) -> void
{
const auto artSetPtr = artSetWeak.lock();
@ -162,14 +138,14 @@ void CWindowWithArtifacts::clickPressedArtPlaceHero(CArtifactsOfHeroBase & artsI
isTransferAllowed = false;
}
if(isTransferAllowed)
artSetPtr->swapArtifacts(srcLoc, dstLoc);
LOCPLINT->cb->swapArtifacts(srcLoc, dstLoc);
}
else if(auto art = artPlace.getArt())
{
if(artSetPtr->getHero()->tempOwner == LOCPLINT->playerID)
{
if(checkSpecialArts(hero, artPlace, std::is_same_v<decltype(artSetWeak), std::weak_ptr<CArtifactsOfHeroAltar>> ? true : false))
artSetPtr->pickUpArtifact(artPlace);
if(checkSpecialArts(*art, hero, std::is_same_v<decltype(artSetWeak), std::weak_ptr<CArtifactsOfHeroAltar>> ? true : false))
LOCPLINT->cb->swapArtifacts(ArtifactLocation(artSetPtr->getHero()->id, artPlace.slot), ArtifactLocation(artSetPtr->getHero()->id, ArtifactPosition::TRANSITION_POS));
}
else
{
@ -217,12 +193,11 @@ void CWindowWithArtifacts::clickPressedArtPlaceHero(CArtifactsOfHeroBase & artsI
else if constexpr(std::is_same_v<decltype(artSetWeak), std::weak_ptr<CArtifactsOfHeroQuickBackpack>>)
{
const auto hero = artSetPtr->getHero();
artSetPtr->swapArtifacts(ArtifactLocation(hero->id, artPlace.slot),
ArtifactLocation(hero->id, artSetPtr->getFilterSlot()));
LOCPLINT->cb->swapArtifacts(ArtifactLocation(hero->id, artPlace.slot), ArtifactLocation(hero->id, artSetPtr->getFilterSlot()));
if(closeCallback)
closeCallback();
}
}, artSetWeak.value());
}, artSet.value());
}
void CWindowWithArtifacts::showPopupArtPlaceHero(CArtifactsOfHeroBase & artsInst, CArtPlace & artPlace, const Point & cursorPosition)
@ -475,3 +450,31 @@ void CWindowWithArtifacts::markPossibleSlots()
std::visit(artifactAssembledBody, artSetWeak);
}
}
bool CWindowWithArtifacts::checkSpecialArts(const CArtifactInstance & artInst, const CGHeroInstance * hero, bool isTrade)
{
const auto artId = artInst.getTypeId();
if(artId == ArtifactID::SPELLBOOK)
{
GH.windows().createAndPushWindow<CSpellWindow>(hero, LOCPLINT, LOCPLINT->battleInt.get());
return false;
}
if(artId == ArtifactID::CATAPULT)
{
// The Catapult must be equipped
LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[312],
std::vector<std::shared_ptr<CComponent>>(1, std::make_shared<CComponent>(ComponentType::ARTIFACT, ArtifactID(ArtifactID::CATAPULT))));
return false;
}
if(isTrade)
{
if(!artInst.artType->isTradable())
{
LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[21],
std::vector<std::shared_ptr<CComponent>>(1, std::make_shared<CComponent>(ComponentType::ARTIFACT, artId)));
return false;
}
}
return true;
}

View File

@ -50,4 +50,5 @@ protected:
std::optional<std::tuple<const CGHeroInstance*, const CArtifactInstance*>> getState();
std::optional<CArtifactsOfHeroPtr> findAOHbyRef(CArtifactsOfHeroBase & artsInst);
void markPossibleSlots();
bool checkSpecialArts(const CArtifactInstance & artInst, const CGHeroInstance * hero, bool isTrade);
};

View File

@ -67,13 +67,6 @@ CAltarArtifacts::CAltarArtifacts(const IMarket * market, const CGHeroInstance *
CTradeBase::deselect();
};
CAltarArtifacts::~CAltarArtifacts()
{
// TODO: If the backpack capacity limit is enabled, artifacts may remain on the altar.
// Perhaps should be erased in CGameHandler::objectVisitEnded if id of visited object will be available
LOCPLINT->cb->bulkMoveArtifacts(altarId, heroArts->getHero()->id, false, true, true);
}
TExpType CAltarArtifacts::calcExpAltarForHero()
{
TExpType expOnAltar(0);
@ -136,8 +129,8 @@ void CAltarArtifacts::updateSlots()
auto slotsToAdd = tradeSlotsMap;
for(auto & altarSlot : items[0])
{
if(altarSlot->id != -1)
{
if(tradeSlotsMap.find(altarSlot->getArtInstance()) == tradeSlotsMap.end())
{
altarSlot->setID(-1);
@ -147,7 +140,7 @@ void CAltarArtifacts::updateSlots()
{
slotsToAdd.erase(altarSlot->getArtInstance());
}
}
}
for(auto & tradeSlot : slotsToAdd)
{
@ -165,7 +158,7 @@ void CAltarArtifacts::updateSlots()
{
altarSlot->setArtInstance(slotInfo.artifact);
altarSlot->subtitle = std::to_string(calcExpCost(slotInfo.artifact));
tradeSlotsMap.emplace(slotInfo.artifact, altarSlot);
tradeSlotsMap.try_emplace(slotInfo.artifact, altarSlot);
break;
}
}
@ -174,6 +167,14 @@ void CAltarArtifacts::updateSlots()
deal->block(tradeSlotsMap.empty());
}
void CAltarArtifacts::putBackArtifacts()
{
// TODO: If the backpack capacity limit is enabled, artifacts may remain on the altar.
// Perhaps should be erased in CGameHandler::objectVisitEnded if id of visited object will be available
if(!altarArtifacts->artifactsInBackpack.empty())
LOCPLINT->cb->bulkMoveArtifacts(altarId, heroArts->getHero()->id, false, true, true);
}
void CAltarArtifacts::onSlotClickPressed(const std::shared_ptr<CTradeableItem> & altarSlot, std::shared_ptr<CTradeableItem> & hCurSlot)
{
assert(altarSlot);
@ -181,11 +182,11 @@ void CAltarArtifacts::onSlotClickPressed(const std::shared_ptr<CTradeableItem> &
if(const auto pickedArtInst = heroArts->getPickedArtifact())
{
if(pickedArtInst->canBePutAt(altarArtifacts))
{
if(pickedArtInst->artType->isTradable())
{
if(altarSlot->id == -1)
tradeSlotsMap.emplace(pickedArtInst, altarSlot);
heroArts->artifactsOnAltar.insert(pickedArtInst);
tradeSlotsMap.try_emplace(pickedArtInst, altarSlot);
deal->block(false);
LOCPLINT->cb->swapArtifacts(ArtifactLocation(heroArts->getHero()->id, ArtifactPosition::TRANSITION_POS),
@ -196,13 +197,13 @@ void CAltarArtifacts::onSlotClickPressed(const std::shared_ptr<CTradeableItem> &
logGlobal->warn("Cannot put special artifact on altar!");
return;
}
}
}
else if(const CArtifactInstance * art = altarSlot->getArtInstance())
{
const auto slot = altarArtifacts->getSlotByInstance(art);
assert(slot != ArtifactPosition::PRE_FIRST);
LOCPLINT->cb->swapArtifacts(ArtifactLocation(altarId, slot), ArtifactLocation(hero->id, ArtifactPosition::TRANSITION_POS));
heroArts->artifactsOnAltar.erase(art);
tradeSlotsMap.erase(art);
}
}

View File

@ -16,7 +16,6 @@ class CAltarArtifacts : public CExperienceAltar
{
public:
CAltarArtifacts(const IMarket * market, const CGHeroInstance * hero);
~CAltarArtifacts();
TExpType calcExpAltarForHero() override;
void makeDeal() override;
void sacrificeAll() override;
@ -25,6 +24,7 @@ public:
std::shared_ptr<CArtifactsOfHeroAltar> getAOHset() const;
ObjectInstanceID getObjId() const;
void updateSlots();
void putBackArtifacts();
private:
ObjectInstanceID altarId;

View File

@ -79,14 +79,18 @@ void CAltarWindow::createAltarArtifacts(const IMarket * market, const CGHeroInst
auto altarArtifacts = std::make_shared<CAltarArtifacts>(market, hero);
altar = altarArtifacts;
artSets.clear();
addSetAndCallbacks(altarArtifacts->getAOHset());
addSetAndCallbacks(altarArtifacts->getAOHset()); altarArtifacts->putBackArtifacts();
changeModeButton = std::make_shared<CButton>(Point(516, 421), AnimationPath::builtin("ALTSACC.DEF"),
CGI->generaltexth->zelp[572], std::bind(&CAltarWindow::createAltarCreatures, this, market, hero));
if(altar->hero->getAlignment() == EAlignment::GOOD)
changeModeButton->block(true);
quitButton = std::make_shared<CButton>(Point(516, 520), AnimationPath::builtin("IOK6432.DEF"),
CGI->generaltexth->zelp[568], std::bind(&CAltarWindow::close, this), EShortcut::GLOBAL_RETURN);
CGI->generaltexth->zelp[568], [this, altarArtifacts]()
{
altarArtifacts->putBackArtifacts();
CAltarWindow::close();
}, EShortcut::GLOBAL_RETURN);
altar->setRedrawParent(true);
redraw();
}