CArtifactHolder.cpp 27 KB

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