CWindowWithArtifacts.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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 "CHeroBackpackWindow.h"
  15. #include "../GameEngine.h"
  16. #include "../GameInstance.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 "../../lib/CConfigHandler.h"
  25. #include "../../lib/GameLibrary.h"
  26. #include "../../lib/entities/artifact/ArtifactUtils.h"
  27. #include "../../lib/entities/artifact/CArtifact.h"
  28. #include "../../lib/mapObjects/CGHeroInstance.h"
  29. #include "../../lib/networkPacks/ArtifactLocation.h"
  30. #include "../../lib/texts/CGeneralTextHandler.h"
  31. #include "../../CCallback.h"
  32. CWindowWithArtifacts::CWindowWithArtifacts(const std::vector<CArtifactsOfHeroPtr> * artSets)
  33. {
  34. if(artSets)
  35. this->artSets.insert(this->artSets.end(), artSets->begin(), artSets->end());
  36. }
  37. void CWindowWithArtifacts::addSet(const std::shared_ptr<CArtifactsOfHeroBase> & newArtSet)
  38. {
  39. artSets.emplace_back(newArtSet);
  40. }
  41. const CGHeroInstance * CWindowWithArtifacts::getHeroPickedArtifact() const
  42. {
  43. const CGHeroInstance * hero = nullptr;
  44. for(const auto & artSet : artSets)
  45. if(const auto pickedArt = artSet->getHero()->getArt(ArtifactPosition::TRANSITION_POS))
  46. {
  47. hero = artSet->getHero();
  48. break;
  49. }
  50. return hero;
  51. }
  52. const CArtifactInstance * CWindowWithArtifacts::getPickedArtifact() const
  53. {
  54. for(const auto & artSet : artSets)
  55. if(const auto pickedArt = artSet->getHero()->getArt(ArtifactPosition::TRANSITION_POS))
  56. {
  57. return pickedArt;
  58. }
  59. return nullptr;
  60. }
  61. void CWindowWithArtifacts::clickPressedOnArtPlace(const CGHeroInstance * hero, const ArtifactPosition & slot,
  62. bool allowExchange, bool altarTrading, bool closeWindow, const Point & cursorPosition)
  63. {
  64. if(!GAME->interface()->makingTurn)
  65. return;
  66. if(hero == nullptr)
  67. return;
  68. if(const auto heroArtOwner = getHeroPickedArtifact())
  69. {
  70. if(allowExchange || hero->id == heroArtOwner->id)
  71. putPickedArtifact(*hero, slot);
  72. }
  73. else if(ENGINE->isKeyboardShiftDown())
  74. {
  75. showQuickBackpackWindow(hero, slot, cursorPosition);
  76. }
  77. else if(auto art = hero->getArt(slot))
  78. {
  79. if(hero->getOwner() == GAME->interface()->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. GAME->interface()->showInfoDialog(LIBRARY->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. GAME->interface()->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(GAME->interface()->artifactController->askToDisassemble(artsInst.getHero(), artPlace.slot))
  107. return;
  108. if(GAME->interface()->artifactController->askToAssemble(artsInst.getHero(), artPlace.slot))
  109. return;
  110. if(artPlace.text.size())
  111. artPlace.LRClickableAreaWTextComp::showPopupWindow(cursorPosition);
  112. }
  113. }
  114. void CWindowWithArtifacts::showQuickBackpackWindow(const CGHeroInstance * hero, const ArtifactPosition & slot,
  115. const Point & cursorPosition) const
  116. {
  117. if(!settings["general"]["enableUiEnhancements"].Bool())
  118. return;
  119. if(!ArtifactUtils::isSlotEquipment(slot))
  120. return;
  121. ENGINE->windows().createAndPushWindow<CHeroQuickBackpackWindow>(hero, slot);
  122. auto backpackWindow = ENGINE->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. ENGINE->cursor().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. ENGINE->cursor().dragAndDropCursor(nullptr);
  160. }
  161. // Make sure the status bar is updated so it does not display old text
  162. if(auto artPlace = artSet->getArtPlace(ENGINE->getCursorPosition()))
  163. artPlace->hover(true);
  164. }
  165. redraw();
  166. }
  167. void CWindowWithArtifacts::markPossibleSlots() const
  168. {
  169. if(const auto pickedArtInst = getPickedArtifact())
  170. {
  171. for(const auto & artSet : artSets)
  172. {
  173. const auto hero = artSet->getHero();
  174. if(hero == nullptr || !artSet->isActive())
  175. continue;
  176. if(getHeroPickedArtifact() == hero || !std::dynamic_pointer_cast<CArtifactsOfHeroKingdom>(artSet))
  177. artSet->markPossibleSlots(pickedArtInst->getType(), hero->tempOwner == GAME->interface()->playerID);
  178. }
  179. }
  180. }
  181. bool CWindowWithArtifacts::checkSpecialArts(const CArtifactInstance & artInst, const CGHeroInstance & hero, bool isTrade) const
  182. {
  183. const auto artId = artInst.getTypeId();
  184. if(artId == ArtifactID::SPELLBOOK)
  185. {
  186. ENGINE->windows().createAndPushWindow<CSpellWindow>(&hero, GAME->interface(), GAME->interface()->battleInt.get());
  187. return false;
  188. }
  189. if(artId == ArtifactID::CATAPULT)
  190. {
  191. // The Catapult must be equipped
  192. GAME->interface()->showInfoDialog(LIBRARY->generaltexth->allTexts[312],
  193. std::vector<std::shared_ptr<CComponent>>(1, std::make_shared<CComponent>(ComponentType::ARTIFACT, ArtifactID(ArtifactID::CATAPULT))));
  194. return false;
  195. }
  196. if(isTrade && !artInst.getType()->isTradable())
  197. {
  198. GAME->interface()->showInfoDialog(LIBRARY->generaltexth->allTexts[21],
  199. std::vector<std::shared_ptr<CComponent>>(1, std::make_shared<CComponent>(ComponentType::ARTIFACT, artId)));
  200. return false;
  201. }
  202. return true;
  203. }
  204. void CWindowWithArtifacts::setCursorAnimation(const CArtifactInstance & artInst) const
  205. {
  206. if(artInst.isScroll() && settings["general"]["enableUiEnhancements"].Bool())
  207. {
  208. assert(artInst.getScrollSpellID().num >= 0);
  209. auto image = ENGINE->renderHandler().loadImage(AnimationPath::builtin("spellscr"), artInst.getScrollSpellID().num, 0, EImageBlitMode::COLORKEY);
  210. image->scaleTo(Point(44,34), EScalingAlgorithm::BILINEAR);
  211. ENGINE->cursor().dragAndDropCursor(image);
  212. }
  213. else
  214. {
  215. ENGINE->cursor().dragAndDropCursor(AnimationPath::builtin("artifact"), artInst.getType()->getIconIndex());
  216. }
  217. }
  218. void CWindowWithArtifacts::putPickedArtifact(const CGHeroInstance & curHero, const ArtifactPosition & targetSlot) const
  219. {
  220. const auto heroArtOwner = getHeroPickedArtifact();
  221. const auto pickedArt = getPickedArtifact();
  222. auto srcLoc = ArtifactLocation(heroArtOwner->id, ArtifactPosition::TRANSITION_POS);
  223. auto dstLoc = ArtifactLocation(curHero.id, targetSlot);
  224. if(ArtifactUtils::isSlotBackpack(dstLoc.slot))
  225. {
  226. if(pickedArt->getType()->isBig())
  227. {
  228. // War machines cannot go to backpack
  229. GAME->interface()->showInfoDialog(boost::str(boost::format(LIBRARY->generaltexth->allTexts[153]) % pickedArt->getType()->getNameTranslated()));
  230. }
  231. else
  232. {
  233. if(ArtifactUtils::isBackpackFreeSlots(heroArtOwner))
  234. GAME->interface()->cb->swapArtifacts(srcLoc, dstLoc);
  235. else
  236. GAME->interface()->showInfoDialog(LIBRARY->generaltexth->translate("core.genrltxt.152"));
  237. }
  238. }
  239. // Check if artifact transfer is possible
  240. else if(pickedArt->canBePutAt(&curHero, dstLoc.slot, true) && (!curHero.getArt(targetSlot) || curHero.tempOwner == GAME->interface()->playerID))
  241. {
  242. GAME->interface()->cb->swapArtifacts(srcLoc, dstLoc);
  243. }
  244. }
  245. void CWindowWithArtifacts::onClickPressedCommonArtifact(const CGHeroInstance & curHero, const ArtifactPosition & slot, bool closeWindow)
  246. {
  247. assert(curHero.getArt(slot));
  248. auto srcLoc = ArtifactLocation(curHero.id, slot);
  249. auto dstLoc = ArtifactLocation(curHero.id, ArtifactPosition::TRANSITION_POS);
  250. if(ENGINE->isKeyboardCmdDown())
  251. {
  252. for(const auto & anotherSet : artSets)
  253. {
  254. if(std::dynamic_pointer_cast<CArtifactsOfHeroMain>(anotherSet) && curHero.id != anotherSet->getHero()->id)
  255. {
  256. dstLoc.slot = ArtifactPosition::FIRST_AVAILABLE;
  257. dstLoc.artHolder = anotherSet->getHero()->id;
  258. break;
  259. }
  260. if(const auto heroSetAltar = std::dynamic_pointer_cast<CArtifactsOfHeroAltar>(anotherSet))
  261. {
  262. dstLoc.slot = ArtifactPosition::FIRST_AVAILABLE;
  263. dstLoc.artHolder = heroSetAltar->altarId;
  264. break;
  265. }
  266. }
  267. }
  268. else if(ENGINE->isKeyboardAltDown())
  269. {
  270. const auto artId = curHero.getArt(slot)->getTypeId();
  271. if(ArtifactUtils::isSlotEquipment(slot))
  272. dstLoc.slot = ArtifactUtils::getArtBackpackPosition(&curHero, artId);
  273. else if(ArtifactUtils::isSlotBackpack(slot))
  274. dstLoc.slot = ArtifactUtils::getArtEquippedPosition(&curHero, artId);
  275. }
  276. else if(closeWindow)
  277. {
  278. close();
  279. }
  280. if(dstLoc.slot != ArtifactPosition::PRE_FIRST)
  281. GAME->interface()->cb->swapArtifacts(srcLoc, dstLoc);
  282. }