CWindowWithArtifacts.cpp 11 KB

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