1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-16 10:19:47 +02:00
vcmi/client/widgets/markets/CAltarArtifacts.cpp

247 lines
7.5 KiB
C++
Raw Normal View History

2023-12-04 23:21:49 +02:00
/*
* CAltarArtifacts.cpp, part of VCMI engine
*
* Authors: listed in file AUTHORS in main folder
*
* License: GNU General Public License v2.0 or later
* Full text of license available in license.txt file, in main folder
*
*/
#include "StdInc.h"
#include "CAltarArtifacts.h"
#include "../../gui/CGuiHandler.h"
#include "../../widgets/Buttons.h"
#include "../../widgets/TextControls.h"
#include "../../CGameInfo.h"
#include "../../CPlayerInterface.h"
#include "../../../CCallback.h"
#include "../../../lib/networkPacks/ArtifactLocation.h"
#include "../../../lib/CGeneralTextHandler.h"
#include "../../../lib/mapObjects/CGHeroInstance.h"
#include "../../../lib/mapObjects/CGMarket.h"
CAltarArtifacts::CAltarArtifacts(const IMarket * market, const CGHeroInstance * hero)
2024-02-29 12:02:39 +02:00
: CTradeBase(market, hero, [this](){return CAltarArtifacts::getSelectionParams();})
2023-12-04 23:21:49 +02:00
{
OBJECT_CONSTRUCTION_CAPTURING(255 - DISPOSE);
2024-01-12 23:57:19 +02:00
assert(market);
auto altarObj = dynamic_cast<const CGArtifactsAltar*>(market);
altarId = altarObj->id;
altarArtifacts = altarObj;
2024-01-10 18:01:34 +02:00
deal = std::make_shared<CButton>(dealButtonPos, AnimationPath::builtin("ALTSACR.DEF"),
CGI->generaltexth->zelp[585], [this]() {CAltarArtifacts::makeDeal(); });
2024-02-29 12:02:39 +02:00
labels.emplace_back(std::make_shared<CLabel>(450, 32, FONT_SMALL, ETextAlignment::CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[477]));
labels.emplace_back(std::make_shared<CLabel>(302, 424, FONT_SMALL, ETextAlignment::CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[478]));
2023-12-04 23:21:49 +02:00
sacrificeAllButton = std::make_shared<CButton>(Point(393, 520), AnimationPath::builtin("ALTFILL.DEF"),
CGI->generaltexth->zelp[571], std::bind(&CExperienceAltar::sacrificeAll, this));
sacrificeAllButton->block(hero->artifactsInBackpack.empty() && hero->artifactsWorn.empty());
sacrificeBackpackButton = std::make_shared<CButton>(Point(147, 520), AnimationPath::builtin("ALTEMBK.DEF"),
CGI->generaltexth->zelp[570], std::bind(&CAltarArtifacts::sacrificeBackpack, this));
sacrificeBackpackButton->block(hero->artifactsInBackpack.empty());
2024-02-25 22:58:53 +02:00
// Hero's artifacts
2024-01-16 16:22:40 +02:00
heroArts = std::make_shared<CArtifactsOfHeroAltar>(Point(-365, -11));
heroArts->setHero(hero);
2023-12-04 23:21:49 +02:00
2024-02-29 12:02:39 +02:00
// Altar
offerTradePanel = std::make_shared<ArtifactsAltarPanel>([this](const std::shared_ptr<CTradeableItem> & altarSlot)
{
CAltarArtifacts::onSlotClickPressed(altarSlot, hRight);
});
offerTradePanel->updateSlotsCallback = std::bind(&CAltarArtifacts::updateAltarSlots, this);
offerTradePanel->moveTo(pos.topLeft() + Point(315, 52));
2023-12-04 23:21:49 +02:00
2024-02-29 12:02:39 +02:00
CTradeBase::updateSelected();
CAltarArtifacts::deselect();
2023-12-04 23:21:49 +02:00
};
TExpType CAltarArtifacts::calcExpAltarForHero()
{
TExpType expOnAltar(0);
2024-01-16 16:22:40 +02:00
for(const auto & tradeSlot : tradeSlotsMap)
2024-02-29 12:02:39 +02:00
expOnAltar += calcExpCost(tradeSlot.second->getTypeId());
2024-01-16 16:22:40 +02:00
expForHero->setText(std::to_string(expOnAltar));
return expOnAltar;
2023-12-04 23:21:49 +02:00
}
2024-02-29 12:02:39 +02:00
void CAltarArtifacts::deselect()
{
CTradeBase::deselect();
expForHero->setText(std::to_string(0));
tradeSlotsMap.clear();
// The event for removing artifacts from the altar will not be triggered. Therefore, we clean the altar immediately.
for(auto & slot : offerTradePanel->slots)
{
slot->setID(-1);
slot->subtitle->clear();
}
}
2023-12-04 23:21:49 +02:00
void CAltarArtifacts::makeDeal()
{
std::vector<TradeItemSell> positions;
2024-02-29 12:02:39 +02:00
for(const auto & [altarSlot, artInst] : tradeSlotsMap)
2023-12-04 23:21:49 +02:00
{
2024-01-16 21:54:00 +02:00
positions.push_back(artInst->getId());
2023-12-04 23:21:49 +02:00
}
LOCPLINT->cb->trade(market, EMarketMode::ARTIFACT_EXP, positions, std::vector<TradeItemBuy>(), std::vector<ui32>(), hero);
2024-02-29 12:02:39 +02:00
deselect();
2023-12-04 23:21:49 +02:00
}
void CAltarArtifacts::sacrificeAll()
{
2024-01-16 16:22:40 +02:00
LOCPLINT->cb->bulkMoveArtifacts(heroArts->getHero()->id, altarId, false, true, true);
2023-12-04 23:21:49 +02:00
}
void CAltarArtifacts::sacrificeBackpack()
{
2024-01-16 16:22:40 +02:00
LOCPLINT->cb->bulkMoveArtifacts(heroArts->getHero()->id, altarId, false, false, true);
2023-12-04 23:21:49 +02:00
}
2024-02-29 12:02:39 +02:00
void CAltarArtifacts::setSelectedArtifact(std::optional<ArtifactID> id)
2023-12-04 23:21:49 +02:00
{
2024-02-29 12:02:39 +02:00
if(id.has_value())
{
hRight = offerTradePanel->selectedSlot;
hRight->setID(id.value().num);
offerQty = calcExpCost(id.value());
}
else
{
hRight.reset();
offerQty = 0;
}
updateSelected();
2023-12-04 23:21:49 +02:00
}
2024-01-16 16:22:40 +02:00
std::shared_ptr<CArtifactsOfHeroAltar> CAltarArtifacts::getAOHset() const
2023-12-04 23:21:49 +02:00
{
2024-01-16 16:22:40 +02:00
return heroArts;
2023-12-04 23:21:49 +02:00
}
2024-01-16 16:22:40 +02:00
ObjectInstanceID CAltarArtifacts::getObjId() const
2023-12-04 23:21:49 +02:00
{
2024-01-16 16:22:40 +02:00
return altarId;
2023-12-04 23:21:49 +02:00
}
2024-02-29 12:02:39 +02:00
void CAltarArtifacts::updateAltarSlots()
2023-12-04 23:21:49 +02:00
{
2024-01-16 21:54:00 +02:00
assert(altarArtifacts->artifactsInBackpack.size() <= GameConstants::ALTAR_ARTIFACTS_SLOTS);
assert(tradeSlotsMap.size() <= GameConstants::ALTAR_ARTIFACTS_SLOTS);
2024-01-16 16:22:40 +02:00
2024-02-29 12:02:39 +02:00
auto tradeSlotsMapNewArts = tradeSlotsMap;
for(auto & altarSlot : offerTradePanel->slots)
2024-01-16 16:22:40 +02:00
if(altarSlot->id != -1)
2024-01-27 23:48:11 +02:00
{
2024-02-29 12:02:39 +02:00
if(tradeSlotsMap.find(altarSlot) == tradeSlotsMap.end())
2024-01-16 16:22:40 +02:00
{
altarSlot->setID(-1);
2024-02-27 13:39:50 +02:00
altarSlot->subtitle->clear();
2024-01-16 16:22:40 +02:00
}
else
{
2024-02-29 12:02:39 +02:00
tradeSlotsMapNewArts.erase(altarSlot);
2024-01-16 16:22:40 +02:00
}
2024-01-27 23:48:11 +02:00
}
2023-12-04 23:21:49 +02:00
2024-02-29 12:02:39 +02:00
for(auto & tradeSlot : tradeSlotsMapNewArts)
2023-12-04 23:21:49 +02:00
{
2024-02-29 12:02:39 +02:00
assert(tradeSlot.first->id == -1);
assert(altarArtifacts->getSlotByInstance(tradeSlot.second) != ArtifactPosition::PRE_FIRST);
tradeSlot.first->setID(tradeSlot.second->getTypeId());
tradeSlot.first->subtitle->setText(std::to_string(calcExpCost(tradeSlot.second->getTypeId())));
2024-01-16 16:22:40 +02:00
}
2024-02-29 12:02:39 +02:00
auto newArtsFromBulkMove = altarArtifacts->artifactsInBackpack;
for(const auto & [altarSlot, art] : tradeSlotsMap)
2024-01-16 16:22:40 +02:00
{
2024-02-29 12:02:39 +02:00
newArtsFromBulkMove.erase(std::remove_if(newArtsFromBulkMove.begin(), newArtsFromBulkMove.end(), [art = art](auto & slotInfo)
{
return slotInfo.artifact == art;
}));
}
for(const auto & slotInfo : newArtsFromBulkMove)
{
for(auto & altarSlot : offerTradePanel->slots)
if(altarSlot->id == -1)
{
altarSlot->setID(slotInfo.artifact->getTypeId());
altarSlot->subtitle->setText(std::to_string(calcExpCost(slotInfo.artifact->getTypeId())));
tradeSlotsMap.try_emplace(altarSlot, slotInfo.artifact);
break;
}
2023-12-04 23:21:49 +02:00
}
2024-02-29 12:02:39 +02:00
2024-01-16 16:22:40 +02:00
calcExpAltarForHero();
deal->block(tradeSlotsMap.empty());
}
2023-12-04 23:21:49 +02:00
2024-01-27 23:48:11 +02:00
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);
}
2024-02-29 12:02:39 +02:00
CTradeBase::SelectionParams CAltarArtifacts::getSelectionParams() const
{
if(hRight)
return std::make_tuple(
std::nullopt,
SelectionParamOneSide {std::to_string(offerQty), GameResID(hRight->id)}
);
return std::make_tuple(std::nullopt, std::nullopt);
}
2024-01-16 16:22:40 +02:00
void CAltarArtifacts::onSlotClickPressed(const std::shared_ptr<CTradeableItem> & altarSlot, std::shared_ptr<CTradeableItem> & hCurSlot)
2023-12-04 23:21:49 +02:00
{
2024-01-16 16:22:40 +02:00
assert(altarSlot);
if(const auto pickedArtInst = heroArts->getPickedArtifact())
2023-12-04 23:21:49 +02:00
{
2024-01-12 23:57:19 +02:00
if(pickedArtInst->canBePutAt(altarArtifacts))
2024-01-27 23:48:11 +02:00
{
2024-01-16 16:22:40 +02:00
if(pickedArtInst->artType->isTradable())
{
if(altarSlot->id == -1)
2024-02-29 12:02:39 +02:00
tradeSlotsMap.try_emplace(altarSlot, pickedArtInst);
2024-01-16 16:22:40 +02:00
deal->block(false);
LOCPLINT->cb->swapArtifacts(ArtifactLocation(heroArts->getHero()->id, ArtifactPosition::TRANSITION_POS),
ArtifactLocation(altarId, ArtifactPosition::ALTAR));
}
else
{
logGlobal->warn("Cannot put special artifact on altar!");
return;
}
2024-01-27 23:48:11 +02:00
}
2023-12-04 23:21:49 +02:00
}
2024-02-29 12:02:39 +02:00
else if(altarSlot->id != -1)
2023-12-04 23:21:49 +02:00
{
2024-02-29 12:02:39 +02:00
assert(tradeSlotsMap.at(altarSlot));
const auto slot = altarArtifacts->getSlotByInstance(tradeSlotsMap.at(altarSlot));
2023-12-04 23:21:49 +02:00
assert(slot != ArtifactPosition::PRE_FIRST);
2024-01-16 16:22:40 +02:00
LOCPLINT->cb->swapArtifacts(ArtifactLocation(altarId, slot), ArtifactLocation(hero->id, ArtifactPosition::TRANSITION_POS));
2024-02-29 12:02:39 +02:00
tradeSlotsMap.erase(altarSlot);
2023-12-04 23:21:49 +02:00
}
2024-01-16 16:22:40 +02:00
}
2024-02-29 12:02:39 +02:00
TExpType CAltarArtifacts::calcExpCost(ArtifactID id)
2024-01-16 16:22:40 +02:00
{
2024-02-19 23:40:43 +02:00
int bidQty = 0;
2024-01-16 16:22:40 +02:00
int expOfArt = 0;
2024-02-29 12:02:39 +02:00
market->getOffer(id, 0, bidQty, expOfArt, EMarketMode::ARTIFACT_EXP);
2024-01-16 16:22:40 +02:00
return hero->calculateXp(expOfArt);
2023-12-04 23:21:49 +02:00
}