CArtHandler.cpp 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377
  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 getAllowedArts = [&](std::vector<ConstTransitivePtr<CArtifact> > &out, std::vector<CArtifact*> *arts, CArtifact::EartClass flag)
  505. {
  506. if (arts->empty()) //restock available arts
  507. fillList(*arts, flag);
  508. for (int i = 0; i < arts->size(); ++i)
  509. {
  510. CArtifact *art = (*arts)[i];
  511. out.push_back(art);
  512. }
  513. };
  514. auto getAllowed = [&](std::vector<ConstTransitivePtr<CArtifact> > &out)
  515. {
  516. if (flags & CArtifact::ART_TREASURE)
  517. getAllowedArts (out, &treasures, CArtifact::ART_TREASURE);
  518. if (flags & CArtifact::ART_MINOR)
  519. getAllowedArts (out, &minors, CArtifact::ART_MINOR);
  520. if (flags & CArtifact::ART_MAJOR)
  521. getAllowedArts (out, &majors, CArtifact::ART_MAJOR);
  522. if (flags & CArtifact::ART_RELIC)
  523. getAllowedArts (out, &relics, CArtifact::ART_RELIC);
  524. if (!out.size()) //no artifact of specified rarity, we need to take another one
  525. {
  526. getAllowedArts (out, &treasures, CArtifact::ART_TREASURE);
  527. getAllowedArts (out, &minors, CArtifact::ART_MINOR);
  528. getAllowedArts (out, &majors, CArtifact::ART_MAJOR);
  529. getAllowedArts (out, &relics, CArtifact::ART_RELIC);
  530. }
  531. if (!out.size()) //no arts are available at all
  532. {
  533. out.resize (64);
  534. std::fill_n (out.begin(), 64, artifacts[2]); //Give Grail - this can't be banned (hopefully)
  535. }
  536. tlog0 << "Treasure count: " << treasures.size() << std::endl;
  537. };
  538. std::vector<ConstTransitivePtr<CArtifact> > out;
  539. getAllowed(out);
  540. ArtifactID artID = out[rand % out.size()]->id;
  541. if(erasePicked)
  542. erasePickedArt (artID);
  543. return artID;
  544. }
  545. Bonus *createBonus(Bonus::BonusType type, int val, int subtype, Bonus::ValueType valType, shared_ptr<ILimiter> limiter = shared_ptr<ILimiter>(), int additionalInfo = 0)
  546. {
  547. Bonus *added = new Bonus(Bonus::PERMANENT,type,Bonus::ARTIFACT,val,-1,subtype);
  548. added->additionalInfo = additionalInfo;
  549. added->valType = valType;
  550. added->limiter = limiter;
  551. return added;
  552. }
  553. Bonus *createBonus(Bonus::BonusType type, int val, int subtype, shared_ptr<IPropagator> propagator = shared_ptr<IPropagator>(), int additionalInfo = 0)
  554. {
  555. Bonus *added = new Bonus(Bonus::PERMANENT,type,Bonus::ARTIFACT,val,-1,subtype);
  556. added->additionalInfo = additionalInfo;
  557. added->valType = Bonus::BASE_NUMBER;
  558. added->propagator = propagator;
  559. return added;
  560. }
  561. void CArtHandler::giveArtBonus( ArtifactID aid, Bonus::BonusType type, int val, int subtype, Bonus::ValueType valType, shared_ptr<ILimiter> limiter, int additionalInfo)
  562. {
  563. giveArtBonus(aid, createBonus(type, val, subtype, valType, limiter, additionalInfo));
  564. }
  565. void CArtHandler::giveArtBonus(ArtifactID aid, Bonus::BonusType type, int val, int subtype, shared_ptr<IPropagator> propagator /*= NULL*/, int additionalInfo)
  566. {
  567. giveArtBonus(aid, createBonus(type, val, subtype, propagator, additionalInfo));
  568. }
  569. void CArtHandler::giveArtBonus(ArtifactID aid, Bonus *bonus)
  570. {
  571. bonus->sid = aid;
  572. if(bonus->subtype == Bonus::MORALE || bonus->type == Bonus::LUCK)
  573. bonus->description = artifacts[aid]->Name() + (bonus->val > 0 ? " +" : " ") + boost::lexical_cast<std::string>(bonus->val);
  574. else
  575. bonus->description = artifacts[aid]->Name();
  576. artifacts[aid]->addNewBonus(bonus);
  577. }
  578. void CArtHandler::makeItCreatureArt (CArtifact * a, bool onlyCreature /*=true*/)
  579. {
  580. if (onlyCreature)
  581. {
  582. a->possibleSlots[ArtBearer::HERO].clear();
  583. a->possibleSlots[ArtBearer::COMMANDER].clear();
  584. }
  585. a->possibleSlots[ArtBearer::CREATURE].push_back(ArtifactPosition::CREATURE_SLOT);
  586. }
  587. void CArtHandler::makeItCreatureArt (ArtifactID aid, bool onlyCreature /*=true*/)
  588. {
  589. CArtifact *a = artifacts[aid];
  590. makeItCreatureArt (a, onlyCreature);
  591. }
  592. void CArtHandler::makeItCommanderArt (CArtifact * a, bool onlyCommander /*= true*/ )
  593. {
  594. if (onlyCommander)
  595. {
  596. a->possibleSlots[ArtBearer::HERO].clear();
  597. a->possibleSlots[ArtBearer::CREATURE].clear();
  598. }
  599. for (int i = ArtifactPosition::COMMANDER1; i <= ArtifactPosition::COMMANDER6; ++i)
  600. a->possibleSlots[ArtBearer::COMMANDER].push_back(ArtifactPosition(i));
  601. }
  602. void CArtHandler::makeItCommanderArt( ArtifactID aid, bool onlyCommander /*= true*/ )
  603. {
  604. CArtifact *a = artifacts[aid];
  605. makeItCommanderArt (a, onlyCommander);
  606. }
  607. void CArtHandler::addBonuses()
  608. {
  609. const JsonNode config(ResourceID("config/artifacts.json"));
  610. BOOST_FOREACH (auto & artifact, config["artifacts"].Struct()) //pair <string, JsonNode> (id, properties)
  611. {
  612. auto ga = artifacts[artifact.second["id"].Float()].get();
  613. BOOST_FOREACH (auto b, artifact.second["bonuses"].Vector())
  614. {
  615. auto bonus = JsonUtils::parseBonus (b);
  616. bonus->sid = ga->id;
  617. ga->addNewBonus (bonus);
  618. }
  619. BOOST_FOREACH (const JsonNode & b, artifact.second["type"].Vector()) //TODO: remove duplicate code
  620. {
  621. auto it = artifactBearerMap.find (b.String());
  622. if (it != artifactBearerMap.end())
  623. {
  624. int bearerType = it->second;
  625. switch (bearerType)
  626. {
  627. case ArtBearer::HERO:
  628. break;
  629. case ArtBearer::COMMANDER:
  630. makeItCommanderArt (ga); //original artifacts should have only one bearer type
  631. break;
  632. case ArtBearer::CREATURE:
  633. makeItCreatureArt (ga);
  634. break;
  635. }
  636. }
  637. else
  638. tlog2 << "Warning! Artifact type " << b.String() << " not recognized!";
  639. }
  640. readComponents (artifact.second, ga);
  641. VLC->modh->identifiers.registerObject ("artifact." + artifact.first, ga->id);
  642. }
  643. }
  644. void CArtHandler::readComponents (const JsonNode & node, CArtifact * art)
  645. {
  646. const JsonNode *value;
  647. value = &node["components"];
  648. if (!value->isNull())
  649. {
  650. art->constituents = new std::vector<ArtifactID>();
  651. BOOST_FOREACH (auto component, value->Vector())
  652. {
  653. VLC->modh->identifiers.requestIdentifier(std::string("artifact.") + component.String(),
  654. [art](si32 id)
  655. {
  656. art->addConstituent(ArtifactID(id));
  657. }
  658. );
  659. }
  660. }
  661. }
  662. void CArtHandler::clear()
  663. {
  664. BOOST_FOREACH(CArtifact *art, artifacts)
  665. delete art;
  666. artifacts.clear();
  667. clearHlpLists();
  668. }
  669. void CArtHandler::clearHlpLists()
  670. {
  671. treasures.clear();
  672. minors.clear();
  673. majors.clear();
  674. relics.clear();
  675. }
  676. bool CArtHandler::legalArtifact(ArtifactID id)
  677. {
  678. return (artifacts[id]->possibleSlots[ArtBearer::HERO].size() ||
  679. (artifacts[id]->possibleSlots[ArtBearer::COMMANDER].size() && VLC->modh->modules.COMMANDERS)) ||
  680. (artifacts[id]->possibleSlots[ArtBearer::CREATURE].size() && VLC->modh->modules.STACK_ARTIFACT);
  681. }
  682. void CArtHandler::initAllowedArtifactsList(const std::vector<bool> &allowed)
  683. {
  684. allowedArtifacts.clear();
  685. clearHlpLists();
  686. for (ArtifactID i=ArtifactID::SPELLBOOK; i<ArtifactID::ART_SELECTION; i.advance(1))
  687. {
  688. if (allowed[i] && legalArtifact(i))
  689. allowedArtifacts.push_back(artifacts[i]);
  690. }
  691. if (VLC->modh->modules.COMMANDERS) //allow all commander artifacts for testing
  692. {
  693. for (int i = 146; i <= 155; ++i)
  694. {
  695. allowedArtifacts.push_back(artifacts[i]);
  696. }
  697. }
  698. for (int i = GameConstants::ARTIFACTS_QUANTITY; i < artifacts.size(); ++i) //allow all new artifacts by default
  699. {
  700. if (artifacts[i]->possibleSlots[ArtBearer::HERO].size())
  701. allowedArtifacts.push_back(artifacts[i]);
  702. else //check if active modules allow artifact to be every used
  703. {
  704. if (legalArtifact(ArtifactID(i)))
  705. allowedArtifacts.push_back(artifacts[i]);
  706. //keep im mind that artifact can be worn by more than one type of bearer
  707. }
  708. }
  709. }
  710. std::vector<bool> CArtHandler::getDefaultAllowedArtifacts() const
  711. {
  712. std::vector<bool> allowedArtifacts;
  713. allowedArtifacts.resize(127, true);
  714. allowedArtifacts.resize(141, false);
  715. allowedArtifacts.resize(GameConstants::ARTIFACTS_QUANTITY, true);
  716. return allowedArtifacts;
  717. }
  718. void CArtHandler::erasePickedArt(ArtifactID id)
  719. {
  720. CArtifact *art = artifacts[id];
  721. if(auto artifactList = listFromClass(art->aClass))
  722. {
  723. if(artifactList->empty())
  724. fillList(*artifactList, art->aClass);
  725. auto itr = vstd::find(*artifactList, art);
  726. if(itr != artifactList->end())
  727. {
  728. artifactList->erase(itr);
  729. }
  730. else
  731. tlog2 << "Problem: cannot erase artifact " << art->Name() << " from list, it was not present\n";
  732. }
  733. else
  734. tlog2 << "Problem: cannot find list for artifact " << art->Name() << ", strange class. (special?)\n";
  735. }
  736. boost::optional<std::vector<CArtifact*>&> CArtHandler::listFromClass( CArtifact::EartClass artifactClass )
  737. {
  738. switch(artifactClass)
  739. {
  740. case CArtifact::ART_TREASURE:
  741. return treasures;
  742. case CArtifact::ART_MINOR:
  743. return minors;
  744. case CArtifact::ART_MAJOR:
  745. return majors;
  746. case CArtifact::ART_RELIC:
  747. return relics;
  748. default: //special artifacts should not be erased
  749. return 0;
  750. }
  751. }
  752. void CArtHandler::fillList( std::vector<CArtifact*> &listToBeFilled, CArtifact::EartClass artifactClass )
  753. {
  754. assert(listToBeFilled.empty());
  755. for (int i = 0; i < allowedArtifacts.size(); ++i)
  756. {
  757. if (allowedArtifacts[i]->aClass == artifactClass)
  758. listToBeFilled.push_back(allowedArtifacts[i]);
  759. }
  760. }
  761. CArtifactInstance::CArtifactInstance()
  762. {
  763. init();
  764. }
  765. CArtifactInstance::CArtifactInstance( CArtifact *Art)
  766. {
  767. init();
  768. setType(Art);
  769. }
  770. // CArtifactInstance::CArtifactInstance(int aid)
  771. // {
  772. // init();
  773. // setType(VLC->arth->artifacts[aid]);
  774. // }
  775. void CArtifactInstance::setType( CArtifact *Art )
  776. {
  777. artType = Art;
  778. attachTo(Art);
  779. }
  780. std::string CArtifactInstance::nodeName() const
  781. {
  782. return "Artifact instance of " + (artType ? artType->Name() : std::string("uninitialized")) + " type";
  783. }
  784. CArtifactInstance * CArtifactInstance::createScroll( const CSpell *s)
  785. {
  786. CArtifactInstance *ret = new CArtifactInstance(VLC->arth->artifacts[1]);
  787. Bonus *b = new Bonus(Bonus::PERMANENT, Bonus::SPELL, Bonus::ARTIFACT_INSTANCE, -1, 1, s->id);
  788. ret->addNewBonus(b);
  789. return ret;
  790. }
  791. void CArtifactInstance::init()
  792. {
  793. id = ArtifactInstanceID();
  794. setNodeType(ARTIFACT_INSTANCE);
  795. }
  796. ArtifactPosition CArtifactInstance::firstAvailableSlot(const CArtifactSet *h) const
  797. {
  798. BOOST_FOREACH(auto slot, artType->possibleSlots[h->bearerType()])
  799. {
  800. if(canBePutAt(h, slot)) //if(artType->fitsAt(h->artifWorn, slot))
  801. {
  802. //we've found a free suitable slot.
  803. return slot;
  804. }
  805. }
  806. //if haven't find proper slot, use backpack
  807. return firstBackpackSlot(h);
  808. }
  809. ArtifactPosition CArtifactInstance::firstBackpackSlot(const CArtifactSet *h) const
  810. {
  811. if(!artType->isBig()) //discard big artifact
  812. return ArtifactPosition(
  813. GameConstants::BACKPACK_START + h->artifactsInBackpack.size());
  814. return ArtifactPosition::PRE_FIRST;
  815. }
  816. bool CArtifactInstance::canBePutAt(const ArtifactLocation al, bool assumeDestRemoved /*= false*/) const
  817. {
  818. return canBePutAt(al.getHolderArtSet(), al.slot, assumeDestRemoved);
  819. }
  820. bool CArtifactInstance::canBePutAt(const CArtifactSet *artSet, ArtifactPosition slot, bool assumeDestRemoved /*= false*/) const
  821. {
  822. if(slot >= GameConstants::BACKPACK_START)
  823. {
  824. if(artType->isBig())
  825. return false;
  826. //TODO backpack limit
  827. return true;
  828. }
  829. auto possibleSlots = artType->possibleSlots.find(artSet->bearerType());
  830. if(possibleSlots == artType->possibleSlots.end())
  831. {
  832. tlog3 << "Warning: artifact " << artType->Name() << " doesn't have defined allowed slots for bearer of type "
  833. << artSet->bearerType() << std::endl;
  834. return false;
  835. }
  836. if(!vstd::contains(possibleSlots->second, slot))
  837. return false;
  838. return artSet->isPositionFree(slot, assumeDestRemoved);
  839. }
  840. void CArtifactInstance::putAt(ArtifactLocation al)
  841. {
  842. assert(canBePutAt(al));
  843. al.getHolderArtSet()->setNewArtSlot(al.slot, this, false);
  844. if(al.slot < GameConstants::BACKPACK_START)
  845. al.getHolderNode()->attachTo(this);
  846. }
  847. void CArtifactInstance::removeFrom(ArtifactLocation al)
  848. {
  849. assert(al.getHolderArtSet()->getArt(al.slot) == this);
  850. al.getHolderArtSet()->eraseArtSlot(al.slot);
  851. if(al.slot < GameConstants::BACKPACK_START)
  852. al.getHolderNode()->detachFrom(this);
  853. //TODO delete me?
  854. }
  855. bool CArtifactInstance::canBeDisassembled() const
  856. {
  857. return artType->constituents && artType->constituentOf->size();
  858. }
  859. std::vector<const CArtifact *> CArtifactInstance::assemblyPossibilities(const CArtifactSet *h) const
  860. {
  861. std::vector<const CArtifact *> ret;
  862. if(!artType->constituentOf //not a part of combined artifact
  863. || artType->constituents) //combined artifact already: no combining of combined artifacts... for now.
  864. return ret;
  865. BOOST_FOREACH(ui32 possibleCombinedArt, *artType->constituentOf)
  866. {
  867. const CArtifact * const artifact = VLC->arth->artifacts[possibleCombinedArt];
  868. assert(artifact->constituents);
  869. bool possible = true;
  870. BOOST_FOREACH(ui32 constituentID, *artifact->constituents) //check if all constituents are available
  871. {
  872. if(!h->hasArt(constituentID, true)) //constituent must be equipped
  873. {
  874. possible = false;
  875. break;
  876. }
  877. }
  878. if(possible)
  879. ret.push_back(artifact);
  880. }
  881. return ret;
  882. }
  883. void CArtifactInstance::move(ArtifactLocation src, ArtifactLocation dst)
  884. {
  885. removeFrom(src);
  886. putAt(dst);
  887. }
  888. CArtifactInstance * CArtifactInstance::createNewArtifactInstance(CArtifact *Art)
  889. {
  890. if(!Art->constituents)
  891. {
  892. auto ret = new CArtifactInstance(Art);
  893. if (dynamic_cast<CGrowingArtifact *>(Art))
  894. {
  895. Bonus * bonus = new Bonus;
  896. bonus->type = Bonus::LEVEL_COUNTER;
  897. bonus->val = 0;
  898. ret->addNewBonus (bonus);
  899. }
  900. return ret;
  901. }
  902. else
  903. {
  904. CCombinedArtifactInstance * ret = new CCombinedArtifactInstance(Art);
  905. ret->createConstituents();
  906. return ret;
  907. }
  908. }
  909. CArtifactInstance * CArtifactInstance::createNewArtifactInstance(int aid)
  910. {
  911. return createNewArtifactInstance(VLC->arth->artifacts[aid]);
  912. }
  913. void CArtifactInstance::deserializationFix()
  914. {
  915. setType(artType);
  916. }
  917. SpellID CArtifactInstance::getGivenSpellID() const
  918. {
  919. const Bonus * b = getBonusLocalFirst(Selector::type(Bonus::SPELL));
  920. if(!b)
  921. {
  922. tlog3 << "Warning: " << nodeName() << " doesn't bear any spell!\n";
  923. return SpellID::NONE;
  924. }
  925. return SpellID(b->subtype);
  926. }
  927. bool CArtifactInstance::isPart(const CArtifactInstance *supposedPart) const
  928. {
  929. return supposedPart == this;
  930. }
  931. bool CCombinedArtifactInstance::canBePutAt(const CArtifactSet *artSet, ArtifactPosition slot, bool assumeDestRemoved /*= false*/) const
  932. {
  933. bool canMainArtifactBePlaced = CArtifactInstance::canBePutAt(artSet, slot, assumeDestRemoved);
  934. if(!canMainArtifactBePlaced)
  935. return false; //no is no...
  936. if(slot >= GameConstants::BACKPACK_START)
  937. return true; //we can always remove combined art to the backapck
  938. assert(artType->constituents);
  939. std::vector<ConstituentInfo> constituentsToBePlaced = constituentsInfo; //we'll remove constituents from that list, as we find a suitable slot for them
  940. //it may be that we picked a combined artifact in hero screen (though technically it's still there) to move it
  941. //so we remove from the list all constituents that are already present on dst hero in the form of locks
  942. BOOST_FOREACH(const ConstituentInfo &constituent, constituentsInfo)
  943. {
  944. if(constituent.art == artSet->getArt(constituent.slot, false)) //no need to worry about locked constituent
  945. constituentsToBePlaced -= constituent;
  946. }
  947. //we iterate over all active slots and check if constituents fits them
  948. for (int i = 0; i < GameConstants::BACKPACK_START; i++)
  949. {
  950. for(auto art = constituentsToBePlaced.begin(); art != constituentsToBePlaced.end(); art++)
  951. {
  952. 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
  953. {
  954. constituentsToBePlaced.erase(art);
  955. break;
  956. }
  957. }
  958. }
  959. return constituentsToBePlaced.empty();
  960. }
  961. bool CCombinedArtifactInstance::canBeDisassembled() const
  962. {
  963. return true;
  964. }
  965. CCombinedArtifactInstance::CCombinedArtifactInstance(CArtifact *Art)
  966. : CArtifactInstance(Art) //TODO: seems unued, but need to be written
  967. {
  968. }
  969. CCombinedArtifactInstance::CCombinedArtifactInstance()
  970. {
  971. }
  972. void CCombinedArtifactInstance::createConstituents()
  973. {
  974. assert(artType);
  975. assert(artType->constituents);
  976. BOOST_FOREACH(ui32 a, *artType->constituents)
  977. {
  978. addAsConstituent(CArtifactInstance::createNewArtifactInstance(a), ArtifactPosition::PRE_FIRST);
  979. }
  980. }
  981. void CCombinedArtifactInstance::addAsConstituent(CArtifactInstance *art, ArtifactPosition slot)
  982. {
  983. assert(vstd::contains(*artType->constituents, art->artType->id));
  984. assert(art->getParentNodes().size() == 1 && art->getParentNodes().front() == art->artType);
  985. constituentsInfo.push_back(ConstituentInfo(art, slot));
  986. attachTo(art);
  987. }
  988. void CCombinedArtifactInstance::putAt(ArtifactLocation al)
  989. {
  990. if(al.slot >= GameConstants::BACKPACK_START)
  991. {
  992. CArtifactInstance::putAt(al);
  993. BOOST_FOREACH(ConstituentInfo &ci, constituentsInfo)
  994. ci.slot = ArtifactPosition::PRE_FIRST;
  995. }
  996. else
  997. {
  998. CArtifactInstance *mainConstituent = figureMainConstituent(al); //it'll be replaced with combined artifact, not a lock
  999. CArtifactInstance::putAt(al); //puts combined art (this)
  1000. BOOST_FOREACH(ConstituentInfo &ci, constituentsInfo)
  1001. {
  1002. if(ci.art != mainConstituent)
  1003. {
  1004. const ArtifactLocation suggestedPos(al.artHolder, ci.slot);
  1005. const bool inActiveSlot = vstd::isbetween(ci.slot, 0, GameConstants::BACKPACK_START);
  1006. const bool suggestedPosValid = ci.art->canBePutAt(suggestedPos);
  1007. ArtifactPosition pos = ArtifactPosition::PRE_FIRST;
  1008. if(inActiveSlot && suggestedPosValid) //there is a valid suggestion where to place lock
  1009. pos = ci.slot;
  1010. else
  1011. ci.slot = pos = ci.art->firstAvailableSlot(al.getHolderArtSet());
  1012. assert(pos < GameConstants::BACKPACK_START);
  1013. al.getHolderArtSet()->setNewArtSlot(pos, ci.art, true); //sets as lock
  1014. }
  1015. else
  1016. {
  1017. ci.slot = ArtifactPosition::PRE_FIRST;
  1018. }
  1019. }
  1020. }
  1021. }
  1022. void CCombinedArtifactInstance::removeFrom(ArtifactLocation al)
  1023. {
  1024. if(al.slot >= GameConstants::BACKPACK_START)
  1025. {
  1026. CArtifactInstance::removeFrom(al);
  1027. }
  1028. else
  1029. {
  1030. BOOST_FOREACH(ConstituentInfo &ci, constituentsInfo)
  1031. {
  1032. if(ci.slot >= 0)
  1033. {
  1034. al.getHolderArtSet()->eraseArtSlot(ci.slot);
  1035. ci.slot = ArtifactPosition::PRE_FIRST;
  1036. }
  1037. else
  1038. {
  1039. //main constituent
  1040. CArtifactInstance::removeFrom(al);
  1041. }
  1042. }
  1043. }
  1044. }
  1045. CArtifactInstance * CCombinedArtifactInstance::figureMainConstituent(const ArtifactLocation al)
  1046. {
  1047. CArtifactInstance *mainConstituent = NULL; //it'll be replaced with combined artifact, not a lock
  1048. BOOST_FOREACH(ConstituentInfo &ci, constituentsInfo)
  1049. if(ci.slot == al.slot)
  1050. mainConstituent = ci.art;
  1051. if(!mainConstituent)
  1052. {
  1053. BOOST_FOREACH(ConstituentInfo &ci, constituentsInfo)
  1054. {
  1055. if(vstd::contains(ci.art->artType->possibleSlots[al.getHolderArtSet()->bearerType()], al.slot))
  1056. {
  1057. mainConstituent = ci.art;
  1058. }
  1059. }
  1060. }
  1061. return mainConstituent;
  1062. }
  1063. void CCombinedArtifactInstance::deserializationFix()
  1064. {
  1065. BOOST_FOREACH(ConstituentInfo &ci, constituentsInfo)
  1066. attachTo(ci.art);
  1067. }
  1068. bool CCombinedArtifactInstance::isPart(const CArtifactInstance *supposedPart) const
  1069. {
  1070. bool me = CArtifactInstance::isPart(supposedPart);
  1071. if(me)
  1072. return true;
  1073. //check for constituents
  1074. BOOST_FOREACH(const ConstituentInfo &constituent, constituentsInfo)
  1075. if(constituent.art == supposedPart)
  1076. return true;
  1077. return false;
  1078. }
  1079. CCombinedArtifactInstance::ConstituentInfo::ConstituentInfo(CArtifactInstance *Art /*= NULL*/, ArtifactPosition Slot /*= -1*/)
  1080. {
  1081. art = Art;
  1082. slot = Slot;
  1083. }
  1084. bool CCombinedArtifactInstance::ConstituentInfo::operator==(const ConstituentInfo &rhs) const
  1085. {
  1086. return art == rhs.art && slot == rhs.slot;
  1087. }
  1088. const CArtifactInstance* CArtifactSet::getArt(ArtifactPosition pos, bool excludeLocked /*= true*/) const
  1089. {
  1090. if(const ArtSlotInfo *si = getSlot(pos))
  1091. {
  1092. if(si->artifact && (!excludeLocked || !si->locked))
  1093. return si->artifact;
  1094. }
  1095. return NULL;
  1096. }
  1097. CArtifactInstance* CArtifactSet::getArt(ArtifactPosition pos, bool excludeLocked /*= true*/)
  1098. {
  1099. return const_cast<CArtifactInstance*>((const_cast<const CArtifactSet*>(this))->getArt(pos, excludeLocked));
  1100. }
  1101. ArtifactPosition CArtifactSet::getArtPos(int aid, bool onlyWorn /*= true*/) const
  1102. {
  1103. for(auto i = artifactsWorn.cbegin(); i != artifactsWorn.cend(); i++)
  1104. if(i->second.artifact->artType->id == aid)
  1105. return i->first;
  1106. if(onlyWorn)
  1107. return ArtifactPosition::PRE_FIRST;
  1108. for(int i = 0; i < artifactsInBackpack.size(); i++)
  1109. if(artifactsInBackpack[i].artifact->artType->id == aid)
  1110. return ArtifactPosition(GameConstants::BACKPACK_START + i);
  1111. return ArtifactPosition::PRE_FIRST;
  1112. }
  1113. ArtifactPosition CArtifactSet::getArtPos(const CArtifactInstance *art) const
  1114. {
  1115. BOOST_FOREACH(auto i, artifactsWorn)
  1116. if(i.second.artifact == art)
  1117. return i.first;
  1118. for(int i = 0; i < artifactsInBackpack.size(); i++)
  1119. if(artifactsInBackpack[i].artifact == art)
  1120. return ArtifactPosition(GameConstants::BACKPACK_START + i);
  1121. return ArtifactPosition::PRE_FIRST;
  1122. }
  1123. const CArtifactInstance * CArtifactSet::getArtByInstanceId( ArtifactInstanceID artInstId ) const
  1124. {
  1125. BOOST_FOREACH(auto i, artifactsWorn)
  1126. if(i.second.artifact->id == artInstId)
  1127. return i.second.artifact;
  1128. BOOST_FOREACH(auto i, artifactsInBackpack)
  1129. if(i.artifact->id == artInstId)
  1130. return i.artifact;
  1131. return NULL;
  1132. }
  1133. bool CArtifactSet::hasArt(ui32 aid, bool onlyWorn /*= false*/) const
  1134. {
  1135. return getArtPos(aid, onlyWorn) != ArtifactPosition::PRE_FIRST;
  1136. }
  1137. const ArtSlotInfo * CArtifactSet::getSlot(ArtifactPosition pos) const
  1138. {
  1139. if(vstd::contains(artifactsWorn, pos))
  1140. return &artifactsWorn[pos];
  1141. if(pos >= ArtifactPosition::AFTER_LAST )
  1142. {
  1143. int backpackPos = (int)pos - GameConstants::BACKPACK_START;
  1144. if(backpackPos < 0 || backpackPos >= artifactsInBackpack.size())
  1145. return NULL;
  1146. else
  1147. return &artifactsInBackpack[backpackPos];
  1148. }
  1149. return NULL;
  1150. }
  1151. bool CArtifactSet::isPositionFree(ArtifactPosition pos, bool onlyLockCheck /*= false*/) const
  1152. {
  1153. if(const ArtSlotInfo *s = getSlot(pos))
  1154. return (onlyLockCheck || !s->artifact) && !s->locked;
  1155. return true; //no slot means not used
  1156. }
  1157. si32 CArtifactSet::getArtTypeId(ArtifactPosition pos) const
  1158. {
  1159. const CArtifactInstance * const a = getArt(pos);
  1160. if(!a)
  1161. {
  1162. tlog2 << (dynamic_cast<const CGHeroInstance*>(this))->name << " has no artifact at " << pos << " (getArtTypeId)\n";
  1163. return -1;
  1164. }
  1165. return a->artType->id;
  1166. }
  1167. CArtifactSet::~CArtifactSet()
  1168. {
  1169. }
  1170. ArtSlotInfo & CArtifactSet::retreiveNewArtSlot(ArtifactPosition slot)
  1171. {
  1172. assert(!vstd::contains(artifactsWorn, slot));
  1173. ArtSlotInfo &ret = slot < GameConstants::BACKPACK_START
  1174. ? artifactsWorn[slot]
  1175. : *artifactsInBackpack.insert(artifactsInBackpack.begin() + (slot - GameConstants::BACKPACK_START), ArtSlotInfo());
  1176. return ret;
  1177. }
  1178. void CArtifactSet::setNewArtSlot(ArtifactPosition slot, CArtifactInstance *art, bool locked)
  1179. {
  1180. ArtSlotInfo &asi = retreiveNewArtSlot(slot);
  1181. asi.artifact = art;
  1182. asi.locked = locked;
  1183. }
  1184. void CArtifactSet::eraseArtSlot(ArtifactPosition slot)
  1185. {
  1186. if(slot < GameConstants::BACKPACK_START)
  1187. {
  1188. artifactsWorn.erase(slot);
  1189. }
  1190. else
  1191. {
  1192. slot = ArtifactPosition(slot - GameConstants::BACKPACK_START);
  1193. artifactsInBackpack.erase(artifactsInBackpack.begin() + slot);
  1194. }
  1195. }
  1196. void CArtifactSet::artDeserializationFix(CBonusSystemNode *node)
  1197. {
  1198. for(auto i = artifactsWorn.begin(); i != artifactsWorn.end(); i++)
  1199. if(i->second.artifact && !i->second.locked)
  1200. node->attachTo(i->second.artifact);
  1201. }