CAltarArtifacts.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. : CTradeBase(market, hero)
  24. {
  25. OBJECT_CONSTRUCTION_CAPTURING(255 - DISPOSE);
  26. assert(market);
  27. auto altarObj = dynamic_cast<const CGArtifactsAltar*>(market);
  28. altarId = altarObj->id;
  29. altarArtifacts = altarObj;
  30. deal = std::make_shared<CButton>(dealButtonPos, AnimationPath::builtin("ALTSACR.DEF"),
  31. CGI->generaltexth->zelp[585], [this]() {CAltarArtifacts::makeDeal(); });
  32. labels.emplace_back(std::make_shared<CLabel>(450, 34, FONT_SMALL, ETextAlignment::CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[477]));
  33. labels.emplace_back(std::make_shared<CLabel>(302, 423, FONT_SMALL, ETextAlignment::CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[478]));
  34. selectedSubtitle = std::make_shared<CLabel>(302, 501, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE);
  35. selectedArt = std::make_shared<CArtPlace>(Point(280, 442));
  36. sacrificeAllButton = std::make_shared<CButton>(Point(393, 520), AnimationPath::builtin("ALTFILL.DEF"),
  37. CGI->generaltexth->zelp[571], std::bind(&CExperienceAltar::sacrificeAll, this));
  38. sacrificeAllButton->block(hero->artifactsInBackpack.empty() && hero->artifactsWorn.empty());
  39. sacrificeBackpackButton = std::make_shared<CButton>(Point(147, 520), AnimationPath::builtin("ALTEMBK.DEF"),
  40. CGI->generaltexth->zelp[570], std::bind(&CAltarArtifacts::sacrificeBackpack, this));
  41. sacrificeBackpackButton->block(hero->artifactsInBackpack.empty());
  42. // Hero's artifacts
  43. heroArts = std::make_shared<CArtifactsOfHeroAltar>(Point(-365, -11));
  44. heroArts->setHero(hero);
  45. int slotNum = 0;
  46. for(auto & altarSlotPos : posSlotsAltar)
  47. {
  48. auto altarSlot = std::make_shared<CTradeableItem>(Rect(altarSlotPos, Point(44, 44)), EType::ARTIFACT_PLACEHOLDER, -1, slotNum);
  49. altarSlot->clickPressedCallback = std::bind(&CAltarArtifacts::onSlotClickPressed, this, _1, hRight);
  50. altarSlot->subtitle->clear();
  51. items.front().emplace_back(altarSlot);
  52. slotNum++;
  53. }
  54. expForHero->setText(std::to_string(0));
  55. CTradeBase::deselect();
  56. };
  57. TExpType CAltarArtifacts::calcExpAltarForHero()
  58. {
  59. TExpType expOnAltar(0);
  60. for(const auto & tradeSlot : tradeSlotsMap)
  61. expOnAltar += calcExpCost(tradeSlot.first);
  62. expForHero->setText(std::to_string(expOnAltar));
  63. return expOnAltar;
  64. }
  65. void CAltarArtifacts::makeDeal()
  66. {
  67. std::vector<TradeItemSell> positions;
  68. for(const auto & [artInst, altarSlot] : tradeSlotsMap)
  69. {
  70. positions.push_back(artInst->getId());
  71. }
  72. LOCPLINT->cb->trade(market, EMarketMode::ARTIFACT_EXP, positions, std::vector<TradeItemBuy>(), std::vector<ui32>(), hero);
  73. tradeSlotsMap.clear();
  74. // The event for removing artifacts from the altar will not be triggered. Therefore, we clean the altar immediately.
  75. for(auto item : items[0])
  76. {
  77. item->setID(-1);
  78. item->subtitle->clear();
  79. }
  80. calcExpAltarForHero();
  81. deal->block(tradeSlotsMap.empty());
  82. }
  83. void CAltarArtifacts::sacrificeAll()
  84. {
  85. LOCPLINT->cb->bulkMoveArtifacts(heroArts->getHero()->id, altarId, false, true, true);
  86. }
  87. void CAltarArtifacts::sacrificeBackpack()
  88. {
  89. LOCPLINT->cb->bulkMoveArtifacts(heroArts->getHero()->id, altarId, false, false, true);
  90. }
  91. void CAltarArtifacts::setSelectedArtifact(const CArtifactInstance * art)
  92. {
  93. selectedArt->setArtifact(art);
  94. selectedSubtitle->setText(art == nullptr ? "" : std::to_string(calcExpCost(art)));
  95. }
  96. std::shared_ptr<CArtifactsOfHeroAltar> CAltarArtifacts::getAOHset() const
  97. {
  98. return heroArts;
  99. }
  100. ObjectInstanceID CAltarArtifacts::getObjId() const
  101. {
  102. return altarId;
  103. }
  104. void CAltarArtifacts::updateSlots()
  105. {
  106. assert(altarArtifacts->artifactsInBackpack.size() <= GameConstants::ALTAR_ARTIFACTS_SLOTS);
  107. assert(tradeSlotsMap.size() <= GameConstants::ALTAR_ARTIFACTS_SLOTS);
  108. auto slotsToAdd = tradeSlotsMap;
  109. for(auto & altarSlot : items[0])
  110. if(altarSlot->id != -1)
  111. {
  112. if(tradeSlotsMap.find(altarSlot->getArtInstance()) == tradeSlotsMap.end())
  113. {
  114. altarSlot->setID(-1);
  115. altarSlot->subtitle->clear();
  116. }
  117. else
  118. {
  119. slotsToAdd.erase(altarSlot->getArtInstance());
  120. }
  121. }
  122. for(auto & tradeSlot : slotsToAdd)
  123. {
  124. assert(tradeSlot.second->id == -1);
  125. assert(altarArtifacts->getSlotByInstance(tradeSlot.first) != ArtifactPosition::PRE_FIRST);
  126. tradeSlot.second->setArtInstance(tradeSlot.first);
  127. tradeSlot.second->subtitle->setText(std::to_string(calcExpCost(tradeSlot.first)));
  128. }
  129. for(auto & slotInfo : altarArtifacts->artifactsInBackpack)
  130. {
  131. if(tradeSlotsMap.find(slotInfo.artifact) == tradeSlotsMap.end())
  132. {
  133. for(auto & altarSlot : items[0])
  134. if(altarSlot->id == -1)
  135. {
  136. altarSlot->setArtInstance(slotInfo.artifact);
  137. altarSlot->subtitle->setText(std::to_string(calcExpCost(slotInfo.artifact)));
  138. tradeSlotsMap.try_emplace(slotInfo.artifact, altarSlot);
  139. break;
  140. }
  141. }
  142. }
  143. calcExpAltarForHero();
  144. deal->block(tradeSlotsMap.empty());
  145. }
  146. void CAltarArtifacts::putBackArtifacts()
  147. {
  148. // TODO: If the backpack capacity limit is enabled, artifacts may remain on the altar.
  149. // Perhaps should be erased in CGameHandler::objectVisitEnded if id of visited object will be available
  150. if(!altarArtifacts->artifactsInBackpack.empty())
  151. LOCPLINT->cb->bulkMoveArtifacts(altarId, heroArts->getHero()->id, false, true, true);
  152. }
  153. void CAltarArtifacts::onSlotClickPressed(const std::shared_ptr<CTradeableItem> & altarSlot, std::shared_ptr<CTradeableItem> & hCurSlot)
  154. {
  155. assert(altarSlot);
  156. if(const auto pickedArtInst = heroArts->getPickedArtifact())
  157. {
  158. if(pickedArtInst->canBePutAt(altarArtifacts))
  159. {
  160. if(pickedArtInst->artType->isTradable())
  161. {
  162. if(altarSlot->id == -1)
  163. tradeSlotsMap.try_emplace(pickedArtInst, altarSlot);
  164. deal->block(false);
  165. LOCPLINT->cb->swapArtifacts(ArtifactLocation(heroArts->getHero()->id, ArtifactPosition::TRANSITION_POS),
  166. ArtifactLocation(altarId, ArtifactPosition::ALTAR));
  167. }
  168. else
  169. {
  170. logGlobal->warn("Cannot put special artifact on altar!");
  171. return;
  172. }
  173. }
  174. }
  175. else if(const CArtifactInstance * art = altarSlot->getArtInstance())
  176. {
  177. const auto slot = altarArtifacts->getSlotByInstance(art);
  178. assert(slot != ArtifactPosition::PRE_FIRST);
  179. LOCPLINT->cb->swapArtifacts(ArtifactLocation(altarId, slot), ArtifactLocation(hero->id, ArtifactPosition::TRANSITION_POS));
  180. tradeSlotsMap.erase(art);
  181. }
  182. }
  183. TExpType CAltarArtifacts::calcExpCost(const CArtifactInstance * art)
  184. {
  185. int bidQty = 0;
  186. int expOfArt = 0;
  187. market->getOffer(art->getTypeId(), 0, bidQty, expOfArt, EMarketMode::ARTIFACT_EXP);
  188. return hero->calculateXp(expOfArt);
  189. }