CArtifactHolder.cpp 27 KB

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