CArtifactHolder.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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(component.type != ComponentType::SPELL_SCROLL)
  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. component.type = ComponentType::SPELL_SCROLL;
  54. component.subType = spellID;
  55. }
  56. else
  57. {
  58. if(settings["general"]["enableUiEnhancements"].Bool() && component.type != ComponentType::ARTIFACT)
  59. {
  60. image->setScale(Point());
  61. image->setAnimationPath(AnimationPath::builtin("artifact"), imageIndex);
  62. image->moveTo(Point(pos.x, pos.y));
  63. }
  64. component.type = ComponentType::ARTIFACT;
  65. component.subType = artInst->getTypeId();
  66. }
  67. image->enable();
  68. text = artInst->getDescription();
  69. }
  70. CArtPlace::CArtPlace(Point position, const CArtifactInstance * art)
  71. : ourArt(art)
  72. , locked(false)
  73. {
  74. pos += position;
  75. pos.w = pos.h = 44;
  76. OBJECT_CONSTRUCTION_CAPTURING(255 - DISPOSE);
  77. imageIndex = 0;
  78. if(locked)
  79. imageIndex = ArtifactID::ART_LOCK;
  80. else if(ourArt)
  81. imageIndex = ourArt->artType->getIconIndex();
  82. image = std::make_shared<CAnimImage>(AnimationPath::builtin("artifact"), imageIndex);
  83. image->disable();
  84. selection = std::make_shared<CAnimImage>(AnimationPath::builtin("artifact"), ArtifactID::ART_SELECTION, 0, -1, -1);
  85. selection->visible = false;
  86. }
  87. const CArtifactInstance * CArtPlace::getArt()
  88. {
  89. return ourArt;
  90. }
  91. CCommanderArtPlace::CCommanderArtPlace(Point position, const CGHeroInstance * commanderOwner, ArtifactPosition artSlot, const CArtifactInstance * art)
  92. : CArtPlace(position, art),
  93. commanderOwner(commanderOwner),
  94. commanderSlotID(artSlot.num)
  95. {
  96. setArtifact(art);
  97. }
  98. void CCommanderArtPlace::returnArtToHeroCallback()
  99. {
  100. ArtifactPosition artifactPos = commanderSlotID;
  101. ArtifactPosition freeSlot = ArtifactUtils::getArtBackpackPosition(commanderOwner, ourArt->getTypeId());
  102. if(freeSlot == ArtifactPosition::PRE_FIRST)
  103. {
  104. LOCPLINT->showInfoDialog(CGI->generaltexth->translate("core.genrltxt.152"));
  105. }
  106. else
  107. {
  108. ArtifactLocation src(commanderOwner->id, artifactPos);
  109. src.creature = SlotID::COMMANDER_SLOT_PLACEHOLDER;
  110. ArtifactLocation dst(commanderOwner->id, freeSlot);
  111. if(ourArt->canBePutAt(commanderOwner, freeSlot, true))
  112. {
  113. LOCPLINT->cb->swapArtifacts(src, dst);
  114. setArtifact(nullptr);
  115. parent->redraw();
  116. }
  117. }
  118. }
  119. void CCommanderArtPlace::clickPressed(const Point & cursorPosition)
  120. {
  121. if(ourArt && text.size())
  122. LOCPLINT->showYesNoDialog(CGI->generaltexth->translate("vcmi.commanderWindow.artifactMessage"), [this]() { returnArtToHeroCallback(); }, []() {});
  123. }
  124. void CCommanderArtPlace::showPopupWindow(const Point & cursorPosition)
  125. {
  126. if(ourArt && text.size())
  127. CArtPlace::showPopupWindow(cursorPosition);
  128. }
  129. CHeroArtPlace::CHeroArtPlace(Point position, const CArtifactInstance * art)
  130. : CArtPlace(position, art)
  131. {
  132. }
  133. void CArtPlace::lockSlot(bool on)
  134. {
  135. if(locked == on)
  136. return;
  137. locked = on;
  138. if(on)
  139. image->setFrame(ArtifactID::ART_LOCK);
  140. else if(ourArt)
  141. image->setFrame(imageIndex);
  142. else
  143. image->setFrame(0);
  144. }
  145. bool CArtPlace::isLocked() const
  146. {
  147. return locked;
  148. }
  149. void CArtPlace::selectSlot(bool on)
  150. {
  151. selection->visible = on;
  152. }
  153. bool CArtPlace::isSelected() const
  154. {
  155. return selection->visible;
  156. }
  157. void CHeroArtPlace::clickPressed(const Point & cursorPosition)
  158. {
  159. if(leftClickCallback)
  160. leftClickCallback(*this);
  161. }
  162. void CHeroArtPlace::showPopupWindow(const Point & cursorPosition)
  163. {
  164. if(showPopupCallback)
  165. showPopupCallback(*this);
  166. }
  167. void CArtPlace::showAll(Canvas & to)
  168. {
  169. CIntObject::showAll(to);
  170. selection->showAll(to);
  171. }
  172. void CArtPlace::setArtifact(const CArtifactInstance * art)
  173. {
  174. setInternals(art);
  175. if(art)
  176. {
  177. image->setFrame(locked ? static_cast<int>(ArtifactID::ART_LOCK) : imageIndex);
  178. if(locked) // Locks should appear as empty.
  179. hoverText = CGI->generaltexth->allTexts[507];
  180. else
  181. hoverText = boost::str(boost::format(CGI->generaltexth->heroscrn[1]) % ourArt->artType->getNameTranslated());
  182. }
  183. else
  184. {
  185. lockSlot(false);
  186. }
  187. }
  188. void CHeroArtPlace::addCombinedArtInfo(std::map<const CArtifact*, int> & arts)
  189. {
  190. for(const auto & combinedArt : arts)
  191. {
  192. std::string artList;
  193. text += "\n\n";
  194. text += "{" + combinedArt.first->getNameTranslated() + "}";
  195. if(arts.size() == 1)
  196. {
  197. for(const auto part : combinedArt.first->getConstituents())
  198. artList += "\n" + part->getNameTranslated();
  199. }
  200. text += " (" + boost::str(boost::format("%d") % combinedArt.second) + " / " +
  201. boost::str(boost::format("%d") % combinedArt.first->getConstituents().size()) + ")" + artList;
  202. }
  203. }
  204. bool ArtifactUtilsClient::askToAssemble(const CGHeroInstance * hero, const ArtifactPosition & slot)
  205. {
  206. assert(hero);
  207. const auto art = hero->getArt(slot);
  208. assert(art);
  209. if(hero->tempOwner != LOCPLINT->playerID)
  210. return false;
  211. auto assemblyPossibilities = ArtifactUtils::assemblyPossibilities(hero, art->getTypeId());
  212. if(!assemblyPossibilities.empty())
  213. {
  214. auto askThread = new boost::thread([hero, art, slot, assemblyPossibilities]() -> void
  215. {
  216. boost::mutex::scoped_lock interfaceLock(GH.interfaceMutex);
  217. for(const auto combinedArt : assemblyPossibilities)
  218. {
  219. bool assembleConfirmed = false;
  220. CFunctionList<void()> onYesHandlers([&assembleConfirmed]() -> void {assembleConfirmed = true; });
  221. onYesHandlers += std::bind(&CCallback::assembleArtifacts, LOCPLINT->cb.get(), hero, slot, true, combinedArt->getId());
  222. LOCPLINT->showArtifactAssemblyDialog(art->artType, combinedArt, onYesHandlers);
  223. LOCPLINT->waitWhileDialog();
  224. if(assembleConfirmed)
  225. break;
  226. }
  227. });
  228. askThread->detach();
  229. return true;
  230. }
  231. return false;
  232. }
  233. bool ArtifactUtilsClient::askToDisassemble(const CGHeroInstance * hero, const ArtifactPosition & slot)
  234. {
  235. assert(hero);
  236. const auto art = hero->getArt(slot);
  237. assert(art);
  238. if(hero->tempOwner != LOCPLINT->playerID)
  239. return false;
  240. if(art->isCombined())
  241. {
  242. if(ArtifactUtils::isSlotBackpack(slot) && !ArtifactUtils::isBackpackFreeSlots(hero, art->artType->getConstituents().size() - 1))
  243. return false;
  244. LOCPLINT->showArtifactAssemblyDialog(
  245. art->artType,
  246. nullptr,
  247. std::bind(&CCallback::assembleArtifacts, LOCPLINT->cb.get(), hero, slot, false, ArtifactID()));
  248. return true;
  249. }
  250. return false;
  251. }