CArtHandler.cpp 37 KB

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