CArtifactHolder.cpp 27 KB

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