CArtifactHolder.cpp 28 KB

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