CArtifactHolder.cpp 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084
  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/CCursorHandler.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->id == ArtifactID::SPELLBOOK)
  107. GH.pushIntT<CSpellWindow>(ourOwner->curHero, LOCPLINT, LOCPLINT->battleInt);
  108. }
  109. if (!down && previousState)
  110. {
  111. if(ourArt && ourArt->artType->id == 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->id == 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->id == 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->getName()));
  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->id));
  194. if(assemblyPossibilities.size() > 2)
  195. logGlobal->warn("More than one possibility of assembling on %s... taking only first", art->artType->getName());
  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(make_unique<CAnimImage>("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. selectSlot(true);
  245. pickSlot(true);
  246. if(ourArt->canBeDisassembled() && slotID < GameConstants::BACKPACK_START) //worn combined artifact -> locks have to disappear
  247. {
  248. for(int i = 0; i < GameConstants::BACKPACK_START; i++)
  249. {
  250. auto ap = ourOwner->getArtPlace(i);
  251. if(ap)//getArtPlace may return null
  252. ap->pickSlot(ourArt->isPart(ap->ourArt));
  253. }
  254. }
  255. CCS->curh->dragAndDropCursor(make_unique<CAnimImage>("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(int i = 0; i < GameConstants::BACKPACK_START; i++)
  272. {
  273. auto place = ourOwner->getArtPlace(i);
  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::SDL_PutPixelWithoutRefresh(to, pos.x, pos.y + i, 240, 220, 120);
  298. CSDL_Ext::SDL_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::SDL_PutPixelWithoutRefresh(to, pos.x + i, pos.y, 240, 220, 120);
  304. CSDL_Ext::SDL_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->id == 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->id;
  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->getName());
  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. }
  578. void CArtifactsOfHero::updateParentWindow()
  579. {
  580. if (CHeroWindow* chw = dynamic_cast<CHeroWindow*>(GH.topInt().get()))
  581. {
  582. if(updateState)
  583. chw->curHero = curHero;
  584. else
  585. chw->update(curHero, true);
  586. }
  587. else if(CExchangeWindow* cew = dynamic_cast<CExchangeWindow*>(GH.topInt().get()))
  588. {
  589. //use our copy of hero to draw window
  590. if(cew->heroInst[0]->id == curHero->id)
  591. cew->heroInst[0] = curHero;
  592. else
  593. cew->heroInst[1] = curHero;
  594. if(!updateState)
  595. {
  596. cew->deactivate();
  597. cew->updateWidgets();
  598. cew->redraw();
  599. cew->activate();
  600. }
  601. }
  602. }
  603. void CArtifactsOfHero::safeRedraw()
  604. {
  605. if (active)
  606. {
  607. if(parent)
  608. parent->redraw();
  609. else
  610. redraw();
  611. }
  612. }
  613. void CArtifactsOfHero::realizeCurrentTransaction()
  614. {
  615. assert(commonInfo->src.AOH);
  616. assert(commonInfo->dst.AOH);
  617. LOCPLINT->cb->swapArtifacts(ArtifactLocation(commonInfo->src.AOH->curHero, commonInfo->src.slotID),
  618. ArtifactLocation(commonInfo->dst.AOH->curHero, commonInfo->dst.slotID));
  619. }
  620. void CArtifactsOfHero::artifactMoved(const ArtifactLocation &src, const ArtifactLocation &dst)
  621. {
  622. bool isCurHeroSrc = src.isHolder(curHero),
  623. isCurHeroDst = dst.isHolder(curHero);
  624. if(isCurHeroSrc && src.slot >= GameConstants::BACKPACK_START)
  625. updateSlot(src.slot);
  626. if(isCurHeroDst && dst.slot >= GameConstants::BACKPACK_START)
  627. updateSlot(dst.slot);
  628. if(isCurHeroSrc || isCurHeroDst) //we need to update all slots, artifact might be combined and affect more slots
  629. updateWornSlots(false);
  630. if (!src.isHolder(curHero) && !isCurHeroDst)
  631. return;
  632. if(commonInfo->src == src) //artifact was taken from us
  633. {
  634. assert(commonInfo->dst == dst //expected movement from slot ot slot
  635. || dst.slot == dst.getHolderArtSet()->artifactsInBackpack.size() + GameConstants::BACKPACK_START //artifact moved back to backpack (eg. to make place for art we are moving)
  636. || dst.getHolderArtSet()->bearerType() != ArtBearer::HERO);
  637. commonInfo->reset();
  638. unmarkSlots();
  639. }
  640. else if(commonInfo->dst == src) //the dest artifact was moved -> we are picking it
  641. {
  642. assert(dst.slot >= GameConstants::BACKPACK_START);
  643. commonInfo->reset();
  644. CArtifactsOfHero::ArtPlacePtr ap;
  645. for(CArtifactsOfHero *aoh : commonInfo->participants)
  646. {
  647. if(dst.isHolder(aoh->curHero))
  648. {
  649. commonInfo->src.AOH = aoh;
  650. if((ap = aoh->getArtPlace(dst.slot)))//getArtPlace may return null
  651. break;
  652. }
  653. }
  654. if(ap)
  655. {
  656. ap->select();
  657. }
  658. else
  659. {
  660. commonInfo->src.art = dst.getArt();
  661. commonInfo->src.slotID = dst.slot;
  662. assert(commonInfo->src.AOH);
  663. CCS->curh->dragAndDropCursor(make_unique<CAnimImage>("artifact", dst.getArt()->artType->getIconIndex()));
  664. markPossibleSlots(dst.getArt());
  665. }
  666. }
  667. else if(src.slot >= GameConstants::BACKPACK_START &&
  668. src.slot < commonInfo->src.slotID &&
  669. src.isHolder(commonInfo->src.AOH->curHero)) //artifact taken from before currently picked one
  670. {
  671. //int fixedSlot = src.hero->getArtPos(commonInfo->src.art);
  672. vstd::advance(commonInfo->src.slotID, -1);
  673. assert(commonInfo->src.valid());
  674. }
  675. else
  676. {
  677. //when moving one artifact onto another it leads to two art movements: dst->backapck; src->dst
  678. // however after first movement we pick the art from backpack and the second movement coming when
  679. // we have a different artifact may look surprising... but it's valid.
  680. }
  681. updateParentWindow();
  682. int shift = 0;
  683. // if(dst.slot >= Arts::BACKPACK_START && dst.slot - Arts::BACKPACK_START < backpackPos)
  684. // shift++;
  685. //
  686. if(src.slot < GameConstants::BACKPACK_START && dst.slot - GameConstants::BACKPACK_START < backpackPos)
  687. shift++;
  688. if(dst.slot < GameConstants::BACKPACK_START && src.slot - GameConstants::BACKPACK_START < backpackPos)
  689. shift--;
  690. if( (isCurHeroSrc && src.slot >= GameConstants::BACKPACK_START)
  691. || (isCurHeroDst && dst.slot >= GameConstants::BACKPACK_START) )
  692. scrollBackpack(shift); //update backpack slots
  693. }
  694. void CArtifactsOfHero::artifactRemoved(const ArtifactLocation &al)
  695. {
  696. if(al.isHolder(curHero))
  697. {
  698. if(al.slot < GameConstants::BACKPACK_START)
  699. updateWornSlots(0);
  700. else
  701. scrollBackpack(0); //update backpack slots
  702. }
  703. }
  704. CArtifactsOfHero::ArtPlacePtr CArtifactsOfHero::getArtPlace(int slot)
  705. {
  706. if(slot < GameConstants::BACKPACK_START)
  707. {
  708. if(artWorn.find(ArtifactPosition(slot)) == artWorn.end())
  709. {
  710. logGlobal->error("CArtifactsOfHero::getArtPlace: invalid slot %d", slot);
  711. return nullptr;
  712. }
  713. return artWorn[ArtifactPosition(slot)];
  714. }
  715. else
  716. {
  717. for(ArtPlacePtr ap : backpack)
  718. if(ap->slotID == slot)
  719. return ap;
  720. return nullptr;
  721. }
  722. }
  723. void CArtifactsOfHero::artifactUpdateSlots(const ArtifactLocation & al)
  724. {
  725. if(al.isHolder(curHero))
  726. {
  727. if(ArtifactUtils::isSlotBackpack(al.slot))
  728. updateBackpackSlots();
  729. else
  730. updateWornSlots();
  731. }
  732. }
  733. void CArtifactsOfHero::updateWornSlots(bool redrawParent)
  734. {
  735. for(auto p : artWorn)
  736. updateSlot(p.first);
  737. if(redrawParent)
  738. updateParentWindow();
  739. }
  740. void CArtifactsOfHero::updateBackpackSlots(bool redrawParent)
  741. {
  742. for(auto artPlace : backpack)
  743. updateSlot(artPlace->slotID);
  744. scrollBackpack(0);
  745. if(redrawParent)
  746. updateParentWindow();
  747. }
  748. const CGHeroInstance * CArtifactsOfHero::getHero() const
  749. {
  750. return curHero;
  751. }
  752. void CArtifactsOfHero::updateSlot(ArtifactPosition slotID)
  753. {
  754. setSlotData(getArtPlace(slotID), slotID);
  755. }
  756. CArtifactHolder::CArtifactHolder()
  757. {
  758. }
  759. void CWindowWithArtifacts::addSet(std::shared_ptr<CArtifactsOfHero> artSet)
  760. {
  761. artSets.emplace_back(artSet);
  762. }
  763. std::shared_ptr<CArtifactsOfHero::SCommonPart> CWindowWithArtifacts::getCommonPart()
  764. {
  765. for(auto artSetWeak : artSets)
  766. {
  767. std::shared_ptr<CArtifactsOfHero> realPtr = artSetWeak.lock();
  768. if(realPtr)
  769. return realPtr->commonInfo;
  770. }
  771. return std::shared_ptr<CArtifactsOfHero::SCommonPart>();
  772. }
  773. void CWindowWithArtifacts::artifactRemoved(const ArtifactLocation &artLoc)
  774. {
  775. for(auto artSetWeak : artSets)
  776. {
  777. std::shared_ptr<CArtifactsOfHero> realPtr = artSetWeak.lock();
  778. if(realPtr)
  779. realPtr->artifactRemoved(artLoc);
  780. }
  781. }
  782. void CWindowWithArtifacts::artifactMoved(const ArtifactLocation &artLoc, const ArtifactLocation &destLoc)
  783. {
  784. CArtifactsOfHero * destaoh = nullptr;
  785. for(auto artSetWeak : artSets)
  786. {
  787. std::shared_ptr<CArtifactsOfHero> realPtr = artSetWeak.lock();
  788. if(realPtr)
  789. {
  790. realPtr->artifactMoved(artLoc, destLoc);
  791. realPtr->redraw();
  792. if(destLoc.isHolder(realPtr->getHero()))
  793. destaoh = realPtr.get();
  794. }
  795. }
  796. //Make sure the status bar is updated so it does not display old text
  797. if(destaoh != nullptr && destaoh->getArtPlace(destLoc.slot) != nullptr)
  798. {
  799. destaoh->getArtPlace(destLoc.slot)->hover(true);
  800. }
  801. }
  802. void CWindowWithArtifacts::artifactDisassembled(const ArtifactLocation &artLoc)
  803. {
  804. for(auto artSetWeak : artSets)
  805. {
  806. std::shared_ptr<CArtifactsOfHero> realPtr = artSetWeak.lock();
  807. if(realPtr)
  808. realPtr->artifactUpdateSlots(artLoc);
  809. }
  810. }
  811. void CWindowWithArtifacts::artifactAssembled(const ArtifactLocation &artLoc)
  812. {
  813. for(auto artSetWeak : artSets)
  814. {
  815. std::shared_ptr<CArtifactsOfHero> realPtr = artSetWeak.lock();
  816. if(realPtr)
  817. realPtr->artifactUpdateSlots(artLoc);
  818. }
  819. }
  820. void CArtifactsOfHero::SCommonPart::Artpos::clear()
  821. {
  822. slotID = ArtifactPosition::PRE_FIRST;
  823. AOH = nullptr;
  824. art = nullptr;
  825. }
  826. CArtifactsOfHero::SCommonPart::Artpos::Artpos()
  827. {
  828. clear();
  829. }
  830. void CArtifactsOfHero::SCommonPart::Artpos::setTo(const CHeroArtPlace *place, bool dontTakeBackpack)
  831. {
  832. slotID = place->slotID;
  833. AOH = place->ourOwner;
  834. if(slotID >= 19 && dontTakeBackpack)
  835. art = nullptr;
  836. else
  837. art = place->ourArt;
  838. }
  839. bool CArtifactsOfHero::SCommonPart::Artpos::operator==(const ArtifactLocation &al) const
  840. {
  841. if(!AOH)
  842. return false;
  843. bool ret = al.isHolder(AOH->curHero) && al.slot == slotID;
  844. //assert(al.getArt() == art);
  845. return ret;
  846. }
  847. bool CArtifactsOfHero::SCommonPart::Artpos::valid()
  848. {
  849. assert(AOH && art);
  850. return art == AOH->curHero->getArt(slotID);
  851. }
  852. CArtPlace::CArtPlace(Point position, const CArtifactInstance * Art) : ourArt(Art)
  853. {
  854. image = nullptr;
  855. pos += position;
  856. pos.w = pos.h = 44;
  857. }
  858. void CArtPlace::clickLeft(tribool down, bool previousState)
  859. {
  860. LRClickableAreaWTextComp::clickLeft(down, previousState);
  861. }
  862. void CArtPlace::clickRight(tribool down, bool previousState)
  863. {
  864. LRClickableAreaWTextComp::clickRight(down, previousState);
  865. }
  866. CCommanderArtPlace::CCommanderArtPlace(Point position, const CGHeroInstance * commanderOwner, ArtifactPosition artSlot, const CArtifactInstance * Art) : CArtPlace(position, Art), commanderOwner(commanderOwner), commanderSlotID(artSlot.num)
  867. {
  868. createImage();
  869. setArtifact(Art);
  870. }
  871. void CCommanderArtPlace::clickLeft(tribool down, bool previousState)
  872. {
  873. if (ourArt && text.size() && down)
  874. LOCPLINT->showYesNoDialog(CGI->generaltexth->localizedTexts["commanderWindow"]["artifactMessage"].String(), [this](){ returnArtToHeroCallback(); }, [](){});
  875. }
  876. void CCommanderArtPlace::clickRight(tribool down, bool previousState)
  877. {
  878. if (ourArt && text.size() && down)
  879. CArtPlace::clickRight(down, previousState);
  880. }
  881. void CCommanderArtPlace::createImage()
  882. {
  883. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  884. int imageIndex = 0;
  885. if(ourArt)
  886. imageIndex = ourArt->artType->getIconIndex();
  887. image = std::make_shared<CAnimImage>("artifact", imageIndex);
  888. if(!ourArt)
  889. image->disable();
  890. }
  891. void CCommanderArtPlace::returnArtToHeroCallback()
  892. {
  893. ArtifactPosition artifactPos = commanderSlotID;
  894. ArtifactPosition freeSlot = ourArt->firstBackpackSlot(commanderOwner);
  895. ArtifactLocation src(commanderOwner->commander.get(), artifactPos);
  896. ArtifactLocation dst(commanderOwner, freeSlot);
  897. if (ourArt->canBePutAt(dst, true))
  898. {
  899. LOCPLINT->cb->swapArtifacts(src, dst);
  900. setArtifact(nullptr);
  901. parent->redraw();
  902. }
  903. }
  904. void CCommanderArtPlace::setArtifact(const CArtifactInstance * art)
  905. {
  906. baseType = -1; //by default we don't store any component
  907. ourArt = art;
  908. if (!art)
  909. {
  910. image->disable();
  911. text.clear();
  912. return;
  913. }
  914. image->enable();
  915. image->setFrame(art->artType->getIconIndex());
  916. text = art->getEffectiveDescription();
  917. if (art->artType->id == ArtifactID::SPELL_SCROLL)
  918. {
  919. int spellID = art->getGivenSpellID();
  920. if (spellID >= 0)
  921. {
  922. //add spell component info (used to provide a pic in r-click popup)
  923. baseType = CComponent::spell;
  924. type = spellID;
  925. bonusValue = 0;
  926. }
  927. }
  928. else
  929. {
  930. baseType = CComponent::artifact;
  931. type = art->artType->id;
  932. bonusValue = 0;
  933. }
  934. }