CArtifactHolder.cpp 28 KB

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