CArtifactHolder.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947
  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. CArtPlace::CArtPlace(Point position, const CArtifactInstance * Art):
  27. locked(false), picked(false), marked(false), ourArt(Art)
  28. {
  29. pos += position;
  30. pos.w = pos.h = 44;
  31. createImage();
  32. }
  33. void CArtPlace::createImage()
  34. {
  35. OBJ_CONSTRUCTION_CAPTURING_ALL;
  36. int graphic = 0;
  37. if (ourArt)
  38. graphic = ourArt->artType->iconIndex;
  39. if (locked)
  40. graphic = ArtifactID::ART_LOCK;
  41. image = new CAnimImage("artifact", graphic);
  42. if (!ourArt)
  43. image->disable();
  44. selection = new CAnimImage("artifact", ArtifactID::ART_SELECTION);
  45. selection->disable();
  46. }
  47. void CArtPlace::lockSlot(bool on)
  48. {
  49. if (locked == on)
  50. return;
  51. locked = on;
  52. if (on)
  53. image->setFrame(ArtifactID::ART_LOCK);
  54. else
  55. image->setFrame(ourArt->artType->iconIndex);
  56. }
  57. void CArtPlace::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 CArtPlace::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 CArtPlace::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 CArtPlace::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 CArtPlace::clickRight(tribool down, bool previousState)
  213. {
  214. if(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 CArtPlace::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. CArtPlace * 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 CArtPlace::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 CArtPlace::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 CArtPlace::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 CArtPlace::setMeAsDest(bool backpackAsVoid /*= true*/)
  325. {
  326. ourOwner->commonInfo->dst.setTo(this, backpackAsVoid);
  327. }
  328. void CArtPlace::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(CArtPlace *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(CArtPlace* 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 (CArtPlace* artPlace, ArtifactPosition slotID)
  499. {
  500. artPlace->pickSlot(false);
  501. artPlace->slotID = slotID;
  502. artPlace->setArtifact(nullptr);
  503. }
  504. CArtifactsOfHero::CArtifactsOfHero(std::map<ArtifactPosition, CArtPlace *> ArtWorn, std::vector<CArtPlace *> 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 CArtPlace(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 CArtPlace(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. CArtPlace *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. CArtPlace * 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(CArtPlace *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 CArtPlace *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. }