CWindowWithArtifacts.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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);
  44. artSet->showPopupCallback = std::bind(&CWindowWithArtifacts::rightClickArtPlaceHero, this, _1, _2);
  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, CHeroArtPlace & artPlace)
  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, CHeroArtPlace & 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>(CComponent::artifact, 3, 0));
  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, ArtifactPosition::TRANSITION_POS);
  109. auto dstLoc = ArtifactLocation(hero, 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(dstLoc, 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. }, artSetWeak.value());
  191. }
  192. void CWindowWithArtifacts::rightClickArtPlaceHero(CArtifactsOfHeroBase & artsInst, CHeroArtPlace & artPlace)
  193. {
  194. const auto artSetWeak = findAOHbyRef(artsInst);
  195. assert(artSetWeak.has_value());
  196. if(artPlace.isLocked())
  197. return;
  198. std::visit(
  199. [&artPlace](auto artSetWeak) -> void
  200. {
  201. const auto artSetPtr = artSetWeak.lock();
  202. // Hero (Main, Exchange) window, Kingdom window, Backpack window right click handler
  203. if constexpr(
  204. std::is_same_v<decltype(artSetWeak), std::weak_ptr<CArtifactsOfHeroMain>> ||
  205. std::is_same_v<decltype(artSetWeak), std::weak_ptr<CArtifactsOfHeroKingdom>> ||
  206. std::is_same_v<decltype(artSetWeak), std::weak_ptr<CArtifactsOfHeroBackpack>>)
  207. {
  208. if(artPlace.getArt())
  209. {
  210. if(ArtifactUtilsClient::askToDisassemble(artSetPtr->getHero(), artPlace.slot))
  211. {
  212. return;
  213. }
  214. if(ArtifactUtilsClient::askToAssemble(artSetPtr->getHero(), artPlace.slot))
  215. {
  216. return;
  217. }
  218. if(artPlace.text.size())
  219. artPlace.LRClickableAreaWTextComp::showPopupWindow(GH.getCursorPosition());
  220. }
  221. }
  222. // Altar window, Market window right click handler
  223. else if constexpr(
  224. std::is_same_v<decltype(artSetWeak), std::weak_ptr<CArtifactsOfHeroAltar>> ||
  225. std::is_same_v<decltype(artSetWeak), std::weak_ptr<CArtifactsOfHeroMarket>>)
  226. {
  227. if(artPlace.getArt() && artPlace.text.size())
  228. artPlace.LRClickableAreaWTextComp::showPopupWindow(GH.getCursorPosition());
  229. }
  230. }, artSetWeak.value());
  231. }
  232. void CWindowWithArtifacts::artifactRemoved(const ArtifactLocation & artLoc)
  233. {
  234. updateSlots();
  235. }
  236. void CWindowWithArtifacts::artifactMoved(const ArtifactLocation & srcLoc, const ArtifactLocation & destLoc, bool withRedraw)
  237. {
  238. auto curState = getState();
  239. if(!curState.has_value())
  240. // Transition state. Nothing to do here. Just skip. Need to wait for final state.
  241. return;
  242. // When moving one artifact onto another it leads to two art movements: dst->TRANSITION_POS; src->dst
  243. // However after first movement we pick the art from TRANSITION_POS and the second movement coming when
  244. // we have a different artifact may look surprising... but it's valid.
  245. auto pickedArtInst = std::get<const CArtifactInstance*>(curState.value());
  246. assert(!pickedArtInst || destLoc.isHolder(std::get<const CGHeroInstance*>(curState.value())));
  247. auto artifactMovedBody = [this, withRedraw, &destLoc, &pickedArtInst](auto artSetWeak) -> void
  248. {
  249. auto artSetPtr = artSetWeak.lock();
  250. if(artSetPtr)
  251. {
  252. const auto hero = artSetPtr->getHero();
  253. if(pickedArtInst)
  254. {
  255. markPossibleSlots();
  256. if(pickedArtInst->getTypeId() == ArtifactID::SPELL_SCROLL && pickedArtInst->getScrollSpellID().num >= 0 && settings["general"]["enableUiEnhancements"].Bool())
  257. {
  258. auto anim = GH.renderHandler().loadAnimation(AnimationPath::builtin("spellscr"));
  259. anim->load(pickedArtInst->getScrollSpellID().num);
  260. std::shared_ptr<IImage> img = anim->getImage(pickedArtInst->getScrollSpellID().num);
  261. CCS->curh->dragAndDropCursor(img->scaleFast(Point(44, 34)));
  262. }
  263. else
  264. CCS->curh->dragAndDropCursor(AnimationPath::builtin("artifact"), pickedArtInst->artType->getIconIndex());
  265. }
  266. else
  267. {
  268. artSetPtr->unmarkSlots();
  269. CCS->curh->dragAndDropCursor(nullptr);
  270. }
  271. if(withRedraw)
  272. {
  273. artSetPtr->updateWornSlots();
  274. artSetPtr->updateBackpackSlots();
  275. // Update arts bonuses on window.
  276. // TODO rework this part when CHeroWindow and CExchangeWindow are reworked
  277. if(auto * chw = dynamic_cast<CHeroWindow*>(this))
  278. {
  279. chw->update(hero, true);
  280. }
  281. else if(auto * cew = dynamic_cast<CExchangeWindow*>(this))
  282. {
  283. cew->updateWidgets();
  284. }
  285. artSetPtr->redraw();
  286. }
  287. // Make sure the status bar is updated so it does not display old text
  288. if(destLoc.getHolderArtSet() == hero)
  289. {
  290. if(auto artPlace = artSetPtr->getArtPlace(destLoc.slot))
  291. artPlace->hover(true);
  292. }
  293. }
  294. };
  295. for(auto artSetWeak : artSets)
  296. std::visit(artifactMovedBody, artSetWeak);
  297. }
  298. void CWindowWithArtifacts::artifactDisassembled(const ArtifactLocation & artLoc)
  299. {
  300. updateSlots();
  301. }
  302. void CWindowWithArtifacts::artifactAssembled(const ArtifactLocation & artLoc)
  303. {
  304. markPossibleSlots();
  305. updateSlots();
  306. }
  307. void CWindowWithArtifacts::updateSlots()
  308. {
  309. auto updateSlotBody = [](auto artSetWeak) -> void
  310. {
  311. if(const auto artSetPtr = artSetWeak.lock())
  312. {
  313. artSetPtr->updateWornSlots();
  314. artSetPtr->updateBackpackSlots();
  315. artSetPtr->redraw();
  316. }
  317. };
  318. for(auto artSetWeak : artSets)
  319. std::visit(updateSlotBody, artSetWeak);
  320. }
  321. std::optional<std::tuple<const CGHeroInstance*, const CArtifactInstance*>> CWindowWithArtifacts::getState()
  322. {
  323. const CArtifactInstance * artInst = nullptr;
  324. std::map<const CGHeroInstance*, size_t> pickedCnt;
  325. auto getHeroArtBody = [&artInst, &pickedCnt](auto artSetWeak) -> void
  326. {
  327. auto artSetPtr = artSetWeak.lock();
  328. if(artSetPtr)
  329. {
  330. if(const auto art = artSetPtr->getPickedArtifact())
  331. {
  332. const auto hero = artSetPtr->getHero();
  333. if(pickedCnt.count(hero) == 0)
  334. {
  335. pickedCnt.insert({ hero, hero->artifactsTransitionPos.size() });
  336. artInst = art;
  337. }
  338. }
  339. }
  340. };
  341. for(auto artSetWeak : artSets)
  342. std::visit(getHeroArtBody, artSetWeak);
  343. // The state is possible when the hero has placed an artifact in the ArtifactPosition::TRANSITION_POS,
  344. // and the previous artifact has not yet removed from the ArtifactPosition::TRANSITION_POS.
  345. // This is a transitional state. Then return nullopt.
  346. if(std::accumulate(std::begin(pickedCnt), std::end(pickedCnt), 0, [](size_t accum, const auto & value)
  347. {
  348. return accum + value.second;
  349. }) > 1)
  350. return std::nullopt;
  351. else
  352. return std::make_tuple(pickedCnt.begin()->first, artInst);
  353. }
  354. std::optional<CWindowWithArtifacts::CArtifactsOfHeroPtr> CWindowWithArtifacts::findAOHbyRef(CArtifactsOfHeroBase & artsInst)
  355. {
  356. std::optional<CArtifactsOfHeroPtr> res;
  357. auto findAOHBody = [&res, &artsInst](auto & artSetWeak) -> void
  358. {
  359. if(&artsInst == artSetWeak.lock().get())
  360. res = artSetWeak;
  361. };
  362. for(auto artSetWeak : artSets)
  363. {
  364. std::visit(findAOHBody, artSetWeak);
  365. if(res.has_value())
  366. return res;
  367. }
  368. return res;
  369. }
  370. void CWindowWithArtifacts::markPossibleSlots()
  371. {
  372. if(const auto pickedArtInst = getPickedArtifact())
  373. {
  374. const auto heroArtOwner = getHeroPickedArtifact();
  375. auto artifactAssembledBody = [&pickedArtInst, &heroArtOwner](auto artSetWeak) -> void
  376. {
  377. if(auto artSetPtr = artSetWeak.lock())
  378. {
  379. if(artSetPtr->isActive())
  380. {
  381. const auto hero = artSetPtr->getHero();
  382. if(heroArtOwner == hero || !std::is_same_v<decltype(artSetWeak), std::weak_ptr<CArtifactsOfHeroKingdom>>)
  383. artSetPtr->markPossibleSlots(pickedArtInst, hero->tempOwner == LOCPLINT->playerID);
  384. }
  385. }
  386. };
  387. for(auto artSetWeak : artSets)
  388. std::visit(artifactAssembledBody, artSetWeak);
  389. }
  390. }