CArtifactHolder.cpp 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082
  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/CursorHandler.h"
  14. #include "Buttons.h"
  15. #include "CComponent.h"
  16. #include "../windows/CHeroWindow.h"
  17. #include "../windows/CSpellWindow.h"
  18. #include "../windows/GUIClasses.h"
  19. #include "../renderSDL/SDL_Extensions.h"
  20. #include "../CPlayerInterface.h"
  21. #include "../CGameInfo.h"
  22. #include "../../CCallback.h"
  23. #include "../../lib/CArtHandler.h"
  24. #include "../../lib/CGeneralTextHandler.h"
  25. #include "../../lib/mapObjects/CGHeroInstance.h"
  26. CHeroArtPlace::CHeroArtPlace(Point position, const CArtifactInstance * Art)
  27. : CArtPlace(position, Art),
  28. locked(false),
  29. picked(false),
  30. marked(false),
  31. ourOwner(nullptr)
  32. {
  33. createImage();
  34. }
  35. void CHeroArtPlace::createImage()
  36. {
  37. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  38. si32 imageIndex = 0;
  39. if(locked)
  40. imageIndex = ArtifactID::ART_LOCK;
  41. else if(ourArt)
  42. imageIndex = ourArt->artType->getIconIndex();
  43. image = std::make_shared<CAnimImage>("artifact", imageIndex);
  44. if(!ourArt)
  45. image->disable();
  46. selection = std::make_shared<CAnimImage>("artifact", ArtifactID::ART_SELECTION);
  47. selection->disable();
  48. }
  49. void CHeroArtPlace::lockSlot(bool on)
  50. {
  51. if (locked == on)
  52. return;
  53. locked = on;
  54. if (on)
  55. image->setFrame(ArtifactID::ART_LOCK);
  56. else if (ourArt)
  57. image->setFrame(ourArt->artType->getIconIndex());
  58. else
  59. image->setFrame(0);
  60. }
  61. void CHeroArtPlace::pickSlot(bool on)
  62. {
  63. if (picked == on)
  64. return;
  65. picked = on;
  66. if (on)
  67. image->disable();
  68. else
  69. image->enable();
  70. }
  71. void CHeroArtPlace::selectSlot(bool on)
  72. {
  73. if (marked == on)
  74. return;
  75. marked = on;
  76. if (on)
  77. selection->enable();
  78. else
  79. selection->disable();
  80. }
  81. void CHeroArtPlace::clickLeft(tribool down, bool previousState)
  82. {
  83. //LRClickableAreaWTextComp::clickLeft(down);
  84. bool inBackpack = ArtifactUtils::isSlotBackpack(slotID);
  85. bool srcInBackpack = ArtifactUtils::isSlotBackpack(ourOwner->commonInfo->src.slotID);
  86. bool srcInSameHero = ourOwner->commonInfo->src.AOH == ourOwner;
  87. if(ourOwner->highlightModeCallback && ourArt)
  88. {
  89. if(down)
  90. {
  91. if(!ourArt->artType->isTradable()) //War Machine or Spellbook
  92. {
  93. LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[21]); //This item can't be traded.
  94. }
  95. else
  96. {
  97. ourOwner->unmarkSlots(false);
  98. selectSlot(true);
  99. ourOwner->highlightModeCallback(this);
  100. }
  101. }
  102. return;
  103. }
  104. // If clicked on spellbook, open it only if no artifact is held at the moment.
  105. if(ourArt && !down && previousState && !ourOwner->commonInfo->src.AOH)
  106. {
  107. if(ourArt->artType->getId() == ArtifactID::SPELLBOOK)
  108. GH.pushIntT<CSpellWindow>(ourOwner->curHero, LOCPLINT, LOCPLINT->battleInt.get());
  109. }
  110. if (!down && previousState)
  111. {
  112. if(ourArt && ourArt->artType->getId() == ArtifactID::SPELLBOOK)
  113. return; //this is handled separately
  114. if(!ourOwner->commonInfo->src.AOH) //nothing has been clicked
  115. {
  116. if(ourArt //to prevent selecting empty slots (bugfix to what GrayFace reported)
  117. && ourOwner->curHero->tempOwner == LOCPLINT->playerID)//can't take art from another player
  118. {
  119. if(ourArt->artType->getId() == ArtifactID::CATAPULT) //catapult cannot be highlighted
  120. {
  121. std::vector<std::shared_ptr<CComponent>> catapult(1, std::make_shared<CComponent>(CComponent::artifact, 3, 0));
  122. LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[312], catapult); //The Catapult must be equipped.
  123. return;
  124. }
  125. select();
  126. }
  127. }
  128. // Perform artifact transition
  129. else if(ourArt != ourOwner->commonInfo->src.art)
  130. {
  131. if(inBackpack) // Backpack destination.
  132. {
  133. if(!srcInBackpack || slotID != ourOwner->commonInfo->src.slotID + 1)
  134. {
  135. const CArtifact * const cur = ourOwner->commonInfo->src.art->artType;
  136. if(cur->getId() == ArtifactID::CATAPULT)
  137. {
  138. //should not happen, catapult cannot be selected
  139. logGlobal->error("Attempt to move Catapult");
  140. }
  141. else if(cur->isBig())
  142. {
  143. //war machines cannot go to backpack
  144. LOCPLINT->showInfoDialog(boost::str(boost::format(CGI->generaltexth->allTexts[153]) % cur->getNameTranslated()));
  145. }
  146. else
  147. {
  148. setMeAsDest();
  149. vstd::amin(ourOwner->commonInfo->dst.slotID, ourOwner->curHero->artifactsInBackpack.size() + GameConstants::BACKPACK_START);
  150. if(ArtifactUtils::isBackpackFreeSlots(ourOwner->curHero))
  151. {
  152. if(!srcInSameHero || ourOwner->commonInfo->dst.slotID != ourOwner->commonInfo->src.slotID)
  153. ourOwner->realizeCurrentTransaction();
  154. }
  155. else
  156. {
  157. LOCPLINT->showInfoDialog(CGI->generaltexth->translate("core.genrltxt.152"));
  158. }
  159. }
  160. }
  161. }
  162. //check if swap is possible
  163. else if (fitsHere(ourOwner->commonInfo->src.art) &&
  164. (!ourArt || ourOwner->curHero->tempOwner == LOCPLINT->playerID))
  165. {
  166. setMeAsDest();
  167. ourOwner->realizeCurrentTransaction();
  168. }
  169. }
  170. }
  171. }
  172. bool CHeroArtPlace::askToAssemble(const CGHeroInstance * hero, ArtifactPosition slot)
  173. {
  174. assert(hero);
  175. const auto art = hero->getArt(slot);
  176. assert(art);
  177. auto assemblyPossibilities = ArtifactUtils::assemblyPossibilities(hero, art->getTypeId(), ArtifactUtils::isSlotEquipment(slot));
  178. // If the artifact can be assembled, display dialog.
  179. for(const auto * combination : assemblyPossibilities)
  180. {
  181. LOCPLINT->showArtifactAssemblyDialog(
  182. art->artType,
  183. combination,
  184. std::bind(&CCallback::assembleArtifacts, LOCPLINT->cb.get(), hero, slot, true, combination->getId()));
  185. if(assemblyPossibilities.size() > 2)
  186. logGlobal->warn("More than one possibility of assembling on %s... taking only first", art->artType->getNameTranslated());
  187. return true;
  188. }
  189. return false;
  190. }
  191. bool CHeroArtPlace::askToDisassemble(const CGHeroInstance * hero, ArtifactPosition slot)
  192. {
  193. assert(hero);
  194. const auto art = hero->getArt(slot);
  195. assert(art);
  196. if(art->canBeDisassembled())
  197. {
  198. if(ArtifactUtils::isSlotBackpack(slot) && !ArtifactUtils::isBackpackFreeSlots(hero, art->artType->constituents->size() - 1))
  199. return false;
  200. LOCPLINT->showArtifactAssemblyDialog(
  201. art->artType,
  202. nullptr,
  203. std::bind(&CCallback::assembleArtifacts, LOCPLINT->cb.get(), hero, slot, false, ArtifactID()));
  204. return true;
  205. }
  206. return false;
  207. }
  208. void CHeroArtPlace::clickRight(tribool down, bool previousState)
  209. {
  210. if(ourArt && down && !locked && text.size() && !picked) //if there is no description or it's a lock, do nothing ;]
  211. {
  212. if(ourOwner->allowedAssembling)
  213. {
  214. // If the artifact can be assembled, display dialog.
  215. if(askToAssemble(ourOwner->curHero, slotID))
  216. {
  217. return;
  218. }
  219. if(askToDisassemble(ourOwner->curHero, slotID))
  220. {
  221. return;
  222. }
  223. }
  224. // Lastly just show the artifact description.
  225. LRClickableAreaWTextComp::clickRight(down, previousState);
  226. }
  227. }
  228. void CArtifactsOfHero::activate()
  229. {
  230. if (commonInfo->src.AOH == this && commonInfo->src.art)
  231. CCS->curh->dragAndDropCursor("artifact", commonInfo->src.art->artType->getIconIndex());
  232. CIntObject::activate();
  233. }
  234. void CArtifactsOfHero::deactivate()
  235. {
  236. if (commonInfo->src.AOH == this && commonInfo->src.art)
  237. CCS->curh->dragAndDropCursor(nullptr);
  238. CIntObject::deactivate();
  239. }
  240. /**
  241. * Selects artifact slot so that the containing artifact looks like it's picked up.
  242. */
  243. void CHeroArtPlace::select()
  244. {
  245. if(locked)
  246. return;
  247. pickSlot(true);
  248. if(ourArt->canBeDisassembled() && ArtifactUtils::isSlotEquipment(slotID)) //worn combined artifact -> locks have to disappear
  249. {
  250. for(auto slot : ArtifactUtils::constituentWornSlots())
  251. {
  252. auto ap = ourOwner->getArtPlace(slot);
  253. if(ap)//getArtPlace may return null
  254. ap->pickSlot(ourArt->isPart(ap->ourArt));
  255. }
  256. }
  257. ourOwner->commonInfo->src.setTo(this, false);
  258. ourOwner->commonInfo->src.slotID = ArtifactPosition::TRANSITION_POS;
  259. LOCPLINT->cb->swapArtifacts(ArtifactLocation(ourOwner->curHero, slotID),
  260. ArtifactLocation(ourOwner->curHero, ArtifactPosition::TRANSITION_POS));
  261. }
  262. void CHeroArtPlace::showAll(SDL_Surface * to)
  263. {
  264. if (ourArt && !picked && ourArt == ourOwner->curHero->getArt(slotID, false)) //last condition is needed for disassembling -> artifact may be gone, but we don't know yet TODO: real, nice solution
  265. {
  266. CIntObject::showAll(to);
  267. }
  268. if(marked && active)
  269. {
  270. // Draw vertical bars.
  271. for (int i = 0; i < pos.h; ++i)
  272. {
  273. CSDL_Ext::putPixelWithoutRefresh(to, pos.x, pos.y + i, 240, 220, 120);
  274. CSDL_Ext::putPixelWithoutRefresh(to, pos.x + pos.w - 1, pos.y + i, 240, 220, 120);
  275. }
  276. // Draw horizontal bars.
  277. for (int i = 0; i < pos.w; ++i)
  278. {
  279. CSDL_Ext::putPixelWithoutRefresh(to, pos.x + i, pos.y, 240, 220, 120);
  280. CSDL_Ext::putPixelWithoutRefresh(to, pos.x + i, pos.y + pos.h - 1, 240, 220, 120);
  281. }
  282. }
  283. }
  284. bool CHeroArtPlace::fitsHere(const CArtifactInstance * art) const
  285. {
  286. // You can place 'no artifact' anywhere.
  287. if(!art)
  288. return true;
  289. // Anything but War Machines can be placed in backpack.
  290. if (slotID >= GameConstants::BACKPACK_START)
  291. return !art->artType->isBig();
  292. return art->canBePutAt(ArtifactLocation(ourOwner->curHero, slotID), true);
  293. }
  294. void CHeroArtPlace::setMeAsDest(bool backpackAsVoid)
  295. {
  296. ourOwner->commonInfo->dst.setTo(this, backpackAsVoid);
  297. }
  298. void CHeroArtPlace::setArtifact(const CArtifactInstance *art)
  299. {
  300. baseType = -1; //by default we don't store any component
  301. ourArt = art;
  302. if(!art)
  303. {
  304. image->disable();
  305. text.clear();
  306. hoverText = CGI->generaltexth->allTexts[507];
  307. return;
  308. }
  309. image->enable();
  310. image->setFrame(locked ? ArtifactID::ART_LOCK : art->artType->getIconIndex());
  311. text = art->getDescription();
  312. // Display info about set
  313. if(ourOwner && ourOwner->curHero && !art->canBeDisassembled())
  314. {
  315. for(const auto combinedArt : art->artType->constituentOf)
  316. {
  317. std::string artList;
  318. text += "\n\n";
  319. text += "{" + combinedArt->getNameTranslated() + "}";
  320. int wornArtifacts = 0;
  321. for(const auto part : *combinedArt->constituents)
  322. {
  323. if(art->artType->constituentOf.size() <= 1)
  324. artList += "\n" + part->getNameTranslated();
  325. if(ourOwner->curHero->hasArt(part->getId(), true))
  326. wornArtifacts++;
  327. }
  328. text += " (" + boost::str(boost::format("%d") % wornArtifacts) + " / " +
  329. boost::str(boost::format("%d") % combinedArt->constituents->size()) + ")" + artList;
  330. }
  331. }
  332. if(art->artType->getId() == ArtifactID::SPELL_SCROLL)
  333. {
  334. int spellID = art->getScrollSpellID();
  335. if(spellID >= 0)
  336. {
  337. //add spell component info (used to provide a pic in r-click popup)
  338. baseType = CComponent::spell;
  339. type = spellID;
  340. bonusValue = 0;
  341. }
  342. }
  343. else
  344. {
  345. baseType = CComponent::artifact;
  346. type = art->artType->getId();
  347. bonusValue = 0;
  348. }
  349. if (locked) // Locks should appear as empty.
  350. hoverText = CGI->generaltexth->allTexts[507];
  351. else
  352. hoverText = boost::str(boost::format(CGI->generaltexth->heroscrn[1]) % ourArt->artType->getNameTranslated());
  353. }
  354. void CArtifactsOfHero::SCommonPart::reset()
  355. {
  356. src.clear();
  357. dst.clear();
  358. CCS->curh->dragAndDropCursor(nullptr);
  359. }
  360. void CArtifactsOfHero::setHero(const CGHeroInstance * hero)
  361. {
  362. curHero = hero;
  363. if (curHero->artifactsInBackpack.size() > 0)
  364. backpackPos %= curHero->artifactsInBackpack.size();
  365. else
  366. backpackPos = 0;
  367. // Fill the slots for worn artifacts and backpack.
  368. for(auto p : artWorn)
  369. {
  370. setSlotData(p.second, p.first);
  371. }
  372. scrollBackpack(0);
  373. }
  374. void CArtifactsOfHero::dispose()
  375. {
  376. CCS->curh->dragAndDropCursor(nullptr);
  377. }
  378. void CArtifactsOfHero::scrollBackpack(int dir)
  379. {
  380. int artsInBackpack = static_cast<int>(curHero->artifactsInBackpack.size());
  381. backpackPos += dir;
  382. if(backpackPos < 0)// No guarantee of modulus behavior with negative operands -> we keep it positive
  383. backpackPos += artsInBackpack;
  384. if(artsInBackpack)
  385. backpackPos %= artsInBackpack;
  386. std::multiset<const CArtifactInstance *> toOmit = artifactsOnAltar;
  387. if(commonInfo->src.art) //if we picked an art from backapck, its slot has to be omitted
  388. toOmit.insert(commonInfo->src.art);
  389. int omitedSoFar = 0;
  390. //set new data
  391. size_t s = 0;
  392. for( ; s < artsInBackpack; ++s)
  393. {
  394. if (s < artsInBackpack)
  395. {
  396. auto slotID = ArtifactPosition(GameConstants::BACKPACK_START + (s + backpackPos)%artsInBackpack);
  397. const CArtifactInstance *art = curHero->getArt(slotID);
  398. assert(art);
  399. if(!vstd::contains(toOmit, art))
  400. {
  401. if(s - omitedSoFar < backpack.size())
  402. setSlotData(backpack[s-omitedSoFar], slotID);
  403. }
  404. else
  405. {
  406. toOmit -= art;
  407. omitedSoFar++;
  408. continue;
  409. }
  410. }
  411. }
  412. for( ; s - omitedSoFar < backpack.size(); s++)
  413. eraseSlotData(backpack[s-omitedSoFar], ArtifactPosition(GameConstants::BACKPACK_START + (si32)s));
  414. //in artifact merchant selling artifacts we may have highlight on one of backpack artifacts -> market needs update, cause artifact under highlight changed
  415. if(highlightModeCallback)
  416. {
  417. for(auto & elem : backpack)
  418. {
  419. if(elem->marked)
  420. {
  421. highlightModeCallback(elem.get());
  422. break;
  423. }
  424. }
  425. }
  426. //blocking scrolling if there is not enough artifacts to scroll
  427. bool scrollingPossible = artsInBackpack - omitedSoFar > backpack.size();
  428. leftArtRoll->block(!scrollingPossible);
  429. rightArtRoll->block(!scrollingPossible);
  430. safeRedraw();
  431. }
  432. /**
  433. * Marks possible slots where a given artifact can be placed, except backpack.
  434. *
  435. * @param art Artifact checked against.
  436. */
  437. void CArtifactsOfHero::markPossibleSlots(const CArtifactInstance* art, bool withRedraw)
  438. {
  439. for(CArtifactsOfHero *aoh : commonInfo->participants)
  440. for(auto p : aoh->artWorn)
  441. p.second->selectSlot(art->canBePutAt(ArtifactLocation(aoh->curHero, p.second->slotID), true));
  442. if(withRedraw)
  443. safeRedraw();
  444. }
  445. /**
  446. * Unamarks all slots.
  447. */
  448. void CArtifactsOfHero::unmarkSlots(bool withRedraw)
  449. {
  450. if(commonInfo)
  451. for(CArtifactsOfHero *aoh : commonInfo->participants)
  452. aoh->unmarkLocalSlots(false);
  453. else
  454. unmarkLocalSlots(false);
  455. if(withRedraw)
  456. safeRedraw();
  457. }
  458. void CArtifactsOfHero::unmarkLocalSlots(bool withRedraw)
  459. {
  460. for(auto & p : artWorn)
  461. p.second->selectSlot(false);
  462. for(auto & place : backpack)
  463. place->selectSlot(false);
  464. if(withRedraw)
  465. safeRedraw();
  466. }
  467. /**
  468. * Assigns an artifacts to an artifact place depending on it's new slot ID.
  469. */
  470. void CArtifactsOfHero::setSlotData(ArtPlacePtr artPlace, ArtifactPosition slotID)
  471. {
  472. if(!artPlace && slotID >= GameConstants::BACKPACK_START) //spurious call from artifactMoved in attempt to update hidden backpack slot
  473. {
  474. return;
  475. }
  476. artPlace->pickSlot(false);
  477. artPlace->slotID = slotID;
  478. if(const ArtSlotInfo *asi = curHero->getSlot(slotID))
  479. {
  480. artPlace->lockSlot(asi->locked);
  481. artPlace->setArtifact(asi->artifact);
  482. }
  483. else
  484. artPlace->setArtifact(nullptr);
  485. }
  486. /**
  487. * Makes given artifact slot appear as empty with a certain slot ID.
  488. */
  489. void CArtifactsOfHero::eraseSlotData(ArtPlacePtr artPlace, ArtifactPosition slotID)
  490. {
  491. artPlace->pickSlot(false);
  492. artPlace->slotID = slotID;
  493. artPlace->setArtifact(nullptr);
  494. }
  495. CArtifactsOfHero::CArtifactsOfHero(ArtPlaceMap ArtWorn, std::vector<ArtPlacePtr> Backpack,
  496. std::shared_ptr<CButton> leftScroll, std::shared_ptr<CButton> rightScroll, bool createCommonPart)
  497. : curHero(nullptr),
  498. artWorn(ArtWorn),
  499. backpack(Backpack),
  500. backpackPos(0),
  501. commonInfo(nullptr),
  502. leftArtRoll(leftScroll),
  503. rightArtRoll(rightScroll),
  504. allowedAssembling(true),
  505. highlightModeCallback(nullptr)
  506. {
  507. if(createCommonPart)
  508. {
  509. commonInfo = std::make_shared<CArtifactsOfHero::SCommonPart>();
  510. commonInfo->participants.insert(this);
  511. }
  512. // Init slots for worn artifacts.
  513. for(auto p : artWorn)
  514. {
  515. p.second->ourOwner = this;
  516. eraseSlotData(p.second, p.first);
  517. }
  518. // Init slots for the backpack.
  519. for(size_t s=0; s<backpack.size(); ++s)
  520. {
  521. backpack[s]->ourOwner = this;
  522. eraseSlotData(backpack[s], ArtifactPosition(GameConstants::BACKPACK_START + (si32)s));
  523. }
  524. leftArtRoll->addCallback(std::bind(&CArtifactsOfHero::scrollBackpack, this,-1));
  525. rightArtRoll->addCallback(std::bind(&CArtifactsOfHero::scrollBackpack, this,+1));
  526. }
  527. CArtifactsOfHero::CArtifactsOfHero(const Point & position, bool createCommonPart)
  528. : curHero(nullptr),
  529. backpackPos(0),
  530. commonInfo(nullptr),
  531. allowedAssembling(true),
  532. highlightModeCallback(nullptr)
  533. {
  534. if(createCommonPart)
  535. {
  536. commonInfo = std::make_shared<CArtifactsOfHero::SCommonPart>();
  537. commonInfo->participants.insert(this);
  538. }
  539. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  540. pos += position;
  541. std::vector<Point> slotPos =
  542. {
  543. Point(509,30), Point(567,240), Point(509,80), //0-2
  544. Point(383,68), Point(564,183), Point(509,130), //3-5
  545. Point(431,68), Point(610,183), Point(515,295), //6-8
  546. Point(383,143), Point(399,194), Point(415,245), //9-11
  547. Point(431,296), Point(564,30), Point(610,30), //12-14
  548. Point(610,76), Point(610,122), Point(610,310), //15-17
  549. Point(381,296) //18
  550. };
  551. // Create slots for worn artifacts.
  552. for(si32 g = 0; g < GameConstants::BACKPACK_START; g++)
  553. {
  554. artWorn[ArtifactPosition(g)] = std::make_shared<CHeroArtPlace>(slotPos[g]);
  555. artWorn[ArtifactPosition(g)]->ourOwner = this;
  556. eraseSlotData(artWorn[ArtifactPosition(g)], ArtifactPosition(g));
  557. }
  558. // Create slots for the backpack.
  559. for(int s=0; s<5; ++s)
  560. {
  561. auto add = std::make_shared<CHeroArtPlace>(Point(403 + 46 * s, 365));
  562. add->ourOwner = this;
  563. eraseSlotData(add, ArtifactPosition(GameConstants::BACKPACK_START + s));
  564. backpack.push_back(add);
  565. }
  566. leftArtRoll = std::make_shared<CButton>(Point(379, 364), "hsbtns3.def", CButton::tooltip(), [&](){ scrollBackpack(-1);}, SDLK_LEFT);
  567. rightArtRoll = std::make_shared<CButton>(Point(632, 364), "hsbtns5.def", CButton::tooltip(), [&](){ scrollBackpack(+1);}, SDLK_RIGHT);
  568. }
  569. CArtifactsOfHero::~CArtifactsOfHero()
  570. {
  571. dispose();
  572. // Artifact located in artifactsTransitionPos should be returned
  573. if(!curHero->artifactsTransitionPos.empty())
  574. {
  575. auto artPlace = getArtPlace(
  576. ArtifactUtils::getArtAnyPosition(curHero, curHero->artifactsTransitionPos.begin()->artifact->getTypeId()));
  577. if(artPlace)
  578. {
  579. assert(artPlace->ourOwner);
  580. artPlace->setMeAsDest();
  581. artPlace->ourOwner->realizeCurrentTransaction();
  582. }
  583. else
  584. {
  585. LOCPLINT->cb->eraseArtifactByClient(ArtifactLocation(curHero, ArtifactPosition::TRANSITION_POS));
  586. }
  587. }
  588. }
  589. void CArtifactsOfHero::updateParentWindow()
  590. {
  591. if(CHeroWindow * chw = dynamic_cast<CHeroWindow*>(GH.topInt().get()))
  592. {
  593. chw->update(curHero, true);
  594. }
  595. else if(CExchangeWindow * cew = dynamic_cast<CExchangeWindow*>(GH.topInt().get()))
  596. {
  597. cew->updateWidgets();
  598. }
  599. }
  600. void CArtifactsOfHero::safeRedraw()
  601. {
  602. if (active)
  603. {
  604. if(parent)
  605. parent->redraw();
  606. else
  607. redraw();
  608. }
  609. }
  610. void CArtifactsOfHero::realizeCurrentTransaction()
  611. {
  612. assert(commonInfo->src.AOH);
  613. assert(commonInfo->dst.AOH);
  614. LOCPLINT->cb->swapArtifacts(ArtifactLocation(commonInfo->src.AOH->curHero, commonInfo->src.slotID),
  615. ArtifactLocation(commonInfo->dst.AOH->curHero, commonInfo->dst.slotID));
  616. }
  617. void CArtifactsOfHero::artifactMoved(const ArtifactLocation & src, const ArtifactLocation & dst, bool withUIUpdate)
  618. {
  619. bool isCurHeroSrc = src.isHolder(curHero),
  620. isCurHeroDst = dst.isHolder(curHero);
  621. if(isCurHeroSrc && ArtifactUtils::isSlotBackpack(src.slot))
  622. updateSlot(src.slot);
  623. if(isCurHeroDst && ArtifactUtils::isSlotBackpack(dst.slot))
  624. updateSlot(dst.slot);
  625. // We need to update all slots, artifact might be combined and affect more slots
  626. if(isCurHeroSrc || isCurHeroDst)
  627. updateWornSlots(false);
  628. if(!isCurHeroSrc && !isCurHeroDst)
  629. return;
  630. // When moving one artifact onto another it leads to two art movements: dst->TRANSITION_POS; src->dst
  631. // however after first movement we pick the art from TRANSITION_POS and the second movement coming when
  632. // we have a different artifact may look surprising... but it's valid.
  633. // Used when doing dragAndDrop and artifact swap multiple times
  634. if(src.slot == ArtifactPosition::TRANSITION_POS &&
  635. commonInfo->src.slotID == ArtifactPosition::TRANSITION_POS &&
  636. commonInfo->dst.slotID == ArtifactPosition::PRE_FIRST &&
  637. isCurHeroDst)
  638. {
  639. auto art = curHero->getArt(ArtifactPosition::TRANSITION_POS);
  640. assert(art);
  641. CCS->curh->dragAndDropCursor("artifact", art->artType->getIconIndex());
  642. if(withUIUpdate)
  643. markPossibleSlots(art);
  644. commonInfo->src.art = art;
  645. commonInfo->src.slotID = src.slot;
  646. }
  647. // Artifact was taken from us
  648. else if(commonInfo->src == src && dst.slot != ArtifactPosition::TRANSITION_POS)
  649. {
  650. // Expected movement from slot ot slot
  651. assert(commonInfo->dst == dst
  652. // Artifact moved back to backpack (eg. to make place for art we are moving)
  653. || dst.slot == dst.getHolderArtSet()->artifactsInBackpack.size() + GameConstants::BACKPACK_START
  654. || dst.getHolderArtSet()->bearerType() != ArtBearer::HERO);
  655. commonInfo->reset();
  656. unmarkSlots();
  657. }
  658. else
  659. {
  660. // The dest artifact was moved after the swap -> we are picking it
  661. if(commonInfo->dst == src)
  662. {
  663. assert(dst.slot == ArtifactPosition::TRANSITION_POS);
  664. commonInfo->reset();
  665. for(CArtifactsOfHero * aoh : commonInfo->participants)
  666. {
  667. if(dst.isHolder(aoh->curHero))
  668. {
  669. commonInfo->src.AOH = aoh;
  670. break;
  671. }
  672. }
  673. commonInfo->src.art = dst.getArt();
  674. commonInfo->src.slotID = dst.slot;
  675. assert(commonInfo->src.AOH);
  676. CCS->curh->dragAndDropCursor("artifact", dst.getArt()->artType->getIconIndex());
  677. }
  678. if(!curHero->artifactsTransitionPos.empty() && withUIUpdate)
  679. {
  680. auto artInst = curHero->getArt(ArtifactPosition::TRANSITION_POS);
  681. markPossibleSlots(artInst);
  682. CCS->curh->dragAndDropCursor("artifact", artInst->artType->getIconIndex());
  683. }
  684. }
  685. if(withUIUpdate)
  686. {
  687. updateParentWindow();
  688. scrollBackpack(0);
  689. }
  690. }
  691. void CArtifactsOfHero::artifactRemoved(const ArtifactLocation &al)
  692. {
  693. if(al.isHolder(curHero))
  694. {
  695. if(al.slot < GameConstants::BACKPACK_START)
  696. updateWornSlots(0);
  697. else
  698. scrollBackpack(0); //update backpack slots
  699. }
  700. }
  701. CArtifactsOfHero::ArtPlacePtr CArtifactsOfHero::getArtPlace(ArtifactPosition slot)
  702. {
  703. if(ArtifactUtils::isSlotEquipment(slot))
  704. {
  705. if(artWorn.find(slot) == artWorn.end())
  706. {
  707. logGlobal->error("CArtifactsOfHero::getArtPlace: invalid slot %d", slot);
  708. return nullptr;
  709. }
  710. return artWorn[slot];
  711. }
  712. if(ArtifactUtils::isSlotBackpack(slot))
  713. {
  714. for(ArtPlacePtr ap : backpack)
  715. if(ap->slotID == slot)
  716. return ap;
  717. return nullptr;
  718. }
  719. else
  720. {
  721. return nullptr;
  722. }
  723. }
  724. void CArtifactsOfHero::artifactUpdateSlots(const ArtifactLocation & al)
  725. {
  726. if(al.isHolder(curHero))
  727. {
  728. if(ArtifactUtils::isSlotBackpack(al.slot))
  729. updateBackpackSlots(true);
  730. else
  731. updateWornSlots(true);
  732. }
  733. }
  734. void CArtifactsOfHero::updateWornSlots(bool redrawParent)
  735. {
  736. for(auto p : artWorn)
  737. updateSlot(p.first);
  738. if(redrawParent)
  739. updateParentWindow();
  740. }
  741. void CArtifactsOfHero::updateBackpackSlots(bool redrawParent)
  742. {
  743. for(auto artPlace : backpack)
  744. updateSlot(artPlace->slotID);
  745. scrollBackpack(0);
  746. if(redrawParent)
  747. updateParentWindow();
  748. }
  749. const CGHeroInstance * CArtifactsOfHero::getHero() const
  750. {
  751. return curHero;
  752. }
  753. void CArtifactsOfHero::updateSlot(ArtifactPosition slotID)
  754. {
  755. setSlotData(getArtPlace(slotID), slotID);
  756. }
  757. CArtifactHolder::CArtifactHolder()
  758. {
  759. }
  760. void CWindowWithArtifacts::addSet(std::shared_ptr<CArtifactsOfHero> artSet)
  761. {
  762. artSets.emplace_back(artSet);
  763. }
  764. std::shared_ptr<CArtifactsOfHero::SCommonPart> CWindowWithArtifacts::getCommonPart()
  765. {
  766. for(auto artSetWeak : artSets)
  767. {
  768. std::shared_ptr<CArtifactsOfHero> realPtr = artSetWeak.lock();
  769. if(realPtr)
  770. return realPtr->commonInfo;
  771. }
  772. return std::shared_ptr<CArtifactsOfHero::SCommonPart>();
  773. }
  774. void CWindowWithArtifacts::artifactRemoved(const ArtifactLocation &artLoc)
  775. {
  776. for(auto artSetWeak : artSets)
  777. {
  778. std::shared_ptr<CArtifactsOfHero> realPtr = artSetWeak.lock();
  779. if(realPtr)
  780. realPtr->artifactRemoved(artLoc);
  781. }
  782. }
  783. void CWindowWithArtifacts::artifactMoved(const ArtifactLocation &artLoc, const ArtifactLocation &destLoc, bool withRedraw)
  784. {
  785. CArtifactsOfHero * destaoh = nullptr;
  786. for(auto artSetWeak : artSets)
  787. {
  788. std::shared_ptr<CArtifactsOfHero> realPtr = artSetWeak.lock();
  789. if(realPtr)
  790. {
  791. realPtr->artifactMoved(artLoc, destLoc, withRedraw);
  792. if(destLoc.isHolder(realPtr->getHero()))
  793. destaoh = realPtr.get();
  794. }
  795. }
  796. //Make sure the status bar is updated so it does not display old text
  797. if(destaoh != nullptr && destaoh->getArtPlace(destLoc.slot) != nullptr)
  798. {
  799. destaoh->getArtPlace(destLoc.slot)->hover(true);
  800. }
  801. }
  802. void CWindowWithArtifacts::artifactDisassembled(const ArtifactLocation &artLoc)
  803. {
  804. for(auto artSetWeak : artSets)
  805. {
  806. std::shared_ptr<CArtifactsOfHero> realPtr = artSetWeak.lock();
  807. if(realPtr)
  808. realPtr->artifactUpdateSlots(artLoc);
  809. }
  810. }
  811. void CWindowWithArtifacts::artifactAssembled(const ArtifactLocation &artLoc)
  812. {
  813. for(auto artSetWeak : artSets)
  814. {
  815. std::shared_ptr<CArtifactsOfHero> realPtr = artSetWeak.lock();
  816. if(realPtr)
  817. realPtr->artifactUpdateSlots(artLoc);
  818. }
  819. }
  820. void CArtifactsOfHero::SCommonPart::Artpos::clear()
  821. {
  822. slotID = ArtifactPosition::PRE_FIRST;
  823. AOH = nullptr;
  824. art = nullptr;
  825. }
  826. void CArtifactsOfHero::SCommonPart::Artpos::setTo(const CHeroArtPlace *place, bool dontTakeBackpack)
  827. {
  828. slotID = place->slotID;
  829. AOH = place->ourOwner;
  830. if(ArtifactUtils::isSlotBackpack(slotID) && dontTakeBackpack)
  831. art = nullptr;
  832. else
  833. art = place->ourArt;
  834. }
  835. bool CArtifactsOfHero::SCommonPart::Artpos::operator==(const ArtifactLocation &al) const
  836. {
  837. if(!AOH)
  838. return false;
  839. bool ret = al.isHolder(AOH->curHero) && al.slot == slotID;
  840. //assert(al.getArt() == art);
  841. return ret;
  842. }
  843. bool CArtifactsOfHero::SCommonPart::Artpos::valid()
  844. {
  845. assert(AOH && art);
  846. return art == AOH->curHero->getArt(slotID);
  847. }
  848. CArtPlace::CArtPlace(Point position, const CArtifactInstance * Art) : ourArt(Art)
  849. {
  850. image = nullptr;
  851. pos += position;
  852. pos.w = pos.h = 44;
  853. }
  854. void CArtPlace::clickLeft(tribool down, bool previousState)
  855. {
  856. LRClickableAreaWTextComp::clickLeft(down, previousState);
  857. }
  858. void CArtPlace::clickRight(tribool down, bool previousState)
  859. {
  860. LRClickableAreaWTextComp::clickRight(down, previousState);
  861. }
  862. CCommanderArtPlace::CCommanderArtPlace(Point position, const CGHeroInstance * commanderOwner, ArtifactPosition artSlot, const CArtifactInstance * Art) : CArtPlace(position, Art), commanderOwner(commanderOwner), commanderSlotID(artSlot.num)
  863. {
  864. createImage();
  865. setArtifact(Art);
  866. }
  867. void CCommanderArtPlace::clickLeft(tribool down, bool previousState)
  868. {
  869. if (ourArt && text.size() && down)
  870. LOCPLINT->showYesNoDialog(CGI->generaltexth->translate("vcmi.commanderWindow.artifactMessage"), [this](){ returnArtToHeroCallback(); }, [](){});
  871. }
  872. void CCommanderArtPlace::clickRight(tribool down, bool previousState)
  873. {
  874. if (ourArt && text.size() && down)
  875. CArtPlace::clickRight(down, previousState);
  876. }
  877. void CCommanderArtPlace::createImage()
  878. {
  879. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  880. int imageIndex = 0;
  881. if(ourArt)
  882. imageIndex = ourArt->artType->getIconIndex();
  883. image = std::make_shared<CAnimImage>("artifact", imageIndex);
  884. if(!ourArt)
  885. image->disable();
  886. }
  887. void CCommanderArtPlace::returnArtToHeroCallback()
  888. {
  889. ArtifactPosition artifactPos = commanderSlotID;
  890. ArtifactPosition freeSlot = ArtifactUtils::getArtBackpackPosition(commanderOwner, ourArt->getTypeId());
  891. if(freeSlot == ArtifactPosition::PRE_FIRST)
  892. {
  893. LOCPLINT->showInfoDialog(CGI->generaltexth->translate("core.genrltxt.152"));
  894. }
  895. else
  896. {
  897. ArtifactLocation src(commanderOwner->commander.get(), artifactPos);
  898. ArtifactLocation dst(commanderOwner, freeSlot);
  899. if(ourArt->canBePutAt(dst, true))
  900. {
  901. LOCPLINT->cb->swapArtifacts(src, dst);
  902. setArtifact(nullptr);
  903. parent->redraw();
  904. }
  905. }
  906. }
  907. void CCommanderArtPlace::setArtifact(const CArtifactInstance * art)
  908. {
  909. baseType = -1; //by default we don't store any component
  910. ourArt = art;
  911. if (!art)
  912. {
  913. image->disable();
  914. text.clear();
  915. return;
  916. }
  917. image->enable();
  918. image->setFrame(art->artType->getIconIndex());
  919. text = art->getDescription();
  920. if (art->artType->getId() == ArtifactID::SPELL_SCROLL)
  921. {
  922. int spellID = art->getScrollSpellID();
  923. if (spellID >= 0)
  924. {
  925. //add spell component info (used to provide a pic in r-click popup)
  926. baseType = CComponent::spell;
  927. type = spellID;
  928. bonusValue = 0;
  929. }
  930. }
  931. else
  932. {
  933. baseType = CComponent::artifact;
  934. type = art->artType->getId();
  935. bonusValue = 0;
  936. }
  937. }