CWindowWithArtifacts.cpp 14 KB

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