CWindowWithArtifacts.cpp 9.2 KB

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