CArtHandler.cpp 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341
  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, int> 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 (ui32 i = 3; i <= 6; i++)
  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 += 146, 147, 148, 150, 151, 152, 153;
  265. static std::map<char, CArtifact::EartClass> classes =
  266. map_list_of('S',CArtifact::ART_SPECIAL)('T',CArtifact::ART_TREASURE)('N',CArtifact::ART_MINOR)('J',CArtifact::ART_MAJOR)('R',CArtifact::ART_RELIC);
  267. CLegacyConfigParser parser("DATA/ARTRAITS.TXT");
  268. CLegacyConfigParser events("DATA/ARTEVENT.TXT");
  269. parser.endLine(); // header
  270. parser.endLine();
  271. std::map<ui32,ui8>::iterator itr;
  272. for (int i=0; i<GameConstants::ARTIFACTS_QUANTITY; i++)
  273. {
  274. CArtifact *art;
  275. if (vstd::contains (growingArtifacts, i))
  276. {
  277. art = new CGrowingArtifact();
  278. }
  279. else
  280. {
  281. art = new CArtifact();
  282. }
  283. CArtifact &nart = *art;
  284. nart.id=i;
  285. nart.iconIndex=i;
  286. nart.setName (parser.readString());
  287. nart.setEventText (events.readString());
  288. events.endLine();
  289. nart.price= parser.readNumber();
  290. for(int j=0;j<slots.size();j++)
  291. {
  292. if(parser.readString() == "x")
  293. nart.possibleSlots[ArtBearer::HERO].push_back(slots[j]);
  294. }
  295. nart.aClass = classes[parser.readString()[0]];
  296. //load description and remove quotation marks
  297. nart.setDescription (parser.readString());
  298. parser.endLine();
  299. if(onlyTxt)
  300. continue;
  301. artifacts.push_back(&nart);
  302. }
  303. if (VLC->modh->modules.COMMANDERS)
  304. { //TODO: move all artifacts config to separate json file
  305. const JsonNode config(ResourceID("config/commanders.json"));
  306. BOOST_FOREACH(const JsonNode &artifact, config["artifacts"].Vector())
  307. {
  308. auto ga = dynamic_cast <CGrowingArtifact *>(artifacts[artifact["id"].Float()].get());
  309. BOOST_FOREACH (auto b, artifact["bonusesPerLevel"].Vector())
  310. {
  311. ga->bonusesPerLevel.push_back (std::pair <ui16, Bonus> (b["level"].Float(), *JsonUtils::parseBonus (b["bonus"].Vector())));
  312. }
  313. BOOST_FOREACH (auto b, artifact["thresholdBonuses"].Vector())
  314. {
  315. ga->thresholdBonuses.push_back (std::pair <ui16, Bonus> (b["level"].Float(), *JsonUtils::parseBonus (b["bonus"].Vector())));
  316. }
  317. }
  318. }
  319. sortArts();
  320. if(onlyTxt)
  321. return;
  322. addBonuses();
  323. // Populate reverse mappings of combinational artifacts.
  324. //TODO: do that also for new artifacts read from mods
  325. BOOST_FOREACH(CArtifact *artifact, artifacts)
  326. {
  327. if (artifact->constituents != NULL)
  328. {
  329. BOOST_FOREACH(ui32 constituentID, *artifact->constituents)
  330. {
  331. if (artifacts[constituentID]->constituentOf == NULL)
  332. artifacts[constituentID]->constituentOf = new std::vector<ui32>();
  333. artifacts[constituentID]->constituentOf->push_back(artifact->id);
  334. }
  335. }
  336. }
  337. }
  338. void CArtHandler::load(const JsonNode & node)
  339. {
  340. BOOST_FOREACH(auto & entry, node.Struct())
  341. {
  342. if (!entry.second.isNull()) // may happens if mod removed creature by setting json entry to null
  343. {
  344. CArtifact * art = loadArtifact(entry.second);
  345. art->id = artifacts.size();
  346. artifacts.push_back(art);
  347. tlog5 << "Added artifact: " << entry.first << "\n";
  348. VLC->modh->identifiers.registerObject (std::string("artifact.") + entry.first, art->id);
  349. }
  350. }
  351. }
  352. CArtifact * CArtHandler::loadArtifact(const JsonNode & node)
  353. {
  354. CArtifact * art = new CArtifact;
  355. const JsonNode *value;
  356. const JsonNode & text = node["text"];
  357. art->setName (text["name"].String());
  358. art->setDescription (text["description"].String());
  359. art->setEventText (text["event"].String());
  360. const JsonNode & graphics = node["graphics"];
  361. art->iconIndex = graphics["iconIndex"].Float();
  362. art->image = graphics["image"].String();
  363. value = &graphics["large"];
  364. if (!value->isNull())
  365. art->large = value->String();
  366. art->advMapDef = graphics["map"].String();
  367. art->price = node["value"].Float();
  368. {
  369. auto it = artifactClassMap.find (node["class"].String());
  370. if (it != artifactClassMap.end())
  371. {
  372. art->aClass = it->second;
  373. }
  374. else
  375. {
  376. tlog2 << "Warning! Artifact rarity " << value->String() << " not recognized!";
  377. art->aClass = CArtifact::ART_SPECIAL;
  378. }
  379. }
  380. int bearerType = -1;
  381. bool heroArt = false;
  382. {
  383. const JsonNode & bearer = node["type"];
  384. BOOST_FOREACH (const JsonNode & b, bearer.Vector())
  385. {
  386. auto it = artifactBearerMap.find (b.String());
  387. if (it != artifactBearerMap.end())
  388. {
  389. bearerType = it->second;
  390. switch (bearerType)
  391. {
  392. case ArtBearer::HERO: //TODO: allow arts having several possible bearers
  393. heroArt = true;
  394. break;
  395. case ArtBearer::COMMANDER:
  396. makeItCommanderArt (art, false); //do not erase already existing slots
  397. break;
  398. case ArtBearer::CREATURE:
  399. makeItCreatureArt (art, false);
  400. break;
  401. }
  402. }
  403. else
  404. tlog2 << "Warning! Artifact type " << b.String() << " not recognized!";
  405. }
  406. }
  407. value = &node["slot"];
  408. if (!value->isNull() && heroArt) //we assume non-hero slots are irrelevant?
  409. {
  410. std::string slotName = value->String();
  411. if (slotName == "MISC")
  412. {
  413. //unfortunatelly slot ids aare not continuous
  414. art->possibleSlots[ArtBearer::HERO] += ArtifactPosition::MISC1, ArtifactPosition::MISC2, ArtifactPosition::MISC3, ArtifactPosition::MISC4, ArtifactPosition::MISC5;
  415. }
  416. else if (slotName == "RING")
  417. {
  418. art->possibleSlots[ArtBearer::HERO] += ArtifactPosition::LEFT_RING, ArtifactPosition::RIGHT_RING;
  419. }
  420. else
  421. {
  422. auto it = artifactPositionMap.find (slotName);
  423. if (it != artifactPositionMap.end())
  424. {
  425. auto slot = it->second;
  426. art->possibleSlots[ArtBearer::HERO].push_back (slot);
  427. }
  428. else
  429. tlog2 << "Warning! Artifact slot " << value->String() << " not recognized!";
  430. }
  431. }
  432. readComponents (node, art);
  433. BOOST_FOREACH (const JsonNode &bonus, node["bonuses"].Vector())
  434. {
  435. auto b = JsonUtils::parseBonus(bonus);
  436. //TODO: bonus->sid = art->id;
  437. art->addNewBonus(b);
  438. }
  439. return art;
  440. }
  441. void CArtifact::addConstituent (ui32 component)
  442. {
  443. assert (constituents);
  444. constituents->push_back (component);
  445. }
  446. int CArtHandler::convertMachineID(int id, bool creToArt )
  447. {
  448. int dif = 142;
  449. if(creToArt)
  450. {
  451. switch (id)
  452. {
  453. case 147:
  454. dif--;
  455. break;
  456. case 148:
  457. dif++;
  458. break;
  459. }
  460. dif = -dif;
  461. }
  462. else
  463. {
  464. switch (id)
  465. {
  466. case 6:
  467. dif--;
  468. break;
  469. case 5:
  470. dif++;
  471. break;
  472. }
  473. }
  474. return id + dif;
  475. }
  476. void CArtHandler::sortArts()
  477. {
  478. //for (int i=0; i<allowedArtifacts.size(); ++i) //do 144, bo nie chcemy bzdurek
  479. //{
  480. // switch (allowedArtifacts[i]->aClass)
  481. // {
  482. // case CArtifact::ART_TREASURE:
  483. // treasures.push_back(allowedArtifacts[i]);
  484. // break;
  485. // case CArtifact::ART_MINOR:
  486. // minors.push_back(allowedArtifacts[i]);
  487. // break;
  488. // case CArtifact::ART_MAJOR:
  489. // majors.push_back(allowedArtifacts[i]);
  490. // break;
  491. // case CArtifact::ART_RELIC:
  492. // relics.push_back(allowedArtifacts[i]);
  493. // break;
  494. // }
  495. //}
  496. }
  497. void CArtHandler::erasePickedArt( TArtifactInstanceID id )
  498. {
  499. std::vector<CArtifact*>* ptr;
  500. CArtifact *art = artifacts[id];
  501. switch (art->aClass)
  502. {
  503. case CArtifact::ART_TREASURE:
  504. ptr = &treasures;
  505. break;
  506. case CArtifact::ART_MINOR:
  507. ptr = &minors;
  508. break;
  509. case CArtifact::ART_MAJOR:
  510. ptr = &majors;
  511. break;
  512. case CArtifact::ART_RELIC:
  513. ptr = &relics;
  514. break;
  515. default: //special artifacts should not be erased
  516. return;
  517. }
  518. ptr->erase (std::find(ptr->begin(), ptr->end(), art)); //remove the artifact from available list
  519. }
  520. ui16 CArtHandler::getRandomArt(int flags)
  521. {
  522. std::vector<ConstTransitivePtr<CArtifact> > out;
  523. getAllowed(out, flags);
  524. ui16 id = out[ran() % out.size()]->id;
  525. erasePickedArt (id);
  526. return id;
  527. }
  528. ui16 CArtHandler::getArtSync (ui32 rand, int flags)
  529. {
  530. std::vector<ConstTransitivePtr<CArtifact> > out;
  531. getAllowed(out, flags);
  532. CArtifact *art = out[rand % out.size()];
  533. return art->id;
  534. }
  535. void CArtHandler::getAllowed(std::vector<ConstTransitivePtr<CArtifact> > &out, int flags)
  536. {
  537. if (flags & CArtifact::ART_TREASURE)
  538. getAllowedArts (out, &treasures, CArtifact::ART_TREASURE);
  539. if (flags & CArtifact::ART_MINOR)
  540. getAllowedArts (out, &minors, CArtifact::ART_MINOR);
  541. if (flags & CArtifact::ART_MAJOR)
  542. getAllowedArts (out, &majors, CArtifact::ART_MAJOR);
  543. if (flags & CArtifact::ART_RELIC)
  544. getAllowedArts (out, &relics, CArtifact::ART_RELIC);
  545. if (!out.size()) //no artifact of specified rarity, we need to take another one
  546. {
  547. getAllowedArts (out, &treasures, CArtifact::ART_TREASURE);
  548. getAllowedArts (out, &minors, CArtifact::ART_MINOR);
  549. getAllowedArts (out, &majors, CArtifact::ART_MAJOR);
  550. getAllowedArts (out, &relics, CArtifact::ART_RELIC);
  551. }
  552. if (!out.size()) //no arts are available at all
  553. {
  554. out.resize (64);
  555. std::fill_n (out.begin(), 64, artifacts[2]); //Give Grail - this can't be banned (hopefully)
  556. }
  557. }
  558. void CArtHandler::getAllowedArts(std::vector<ConstTransitivePtr<CArtifact> > &out, std::vector<CArtifact*> *arts, int flag)
  559. {
  560. if (arts->empty()) //restock available arts
  561. {
  562. for (int i = 0; i < allowedArtifacts.size(); ++i)
  563. {
  564. if (allowedArtifacts[i]->aClass == flag)
  565. arts->push_back(allowedArtifacts[i]);
  566. }
  567. }
  568. for (int i = 0; i < arts->size(); ++i)
  569. {
  570. CArtifact *art = (*arts)[i];
  571. out.push_back(art);
  572. }
  573. }
  574. Bonus *createBonus(Bonus::BonusType type, int val, int subtype, int valType, shared_ptr<ILimiter> limiter = shared_ptr<ILimiter>(), int additionalInfo = 0)
  575. {
  576. Bonus *added = new Bonus(Bonus::PERMANENT,type,Bonus::ARTIFACT,val,-1,subtype);
  577. added->additionalInfo = additionalInfo;
  578. added->valType = valType;
  579. added->limiter = limiter;
  580. return added;
  581. }
  582. Bonus *createBonus(Bonus::BonusType type, int val, int subtype, shared_ptr<IPropagator> propagator = shared_ptr<IPropagator>(), int additionalInfo = 0)
  583. {
  584. Bonus *added = new Bonus(Bonus::PERMANENT,type,Bonus::ARTIFACT,val,-1,subtype);
  585. added->additionalInfo = additionalInfo;
  586. added->valType = Bonus::BASE_NUMBER;
  587. added->propagator = propagator;
  588. return added;
  589. }
  590. void CArtHandler::giveArtBonus( TArtifactID aid, Bonus::BonusType type, int val, int subtype, int valType, shared_ptr<ILimiter> limiter, int additionalInfo)
  591. {
  592. giveArtBonus(aid, createBonus(type, val, subtype, valType, limiter, additionalInfo));
  593. }
  594. void CArtHandler::giveArtBonus(TArtifactID aid, Bonus::BonusType type, int val, int subtype, shared_ptr<IPropagator> propagator /*= NULL*/, int additionalInfo)
  595. {
  596. giveArtBonus(aid, createBonus(type, val, subtype, propagator, additionalInfo));
  597. }
  598. void CArtHandler::giveArtBonus(TArtifactID aid, Bonus *bonus)
  599. {
  600. bonus->sid = aid;
  601. if(bonus->subtype == Bonus::MORALE || bonus->type == Bonus::LUCK)
  602. bonus->description = artifacts[aid]->Name() + (bonus->val > 0 ? " +" : " ") + boost::lexical_cast<std::string>(bonus->val);
  603. else
  604. bonus->description = artifacts[aid]->Name();
  605. artifacts[aid]->addNewBonus(bonus);
  606. }
  607. void CArtHandler::makeItCreatureArt (CArtifact * a, bool onlyCreature /*=true*/)
  608. {
  609. if (onlyCreature)
  610. {
  611. a->possibleSlots[ArtBearer::HERO].clear();
  612. a->possibleSlots[ArtBearer::COMMANDER].clear();
  613. }
  614. a->possibleSlots[ArtBearer::CREATURE].push_back(ArtifactPosition::CREATURE_SLOT);
  615. }
  616. void CArtHandler::makeItCreatureArt (TArtifactInstanceID aid, bool onlyCreature /*=true*/)
  617. {
  618. CArtifact *a = artifacts[aid];
  619. makeItCreatureArt (a, onlyCreature);
  620. }
  621. void CArtHandler::makeItCommanderArt (CArtifact * a, bool onlyCommander /*= true*/ )
  622. {
  623. if (onlyCommander)
  624. {
  625. a->possibleSlots[ArtBearer::HERO].clear();
  626. a->possibleSlots[ArtBearer::CREATURE].clear();
  627. }
  628. for (int i = ArtifactPosition::COMMANDER1; i <= ArtifactPosition::COMMANDER6; ++i)
  629. a->possibleSlots[ArtBearer::COMMANDER].push_back(i);
  630. }
  631. void CArtHandler::makeItCommanderArt( TArtifactInstanceID aid, bool onlyCommander /*= true*/ )
  632. {
  633. CArtifact *a = artifacts[aid];
  634. makeItCommanderArt (a, onlyCommander);
  635. }
  636. void CArtHandler::addBonuses()
  637. {
  638. const JsonNode config(ResourceID("config/artifacts.json"));
  639. BOOST_FOREACH (auto & artifact, config["artifacts"].Struct()) //pair <string, JsonNode> (id, properties)
  640. {
  641. auto ga = artifacts[artifact.second["id"].Float()].get();
  642. BOOST_FOREACH (auto b, artifact.second["bonuses"].Vector())
  643. {
  644. auto bonus = JsonUtils::parseBonus (b);
  645. bonus->sid = ga->id;
  646. ga->addNewBonus (bonus);
  647. }
  648. BOOST_FOREACH (const JsonNode & b, artifact.second["type"].Vector()) //TODO: remove duplicate code
  649. {
  650. auto it = artifactBearerMap.find (b.String());
  651. if (it != artifactBearerMap.end())
  652. {
  653. int bearerType = it->second;
  654. switch (bearerType)
  655. {
  656. case ArtBearer::HERO:
  657. break;
  658. case ArtBearer::COMMANDER:
  659. makeItCommanderArt (ga); //original artifacts should have only one bearer type
  660. break;
  661. case ArtBearer::CREATURE:
  662. makeItCreatureArt (ga);
  663. break;
  664. }
  665. }
  666. else
  667. tlog2 << "Warning! Artifact type " << b.String() << " not recognized!";
  668. }
  669. readComponents (artifact.second, ga);
  670. VLC->modh->identifiers.registerObject ("artifact." + artifact.first, ga->id);
  671. }
  672. }
  673. void CArtHandler::readComponents (const JsonNode & node, CArtifact * art)
  674. {
  675. const JsonNode *value;
  676. value = &node["components"];
  677. if (!value->isNull())
  678. {
  679. art->constituents = new std::vector<ui32>();
  680. BOOST_FOREACH (auto component, value->Vector())
  681. {
  682. VLC->modh->identifiers.requestIdentifier(std::string("artifact.") + component.String(),
  683. boost::bind (&CArtifact::addConstituent, art, _1)
  684. );
  685. }
  686. }
  687. }
  688. void CArtHandler::clear()
  689. {
  690. BOOST_FOREACH(CArtifact *art, artifacts)
  691. delete art;
  692. artifacts.clear();
  693. clearHlpLists();
  694. }
  695. void CArtHandler::clearHlpLists()
  696. {
  697. treasures.clear();
  698. minors.clear();
  699. majors.clear();
  700. relics.clear();
  701. }
  702. void CArtHandler::initAllowedArtifactsList(const std::vector<ui8> &allowed)
  703. {
  704. allowedArtifacts.clear();
  705. clearHlpLists();
  706. for (int i=0; i<144; ++i) //yes, 144
  707. {
  708. if (allowed[i])
  709. allowedArtifacts.push_back(artifacts[i]);
  710. }
  711. if (VLC->modh->modules.COMMANDERS) //allow all commander artifacts for testing
  712. {
  713. for (int i = 146; i <= 155; ++i)
  714. {
  715. allowedArtifacts.push_back(artifacts[i]);
  716. }
  717. }
  718. for (int i = GameConstants::ARTIFACTS_QUANTITY; i < artifacts.size(); ++i) //allow all new artifacts by default
  719. {
  720. if (artifacts[i]->possibleSlots[ArtBearer::HERO].size())
  721. allowedArtifacts.push_back(artifacts[i]);
  722. else //check if active modules allow artifact to be every used
  723. {
  724. if ((artifacts[i]->possibleSlots[ArtBearer::COMMANDER].size() && VLC->modh->modules.COMMANDERS) ||
  725. (artifacts[i]->possibleSlots[ArtBearer::CREATURE].size() && VLC->modh->modules.STACK_ARTIFACT))
  726. allowedArtifacts.push_back(artifacts[i]);
  727. //keep im mind that artifact can be worn by more than one type of bearer
  728. }
  729. }
  730. }
  731. std::vector<ui8> CArtHandler::getDefaultAllowedArtifacts() const
  732. {
  733. std::vector<ui8> allowedArtifacts;
  734. allowedArtifacts.resize(127, 1);
  735. allowedArtifacts.resize(141, 0);
  736. allowedArtifacts.resize(GameConstants::ARTIFACTS_QUANTITY, 1);
  737. return allowedArtifacts;
  738. }
  739. CArtifactInstance::CArtifactInstance()
  740. {
  741. init();
  742. }
  743. CArtifactInstance::CArtifactInstance( CArtifact *Art)
  744. {
  745. init();
  746. setType(Art);
  747. }
  748. // CArtifactInstance::CArtifactInstance(int aid)
  749. // {
  750. // init();
  751. // setType(VLC->arth->artifacts[aid]);
  752. // }
  753. void CArtifactInstance::setType( CArtifact *Art )
  754. {
  755. artType = Art;
  756. attachTo(Art);
  757. }
  758. std::string CArtifactInstance::nodeName() const
  759. {
  760. return "Artifact instance of " + (artType ? artType->Name() : std::string("uninitialized")) + " type";
  761. }
  762. CArtifactInstance * CArtifactInstance::createScroll( const CSpell *s)
  763. {
  764. CArtifactInstance *ret = new CArtifactInstance(VLC->arth->artifacts[1]);
  765. Bonus *b = new Bonus(Bonus::PERMANENT, Bonus::SPELL, Bonus::ARTIFACT_INSTANCE, -1, 1, s->id);
  766. ret->addNewBonus(b);
  767. return ret;
  768. }
  769. void CArtifactInstance::init()
  770. {
  771. id = -1;
  772. setNodeType(ARTIFACT_INSTANCE);
  773. }
  774. int CArtifactInstance::firstAvailableSlot(const CArtifactSet *h) const
  775. {
  776. BOOST_FOREACH(ui16 slot, artType->possibleSlots[h->bearerType()])
  777. {
  778. if(canBePutAt(h, slot)) //if(artType->fitsAt(h->artifWorn, slot))
  779. {
  780. //we've found a free suitable slot.
  781. return slot;
  782. }
  783. }
  784. //if haven't find proper slot, use backpack
  785. return firstBackpackSlot(h);
  786. }
  787. int CArtifactInstance::firstBackpackSlot(const CArtifactSet *h) const
  788. {
  789. if(!artType->isBig()) //discard big artifact
  790. return GameConstants::BACKPACK_START + h->artifactsInBackpack.size();
  791. return -1;
  792. }
  793. bool CArtifactInstance::canBePutAt(const ArtifactLocation al, bool assumeDestRemoved /*= false*/) const
  794. {
  795. return canBePutAt(al.getHolderArtSet(), al.slot, assumeDestRemoved);
  796. }
  797. bool CArtifactInstance::canBePutAt(const CArtifactSet *artSet, int slot, bool assumeDestRemoved /*= false*/) const
  798. {
  799. if(slot >= GameConstants::BACKPACK_START)
  800. {
  801. if(artType->isBig())
  802. return false;
  803. //TODO backpack limit
  804. return true;
  805. }
  806. auto possibleSlots = artType->possibleSlots.find(artSet->bearerType());
  807. if(possibleSlots == artType->possibleSlots.end())
  808. {
  809. tlog3 << "Warning: arrtifact " << artType->Name() << " doesn't have defined allowed slots for bearer of type "
  810. << artSet->bearerType() << std::endl;
  811. return false;
  812. }
  813. if(!vstd::contains(possibleSlots->second, slot))
  814. return false;
  815. return artSet->isPositionFree(slot, assumeDestRemoved);
  816. }
  817. void CArtifactInstance::putAt(ArtifactLocation al)
  818. {
  819. assert(canBePutAt(al));
  820. al.getHolderArtSet()->setNewArtSlot(al.slot, this, false);
  821. if(al.slot < GameConstants::BACKPACK_START)
  822. al.getHolderNode()->attachTo(this);
  823. }
  824. void CArtifactInstance::removeFrom(ArtifactLocation al)
  825. {
  826. assert(al.getHolderArtSet()->getArt(al.slot) == this);
  827. al.getHolderArtSet()->eraseArtSlot(al.slot);
  828. if(al.slot < GameConstants::BACKPACK_START)
  829. al.getHolderNode()->detachFrom(this);
  830. //TODO delete me?
  831. }
  832. bool CArtifactInstance::canBeDisassembled() const
  833. {
  834. return artType->constituents && artType->constituentOf->size();
  835. }
  836. std::vector<const CArtifact *> CArtifactInstance::assemblyPossibilities(const CArtifactSet *h) const
  837. {
  838. std::vector<const CArtifact *> ret;
  839. if(!artType->constituentOf //not a part of combined artifact
  840. || artType->constituents) //combined artifact already: no combining of combined artifacts... for now.
  841. return ret;
  842. BOOST_FOREACH(ui32 possibleCombinedArt, *artType->constituentOf)
  843. {
  844. const CArtifact * const artifact = VLC->arth->artifacts[possibleCombinedArt];
  845. assert(artifact->constituents);
  846. bool possible = true;
  847. BOOST_FOREACH(ui32 constituentID, *artifact->constituents) //check if all constituents are available
  848. {
  849. if(!h->hasArt(constituentID, true)) //constituent must be equipped
  850. {
  851. possible = false;
  852. break;
  853. }
  854. }
  855. if(possible)
  856. ret.push_back(artifact);
  857. }
  858. return ret;
  859. }
  860. void CArtifactInstance::move(ArtifactLocation src, ArtifactLocation dst)
  861. {
  862. removeFrom(src);
  863. putAt(dst);
  864. }
  865. CArtifactInstance * CArtifactInstance::createNewArtifactInstance(CArtifact *Art)
  866. {
  867. if(!Art->constituents)
  868. {
  869. auto ret = new CArtifactInstance(Art);
  870. if (dynamic_cast<CGrowingArtifact *>(Art))
  871. {
  872. Bonus * bonus = new Bonus;
  873. bonus->type = Bonus::LEVEL_COUNTER;
  874. bonus->val = 0;
  875. ret->addNewBonus (bonus);
  876. }
  877. return ret;
  878. }
  879. else
  880. {
  881. CCombinedArtifactInstance * ret = new CCombinedArtifactInstance(Art);
  882. ret->createConstituents();
  883. return ret;
  884. }
  885. }
  886. CArtifactInstance * CArtifactInstance::createNewArtifactInstance(int aid)
  887. {
  888. return createNewArtifactInstance(VLC->arth->artifacts[aid]);
  889. }
  890. void CArtifactInstance::deserializationFix()
  891. {
  892. setType(artType);
  893. }
  894. int CArtifactInstance::getGivenSpellID() const
  895. {
  896. const Bonus * b = getBonusLocalFirst(Selector::type(Bonus::SPELL));
  897. if(!b)
  898. {
  899. tlog3 << "Warning: " << nodeName() << " doesn't bear any spell!\n";
  900. return -1;
  901. }
  902. return b->subtype;
  903. }
  904. bool CArtifactInstance::isPart(const CArtifactInstance *supposedPart) const
  905. {
  906. return supposedPart == this;
  907. }
  908. bool CCombinedArtifactInstance::canBePutAt(const CArtifactSet *artSet, int slot, bool assumeDestRemoved /*= false*/) const
  909. {
  910. bool canMainArtifactBePlaced = CArtifactInstance::canBePutAt(artSet, slot, assumeDestRemoved);
  911. if(!canMainArtifactBePlaced)
  912. return false; //no is no...
  913. if(slot >= GameConstants::BACKPACK_START)
  914. return true; //we can always remove combined art to the backapck
  915. assert(artType->constituents);
  916. std::vector<ConstituentInfo> constituentsToBePlaced = constituentsInfo; //we'll remove constituents from that list, as we find a suitable slot for them
  917. //it may be that we picked a combined artifact in hero screen (though technically it's still there) to move it
  918. //so we remove from the list all constituents that are already present on dst hero in the form of locks
  919. BOOST_FOREACH(const ConstituentInfo &constituent, constituentsInfo)
  920. {
  921. if(constituent.art == artSet->getArt(constituent.slot, false)) //no need to worry about locked constituent
  922. constituentsToBePlaced -= constituent;
  923. }
  924. //we iterate over all active slots and check if constituents fits them
  925. for (int i = 0; i < GameConstants::BACKPACK_START; i++)
  926. {
  927. for(std::vector<ConstituentInfo>::iterator art = constituentsToBePlaced.begin(); art != constituentsToBePlaced.end(); art++)
  928. {
  929. if(art->art->canBePutAt(artSet, i, i == slot)) // i == al.slot because we can remove already worn artifact only from that slot that is our main destination
  930. {
  931. constituentsToBePlaced.erase(art);
  932. break;
  933. }
  934. }
  935. }
  936. return constituentsToBePlaced.empty();
  937. }
  938. bool CCombinedArtifactInstance::canBeDisassembled() const
  939. {
  940. return true;
  941. }
  942. CCombinedArtifactInstance::CCombinedArtifactInstance(CArtifact *Art)
  943. : CArtifactInstance(Art) //TODO: seems unued, but need to be written
  944. {
  945. }
  946. CCombinedArtifactInstance::CCombinedArtifactInstance()
  947. {
  948. }
  949. void CCombinedArtifactInstance::createConstituents()
  950. {
  951. assert(artType);
  952. assert(artType->constituents);
  953. BOOST_FOREACH(ui32 a, *artType->constituents)
  954. {
  955. addAsConstituent(CArtifactInstance::createNewArtifactInstance(a), -1);
  956. }
  957. }
  958. void CCombinedArtifactInstance::addAsConstituent(CArtifactInstance *art, int slot)
  959. {
  960. assert(vstd::contains(*artType->constituents, art->artType->id));
  961. assert(art->getParentNodes().size() == 1 && art->getParentNodes().front() == art->artType);
  962. constituentsInfo.push_back(ConstituentInfo(art, slot));
  963. attachTo(art);
  964. }
  965. void CCombinedArtifactInstance::putAt(ArtifactLocation al)
  966. {
  967. if(al.slot >= GameConstants::BACKPACK_START)
  968. {
  969. CArtifactInstance::putAt(al);
  970. BOOST_FOREACH(ConstituentInfo &ci, constituentsInfo)
  971. ci.slot = -1;
  972. }
  973. else
  974. {
  975. CArtifactInstance *mainConstituent = figureMainConstituent(al); //it'll be replaced with combined artifact, not a lock
  976. CArtifactInstance::putAt(al); //puts combined art (this)
  977. BOOST_FOREACH(ConstituentInfo &ci, constituentsInfo)
  978. {
  979. if(ci.art != mainConstituent)
  980. {
  981. const ArtifactLocation suggestedPos(al.artHolder, ci.slot);
  982. const bool inActiveSlot = vstd::isbetween(ci.slot, 0, GameConstants::BACKPACK_START);
  983. const bool suggestedPosValid = ci.art->canBePutAt(suggestedPos);
  984. int pos = -1;
  985. if(inActiveSlot && suggestedPosValid) //there is a valid suggestion where to place lock
  986. pos = ci.slot;
  987. else
  988. ci.slot = pos = ci.art->firstAvailableSlot(al.getHolderArtSet());
  989. assert(pos < GameConstants::BACKPACK_START);
  990. al.getHolderArtSet()->setNewArtSlot(pos, ci.art, true); //sets as lock
  991. }
  992. else
  993. {
  994. ci.slot = -1;
  995. }
  996. }
  997. }
  998. }
  999. void CCombinedArtifactInstance::removeFrom(ArtifactLocation al)
  1000. {
  1001. if(al.slot >= GameConstants::BACKPACK_START)
  1002. {
  1003. CArtifactInstance::removeFrom(al);
  1004. }
  1005. else
  1006. {
  1007. BOOST_FOREACH(ConstituentInfo &ci, constituentsInfo)
  1008. {
  1009. if(ci.slot >= 0)
  1010. {
  1011. al.getHolderArtSet()->eraseArtSlot(ci.slot);
  1012. ci.slot = -1;
  1013. }
  1014. else
  1015. {
  1016. //main constituent
  1017. CArtifactInstance::removeFrom(al);
  1018. }
  1019. }
  1020. }
  1021. }
  1022. CArtifactInstance * CCombinedArtifactInstance::figureMainConstituent(const ArtifactLocation al)
  1023. {
  1024. CArtifactInstance *mainConstituent = NULL; //it'll be replaced with combined artifact, not a lock
  1025. BOOST_FOREACH(ConstituentInfo &ci, constituentsInfo)
  1026. if(ci.slot == al.slot)
  1027. mainConstituent = ci.art;
  1028. if(!mainConstituent)
  1029. {
  1030. BOOST_FOREACH(ConstituentInfo &ci, constituentsInfo)
  1031. {
  1032. if(vstd::contains(ci.art->artType->possibleSlots[al.getHolderArtSet()->bearerType()], al.slot))
  1033. {
  1034. mainConstituent = ci.art;
  1035. }
  1036. }
  1037. }
  1038. return mainConstituent;
  1039. }
  1040. void CCombinedArtifactInstance::deserializationFix()
  1041. {
  1042. BOOST_FOREACH(ConstituentInfo &ci, constituentsInfo)
  1043. attachTo(ci.art);
  1044. }
  1045. bool CCombinedArtifactInstance::isPart(const CArtifactInstance *supposedPart) const
  1046. {
  1047. bool me = CArtifactInstance::isPart(supposedPart);
  1048. if(me)
  1049. return true;
  1050. //check for constituents
  1051. BOOST_FOREACH(const ConstituentInfo &constituent, constituentsInfo)
  1052. if(constituent.art == supposedPart)
  1053. return true;
  1054. return false;
  1055. }
  1056. CCombinedArtifactInstance::ConstituentInfo::ConstituentInfo(CArtifactInstance *Art /*= NULL*/, ui16 Slot /*= -1*/)
  1057. {
  1058. art = Art;
  1059. slot = Slot;
  1060. }
  1061. bool CCombinedArtifactInstance::ConstituentInfo::operator==(const ConstituentInfo &rhs) const
  1062. {
  1063. return art == rhs.art && slot == rhs.slot;
  1064. }
  1065. const CArtifactInstance* CArtifactSet::getArt(ui16 pos, bool excludeLocked /*= true*/) const
  1066. {
  1067. if(const ArtSlotInfo *si = getSlot(pos))
  1068. {
  1069. if(si->artifact && (!excludeLocked || !si->locked))
  1070. return si->artifact;
  1071. }
  1072. return NULL;
  1073. }
  1074. CArtifactInstance* CArtifactSet::getArt(ui16 pos, bool excludeLocked /*= true*/)
  1075. {
  1076. return const_cast<CArtifactInstance*>((const_cast<const CArtifactSet*>(this))->getArt(pos, excludeLocked));
  1077. }
  1078. si32 CArtifactSet::getArtPos(int aid, bool onlyWorn /*= true*/) const
  1079. {
  1080. for(std::map<ui16, ArtSlotInfo>::const_iterator i = artifactsWorn.begin(); i != artifactsWorn.end(); i++)
  1081. if(i->second.artifact->artType->id == aid)
  1082. return i->first;
  1083. if(onlyWorn)
  1084. return -1;
  1085. for(int i = 0; i < artifactsInBackpack.size(); i++)
  1086. if(artifactsInBackpack[i].artifact->artType->id == aid)
  1087. return GameConstants::BACKPACK_START + i;
  1088. return -1;
  1089. }
  1090. si32 CArtifactSet::getArtPos(const CArtifactInstance *art) const
  1091. {
  1092. for(std::map<ui16, ArtSlotInfo>::const_iterator i = artifactsWorn.begin(); i != artifactsWorn.end(); i++)
  1093. if(i->second.artifact == art)
  1094. return i->first;
  1095. for(int i = 0; i < artifactsInBackpack.size(); i++)
  1096. if(artifactsInBackpack[i].artifact == art)
  1097. return GameConstants::BACKPACK_START + i;
  1098. return -1;
  1099. }
  1100. const CArtifactInstance * CArtifactSet::getArtByInstanceId( TArtifactInstanceID artInstId ) const
  1101. {
  1102. for(std::map<ui16, ArtSlotInfo>::const_iterator i = artifactsWorn.begin(); i != artifactsWorn.end(); i++)
  1103. if(i->second.artifact->id == artInstId)
  1104. return i->second.artifact;
  1105. for(int i = 0; i < artifactsInBackpack.size(); i++)
  1106. if(artifactsInBackpack[i].artifact->id == artInstId)
  1107. return artifactsInBackpack[i].artifact;
  1108. return NULL;
  1109. }
  1110. bool CArtifactSet::hasArt(ui32 aid, bool onlyWorn /*= false*/) const
  1111. {
  1112. return getArtPos(aid, onlyWorn) != -1;
  1113. }
  1114. const ArtSlotInfo * CArtifactSet::getSlot(ui16 pos) const
  1115. {
  1116. if(vstd::contains(artifactsWorn, pos))
  1117. return &artifactsWorn[pos];
  1118. if(pos >= ArtifactPosition::AFTER_LAST )
  1119. {
  1120. int backpackPos = (int)pos - GameConstants::BACKPACK_START;
  1121. if(backpackPos < 0 || backpackPos >= artifactsInBackpack.size())
  1122. return NULL;
  1123. else
  1124. return &artifactsInBackpack[backpackPos];
  1125. }
  1126. return NULL;
  1127. }
  1128. bool CArtifactSet::isPositionFree(ui16 pos, bool onlyLockCheck /*= false*/) const
  1129. {
  1130. if(const ArtSlotInfo *s = getSlot(pos))
  1131. return (onlyLockCheck || !s->artifact) && !s->locked;
  1132. return true; //no slot means not used
  1133. }
  1134. si32 CArtifactSet::getArtTypeId(ui16 pos) const
  1135. {
  1136. const CArtifactInstance * const a = getArt(pos);
  1137. if(!a)
  1138. {
  1139. tlog2 << (dynamic_cast<const CGHeroInstance*>(this))->name << " has no artifact at " << pos << " (getArtTypeId)\n";
  1140. return -1;
  1141. }
  1142. return a->artType->id;
  1143. }
  1144. CArtifactSet::~CArtifactSet()
  1145. {
  1146. }
  1147. ArtSlotInfo & CArtifactSet::retreiveNewArtSlot(ui16 slot)
  1148. {
  1149. assert(!vstd::contains(artifactsWorn, slot));
  1150. ArtSlotInfo &ret = slot < GameConstants::BACKPACK_START
  1151. ? artifactsWorn[slot]
  1152. : *artifactsInBackpack.insert(artifactsInBackpack.begin() + (slot - GameConstants::BACKPACK_START), ArtSlotInfo());
  1153. return ret;
  1154. }
  1155. void CArtifactSet::setNewArtSlot(ui16 slot, CArtifactInstance *art, bool locked)
  1156. {
  1157. ArtSlotInfo &asi = retreiveNewArtSlot(slot);
  1158. asi.artifact = art;
  1159. asi.locked = locked;
  1160. }
  1161. void CArtifactSet::eraseArtSlot(ui16 slot)
  1162. {
  1163. if(slot < GameConstants::BACKPACK_START)
  1164. {
  1165. artifactsWorn.erase(slot);
  1166. }
  1167. else
  1168. {
  1169. slot -= GameConstants::BACKPACK_START;
  1170. artifactsInBackpack.erase(artifactsInBackpack.begin() + slot);
  1171. }
  1172. }
  1173. void CArtifactSet::artDeserializationFix(CBonusSystemNode *node)
  1174. {
  1175. for(bmap<ui16, ArtSlotInfo>::iterator i = artifactsWorn.begin(); i != artifactsWorn.end(); i++)
  1176. if(i->second.artifact && !i->second.locked)
  1177. node->attachTo(i->second.artifact);
  1178. }