CArtifactHolder.cpp 7.9 KB

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