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