CWindowWithArtifacts.cpp 14 KB

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