CWindowWithArtifacts.cpp 13 KB

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