CWindowWithArtifacts.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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 "CHeroWindow.h"
  13. #include "CSpellWindow.h"
  14. #include "CExchangeWindow.h"
  15. #include "CHeroBackpackWindow.h"
  16. #include "../gui/CGuiHandler.h"
  17. #include "../gui/CursorHandler.h"
  18. #include "../gui/WindowHandler.h"
  19. #include "../render/IRenderHandler.h"
  20. #include "../render/CAnimation.h"
  21. #include "../render/IImage.h"
  22. #include "../widgets/CComponent.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<CArtifactsOfHeroPtr> * artSets)
  32. {
  33. if(artSets)
  34. this->artSets.insert(this->artSets.end(), artSets->begin(), artSets->end());
  35. }
  36. void CWindowWithArtifacts::addSet(const std::shared_ptr<CArtifactsOfHeroBase> & newArtSet)
  37. {
  38. artSets.emplace_back(newArtSet);
  39. }
  40. const CGHeroInstance * CWindowWithArtifacts::getHeroPickedArtifact() const
  41. {
  42. const CGHeroInstance * hero = nullptr;
  43. for(const auto & artSet : artSets)
  44. if(const auto pickedArt = artSet->getHero()->getArt(ArtifactPosition::TRANSITION_POS))
  45. {
  46. hero = artSet->getHero();
  47. break;
  48. }
  49. return hero;
  50. }
  51. const CArtifactInstance * CWindowWithArtifacts::getPickedArtifact() const
  52. {
  53. const CArtifactInstance * art = nullptr;
  54. for(const auto & artSet : artSets)
  55. if(const auto pickedArt = artSet->getHero()->getArt(ArtifactPosition::TRANSITION_POS))
  56. {
  57. art = pickedArt;
  58. break;
  59. }
  60. return art;
  61. }
  62. void CWindowWithArtifacts::clickPressedOnArtPlace(const CGHeroInstance * hero, const ArtifactPosition & slot,
  63. bool allowExchange, bool altarTrading, bool closeWindow)
  64. {
  65. if(!LOCPLINT->makingTurn)
  66. return;
  67. if(hero == nullptr)
  68. return;
  69. if(const auto heroArtOwner = getHeroPickedArtifact())
  70. {
  71. if(allowExchange || hero->id == heroArtOwner->id)
  72. putPickedArtifact(*hero, slot);
  73. }
  74. else if(auto art = hero->getArt(slot))
  75. {
  76. if(hero->getOwner() == LOCPLINT->playerID)
  77. {
  78. if(checkSpecialArts(*art, *hero, altarTrading))
  79. onClickPressedCommonArtifact(*hero, slot, closeWindow);
  80. }
  81. else
  82. {
  83. for(const auto & artSlot : ArtifactUtils::unmovableSlots())
  84. if(slot == artSlot)
  85. {
  86. LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[21]);
  87. break;
  88. }
  89. }
  90. }
  91. }
  92. void CWindowWithArtifacts::swapArtifactAndClose(const CArtifactsOfHeroBase & artsInst, const ArtifactPosition & slot,
  93. const ArtifactLocation & dstLoc)
  94. {
  95. LOCPLINT->cb->swapArtifacts(ArtifactLocation(artsInst.getHero()->id, slot), dstLoc);
  96. close();
  97. }
  98. void CWindowWithArtifacts::showArtifactAssembling(const CArtifactsOfHeroBase & artsInst, CArtPlace & artPlace,
  99. const Point & cursorPosition) const
  100. {
  101. if(artsInst.getArt(artPlace.slot))
  102. {
  103. if(LOCPLINT->askToDisassemble(artsInst.getHero(), artPlace.slot))
  104. return;
  105. if(LOCPLINT->askToAssemble(artsInst.getHero(), artPlace.slot))
  106. return;
  107. if(artPlace.text.size())
  108. artPlace.LRClickableAreaWTextComp::showPopupWindow(cursorPosition);
  109. }
  110. }
  111. void CWindowWithArtifacts::showArifactInfo(const CArtifactsOfHeroBase & artsInst, CArtPlace & artPlace, const Point & cursorPosition) const
  112. {
  113. if(artsInst.getArt(artPlace.slot) && artPlace.text.size())
  114. artPlace.LRClickableAreaWTextComp::showPopupWindow(cursorPosition);
  115. }
  116. void CWindowWithArtifacts::showQuickBackpackWindow(const CGHeroInstance * hero, const ArtifactPosition & slot,
  117. const Point & cursorPosition) const
  118. {
  119. if(!settings["general"]["enableUiEnhancements"].Bool())
  120. return;
  121. GH.windows().createAndPushWindow<CHeroQuickBackpackWindow>(hero, slot);
  122. auto backpackWindow = GH.windows().topWindow<CHeroQuickBackpackWindow>();
  123. backpackWindow->moveTo(cursorPosition - Point(1, 1));
  124. backpackWindow->fitToScreen(15);
  125. }
  126. void CWindowWithArtifacts::activate()
  127. {
  128. if(const auto art = getPickedArtifact())
  129. {
  130. markPossibleSlots();
  131. setCursorAnimation(*art);
  132. }
  133. CWindowObject::activate();
  134. }
  135. void CWindowWithArtifacts::deactivate()
  136. {
  137. CCS->curh->dragAndDropCursor(nullptr);
  138. CWindowObject::deactivate();
  139. }
  140. void CWindowWithArtifacts::enableKeyboardShortcuts() const
  141. {
  142. for(auto & artSet : artSets)
  143. artSet->enableKeyboardShortcuts();
  144. }
  145. void CWindowWithArtifacts::update()
  146. {
  147. for(const auto & artSet : artSets)
  148. {
  149. artSet->updateWornSlots();
  150. artSet->updateBackpackSlots();
  151. if(const auto pickedArtInst = getPickedArtifact())
  152. {
  153. markPossibleSlots();
  154. setCursorAnimation(*pickedArtInst);
  155. }
  156. else
  157. {
  158. artSet->unmarkSlots();
  159. CCS->curh->dragAndDropCursor(nullptr);
  160. }
  161. // Make sure the status bar is updated so it does not display old text
  162. if(auto artPlace = artSet->getArtPlace(GH.getCursorPosition()))
  163. artPlace->hover(true);
  164. }
  165. }
  166. void CWindowWithArtifacts::markPossibleSlots() const
  167. {
  168. if(const auto pickedArtInst = getPickedArtifact())
  169. {
  170. for(const auto & artSet : artSets)
  171. {
  172. const auto hero = artSet->getHero();
  173. if(hero == nullptr || !artSet->isActive())
  174. continue;
  175. if(getHeroPickedArtifact() == hero || !std::dynamic_pointer_cast<CArtifactsOfHeroKingdom>(artSet))
  176. artSet->markPossibleSlots(pickedArtInst, hero->tempOwner == LOCPLINT->playerID);
  177. }
  178. }
  179. }
  180. bool CWindowWithArtifacts::checkSpecialArts(const CArtifactInstance & artInst, const CGHeroInstance & hero, bool isTrade) const
  181. {
  182. const auto artId = artInst.getTypeId();
  183. if(artId == ArtifactID::SPELLBOOK)
  184. {
  185. GH.windows().createAndPushWindow<CSpellWindow>(&hero, LOCPLINT, LOCPLINT->battleInt.get());
  186. return false;
  187. }
  188. if(artId == ArtifactID::CATAPULT)
  189. {
  190. // The Catapult must be equipped
  191. LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[312],
  192. std::vector<std::shared_ptr<CComponent>>(1, std::make_shared<CComponent>(ComponentType::ARTIFACT, ArtifactID(ArtifactID::CATAPULT))));
  193. return false;
  194. }
  195. if(isTrade && !artInst.artType->isTradable())
  196. {
  197. LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[21],
  198. std::vector<std::shared_ptr<CComponent>>(1, std::make_shared<CComponent>(ComponentType::ARTIFACT, artId)));
  199. return false;
  200. }
  201. return true;
  202. }
  203. void CWindowWithArtifacts::setCursorAnimation(const CArtifactInstance & artInst) const
  204. {
  205. if(artInst.isScroll() && settings["general"]["enableUiEnhancements"].Bool())
  206. {
  207. assert(artInst.getScrollSpellID().num >= 0);
  208. const auto animation = GH.renderHandler().loadAnimation(AnimationPath::builtin("spellscr"));
  209. animation->load(artInst.getScrollSpellID().num);
  210. CCS->curh->dragAndDropCursor(animation->getImage(artInst.getScrollSpellID().num)->scaleFast(Point(44, 34)));
  211. }
  212. else
  213. {
  214. CCS->curh->dragAndDropCursor(AnimationPath::builtin("artifact"), artInst.artType->getIconIndex());
  215. }
  216. }
  217. void CWindowWithArtifacts::putPickedArtifact(const CGHeroInstance & curHero, const ArtifactPosition & targetSlot) const
  218. {
  219. const auto heroArtOwner = getHeroPickedArtifact();
  220. const auto pickedArt = getPickedArtifact();
  221. auto srcLoc = ArtifactLocation(heroArtOwner->id, ArtifactPosition::TRANSITION_POS);
  222. auto dstLoc = ArtifactLocation(curHero.id, targetSlot);
  223. if(ArtifactUtils::isSlotBackpack(dstLoc.slot))
  224. {
  225. if(pickedArt->artType->isBig())
  226. {
  227. // War machines cannot go to backpack
  228. LOCPLINT->showInfoDialog(boost::str(boost::format(CGI->generaltexth->allTexts[153]) % pickedArt->artType->getNameTranslated()));
  229. }
  230. else
  231. {
  232. if(ArtifactUtils::isBackpackFreeSlots(heroArtOwner))
  233. LOCPLINT->cb->swapArtifacts(srcLoc, dstLoc);
  234. else
  235. LOCPLINT->showInfoDialog(CGI->generaltexth->translate("core.genrltxt.152"));
  236. }
  237. }
  238. // Check if artifact transfer is possible
  239. else if(pickedArt->canBePutAt(&curHero, dstLoc.slot, true) && (!curHero.getArt(targetSlot) || curHero.tempOwner == LOCPLINT->playerID))
  240. {
  241. LOCPLINT->cb->swapArtifacts(srcLoc, dstLoc);
  242. }
  243. }
  244. void CWindowWithArtifacts::onClickPressedCommonArtifact(const CGHeroInstance & curHero, const ArtifactPosition & slot, bool closeWindow)
  245. {
  246. assert(curHero.getArt(slot));
  247. auto srcLoc = ArtifactLocation(curHero.id, slot);
  248. auto dstLoc = ArtifactLocation(curHero.id, ArtifactPosition::TRANSITION_POS);
  249. if(GH.isKeyboardCmdDown())
  250. {
  251. for(const auto & anotherSet : artSets)
  252. {
  253. if(std::dynamic_pointer_cast<CArtifactsOfHeroMain>(anotherSet) && curHero.id != anotherSet->getHero()->id)
  254. {
  255. dstLoc.slot = ArtifactPosition::FIRST_AVAILABLE;
  256. dstLoc.artHolder = anotherSet->getHero()->id;
  257. break;
  258. }
  259. if(const auto heroSetAltar = std::dynamic_pointer_cast<CArtifactsOfHeroAltar>(anotherSet))
  260. {
  261. dstLoc.slot = ArtifactPosition::FIRST_AVAILABLE;
  262. dstLoc.artHolder = heroSetAltar->altarId;
  263. break;
  264. }
  265. }
  266. }
  267. else if(GH.isKeyboardAltDown())
  268. {
  269. const auto artId = curHero.getArt(slot)->getTypeId();
  270. if(ArtifactUtils::isSlotEquipment(slot))
  271. dstLoc.slot = ArtifactUtils::getArtBackpackPosition(&curHero, artId);
  272. else if(ArtifactUtils::isSlotBackpack(slot))
  273. dstLoc.slot = ArtifactUtils::getArtEquippedPosition(&curHero, artId);
  274. }
  275. else if(closeWindow)
  276. {
  277. close();
  278. }
  279. if(dstLoc.slot != ArtifactPosition::PRE_FIRST)
  280. LOCPLINT->cb->swapArtifacts(srcLoc, dstLoc);
  281. }