CArtHandler.cpp 36 KB

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