CArtifactHolder.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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->id, artifactPos);
  111. src.creature = SlotID::COMMANDER_SLOT_PLACEHOLDER;
  112. ArtifactLocation dst(commanderOwner->id, freeSlot);
  113. if(ourArt->canBePutAt(commanderOwner, freeSlot, true))
  114. {
  115. LOCPLINT->cb->swapArtifacts(src, dst);
  116. setArtifact(nullptr);
  117. parent->redraw();
  118. }
  119. }
  120. }
  121. void CCommanderArtPlace::clickPressed(const Point & cursorPosition)
  122. {
  123. if(ourArt && text.size())
  124. LOCPLINT->showYesNoDialog(CGI->generaltexth->translate("vcmi.commanderWindow.artifactMessage"), [this]() { returnArtToHeroCallback(); }, []() {});
  125. }
  126. void CCommanderArtPlace::showPopupWindow(const Point & cursorPosition)
  127. {
  128. if(ourArt && text.size())
  129. CArtPlace::showPopupWindow(cursorPosition);
  130. }
  131. void CCommanderArtPlace::setArtifact(const CArtifactInstance * art)
  132. {
  133. setInternals(art);
  134. }
  135. CHeroArtPlace::CHeroArtPlace(Point position, const CArtifactInstance * Art)
  136. : CArtPlace(position, Art),
  137. locked(false),
  138. marked(false)
  139. {
  140. createImage();
  141. }
  142. void CHeroArtPlace::lockSlot(bool on)
  143. {
  144. if(locked == on)
  145. return;
  146. locked = on;
  147. if(on)
  148. image->setFrame(ArtifactID::ART_LOCK);
  149. else if(ourArt)
  150. image->setFrame(imageIndex);
  151. else
  152. image->setFrame(0);
  153. }
  154. bool CHeroArtPlace::isLocked()
  155. {
  156. return locked;
  157. }
  158. void CHeroArtPlace::selectSlot(bool on)
  159. {
  160. if(marked == on)
  161. return;
  162. marked = on;
  163. if(on)
  164. selection->enable();
  165. else
  166. selection->disable();
  167. }
  168. bool CHeroArtPlace::isMarked() const
  169. {
  170. return marked;
  171. }
  172. void CHeroArtPlace::clickPressed(const Point & cursorPosition)
  173. {
  174. if(leftClickCallback)
  175. leftClickCallback(*this);
  176. }
  177. void CHeroArtPlace::showPopupWindow(const Point & cursorPosition)
  178. {
  179. if(showPopupCallback)
  180. showPopupCallback(*this);
  181. }
  182. void CHeroArtPlace::showAll(Canvas & to)
  183. {
  184. if(ourArt)
  185. {
  186. CIntObject::showAll(to);
  187. }
  188. if(marked && isActive())
  189. to.drawBorder(pos, Colors::BRIGHT_YELLOW);
  190. }
  191. void CHeroArtPlace::setArtifact(const CArtifactInstance * art)
  192. {
  193. setInternals(art);
  194. if(art)
  195. {
  196. image->setFrame(locked ? static_cast<int>(ArtifactID::ART_LOCK) : imageIndex);
  197. if(locked) // Locks should appear as empty.
  198. hoverText = CGI->generaltexth->allTexts[507];
  199. else
  200. hoverText = boost::str(boost::format(CGI->generaltexth->heroscrn[1]) % ourArt->artType->getNameTranslated());
  201. }
  202. else
  203. {
  204. lockSlot(false);
  205. }
  206. }
  207. void CHeroArtPlace::addCombinedArtInfo(std::map<const CArtifact*, int> & arts)
  208. {
  209. for(const auto & combinedArt : arts)
  210. {
  211. std::string artList;
  212. text += "\n\n";
  213. text += "{" + combinedArt.first->getNameTranslated() + "}";
  214. if(arts.size() == 1)
  215. {
  216. for(const auto part : combinedArt.first->getConstituents())
  217. artList += "\n" + part->getNameTranslated();
  218. }
  219. text += " (" + boost::str(boost::format("%d") % combinedArt.second) + " / " +
  220. boost::str(boost::format("%d") % combinedArt.first->getConstituents().size()) + ")" + artList;
  221. }
  222. }
  223. void CHeroArtPlace::createImage()
  224. {
  225. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  226. si32 imageIndex = 0;
  227. if(locked)
  228. imageIndex = ArtifactID::ART_LOCK;
  229. else if(ourArt)
  230. imageIndex = ourArt->artType->getIconIndex();
  231. image = std::make_shared<CAnimImage>(AnimationPath::builtin("artifact"), imageIndex);
  232. image->disable();
  233. selection = std::make_shared<CAnimImage>(AnimationPath::builtin("artifact"), ArtifactID::ART_SELECTION);
  234. selection->disable();
  235. }
  236. bool ArtifactUtilsClient::askToAssemble(const CGHeroInstance * hero, const ArtifactPosition & slot)
  237. {
  238. assert(hero);
  239. const auto art = hero->getArt(slot);
  240. assert(art);
  241. if(hero->tempOwner != LOCPLINT->playerID)
  242. return false;
  243. auto assemblyPossibilities = ArtifactUtils::assemblyPossibilities(hero, art->getTypeId());
  244. if(!assemblyPossibilities.empty())
  245. {
  246. auto askThread = new boost::thread([hero, art, slot, assemblyPossibilities]() -> void
  247. {
  248. boost::mutex::scoped_lock interfaceLock(GH.interfaceMutex);
  249. for(const auto combinedArt : assemblyPossibilities)
  250. {
  251. bool assembleConfirmed = false;
  252. CFunctionList<void()> onYesHandlers([&assembleConfirmed]() -> void {assembleConfirmed = true; });
  253. onYesHandlers += std::bind(&CCallback::assembleArtifacts, LOCPLINT->cb.get(), hero, slot, true, combinedArt->getId());
  254. LOCPLINT->showArtifactAssemblyDialog(art->artType, combinedArt, onYesHandlers);
  255. LOCPLINT->waitWhileDialog();
  256. if(assembleConfirmed)
  257. break;
  258. }
  259. });
  260. askThread->detach();
  261. return true;
  262. }
  263. return false;
  264. }
  265. bool ArtifactUtilsClient::askToDisassemble(const CGHeroInstance * hero, const ArtifactPosition & slot)
  266. {
  267. assert(hero);
  268. const auto art = hero->getArt(slot);
  269. assert(art);
  270. if(hero->tempOwner != LOCPLINT->playerID)
  271. return false;
  272. if(art->isCombined())
  273. {
  274. if(ArtifactUtils::isSlotBackpack(slot) && !ArtifactUtils::isBackpackFreeSlots(hero, art->artType->getConstituents().size() - 1))
  275. return false;
  276. LOCPLINT->showArtifactAssemblyDialog(
  277. art->artType,
  278. nullptr,
  279. std::bind(&CCallback::assembleArtifacts, LOCPLINT->cb.get(), hero, slot, false, ArtifactID()));
  280. return true;
  281. }
  282. return false;
  283. }