CArtifactHolder.cpp 28 KB

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