CAltarArtifacts.cpp 7.0 KB

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