CWindowWithArtifacts.cpp 12 KB

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