CArtHandler.cpp 38 KB

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