CArtifactHolder.cpp 27 KB

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