CArtifactHolder.cpp 28 KB

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