CWindowWithArtifacts.cpp 9.3 KB

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