CArtifactHolder.cpp 7.5 KB

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