CWindowWithArtifacts.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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. if(!settings["general"]["enableUiEnhancements"].Bool())
  258. return;
  259. GH.windows().createAndPushWindow<CHeroQuickBackpackWindow>(artSetPtr->getHero(), artPlace.slot);
  260. auto backpackWindow = GH.windows().topWindow<CHeroQuickBackpackWindow>();
  261. backpackWindow->moveTo(cursorPosition - Point(1, 1));
  262. backpackWindow->fitToScreen(15);
  263. }
  264. }, artSetWeak.value());
  265. }
  266. void CWindowWithArtifacts::artifactRemoved(const ArtifactLocation & artLoc)
  267. {
  268. updateSlots();
  269. }
  270. void CWindowWithArtifacts::artifactMoved(const ArtifactLocation & srcLoc, const ArtifactLocation & destLoc, bool withRedraw)
  271. {
  272. auto curState = getState();
  273. if(!curState.has_value())
  274. // Transition state. Nothing to do here. Just skip. Need to wait for final state.
  275. return;
  276. // When moving one artifact onto another it leads to two art movements: dst->TRANSITION_POS; src->dst
  277. // However after first movement we pick the art from TRANSITION_POS and the second movement coming when
  278. // we have a different artifact may look surprising... but it's valid.
  279. auto pickedArtInst = std::get<const CArtifactInstance*>(curState.value());
  280. assert(!pickedArtInst || destLoc.artHolder == std::get<const CGHeroInstance*>(curState.value())->id);
  281. auto artifactMovedBody = [this, withRedraw, &destLoc, &pickedArtInst](auto artSetWeak) -> void
  282. {
  283. auto artSetPtr = artSetWeak.lock();
  284. if(artSetPtr)
  285. {
  286. const auto hero = artSetPtr->getHero();
  287. if(pickedArtInst)
  288. {
  289. markPossibleSlots();
  290. if(pickedArtInst->getTypeId() == ArtifactID::SPELL_SCROLL && pickedArtInst->getScrollSpellID().num >= 0 && settings["general"]["enableUiEnhancements"].Bool())
  291. {
  292. auto anim = GH.renderHandler().loadAnimation(AnimationPath::builtin("spellscr"));
  293. anim->load(pickedArtInst->getScrollSpellID().num);
  294. std::shared_ptr<IImage> img = anim->getImage(pickedArtInst->getScrollSpellID().num);
  295. CCS->curh->dragAndDropCursor(img->scaleFast(Point(44, 34)));
  296. }
  297. else
  298. CCS->curh->dragAndDropCursor(AnimationPath::builtin("artifact"), pickedArtInst->artType->getIconIndex());
  299. }
  300. else
  301. {
  302. artSetPtr->unmarkSlots();
  303. CCS->curh->dragAndDropCursor(nullptr);
  304. }
  305. if(withRedraw)
  306. {
  307. artSetPtr->updateWornSlots();
  308. artSetPtr->updateBackpackSlots();
  309. // Update arts bonuses on window.
  310. // TODO rework this part when CHeroWindow and CExchangeWindow are reworked
  311. if(auto * chw = dynamic_cast<CHeroWindow*>(this))
  312. {
  313. chw->update(hero, true);
  314. }
  315. else if(auto * cew = dynamic_cast<CExchangeWindow*>(this))
  316. {
  317. cew->updateWidgets();
  318. }
  319. artSetPtr->redraw();
  320. }
  321. // Make sure the status bar is updated so it does not display old text
  322. if(destLoc.artHolder == hero->id)
  323. {
  324. if(auto artPlace = artSetPtr->getArtPlace(destLoc.slot))
  325. artPlace->hover(true);
  326. }
  327. }
  328. };
  329. for(auto artSetWeak : artSets)
  330. std::visit(artifactMovedBody, artSetWeak);
  331. }
  332. void CWindowWithArtifacts::artifactDisassembled(const ArtifactLocation & artLoc)
  333. {
  334. updateSlots();
  335. }
  336. void CWindowWithArtifacts::artifactAssembled(const ArtifactLocation & artLoc)
  337. {
  338. markPossibleSlots();
  339. updateSlots();
  340. }
  341. void CWindowWithArtifacts::updateSlots()
  342. {
  343. auto updateSlotBody = [](auto artSetWeak) -> void
  344. {
  345. if(const auto artSetPtr = artSetWeak.lock())
  346. {
  347. artSetPtr->updateWornSlots();
  348. artSetPtr->updateBackpackSlots();
  349. artSetPtr->redraw();
  350. }
  351. };
  352. for(auto artSetWeak : artSets)
  353. std::visit(updateSlotBody, artSetWeak);
  354. }
  355. std::optional<std::tuple<const CGHeroInstance*, const CArtifactInstance*>> CWindowWithArtifacts::getState()
  356. {
  357. const CArtifactInstance * artInst = nullptr;
  358. std::map<const CGHeroInstance*, size_t> pickedCnt;
  359. auto getHeroArtBody = [&artInst, &pickedCnt](auto artSetWeak) -> void
  360. {
  361. auto artSetPtr = artSetWeak.lock();
  362. if(artSetPtr)
  363. {
  364. if(const auto art = artSetPtr->getPickedArtifact())
  365. {
  366. const auto hero = artSetPtr->getHero();
  367. if(pickedCnt.count(hero) == 0)
  368. {
  369. pickedCnt.insert({ hero, hero->artifactsTransitionPos.size() });
  370. artInst = art;
  371. }
  372. }
  373. }
  374. };
  375. for(auto artSetWeak : artSets)
  376. std::visit(getHeroArtBody, artSetWeak);
  377. // The state is possible when the hero has placed an artifact in the ArtifactPosition::TRANSITION_POS,
  378. // and the previous artifact has not yet removed from the ArtifactPosition::TRANSITION_POS.
  379. // This is a transitional state. Then return nullopt.
  380. if(std::accumulate(std::begin(pickedCnt), std::end(pickedCnt), 0, [](size_t accum, const auto & value)
  381. {
  382. return accum + value.second;
  383. }) > 1)
  384. return std::nullopt;
  385. else
  386. return std::make_tuple(pickedCnt.begin()->first, artInst);
  387. }
  388. std::optional<CWindowWithArtifacts::CArtifactsOfHeroPtr> CWindowWithArtifacts::findAOHbyRef(CArtifactsOfHeroBase & artsInst)
  389. {
  390. std::optional<CArtifactsOfHeroPtr> res;
  391. auto findAOHBody = [&res, &artsInst](auto & artSetWeak) -> void
  392. {
  393. if(&artsInst == artSetWeak.lock().get())
  394. res = artSetWeak;
  395. };
  396. for(auto artSetWeak : artSets)
  397. {
  398. std::visit(findAOHBody, artSetWeak);
  399. if(res.has_value())
  400. return res;
  401. }
  402. return res;
  403. }
  404. void CWindowWithArtifacts::markPossibleSlots()
  405. {
  406. if(const auto pickedArtInst = getPickedArtifact())
  407. {
  408. const auto heroArtOwner = getHeroPickedArtifact();
  409. auto artifactAssembledBody = [&pickedArtInst, &heroArtOwner](auto artSetWeak) -> void
  410. {
  411. if(auto artSetPtr = artSetWeak.lock())
  412. {
  413. if(artSetPtr->isActive())
  414. {
  415. const auto hero = artSetPtr->getHero();
  416. if(heroArtOwner == hero || !std::is_same_v<decltype(artSetWeak), std::weak_ptr<CArtifactsOfHeroKingdom>>)
  417. artSetPtr->markPossibleSlots(pickedArtInst, hero->tempOwner == LOCPLINT->playerID);
  418. }
  419. }
  420. };
  421. for(auto artSetWeak : artSets)
  422. std::visit(artifactAssembledBody, artSetWeak);
  423. }
  424. }