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