CArtifactHolder.cpp 27 KB

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