CAltarArtifacts.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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 "../../gui/CGuiHandler.h"
  13. #include "../../widgets/Buttons.h"
  14. #include "../../widgets/TextControls.h"
  15. #include "../../CGameInfo.h"
  16. #include "../../CPlayerInterface.h"
  17. #include "../../../CCallback.h"
  18. #include "../../../lib/networkPacks/ArtifactLocation.h"
  19. #include "../../../lib/CGeneralTextHandler.h"
  20. #include "../../../lib/mapObjects/CGHeroInstance.h"
  21. #include "../../../lib/mapObjects/CGMarket.h"
  22. CAltarArtifacts::CAltarArtifacts(const IMarket * market, const CGHeroInstance * hero)
  23. : CMarketBase(market, hero, [this](){return CAltarArtifacts::getSelectionParams();})
  24. {
  25. OBJECT_CONSTRUCTION_CAPTURING(255 - DISPOSE);
  26. assert(dynamic_cast<const CGArtifactsAltar*>(market));
  27. auto altarObj = dynamic_cast<const CGArtifactsAltar*>(market);
  28. altarId = altarObj->id;
  29. altarArtifacts = altarObj;
  30. deal = std::make_shared<CButton>(Point(269, 520), AnimationPath::builtin("ALTSACR.DEF"),
  31. CGI->generaltexth->zelp[585], [this]() {CAltarArtifacts::makeDeal(); });
  32. labels.emplace_back(std::make_shared<CLabel>(450, 32, FONT_SMALL, ETextAlignment::CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[477]));
  33. labels.emplace_back(std::make_shared<CLabel>(302, 424, FONT_SMALL, ETextAlignment::CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[478]));
  34. sacrificeAllButton = std::make_shared<CButton>(Point(393, 520), AnimationPath::builtin("ALTFILL.DEF"),
  35. CGI->generaltexth->zelp[571], std::bind(&CExperienceAltar::sacrificeAll, this));
  36. sacrificeAllButton->block(hero->artifactsInBackpack.empty() && hero->artifactsWorn.empty());
  37. sacrificeBackpackButton = std::make_shared<CButton>(Point(147, 520), AnimationPath::builtin("ALTEMBK.DEF"),
  38. CGI->generaltexth->zelp[570], std::bind(&CAltarArtifacts::sacrificeBackpack, this));
  39. sacrificeBackpackButton->block(hero->artifactsInBackpack.empty());
  40. // Hero's artifacts
  41. heroArts = std::make_shared<CArtifactsOfHeroAltar>(Point(-365, -11));
  42. heroArts->setHero(hero);
  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, 52));
  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. {
  77. offerTradePanel->showcaseSlot->setID(art->getTypeId().num);
  78. offerTradePanel->highlightedSlot = offerTradePanel->showcaseSlot;
  79. offerQty = calcExpCost(art->getTypeId());
  80. }
  81. else
  82. {
  83. offerTradePanel->showcaseSlot->clear();
  84. offerTradePanel->highlightedSlot.reset();
  85. offerQty = 0;
  86. }
  87. updateShowcases();
  88. }
  89. void CAltarArtifacts::makeDeal()
  90. {
  91. std::vector<TradeItemSell> positions;
  92. for(const auto & [altarSlot, artInst] : tradeSlotsMap)
  93. {
  94. positions.push_back(artInst->getId());
  95. }
  96. LOCPLINT->cb->trade(market, EMarketMode::ARTIFACT_EXP, positions, std::vector<TradeItemBuy>(), std::vector<ui32>(), hero);
  97. deselect();
  98. }
  99. void CAltarArtifacts::sacrificeAll()
  100. {
  101. LOCPLINT->cb->bulkMoveArtifacts(heroArts->getHero()->id, altarId, false, true, true);
  102. }
  103. void CAltarArtifacts::sacrificeBackpack()
  104. {
  105. LOCPLINT->cb->bulkMoveArtifacts(heroArts->getHero()->id, altarId, false, false, true);
  106. }
  107. std::shared_ptr<CArtifactsOfHeroAltar> CAltarArtifacts::getAOHset() const
  108. {
  109. return heroArts;
  110. }
  111. void CAltarArtifacts::updateAltarSlots()
  112. {
  113. assert(altarArtifacts->artifactsInBackpack.size() <= GameConstants::ALTAR_ARTIFACTS_SLOTS);
  114. assert(tradeSlotsMap.size() <= GameConstants::ALTAR_ARTIFACTS_SLOTS);
  115. auto tradeSlotsMapNewArts = tradeSlotsMap;
  116. for(const auto & altarSlot : offerTradePanel->slots)
  117. if(altarSlot->id != -1)
  118. {
  119. if(tradeSlotsMap.find(altarSlot) == tradeSlotsMap.end())
  120. {
  121. altarSlot->setID(-1);
  122. altarSlot->subtitle->clear();
  123. }
  124. else
  125. {
  126. tradeSlotsMapNewArts.erase(altarSlot);
  127. }
  128. }
  129. for(auto & tradeSlot : tradeSlotsMapNewArts)
  130. {
  131. assert(tradeSlot.first->id == -1);
  132. assert(altarArtifacts->getSlotByInstance(tradeSlot.second) != ArtifactPosition::PRE_FIRST);
  133. tradeSlot.first->setID(tradeSlot.second->getTypeId().num);
  134. tradeSlot.first->subtitle->setText(std::to_string(calcExpCost(tradeSlot.second->getTypeId())));
  135. }
  136. auto newArtsFromBulkMove = altarArtifacts->artifactsInBackpack;
  137. for(const auto & [altarSlot, art] : tradeSlotsMap)
  138. {
  139. newArtsFromBulkMove.erase(std::remove_if(newArtsFromBulkMove.begin(), newArtsFromBulkMove.end(), [artForRemove = art](auto & slotInfo)
  140. {
  141. return slotInfo.artifact == artForRemove;
  142. }));
  143. }
  144. for(const auto & slotInfo : newArtsFromBulkMove)
  145. {
  146. for(const auto & altarSlot : offerTradePanel->slots)
  147. if(altarSlot->id == -1)
  148. {
  149. altarSlot->setID(slotInfo.artifact->getTypeId().num);
  150. altarSlot->subtitle->setText(std::to_string(calcExpCost(slotInfo.artifact->getTypeId())));
  151. tradeSlotsMap.try_emplace(altarSlot, slotInfo.artifact);
  152. break;
  153. }
  154. }
  155. calcExpAltarForHero();
  156. deal->block(tradeSlotsMap.empty());
  157. }
  158. void CAltarArtifacts::putBackArtifacts()
  159. {
  160. // TODO: If the backpack capacity limit is enabled, artifacts may remain on the altar.
  161. // Perhaps should be erased in CGameHandler::objectVisitEnded if id of visited object will be available
  162. if(!altarArtifacts->artifactsInBackpack.empty())
  163. LOCPLINT->cb->bulkMoveArtifacts(altarId, heroArts->getHero()->id, false, true, true);
  164. }
  165. CMarketBase::SelectionParams CAltarArtifacts::getSelectionParams() const
  166. {
  167. if(offerTradePanel->highlightedSlot)
  168. return std::make_tuple(
  169. std::nullopt,
  170. SelectionParamOneSide {std::to_string(offerQty), CGI->artifacts()->getByIndex(offerTradePanel->highlightedSlot->id)->getIconIndex()}
  171. );
  172. return std::make_tuple(std::nullopt, std::nullopt);
  173. }
  174. void CAltarArtifacts::onSlotClickPressed(const std::shared_ptr<CTradeableItem> & altarSlot, std::shared_ptr<TradePanelBase> & curPanel)
  175. {
  176. assert(altarSlot);
  177. if(const auto pickedArtInst = heroArts->getPickedArtifact())
  178. {
  179. if(pickedArtInst->canBePutAt(altarArtifacts))
  180. {
  181. if(pickedArtInst->artType->isTradable())
  182. {
  183. if(altarSlot->id == -1)
  184. tradeSlotsMap.try_emplace(altarSlot, pickedArtInst);
  185. deal->block(false);
  186. LOCPLINT->cb->swapArtifacts(ArtifactLocation(heroArts->getHero()->id, ArtifactPosition::TRANSITION_POS),
  187. ArtifactLocation(altarId, ArtifactPosition::ALTAR));
  188. }
  189. else
  190. {
  191. logGlobal->warn("Cannot put special artifact on altar!");
  192. return;
  193. }
  194. }
  195. }
  196. else if(altarSlot->id != -1)
  197. {
  198. assert(tradeSlotsMap.at(altarSlot));
  199. const auto slot = altarArtifacts->getSlotByInstance(tradeSlotsMap.at(altarSlot));
  200. assert(slot != ArtifactPosition::PRE_FIRST);
  201. LOCPLINT->cb->swapArtifacts(ArtifactLocation(altarId, slot), ArtifactLocation(hero->id, ArtifactPosition::TRANSITION_POS));
  202. tradeSlotsMap.erase(altarSlot);
  203. }
  204. }
  205. TExpType CAltarArtifacts::calcExpCost(ArtifactID id) const
  206. {
  207. int bidQty = 0;
  208. int expOfArt = 0;
  209. market->getOffer(id, 0, bidQty, expOfArt, EMarketMode::ARTIFACT_EXP);
  210. return hero->calculateXp(expOfArt);
  211. }