CArtifactHolder.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035
  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, bool withRedraw)
  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. if(withRedraw)
  413. safeRedraw();
  414. }
  415. /**
  416. * Unamarks all slots.
  417. */
  418. void CArtifactsOfHero::unmarkSlots(bool withRedraw)
  419. {
  420. if(commonInfo)
  421. for(CArtifactsOfHero *aoh : commonInfo->participants)
  422. aoh->unmarkLocalSlots(false);
  423. else
  424. unmarkLocalSlots(false);
  425. if(withRedraw)
  426. safeRedraw();
  427. }
  428. void CArtifactsOfHero::unmarkLocalSlots(bool withRedraw)
  429. {
  430. for(auto & p : artWorn)
  431. p.second->selectSlot(false);
  432. for(auto & place : backpack)
  433. place->selectSlot(false);
  434. if(withRedraw)
  435. safeRedraw();
  436. }
  437. /**
  438. * Assigns an artifacts to an artifact place depending on it's new slot ID.
  439. */
  440. void CArtifactsOfHero::setSlotData(ArtPlacePtr artPlace, ArtifactPosition slotID)
  441. {
  442. if(!artPlace && slotID >= GameConstants::BACKPACK_START) //spurious call from artifactMoved in attempt to update hidden backpack slot
  443. {
  444. return;
  445. }
  446. artPlace->pickSlot(false);
  447. artPlace->slotID = slotID;
  448. if(const ArtSlotInfo *asi = curHero->getSlot(slotID))
  449. {
  450. artPlace->lockSlot(asi->locked);
  451. artPlace->setArtifact(asi->artifact);
  452. }
  453. else
  454. artPlace->setArtifact(nullptr);
  455. }
  456. /**
  457. * Makes given artifact slot appear as empty with a certain slot ID.
  458. */
  459. void CArtifactsOfHero::eraseSlotData(ArtPlacePtr artPlace, ArtifactPosition slotID)
  460. {
  461. artPlace->pickSlot(false);
  462. artPlace->slotID = slotID;
  463. artPlace->setArtifact(nullptr);
  464. }
  465. CArtifactsOfHero::CArtifactsOfHero(ArtPlaceMap ArtWorn, std::vector<ArtPlacePtr> Backpack,
  466. std::shared_ptr<CButton> leftScroll, std::shared_ptr<CButton> rightScroll, bool createCommonPart)
  467. : curHero(nullptr),
  468. artWorn(ArtWorn),
  469. backpack(Backpack),
  470. backpackPos(0),
  471. commonInfo(nullptr),
  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. allowedAssembling(true),
  502. highlightModeCallback(nullptr)
  503. {
  504. if(createCommonPart)
  505. {
  506. commonInfo = std::make_shared<CArtifactsOfHero::SCommonPart>();
  507. commonInfo->participants.insert(this);
  508. }
  509. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  510. pos += position;
  511. std::vector<Point> slotPos =
  512. {
  513. Point(509,30), Point(567,240), Point(509,80), //0-2
  514. Point(383,68), Point(564,183), Point(509,130), //3-5
  515. Point(431,68), Point(610,183), Point(515,295), //6-8
  516. Point(383,143), Point(399,194), Point(415,245), //9-11
  517. Point(431,296), Point(564,30), Point(610,30), //12-14
  518. Point(610,76), Point(610,122), Point(610,310), //15-17
  519. Point(381,296) //18
  520. };
  521. // Create slots for worn artifacts.
  522. for(si32 g = 0; g < GameConstants::BACKPACK_START; g++)
  523. {
  524. artWorn[ArtifactPosition(g)] = std::make_shared<CHeroArtPlace>(slotPos[g]);
  525. artWorn[ArtifactPosition(g)]->ourOwner = this;
  526. eraseSlotData(artWorn[ArtifactPosition(g)], ArtifactPosition(g));
  527. }
  528. // Create slots for the backpack.
  529. for(int s=0; s<5; ++s)
  530. {
  531. auto add = std::make_shared<CHeroArtPlace>(Point(403 + 46 * s, 365));
  532. add->ourOwner = this;
  533. eraseSlotData(add, ArtifactPosition(GameConstants::BACKPACK_START + s));
  534. backpack.push_back(add);
  535. }
  536. leftArtRoll = std::make_shared<CButton>(Point(379, 364), "hsbtns3.def", CButton::tooltip(), [&](){ scrollBackpack(-1);}, SDLK_LEFT);
  537. rightArtRoll = std::make_shared<CButton>(Point(632, 364), "hsbtns5.def", CButton::tooltip(), [&](){ scrollBackpack(+1);}, SDLK_RIGHT);
  538. }
  539. CArtifactsOfHero::~CArtifactsOfHero()
  540. {
  541. dispose();
  542. // Artifact located in artifactsTransitionPos should be returned
  543. if(!curHero->artifactsTransitionPos.empty())
  544. {
  545. auto artPlace = getArtPlace(
  546. ArtifactUtils::getArtifactDstPosition(curHero->artifactsTransitionPos.begin()->artifact, curHero));
  547. assert(artPlace);
  548. assert(artPlace->ourOwner);
  549. artPlace->setMeAsDest();
  550. artPlace->ourOwner->realizeCurrentTransaction();
  551. }
  552. }
  553. void CArtifactsOfHero::updateParentWindow()
  554. {
  555. if(CHeroWindow * chw = dynamic_cast<CHeroWindow*>(GH.topInt().get()))
  556. {
  557. chw->update(curHero, true);
  558. }
  559. else if(CExchangeWindow * cew = dynamic_cast<CExchangeWindow*>(GH.topInt().get()))
  560. {
  561. cew->updateWidgets();
  562. }
  563. }
  564. void CArtifactsOfHero::safeRedraw()
  565. {
  566. if (active)
  567. {
  568. if(parent)
  569. parent->redraw();
  570. else
  571. redraw();
  572. }
  573. }
  574. void CArtifactsOfHero::realizeCurrentTransaction()
  575. {
  576. assert(commonInfo->src.AOH);
  577. assert(commonInfo->dst.AOH);
  578. LOCPLINT->cb->swapArtifacts(ArtifactLocation(commonInfo->src.AOH->curHero, commonInfo->src.slotID),
  579. ArtifactLocation(commonInfo->dst.AOH->curHero, commonInfo->dst.slotID));
  580. }
  581. void CArtifactsOfHero::artifactMoved(const ArtifactLocation & src, const ArtifactLocation & dst, bool withUIUpdate)
  582. {
  583. bool isCurHeroSrc = src.isHolder(curHero),
  584. isCurHeroDst = dst.isHolder(curHero);
  585. if(isCurHeroSrc && ArtifactUtils::isSlotBackpack(src.slot))
  586. updateSlot(src.slot);
  587. if(isCurHeroDst && ArtifactUtils::isSlotBackpack(dst.slot))
  588. updateSlot(dst.slot);
  589. // We need to update all slots, artifact might be combined and affect more slots
  590. if(isCurHeroSrc || isCurHeroDst)
  591. updateWornSlots(false);
  592. if(!isCurHeroSrc && !isCurHeroDst)
  593. return;
  594. // When moving one artifact onto another it leads to two art movements: dst->TRANSITION_POS; src->dst
  595. // however after first movement we pick the art from TRANSITION_POS and the second movement coming when
  596. // we have a different artifact may look surprising... but it's valid.
  597. // Used when doing dragAndDrop and artifact swap multiple times
  598. if(src.slot == ArtifactPosition::TRANSITION_POS &&
  599. commonInfo->src.slotID == ArtifactPosition::TRANSITION_POS &&
  600. commonInfo->dst.slotID == ArtifactPosition::PRE_FIRST &&
  601. isCurHeroDst)
  602. {
  603. auto art = curHero->getArt(ArtifactPosition::TRANSITION_POS);
  604. assert(art);
  605. CCS->curh->dragAndDropCursor("artifact", art->artType->getIconIndex());
  606. if(withUIUpdate)
  607. markPossibleSlots(art);
  608. commonInfo->src.art = art;
  609. commonInfo->src.slotID = src.slot;
  610. }
  611. // Artifact was taken from us
  612. else if(commonInfo->src == src && dst.slot != ArtifactPosition::TRANSITION_POS)
  613. {
  614. // Expected movement from slot ot slot
  615. assert(commonInfo->dst == dst
  616. // Artifact moved back to backpack (eg. to make place for art we are moving)
  617. || dst.slot == dst.getHolderArtSet()->artifactsInBackpack.size() + GameConstants::BACKPACK_START
  618. || dst.getHolderArtSet()->bearerType() != ArtBearer::HERO);
  619. commonInfo->reset();
  620. unmarkSlots();
  621. }
  622. else
  623. {
  624. // The dest artifact was moved after the swap -> we are picking it
  625. if(commonInfo->dst == src)
  626. {
  627. assert(dst.slot == ArtifactPosition::TRANSITION_POS);
  628. commonInfo->reset();
  629. for(CArtifactsOfHero * aoh : commonInfo->participants)
  630. {
  631. if(dst.isHolder(aoh->curHero))
  632. {
  633. commonInfo->src.AOH = aoh;
  634. break;
  635. }
  636. }
  637. commonInfo->src.art = dst.getArt();
  638. commonInfo->src.slotID = dst.slot;
  639. assert(commonInfo->src.AOH);
  640. CCS->curh->dragAndDropCursor("artifact", dst.getArt()->artType->getIconIndex());
  641. }
  642. if(!curHero->artifactsTransitionPos.empty() && withUIUpdate)
  643. {
  644. markPossibleSlots(curHero->getArt(ArtifactPosition::TRANSITION_POS));
  645. }
  646. }
  647. if(withUIUpdate)
  648. {
  649. updateParentWindow();
  650. scrollBackpack(0);
  651. }
  652. }
  653. void CArtifactsOfHero::artifactRemoved(const ArtifactLocation &al)
  654. {
  655. if(al.isHolder(curHero))
  656. {
  657. if(al.slot < GameConstants::BACKPACK_START)
  658. updateWornSlots(0);
  659. else
  660. scrollBackpack(0); //update backpack slots
  661. }
  662. }
  663. CArtifactsOfHero::ArtPlacePtr CArtifactsOfHero::getArtPlace(ArtifactPosition slot)
  664. {
  665. if(slot == ArtifactPosition::TRANSITION_POS)
  666. {
  667. return nullptr;
  668. }
  669. if(slot < GameConstants::BACKPACK_START)
  670. {
  671. if(artWorn.find(slot) == artWorn.end())
  672. {
  673. logGlobal->error("CArtifactsOfHero::getArtPlace: invalid slot %d", slot);
  674. return nullptr;
  675. }
  676. return artWorn[ArtifactPosition(slot)];
  677. }
  678. else
  679. {
  680. for(ArtPlacePtr ap : backpack)
  681. if(ap->slotID == slot)
  682. return ap;
  683. return nullptr;
  684. }
  685. }
  686. void CArtifactsOfHero::artifactUpdateSlots(const ArtifactLocation & al)
  687. {
  688. if(al.isHolder(curHero))
  689. {
  690. if(ArtifactUtils::isSlotBackpack(al.slot))
  691. updateBackpackSlots(true);
  692. else
  693. updateWornSlots(true);
  694. }
  695. }
  696. void CArtifactsOfHero::updateWornSlots(bool redrawParent)
  697. {
  698. for(auto p : artWorn)
  699. updateSlot(p.first);
  700. if(redrawParent)
  701. updateParentWindow();
  702. }
  703. void CArtifactsOfHero::updateBackpackSlots(bool redrawParent)
  704. {
  705. for(auto artPlace : backpack)
  706. updateSlot(artPlace->slotID);
  707. scrollBackpack(0);
  708. if(redrawParent)
  709. updateParentWindow();
  710. }
  711. const CGHeroInstance * CArtifactsOfHero::getHero() const
  712. {
  713. return curHero;
  714. }
  715. void CArtifactsOfHero::updateSlot(ArtifactPosition slotID)
  716. {
  717. setSlotData(getArtPlace(slotID), slotID);
  718. }
  719. CArtifactHolder::CArtifactHolder()
  720. {
  721. }
  722. void CWindowWithArtifacts::addSet(std::shared_ptr<CArtifactsOfHero> artSet)
  723. {
  724. artSets.emplace_back(artSet);
  725. }
  726. std::shared_ptr<CArtifactsOfHero::SCommonPart> CWindowWithArtifacts::getCommonPart()
  727. {
  728. for(auto artSetWeak : artSets)
  729. {
  730. std::shared_ptr<CArtifactsOfHero> realPtr = artSetWeak.lock();
  731. if(realPtr)
  732. return realPtr->commonInfo;
  733. }
  734. return std::shared_ptr<CArtifactsOfHero::SCommonPart>();
  735. }
  736. void CWindowWithArtifacts::artifactRemoved(const ArtifactLocation &artLoc)
  737. {
  738. for(auto artSetWeak : artSets)
  739. {
  740. std::shared_ptr<CArtifactsOfHero> realPtr = artSetWeak.lock();
  741. if(realPtr)
  742. realPtr->artifactRemoved(artLoc);
  743. }
  744. }
  745. void CWindowWithArtifacts::artifactMoved(const ArtifactLocation &artLoc, const ArtifactLocation &destLoc, bool withRedraw)
  746. {
  747. CArtifactsOfHero * destaoh = nullptr;
  748. for(auto artSetWeak : artSets)
  749. {
  750. std::shared_ptr<CArtifactsOfHero> realPtr = artSetWeak.lock();
  751. if(realPtr)
  752. {
  753. realPtr->artifactMoved(artLoc, destLoc, withRedraw);
  754. if(destLoc.isHolder(realPtr->getHero()))
  755. destaoh = realPtr.get();
  756. }
  757. }
  758. //Make sure the status bar is updated so it does not display old text
  759. if(destaoh != nullptr && destaoh->getArtPlace(destLoc.slot) != nullptr)
  760. {
  761. destaoh->getArtPlace(destLoc.slot)->hover(true);
  762. }
  763. }
  764. void CWindowWithArtifacts::artifactDisassembled(const ArtifactLocation &artLoc)
  765. {
  766. for(auto artSetWeak : artSets)
  767. {
  768. std::shared_ptr<CArtifactsOfHero> realPtr = artSetWeak.lock();
  769. if(realPtr)
  770. realPtr->artifactUpdateSlots(artLoc);
  771. }
  772. }
  773. void CWindowWithArtifacts::artifactAssembled(const ArtifactLocation &artLoc)
  774. {
  775. for(auto artSetWeak : artSets)
  776. {
  777. std::shared_ptr<CArtifactsOfHero> realPtr = artSetWeak.lock();
  778. if(realPtr)
  779. realPtr->artifactUpdateSlots(artLoc);
  780. }
  781. }
  782. void CArtifactsOfHero::SCommonPart::Artpos::clear()
  783. {
  784. slotID = ArtifactPosition::PRE_FIRST;
  785. AOH = nullptr;
  786. art = nullptr;
  787. }
  788. void CArtifactsOfHero::SCommonPart::Artpos::setTo(const CHeroArtPlace *place, bool dontTakeBackpack)
  789. {
  790. slotID = place->slotID;
  791. AOH = place->ourOwner;
  792. if(ArtifactUtils::isSlotBackpack(slotID) && dontTakeBackpack)
  793. art = nullptr;
  794. else
  795. art = place->ourArt;
  796. }
  797. bool CArtifactsOfHero::SCommonPart::Artpos::operator==(const ArtifactLocation &al) const
  798. {
  799. if(!AOH)
  800. return false;
  801. bool ret = al.isHolder(AOH->curHero) && al.slot == slotID;
  802. //assert(al.getArt() == art);
  803. return ret;
  804. }
  805. bool CArtifactsOfHero::SCommonPart::Artpos::valid()
  806. {
  807. assert(AOH && art);
  808. return art == AOH->curHero->getArt(slotID);
  809. }
  810. CArtPlace::CArtPlace(Point position, const CArtifactInstance * Art) : ourArt(Art)
  811. {
  812. image = nullptr;
  813. pos += position;
  814. pos.w = pos.h = 44;
  815. }
  816. void CArtPlace::clickLeft(tribool down, bool previousState)
  817. {
  818. LRClickableAreaWTextComp::clickLeft(down, previousState);
  819. }
  820. void CArtPlace::clickRight(tribool down, bool previousState)
  821. {
  822. LRClickableAreaWTextComp::clickRight(down, previousState);
  823. }
  824. CCommanderArtPlace::CCommanderArtPlace(Point position, const CGHeroInstance * commanderOwner, ArtifactPosition artSlot, const CArtifactInstance * Art) : CArtPlace(position, Art), commanderOwner(commanderOwner), commanderSlotID(artSlot.num)
  825. {
  826. createImage();
  827. setArtifact(Art);
  828. }
  829. void CCommanderArtPlace::clickLeft(tribool down, bool previousState)
  830. {
  831. if (ourArt && text.size() && down)
  832. LOCPLINT->showYesNoDialog(CGI->generaltexth->translate("vcmi.commanderWindow.artifactMessage"), [this](){ returnArtToHeroCallback(); }, [](){});
  833. }
  834. void CCommanderArtPlace::clickRight(tribool down, bool previousState)
  835. {
  836. if (ourArt && text.size() && down)
  837. CArtPlace::clickRight(down, previousState);
  838. }
  839. void CCommanderArtPlace::createImage()
  840. {
  841. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  842. int imageIndex = 0;
  843. if(ourArt)
  844. imageIndex = ourArt->artType->getIconIndex();
  845. image = std::make_shared<CAnimImage>("artifact", imageIndex);
  846. if(!ourArt)
  847. image->disable();
  848. }
  849. void CCommanderArtPlace::returnArtToHeroCallback()
  850. {
  851. ArtifactPosition artifactPos = commanderSlotID;
  852. ArtifactPosition freeSlot = ourArt->firstBackpackSlot(commanderOwner);
  853. ArtifactLocation src(commanderOwner->commander.get(), artifactPos);
  854. ArtifactLocation dst(commanderOwner, freeSlot);
  855. if (ourArt->canBePutAt(dst, true))
  856. {
  857. LOCPLINT->cb->swapArtifacts(src, dst);
  858. setArtifact(nullptr);
  859. parent->redraw();
  860. }
  861. }
  862. void CCommanderArtPlace::setArtifact(const CArtifactInstance * art)
  863. {
  864. baseType = -1; //by default we don't store any component
  865. ourArt = art;
  866. if (!art)
  867. {
  868. image->disable();
  869. text.clear();
  870. return;
  871. }
  872. image->enable();
  873. image->setFrame(art->artType->getIconIndex());
  874. text = art->getEffectiveDescription();
  875. if (art->artType->getId() == ArtifactID::SPELL_SCROLL)
  876. {
  877. int spellID = art->getGivenSpellID();
  878. if (spellID >= 0)
  879. {
  880. //add spell component info (used to provide a pic in r-click popup)
  881. baseType = CComponent::spell;
  882. type = spellID;
  883. bonusValue = 0;
  884. }
  885. }
  886. else
  887. {
  888. baseType = CComponent::artifact;
  889. type = art->artType->getId();
  890. bonusValue = 0;
  891. }
  892. }