CArtHandler.cpp 36 KB

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