CWindowWithArtifacts.cpp 13 KB

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