CWindowWithArtifacts.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. /*
  2. * CWindowWithArtifacts.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 "CWindowWithArtifacts.h"
  12. #include "../gui/CGuiHandler.h"
  13. #include "../gui/CursorHandler.h"
  14. #include "CComponent.h"
  15. #include "../windows/CHeroWindow.h"
  16. #include "../windows/CSpellWindow.h"
  17. #include "../windows/GUIClasses.h"
  18. #include "../CPlayerInterface.h"
  19. #include "../CGameInfo.h"
  20. #include "../../lib/CGeneralTextHandler.h"
  21. #include "../../lib/mapObjects/CGHeroInstance.h"
  22. void CWindowWithArtifacts::addSet(CArtifactsOfHeroPtr artSet)
  23. {
  24. artSets.emplace_back(artSet);
  25. std::visit([this](auto artSetWeak)
  26. {
  27. auto artSet = artSetWeak.lock();
  28. artSet->leftClickCallback = std::bind(&CWindowWithArtifacts::leftClickArtPlaceHero, this, _1, _2);
  29. artSet->rightClickCallback = std::bind(&CWindowWithArtifacts::rightClickArtPlaceHero, this, _1, _2);
  30. }, artSet);
  31. }
  32. const CGHeroInstance * CWindowWithArtifacts::getHeroPickedArtifact()
  33. {
  34. auto res = getState();
  35. if(res.has_value())
  36. return std::get<const CGHeroInstance*>(res.value());
  37. else
  38. return nullptr;
  39. }
  40. const CArtifactInstance * CWindowWithArtifacts::getPickedArtifact()
  41. {
  42. auto res = getState();
  43. if(res.has_value())
  44. return std::get<const CArtifactInstance*>(res.value());
  45. else
  46. return nullptr;
  47. }
  48. void CWindowWithArtifacts::leftClickArtPlaceHero(CArtifactsOfHeroBase & artsInst, CHeroArtPlace & artPlace)
  49. {
  50. const auto artSetWeak = findAOHbyRef(artsInst);
  51. assert(artSetWeak.has_value());
  52. if(artPlace.isLocked())
  53. return;
  54. const auto checkSpecialArts = [](const CGHeroInstance * hero, CHeroArtPlace & artPlace) -> bool
  55. {
  56. if(artPlace.getArt()->getTypeId() == ArtifactID::SPELLBOOK)
  57. {
  58. GH.pushIntT<CSpellWindow>(hero, LOCPLINT, LOCPLINT->battleInt.get());
  59. return false;
  60. }
  61. if(artPlace.getArt()->getTypeId() == ArtifactID::CATAPULT)
  62. {
  63. // The Catapult must be equipped
  64. std::vector<std::shared_ptr<CComponent>> catapult(1, std::make_shared<CComponent>(CComponent::artifact, 3, 0));
  65. LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[312], catapult);
  66. return false;
  67. }
  68. return true;
  69. };
  70. std::visit(
  71. [checkSpecialArts, this, &artPlace](auto artSetWeak) -> void
  72. {
  73. const auto artSetPtr = artSetWeak.lock();
  74. // Hero(Main, Exchange) window, Kingdom window, Altar window left click handler
  75. if constexpr(
  76. std::is_same_v<decltype(artSetWeak), std::weak_ptr<CArtifactsOfHeroMain>> ||
  77. std::is_same_v<decltype(artSetWeak), std::weak_ptr<CArtifactsOfHeroKingdom>> ||
  78. std::is_same_v<decltype(artSetWeak), std::weak_ptr<CArtifactsOfHeroAltar>>)
  79. {
  80. const auto pickedArtInst = getPickedArtifact();
  81. const auto heroPickedArt = getHeroPickedArtifact();
  82. const auto hero = artSetPtr->getHero();
  83. if(pickedArtInst)
  84. {
  85. auto srcLoc = ArtifactLocation(heroPickedArt, ArtifactPosition::TRANSITION_POS);
  86. auto dstLoc = ArtifactLocation(hero, artPlace.slot);
  87. auto isTransferAllowed = false;
  88. if(ArtifactUtils::isSlotBackpack(artPlace.slot))
  89. {
  90. if(pickedArtInst->artType->isBig())
  91. {
  92. // War machines cannot go to backpack
  93. LOCPLINT->showInfoDialog(boost::str(boost::format(CGI->generaltexth->allTexts[153]) % pickedArtInst->artType->getNameTranslated()));
  94. }
  95. else
  96. {
  97. if(ArtifactUtils::isBackpackFreeSlots(heroPickedArt))
  98. isTransferAllowed = true;
  99. else
  100. LOCPLINT->showInfoDialog(CGI->generaltexth->translate("core.genrltxt.152"));
  101. }
  102. }
  103. // Check if artifact transfer is possible
  104. else if(pickedArtInst->canBePutAt(dstLoc, true) && (!artPlace.getArt() || hero->tempOwner == LOCPLINT->playerID))
  105. {
  106. isTransferAllowed = true;
  107. }
  108. if constexpr(std::is_same_v<decltype(artSetWeak), std::weak_ptr<CArtifactsOfHeroKingdom>>)
  109. {
  110. if(hero != heroPickedArt)
  111. isTransferAllowed = false;
  112. }
  113. if(isTransferAllowed)
  114. artSetPtr->swapArtifacts(srcLoc, dstLoc);
  115. }
  116. else
  117. {
  118. if(artPlace.getArt())
  119. {
  120. if(artSetPtr->getHero()->tempOwner == LOCPLINT->playerID)
  121. {
  122. if(checkSpecialArts(hero, artPlace))
  123. artSetPtr->pickUpArtifact(artPlace);
  124. }
  125. else
  126. {
  127. for(const auto artSlot : ArtifactUtils::unmovableSlots())
  128. if(artPlace.slot == artSlot)
  129. {
  130. LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[21]);
  131. break;
  132. }
  133. }
  134. }
  135. }
  136. }
  137. // Market window left click handler
  138. else if constexpr(std::is_same_v<decltype(artSetWeak), std::weak_ptr<CArtifactsOfHeroMarket>>)
  139. {
  140. if(artSetPtr->selectArtCallback && artPlace.getArt())
  141. {
  142. if(artPlace.getArt()->artType->isTradable())
  143. {
  144. artSetPtr->unmarkSlots();
  145. artPlace.selectSlot(true);
  146. artSetPtr->selectArtCallback(&artPlace);
  147. }
  148. else
  149. {
  150. // This item can't be traded
  151. LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[21]);
  152. }
  153. }
  154. }
  155. }, artSetWeak.value());
  156. }
  157. void CWindowWithArtifacts::rightClickArtPlaceHero(CArtifactsOfHeroBase & artsInst, CHeroArtPlace & artPlace)
  158. {
  159. const auto artSetWeak = findAOHbyRef(artsInst);
  160. assert(artSetWeak.has_value());
  161. if(artPlace.isLocked())
  162. return;
  163. std::visit(
  164. [&artPlace](auto artSetWeak) -> void
  165. {
  166. const auto artSetPtr = artSetWeak.lock();
  167. // Hero(Main, Exchange) window, Kingdom window right click handler
  168. if constexpr(
  169. std::is_same_v<decltype(artSetWeak), std::weak_ptr<CArtifactsOfHeroMain>> ||
  170. std::is_same_v<decltype(artSetWeak), std::weak_ptr<CArtifactsOfHeroKingdom>>)
  171. {
  172. if(artPlace.getArt())
  173. {
  174. if(ArtifactUtilsClient::askToDisassemble(artSetPtr->getHero(), artPlace.slot))
  175. {
  176. return;
  177. }
  178. if(ArtifactUtilsClient::askToAssemble(artSetPtr->getHero(), artPlace.slot))
  179. {
  180. return;
  181. }
  182. if(artPlace.text.size())
  183. artPlace.LRClickableAreaWTextComp::clickRight(boost::logic::tribool::true_value, false);
  184. }
  185. }
  186. // Altar window, Market window right click handler
  187. else if constexpr(
  188. std::is_same_v<decltype(artSetWeak), std::weak_ptr<CArtifactsOfHeroAltar>> ||
  189. std::is_same_v<decltype(artSetWeak), std::weak_ptr<CArtifactsOfHeroMarket>>)
  190. {
  191. if(artPlace.getArt() && artPlace.text.size())
  192. artPlace.LRClickableAreaWTextComp::clickRight(boost::logic::tribool::true_value, false);
  193. }
  194. }, artSetWeak.value());
  195. }
  196. void CWindowWithArtifacts::artifactRemoved(const ArtifactLocation & artLoc)
  197. {
  198. updateSlots(artLoc.slot);
  199. }
  200. void CWindowWithArtifacts::artifactMoved(const ArtifactLocation & srcLoc, const ArtifactLocation & destLoc, bool withRedraw)
  201. {
  202. auto curState = getState();
  203. if(!curState.has_value())
  204. // Transition state. Nothing to do here. Just skip. Need to wait for final state.
  205. return;
  206. // When moving one artifact onto another it leads to two art movements: dst->TRANSITION_POS; src->dst
  207. // However after first movement we pick the art from TRANSITION_POS and the second movement coming when
  208. // we have a different artifact may look surprising... but it's valid.
  209. auto pickedArtInst = std::get<const CArtifactInstance*>(curState.value());
  210. assert(!pickedArtInst || destLoc.isHolder(std::get<const CGHeroInstance*>(curState.value())));
  211. auto artifactMovedBody = [this, withRedraw, &srcLoc, &destLoc, &pickedArtInst](auto artSetWeak) -> void
  212. {
  213. auto artSetPtr = artSetWeak.lock();
  214. if(artSetPtr)
  215. {
  216. const auto hero = artSetPtr->getHero();
  217. if(artSetPtr->active)
  218. {
  219. if(pickedArtInst)
  220. {
  221. CCS->curh->dragAndDropCursor("artifact", pickedArtInst->artType->getIconIndex());
  222. if(srcLoc.isHolder(hero) || !std::is_same_v<decltype(artSetWeak), std::weak_ptr<CArtifactsOfHeroKingdom>>)
  223. artSetPtr->markPossibleSlots(pickedArtInst, hero->tempOwner == LOCPLINT->playerID);
  224. }
  225. else
  226. {
  227. artSetPtr->unmarkSlots();
  228. CCS->curh->dragAndDropCursor(nullptr);
  229. }
  230. }
  231. if(withRedraw)
  232. {
  233. artSetPtr->updateWornSlots();
  234. artSetPtr->updateBackpackSlots();
  235. // Update arts bonuses on window.
  236. // TODO rework this part when CHeroWindow and CExchangeWindow are reworked
  237. if(auto * chw = dynamic_cast<CHeroWindow*>(this))
  238. {
  239. chw->update(hero, true);
  240. }
  241. else if(auto * cew = dynamic_cast<CExchangeWindow*>(this))
  242. {
  243. cew->updateWidgets();
  244. }
  245. artSetPtr->safeRedraw();
  246. }
  247. // Make sure the status bar is updated so it does not display old text
  248. if(destLoc.getHolderArtSet() == hero)
  249. {
  250. if(auto artPlace = artSetPtr->getArtPlace(destLoc.slot))
  251. artPlace->hover(true);
  252. }
  253. }
  254. };
  255. for(auto artSetWeak : artSets)
  256. std::visit(artifactMovedBody, artSetWeak);
  257. }
  258. void CWindowWithArtifacts::artifactDisassembled(const ArtifactLocation & artLoc)
  259. {
  260. updateSlots(artLoc.slot);
  261. }
  262. void CWindowWithArtifacts::artifactAssembled(const ArtifactLocation & artLoc)
  263. {
  264. updateSlots(artLoc.slot);
  265. }
  266. void CWindowWithArtifacts::updateSlots(const ArtifactPosition & slot)
  267. {
  268. auto updateSlotBody = [slot](auto artSetWeak) -> void
  269. {
  270. if(const auto artSetPtr = artSetWeak.lock())
  271. {
  272. if(ArtifactUtils::isSlotEquipment(slot))
  273. artSetPtr->updateWornSlots();
  274. else if(ArtifactUtils::isSlotBackpack(slot))
  275. artSetPtr->updateBackpackSlots();
  276. artSetPtr->safeRedraw();
  277. }
  278. };
  279. for(auto artSetWeak : artSets)
  280. std::visit(updateSlotBody, artSetWeak);
  281. }
  282. std::optional<std::tuple<const CGHeroInstance*, const CArtifactInstance*>> CWindowWithArtifacts::getState()
  283. {
  284. const CArtifactInstance * artInst = nullptr;
  285. const CGHeroInstance * hero = nullptr;
  286. size_t pickedCnt = 0;
  287. auto getHeroArtBody = [&hero, &artInst, &pickedCnt](auto artSetWeak) -> void
  288. {
  289. auto artSetPtr = artSetWeak.lock();
  290. if(artSetPtr)
  291. {
  292. if(const auto art = artSetPtr->getPickedArtifact())
  293. {
  294. artInst = art;
  295. hero = artSetPtr->getHero();
  296. pickedCnt += hero->artifactsTransitionPos.size();
  297. }
  298. }
  299. };
  300. for(auto artSetWeak : artSets)
  301. std::visit(getHeroArtBody, artSetWeak);
  302. // The state is possible when the hero has placed an artifact in the ArtifactPosition::TRANSITION_POS,
  303. // and the previous artifact has not yet removed from the ArtifactPosition::TRANSITION_POS.
  304. // This is a transitional state. Then return nullopt.
  305. if(pickedCnt > 1)
  306. return std::nullopt;
  307. else
  308. return std::make_tuple(hero, artInst);
  309. }
  310. std::optional<CWindowWithArtifacts::CArtifactsOfHeroPtr> CWindowWithArtifacts::findAOHbyRef(CArtifactsOfHeroBase & artsInst)
  311. {
  312. std::optional<CArtifactsOfHeroPtr> res;
  313. auto findAOHBody = [&res, &artsInst](auto & artSetWeak) -> void
  314. {
  315. if(&artsInst == artSetWeak.lock().get())
  316. res = artSetWeak;
  317. };
  318. for(auto artSetWeak : artSets)
  319. {
  320. std::visit(findAOHBody, artSetWeak);
  321. if(res.has_value())
  322. return res;
  323. }
  324. return res;
  325. }