CAltarArtifacts.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. * CAltarArtifacts.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "CAltarArtifacts.h"
  12. #include "../../GameEngine.h"
  13. #include "../../GameInstance.h"
  14. #include "../../gui/Shortcut.h"
  15. #include "../../widgets/Buttons.h"
  16. #include "../../widgets/TextControls.h"
  17. #include "../../CPlayerInterface.h"
  18. #include "../../../CCallback.h"
  19. #include "../../../lib/networkPacks/ArtifactLocation.h"
  20. #include "../../../lib/texts/CGeneralTextHandler.h"
  21. #include "../../../lib/mapObjects/CGHeroInstance.h"
  22. #include "../../../lib/mapObjects/IMarket.h"
  23. CAltarArtifacts::CAltarArtifacts(const IMarket * market, const CGHeroInstance * hero)
  24. : CMarketBase(market, hero)
  25. {
  26. OBJECT_CONSTRUCTION;
  27. assert(market->getArtifactsStorage());
  28. altarArtifactsStorage = market->getArtifactsStorage();
  29. deal = std::make_shared<CButton>(Point(269, 520), AnimationPath::builtin("ALTSACR.DEF"),
  30. VLC->generaltexth->zelp[585], [this]() {CAltarArtifacts::makeDeal(); }, EShortcut::MARKET_DEAL);
  31. labels.emplace_back(std::make_shared<CLabel>(450, 32, FONT_SMALL, ETextAlignment::CENTER, Colors::YELLOW, VLC->generaltexth->allTexts[477]));
  32. labels.emplace_back(std::make_shared<CLabel>(302, 424, FONT_SMALL, ETextAlignment::CENTER, Colors::YELLOW, VLC->generaltexth->allTexts[478]));
  33. sacrificeAllButton = std::make_shared<CButton>(Point(393, 520), AnimationPath::builtin("ALTFILL.DEF"),
  34. VLC->generaltexth->zelp[571], std::bind(&CExperienceAltar::sacrificeAll, this), EShortcut::MARKET_SACRIFICE_ALL);
  35. sacrificeAllButton->block(hero->artifactsInBackpack.empty() && hero->artifactsWorn.empty());
  36. sacrificeBackpackButton = std::make_shared<CButton>(Point(147, 520), AnimationPath::builtin("ALTEMBK.DEF"),
  37. VLC->generaltexth->zelp[570], std::bind(&CAltarArtifacts::sacrificeBackpack, this), EShortcut::MARKET_SACRIFICE_BACKPACK);
  38. sacrificeBackpackButton->block(hero->artifactsInBackpack.empty());
  39. // Hero's artifacts
  40. heroArts = std::make_shared<CArtifactsOfHeroAltar>(Point(-365, -11));
  41. heroArts->setHero(hero);
  42. heroArts->altarId = market->getObjInstanceID();
  43. // Altar
  44. offerTradePanel = std::make_shared<ArtifactsAltarPanel>([this](const std::shared_ptr<CTradeableItem> & altarSlot)
  45. {
  46. CAltarArtifacts::onSlotClickPressed(altarSlot, offerTradePanel);
  47. });
  48. offerTradePanel->updateSlotsCallback = std::bind(&CAltarArtifacts::updateAltarSlots, this);
  49. offerTradePanel->moveTo(pos.topLeft() + Point(315, 53));
  50. CMarketBase::updateShowcases();
  51. CAltarArtifacts::deselect();
  52. };
  53. TExpType CAltarArtifacts::calcExpAltarForHero()
  54. {
  55. TExpType expOnAltar(0);
  56. for(const auto & tradeSlot : tradeSlotsMap)
  57. expOnAltar += calcExpCost(tradeSlot.second->getTypeId());
  58. expForHero->setText(std::to_string(expOnAltar));
  59. return expOnAltar;
  60. }
  61. void CAltarArtifacts::deselect()
  62. {
  63. CMarketBase::deselect();
  64. CExperienceAltar::deselect();
  65. tradeSlotsMap.clear();
  66. // The event for removing artifacts from the altar will not be triggered. Therefore, we clean the altar immediately.
  67. for(const auto & slot : offerTradePanel->slots)
  68. slot->clear();
  69. offerTradePanel->showcaseSlot->clear();
  70. }
  71. void CAltarArtifacts::update()
  72. {
  73. CMarketBase::update();
  74. CExperienceAltar::update();
  75. if(const auto art = hero->getArt(ArtifactPosition::TRANSITION_POS))
  76. offerQty = calcExpCost(art->getTypeId());
  77. else
  78. offerQty = 0;
  79. updateShowcases();
  80. redraw();
  81. }
  82. void CAltarArtifacts::makeDeal()
  83. {
  84. std::vector<TradeItemSell> positions;
  85. for(const auto & [altarSlot, artInst] : tradeSlotsMap)
  86. {
  87. positions.push_back(artInst->getId());
  88. }
  89. GAME->interface()->cb->trade(market->getObjInstanceID(), EMarketMode::ARTIFACT_EXP, positions, std::vector<TradeItemBuy>(), std::vector<ui32>(), hero);
  90. deselect();
  91. }
  92. void CAltarArtifacts::sacrificeAll()
  93. {
  94. GAME->interface()->cb->bulkMoveArtifacts(heroArts->getHero()->id, heroArts->altarId, false, true, true);
  95. }
  96. void CAltarArtifacts::sacrificeBackpack()
  97. {
  98. GAME->interface()->cb->bulkMoveArtifacts(heroArts->getHero()->id, heroArts->altarId, false, false, true);
  99. }
  100. std::shared_ptr<CArtifactsOfHeroAltar> CAltarArtifacts::getAOHset() const
  101. {
  102. return heroArts;
  103. }
  104. void CAltarArtifacts::updateAltarSlots()
  105. {
  106. assert(altarArtifactsStorage->artifactsInBackpack.size() <= GameConstants::ALTAR_ARTIFACTS_SLOTS);
  107. assert(tradeSlotsMap.size() <= GameConstants::ALTAR_ARTIFACTS_SLOTS);
  108. auto tradeSlotsMapNewArts = tradeSlotsMap;
  109. for(const auto & altarSlot : offerTradePanel->slots)
  110. if(altarSlot->id != -1)
  111. {
  112. if(tradeSlotsMap.find(altarSlot) == tradeSlotsMap.end())
  113. {
  114. altarSlot->setID(-1);
  115. altarSlot->subtitle->clear();
  116. }
  117. else
  118. {
  119. tradeSlotsMapNewArts.erase(altarSlot);
  120. }
  121. }
  122. for(auto & tradeSlot : tradeSlotsMapNewArts)
  123. {
  124. assert(tradeSlot.first->id == -1);
  125. assert(altarArtifactsStorage->getArtPos(tradeSlot.second) != ArtifactPosition::PRE_FIRST);
  126. tradeSlot.first->setID(tradeSlot.second->getTypeId().num);
  127. tradeSlot.first->subtitle->setText(std::to_string(calcExpCost(tradeSlot.second->getTypeId())));
  128. }
  129. auto newArtsFromBulkMove = altarArtifactsStorage->artifactsInBackpack;
  130. for(const auto & [altarSlot, art] : tradeSlotsMap)
  131. {
  132. newArtsFromBulkMove.erase(std::remove_if(newArtsFromBulkMove.begin(), newArtsFromBulkMove.end(), [artForRemove = art](auto & slotInfo)
  133. {
  134. return slotInfo.artifact == artForRemove;
  135. }));
  136. }
  137. for(const auto & slotInfo : newArtsFromBulkMove)
  138. {
  139. for(const auto & altarSlot : offerTradePanel->slots)
  140. if(altarSlot->id == -1)
  141. {
  142. altarSlot->setID(slotInfo.artifact->getTypeId().num);
  143. altarSlot->subtitle->setText(std::to_string(calcExpCost(slotInfo.artifact->getTypeId())));
  144. tradeSlotsMap.try_emplace(altarSlot, slotInfo.artifact);
  145. break;
  146. }
  147. }
  148. calcExpAltarForHero();
  149. deal->block(tradeSlotsMap.empty() || !GAME->interface()->makingTurn);
  150. }
  151. void CAltarArtifacts::putBackArtifacts()
  152. {
  153. // TODO: If the backpack capacity limit is enabled, artifacts may remain on the altar.
  154. // Perhaps should be erased in CGameHandler::objectVisitEnded if id of visited object will be available
  155. if(!altarArtifactsStorage->artifactsInBackpack.empty())
  156. GAME->interface()->cb->bulkMoveArtifacts(heroArts->altarId, heroArts->getHero()->id, false, true, true);
  157. }
  158. CMarketBase::MarketShowcasesParams CAltarArtifacts::getShowcasesParams() const
  159. {
  160. if(const auto art = hero->getArt(ArtifactPosition::TRANSITION_POS))
  161. return MarketShowcasesParams
  162. {
  163. std::nullopt,
  164. ShowcaseParams {std::to_string(offerQty), VLC->artifacts()->getByIndex(art->getTypeId())->getIconIndex()}
  165. };
  166. return MarketShowcasesParams {std::nullopt, std::nullopt};
  167. }
  168. void CAltarArtifacts::onSlotClickPressed(const std::shared_ptr<CTradeableItem> & altarSlot, std::shared_ptr<TradePanelBase> & curPanel)
  169. {
  170. assert(altarSlot);
  171. if(const auto pickedArtInst = heroArts->getPickedArtifact())
  172. {
  173. if(pickedArtInst->canBePutAt(altarArtifactsStorage))
  174. {
  175. if(pickedArtInst->getType()->isTradable())
  176. {
  177. if(altarSlot->id == -1)
  178. tradeSlotsMap.try_emplace(altarSlot, pickedArtInst);
  179. deal->block(!GAME->interface()->makingTurn);
  180. GAME->interface()->cb->swapArtifacts(ArtifactLocation(heroArts->getHero()->id, ArtifactPosition::TRANSITION_POS),
  181. ArtifactLocation(heroArts->altarId, ArtifactPosition::ALTAR));
  182. }
  183. else
  184. {
  185. logGlobal->warn("Cannot put special artifact on altar!");
  186. return;
  187. }
  188. }
  189. }
  190. else if(altarSlot->id != -1)
  191. {
  192. assert(tradeSlotsMap.at(altarSlot));
  193. const auto slot = altarArtifactsStorage->getArtPos(tradeSlotsMap.at(altarSlot));
  194. assert(slot != ArtifactPosition::PRE_FIRST);
  195. GAME->interface()->cb->swapArtifacts(ArtifactLocation(heroArts->altarId, slot),
  196. ArtifactLocation(hero->id, ENGINE->isKeyboardCtrlDown() ? ArtifactPosition::FIRST_AVAILABLE : ArtifactPosition::TRANSITION_POS));
  197. tradeSlotsMap.erase(altarSlot);
  198. }
  199. }
  200. TExpType CAltarArtifacts::calcExpCost(ArtifactID id) const
  201. {
  202. int bidQty = 0;
  203. int expOfArt = 0;
  204. market->getOffer(id, 0, bidQty, expOfArt, EMarketMode::ARTIFACT_EXP);
  205. return hero->calculateXp(expOfArt);
  206. }