CArtHandler.cpp 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356
  1. #include "StdInc.h"
  2. #include "CArtHandler.h"
  3. #include "Filesystem/CResourceLoader.h"
  4. #include "CGeneralTextHandler.h"
  5. #include <boost/random/linear_congruential.hpp>
  6. #include "VCMI_Lib.h"
  7. #include "CModHandler.h"
  8. #include "CSpellHandler.h"
  9. #include "CObjectHandler.h"
  10. #include "NetPacks.h"
  11. #include "GameConstants.h"
  12. using namespace boost::assign;
  13. /*
  14. * CArtHandler.cpp, part of VCMI engine
  15. *
  16. * Authors: listed in file AUTHORS in main folder
  17. *
  18. * License: GNU General Public License v2.0 or later
  19. * Full text of license available in license.txt file, in main folder
  20. *
  21. */
  22. extern boost::rand48 ran;
  23. const std::map<std::string, CArtifact::EartClass> artifactClassMap = boost::assign::map_list_of
  24. ("TREASURE", CArtifact::ART_TREASURE)
  25. ("MINOR", CArtifact::ART_MINOR)
  26. ("MAJOR", CArtifact::ART_MAJOR)
  27. ("RELIC", CArtifact::ART_RELIC)
  28. ("SPECIAL", CArtifact::ART_SPECIAL);
  29. #define ART_BEARER(x) ( #x, ArtBearer::x )
  30. const std::map<std::string, int> artifactBearerMap = boost::assign::map_list_of ART_BEARER_LIST;
  31. #undef ART_BEARER
  32. #define ART_POS(x) ( #x, ArtifactPosition::x )
  33. const std::map<std::string, ArtifactPosition> artifactPositionMap = boost::assign::map_list_of
  34. ART_POS(HEAD)
  35. ART_POS(SHOULDERS)
  36. ART_POS(NECK)
  37. ART_POS(RIGHT_HAND)
  38. ART_POS(LEFT_HAND)
  39. ART_POS(TORSO)
  40. ART_POS(RIGHT_RING)
  41. ART_POS(LEFT_RING)
  42. ART_POS(FEET)
  43. ART_POS(MISC1)
  44. ART_POS(MISC2)
  45. ART_POS(MISC3)
  46. ART_POS(MISC4)
  47. ART_POS(MISC5)
  48. ART_POS(MACH1)
  49. ART_POS(MACH2)
  50. ART_POS(MACH3)
  51. ART_POS(MACH4)
  52. ART_POS(SPELLBOOK); //no need to specify commander / stack position?
  53. const std::string & CArtifact::Name() const
  54. {
  55. return name;
  56. }
  57. const std::string & CArtifact::Description() const
  58. {
  59. return description;
  60. }
  61. const std::string & CArtifact::EventText() const
  62. {
  63. return eventText;
  64. }
  65. bool CArtifact::isBig () const
  66. {
  67. return VLC->arth->isBigArtifact(id);
  68. }
  69. // /**
  70. // * Checks whether the artifact fits at a given slot.
  71. // * @param artifWorn A hero's set of worn artifacts.
  72. // */
  73. // bool CArtifact::fitsAt (const std::map<ui16, const CArtifact*> &artifWorn, ui16 slotID) const
  74. // {
  75. // if (!vstd::contains(possibleSlots, slotID))
  76. // return false;
  77. //
  78. // // Can't put an artifact in a locked slot.
  79. // std::map<ui16, const CArtifact*>::const_iterator it = artifWorn.find(slotID);
  80. // if (it != artifWorn.end() && it->second->id == 145)
  81. // return false;
  82. //
  83. // // Check if a combination artifact fits.
  84. // // TODO: Might want a more general algorithm?
  85. // // Assumes that misc & rings fits only in their slots, and others in only one slot and no duplicates.
  86. // if (constituents != NULL)
  87. // {
  88. // std::map<ui16, const CArtifact*> tempArtifWorn = artifWorn;
  89. // const ui16 ringSlots[] = {6, 7};
  90. // const ui16 miscSlots[] = {9, 10, 11, 12, 18};
  91. // int rings = 0;
  92. // int misc = 0;
  93. //
  94. // VLC->arth->unequipArtifact(tempArtifWorn, slotID);
  95. //
  96. // BOOST_FOREACH(ui32 constituentID, *constituents)
  97. // {
  98. // const CArtifact& constituent = *VLC->arth->artifacts[constituentID];
  99. // const int slot = constituent.possibleSlots[0];
  100. //
  101. // if (slot == 6 || slot == 7)
  102. // rings++;
  103. // else if ((slot >= 9 && slot <= 12) || slot == 18)
  104. // misc++;
  105. // else if (tempArtifWorn.find(slot) != tempArtifWorn.end())
  106. // return false;
  107. // }
  108. //
  109. // // Ensure enough ring slots are free
  110. // for (int i = 0; i < sizeof(ringSlots)/sizeof(*ringSlots); i++)
  111. // {
  112. // if (tempArtifWorn.find(ringSlots[i]) == tempArtifWorn.end() || ringSlots[i] == slotID)
  113. // rings--;
  114. // }
  115. // if (rings > 0)
  116. // return false;
  117. //
  118. // // Ensure enough misc slots are free.
  119. // for (int i = 0; i < sizeof(miscSlots)/sizeof(*miscSlots); i++)
  120. // {
  121. // if (tempArtifWorn.find(miscSlots[i]) == tempArtifWorn.end() || miscSlots[i] == slotID)
  122. // misc--;
  123. // }
  124. // if (misc > 0)
  125. // return false;
  126. // }
  127. //
  128. // return true;
  129. // }
  130. // bool CArtifact::canBeAssembledTo (const std::map<ui16, const CArtifact*> &artifWorn, ui32 artifactID) const
  131. // {
  132. // if (constituentOf == NULL || !vstd::contains(*constituentOf, artifactID))
  133. // return false;
  134. //
  135. // const CArtifact &artifact = *VLC->arth->artifacts[artifactID];
  136. // assert(artifact.constituents);
  137. //
  138. // BOOST_FOREACH(ui32 constituentID, *artifact.constituents)
  139. // {
  140. // bool found = false;
  141. // for (std::map<ui16, const CArtifact*>::const_iterator it = artifWorn.begin(); it != artifWorn.end(); ++it)
  142. // {
  143. // if (it->second->id == constituentID)
  144. // {
  145. // found = true;
  146. // break;
  147. // }
  148. // }
  149. // if (!found)
  150. // return false;
  151. // }
  152. //
  153. // return true;
  154. // }
  155. CArtifact::CArtifact()
  156. {
  157. setNodeType(ARTIFACT);
  158. possibleSlots[ArtBearer::HERO]; //we want to generate map entry even if it will be empty
  159. possibleSlots[ArtBearer::CREATURE]; //we want to generate map entry even if it will be empty
  160. possibleSlots[ArtBearer::COMMANDER];
  161. constituents = NULL; //default pointer to zero
  162. constituentOf = NULL;
  163. }
  164. CArtifact::~CArtifact()
  165. {
  166. }
  167. int CArtifact::getArtClassSerial() const
  168. {
  169. if(id == 1)
  170. return 4;
  171. switch(aClass)
  172. {
  173. case ART_TREASURE:
  174. return 0;
  175. case ART_MINOR:
  176. return 1;
  177. case ART_MAJOR:
  178. return 2;
  179. case ART_RELIC:
  180. return 3;
  181. case ART_SPECIAL:
  182. return 5;
  183. }
  184. return -1;
  185. }
  186. std::string CArtifact::nodeName() const
  187. {
  188. return "Artifact: " + Name();
  189. }
  190. // void CArtifact::getParents(TCNodes &out, const CBonusSystemNode *root /*= NULL*/) const
  191. // {
  192. // //combined artifact carries bonuses from its parts
  193. // if(constituents)
  194. // {
  195. // BOOST_FOREACH(ui32 id, *constituents)
  196. // out.insert(VLC->arth->artifacts[id]);
  197. // }
  198. // }
  199. // void CScroll::Init()
  200. // {
  201. // // addNewBonus (Bonus (Bonus::PERMANENT, Bonus::SPELL, Bonus::ARTIFACT, 1, id, spellid, Bonus::INDEPENDENT_MAX));
  202. // // //boost::algorithm::replace_first(description, "[spell name]", VLC->spellh->spells[spellid].name);
  203. // }
  204. void CArtifact::addNewBonus(Bonus *b)
  205. {
  206. b->source = Bonus::ARTIFACT;
  207. b->duration = Bonus::PERMANENT;
  208. b->description = name;
  209. CBonusSystemNode::addNewBonus(b);
  210. }
  211. void CArtifact::setName (std::string desc)
  212. {
  213. name = desc;
  214. }
  215. void CArtifact::setDescription (std::string desc)
  216. {
  217. description = desc;
  218. }
  219. void CArtifact::setEventText (std::string desc)
  220. {
  221. eventText = desc;
  222. }
  223. void CGrowingArtifact::levelUpArtifact (CArtifactInstance * art)
  224. {
  225. Bonus b;
  226. b.type = Bonus::LEVEL_COUNTER;
  227. b.val = 1;
  228. b.duration = Bonus::COMMANDER_KILLED;
  229. art->accumulateBonus (b);
  230. BOOST_FOREACH (auto bonus, bonusesPerLevel)
  231. {
  232. if (art->valOfBonuses(Bonus::LEVEL_COUNTER) % bonus.first == 0) //every n levels
  233. {
  234. art->accumulateBonus (bonus.second);
  235. }
  236. }
  237. BOOST_FOREACH (auto bonus, thresholdBonuses)
  238. {
  239. if (art->valOfBonuses(Bonus::LEVEL_COUNTER) == bonus.first) //every n levels
  240. {
  241. art->addNewBonus (&bonus.second);
  242. }
  243. }
  244. }
  245. CArtHandler::CArtHandler()
  246. {
  247. //VLC->arth = this;
  248. // War machines are the default big artifacts.
  249. for (ArtifactID i = ArtifactID::CATAPULT; i <= ArtifactID::FIRST_AID_TENT; i.advance(1))
  250. bigArtifacts.insert(i);
  251. }
  252. CArtHandler::~CArtHandler()
  253. {
  254. for (std::vector< ConstTransitivePtr<CArtifact> >::iterator it = artifacts.begin(); it != artifacts.end(); ++it)
  255. {
  256. delete (*it)->constituents;
  257. delete (*it)->constituentOf;
  258. }
  259. }
  260. void CArtHandler::loadArtifacts(bool onlyTxt)
  261. {
  262. std::vector<ui16> slots;
  263. slots += 17, 16, 15, 14, 13, 18, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0;
  264. growingArtifacts += ArtifactID::AXE_OF_SMASHING, ArtifactID::MITHRIL_MAIL,
  265. ArtifactID::SWORD_OF_SHARPNESS, ArtifactID::PENDANT_OF_SORCERY, ArtifactID::BOOTS_OF_HASTE,
  266. ArtifactID::BOW_OF_SEEKING, ArtifactID::DRAGON_EYE_RING;
  267. static std::map<char, CArtifact::EartClass> classes =
  268. map_list_of('S',CArtifact::ART_SPECIAL)('T',CArtifact::ART_TREASURE)('N',CArtifact::ART_MINOR)('J',CArtifact::ART_MAJOR)('R',CArtifact::ART_RELIC);
  269. CLegacyConfigParser parser("DATA/ARTRAITS.TXT");
  270. CLegacyConfigParser events("DATA/ARTEVENT.TXT");
  271. parser.endLine(); // header
  272. parser.endLine();
  273. std::map<ui32,ui8>::iterator itr;
  274. for (ArtifactID i=ArtifactID(0); i<GameConstants::ARTIFACTS_QUANTITY; i.advance(1))
  275. {
  276. CArtifact *art;
  277. if (vstd::contains (growingArtifacts, i))
  278. {
  279. art = new CGrowingArtifact();
  280. }
  281. else
  282. {
  283. art = new CArtifact();
  284. }
  285. CArtifact &nart = *art;
  286. nart.id=i;
  287. nart.iconIndex=i;
  288. nart.setName (parser.readString());
  289. nart.setEventText (events.readString());
  290. events.endLine();
  291. nart.price= parser.readNumber();
  292. for(int j=0;j<slots.size();j++)
  293. {
  294. if(parser.readString() == "x")
  295. nart.possibleSlots[ArtBearer::HERO].push_back(ArtifactPosition(slots[j]));
  296. }
  297. nart.aClass = classes[parser.readString()[0]];
  298. //load description and remove quotation marks
  299. nart.setDescription (parser.readString());
  300. parser.endLine();
  301. if(onlyTxt)
  302. continue;
  303. artifacts.push_back(&nart);
  304. }
  305. if (VLC->modh->modules.COMMANDERS)
  306. { //TODO: move all artifacts config to separate json file
  307. const JsonNode config(ResourceID("config/commanders.json"));
  308. BOOST_FOREACH(const JsonNode &artifact, config["artifacts"].Vector())
  309. {
  310. auto ga = dynamic_cast <CGrowingArtifact *>(artifacts[artifact["id"].Float()].get());
  311. BOOST_FOREACH (auto b, artifact["bonusesPerLevel"].Vector())
  312. {
  313. ga->bonusesPerLevel.push_back (std::pair <ui16, Bonus> (b["level"].Float(), *JsonUtils::parseBonus (b["bonus"].Vector())));
  314. }
  315. BOOST_FOREACH (auto b, artifact["thresholdBonuses"].Vector())
  316. {
  317. ga->thresholdBonuses.push_back (std::pair <ui16, Bonus> (b["level"].Float(), *JsonUtils::parseBonus (b["bonus"].Vector())));
  318. }
  319. }
  320. }
  321. sortArts();
  322. if(onlyTxt)
  323. return;
  324. addBonuses();
  325. // Populate reverse mappings of combinational artifacts.
  326. //TODO: do that also for new artifacts read from mods
  327. BOOST_FOREACH(CArtifact *artifact, artifacts)
  328. {
  329. if (artifact->constituents != NULL)
  330. {
  331. BOOST_FOREACH(ui32 constituentID, *artifact->constituents)
  332. {
  333. if (artifacts[constituentID]->constituentOf == NULL)
  334. artifacts[constituentID]->constituentOf = new std::vector<ArtifactID>();
  335. artifacts[constituentID]->constituentOf->push_back(artifact->id);
  336. }
  337. }
  338. }
  339. }
  340. void CArtHandler::load(const JsonNode & node)
  341. {
  342. BOOST_FOREACH(auto & entry, node.Struct())
  343. {
  344. if (!entry.second.isNull()) // may happens if mod removed creature by setting json entry to null
  345. {
  346. CArtifact * art = loadArtifact(entry.second);
  347. art->id = ArtifactID(artifacts.size());
  348. artifacts.push_back(art);
  349. tlog5 << "Added artifact: " << entry.first << "\n";
  350. VLC->modh->identifiers.registerObject (std::string("artifact.") + entry.first, art->id);
  351. }
  352. }
  353. }
  354. CArtifact * CArtHandler::loadArtifact(const JsonNode & node)
  355. {
  356. CArtifact * art = new CArtifact;
  357. const JsonNode *value;
  358. const JsonNode & text = node["text"];
  359. art->setName (text["name"].String());
  360. art->setDescription (text["description"].String());
  361. art->setEventText (text["event"].String());
  362. const JsonNode & graphics = node["graphics"];
  363. art->iconIndex = graphics["iconIndex"].Float();
  364. art->image = graphics["image"].String();
  365. value = &graphics["large"];
  366. if (!value->isNull())
  367. art->large = value->String();
  368. art->advMapDef = graphics["map"].String();
  369. art->price = node["value"].Float();
  370. {
  371. auto it = artifactClassMap.find (node["class"].String());
  372. if (it != artifactClassMap.end())
  373. {
  374. art->aClass = it->second;
  375. }
  376. else
  377. {
  378. tlog2 << "Warning! Artifact rarity " << value->String() << " not recognized!";
  379. art->aClass = CArtifact::ART_SPECIAL;
  380. }
  381. }
  382. int bearerType = -1;
  383. bool heroArt = false;
  384. {
  385. const JsonNode & bearer = node["type"];
  386. BOOST_FOREACH (const JsonNode & b, bearer.Vector())
  387. {
  388. auto it = artifactBearerMap.find (b.String());
  389. if (it != artifactBearerMap.end())
  390. {
  391. bearerType = it->second;
  392. switch (bearerType)
  393. {
  394. case ArtBearer::HERO: //TODO: allow arts having several possible bearers
  395. heroArt = true;
  396. break;
  397. case ArtBearer::COMMANDER:
  398. makeItCommanderArt (art, false); //do not erase already existing slots
  399. break;
  400. case ArtBearer::CREATURE:
  401. makeItCreatureArt (art, false);
  402. break;
  403. }
  404. }
  405. else
  406. tlog2 << "Warning! Artifact type " << b.String() << " not recognized!";
  407. }
  408. }
  409. value = &node["slot"];
  410. if (!value->isNull() && heroArt) //we assume non-hero slots are irrelevant?
  411. {
  412. std::string slotName = value->String();
  413. if (slotName == "MISC")
  414. {
  415. //unfortunatelly slot ids aare not continuous
  416. art->possibleSlots[ArtBearer::HERO] += ArtifactPosition::MISC1, ArtifactPosition::MISC2, ArtifactPosition::MISC3, ArtifactPosition::MISC4, ArtifactPosition::MISC5;
  417. }
  418. else if (slotName == "RING")
  419. {
  420. art->possibleSlots[ArtBearer::HERO] += ArtifactPosition::LEFT_RING, ArtifactPosition::RIGHT_RING;
  421. }
  422. else
  423. {
  424. auto it = artifactPositionMap.find (slotName);
  425. if (it != artifactPositionMap.end())
  426. {
  427. auto slot = it->second;
  428. art->possibleSlots[ArtBearer::HERO].push_back (slot);
  429. }
  430. else
  431. tlog2 << "Warning! Artifact slot " << value->String() << " not recognized!";
  432. }
  433. }
  434. readComponents (node, art);
  435. BOOST_FOREACH (const JsonNode &bonus, node["bonuses"].Vector())
  436. {
  437. auto b = JsonUtils::parseBonus(bonus);
  438. //TODO: bonus->sid = art->id;
  439. art->addNewBonus(b);
  440. }
  441. return art;
  442. }
  443. void CArtifact::addConstituent (ArtifactID component)
  444. {
  445. assert (constituents);
  446. constituents->push_back (component);
  447. }
  448. ArtifactID CArtHandler::creatureToMachineID(CreatureID id)
  449. {
  450. int dif = 142;
  451. switch (id)
  452. {
  453. case 147:
  454. dif--;
  455. break;
  456. case 148:
  457. dif++;
  458. break;
  459. }
  460. dif = -dif;
  461. return ArtifactID(id+dif);
  462. }
  463. CreatureID CArtHandler::machineIDToCreature(ArtifactID id)
  464. {
  465. int dif = 142;
  466. switch (id)
  467. {
  468. case 6:
  469. dif--;
  470. break;
  471. case 5:
  472. dif++;
  473. break;
  474. }
  475. return CreatureID(id + dif);
  476. }
  477. void CArtHandler::sortArts()
  478. {
  479. //for (int i=0; i<allowedArtifacts.size(); ++i) //do 144, bo nie chcemy bzdurek
  480. //{
  481. // switch (allowedArtifacts[i]->aClass)
  482. // {
  483. // case CArtifact::ART_TREASURE:
  484. // treasures.push_back(allowedArtifacts[i]);
  485. // break;
  486. // case CArtifact::ART_MINOR:
  487. // minors.push_back(allowedArtifacts[i]);
  488. // break;
  489. // case CArtifact::ART_MAJOR:
  490. // majors.push_back(allowedArtifacts[i]);
  491. // break;
  492. // case CArtifact::ART_RELIC:
  493. // relics.push_back(allowedArtifacts[i]);
  494. // break;
  495. // }
  496. //}
  497. }
  498. ArtifactID CArtHandler::getRandomArt(int flags)
  499. {
  500. return getArtSync(ran(), flags, true);
  501. }
  502. ArtifactID CArtHandler::getArtSync (ui32 rand, int flags, bool erasePicked)
  503. {
  504. auto erasePickedArt = [&]( ArtifactID id )
  505. {
  506. std::vector<CArtifact*>* ptr;
  507. CArtifact *art = artifacts[id];
  508. switch (art->aClass)
  509. {
  510. case CArtifact::ART_TREASURE:
  511. ptr = &treasures;
  512. break;
  513. case CArtifact::ART_MINOR:
  514. ptr = &minors;
  515. break;
  516. case CArtifact::ART_MAJOR:
  517. ptr = &majors;
  518. break;
  519. case CArtifact::ART_RELIC:
  520. ptr = &relics;
  521. break;
  522. default: //special artifacts should not be erased
  523. return;
  524. }
  525. ptr->erase (std::find(ptr->begin(), ptr->end(), art)); //remove the artifact from available list
  526. };
  527. auto getAllowedArts = [&](std::vector<ConstTransitivePtr<CArtifact> > &out, std::vector<CArtifact*> *arts, int flag)
  528. {
  529. if (arts->empty()) //restock available arts
  530. {
  531. for (int i = 0; i < allowedArtifacts.size(); ++i)
  532. {
  533. if (allowedArtifacts[i]->aClass == flag)
  534. arts->push_back(allowedArtifacts[i]);
  535. }
  536. }
  537. for (int i = 0; i < arts->size(); ++i)
  538. {
  539. CArtifact *art = (*arts)[i];
  540. out.push_back(art);
  541. }
  542. };
  543. auto getAllowed = [&](std::vector<ConstTransitivePtr<CArtifact> > &out)
  544. {
  545. if (flags & CArtifact::ART_TREASURE)
  546. getAllowedArts (out, &treasures, CArtifact::ART_TREASURE);
  547. if (flags & CArtifact::ART_MINOR)
  548. getAllowedArts (out, &minors, CArtifact::ART_MINOR);
  549. if (flags & CArtifact::ART_MAJOR)
  550. getAllowedArts (out, &majors, CArtifact::ART_MAJOR);
  551. if (flags & CArtifact::ART_RELIC)
  552. getAllowedArts (out, &relics, CArtifact::ART_RELIC);
  553. if (!out.size()) //no artifact of specified rarity, we need to take another one
  554. {
  555. getAllowedArts (out, &treasures, CArtifact::ART_TREASURE);
  556. getAllowedArts (out, &minors, CArtifact::ART_MINOR);
  557. getAllowedArts (out, &majors, CArtifact::ART_MAJOR);
  558. getAllowedArts (out, &relics, CArtifact::ART_RELIC);
  559. }
  560. if (!out.size()) //no arts are available at all
  561. {
  562. out.resize (64);
  563. std::fill_n (out.begin(), 64, artifacts[2]); //Give Grail - this can't be banned (hopefully)
  564. }
  565. };
  566. std::vector<ConstTransitivePtr<CArtifact> > out;
  567. getAllowed(out);
  568. ArtifactID artID = out[rand % out.size()]->id;
  569. if(erasePicked)
  570. erasePickedArt (artID);
  571. return artID;
  572. }
  573. Bonus *createBonus(Bonus::BonusType type, int val, int subtype, Bonus::ValueType valType, shared_ptr<ILimiter> limiter = shared_ptr<ILimiter>(), int additionalInfo = 0)
  574. {
  575. Bonus *added = new Bonus(Bonus::PERMANENT,type,Bonus::ARTIFACT,val,-1,subtype);
  576. added->additionalInfo = additionalInfo;
  577. added->valType = valType;
  578. added->limiter = limiter;
  579. return added;
  580. }
  581. Bonus *createBonus(Bonus::BonusType type, int val, int subtype, shared_ptr<IPropagator> propagator = shared_ptr<IPropagator>(), int additionalInfo = 0)
  582. {
  583. Bonus *added = new Bonus(Bonus::PERMANENT,type,Bonus::ARTIFACT,val,-1,subtype);
  584. added->additionalInfo = additionalInfo;
  585. added->valType = Bonus::BASE_NUMBER;
  586. added->propagator = propagator;
  587. return added;
  588. }
  589. void CArtHandler::giveArtBonus( ArtifactID aid, Bonus::BonusType type, int val, int subtype, Bonus::ValueType valType, shared_ptr<ILimiter> limiter, int additionalInfo)
  590. {
  591. giveArtBonus(aid, createBonus(type, val, subtype, valType, limiter, additionalInfo));
  592. }
  593. void CArtHandler::giveArtBonus(ArtifactID aid, Bonus::BonusType type, int val, int subtype, shared_ptr<IPropagator> propagator /*= NULL*/, int additionalInfo)
  594. {
  595. giveArtBonus(aid, createBonus(type, val, subtype, propagator, additionalInfo));
  596. }
  597. void CArtHandler::giveArtBonus(ArtifactID aid, Bonus *bonus)
  598. {
  599. bonus->sid = aid;
  600. if(bonus->subtype == Bonus::MORALE || bonus->type == Bonus::LUCK)
  601. bonus->description = artifacts[aid]->Name() + (bonus->val > 0 ? " +" : " ") + boost::lexical_cast<std::string>(bonus->val);
  602. else
  603. bonus->description = artifacts[aid]->Name();
  604. artifacts[aid]->addNewBonus(bonus);
  605. }
  606. void CArtHandler::makeItCreatureArt (CArtifact * a, bool onlyCreature /*=true*/)
  607. {
  608. if (onlyCreature)
  609. {
  610. a->possibleSlots[ArtBearer::HERO].clear();
  611. a->possibleSlots[ArtBearer::COMMANDER].clear();
  612. }
  613. a->possibleSlots[ArtBearer::CREATURE].push_back(ArtifactPosition::CREATURE_SLOT);
  614. }
  615. void CArtHandler::makeItCreatureArt (ArtifactID aid, bool onlyCreature /*=true*/)
  616. {
  617. CArtifact *a = artifacts[aid];
  618. makeItCreatureArt (a, onlyCreature);
  619. }
  620. void CArtHandler::makeItCommanderArt (CArtifact * a, bool onlyCommander /*= true*/ )
  621. {
  622. if (onlyCommander)
  623. {
  624. a->possibleSlots[ArtBearer::HERO].clear();
  625. a->possibleSlots[ArtBearer::CREATURE].clear();
  626. }
  627. for (int i = ArtifactPosition::COMMANDER1; i <= ArtifactPosition::COMMANDER6; ++i)
  628. a->possibleSlots[ArtBearer::COMMANDER].push_back(ArtifactPosition(i));
  629. }
  630. void CArtHandler::makeItCommanderArt( ArtifactID aid, bool onlyCommander /*= true*/ )
  631. {
  632. CArtifact *a = artifacts[aid];
  633. makeItCommanderArt (a, onlyCommander);
  634. }
  635. void CArtHandler::addBonuses()
  636. {
  637. const JsonNode config(ResourceID("config/artifacts.json"));
  638. BOOST_FOREACH (auto & artifact, config["artifacts"].Struct()) //pair <string, JsonNode> (id, properties)
  639. {
  640. auto ga = artifacts[artifact.second["id"].Float()].get();
  641. BOOST_FOREACH (auto b, artifact.second["bonuses"].Vector())
  642. {
  643. auto bonus = JsonUtils::parseBonus (b);
  644. bonus->sid = ga->id;
  645. ga->addNewBonus (bonus);
  646. }
  647. BOOST_FOREACH (const JsonNode & b, artifact.second["type"].Vector()) //TODO: remove duplicate code
  648. {
  649. auto it = artifactBearerMap.find (b.String());
  650. if (it != artifactBearerMap.end())
  651. {
  652. int bearerType = it->second;
  653. switch (bearerType)
  654. {
  655. case ArtBearer::HERO:
  656. break;
  657. case ArtBearer::COMMANDER:
  658. makeItCommanderArt (ga); //original artifacts should have only one bearer type
  659. break;
  660. case ArtBearer::CREATURE:
  661. makeItCreatureArt (ga);
  662. break;
  663. }
  664. }
  665. else
  666. tlog2 << "Warning! Artifact type " << b.String() << " not recognized!";
  667. }
  668. readComponents (artifact.second, ga);
  669. VLC->modh->identifiers.registerObject ("artifact." + artifact.first, ga->id);
  670. }
  671. }
  672. void CArtHandler::readComponents (const JsonNode & node, CArtifact * art)
  673. {
  674. const JsonNode *value;
  675. value = &node["components"];
  676. if (!value->isNull())
  677. {
  678. art->constituents = new std::vector<ArtifactID>();
  679. BOOST_FOREACH (auto component, value->Vector())
  680. {
  681. VLC->modh->identifiers.requestIdentifier(std::string("artifact.") + component.String(),
  682. [art](si32 id)
  683. {
  684. art->addConstituent(ArtifactID(id));
  685. }
  686. );
  687. }
  688. }
  689. }
  690. void CArtHandler::clear()
  691. {
  692. BOOST_FOREACH(CArtifact *art, artifacts)
  693. delete art;
  694. artifacts.clear();
  695. clearHlpLists();
  696. }
  697. void CArtHandler::clearHlpLists()
  698. {
  699. treasures.clear();
  700. minors.clear();
  701. majors.clear();
  702. relics.clear();
  703. }
  704. bool CArtHandler::legalArtifact(ArtifactID id)
  705. {
  706. return (artifacts[id]->possibleSlots[ArtBearer::HERO].size() ||
  707. (artifacts[id]->possibleSlots[ArtBearer::COMMANDER].size() && VLC->modh->modules.COMMANDERS)) ||
  708. (artifacts[id]->possibleSlots[ArtBearer::CREATURE].size() && VLC->modh->modules.STACK_ARTIFACT);
  709. }
  710. void CArtHandler::initAllowedArtifactsList(const std::vector<bool> &allowed)
  711. {
  712. allowedArtifacts.clear();
  713. clearHlpLists();
  714. for (ArtifactID i=ArtifactID::SPELLBOOK; i<ArtifactID::ART_SELECTION; i.advance(1))
  715. {
  716. if (allowed[i] && legalArtifact(i))
  717. allowedArtifacts.push_back(artifacts[i]);
  718. }
  719. if (VLC->modh->modules.COMMANDERS) //allow all commander artifacts for testing
  720. {
  721. for (int i = 146; i <= 155; ++i)
  722. {
  723. allowedArtifacts.push_back(artifacts[i]);
  724. }
  725. }
  726. for (int i = GameConstants::ARTIFACTS_QUANTITY; i < artifacts.size(); ++i) //allow all new artifacts by default
  727. {
  728. if (artifacts[i]->possibleSlots[ArtBearer::HERO].size())
  729. allowedArtifacts.push_back(artifacts[i]);
  730. else //check if active modules allow artifact to be every used
  731. {
  732. if (legalArtifact(ArtifactID(i)))
  733. allowedArtifacts.push_back(artifacts[i]);
  734. //keep im mind that artifact can be worn by more than one type of bearer
  735. }
  736. }
  737. }
  738. std::vector<bool> CArtHandler::getDefaultAllowedArtifacts() const
  739. {
  740. std::vector<bool> allowedArtifacts;
  741. allowedArtifacts.resize(127, true);
  742. allowedArtifacts.resize(141, false);
  743. allowedArtifacts.resize(GameConstants::ARTIFACTS_QUANTITY, true);
  744. return allowedArtifacts;
  745. }
  746. CArtifactInstance::CArtifactInstance()
  747. {
  748. init();
  749. }
  750. CArtifactInstance::CArtifactInstance( CArtifact *Art)
  751. {
  752. init();
  753. setType(Art);
  754. }
  755. // CArtifactInstance::CArtifactInstance(int aid)
  756. // {
  757. // init();
  758. // setType(VLC->arth->artifacts[aid]);
  759. // }
  760. void CArtifactInstance::setType( CArtifact *Art )
  761. {
  762. artType = Art;
  763. attachTo(Art);
  764. }
  765. std::string CArtifactInstance::nodeName() const
  766. {
  767. return "Artifact instance of " + (artType ? artType->Name() : std::string("uninitialized")) + " type";
  768. }
  769. CArtifactInstance * CArtifactInstance::createScroll( const CSpell *s)
  770. {
  771. CArtifactInstance *ret = new CArtifactInstance(VLC->arth->artifacts[1]);
  772. Bonus *b = new Bonus(Bonus::PERMANENT, Bonus::SPELL, Bonus::ARTIFACT_INSTANCE, -1, 1, s->id);
  773. ret->addNewBonus(b);
  774. return ret;
  775. }
  776. void CArtifactInstance::init()
  777. {
  778. id = ArtifactInstanceID();
  779. setNodeType(ARTIFACT_INSTANCE);
  780. }
  781. ArtifactPosition CArtifactInstance::firstAvailableSlot(const CArtifactSet *h) const
  782. {
  783. BOOST_FOREACH(auto slot, artType->possibleSlots[h->bearerType()])
  784. {
  785. if(canBePutAt(h, slot)) //if(artType->fitsAt(h->artifWorn, slot))
  786. {
  787. //we've found a free suitable slot.
  788. return slot;
  789. }
  790. }
  791. //if haven't find proper slot, use backpack
  792. return firstBackpackSlot(h);
  793. }
  794. ArtifactPosition CArtifactInstance::firstBackpackSlot(const CArtifactSet *h) const
  795. {
  796. if(!artType->isBig()) //discard big artifact
  797. return ArtifactPosition(
  798. GameConstants::BACKPACK_START + h->artifactsInBackpack.size());
  799. return ArtifactPosition::PRE_FIRST;
  800. }
  801. bool CArtifactInstance::canBePutAt(const ArtifactLocation al, bool assumeDestRemoved /*= false*/) const
  802. {
  803. return canBePutAt(al.getHolderArtSet(), al.slot, assumeDestRemoved);
  804. }
  805. bool CArtifactInstance::canBePutAt(const CArtifactSet *artSet, ArtifactPosition slot, bool assumeDestRemoved /*= false*/) const
  806. {
  807. if(slot >= GameConstants::BACKPACK_START)
  808. {
  809. if(artType->isBig())
  810. return false;
  811. //TODO backpack limit
  812. return true;
  813. }
  814. auto possibleSlots = artType->possibleSlots.find(artSet->bearerType());
  815. if(possibleSlots == artType->possibleSlots.end())
  816. {
  817. tlog3 << "Warning: artifact " << artType->Name() << " doesn't have defined allowed slots for bearer of type "
  818. << artSet->bearerType() << std::endl;
  819. return false;
  820. }
  821. if(!vstd::contains(possibleSlots->second, slot))
  822. return false;
  823. return artSet->isPositionFree(slot, assumeDestRemoved);
  824. }
  825. void CArtifactInstance::putAt(ArtifactLocation al)
  826. {
  827. assert(canBePutAt(al));
  828. al.getHolderArtSet()->setNewArtSlot(al.slot, this, false);
  829. if(al.slot < GameConstants::BACKPACK_START)
  830. al.getHolderNode()->attachTo(this);
  831. }
  832. void CArtifactInstance::removeFrom(ArtifactLocation al)
  833. {
  834. assert(al.getHolderArtSet()->getArt(al.slot) == this);
  835. al.getHolderArtSet()->eraseArtSlot(al.slot);
  836. if(al.slot < GameConstants::BACKPACK_START)
  837. al.getHolderNode()->detachFrom(this);
  838. //TODO delete me?
  839. }
  840. bool CArtifactInstance::canBeDisassembled() const
  841. {
  842. return artType->constituents && artType->constituentOf->size();
  843. }
  844. std::vector<const CArtifact *> CArtifactInstance::assemblyPossibilities(const CArtifactSet *h) const
  845. {
  846. std::vector<const CArtifact *> ret;
  847. if(!artType->constituentOf //not a part of combined artifact
  848. || artType->constituents) //combined artifact already: no combining of combined artifacts... for now.
  849. return ret;
  850. BOOST_FOREACH(ui32 possibleCombinedArt, *artType->constituentOf)
  851. {
  852. const CArtifact * const artifact = VLC->arth->artifacts[possibleCombinedArt];
  853. assert(artifact->constituents);
  854. bool possible = true;
  855. BOOST_FOREACH(ui32 constituentID, *artifact->constituents) //check if all constituents are available
  856. {
  857. if(!h->hasArt(constituentID, true)) //constituent must be equipped
  858. {
  859. possible = false;
  860. break;
  861. }
  862. }
  863. if(possible)
  864. ret.push_back(artifact);
  865. }
  866. return ret;
  867. }
  868. void CArtifactInstance::move(ArtifactLocation src, ArtifactLocation dst)
  869. {
  870. removeFrom(src);
  871. putAt(dst);
  872. }
  873. CArtifactInstance * CArtifactInstance::createNewArtifactInstance(CArtifact *Art)
  874. {
  875. if(!Art->constituents)
  876. {
  877. auto ret = new CArtifactInstance(Art);
  878. if (dynamic_cast<CGrowingArtifact *>(Art))
  879. {
  880. Bonus * bonus = new Bonus;
  881. bonus->type = Bonus::LEVEL_COUNTER;
  882. bonus->val = 0;
  883. ret->addNewBonus (bonus);
  884. }
  885. return ret;
  886. }
  887. else
  888. {
  889. CCombinedArtifactInstance * ret = new CCombinedArtifactInstance(Art);
  890. ret->createConstituents();
  891. return ret;
  892. }
  893. }
  894. CArtifactInstance * CArtifactInstance::createNewArtifactInstance(int aid)
  895. {
  896. return createNewArtifactInstance(VLC->arth->artifacts[aid]);
  897. }
  898. void CArtifactInstance::deserializationFix()
  899. {
  900. setType(artType);
  901. }
  902. SpellID CArtifactInstance::getGivenSpellID() const
  903. {
  904. const Bonus * b = getBonusLocalFirst(Selector::type(Bonus::SPELL));
  905. if(!b)
  906. {
  907. tlog3 << "Warning: " << nodeName() << " doesn't bear any spell!\n";
  908. return SpellID::NONE;
  909. }
  910. return SpellID(b->subtype);
  911. }
  912. bool CArtifactInstance::isPart(const CArtifactInstance *supposedPart) const
  913. {
  914. return supposedPart == this;
  915. }
  916. bool CCombinedArtifactInstance::canBePutAt(const CArtifactSet *artSet, ArtifactPosition slot, bool assumeDestRemoved /*= false*/) const
  917. {
  918. bool canMainArtifactBePlaced = CArtifactInstance::canBePutAt(artSet, slot, assumeDestRemoved);
  919. if(!canMainArtifactBePlaced)
  920. return false; //no is no...
  921. if(slot >= GameConstants::BACKPACK_START)
  922. return true; //we can always remove combined art to the backapck
  923. assert(artType->constituents);
  924. std::vector<ConstituentInfo> constituentsToBePlaced = constituentsInfo; //we'll remove constituents from that list, as we find a suitable slot for them
  925. //it may be that we picked a combined artifact in hero screen (though technically it's still there) to move it
  926. //so we remove from the list all constituents that are already present on dst hero in the form of locks
  927. BOOST_FOREACH(const ConstituentInfo &constituent, constituentsInfo)
  928. {
  929. if(constituent.art == artSet->getArt(constituent.slot, false)) //no need to worry about locked constituent
  930. constituentsToBePlaced -= constituent;
  931. }
  932. //we iterate over all active slots and check if constituents fits them
  933. for (int i = 0; i < GameConstants::BACKPACK_START; i++)
  934. {
  935. for(auto art = constituentsToBePlaced.begin(); art != constituentsToBePlaced.end(); art++)
  936. {
  937. if(art->art->canBePutAt(artSet, ArtifactPosition(i), i == slot)) // i == al.slot because we can remove already worn artifact only from that slot that is our main destination
  938. {
  939. constituentsToBePlaced.erase(art);
  940. break;
  941. }
  942. }
  943. }
  944. return constituentsToBePlaced.empty();
  945. }
  946. bool CCombinedArtifactInstance::canBeDisassembled() const
  947. {
  948. return true;
  949. }
  950. CCombinedArtifactInstance::CCombinedArtifactInstance(CArtifact *Art)
  951. : CArtifactInstance(Art) //TODO: seems unued, but need to be written
  952. {
  953. }
  954. CCombinedArtifactInstance::CCombinedArtifactInstance()
  955. {
  956. }
  957. void CCombinedArtifactInstance::createConstituents()
  958. {
  959. assert(artType);
  960. assert(artType->constituents);
  961. BOOST_FOREACH(ui32 a, *artType->constituents)
  962. {
  963. addAsConstituent(CArtifactInstance::createNewArtifactInstance(a), ArtifactPosition::PRE_FIRST);
  964. }
  965. }
  966. void CCombinedArtifactInstance::addAsConstituent(CArtifactInstance *art, ArtifactPosition slot)
  967. {
  968. assert(vstd::contains(*artType->constituents, art->artType->id));
  969. assert(art->getParentNodes().size() == 1 && art->getParentNodes().front() == art->artType);
  970. constituentsInfo.push_back(ConstituentInfo(art, slot));
  971. attachTo(art);
  972. }
  973. void CCombinedArtifactInstance::putAt(ArtifactLocation al)
  974. {
  975. if(al.slot >= GameConstants::BACKPACK_START)
  976. {
  977. CArtifactInstance::putAt(al);
  978. BOOST_FOREACH(ConstituentInfo &ci, constituentsInfo)
  979. ci.slot = ArtifactPosition::PRE_FIRST;
  980. }
  981. else
  982. {
  983. CArtifactInstance *mainConstituent = figureMainConstituent(al); //it'll be replaced with combined artifact, not a lock
  984. CArtifactInstance::putAt(al); //puts combined art (this)
  985. BOOST_FOREACH(ConstituentInfo &ci, constituentsInfo)
  986. {
  987. if(ci.art != mainConstituent)
  988. {
  989. const ArtifactLocation suggestedPos(al.artHolder, ci.slot);
  990. const bool inActiveSlot = vstd::isbetween(ci.slot, 0, GameConstants::BACKPACK_START);
  991. const bool suggestedPosValid = ci.art->canBePutAt(suggestedPos);
  992. ArtifactPosition pos = ArtifactPosition::PRE_FIRST;
  993. if(inActiveSlot && suggestedPosValid) //there is a valid suggestion where to place lock
  994. pos = ci.slot;
  995. else
  996. ci.slot = pos = ci.art->firstAvailableSlot(al.getHolderArtSet());
  997. assert(pos < GameConstants::BACKPACK_START);
  998. al.getHolderArtSet()->setNewArtSlot(pos, ci.art, true); //sets as lock
  999. }
  1000. else
  1001. {
  1002. ci.slot = ArtifactPosition::PRE_FIRST;
  1003. }
  1004. }
  1005. }
  1006. }
  1007. void CCombinedArtifactInstance::removeFrom(ArtifactLocation al)
  1008. {
  1009. if(al.slot >= GameConstants::BACKPACK_START)
  1010. {
  1011. CArtifactInstance::removeFrom(al);
  1012. }
  1013. else
  1014. {
  1015. BOOST_FOREACH(ConstituentInfo &ci, constituentsInfo)
  1016. {
  1017. if(ci.slot >= 0)
  1018. {
  1019. al.getHolderArtSet()->eraseArtSlot(ci.slot);
  1020. ci.slot = ArtifactPosition::PRE_FIRST;
  1021. }
  1022. else
  1023. {
  1024. //main constituent
  1025. CArtifactInstance::removeFrom(al);
  1026. }
  1027. }
  1028. }
  1029. }
  1030. CArtifactInstance * CCombinedArtifactInstance::figureMainConstituent(const ArtifactLocation al)
  1031. {
  1032. CArtifactInstance *mainConstituent = NULL; //it'll be replaced with combined artifact, not a lock
  1033. BOOST_FOREACH(ConstituentInfo &ci, constituentsInfo)
  1034. if(ci.slot == al.slot)
  1035. mainConstituent = ci.art;
  1036. if(!mainConstituent)
  1037. {
  1038. BOOST_FOREACH(ConstituentInfo &ci, constituentsInfo)
  1039. {
  1040. if(vstd::contains(ci.art->artType->possibleSlots[al.getHolderArtSet()->bearerType()], al.slot))
  1041. {
  1042. mainConstituent = ci.art;
  1043. }
  1044. }
  1045. }
  1046. return mainConstituent;
  1047. }
  1048. void CCombinedArtifactInstance::deserializationFix()
  1049. {
  1050. BOOST_FOREACH(ConstituentInfo &ci, constituentsInfo)
  1051. attachTo(ci.art);
  1052. }
  1053. bool CCombinedArtifactInstance::isPart(const CArtifactInstance *supposedPart) const
  1054. {
  1055. bool me = CArtifactInstance::isPart(supposedPart);
  1056. if(me)
  1057. return true;
  1058. //check for constituents
  1059. BOOST_FOREACH(const ConstituentInfo &constituent, constituentsInfo)
  1060. if(constituent.art == supposedPart)
  1061. return true;
  1062. return false;
  1063. }
  1064. CCombinedArtifactInstance::ConstituentInfo::ConstituentInfo(CArtifactInstance *Art /*= NULL*/, ArtifactPosition Slot /*= -1*/)
  1065. {
  1066. art = Art;
  1067. slot = Slot;
  1068. }
  1069. bool CCombinedArtifactInstance::ConstituentInfo::operator==(const ConstituentInfo &rhs) const
  1070. {
  1071. return art == rhs.art && slot == rhs.slot;
  1072. }
  1073. const CArtifactInstance* CArtifactSet::getArt(ArtifactPosition pos, bool excludeLocked /*= true*/) const
  1074. {
  1075. if(const ArtSlotInfo *si = getSlot(pos))
  1076. {
  1077. if(si->artifact && (!excludeLocked || !si->locked))
  1078. return si->artifact;
  1079. }
  1080. return NULL;
  1081. }
  1082. CArtifactInstance* CArtifactSet::getArt(ArtifactPosition pos, bool excludeLocked /*= true*/)
  1083. {
  1084. return const_cast<CArtifactInstance*>((const_cast<const CArtifactSet*>(this))->getArt(pos, excludeLocked));
  1085. }
  1086. ArtifactPosition CArtifactSet::getArtPos(int aid, bool onlyWorn /*= true*/) const
  1087. {
  1088. for(auto i = artifactsWorn.cbegin(); i != artifactsWorn.cend(); i++)
  1089. if(i->second.artifact->artType->id == aid)
  1090. return i->first;
  1091. if(onlyWorn)
  1092. return ArtifactPosition::PRE_FIRST;
  1093. for(int i = 0; i < artifactsInBackpack.size(); i++)
  1094. if(artifactsInBackpack[i].artifact->artType->id == aid)
  1095. return ArtifactPosition(GameConstants::BACKPACK_START + i);
  1096. return ArtifactPosition::PRE_FIRST;
  1097. }
  1098. ArtifactPosition CArtifactSet::getArtPos(const CArtifactInstance *art) const
  1099. {
  1100. BOOST_FOREACH(auto i, artifactsWorn)
  1101. if(i.second.artifact == art)
  1102. return i.first;
  1103. for(int i = 0; i < artifactsInBackpack.size(); i++)
  1104. if(artifactsInBackpack[i].artifact == art)
  1105. return ArtifactPosition(GameConstants::BACKPACK_START + i);
  1106. return ArtifactPosition::PRE_FIRST;
  1107. }
  1108. const CArtifactInstance * CArtifactSet::getArtByInstanceId( ArtifactInstanceID artInstId ) const
  1109. {
  1110. BOOST_FOREACH(auto i, artifactsWorn)
  1111. if(i.second.artifact->id == artInstId)
  1112. return i.second.artifact;
  1113. BOOST_FOREACH(auto i, artifactsInBackpack)
  1114. if(i.artifact->id == artInstId)
  1115. return i.artifact;
  1116. return NULL;
  1117. }
  1118. bool CArtifactSet::hasArt(ui32 aid, bool onlyWorn /*= false*/) const
  1119. {
  1120. return getArtPos(aid, onlyWorn) != ArtifactPosition::PRE_FIRST;
  1121. }
  1122. const ArtSlotInfo * CArtifactSet::getSlot(ArtifactPosition pos) const
  1123. {
  1124. if(vstd::contains(artifactsWorn, pos))
  1125. return &artifactsWorn[pos];
  1126. if(pos >= ArtifactPosition::AFTER_LAST )
  1127. {
  1128. int backpackPos = (int)pos - GameConstants::BACKPACK_START;
  1129. if(backpackPos < 0 || backpackPos >= artifactsInBackpack.size())
  1130. return NULL;
  1131. else
  1132. return &artifactsInBackpack[backpackPos];
  1133. }
  1134. return NULL;
  1135. }
  1136. bool CArtifactSet::isPositionFree(ArtifactPosition pos, bool onlyLockCheck /*= false*/) const
  1137. {
  1138. if(const ArtSlotInfo *s = getSlot(pos))
  1139. return (onlyLockCheck || !s->artifact) && !s->locked;
  1140. return true; //no slot means not used
  1141. }
  1142. si32 CArtifactSet::getArtTypeId(ArtifactPosition pos) const
  1143. {
  1144. const CArtifactInstance * const a = getArt(pos);
  1145. if(!a)
  1146. {
  1147. tlog2 << (dynamic_cast<const CGHeroInstance*>(this))->name << " has no artifact at " << pos << " (getArtTypeId)\n";
  1148. return -1;
  1149. }
  1150. return a->artType->id;
  1151. }
  1152. CArtifactSet::~CArtifactSet()
  1153. {
  1154. }
  1155. ArtSlotInfo & CArtifactSet::retreiveNewArtSlot(ArtifactPosition slot)
  1156. {
  1157. assert(!vstd::contains(artifactsWorn, slot));
  1158. ArtSlotInfo &ret = slot < GameConstants::BACKPACK_START
  1159. ? artifactsWorn[slot]
  1160. : *artifactsInBackpack.insert(artifactsInBackpack.begin() + (slot - GameConstants::BACKPACK_START), ArtSlotInfo());
  1161. return ret;
  1162. }
  1163. void CArtifactSet::setNewArtSlot(ArtifactPosition slot, CArtifactInstance *art, bool locked)
  1164. {
  1165. ArtSlotInfo &asi = retreiveNewArtSlot(slot);
  1166. asi.artifact = art;
  1167. asi.locked = locked;
  1168. }
  1169. void CArtifactSet::eraseArtSlot(ArtifactPosition slot)
  1170. {
  1171. if(slot < GameConstants::BACKPACK_START)
  1172. {
  1173. artifactsWorn.erase(slot);
  1174. }
  1175. else
  1176. {
  1177. slot = ArtifactPosition(slot - GameConstants::BACKPACK_START);
  1178. artifactsInBackpack.erase(artifactsInBackpack.begin() + slot);
  1179. }
  1180. }
  1181. void CArtifactSet::artDeserializationFix(CBonusSystemNode *node)
  1182. {
  1183. for(auto i = artifactsWorn.begin(); i != artifactsWorn.end(); i++)
  1184. if(i->second.artifact && !i->second.locked)
  1185. node->attachTo(i->second.artifact);
  1186. }