CArtifactHolder.cpp 26 KB

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