CArtifactHolder.cpp 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046
  1. #include "StdInc.h"
  2. #include "CArtifactHolder.h"
  3. #include "../gui/CGuiHandler.h"
  4. #include "../gui/CCursorHandler.h"
  5. #include "Buttons.h"
  6. #include "CComponent.h"
  7. #include "../windows/CHeroWindow.h"
  8. #include "../windows/CSpellWindow.h"
  9. #include "../windows/GUIClasses.h"
  10. #include "../CPlayerInterface.h"
  11. #include "../CGameInfo.h"
  12. #include "../../CCallback.h"
  13. #include "../../lib/CArtHandler.h"
  14. #include "../../lib/spells/CSpellHandler.h"
  15. #include "../../lib/CGeneralTextHandler.h"
  16. #include "../../lib/mapObjects/CGHeroInstance.h"
  17. /*
  18. * CArtifactHolder.cpp, part of VCMI engine
  19. *
  20. * Authors: listed in file AUTHORS in main folder
  21. *
  22. * License: GNU General Public License v2.0 or later
  23. * Full text of license available in license.txt file, in main folder
  24. *
  25. */
  26. CHeroArtPlace::CHeroArtPlace(Point position, const CArtifactInstance * Art): CArtPlace(position, Art),
  27. locked(false), picked(false), marked(false), ourOwner(nullptr)
  28. {
  29. createImage();
  30. }
  31. void CHeroArtPlace::createImage()
  32. {
  33. OBJ_CONSTRUCTION_CAPTURING_ALL;
  34. int imageIndex = 0;
  35. if (ourArt)
  36. imageIndex = ourArt->artType->iconIndex;
  37. if (locked)
  38. imageIndex = ArtifactID::ART_LOCK;
  39. image = new CAnimImage("artifact", imageIndex);
  40. if (!ourArt)
  41. image->disable();
  42. selection = new CAnimImage("artifact", ArtifactID::ART_SELECTION);
  43. selection->disable();
  44. }
  45. void CHeroArtPlace::lockSlot(bool on)
  46. {
  47. if (locked == on)
  48. return;
  49. locked = on;
  50. if (on)
  51. image->setFrame(ArtifactID::ART_LOCK);
  52. else if (ourArt)
  53. image->setFrame(ourArt->artType->iconIndex);
  54. else
  55. image->setFrame(0);
  56. }
  57. void CHeroArtPlace::pickSlot(bool on)
  58. {
  59. if (picked == on)
  60. return;
  61. picked = on;
  62. if (on)
  63. image->disable();
  64. else
  65. image->enable();
  66. }
  67. void CHeroArtPlace::selectSlot(bool on)
  68. {
  69. if (marked == on)
  70. return;
  71. marked = on;
  72. if (on)
  73. selection->enable();
  74. else
  75. selection->disable();
  76. }
  77. void CHeroArtPlace::clickLeft(tribool down, bool previousState)
  78. {
  79. //LRClickableAreaWTextComp::clickLeft(down);
  80. bool inBackpack = slotID >= GameConstants::BACKPACK_START,
  81. srcInBackpack = ourOwner->commonInfo->src.slotID >= GameConstants::BACKPACK_START,
  82. srcInSameHero = ourOwner->commonInfo->src.AOH == ourOwner;
  83. if(ourOwner->highlightModeCallback && ourArt)
  84. {
  85. if(down)
  86. {
  87. if(!ourArt->artType->isTradable()) //War Machine or Spellbook
  88. {
  89. LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[21]); //This item can't be traded.
  90. }
  91. else
  92. {
  93. ourOwner->unmarkSlots(false);
  94. selectSlot(true);
  95. ourOwner->highlightModeCallback(this);
  96. }
  97. }
  98. return;
  99. }
  100. // If clicked on spellbook, open it only if no artifact is held at the moment.
  101. if(ourArt && !down && previousState && !ourOwner->commonInfo->src.AOH)
  102. {
  103. if(ourArt->artType->id == ArtifactID::SPELLBOOK)
  104. GH.pushInt(new CSpellWindow(ourOwner->curHero, LOCPLINT, LOCPLINT->battleInt));
  105. }
  106. if (!down && previousState)
  107. {
  108. if(ourArt && ourArt->artType->id == ArtifactID::SPELLBOOK)
  109. return; //this is handled separately
  110. if(!ourOwner->commonInfo->src.AOH) //nothing has been clicked
  111. {
  112. if(ourArt //to prevent selecting empty slots (bugfix to what GrayFace reported)
  113. && ourOwner->curHero->tempOwner == LOCPLINT->playerID)//can't take art from another player
  114. {
  115. if(ourArt->artType->id == ArtifactID::CATAPULT) //catapult cannot be highlighted
  116. {
  117. std::vector<CComponent *> catapult(1, new CComponent(CComponent::artifact, 3, 0));
  118. LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[312], catapult); //The Catapult must be equipped.
  119. return;
  120. }
  121. select();
  122. }
  123. }
  124. else if(ourArt == ourOwner->commonInfo->src.art) //restore previously picked artifact
  125. {
  126. deselect();
  127. }
  128. else //perform artifact transition
  129. {
  130. if(inBackpack) // Backpack destination.
  131. {
  132. if(srcInBackpack && slotID == ourOwner->commonInfo->src.slotID + 1) //next slot (our is not visible, so visually same as "old" place) to the art -> make nothing, return artifact to slot
  133. {
  134. deselect();
  135. }
  136. else
  137. {
  138. const CArtifact * const cur = ourOwner->commonInfo->src.art->artType;
  139. switch(cur->id)
  140. {
  141. case ArtifactID::CATAPULT:
  142. //should not happen, catapult cannot be selected
  143. assert(cur->id != ArtifactID::CATAPULT);
  144. break;
  145. case ArtifactID::BALLISTA: case ArtifactID::AMMO_CART: case ArtifactID::FIRST_AID_TENT: //war machines cannot go to backpack
  146. LOCPLINT->showInfoDialog(boost::str(boost::format(CGI->generaltexth->allTexts[153]) % cur->Name()));
  147. break;
  148. default:
  149. setMeAsDest();
  150. vstd::amin(ourOwner->commonInfo->dst.slotID, ArtifactPosition(
  151. ourOwner->curHero->artifactsInBackpack.size() + GameConstants::BACKPACK_START));
  152. if(srcInBackpack && srcInSameHero)
  153. {
  154. 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
  155. || ourOwner->commonInfo->src.slotID < ourOwner->commonInfo->dst.slotID) //rearranging arts in backpack after taking src artifact, the dest id will be shifted
  156. vstd::advance(ourOwner->commonInfo->dst.slotID, -1);
  157. }
  158. if(srcInSameHero && ourOwner->commonInfo->dst.slotID == ourOwner->commonInfo->src.slotID) //we came to src == dst
  159. deselect();
  160. else
  161. ourOwner->realizeCurrentTransaction();
  162. break;
  163. }
  164. }
  165. }
  166. //check if swap is possible
  167. else if (fitsHere(ourOwner->commonInfo->src.art) &&
  168. (!ourArt || ourOwner->curHero->tempOwner == LOCPLINT->playerID))
  169. {
  170. setMeAsDest();
  171. //
  172. // // Special case when the dest artifact can't be fit into the src slot.
  173. // //CGI->arth->unequipArtifact(ourOwner->curHero->artifWorn, slotID);
  174. // const CArtifactsOfHero* srcAOH = ourOwner->commonInfo->src.AOH;
  175. // ui16 srcSlotID = ourOwner->commonInfo->src.slotID;
  176. // if (ourArt && srcSlotID < 19 && !ourArt->canBePutAt(ArtifactLocation(srcAOH->curHero, srcSlotID)))
  177. // {
  178. // // Put dest artifact into owner's backpack.
  179. // ourOwner->commonInfo->src.AOH = ourOwner;
  180. // ourOwner->commonInfo->src.slotID = ourOwner->curHero->artifacts.size() + 19;
  181. // }
  182. ourOwner->realizeCurrentTransaction();
  183. }
  184. }
  185. }
  186. }
  187. bool CHeroArtPlace::askToAssemble(const CArtifactInstance *art, ArtifactPosition slot,
  188. 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 !CGI->arth->isBigArtifact(art->artType->id);
  322. return art->canBePutAt(ArtifactLocation(ourOwner->curHero, slotID), true);
  323. }
  324. void CHeroArtPlace::setMeAsDest(bool backpackAsVoid /*= true*/)
  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 /*= true*/)
  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 /*= true*/)
  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,
  505. CButton *leftScroll, CButton *rightScroll, bool createCommonPart):
  506. curHero(nullptr),
  507. artWorn(ArtWorn), backpack(Backpack),
  508. backpackPos(0), commonInfo(nullptr), updateState(false),
  509. leftArtRoll(leftScroll), rightArtRoll(rightScroll),
  510. allowedAssembling(true), highlightModeCallback(nullptr)
  511. {
  512. if(createCommonPart)
  513. {
  514. commonInfo = std::make_shared<CArtifactsOfHero::SCommonPart>();
  515. commonInfo->participants.insert(this);
  516. }
  517. // Init slots for worn artifacts.
  518. for (auto p : artWorn)
  519. {
  520. p.second->ourOwner = this;
  521. eraseSlotData(p.second, p.first);
  522. }
  523. // Init slots for the backpack.
  524. for(size_t s=0; s<backpack.size(); ++s)
  525. {
  526. backpack[s]->ourOwner = this;
  527. eraseSlotData(backpack[s], ArtifactPosition(GameConstants::BACKPACK_START + s));
  528. }
  529. leftArtRoll->addCallback(std::bind(&CArtifactsOfHero::scrollBackpack,this,-1));
  530. rightArtRoll->addCallback(std::bind(&CArtifactsOfHero::scrollBackpack,this,+1));
  531. }
  532. CArtifactsOfHero::CArtifactsOfHero(const Point& position, bool createCommonPart /*= false*/)
  533. : curHero(nullptr), backpackPos(0), commonInfo(nullptr), updateState(false), allowedAssembling(true), highlightModeCallback(nullptr)
  534. {
  535. if(createCommonPart)
  536. {
  537. commonInfo = std::make_shared<CArtifactsOfHero::SCommonPart>();
  538. commonInfo->participants.insert(this);
  539. }
  540. OBJ_CONSTRUCTION_CAPTURING_ALL;
  541. pos += position;
  542. std::vector<Point> slotPos =
  543. {
  544. Point(509,30), Point(567,240), Point(509,80), //0-2
  545. Point(383,68), Point(564,183), Point(509,130), //3-5
  546. Point(431,68), Point(610,183), Point(515,295), //6-8
  547. Point(383,143), Point(399,194), Point(415,245), //9-11
  548. Point(431,296), Point(564,30), Point(610,30), //12-14
  549. Point(610,76), Point(610,122), Point(610,310), //15-17
  550. Point(381,296) //18
  551. };
  552. // Create slots for worn artifacts.
  553. for (size_t g = 0; g < GameConstants::BACKPACK_START ; g++)
  554. {
  555. artWorn[ArtifactPosition(g)] = new CHeroArtPlace(slotPos[g]);
  556. artWorn[ArtifactPosition(g)]->ourOwner = this;
  557. eraseSlotData(artWorn[ArtifactPosition(g)], ArtifactPosition(g));
  558. }
  559. // Create slots for the backpack.
  560. for(size_t s=0; s<5; ++s)
  561. {
  562. auto add = new CHeroArtPlace(Point(403 + 46 * s, 365));
  563. add->ourOwner = this;
  564. eraseSlotData(add, ArtifactPosition(GameConstants::BACKPACK_START + s));
  565. backpack.push_back(add);
  566. }
  567. leftArtRoll = new CButton(Point(379, 364), "hsbtns3.def", CButton::tooltip(), [&]{ scrollBackpack(-1);}, SDLK_LEFT);
  568. rightArtRoll = new CButton(Point(632, 364), "hsbtns5.def", CButton::tooltip(), [&]{ scrollBackpack(+1);}, SDLK_RIGHT);
  569. }
  570. CArtifactsOfHero::~CArtifactsOfHero()
  571. {
  572. dispose();
  573. }
  574. void CArtifactsOfHero::updateParentWindow()
  575. {
  576. if (CHeroWindow* chw = dynamic_cast<CHeroWindow*>(GH.topInt()))
  577. {
  578. if(updateState)
  579. chw->curHero = curHero;
  580. else
  581. chw->update(curHero, true);
  582. }
  583. else if(CExchangeWindow* cew = dynamic_cast<CExchangeWindow*>(GH.topInt()))
  584. {
  585. //use our copy of hero to draw window
  586. if(cew->heroInst[0]->id == curHero->id)
  587. cew->heroInst[0] = curHero;
  588. else
  589. cew->heroInst[1] = curHero;
  590. if(!updateState)
  591. {
  592. cew->deactivate();
  593. // for(int g=0; g<ARRAY_COUNT(cew->heroInst); ++g)
  594. // {
  595. // if(cew->heroInst[g] == curHero)
  596. // {
  597. // cew->artifs[g]->setHero(curHero);
  598. // }
  599. // }
  600. cew->prepareBackground();
  601. cew->redraw();
  602. cew->activate();
  603. }
  604. }
  605. }
  606. void CArtifactsOfHero::safeRedraw()
  607. {
  608. if (active)
  609. {
  610. if(parent)
  611. parent->redraw();
  612. else
  613. redraw();
  614. }
  615. }
  616. void CArtifactsOfHero::realizeCurrentTransaction()
  617. {
  618. assert(commonInfo->src.AOH);
  619. assert(commonInfo->dst.AOH);
  620. LOCPLINT->cb->swapArtifacts(ArtifactLocation(commonInfo->src.AOH->curHero, commonInfo->src.slotID),
  621. ArtifactLocation(commonInfo->dst.AOH->curHero, commonInfo->dst.slotID));
  622. }
  623. void CArtifactsOfHero::artifactMoved(const ArtifactLocation &src, const ArtifactLocation &dst)
  624. {
  625. bool isCurHeroSrc = src.isHolder(curHero),
  626. isCurHeroDst = dst.isHolder(curHero);
  627. if(isCurHeroSrc && src.slot >= GameConstants::BACKPACK_START)
  628. updateSlot(src.slot);
  629. if(isCurHeroDst && dst.slot >= GameConstants::BACKPACK_START)
  630. updateSlot(dst.slot);
  631. if(isCurHeroSrc || isCurHeroDst) //we need to update all slots, artifact might be combined and affect more slots
  632. updateWornSlots(false);
  633. if (!src.isHolder(curHero) && !isCurHeroDst)
  634. return;
  635. if(commonInfo->src == src) //artifact was taken from us
  636. {
  637. assert(commonInfo->dst == dst //expected movement from slot ot slot
  638. || dst.slot == dst.getHolderArtSet()->artifactsInBackpack.size() + GameConstants::BACKPACK_START //artifact moved back to backpack (eg. to make place for art we are moving)
  639. || dst.getHolderArtSet()->bearerType() != ArtBearer::HERO);
  640. commonInfo->reset();
  641. unmarkSlots();
  642. }
  643. else if(commonInfo->dst == src) //the dest artifact was moved -> we are picking it
  644. {
  645. assert(dst.slot >= GameConstants::BACKPACK_START);
  646. commonInfo->reset();
  647. CHeroArtPlace *ap = nullptr;
  648. for(CArtifactsOfHero *aoh : commonInfo->participants)
  649. {
  650. if(dst.isHolder(aoh->curHero))
  651. {
  652. commonInfo->src.AOH = aoh;
  653. if((ap = aoh->getArtPlace(dst.slot)))//getArtPlace may return null
  654. break;
  655. }
  656. }
  657. if(ap)
  658. {
  659. ap->select();
  660. }
  661. else
  662. {
  663. commonInfo->src.art = dst.getArt();
  664. commonInfo->src.slotID = dst.slot;
  665. assert(commonInfo->src.AOH);
  666. CCS->curh->dragAndDropCursor(new CAnimImage("artifact", dst.getArt()->artType->iconIndex));
  667. markPossibleSlots(dst.getArt());
  668. }
  669. }
  670. else if(src.slot >= GameConstants::BACKPACK_START &&
  671. src.slot < commonInfo->src.slotID &&
  672. src.isHolder(commonInfo->src.AOH->curHero)) //artifact taken from before currently picked one
  673. {
  674. //int fixedSlot = src.hero->getArtPos(commonInfo->src.art);
  675. vstd::advance(commonInfo->src.slotID, -1);
  676. assert(commonInfo->src.valid());
  677. }
  678. else
  679. {
  680. //when moving one artifact onto another it leads to two art movements: dst->backapck; src->dst
  681. // however after first movement we pick the art from backpack and the second movement coming when
  682. // we have a different artifact may look surprising... but it's valid.
  683. }
  684. updateParentWindow();
  685. int shift = 0;
  686. // if(dst.slot >= Arts::BACKPACK_START && dst.slot - Arts::BACKPACK_START < backpackPos)
  687. // shift++;
  688. //
  689. if(src.slot < GameConstants::BACKPACK_START && dst.slot - GameConstants::BACKPACK_START < backpackPos)
  690. shift++;
  691. if(dst.slot < GameConstants::BACKPACK_START && src.slot - GameConstants::BACKPACK_START < backpackPos)
  692. shift--;
  693. if( (isCurHeroSrc && src.slot >= GameConstants::BACKPACK_START)
  694. || (isCurHeroDst && dst.slot >= GameConstants::BACKPACK_START) )
  695. scrollBackpack(shift); //update backpack slots
  696. }
  697. void CArtifactsOfHero::artifactRemoved(const ArtifactLocation &al)
  698. {
  699. if(al.isHolder(curHero))
  700. {
  701. if(al.slot < GameConstants::BACKPACK_START)
  702. updateWornSlots(0);
  703. else
  704. scrollBackpack(0); //update backpack slots
  705. }
  706. }
  707. CHeroArtPlace * CArtifactsOfHero::getArtPlace(int slot)
  708. {
  709. if(slot < GameConstants::BACKPACK_START)
  710. {
  711. if(artWorn.find(ArtifactPosition(slot)) == artWorn.end())
  712. {
  713. logGlobal->errorStream() << "CArtifactsOfHero::getArtPlace: invalid slot " << slot;
  714. return nullptr;
  715. }
  716. return artWorn[ArtifactPosition(slot)];
  717. }
  718. else
  719. {
  720. for(CHeroArtPlace *ap : backpack)
  721. if(ap->slotID == slot)
  722. return ap;
  723. return nullptr;
  724. }
  725. }
  726. void CArtifactsOfHero::artifactAssembled(const ArtifactLocation &al)
  727. {
  728. if(al.isHolder(curHero))
  729. updateWornSlots();
  730. }
  731. void CArtifactsOfHero::artifactDisassembled(const ArtifactLocation &al)
  732. {
  733. if(al.isHolder(curHero))
  734. updateWornSlots();
  735. }
  736. void CArtifactsOfHero::updateWornSlots(bool redrawParent /*= true*/)
  737. {
  738. for(auto p : artWorn)
  739. updateSlot(p.first);
  740. if(redrawParent)
  741. updateParentWindow();
  742. }
  743. const CGHeroInstance * CArtifactsOfHero::getHero() const
  744. {
  745. return curHero;
  746. }
  747. void CArtifactsOfHero::updateSlot(ArtifactPosition slotID)
  748. {
  749. setSlotData(getArtPlace(slotID), slotID);
  750. }
  751. CArtifactHolder::CArtifactHolder()
  752. {
  753. }
  754. void CWindowWithArtifacts::artifactRemoved(const ArtifactLocation &artLoc)
  755. {
  756. for(CArtifactsOfHero *aoh : artSets)
  757. aoh->artifactRemoved(artLoc);
  758. }
  759. void CWindowWithArtifacts::artifactMoved(const ArtifactLocation &artLoc, const ArtifactLocation &destLoc)
  760. {
  761. CArtifactsOfHero *destaoh = nullptr;
  762. for(CArtifactsOfHero *aoh : artSets)
  763. {
  764. aoh->artifactMoved(artLoc, destLoc);
  765. aoh->redraw();
  766. if(destLoc.isHolder(aoh->getHero()))
  767. destaoh = aoh;
  768. }
  769. //Make sure the status bar is updated so it does not display old text
  770. if(destaoh != nullptr && destaoh->getArtPlace(destLoc.slot) != nullptr)
  771. {
  772. destaoh->getArtPlace(destLoc.slot)->hover(true);
  773. }
  774. }
  775. void CWindowWithArtifacts::artifactDisassembled(const ArtifactLocation &artLoc)
  776. {
  777. for(CArtifactsOfHero *aoh : artSets)
  778. aoh->artifactDisassembled(artLoc);
  779. }
  780. void CWindowWithArtifacts::artifactAssembled(const ArtifactLocation &artLoc)
  781. {
  782. for(CArtifactsOfHero *aoh : artSets)
  783. aoh->artifactAssembled(artLoc);
  784. }
  785. void CArtifactsOfHero::SCommonPart::Artpos::clear()
  786. {
  787. slotID = ArtifactPosition::PRE_FIRST;
  788. AOH = nullptr;
  789. art = nullptr;
  790. }
  791. CArtifactsOfHero::SCommonPart::Artpos::Artpos()
  792. {
  793. clear();
  794. }
  795. void CArtifactsOfHero::SCommonPart::Artpos::setTo(const CHeroArtPlace *place, bool dontTakeBackpack)
  796. {
  797. slotID = place->slotID;
  798. AOH = place->ourOwner;
  799. if(slotID >= 19 && dontTakeBackpack)
  800. art = nullptr;
  801. else
  802. art = place->ourArt;
  803. }
  804. bool CArtifactsOfHero::SCommonPart::Artpos::operator==(const ArtifactLocation &al) const
  805. {
  806. if(!AOH)
  807. return false;
  808. bool ret = al.isHolder(AOH->curHero) && al.slot == slotID;
  809. //assert(al.getArt() == art);
  810. return ret;
  811. }
  812. bool CArtifactsOfHero::SCommonPart::Artpos::valid()
  813. {
  814. assert(AOH && art);
  815. return art == AOH->curHero->getArt(slotID);
  816. }
  817. CArtPlace::CArtPlace(Point position, const CArtifactInstance * Art) : ourArt(Art)
  818. {
  819. image = nullptr;
  820. pos += position;
  821. pos.w = pos.h = 44;
  822. }
  823. void CArtPlace::clickLeft(tribool down, bool previousState)
  824. {
  825. LRClickableAreaWTextComp::clickLeft(down, previousState);
  826. }
  827. void CArtPlace::clickRight(tribool down, bool previousState)
  828. {
  829. LRClickableAreaWTextComp::clickRight(down, previousState);
  830. }
  831. CCommanderArtPlace::CCommanderArtPlace(Point position, const CGHeroInstance * commanderOwner, ArtifactPosition artSlot, const CArtifactInstance * Art) : CArtPlace(position, Art), commanderOwner(commanderOwner), commanderSlotID(artSlot.num)
  832. {
  833. createImage();
  834. setArtifact(Art);
  835. }
  836. void CCommanderArtPlace::clickLeft(tribool down, bool previousState)
  837. {
  838. if (ourArt && text.size() && down)
  839. LOCPLINT->showYesNoDialog(CGI->generaltexth->localizedTexts["commanderWindow"]["artifactMessage"].String(), [this] { returnArtToHeroCallback(); }, [] {});
  840. }
  841. void CCommanderArtPlace::clickRight(tribool down, bool previousState)
  842. {
  843. if (ourArt && text.size() && down)
  844. CArtPlace::clickRight(down, previousState);
  845. }
  846. void CCommanderArtPlace::createImage()
  847. {
  848. OBJ_CONSTRUCTION_CAPTURING_ALL;
  849. int imageIndex = 0;
  850. if (ourArt)
  851. imageIndex = ourArt->artType->iconIndex;
  852. image = new CAnimImage("artifact", imageIndex);
  853. if (!ourArt)
  854. image->disable();
  855. }
  856. void CCommanderArtPlace::returnArtToHeroCallback()
  857. {
  858. ArtifactPosition artifactPos = commanderSlotID;;
  859. ArtifactPosition freeSlot = ourArt->firstBackpackSlot(commanderOwner);
  860. ArtifactLocation src(commanderOwner->commander.get(), artifactPos);
  861. ArtifactLocation dst(commanderOwner, freeSlot);
  862. if (ourArt->canBePutAt(dst, true))
  863. {
  864. LOCPLINT->cb->swapArtifacts(src, dst);
  865. setArtifact(nullptr);
  866. parent->redraw();
  867. }
  868. }
  869. void CCommanderArtPlace::setArtifact(const CArtifactInstance * art)
  870. {
  871. baseType = -1; //by default we don't store any component
  872. ourArt = art;
  873. if (!art)
  874. {
  875. image->disable();
  876. text = std::string();
  877. return;
  878. }
  879. image->enable();
  880. image->setFrame(art->artType->iconIndex);
  881. text = art->getEffectiveDescription();
  882. if (art->artType->id == ArtifactID::SPELL_SCROLL)
  883. {
  884. int spellID = art->getGivenSpellID();
  885. if (spellID >= 0)
  886. {
  887. //add spell component info (used to provide a pic in r-click popup)
  888. baseType = CComponent::spell;
  889. type = spellID;
  890. bonusValue = 0;
  891. }
  892. }
  893. else
  894. {
  895. baseType = CComponent::artifact;
  896. type = art->artType->id;
  897. bonusValue = 0;
  898. }
  899. }