CAltarArtifacts.cpp 6.9 KB

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