CArtifactHolder.cpp 27 KB

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