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 = 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. CCS->curh->dragAndDropCursor("artifact", ourArt->artType->getIconIndex());
  247. ourOwner->commonInfo->src.setTo(this, false);
  248. ourOwner->commonInfo->src.slotID = ArtifactPosition::TRANSITION_POS;
  249. LOCPLINT->cb->swapArtifacts(ArtifactLocation(ourOwner->curHero, slotID),
  250. ArtifactLocation(ourOwner->curHero, ArtifactPosition::TRANSITION_POS));
  251. }
  252. void CHeroArtPlace::showAll(SDL_Surface * to)
  253. {
  254. 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
  255. {
  256. CIntObject::showAll(to);
  257. }
  258. if(marked && active)
  259. {
  260. // Draw vertical bars.
  261. for (int i = 0; i < pos.h; ++i)
  262. {
  263. CSDL_Ext::putPixelWithoutRefresh(to, pos.x, pos.y + i, 240, 220, 120);
  264. CSDL_Ext::putPixelWithoutRefresh(to, pos.x + pos.w - 1, pos.y + i, 240, 220, 120);
  265. }
  266. // Draw horizontal bars.
  267. for (int i = 0; i < pos.w; ++i)
  268. {
  269. CSDL_Ext::putPixelWithoutRefresh(to, pos.x + i, pos.y, 240, 220, 120);
  270. CSDL_Ext::putPixelWithoutRefresh(to, pos.x + i, pos.y + pos.h - 1, 240, 220, 120);
  271. }
  272. }
  273. }
  274. bool CHeroArtPlace::fitsHere(const CArtifactInstance * art) const
  275. {
  276. // You can place 'no artifact' anywhere.
  277. if(!art)
  278. return true;
  279. // Anything but War Machines can be placed in backpack.
  280. if (slotID >= GameConstants::BACKPACK_START)
  281. return !art->artType->isBig();
  282. return art->canBePutAt(ArtifactLocation(ourOwner->curHero, slotID), true);
  283. }
  284. void CHeroArtPlace::setMeAsDest(bool backpackAsVoid)
  285. {
  286. ourOwner->commonInfo->dst.setTo(this, backpackAsVoid);
  287. }
  288. void CHeroArtPlace::setArtifact(const CArtifactInstance *art)
  289. {
  290. baseType = -1; //by default we don't store any component
  291. ourArt = art;
  292. if(!art)
  293. {
  294. image->disable();
  295. text.clear();
  296. hoverText = CGI->generaltexth->allTexts[507];
  297. return;
  298. }
  299. image->enable();
  300. image->setFrame(locked ? ArtifactID::ART_LOCK : art->artType->getIconIndex());
  301. text = art->getEffectiveDescription(ourOwner->curHero);
  302. if(art->artType->getId() == ArtifactID::SPELL_SCROLL)
  303. {
  304. int spellID = art->getGivenSpellID();
  305. if(spellID >= 0)
  306. {
  307. //add spell component info (used to provide a pic in r-click popup)
  308. baseType = CComponent::spell;
  309. type = spellID;
  310. bonusValue = 0;
  311. }
  312. }
  313. else
  314. {
  315. baseType = CComponent::artifact;
  316. type = art->artType->getId();
  317. bonusValue = 0;
  318. }
  319. if (locked) // Locks should appear as empty.
  320. hoverText = CGI->generaltexth->allTexts[507];
  321. else
  322. hoverText = boost::str(boost::format(CGI->generaltexth->heroscrn[1]) % ourArt->artType->getNameTranslated());
  323. }
  324. void CArtifactsOfHero::SCommonPart::reset()
  325. {
  326. src.clear();
  327. dst.clear();
  328. CCS->curh->dragAndDropCursor(nullptr);
  329. }
  330. void CArtifactsOfHero::setHero(const CGHeroInstance * hero)
  331. {
  332. curHero = hero;
  333. if (curHero->artifactsInBackpack.size() > 0)
  334. backpackPos %= curHero->artifactsInBackpack.size();
  335. else
  336. backpackPos = 0;
  337. // Fill the slots for worn artifacts and backpack.
  338. for(auto p : artWorn)
  339. {
  340. setSlotData(p.second, p.first);
  341. }
  342. scrollBackpack(0);
  343. }
  344. void CArtifactsOfHero::dispose()
  345. {
  346. CCS->curh->dragAndDropCursor(nullptr);
  347. }
  348. void CArtifactsOfHero::scrollBackpack(int dir)
  349. {
  350. int artsInBackpack = static_cast<int>(curHero->artifactsInBackpack.size());
  351. backpackPos += dir;
  352. if(backpackPos < 0)// No guarantee of modulus behavior with negative operands -> we keep it positive
  353. backpackPos += artsInBackpack;
  354. if(artsInBackpack)
  355. backpackPos %= artsInBackpack;
  356. std::multiset<const CArtifactInstance *> toOmit = artifactsOnAltar;
  357. if(commonInfo->src.art) //if we picked an art from backapck, its slot has to be omitted
  358. toOmit.insert(commonInfo->src.art);
  359. int omitedSoFar = 0;
  360. //set new data
  361. size_t s = 0;
  362. for( ; s < artsInBackpack; ++s)
  363. {
  364. if (s < artsInBackpack)
  365. {
  366. auto slotID = ArtifactPosition(GameConstants::BACKPACK_START + (s + backpackPos)%artsInBackpack);
  367. const CArtifactInstance *art = curHero->getArt(slotID);
  368. assert(art);
  369. if(!vstd::contains(toOmit, art))
  370. {
  371. if(s - omitedSoFar < backpack.size())
  372. setSlotData(backpack[s-omitedSoFar], slotID);
  373. }
  374. else
  375. {
  376. toOmit -= art;
  377. omitedSoFar++;
  378. continue;
  379. }
  380. }
  381. }
  382. for( ; s - omitedSoFar < backpack.size(); s++)
  383. eraseSlotData(backpack[s-omitedSoFar], ArtifactPosition(GameConstants::BACKPACK_START + (si32)s));
  384. //in artifact merchant selling artifacts we may have highlight on one of backpack artifacts -> market needs update, cause artifact under highlight changed
  385. if(highlightModeCallback)
  386. {
  387. for(auto & elem : backpack)
  388. {
  389. if(elem->marked)
  390. {
  391. highlightModeCallback(elem.get());
  392. break;
  393. }
  394. }
  395. }
  396. //blocking scrolling if there is not enough artifacts to scroll
  397. bool scrollingPossible = artsInBackpack - omitedSoFar > backpack.size();
  398. leftArtRoll->block(!scrollingPossible);
  399. rightArtRoll->block(!scrollingPossible);
  400. safeRedraw();
  401. }
  402. /**
  403. * Marks possible slots where a given artifact can be placed, except backpack.
  404. *
  405. * @param art Artifact checked against.
  406. */
  407. void CArtifactsOfHero::markPossibleSlots(const CArtifactInstance* art)
  408. {
  409. for(CArtifactsOfHero *aoh : commonInfo->participants)
  410. for(auto p : aoh->artWorn)
  411. p.second->selectSlot(art->canBePutAt(ArtifactLocation(aoh->curHero, p.second->slotID), true));
  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. updateState(false),
  472. leftArtRoll(leftScroll),
  473. rightArtRoll(rightScroll),
  474. allowedAssembling(true),
  475. highlightModeCallback(nullptr)
  476. {
  477. if(createCommonPart)
  478. {
  479. commonInfo = std::make_shared<CArtifactsOfHero::SCommonPart>();
  480. commonInfo->participants.insert(this);
  481. }
  482. // Init slots for worn artifacts.
  483. for(auto p : artWorn)
  484. {
  485. p.second->ourOwner = this;
  486. eraseSlotData(p.second, p.first);
  487. }
  488. // Init slots for the backpack.
  489. for(size_t s=0; s<backpack.size(); ++s)
  490. {
  491. backpack[s]->ourOwner = this;
  492. eraseSlotData(backpack[s], ArtifactPosition(GameConstants::BACKPACK_START + (si32)s));
  493. }
  494. leftArtRoll->addCallback(std::bind(&CArtifactsOfHero::scrollBackpack, this,-1));
  495. rightArtRoll->addCallback(std::bind(&CArtifactsOfHero::scrollBackpack, this,+1));
  496. }
  497. CArtifactsOfHero::CArtifactsOfHero(const Point & position, bool createCommonPart)
  498. : curHero(nullptr),
  499. backpackPos(0),
  500. commonInfo(nullptr),
  501. updateState(false),
  502. allowedAssembling(true),
  503. highlightModeCallback(nullptr)
  504. {
  505. if(createCommonPart)
  506. {
  507. commonInfo = std::make_shared<CArtifactsOfHero::SCommonPart>();
  508. commonInfo->participants.insert(this);
  509. }
  510. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  511. pos += position;
  512. std::vector<Point> slotPos =
  513. {
  514. Point(509,30), Point(567,240), Point(509,80), //0-2
  515. Point(383,68), Point(564,183), Point(509,130), //3-5
  516. Point(431,68), Point(610,183), Point(515,295), //6-8
  517. Point(383,143), Point(399,194), Point(415,245), //9-11
  518. Point(431,296), Point(564,30), Point(610,30), //12-14
  519. Point(610,76), Point(610,122), Point(610,310), //15-17
  520. Point(381,296) //18
  521. };
  522. // Create slots for worn artifacts.
  523. for(si32 g = 0; g < GameConstants::BACKPACK_START; g++)
  524. {
  525. artWorn[ArtifactPosition(g)] = std::make_shared<CHeroArtPlace>(slotPos[g]);
  526. artWorn[ArtifactPosition(g)]->ourOwner = this;
  527. eraseSlotData(artWorn[ArtifactPosition(g)], ArtifactPosition(g));
  528. }
  529. // Create slots for the backpack.
  530. for(int s=0; s<5; ++s)
  531. {
  532. auto add = std::make_shared<CHeroArtPlace>(Point(403 + 46 * s, 365));
  533. add->ourOwner = this;
  534. eraseSlotData(add, ArtifactPosition(GameConstants::BACKPACK_START + s));
  535. backpack.push_back(add);
  536. }
  537. leftArtRoll = std::make_shared<CButton>(Point(379, 364), "hsbtns3.def", CButton::tooltip(), [&](){ scrollBackpack(-1);}, SDLK_LEFT);
  538. rightArtRoll = std::make_shared<CButton>(Point(632, 364), "hsbtns5.def", CButton::tooltip(), [&](){ scrollBackpack(+1);}, SDLK_RIGHT);
  539. }
  540. CArtifactsOfHero::~CArtifactsOfHero()
  541. {
  542. dispose();
  543. // Artifact located in artifactsTransitionPos should be returned
  544. if(!curHero->artifactsTransitionPos.empty())
  545. {
  546. auto artPlace = getArtPlace(
  547. ArtifactUtils::getArtifactDstPosition(curHero->artifactsTransitionPos.begin()->artifact, curHero, curHero->bearerType()));
  548. assert(artPlace);
  549. assert(artPlace->ourOwner);
  550. artPlace->setMeAsDest();
  551. artPlace->ourOwner->realizeCurrentTransaction();
  552. }
  553. }
  554. void CArtifactsOfHero::updateParentWindow()
  555. {
  556. if (CHeroWindow* chw = dynamic_cast<CHeroWindow*>(GH.topInt().get()))
  557. {
  558. if(updateState)
  559. chw->curHero = curHero;
  560. else
  561. chw->update(curHero, true);
  562. }
  563. else if(CExchangeWindow* cew = dynamic_cast<CExchangeWindow*>(GH.topInt().get()))
  564. {
  565. //use our copy of hero to draw window
  566. if(cew->heroInst[0]->id == curHero->id)
  567. cew->heroInst[0] = curHero;
  568. else
  569. cew->heroInst[1] = curHero;
  570. if(!updateState)
  571. {
  572. cew->updateWidgets();
  573. cew->redraw();
  574. }
  575. }
  576. }
  577. void CArtifactsOfHero::safeRedraw()
  578. {
  579. if (active)
  580. {
  581. if(parent)
  582. parent->redraw();
  583. else
  584. redraw();
  585. }
  586. }
  587. void CArtifactsOfHero::realizeCurrentTransaction()
  588. {
  589. assert(commonInfo->src.AOH);
  590. assert(commonInfo->dst.AOH);
  591. LOCPLINT->cb->swapArtifacts(ArtifactLocation(commonInfo->src.AOH->curHero, commonInfo->src.slotID),
  592. ArtifactLocation(commonInfo->dst.AOH->curHero, commonInfo->dst.slotID));
  593. }
  594. void CArtifactsOfHero::artifactMoved(const ArtifactLocation & src, const ArtifactLocation & dst)
  595. {
  596. bool isCurHeroSrc = src.isHolder(curHero),
  597. isCurHeroDst = dst.isHolder(curHero);
  598. if(isCurHeroSrc && ArtifactUtils::isSlotBackpack(src.slot))
  599. updateSlot(src.slot);
  600. if(isCurHeroDst && ArtifactUtils::isSlotBackpack(dst.slot))
  601. updateSlot(dst.slot);
  602. // We need to update all slots, artifact might be combined and affect more slots
  603. if(isCurHeroSrc || isCurHeroDst)
  604. updateWornSlots(false);
  605. if(!isCurHeroSrc && !isCurHeroDst)
  606. return;
  607. // When moving one artifact onto another it leads to two art movements: dst->TRANSITION_POS; src->dst
  608. // however after first movement we pick the art from TRANSITION_POS and the second movement coming when
  609. // we have a different artifact may look surprising... but it's valid.
  610. // Used when doing dragAndDrop and artifact swap multiple times
  611. if(src.slot == ArtifactPosition::TRANSITION_POS &&
  612. commonInfo->src.slotID == ArtifactPosition::TRANSITION_POS &&
  613. commonInfo->dst.slotID == ArtifactPosition::PRE_FIRST &&
  614. isCurHeroDst)
  615. {
  616. auto art = curHero->getArt(ArtifactPosition::TRANSITION_POS);
  617. assert(art);
  618. CCS->curh->dragAndDropCursor("artifact", art->artType->getIconIndex());
  619. markPossibleSlots(art);
  620. commonInfo->src.art = art;
  621. commonInfo->src.slotID = src.slot;
  622. }
  623. // Artifact was taken from us
  624. else if(commonInfo->src == src && dst.slot != ArtifactPosition::TRANSITION_POS)
  625. {
  626. // Expected movement from slot ot slot
  627. assert(commonInfo->dst == dst
  628. // Artifact moved back to backpack (eg. to make place for art we are moving)
  629. || dst.slot == dst.getHolderArtSet()->artifactsInBackpack.size() + GameConstants::BACKPACK_START
  630. || dst.getHolderArtSet()->bearerType() != ArtBearer::HERO);
  631. commonInfo->reset();
  632. unmarkSlots();
  633. }
  634. else
  635. {
  636. // The dest artifact was moved after the swap -> we are picking it
  637. if(commonInfo->dst == src)
  638. {
  639. assert(dst.slot == ArtifactPosition::TRANSITION_POS);
  640. commonInfo->reset();
  641. for(CArtifactsOfHero * aoh : commonInfo->participants)
  642. {
  643. if(dst.isHolder(aoh->curHero))
  644. {
  645. commonInfo->src.AOH = aoh;
  646. break;
  647. }
  648. }
  649. commonInfo->src.art = dst.getArt();
  650. commonInfo->src.slotID = dst.slot;
  651. assert(commonInfo->src.AOH);
  652. CCS->curh->dragAndDropCursor("artifact", dst.getArt()->artType->getIconIndex());
  653. }
  654. if(!curHero->artifactsTransitionPos.empty())
  655. {
  656. markPossibleSlots(curHero->getArt(ArtifactPosition::TRANSITION_POS));
  657. }
  658. }
  659. updateParentWindow();
  660. // If backpack is changed, update it
  661. if((isCurHeroSrc && ArtifactUtils::isSlotBackpack(src.slot))
  662. || (isCurHeroDst && ArtifactUtils::isSlotBackpack(dst.slot)))
  663. scrollBackpack(0);
  664. }
  665. void CArtifactsOfHero::artifactRemoved(const ArtifactLocation &al)
  666. {
  667. if(al.isHolder(curHero))
  668. {
  669. if(al.slot < GameConstants::BACKPACK_START)
  670. updateWornSlots(0);
  671. else
  672. scrollBackpack(0); //update backpack slots
  673. }
  674. }
  675. CArtifactsOfHero::ArtPlacePtr CArtifactsOfHero::getArtPlace(ArtifactPosition slot)
  676. {
  677. if(slot == ArtifactPosition::TRANSITION_POS)
  678. {
  679. return nullptr;
  680. }
  681. if(slot < GameConstants::BACKPACK_START)
  682. {
  683. if(artWorn.find(slot) == artWorn.end())
  684. {
  685. logGlobal->error("CArtifactsOfHero::getArtPlace: invalid slot %d", slot);
  686. return nullptr;
  687. }
  688. return artWorn[ArtifactPosition(slot)];
  689. }
  690. else
  691. {
  692. for(ArtPlacePtr ap : backpack)
  693. if(ap->slotID == slot)
  694. return ap;
  695. return nullptr;
  696. }
  697. }
  698. void CArtifactsOfHero::artifactUpdateSlots(const ArtifactLocation & al)
  699. {
  700. if(al.isHolder(curHero))
  701. {
  702. if(ArtifactUtils::isSlotBackpack(al.slot))
  703. updateBackpackSlots();
  704. else
  705. updateWornSlots();
  706. }
  707. }
  708. void CArtifactsOfHero::updateWornSlots(bool redrawParent)
  709. {
  710. for(auto p : artWorn)
  711. updateSlot(p.first);
  712. if(redrawParent)
  713. updateParentWindow();
  714. }
  715. void CArtifactsOfHero::updateBackpackSlots(bool redrawParent)
  716. {
  717. for(auto artPlace : backpack)
  718. updateSlot(artPlace->slotID);
  719. scrollBackpack(0);
  720. if(redrawParent)
  721. updateParentWindow();
  722. }
  723. const CGHeroInstance * CArtifactsOfHero::getHero() const
  724. {
  725. return curHero;
  726. }
  727. void CArtifactsOfHero::updateSlot(ArtifactPosition slotID)
  728. {
  729. setSlotData(getArtPlace(slotID), slotID);
  730. }
  731. CArtifactHolder::CArtifactHolder()
  732. {
  733. }
  734. void CWindowWithArtifacts::addSet(std::shared_ptr<CArtifactsOfHero> artSet)
  735. {
  736. artSets.emplace_back(artSet);
  737. }
  738. std::shared_ptr<CArtifactsOfHero::SCommonPart> CWindowWithArtifacts::getCommonPart()
  739. {
  740. for(auto artSetWeak : artSets)
  741. {
  742. std::shared_ptr<CArtifactsOfHero> realPtr = artSetWeak.lock();
  743. if(realPtr)
  744. return realPtr->commonInfo;
  745. }
  746. return std::shared_ptr<CArtifactsOfHero::SCommonPart>();
  747. }
  748. void CWindowWithArtifacts::artifactRemoved(const ArtifactLocation &artLoc)
  749. {
  750. for(auto artSetWeak : artSets)
  751. {
  752. std::shared_ptr<CArtifactsOfHero> realPtr = artSetWeak.lock();
  753. if(realPtr)
  754. realPtr->artifactRemoved(artLoc);
  755. }
  756. }
  757. void CWindowWithArtifacts::artifactMoved(const ArtifactLocation &artLoc, const ArtifactLocation &destLoc)
  758. {
  759. CArtifactsOfHero * destaoh = nullptr;
  760. for(auto artSetWeak : artSets)
  761. {
  762. std::shared_ptr<CArtifactsOfHero> realPtr = artSetWeak.lock();
  763. if(realPtr)
  764. {
  765. realPtr->artifactMoved(artLoc, destLoc);
  766. realPtr->redraw();
  767. if(destLoc.isHolder(realPtr->getHero()))
  768. destaoh = realPtr.get();
  769. }
  770. }
  771. //Make sure the status bar is updated so it does not display old text
  772. if(destaoh != nullptr && destaoh->getArtPlace(destLoc.slot) != nullptr)
  773. {
  774. destaoh->getArtPlace(destLoc.slot)->hover(true);
  775. }
  776. }
  777. void CWindowWithArtifacts::artifactDisassembled(const ArtifactLocation &artLoc)
  778. {
  779. for(auto artSetWeak : artSets)
  780. {
  781. std::shared_ptr<CArtifactsOfHero> realPtr = artSetWeak.lock();
  782. if(realPtr)
  783. realPtr->artifactUpdateSlots(artLoc);
  784. }
  785. }
  786. void CWindowWithArtifacts::artifactAssembled(const ArtifactLocation &artLoc)
  787. {
  788. for(auto artSetWeak : artSets)
  789. {
  790. std::shared_ptr<CArtifactsOfHero> realPtr = artSetWeak.lock();
  791. if(realPtr)
  792. realPtr->artifactUpdateSlots(artLoc);
  793. }
  794. }
  795. void CArtifactsOfHero::SCommonPart::Artpos::clear()
  796. {
  797. slotID = ArtifactPosition::PRE_FIRST;
  798. AOH = nullptr;
  799. art = nullptr;
  800. }
  801. CArtifactsOfHero::SCommonPart::Artpos::Artpos()
  802. {
  803. clear();
  804. }
  805. void CArtifactsOfHero::SCommonPart::Artpos::setTo(const CHeroArtPlace *place, bool dontTakeBackpack)
  806. {
  807. slotID = place->slotID;
  808. AOH = place->ourOwner;
  809. if(slotID >= 19 && 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. }