CWindowWithArtifacts.cpp 12 KB

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