CArtifactHolder.cpp 28 KB

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