CArtifactHolder.cpp 7.5 KB

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