CArtHandler.cpp 36 KB

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