CArtifactHolder.cpp 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061
  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 = art->assemblyPossibilities(hero, 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->getEffectiveDescription(ourOwner->curHero);
  312. if(art->artType->getId() == ArtifactID::SPELL_SCROLL)
  313. {
  314. int spellID = art->getGivenSpellID();
  315. if(spellID >= 0)
  316. {
  317. //add spell component info (used to provide a pic in r-click popup)
  318. baseType = CComponent::spell;
  319. type = spellID;
  320. bonusValue = 0;
  321. }
  322. }
  323. else
  324. {
  325. baseType = CComponent::artifact;
  326. type = art->artType->getId();
  327. bonusValue = 0;
  328. }
  329. if (locked) // Locks should appear as empty.
  330. hoverText = CGI->generaltexth->allTexts[507];
  331. else
  332. hoverText = boost::str(boost::format(CGI->generaltexth->heroscrn[1]) % ourArt->artType->getNameTranslated());
  333. }
  334. void CArtifactsOfHero::SCommonPart::reset()
  335. {
  336. src.clear();
  337. dst.clear();
  338. CCS->curh->dragAndDropCursor(nullptr);
  339. }
  340. void CArtifactsOfHero::setHero(const CGHeroInstance * hero)
  341. {
  342. curHero = hero;
  343. if (curHero->artifactsInBackpack.size() > 0)
  344. backpackPos %= curHero->artifactsInBackpack.size();
  345. else
  346. backpackPos = 0;
  347. // Fill the slots for worn artifacts and backpack.
  348. for(auto p : artWorn)
  349. {
  350. setSlotData(p.second, p.first);
  351. }
  352. scrollBackpack(0);
  353. }
  354. void CArtifactsOfHero::dispose()
  355. {
  356. CCS->curh->dragAndDropCursor(nullptr);
  357. }
  358. void CArtifactsOfHero::scrollBackpack(int dir)
  359. {
  360. int artsInBackpack = static_cast<int>(curHero->artifactsInBackpack.size());
  361. backpackPos += dir;
  362. if(backpackPos < 0)// No guarantee of modulus behavior with negative operands -> we keep it positive
  363. backpackPos += artsInBackpack;
  364. if(artsInBackpack)
  365. backpackPos %= artsInBackpack;
  366. std::multiset<const CArtifactInstance *> toOmit = artifactsOnAltar;
  367. if(commonInfo->src.art) //if we picked an art from backapck, its slot has to be omitted
  368. toOmit.insert(commonInfo->src.art);
  369. int omitedSoFar = 0;
  370. //set new data
  371. size_t s = 0;
  372. for( ; s < artsInBackpack; ++s)
  373. {
  374. if (s < artsInBackpack)
  375. {
  376. auto slotID = ArtifactPosition(GameConstants::BACKPACK_START + (s + backpackPos)%artsInBackpack);
  377. const CArtifactInstance *art = curHero->getArt(slotID);
  378. assert(art);
  379. if(!vstd::contains(toOmit, art))
  380. {
  381. if(s - omitedSoFar < backpack.size())
  382. setSlotData(backpack[s-omitedSoFar], slotID);
  383. }
  384. else
  385. {
  386. toOmit -= art;
  387. omitedSoFar++;
  388. continue;
  389. }
  390. }
  391. }
  392. for( ; s - omitedSoFar < backpack.size(); s++)
  393. eraseSlotData(backpack[s-omitedSoFar], ArtifactPosition(GameConstants::BACKPACK_START + (si32)s));
  394. //in artifact merchant selling artifacts we may have highlight on one of backpack artifacts -> market needs update, cause artifact under highlight changed
  395. if(highlightModeCallback)
  396. {
  397. for(auto & elem : backpack)
  398. {
  399. if(elem->marked)
  400. {
  401. highlightModeCallback(elem.get());
  402. break;
  403. }
  404. }
  405. }
  406. //blocking scrolling if there is not enough artifacts to scroll
  407. bool scrollingPossible = artsInBackpack - omitedSoFar > backpack.size();
  408. leftArtRoll->block(!scrollingPossible);
  409. rightArtRoll->block(!scrollingPossible);
  410. safeRedraw();
  411. }
  412. /**
  413. * Marks possible slots where a given artifact can be placed, except backpack.
  414. *
  415. * @param art Artifact checked against.
  416. */
  417. void CArtifactsOfHero::markPossibleSlots(const CArtifactInstance* art, bool withRedraw)
  418. {
  419. for(CArtifactsOfHero *aoh : commonInfo->participants)
  420. for(auto p : aoh->artWorn)
  421. p.second->selectSlot(art->canBePutAt(ArtifactLocation(aoh->curHero, p.second->slotID), true));
  422. if(withRedraw)
  423. safeRedraw();
  424. }
  425. /**
  426. * Unamarks all slots.
  427. */
  428. void CArtifactsOfHero::unmarkSlots(bool withRedraw)
  429. {
  430. if(commonInfo)
  431. for(CArtifactsOfHero *aoh : commonInfo->participants)
  432. aoh->unmarkLocalSlots(false);
  433. else
  434. unmarkLocalSlots(false);
  435. if(withRedraw)
  436. safeRedraw();
  437. }
  438. void CArtifactsOfHero::unmarkLocalSlots(bool withRedraw)
  439. {
  440. for(auto & p : artWorn)
  441. p.second->selectSlot(false);
  442. for(auto & place : backpack)
  443. place->selectSlot(false);
  444. if(withRedraw)
  445. safeRedraw();
  446. }
  447. /**
  448. * Assigns an artifacts to an artifact place depending on it's new slot ID.
  449. */
  450. void CArtifactsOfHero::setSlotData(ArtPlacePtr artPlace, ArtifactPosition slotID)
  451. {
  452. if(!artPlace && slotID >= GameConstants::BACKPACK_START) //spurious call from artifactMoved in attempt to update hidden backpack slot
  453. {
  454. return;
  455. }
  456. artPlace->pickSlot(false);
  457. artPlace->slotID = slotID;
  458. if(const ArtSlotInfo *asi = curHero->getSlot(slotID))
  459. {
  460. artPlace->lockSlot(asi->locked);
  461. artPlace->setArtifact(asi->artifact);
  462. }
  463. else
  464. artPlace->setArtifact(nullptr);
  465. }
  466. /**
  467. * Makes given artifact slot appear as empty with a certain slot ID.
  468. */
  469. void CArtifactsOfHero::eraseSlotData(ArtPlacePtr artPlace, ArtifactPosition slotID)
  470. {
  471. artPlace->pickSlot(false);
  472. artPlace->slotID = slotID;
  473. artPlace->setArtifact(nullptr);
  474. }
  475. CArtifactsOfHero::CArtifactsOfHero(ArtPlaceMap ArtWorn, std::vector<ArtPlacePtr> Backpack,
  476. std::shared_ptr<CButton> leftScroll, std::shared_ptr<CButton> rightScroll, bool createCommonPart)
  477. : curHero(nullptr),
  478. artWorn(ArtWorn),
  479. backpack(Backpack),
  480. backpackPos(0),
  481. commonInfo(nullptr),
  482. leftArtRoll(leftScroll),
  483. rightArtRoll(rightScroll),
  484. allowedAssembling(true),
  485. highlightModeCallback(nullptr)
  486. {
  487. if(createCommonPart)
  488. {
  489. commonInfo = std::make_shared<CArtifactsOfHero::SCommonPart>();
  490. commonInfo->participants.insert(this);
  491. }
  492. // Init slots for worn artifacts.
  493. for(auto p : artWorn)
  494. {
  495. p.second->ourOwner = this;
  496. eraseSlotData(p.second, p.first);
  497. }
  498. // Init slots for the backpack.
  499. for(size_t s=0; s<backpack.size(); ++s)
  500. {
  501. backpack[s]->ourOwner = this;
  502. eraseSlotData(backpack[s], ArtifactPosition(GameConstants::BACKPACK_START + (si32)s));
  503. }
  504. leftArtRoll->addCallback(std::bind(&CArtifactsOfHero::scrollBackpack, this,-1));
  505. rightArtRoll->addCallback(std::bind(&CArtifactsOfHero::scrollBackpack, this,+1));
  506. }
  507. CArtifactsOfHero::CArtifactsOfHero(const Point & position, bool createCommonPart)
  508. : curHero(nullptr),
  509. backpackPos(0),
  510. commonInfo(nullptr),
  511. allowedAssembling(true),
  512. highlightModeCallback(nullptr)
  513. {
  514. if(createCommonPart)
  515. {
  516. commonInfo = std::make_shared<CArtifactsOfHero::SCommonPart>();
  517. commonInfo->participants.insert(this);
  518. }
  519. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  520. pos += position;
  521. std::vector<Point> slotPos =
  522. {
  523. Point(509,30), Point(567,240), Point(509,80), //0-2
  524. Point(383,68), Point(564,183), Point(509,130), //3-5
  525. Point(431,68), Point(610,183), Point(515,295), //6-8
  526. Point(383,143), Point(399,194), Point(415,245), //9-11
  527. Point(431,296), Point(564,30), Point(610,30), //12-14
  528. Point(610,76), Point(610,122), Point(610,310), //15-17
  529. Point(381,296) //18
  530. };
  531. // Create slots for worn artifacts.
  532. for(si32 g = 0; g < GameConstants::BACKPACK_START; g++)
  533. {
  534. artWorn[ArtifactPosition(g)] = std::make_shared<CHeroArtPlace>(slotPos[g]);
  535. artWorn[ArtifactPosition(g)]->ourOwner = this;
  536. eraseSlotData(artWorn[ArtifactPosition(g)], ArtifactPosition(g));
  537. }
  538. // Create slots for the backpack.
  539. for(int s=0; s<5; ++s)
  540. {
  541. auto add = std::make_shared<CHeroArtPlace>(Point(403 + 46 * s, 365));
  542. add->ourOwner = this;
  543. eraseSlotData(add, ArtifactPosition(GameConstants::BACKPACK_START + s));
  544. backpack.push_back(add);
  545. }
  546. leftArtRoll = std::make_shared<CButton>(Point(379, 364), "hsbtns3.def", CButton::tooltip(), [&](){ scrollBackpack(-1);}, SDLK_LEFT);
  547. rightArtRoll = std::make_shared<CButton>(Point(632, 364), "hsbtns5.def", CButton::tooltip(), [&](){ scrollBackpack(+1);}, SDLK_RIGHT);
  548. }
  549. CArtifactsOfHero::~CArtifactsOfHero()
  550. {
  551. dispose();
  552. // Artifact located in artifactsTransitionPos should be returned
  553. if(!curHero->artifactsTransitionPos.empty())
  554. {
  555. auto artPlace = getArtPlace(
  556. ArtifactUtils::getArtAnyPosition(curHero, curHero->artifactsTransitionPos.begin()->artifact->getTypeId()));
  557. if(artPlace)
  558. {
  559. assert(artPlace->ourOwner);
  560. artPlace->setMeAsDest();
  561. artPlace->ourOwner->realizeCurrentTransaction();
  562. }
  563. else
  564. {
  565. LOCPLINT->cb->eraseArtifactByClient(ArtifactLocation(curHero, ArtifactPosition::TRANSITION_POS));
  566. }
  567. }
  568. }
  569. void CArtifactsOfHero::updateParentWindow()
  570. {
  571. if(CHeroWindow * chw = dynamic_cast<CHeroWindow*>(GH.topInt().get()))
  572. {
  573. chw->update(curHero, true);
  574. }
  575. else if(CExchangeWindow * cew = dynamic_cast<CExchangeWindow*>(GH.topInt().get()))
  576. {
  577. cew->updateWidgets();
  578. }
  579. }
  580. void CArtifactsOfHero::safeRedraw()
  581. {
  582. if (active)
  583. {
  584. if(parent)
  585. parent->redraw();
  586. else
  587. redraw();
  588. }
  589. }
  590. void CArtifactsOfHero::realizeCurrentTransaction()
  591. {
  592. assert(commonInfo->src.AOH);
  593. assert(commonInfo->dst.AOH);
  594. LOCPLINT->cb->swapArtifacts(ArtifactLocation(commonInfo->src.AOH->curHero, commonInfo->src.slotID),
  595. ArtifactLocation(commonInfo->dst.AOH->curHero, commonInfo->dst.slotID));
  596. }
  597. void CArtifactsOfHero::artifactMoved(const ArtifactLocation & src, const ArtifactLocation & dst, bool withUIUpdate)
  598. {
  599. bool isCurHeroSrc = src.isHolder(curHero),
  600. isCurHeroDst = dst.isHolder(curHero);
  601. if(isCurHeroSrc && ArtifactUtils::isSlotBackpack(src.slot))
  602. updateSlot(src.slot);
  603. if(isCurHeroDst && ArtifactUtils::isSlotBackpack(dst.slot))
  604. updateSlot(dst.slot);
  605. // We need to update all slots, artifact might be combined and affect more slots
  606. if(isCurHeroSrc || isCurHeroDst)
  607. updateWornSlots(false);
  608. if(!isCurHeroSrc && !isCurHeroDst)
  609. return;
  610. // When moving one artifact onto another it leads to two art movements: dst->TRANSITION_POS; src->dst
  611. // however after first movement we pick the art from TRANSITION_POS and the second movement coming when
  612. // we have a different artifact may look surprising... but it's valid.
  613. // Used when doing dragAndDrop and artifact swap multiple times
  614. if(src.slot == ArtifactPosition::TRANSITION_POS &&
  615. commonInfo->src.slotID == ArtifactPosition::TRANSITION_POS &&
  616. commonInfo->dst.slotID == ArtifactPosition::PRE_FIRST &&
  617. isCurHeroDst)
  618. {
  619. auto art = curHero->getArt(ArtifactPosition::TRANSITION_POS);
  620. assert(art);
  621. CCS->curh->dragAndDropCursor("artifact", art->artType->getIconIndex());
  622. if(withUIUpdate)
  623. markPossibleSlots(art);
  624. commonInfo->src.art = art;
  625. commonInfo->src.slotID = src.slot;
  626. }
  627. // Artifact was taken from us
  628. else if(commonInfo->src == src && dst.slot != ArtifactPosition::TRANSITION_POS)
  629. {
  630. // Expected movement from slot ot slot
  631. assert(commonInfo->dst == dst
  632. // Artifact moved back to backpack (eg. to make place for art we are moving)
  633. || dst.slot == dst.getHolderArtSet()->artifactsInBackpack.size() + GameConstants::BACKPACK_START
  634. || dst.getHolderArtSet()->bearerType() != ArtBearer::HERO);
  635. commonInfo->reset();
  636. unmarkSlots();
  637. }
  638. else
  639. {
  640. // The dest artifact was moved after the swap -> we are picking it
  641. if(commonInfo->dst == src)
  642. {
  643. assert(dst.slot == ArtifactPosition::TRANSITION_POS);
  644. commonInfo->reset();
  645. for(CArtifactsOfHero * aoh : commonInfo->participants)
  646. {
  647. if(dst.isHolder(aoh->curHero))
  648. {
  649. commonInfo->src.AOH = aoh;
  650. break;
  651. }
  652. }
  653. commonInfo->src.art = dst.getArt();
  654. commonInfo->src.slotID = dst.slot;
  655. assert(commonInfo->src.AOH);
  656. CCS->curh->dragAndDropCursor("artifact", dst.getArt()->artType->getIconIndex());
  657. }
  658. if(!curHero->artifactsTransitionPos.empty() && withUIUpdate)
  659. {
  660. auto artInst = curHero->getArt(ArtifactPosition::TRANSITION_POS);
  661. markPossibleSlots(artInst);
  662. CCS->curh->dragAndDropCursor("artifact", artInst->artType->getIconIndex());
  663. }
  664. }
  665. if(withUIUpdate)
  666. {
  667. updateParentWindow();
  668. scrollBackpack(0);
  669. }
  670. }
  671. void CArtifactsOfHero::artifactRemoved(const ArtifactLocation &al)
  672. {
  673. if(al.isHolder(curHero))
  674. {
  675. if(al.slot < GameConstants::BACKPACK_START)
  676. updateWornSlots(0);
  677. else
  678. scrollBackpack(0); //update backpack slots
  679. }
  680. }
  681. CArtifactsOfHero::ArtPlacePtr CArtifactsOfHero::getArtPlace(ArtifactPosition slot)
  682. {
  683. if(ArtifactUtils::isSlotEquipment(slot))
  684. {
  685. if(artWorn.find(slot) == artWorn.end())
  686. {
  687. logGlobal->error("CArtifactsOfHero::getArtPlace: invalid slot %d", slot);
  688. return nullptr;
  689. }
  690. return artWorn[slot];
  691. }
  692. if(ArtifactUtils::isSlotBackpack(slot))
  693. {
  694. for(ArtPlacePtr ap : backpack)
  695. if(ap->slotID == slot)
  696. return ap;
  697. return nullptr;
  698. }
  699. else
  700. {
  701. return nullptr;
  702. }
  703. }
  704. void CArtifactsOfHero::artifactUpdateSlots(const ArtifactLocation & al)
  705. {
  706. if(al.isHolder(curHero))
  707. {
  708. if(ArtifactUtils::isSlotBackpack(al.slot))
  709. updateBackpackSlots(true);
  710. else
  711. updateWornSlots(true);
  712. }
  713. }
  714. void CArtifactsOfHero::updateWornSlots(bool redrawParent)
  715. {
  716. for(auto p : artWorn)
  717. updateSlot(p.first);
  718. if(redrawParent)
  719. updateParentWindow();
  720. }
  721. void CArtifactsOfHero::updateBackpackSlots(bool redrawParent)
  722. {
  723. for(auto artPlace : backpack)
  724. updateSlot(artPlace->slotID);
  725. scrollBackpack(0);
  726. if(redrawParent)
  727. updateParentWindow();
  728. }
  729. const CGHeroInstance * CArtifactsOfHero::getHero() const
  730. {
  731. return curHero;
  732. }
  733. void CArtifactsOfHero::updateSlot(ArtifactPosition slotID)
  734. {
  735. setSlotData(getArtPlace(slotID), slotID);
  736. }
  737. CArtifactHolder::CArtifactHolder()
  738. {
  739. }
  740. void CWindowWithArtifacts::addSet(std::shared_ptr<CArtifactsOfHero> artSet)
  741. {
  742. artSets.emplace_back(artSet);
  743. }
  744. std::shared_ptr<CArtifactsOfHero::SCommonPart> CWindowWithArtifacts::getCommonPart()
  745. {
  746. for(auto artSetWeak : artSets)
  747. {
  748. std::shared_ptr<CArtifactsOfHero> realPtr = artSetWeak.lock();
  749. if(realPtr)
  750. return realPtr->commonInfo;
  751. }
  752. return std::shared_ptr<CArtifactsOfHero::SCommonPart>();
  753. }
  754. void CWindowWithArtifacts::artifactRemoved(const ArtifactLocation &artLoc)
  755. {
  756. for(auto artSetWeak : artSets)
  757. {
  758. std::shared_ptr<CArtifactsOfHero> realPtr = artSetWeak.lock();
  759. if(realPtr)
  760. realPtr->artifactRemoved(artLoc);
  761. }
  762. }
  763. void CWindowWithArtifacts::artifactMoved(const ArtifactLocation &artLoc, const ArtifactLocation &destLoc, bool withRedraw)
  764. {
  765. CArtifactsOfHero * destaoh = nullptr;
  766. for(auto artSetWeak : artSets)
  767. {
  768. std::shared_ptr<CArtifactsOfHero> realPtr = artSetWeak.lock();
  769. if(realPtr)
  770. {
  771. realPtr->artifactMoved(artLoc, destLoc, withRedraw);
  772. if(destLoc.isHolder(realPtr->getHero()))
  773. destaoh = realPtr.get();
  774. }
  775. }
  776. //Make sure the status bar is updated so it does not display old text
  777. if(destaoh != nullptr && destaoh->getArtPlace(destLoc.slot) != nullptr)
  778. {
  779. destaoh->getArtPlace(destLoc.slot)->hover(true);
  780. }
  781. }
  782. void CWindowWithArtifacts::artifactDisassembled(const ArtifactLocation &artLoc)
  783. {
  784. for(auto artSetWeak : artSets)
  785. {
  786. std::shared_ptr<CArtifactsOfHero> realPtr = artSetWeak.lock();
  787. if(realPtr)
  788. realPtr->artifactUpdateSlots(artLoc);
  789. }
  790. }
  791. void CWindowWithArtifacts::artifactAssembled(const ArtifactLocation &artLoc)
  792. {
  793. for(auto artSetWeak : artSets)
  794. {
  795. std::shared_ptr<CArtifactsOfHero> realPtr = artSetWeak.lock();
  796. if(realPtr)
  797. realPtr->artifactUpdateSlots(artLoc);
  798. }
  799. }
  800. void CArtifactsOfHero::SCommonPart::Artpos::clear()
  801. {
  802. slotID = ArtifactPosition::PRE_FIRST;
  803. AOH = nullptr;
  804. art = nullptr;
  805. }
  806. void CArtifactsOfHero::SCommonPart::Artpos::setTo(const CHeroArtPlace *place, bool dontTakeBackpack)
  807. {
  808. slotID = place->slotID;
  809. AOH = place->ourOwner;
  810. if(ArtifactUtils::isSlotBackpack(slotID) && dontTakeBackpack)
  811. art = nullptr;
  812. else
  813. art = place->ourArt;
  814. }
  815. bool CArtifactsOfHero::SCommonPart::Artpos::operator==(const ArtifactLocation &al) const
  816. {
  817. if(!AOH)
  818. return false;
  819. bool ret = al.isHolder(AOH->curHero) && al.slot == slotID;
  820. //assert(al.getArt() == art);
  821. return ret;
  822. }
  823. bool CArtifactsOfHero::SCommonPart::Artpos::valid()
  824. {
  825. assert(AOH && art);
  826. return art == AOH->curHero->getArt(slotID);
  827. }
  828. CArtPlace::CArtPlace(Point position, const CArtifactInstance * Art) : ourArt(Art)
  829. {
  830. image = nullptr;
  831. pos += position;
  832. pos.w = pos.h = 44;
  833. }
  834. void CArtPlace::clickLeft(tribool down, bool previousState)
  835. {
  836. LRClickableAreaWTextComp::clickLeft(down, previousState);
  837. }
  838. void CArtPlace::clickRight(tribool down, bool previousState)
  839. {
  840. LRClickableAreaWTextComp::clickRight(down, previousState);
  841. }
  842. CCommanderArtPlace::CCommanderArtPlace(Point position, const CGHeroInstance * commanderOwner, ArtifactPosition artSlot, const CArtifactInstance * Art) : CArtPlace(position, Art), commanderOwner(commanderOwner), commanderSlotID(artSlot.num)
  843. {
  844. createImage();
  845. setArtifact(Art);
  846. }
  847. void CCommanderArtPlace::clickLeft(tribool down, bool previousState)
  848. {
  849. if (ourArt && text.size() && down)
  850. LOCPLINT->showYesNoDialog(CGI->generaltexth->translate("vcmi.commanderWindow.artifactMessage"), [this](){ returnArtToHeroCallback(); }, [](){});
  851. }
  852. void CCommanderArtPlace::clickRight(tribool down, bool previousState)
  853. {
  854. if (ourArt && text.size() && down)
  855. CArtPlace::clickRight(down, previousState);
  856. }
  857. void CCommanderArtPlace::createImage()
  858. {
  859. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  860. int imageIndex = 0;
  861. if(ourArt)
  862. imageIndex = ourArt->artType->getIconIndex();
  863. image = std::make_shared<CAnimImage>("artifact", imageIndex);
  864. if(!ourArt)
  865. image->disable();
  866. }
  867. void CCommanderArtPlace::returnArtToHeroCallback()
  868. {
  869. ArtifactPosition artifactPos = commanderSlotID;
  870. ArtifactPosition freeSlot = ArtifactUtils::getArtBackpackPosition(commanderOwner, ourArt->getTypeId());
  871. if(freeSlot == ArtifactPosition::PRE_FIRST)
  872. {
  873. LOCPLINT->showInfoDialog(CGI->generaltexth->translate("core.genrltxt.152"));
  874. }
  875. else
  876. {
  877. ArtifactLocation src(commanderOwner->commander.get(), artifactPos);
  878. ArtifactLocation dst(commanderOwner, freeSlot);
  879. if(ourArt->canBePutAt(dst, true))
  880. {
  881. LOCPLINT->cb->swapArtifacts(src, dst);
  882. setArtifact(nullptr);
  883. parent->redraw();
  884. }
  885. }
  886. }
  887. void CCommanderArtPlace::setArtifact(const CArtifactInstance * art)
  888. {
  889. baseType = -1; //by default we don't store any component
  890. ourArt = art;
  891. if (!art)
  892. {
  893. image->disable();
  894. text.clear();
  895. return;
  896. }
  897. image->enable();
  898. image->setFrame(art->artType->getIconIndex());
  899. text = art->getEffectiveDescription();
  900. if (art->artType->getId() == ArtifactID::SPELL_SCROLL)
  901. {
  902. int spellID = art->getGivenSpellID();
  903. if (spellID >= 0)
  904. {
  905. //add spell component info (used to provide a pic in r-click popup)
  906. baseType = CComponent::spell;
  907. type = spellID;
  908. bonusValue = 0;
  909. }
  910. }
  911. else
  912. {
  913. baseType = CComponent::artifact;
  914. type = art->artType->getId();
  915. bonusValue = 0;
  916. }
  917. }