CArtifactHolder.cpp 25 KB

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