CArtifactHolder.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /*
  2. * CArtifactHolder.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 "CArtifactHolder.h"
  12. #include "../gui/CGuiHandler.h"
  13. #include "../gui/Shortcut.h"
  14. #include "CComponent.h"
  15. #include "../windows/GUIClasses.h"
  16. #include "../render/Canvas.h"
  17. #include "../render/Colors.h"
  18. #include "../CPlayerInterface.h"
  19. #include "../CGameInfo.h"
  20. #include "../../CCallback.h"
  21. #include "../../lib/CGeneralTextHandler.h"
  22. #include "../../lib/ArtifactUtils.h"
  23. #include "../../lib/mapObjects/CGHeroInstance.h"
  24. void CArtPlace::setInternals(const CArtifactInstance * artInst)
  25. {
  26. baseType = -1; // By default we don't store any component
  27. ourArt = artInst;
  28. if(!artInst)
  29. {
  30. image->disable();
  31. text.clear();
  32. hoverText = CGI->generaltexth->allTexts[507];
  33. return;
  34. }
  35. image->enable();
  36. image->setFrame(artInst->artType->getIconIndex());
  37. if(artInst->getTypeId() == ArtifactID::SPELL_SCROLL)
  38. {
  39. auto spellID = artInst->getScrollSpellID();
  40. if(spellID.num >= 0)
  41. {
  42. // Add spell component info (used to provide a pic in r-click popup)
  43. baseType = CComponent::spell;
  44. type = spellID;
  45. bonusValue = 0;
  46. }
  47. }
  48. else
  49. {
  50. baseType = CComponent::artifact;
  51. type = artInst->getTypeId();
  52. bonusValue = 0;
  53. }
  54. text = artInst->getDescription();
  55. }
  56. CArtPlace::CArtPlace(Point position, const CArtifactInstance * Art)
  57. : ourArt(Art)
  58. {
  59. image = nullptr;
  60. pos += position;
  61. pos.w = pos.h = 44;
  62. }
  63. void CArtPlace::clickLeft(tribool down, bool previousState)
  64. {
  65. LRClickableAreaWTextComp::clickLeft(down, previousState);
  66. }
  67. void CArtPlace::clickRight(tribool down, bool previousState)
  68. {
  69. LRClickableAreaWTextComp::clickRight(down, previousState);
  70. }
  71. const CArtifactInstance * CArtPlace::getArt()
  72. {
  73. return ourArt;
  74. }
  75. CCommanderArtPlace::CCommanderArtPlace(Point position, const CGHeroInstance * commanderOwner, ArtifactPosition artSlot, const CArtifactInstance * Art)
  76. : CArtPlace(position, Art),
  77. commanderOwner(commanderOwner),
  78. commanderSlotID(artSlot.num)
  79. {
  80. createImage();
  81. setArtifact(Art);
  82. }
  83. void CCommanderArtPlace::createImage()
  84. {
  85. OBJECT_CONSTRUCTION_CAPTURING(255 - DISPOSE);
  86. int imageIndex = 0;
  87. if(ourArt)
  88. imageIndex = ourArt->artType->getIconIndex();
  89. image = std::make_shared<CAnimImage>("artifact", imageIndex);
  90. if(!ourArt)
  91. image->disable();
  92. }
  93. void CCommanderArtPlace::returnArtToHeroCallback()
  94. {
  95. ArtifactPosition artifactPos = commanderSlotID;
  96. ArtifactPosition freeSlot = ArtifactUtils::getArtBackpackPosition(commanderOwner, ourArt->getTypeId());
  97. if(freeSlot == ArtifactPosition::PRE_FIRST)
  98. {
  99. LOCPLINT->showInfoDialog(CGI->generaltexth->translate("core.genrltxt.152"));
  100. }
  101. else
  102. {
  103. ArtifactLocation src(commanderOwner->commander.get(), artifactPos);
  104. ArtifactLocation dst(commanderOwner, freeSlot);
  105. if(ourArt->canBePutAt(dst, true))
  106. {
  107. LOCPLINT->cb->swapArtifacts(src, dst);
  108. setArtifact(nullptr);
  109. parent->redraw();
  110. }
  111. }
  112. }
  113. void CCommanderArtPlace::clickLeft(tribool down, bool previousState)
  114. {
  115. if(ourArt && text.size() && down)
  116. LOCPLINT->showYesNoDialog(CGI->generaltexth->translate("vcmi.commanderWindow.artifactMessage"), [this]() { returnArtToHeroCallback(); }, []() {});
  117. }
  118. void CCommanderArtPlace::clickRight(tribool down, bool previousState)
  119. {
  120. if(ourArt && text.size() && down)
  121. CArtPlace::clickRight(down, previousState);
  122. }
  123. void CCommanderArtPlace::setArtifact(const CArtifactInstance * art)
  124. {
  125. setInternals(art);
  126. }
  127. CHeroArtPlace::CHeroArtPlace(Point position, const CArtifactInstance * Art)
  128. : CArtPlace(position, Art),
  129. locked(false),
  130. marked(false)
  131. {
  132. createImage();
  133. }
  134. void CHeroArtPlace::lockSlot(bool on)
  135. {
  136. if(locked == on)
  137. return;
  138. locked = on;
  139. if(on)
  140. image->setFrame(ArtifactID::ART_LOCK);
  141. else if(ourArt)
  142. image->setFrame(ourArt->artType->getIconIndex());
  143. else
  144. image->setFrame(0);
  145. }
  146. bool CHeroArtPlace::isLocked()
  147. {
  148. return locked;
  149. }
  150. void CHeroArtPlace::selectSlot(bool on)
  151. {
  152. if(marked == on)
  153. return;
  154. marked = on;
  155. if(on)
  156. selection->enable();
  157. else
  158. selection->disable();
  159. }
  160. bool CHeroArtPlace::isMarked() const
  161. {
  162. return marked;
  163. }
  164. void CHeroArtPlace::clickLeft(tribool down, bool previousState)
  165. {
  166. if(down || !previousState)
  167. return;
  168. if(leftClickCallback)
  169. leftClickCallback(*this);
  170. }
  171. void CHeroArtPlace::clickRight(tribool down, bool previousState)
  172. {
  173. if(down)
  174. {
  175. if(rightClickCallback)
  176. rightClickCallback(*this);
  177. }
  178. }
  179. void CHeroArtPlace::showAll(Canvas & to)
  180. {
  181. if(ourArt)
  182. {
  183. CIntObject::showAll(to);
  184. }
  185. if(marked && isActive())
  186. to.drawBorder(pos, Colors::BRIGHT_YELLOW);
  187. }
  188. void CHeroArtPlace::setArtifact(const CArtifactInstance * art)
  189. {
  190. setInternals(art);
  191. if(art)
  192. {
  193. image->setFrame(locked ? ArtifactID::ART_LOCK : art->artType->getIconIndex());
  194. if(locked) // Locks should appear as empty.
  195. hoverText = CGI->generaltexth->allTexts[507];
  196. else
  197. hoverText = boost::str(boost::format(CGI->generaltexth->heroscrn[1]) % ourArt->artType->getNameTranslated());
  198. }
  199. else
  200. {
  201. lockSlot(false);
  202. }
  203. }
  204. void CHeroArtPlace::addCombinedArtInfo(std::map<const CArtifact*, int> & arts)
  205. {
  206. for(const auto & combinedArt : arts)
  207. {
  208. std::string artList;
  209. text += "\n\n";
  210. text += "{" + combinedArt.first->getNameTranslated() + "}";
  211. if(arts.size() == 1)
  212. {
  213. for(const auto part : *combinedArt.first->constituents)
  214. artList += "\n" + part->getNameTranslated();
  215. }
  216. text += " (" + boost::str(boost::format("%d") % combinedArt.second) + " / " +
  217. boost::str(boost::format("%d") % combinedArt.first->constituents->size()) + ")" + artList;
  218. }
  219. }
  220. void CHeroArtPlace::createImage()
  221. {
  222. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  223. si32 imageIndex = 0;
  224. if(locked)
  225. imageIndex = ArtifactID::ART_LOCK;
  226. else if(ourArt)
  227. imageIndex = ourArt->artType->getIconIndex();
  228. image = std::make_shared<CAnimImage>("artifact", imageIndex);
  229. if(!ourArt)
  230. image->disable();
  231. selection = std::make_shared<CAnimImage>("artifact", ArtifactID::ART_SELECTION);
  232. selection->disable();
  233. }
  234. bool ArtifactUtilsClient::askToAssemble(const CGHeroInstance * hero, const ArtifactPosition & slot)
  235. {
  236. assert(hero);
  237. const auto art = hero->getArt(slot);
  238. assert(art);
  239. auto assemblyPossibilities = ArtifactUtils::assemblyPossibilities(hero, art->getTypeId(), ArtifactUtils::isSlotEquipment(slot));
  240. for(const auto combinedArt : assemblyPossibilities)
  241. {
  242. LOCPLINT->showArtifactAssemblyDialog(
  243. art->artType,
  244. combinedArt,
  245. std::bind(&CCallback::assembleArtifacts, LOCPLINT->cb.get(), hero, slot, true, combinedArt->getId()));
  246. if(assemblyPossibilities.size() > 2)
  247. logGlobal->warn("More than one possibility of assembling on %s... taking only first", art->artType->getNameTranslated());
  248. return true;
  249. }
  250. return false;
  251. }
  252. bool ArtifactUtilsClient::askToDisassemble(const CGHeroInstance * hero, const ArtifactPosition & slot)
  253. {
  254. assert(hero);
  255. const auto art = hero->getArt(slot);
  256. assert(art);
  257. if(art->canBeDisassembled())
  258. {
  259. if(ArtifactUtils::isSlotBackpack(slot) && !ArtifactUtils::isBackpackFreeSlots(hero, art->artType->constituents->size() - 1))
  260. return false;
  261. LOCPLINT->showArtifactAssemblyDialog(
  262. art->artType,
  263. nullptr,
  264. std::bind(&CCallback::assembleArtifacts, LOCPLINT->cb.get(), hero, slot, false, ArtifactID()));
  265. return true;
  266. }
  267. return false;
  268. }