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

219 lines
7.0 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)
: CTradeBase(market, hero)
{
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(); });
2023-12-04 23:21:49 +02:00
labels.emplace_back(std::make_shared<CLabel>(450, 34, FONT_SMALL, ETextAlignment::CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[477]));
labels.emplace_back(std::make_shared<CLabel>(302, 423, FONT_SMALL, ETextAlignment::CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[478]));
2024-02-19 23:40:43 +02:00
selectedSubtitle = std::make_shared<CLabel>(302, 501, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE);
2023-12-04 23:21:49 +02:00
selectedArt = std::make_shared<CArtPlace>(Point(280, 442));
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
int slotNum = 0;
for(auto & altarSlotPos : posSlotsAltar)
{
2024-02-27 13:39:50 +02:00
auto altarSlot = std::make_shared<CTradeableItem>(Rect(altarSlotPos, Point(44, 44)), EType::ARTIFACT_PLACEHOLDER, -1, slotNum);
2023-12-04 23:21:49 +02:00
altarSlot->clickPressedCallback = std::bind(&CAltarArtifacts::onSlotClickPressed, this, _1, hRight);
2024-02-27 13:39:50 +02:00
altarSlot->subtitle->clear();
2023-12-04 23:21:49 +02:00
items.front().emplace_back(altarSlot);
slotNum++;
}
expForHero->setText(std::to_string(0));
CTradeBase::deselect();
};
TExpType CAltarArtifacts::calcExpAltarForHero()
{
TExpType expOnAltar(0);
2024-01-16 16:22:40 +02:00
for(const auto & tradeSlot : tradeSlotsMap)
expOnAltar += calcExpCost(tradeSlot.first);
expForHero->setText(std::to_string(expOnAltar));
return expOnAltar;
2023-12-04 23:21:49 +02:00
}
void CAltarArtifacts::makeDeal()
{
std::vector<TradeItemSell> positions;
2024-01-16 21:54:00 +02:00
for(const auto & [artInst, altarSlot] : 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-01-16 21:54:00 +02:00
tradeSlotsMap.clear();
// The event for removing artifacts from the altar will not be triggered. Therefore, we clean the altar immediately.
2023-12-04 23:21:49 +02:00
for(auto item : items[0])
{
item->setID(-1);
2024-02-27 13:39:50 +02:00
item->subtitle->clear();
2023-12-04 23:21:49 +02:00
}
calcExpAltarForHero();
2024-01-16 21:54:00 +02:00
deal->block(tradeSlotsMap.empty());
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
}
void CAltarArtifacts::setSelectedArtifact(const CArtifactInstance * art)
{
2024-01-16 21:54:00 +02:00
selectedArt->setArtifact(art);
2024-02-19 23:40:43 +02:00
selectedSubtitle->setText(art == nullptr ? "" : std::to_string(calcExpCost(art)));
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-01-16 16:22:40 +02:00
void CAltarArtifacts::updateSlots()
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
auto slotsToAdd = tradeSlotsMap;
for(auto & altarSlot : items[0])
if(altarSlot->id != -1)
2024-01-27 23:48:11 +02:00
{
2024-01-16 16:22:40 +02:00
if(tradeSlotsMap.find(altarSlot->getArtInstance()) == tradeSlotsMap.end())
{
altarSlot->setID(-1);
2024-02-27 13:39:50 +02:00
altarSlot->subtitle->clear();
2024-01-16 16:22:40 +02:00
}
else
{
slotsToAdd.erase(altarSlot->getArtInstance());
}
2024-01-27 23:48:11 +02:00
}
2023-12-04 23:21:49 +02:00
2024-01-16 16:22:40 +02:00
for(auto & tradeSlot : slotsToAdd)
2023-12-04 23:21:49 +02:00
{
2024-01-16 16:22:40 +02:00
assert(tradeSlot.second->id == -1);
assert(altarArtifacts->getSlotByInstance(tradeSlot.first) != ArtifactPosition::PRE_FIRST);
tradeSlot.second->setArtInstance(tradeSlot.first);
2024-02-27 13:39:50 +02:00
tradeSlot.second->subtitle->setText(std::to_string(calcExpCost(tradeSlot.first)));
2024-01-16 16:22:40 +02:00
}
for(auto & slotInfo : altarArtifacts->artifactsInBackpack)
{
if(tradeSlotsMap.find(slotInfo.artifact) == tradeSlotsMap.end())
2023-12-04 23:21:49 +02:00
{
2024-01-16 16:22:40 +02:00
for(auto & altarSlot : items[0])
if(altarSlot->id == -1)
{
altarSlot->setArtInstance(slotInfo.artifact);
2024-02-27 13:39:50 +02:00
altarSlot->subtitle->setText(std::to_string(calcExpCost(slotInfo.artifact)));
2024-01-27 23:48:11 +02:00
tradeSlotsMap.try_emplace(slotInfo.artifact, altarSlot);
2024-01-16 16:22:40 +02:00
break;
}
2023-12-04 23:21:49 +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-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-01-27 23:48:11 +02:00
tradeSlotsMap.try_emplace(pickedArtInst, altarSlot);
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-01-16 16:22:40 +02:00
else if(const CArtifactInstance * art = altarSlot->getArtInstance())
2023-12-04 23:21:49 +02:00
{
2024-01-12 23:57:19 +02:00
const auto slot = altarArtifacts->getSlotByInstance(art);
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));
tradeSlotsMap.erase(art);
2023-12-04 23:21:49 +02:00
}
2024-01-16 16:22:40 +02:00
}
2024-01-16 21:54:00 +02:00
TExpType CAltarArtifacts::calcExpCost(const CArtifactInstance * art)
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-19 23:40:43 +02:00
market->getOffer(art->getTypeId(), 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
}