CAltarArtifacts.cpp 8.0 KB

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