CArtHandler.cpp 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347
  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. bool CArtHandler::legalArtifact(int id)
  703. {
  704. return (artifacts[id]->possibleSlots[ArtBearer::HERO].size() ||
  705. (artifacts[id]->possibleSlots[ArtBearer::COMMANDER].size() && VLC->modh->modules.COMMANDERS)) ||
  706. (artifacts[id]->possibleSlots[ArtBearer::CREATURE].size() && VLC->modh->modules.STACK_ARTIFACT);
  707. }
  708. void CArtHandler::initAllowedArtifactsList(const std::vector<ui8> &allowed)
  709. {
  710. allowedArtifacts.clear();
  711. clearHlpLists();
  712. for (int i=0; i<144; ++i) //yes, 144
  713. {
  714. if (allowed[i] && legalArtifact(i))
  715. allowedArtifacts.push_back(artifacts[i]);
  716. }
  717. if (VLC->modh->modules.COMMANDERS) //allow all commander artifacts for testing
  718. {
  719. for (int i = 146; i <= 155; ++i)
  720. {
  721. allowedArtifacts.push_back(artifacts[i]);
  722. }
  723. }
  724. for (int i = GameConstants::ARTIFACTS_QUANTITY; i < artifacts.size(); ++i) //allow all new artifacts by default
  725. {
  726. if (artifacts[i]->possibleSlots[ArtBearer::HERO].size())
  727. allowedArtifacts.push_back(artifacts[i]);
  728. else //check if active modules allow artifact to be every used
  729. {
  730. if (legalArtifact(i))
  731. allowedArtifacts.push_back(artifacts[i]);
  732. //keep im mind that artifact can be worn by more than one type of bearer
  733. }
  734. }
  735. }
  736. std::vector<ui8> CArtHandler::getDefaultAllowedArtifacts() const
  737. {
  738. std::vector<ui8> allowedArtifacts;
  739. allowedArtifacts.resize(127, 1);
  740. allowedArtifacts.resize(141, 0);
  741. allowedArtifacts.resize(GameConstants::ARTIFACTS_QUANTITY, 1);
  742. return allowedArtifacts;
  743. }
  744. CArtifactInstance::CArtifactInstance()
  745. {
  746. init();
  747. }
  748. CArtifactInstance::CArtifactInstance( CArtifact *Art)
  749. {
  750. init();
  751. setType(Art);
  752. }
  753. // CArtifactInstance::CArtifactInstance(int aid)
  754. // {
  755. // init();
  756. // setType(VLC->arth->artifacts[aid]);
  757. // }
  758. void CArtifactInstance::setType( CArtifact *Art )
  759. {
  760. artType = Art;
  761. attachTo(Art);
  762. }
  763. std::string CArtifactInstance::nodeName() const
  764. {
  765. return "Artifact instance of " + (artType ? artType->Name() : std::string("uninitialized")) + " type";
  766. }
  767. CArtifactInstance * CArtifactInstance::createScroll( const CSpell *s)
  768. {
  769. CArtifactInstance *ret = new CArtifactInstance(VLC->arth->artifacts[1]);
  770. Bonus *b = new Bonus(Bonus::PERMANENT, Bonus::SPELL, Bonus::ARTIFACT_INSTANCE, -1, 1, s->id);
  771. ret->addNewBonus(b);
  772. return ret;
  773. }
  774. void CArtifactInstance::init()
  775. {
  776. id = -1;
  777. setNodeType(ARTIFACT_INSTANCE);
  778. }
  779. int CArtifactInstance::firstAvailableSlot(const CArtifactSet *h) const
  780. {
  781. BOOST_FOREACH(ui16 slot, artType->possibleSlots[h->bearerType()])
  782. {
  783. if(canBePutAt(h, slot)) //if(artType->fitsAt(h->artifWorn, slot))
  784. {
  785. //we've found a free suitable slot.
  786. return slot;
  787. }
  788. }
  789. //if haven't find proper slot, use backpack
  790. return firstBackpackSlot(h);
  791. }
  792. int CArtifactInstance::firstBackpackSlot(const CArtifactSet *h) const
  793. {
  794. if(!artType->isBig()) //discard big artifact
  795. return GameConstants::BACKPACK_START + h->artifactsInBackpack.size();
  796. return -1;
  797. }
  798. bool CArtifactInstance::canBePutAt(const ArtifactLocation al, bool assumeDestRemoved /*= false*/) const
  799. {
  800. return canBePutAt(al.getHolderArtSet(), al.slot, assumeDestRemoved);
  801. }
  802. bool CArtifactInstance::canBePutAt(const CArtifactSet *artSet, int slot, bool assumeDestRemoved /*= false*/) const
  803. {
  804. if(slot >= GameConstants::BACKPACK_START)
  805. {
  806. if(artType->isBig())
  807. return false;
  808. //TODO backpack limit
  809. return true;
  810. }
  811. auto possibleSlots = artType->possibleSlots.find(artSet->bearerType());
  812. if(possibleSlots == artType->possibleSlots.end())
  813. {
  814. tlog3 << "Warning: arrtifact " << artType->Name() << " doesn't have defined allowed slots for bearer of type "
  815. << artSet->bearerType() << std::endl;
  816. return false;
  817. }
  818. if(!vstd::contains(possibleSlots->second, slot))
  819. return false;
  820. return artSet->isPositionFree(slot, assumeDestRemoved);
  821. }
  822. void CArtifactInstance::putAt(ArtifactLocation al)
  823. {
  824. assert(canBePutAt(al));
  825. al.getHolderArtSet()->setNewArtSlot(al.slot, this, false);
  826. if(al.slot < GameConstants::BACKPACK_START)
  827. al.getHolderNode()->attachTo(this);
  828. }
  829. void CArtifactInstance::removeFrom(ArtifactLocation al)
  830. {
  831. assert(al.getHolderArtSet()->getArt(al.slot) == this);
  832. al.getHolderArtSet()->eraseArtSlot(al.slot);
  833. if(al.slot < GameConstants::BACKPACK_START)
  834. al.getHolderNode()->detachFrom(this);
  835. //TODO delete me?
  836. }
  837. bool CArtifactInstance::canBeDisassembled() const
  838. {
  839. return artType->constituents && artType->constituentOf->size();
  840. }
  841. std::vector<const CArtifact *> CArtifactInstance::assemblyPossibilities(const CArtifactSet *h) const
  842. {
  843. std::vector<const CArtifact *> ret;
  844. if(!artType->constituentOf //not a part of combined artifact
  845. || artType->constituents) //combined artifact already: no combining of combined artifacts... for now.
  846. return ret;
  847. BOOST_FOREACH(ui32 possibleCombinedArt, *artType->constituentOf)
  848. {
  849. const CArtifact * const artifact = VLC->arth->artifacts[possibleCombinedArt];
  850. assert(artifact->constituents);
  851. bool possible = true;
  852. BOOST_FOREACH(ui32 constituentID, *artifact->constituents) //check if all constituents are available
  853. {
  854. if(!h->hasArt(constituentID, true)) //constituent must be equipped
  855. {
  856. possible = false;
  857. break;
  858. }
  859. }
  860. if(possible)
  861. ret.push_back(artifact);
  862. }
  863. return ret;
  864. }
  865. void CArtifactInstance::move(ArtifactLocation src, ArtifactLocation dst)
  866. {
  867. removeFrom(src);
  868. putAt(dst);
  869. }
  870. CArtifactInstance * CArtifactInstance::createNewArtifactInstance(CArtifact *Art)
  871. {
  872. if(!Art->constituents)
  873. {
  874. auto ret = new CArtifactInstance(Art);
  875. if (dynamic_cast<CGrowingArtifact *>(Art))
  876. {
  877. Bonus * bonus = new Bonus;
  878. bonus->type = Bonus::LEVEL_COUNTER;
  879. bonus->val = 0;
  880. ret->addNewBonus (bonus);
  881. }
  882. return ret;
  883. }
  884. else
  885. {
  886. CCombinedArtifactInstance * ret = new CCombinedArtifactInstance(Art);
  887. ret->createConstituents();
  888. return ret;
  889. }
  890. }
  891. CArtifactInstance * CArtifactInstance::createNewArtifactInstance(int aid)
  892. {
  893. return createNewArtifactInstance(VLC->arth->artifacts[aid]);
  894. }
  895. void CArtifactInstance::deserializationFix()
  896. {
  897. setType(artType);
  898. }
  899. int CArtifactInstance::getGivenSpellID() const
  900. {
  901. const Bonus * b = getBonusLocalFirst(Selector::type(Bonus::SPELL));
  902. if(!b)
  903. {
  904. tlog3 << "Warning: " << nodeName() << " doesn't bear any spell!\n";
  905. return -1;
  906. }
  907. return b->subtype;
  908. }
  909. bool CArtifactInstance::isPart(const CArtifactInstance *supposedPart) const
  910. {
  911. return supposedPart == this;
  912. }
  913. bool CCombinedArtifactInstance::canBePutAt(const CArtifactSet *artSet, int slot, bool assumeDestRemoved /*= false*/) const
  914. {
  915. bool canMainArtifactBePlaced = CArtifactInstance::canBePutAt(artSet, slot, assumeDestRemoved);
  916. if(!canMainArtifactBePlaced)
  917. return false; //no is no...
  918. if(slot >= GameConstants::BACKPACK_START)
  919. return true; //we can always remove combined art to the backapck
  920. assert(artType->constituents);
  921. std::vector<ConstituentInfo> constituentsToBePlaced = constituentsInfo; //we'll remove constituents from that list, as we find a suitable slot for them
  922. //it may be that we picked a combined artifact in hero screen (though technically it's still there) to move it
  923. //so we remove from the list all constituents that are already present on dst hero in the form of locks
  924. BOOST_FOREACH(const ConstituentInfo &constituent, constituentsInfo)
  925. {
  926. if(constituent.art == artSet->getArt(constituent.slot, false)) //no need to worry about locked constituent
  927. constituentsToBePlaced -= constituent;
  928. }
  929. //we iterate over all active slots and check if constituents fits them
  930. for (int i = 0; i < GameConstants::BACKPACK_START; i++)
  931. {
  932. for(std::vector<ConstituentInfo>::iterator art = constituentsToBePlaced.begin(); art != constituentsToBePlaced.end(); art++)
  933. {
  934. 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
  935. {
  936. constituentsToBePlaced.erase(art);
  937. break;
  938. }
  939. }
  940. }
  941. return constituentsToBePlaced.empty();
  942. }
  943. bool CCombinedArtifactInstance::canBeDisassembled() const
  944. {
  945. return true;
  946. }
  947. CCombinedArtifactInstance::CCombinedArtifactInstance(CArtifact *Art)
  948. : CArtifactInstance(Art) //TODO: seems unued, but need to be written
  949. {
  950. }
  951. CCombinedArtifactInstance::CCombinedArtifactInstance()
  952. {
  953. }
  954. void CCombinedArtifactInstance::createConstituents()
  955. {
  956. assert(artType);
  957. assert(artType->constituents);
  958. BOOST_FOREACH(ui32 a, *artType->constituents)
  959. {
  960. addAsConstituent(CArtifactInstance::createNewArtifactInstance(a), -1);
  961. }
  962. }
  963. void CCombinedArtifactInstance::addAsConstituent(CArtifactInstance *art, int slot)
  964. {
  965. assert(vstd::contains(*artType->constituents, art->artType->id));
  966. assert(art->getParentNodes().size() == 1 && art->getParentNodes().front() == art->artType);
  967. constituentsInfo.push_back(ConstituentInfo(art, slot));
  968. attachTo(art);
  969. }
  970. void CCombinedArtifactInstance::putAt(ArtifactLocation al)
  971. {
  972. if(al.slot >= GameConstants::BACKPACK_START)
  973. {
  974. CArtifactInstance::putAt(al);
  975. BOOST_FOREACH(ConstituentInfo &ci, constituentsInfo)
  976. ci.slot = -1;
  977. }
  978. else
  979. {
  980. CArtifactInstance *mainConstituent = figureMainConstituent(al); //it'll be replaced with combined artifact, not a lock
  981. CArtifactInstance::putAt(al); //puts combined art (this)
  982. BOOST_FOREACH(ConstituentInfo &ci, constituentsInfo)
  983. {
  984. if(ci.art != mainConstituent)
  985. {
  986. const ArtifactLocation suggestedPos(al.artHolder, ci.slot);
  987. const bool inActiveSlot = vstd::isbetween(ci.slot, 0, GameConstants::BACKPACK_START);
  988. const bool suggestedPosValid = ci.art->canBePutAt(suggestedPos);
  989. int pos = -1;
  990. if(inActiveSlot && suggestedPosValid) //there is a valid suggestion where to place lock
  991. pos = ci.slot;
  992. else
  993. ci.slot = pos = ci.art->firstAvailableSlot(al.getHolderArtSet());
  994. assert(pos < GameConstants::BACKPACK_START);
  995. al.getHolderArtSet()->setNewArtSlot(pos, ci.art, true); //sets as lock
  996. }
  997. else
  998. {
  999. ci.slot = -1;
  1000. }
  1001. }
  1002. }
  1003. }
  1004. void CCombinedArtifactInstance::removeFrom(ArtifactLocation al)
  1005. {
  1006. if(al.slot >= GameConstants::BACKPACK_START)
  1007. {
  1008. CArtifactInstance::removeFrom(al);
  1009. }
  1010. else
  1011. {
  1012. BOOST_FOREACH(ConstituentInfo &ci, constituentsInfo)
  1013. {
  1014. if(ci.slot >= 0)
  1015. {
  1016. al.getHolderArtSet()->eraseArtSlot(ci.slot);
  1017. ci.slot = -1;
  1018. }
  1019. else
  1020. {
  1021. //main constituent
  1022. CArtifactInstance::removeFrom(al);
  1023. }
  1024. }
  1025. }
  1026. }
  1027. CArtifactInstance * CCombinedArtifactInstance::figureMainConstituent(const ArtifactLocation al)
  1028. {
  1029. CArtifactInstance *mainConstituent = NULL; //it'll be replaced with combined artifact, not a lock
  1030. BOOST_FOREACH(ConstituentInfo &ci, constituentsInfo)
  1031. if(ci.slot == al.slot)
  1032. mainConstituent = ci.art;
  1033. if(!mainConstituent)
  1034. {
  1035. BOOST_FOREACH(ConstituentInfo &ci, constituentsInfo)
  1036. {
  1037. if(vstd::contains(ci.art->artType->possibleSlots[al.getHolderArtSet()->bearerType()], al.slot))
  1038. {
  1039. mainConstituent = ci.art;
  1040. }
  1041. }
  1042. }
  1043. return mainConstituent;
  1044. }
  1045. void CCombinedArtifactInstance::deserializationFix()
  1046. {
  1047. BOOST_FOREACH(ConstituentInfo &ci, constituentsInfo)
  1048. attachTo(ci.art);
  1049. }
  1050. bool CCombinedArtifactInstance::isPart(const CArtifactInstance *supposedPart) const
  1051. {
  1052. bool me = CArtifactInstance::isPart(supposedPart);
  1053. if(me)
  1054. return true;
  1055. //check for constituents
  1056. BOOST_FOREACH(const ConstituentInfo &constituent, constituentsInfo)
  1057. if(constituent.art == supposedPart)
  1058. return true;
  1059. return false;
  1060. }
  1061. CCombinedArtifactInstance::ConstituentInfo::ConstituentInfo(CArtifactInstance *Art /*= NULL*/, ui16 Slot /*= -1*/)
  1062. {
  1063. art = Art;
  1064. slot = Slot;
  1065. }
  1066. bool CCombinedArtifactInstance::ConstituentInfo::operator==(const ConstituentInfo &rhs) const
  1067. {
  1068. return art == rhs.art && slot == rhs.slot;
  1069. }
  1070. const CArtifactInstance* CArtifactSet::getArt(ui16 pos, bool excludeLocked /*= true*/) const
  1071. {
  1072. if(const ArtSlotInfo *si = getSlot(pos))
  1073. {
  1074. if(si->artifact && (!excludeLocked || !si->locked))
  1075. return si->artifact;
  1076. }
  1077. return NULL;
  1078. }
  1079. CArtifactInstance* CArtifactSet::getArt(ui16 pos, bool excludeLocked /*= true*/)
  1080. {
  1081. return const_cast<CArtifactInstance*>((const_cast<const CArtifactSet*>(this))->getArt(pos, excludeLocked));
  1082. }
  1083. si32 CArtifactSet::getArtPos(int aid, bool onlyWorn /*= true*/) const
  1084. {
  1085. for(std::map<ui16, ArtSlotInfo>::const_iterator i = artifactsWorn.begin(); i != artifactsWorn.end(); i++)
  1086. if(i->second.artifact->artType->id == aid)
  1087. return i->first;
  1088. if(onlyWorn)
  1089. return -1;
  1090. for(int i = 0; i < artifactsInBackpack.size(); i++)
  1091. if(artifactsInBackpack[i].artifact->artType->id == aid)
  1092. return GameConstants::BACKPACK_START + i;
  1093. return -1;
  1094. }
  1095. si32 CArtifactSet::getArtPos(const CArtifactInstance *art) const
  1096. {
  1097. for(std::map<ui16, ArtSlotInfo>::const_iterator i = artifactsWorn.begin(); i != artifactsWorn.end(); i++)
  1098. if(i->second.artifact == art)
  1099. return i->first;
  1100. for(int i = 0; i < artifactsInBackpack.size(); i++)
  1101. if(artifactsInBackpack[i].artifact == art)
  1102. return GameConstants::BACKPACK_START + i;
  1103. return -1;
  1104. }
  1105. const CArtifactInstance * CArtifactSet::getArtByInstanceId( TArtifactInstanceID artInstId ) const
  1106. {
  1107. for(std::map<ui16, ArtSlotInfo>::const_iterator i = artifactsWorn.begin(); i != artifactsWorn.end(); i++)
  1108. if(i->second.artifact->id == artInstId)
  1109. return i->second.artifact;
  1110. for(int i = 0; i < artifactsInBackpack.size(); i++)
  1111. if(artifactsInBackpack[i].artifact->id == artInstId)
  1112. return artifactsInBackpack[i].artifact;
  1113. return NULL;
  1114. }
  1115. bool CArtifactSet::hasArt(ui32 aid, bool onlyWorn /*= false*/) const
  1116. {
  1117. return getArtPos(aid, onlyWorn) != -1;
  1118. }
  1119. const ArtSlotInfo * CArtifactSet::getSlot(ui16 pos) const
  1120. {
  1121. if(vstd::contains(artifactsWorn, pos))
  1122. return &artifactsWorn[pos];
  1123. if(pos >= ArtifactPosition::AFTER_LAST )
  1124. {
  1125. int backpackPos = (int)pos - GameConstants::BACKPACK_START;
  1126. if(backpackPos < 0 || backpackPos >= artifactsInBackpack.size())
  1127. return NULL;
  1128. else
  1129. return &artifactsInBackpack[backpackPos];
  1130. }
  1131. return NULL;
  1132. }
  1133. bool CArtifactSet::isPositionFree(ui16 pos, bool onlyLockCheck /*= false*/) const
  1134. {
  1135. if(const ArtSlotInfo *s = getSlot(pos))
  1136. return (onlyLockCheck || !s->artifact) && !s->locked;
  1137. return true; //no slot means not used
  1138. }
  1139. si32 CArtifactSet::getArtTypeId(ui16 pos) const
  1140. {
  1141. const CArtifactInstance * const a = getArt(pos);
  1142. if(!a)
  1143. {
  1144. tlog2 << (dynamic_cast<const CGHeroInstance*>(this))->name << " has no artifact at " << pos << " (getArtTypeId)\n";
  1145. return -1;
  1146. }
  1147. return a->artType->id;
  1148. }
  1149. CArtifactSet::~CArtifactSet()
  1150. {
  1151. }
  1152. ArtSlotInfo & CArtifactSet::retreiveNewArtSlot(ui16 slot)
  1153. {
  1154. assert(!vstd::contains(artifactsWorn, slot));
  1155. ArtSlotInfo &ret = slot < GameConstants::BACKPACK_START
  1156. ? artifactsWorn[slot]
  1157. : *artifactsInBackpack.insert(artifactsInBackpack.begin() + (slot - GameConstants::BACKPACK_START), ArtSlotInfo());
  1158. return ret;
  1159. }
  1160. void CArtifactSet::setNewArtSlot(ui16 slot, CArtifactInstance *art, bool locked)
  1161. {
  1162. ArtSlotInfo &asi = retreiveNewArtSlot(slot);
  1163. asi.artifact = art;
  1164. asi.locked = locked;
  1165. }
  1166. void CArtifactSet::eraseArtSlot(ui16 slot)
  1167. {
  1168. if(slot < GameConstants::BACKPACK_START)
  1169. {
  1170. artifactsWorn.erase(slot);
  1171. }
  1172. else
  1173. {
  1174. slot -= GameConstants::BACKPACK_START;
  1175. artifactsInBackpack.erase(artifactsInBackpack.begin() + slot);
  1176. }
  1177. }
  1178. void CArtifactSet::artDeserializationFix(CBonusSystemNode *node)
  1179. {
  1180. for(bmap<ui16, ArtSlotInfo>::iterator i = artifactsWorn.begin(); i != artifactsWorn.end(); i++)
  1181. if(i->second.artifact && !i->second.locked)
  1182. node->attachTo(i->second.artifact);
  1183. }