CWindowWithArtifacts.cpp 11 KB

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