CWindowWithArtifacts.cpp 14 KB

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