CArtifactHolder.cpp 26 KB

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