CArtifactHolder.cpp 28 KB

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