CArtHandler.cpp 37 KB

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