CArtHandler.cpp 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399
  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 "StringConstants.h"
  20. #include "CRandomGenerator.h"
  21. #include "mapObjects/CObjectClassesHandler.h"
  22. #include "mapping/CMap.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, normalizeIdentifier(scope, "core", 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", name, 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, normalizeIdentifier(scope, "core", 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", name, 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(artifacts.size(), 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. si32 CArtHandler::decodeArfifact(const std::string& identifier)
  652. {
  653. auto rawId = VLC->modh->identifiers.getIdentifier("core", "artifact", identifier);
  654. if(rawId)
  655. return rawId.get();
  656. else
  657. return -1;
  658. }
  659. std::string CArtHandler::encodeArtifact(const si32 index)
  660. {
  661. return VLC->arth->artifacts[index]->identifier;
  662. }
  663. CArtifactInstance::CArtifactInstance()
  664. {
  665. init();
  666. }
  667. CArtifactInstance::CArtifactInstance( CArtifact *Art)
  668. {
  669. init();
  670. setType(Art);
  671. }
  672. void CArtifactInstance::setType( CArtifact *Art )
  673. {
  674. artType = Art;
  675. attachTo(Art);
  676. }
  677. std::string CArtifactInstance::nodeName() const
  678. {
  679. return "Artifact instance of " + (artType ? artType->Name() : std::string("uninitialized")) + " type";
  680. }
  681. CArtifactInstance * CArtifactInstance::createScroll( const CSpell *s)
  682. {
  683. return createScroll(s->id);
  684. }
  685. CArtifactInstance *CArtifactInstance::createScroll(SpellID sid)
  686. {
  687. auto ret = new CArtifactInstance(VLC->arth->artifacts[ArtifactID::SPELL_SCROLL]);
  688. auto b = new Bonus(Bonus::PERMANENT, Bonus::SPELL, Bonus::ARTIFACT_INSTANCE, -1, ArtifactID::SPELL_SCROLL, sid);
  689. ret->addNewBonus(b);
  690. return ret;
  691. }
  692. void CArtifactInstance::init()
  693. {
  694. id = ArtifactInstanceID();
  695. id = static_cast<ArtifactInstanceID>(ArtifactID::NONE); //to be randomized
  696. setNodeType(ARTIFACT_INSTANCE);
  697. }
  698. std::string CArtifactInstance::getEffectiveDescription(
  699. const CGHeroInstance *hero) const
  700. {
  701. std::string text = artType->Description();
  702. if (!vstd::contains(text, '{'))
  703. text = '{' + artType->Name() + "}\n\n" + text; //workaround for new artifacts with single name, turns it to H3-style
  704. if(artType->id == ArtifactID::SPELL_SCROLL)
  705. {
  706. // 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.
  707. // so we want to replace text in [...] with a spell name
  708. // however other language versions don't have name placeholder at all, so we have to be careful
  709. int spellID = getGivenSpellID();
  710. size_t nameStart = text.find_first_of('[');
  711. size_t nameEnd = text.find_first_of(']', nameStart);
  712. if(spellID >= 0)
  713. {
  714. if(nameStart != std::string::npos && nameEnd != std::string::npos)
  715. text = text.replace(nameStart, nameEnd - nameStart + 1, VLC->spellh->objects[spellID]->name);
  716. }
  717. }
  718. else if (hero && artType->constituentOf.size()) //display info about set
  719. {
  720. std::string artList;
  721. auto combinedArt = artType->constituentOf[0];
  722. text += "\n\n";
  723. text += "{" + combinedArt->Name() + "}";
  724. int wornArtifacts = 0;
  725. for (auto a : *combinedArt->constituents) //TODO: can the artifact be a part of more than one set?
  726. {
  727. artList += "\n" + a->Name();
  728. if (hero->hasArt(a->id, true))
  729. wornArtifacts++;
  730. }
  731. text += " (" + boost::str(boost::format("%d") % wornArtifacts) + " / " +
  732. boost::str(boost::format("%d") % combinedArt->constituents->size()) + ")" + artList;
  733. //TODO: fancy colors and fonts for this text
  734. }
  735. return text;
  736. }
  737. ArtifactPosition CArtifactInstance::firstAvailableSlot(const CArtifactSet *h) const
  738. {
  739. for(auto slot : artType->possibleSlots.at(h->bearerType()))
  740. {
  741. if(canBePutAt(h, slot)) //if(artType->fitsAt(h->artifWorn, slot))
  742. {
  743. //we've found a free suitable slot.
  744. return slot;
  745. }
  746. }
  747. //if haven't find proper slot, use backpack
  748. return firstBackpackSlot(h);
  749. }
  750. ArtifactPosition CArtifactInstance::firstBackpackSlot(const CArtifactSet *h) const
  751. {
  752. if(!artType->isBig()) //discard big artifact
  753. return ArtifactPosition(
  754. GameConstants::BACKPACK_START + h->artifactsInBackpack.size());
  755. return ArtifactPosition::PRE_FIRST;
  756. }
  757. bool CArtifactInstance::canBePutAt(const ArtifactLocation & al, bool assumeDestRemoved /*= false*/) const
  758. {
  759. return canBePutAt(al.getHolderArtSet(), al.slot, assumeDestRemoved);
  760. }
  761. bool CArtifactInstance::canBePutAt(const CArtifactSet *artSet, ArtifactPosition slot, bool assumeDestRemoved /*= false*/) const
  762. {
  763. if(slot >= GameConstants::BACKPACK_START)
  764. {
  765. if(artType->isBig())
  766. return false;
  767. //TODO backpack limit
  768. return true;
  769. }
  770. auto possibleSlots = artType->possibleSlots.find(artSet->bearerType());
  771. if(possibleSlots == artType->possibleSlots.end())
  772. {
  773. logGlobal->warnStream() << "Warning: artifact " << artType->Name() << " doesn't have defined allowed slots for bearer of type "
  774. << artSet->bearerType();
  775. return false;
  776. }
  777. if(!vstd::contains(possibleSlots->second, slot))
  778. return false;
  779. return artSet->isPositionFree(slot, assumeDestRemoved);
  780. }
  781. void CArtifactInstance::putAt(ArtifactLocation al)
  782. {
  783. assert(canBePutAt(al));
  784. al.getHolderArtSet()->setNewArtSlot(al.slot, this, false);
  785. if(al.slot < GameConstants::BACKPACK_START)
  786. al.getHolderNode()->attachTo(this);
  787. }
  788. void CArtifactInstance::removeFrom(ArtifactLocation al)
  789. {
  790. assert(al.getHolderArtSet()->getArt(al.slot) == this);
  791. al.getHolderArtSet()->eraseArtSlot(al.slot);
  792. if(al.slot < GameConstants::BACKPACK_START)
  793. al.getHolderNode()->detachFrom(this);
  794. //TODO delete me?
  795. }
  796. bool CArtifactInstance::canBeDisassembled() const
  797. {
  798. return bool(artType->constituents);
  799. }
  800. std::vector<const CArtifact *> CArtifactInstance::assemblyPossibilities(const CArtifactSet *h) const
  801. {
  802. std::vector<const CArtifact *> ret;
  803. if(artType->constituents) //combined artifact already: no combining of combined artifacts... for now.
  804. return ret;
  805. for(const CArtifact * artifact : artType->constituentOf)
  806. {
  807. assert(artifact->constituents);
  808. bool possible = true;
  809. for(const CArtifact * constituent : *artifact->constituents) //check if all constituents are available
  810. {
  811. if(!h->hasArt(constituent->id, true)) //constituent must be equipped
  812. {
  813. possible = false;
  814. break;
  815. }
  816. }
  817. if(possible)
  818. ret.push_back(artifact);
  819. }
  820. return ret;
  821. }
  822. void CArtifactInstance::move(ArtifactLocation src, ArtifactLocation dst)
  823. {
  824. removeFrom(src);
  825. putAt(dst);
  826. }
  827. CArtifactInstance * CArtifactInstance::createNewArtifactInstance(CArtifact *Art)
  828. {
  829. if(!Art->constituents)
  830. {
  831. auto ret = new CArtifactInstance(Art);
  832. if (dynamic_cast<CGrowingArtifact *>(Art))
  833. {
  834. auto bonus = new Bonus;
  835. bonus->type = Bonus::LEVEL_COUNTER;
  836. bonus->val = 0;
  837. ret->addNewBonus (bonus);
  838. }
  839. return ret;
  840. }
  841. else
  842. {
  843. auto ret = new CCombinedArtifactInstance(Art);
  844. ret->createConstituents();
  845. return ret;
  846. }
  847. }
  848. CArtifactInstance * CArtifactInstance::createNewArtifactInstance(int aid)
  849. {
  850. return createNewArtifactInstance(VLC->arth->artifacts[aid]);
  851. }
  852. CArtifactInstance * CArtifactInstance::createArtifact(CMap * map, int aid, int spellID)
  853. {
  854. CArtifactInstance * a = nullptr;
  855. if(aid >= 0)
  856. {
  857. if(spellID < 0)
  858. {
  859. a = CArtifactInstance::createNewArtifactInstance(aid);
  860. }
  861. else
  862. {
  863. a = CArtifactInstance::createScroll(SpellID(spellID).toSpell());
  864. }
  865. }
  866. else //FIXME: create combined artifact instance for random combined artifacts, just in case
  867. {
  868. a = new CArtifactInstance(); //random, empty
  869. }
  870. map->addNewArtifactInstance(a);
  871. //TODO make it nicer
  872. if(a->artType && (!!a->artType->constituents))
  873. {
  874. CCombinedArtifactInstance * comb = dynamic_cast<CCombinedArtifactInstance *>(a);
  875. for(CCombinedArtifactInstance::ConstituentInfo & ci : comb->constituentsInfo)
  876. {
  877. map->addNewArtifactInstance(ci.art);
  878. }
  879. }
  880. return a;
  881. }
  882. void CArtifactInstance::deserializationFix()
  883. {
  884. setType(artType);
  885. }
  886. SpellID CArtifactInstance::getGivenSpellID() const
  887. {
  888. const Bonus * b = getBonusLocalFirst(Selector::type(Bonus::SPELL));
  889. if(!b)
  890. {
  891. logGlobal->warnStream() << "Warning: " << nodeName() << " doesn't bear any spell!";
  892. return SpellID::NONE;
  893. }
  894. return SpellID(b->subtype);
  895. }
  896. bool CArtifactInstance::isPart(const CArtifactInstance *supposedPart) const
  897. {
  898. return supposedPart == this;
  899. }
  900. bool CCombinedArtifactInstance::canBePutAt(const CArtifactSet *artSet, ArtifactPosition slot, bool assumeDestRemoved /*= false*/) const
  901. {
  902. bool canMainArtifactBePlaced = CArtifactInstance::canBePutAt(artSet, slot, assumeDestRemoved);
  903. if(!canMainArtifactBePlaced)
  904. return false; //no is no...
  905. if(slot >= GameConstants::BACKPACK_START)
  906. return true; //we can always remove combined art to the backapck
  907. assert(artType->constituents);
  908. std::vector<ConstituentInfo> constituentsToBePlaced = constituentsInfo; //we'll remove constituents from that list, as we find a suitable slot for them
  909. //it may be that we picked a combined artifact in hero screen (though technically it's still there) to move it
  910. //so we remove from the list all constituents that are already present on dst hero in the form of locks
  911. for(const ConstituentInfo &constituent : constituentsInfo)
  912. {
  913. if(constituent.art == artSet->getArt(constituent.slot, false)) //no need to worry about locked constituent
  914. constituentsToBePlaced -= constituent;
  915. }
  916. //we iterate over all active slots and check if constituents fits them
  917. for (int i = 0; i < GameConstants::BACKPACK_START; i++)
  918. {
  919. for(auto art = constituentsToBePlaced.begin(); art != constituentsToBePlaced.end(); art++)
  920. {
  921. 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
  922. {
  923. constituentsToBePlaced.erase(art);
  924. break;
  925. }
  926. }
  927. }
  928. return constituentsToBePlaced.empty();
  929. }
  930. bool CCombinedArtifactInstance::canBeDisassembled() const
  931. {
  932. return true;
  933. }
  934. CCombinedArtifactInstance::CCombinedArtifactInstance(CArtifact *Art)
  935. : CArtifactInstance(Art) //TODO: seems unued, but need to be written
  936. {
  937. }
  938. CCombinedArtifactInstance::CCombinedArtifactInstance()
  939. {
  940. }
  941. void CCombinedArtifactInstance::createConstituents()
  942. {
  943. assert(artType);
  944. assert(artType->constituents);
  945. for(const CArtifact * art : *artType->constituents)
  946. {
  947. addAsConstituent(CArtifactInstance::createNewArtifactInstance(art->id), ArtifactPosition::PRE_FIRST);
  948. }
  949. }
  950. void CCombinedArtifactInstance::addAsConstituent(CArtifactInstance *art, ArtifactPosition slot)
  951. {
  952. assert(vstd::contains(*artType->constituents, art->artType.get()));
  953. assert(art->getParentNodes().size() == 1 && art->getParentNodes().front() == art->artType);
  954. constituentsInfo.push_back(ConstituentInfo(art, slot));
  955. attachTo(art);
  956. }
  957. void CCombinedArtifactInstance::putAt(ArtifactLocation al)
  958. {
  959. if(al.slot >= GameConstants::BACKPACK_START)
  960. {
  961. CArtifactInstance::putAt(al);
  962. for(ConstituentInfo &ci : constituentsInfo)
  963. ci.slot = ArtifactPosition::PRE_FIRST;
  964. }
  965. else
  966. {
  967. CArtifactInstance *mainConstituent = figureMainConstituent(al); //it'll be replaced with combined artifact, not a lock
  968. CArtifactInstance::putAt(al); //puts combined art (this)
  969. for(ConstituentInfo &ci : constituentsInfo)
  970. {
  971. if(ci.art != mainConstituent)
  972. {
  973. const ArtifactLocation suggestedPos(al.artHolder, ci.slot);
  974. const bool inActiveSlot = vstd::isbetween(ci.slot, 0, GameConstants::BACKPACK_START);
  975. const bool suggestedPosValid = ci.art->canBePutAt(suggestedPos);
  976. ArtifactPosition pos = ArtifactPosition::PRE_FIRST;
  977. if(inActiveSlot && suggestedPosValid) //there is a valid suggestion where to place lock
  978. pos = ci.slot;
  979. else
  980. ci.slot = pos = ci.art->firstAvailableSlot(al.getHolderArtSet());
  981. assert(pos < GameConstants::BACKPACK_START);
  982. al.getHolderArtSet()->setNewArtSlot(pos, ci.art, true); //sets as lock
  983. }
  984. else
  985. {
  986. ci.slot = ArtifactPosition::PRE_FIRST;
  987. }
  988. }
  989. }
  990. }
  991. void CCombinedArtifactInstance::removeFrom(ArtifactLocation al)
  992. {
  993. if(al.slot >= GameConstants::BACKPACK_START)
  994. {
  995. CArtifactInstance::removeFrom(al);
  996. }
  997. else
  998. {
  999. for(ConstituentInfo &ci : constituentsInfo)
  1000. {
  1001. if(ci.slot >= 0)
  1002. {
  1003. al.getHolderArtSet()->eraseArtSlot(ci.slot);
  1004. ci.slot = ArtifactPosition::PRE_FIRST;
  1005. }
  1006. else
  1007. {
  1008. //main constituent
  1009. CArtifactInstance::removeFrom(al);
  1010. }
  1011. }
  1012. }
  1013. }
  1014. CArtifactInstance * CCombinedArtifactInstance::figureMainConstituent(const ArtifactLocation al)
  1015. {
  1016. CArtifactInstance *mainConstituent = nullptr; //it'll be replaced with combined artifact, not a lock
  1017. for(ConstituentInfo &ci : constituentsInfo)
  1018. if(ci.slot == al.slot)
  1019. mainConstituent = ci.art;
  1020. if(!mainConstituent)
  1021. {
  1022. for(ConstituentInfo &ci : constituentsInfo)
  1023. {
  1024. if(vstd::contains(ci.art->artType->possibleSlots[al.getHolderArtSet()->bearerType()], al.slot))
  1025. {
  1026. mainConstituent = ci.art;
  1027. }
  1028. }
  1029. }
  1030. return mainConstituent;
  1031. }
  1032. void CCombinedArtifactInstance::deserializationFix()
  1033. {
  1034. for(ConstituentInfo &ci : constituentsInfo)
  1035. attachTo(ci.art);
  1036. }
  1037. bool CCombinedArtifactInstance::isPart(const CArtifactInstance *supposedPart) const
  1038. {
  1039. bool me = CArtifactInstance::isPart(supposedPart);
  1040. if(me)
  1041. return true;
  1042. //check for constituents
  1043. for(const ConstituentInfo &constituent : constituentsInfo)
  1044. if(constituent.art == supposedPart)
  1045. return true;
  1046. return false;
  1047. }
  1048. CCombinedArtifactInstance::ConstituentInfo::ConstituentInfo(CArtifactInstance *Art /*= nullptr*/, ArtifactPosition Slot /*= -1*/)
  1049. {
  1050. art = Art;
  1051. slot = Slot;
  1052. }
  1053. bool CCombinedArtifactInstance::ConstituentInfo::operator==(const ConstituentInfo &rhs) const
  1054. {
  1055. return art == rhs.art && slot == rhs.slot;
  1056. }
  1057. const CArtifactInstance* CArtifactSet::getArt(ArtifactPosition pos, bool excludeLocked /*= true*/) const
  1058. {
  1059. if(const ArtSlotInfo *si = getSlot(pos))
  1060. {
  1061. if(si->artifact && (!excludeLocked || !si->locked))
  1062. return si->artifact;
  1063. }
  1064. return nullptr;
  1065. }
  1066. CArtifactInstance* CArtifactSet::getArt(ArtifactPosition pos, bool excludeLocked /*= true*/)
  1067. {
  1068. return const_cast<CArtifactInstance*>((const_cast<const CArtifactSet*>(this))->getArt(pos, excludeLocked));
  1069. }
  1070. ArtifactPosition CArtifactSet::getArtPos(int aid, bool onlyWorn /*= true*/) const
  1071. {
  1072. for(auto i = artifactsWorn.cbegin(); i != artifactsWorn.cend(); i++)
  1073. if(i->second.artifact->artType->id == aid)
  1074. return i->first;
  1075. if(onlyWorn)
  1076. return ArtifactPosition::PRE_FIRST;
  1077. for(int i = 0; i < artifactsInBackpack.size(); i++)
  1078. if(artifactsInBackpack[i].artifact->artType->id == aid)
  1079. return ArtifactPosition(GameConstants::BACKPACK_START + i);
  1080. return ArtifactPosition::PRE_FIRST;
  1081. }
  1082. ArtifactPosition CArtifactSet::getArtPos(const CArtifactInstance *art) const
  1083. {
  1084. for(auto i : artifactsWorn)
  1085. if(i.second.artifact == art)
  1086. return i.first;
  1087. for(int i = 0; i < artifactsInBackpack.size(); i++)
  1088. if(artifactsInBackpack[i].artifact == art)
  1089. return ArtifactPosition(GameConstants::BACKPACK_START + i);
  1090. return ArtifactPosition::PRE_FIRST;
  1091. }
  1092. const CArtifactInstance * CArtifactSet::getArtByInstanceId( ArtifactInstanceID artInstId ) const
  1093. {
  1094. for(auto i : artifactsWorn)
  1095. if(i.second.artifact->id == artInstId)
  1096. return i.second.artifact;
  1097. for(auto i : artifactsInBackpack)
  1098. if(i.artifact->id == artInstId)
  1099. return i.artifact;
  1100. return nullptr;
  1101. }
  1102. bool CArtifactSet::hasArt(ui32 aid, bool onlyWorn /*= false*/,
  1103. bool searchBackpackAssemblies /*= false*/) const
  1104. {
  1105. return getArtPos(aid, onlyWorn) != ArtifactPosition::PRE_FIRST ||
  1106. (searchBackpackAssemblies && getHiddenArt(aid));
  1107. }
  1108. std::pair<const CCombinedArtifactInstance *, const CArtifactInstance *>
  1109. CArtifactSet::searchForConstituent(int aid) const
  1110. {
  1111. for(auto & slot : artifactsInBackpack)
  1112. {
  1113. auto art = slot.artifact;
  1114. if(art->canBeDisassembled())
  1115. {
  1116. auto ass = static_cast<CCombinedArtifactInstance *>(art.get());
  1117. for(auto& ci : ass->constituentsInfo)
  1118. {
  1119. if(ci.art->artType->id == aid)
  1120. {
  1121. return {ass, ci.art};
  1122. }
  1123. }
  1124. }
  1125. }
  1126. return {nullptr, nullptr};
  1127. }
  1128. const CArtifactInstance *CArtifactSet::getHiddenArt(int aid) const
  1129. {
  1130. return searchForConstituent(aid).second;
  1131. }
  1132. const CCombinedArtifactInstance *CArtifactSet::getAssemblyByConstituent(int aid) const
  1133. {
  1134. return searchForConstituent(aid).first;
  1135. }
  1136. const ArtSlotInfo * CArtifactSet::getSlot(ArtifactPosition pos) const
  1137. {
  1138. if(vstd::contains(artifactsWorn, pos))
  1139. return &artifactsWorn.at(pos);
  1140. if(pos >= ArtifactPosition::AFTER_LAST )
  1141. {
  1142. int backpackPos = (int)pos - GameConstants::BACKPACK_START;
  1143. if(backpackPos < 0 || backpackPos >= artifactsInBackpack.size())
  1144. return nullptr;
  1145. else
  1146. return &artifactsInBackpack[backpackPos];
  1147. }
  1148. return nullptr;
  1149. }
  1150. bool CArtifactSet::isPositionFree(ArtifactPosition pos, bool onlyLockCheck /*= false*/) const
  1151. {
  1152. if(const ArtSlotInfo *s = getSlot(pos))
  1153. return (onlyLockCheck || !s->artifact) && !s->locked;
  1154. return true; //no slot means not used
  1155. }
  1156. si32 CArtifactSet::getArtTypeId(ArtifactPosition pos) const
  1157. {
  1158. const CArtifactInstance * const a = getArt(pos);
  1159. if(!a)
  1160. {
  1161. logGlobal->warnStream() << (dynamic_cast<const CGHeroInstance*>(this))->name << " has no artifact at " << pos << " (getArtTypeId)";
  1162. return -1;
  1163. }
  1164. return a->artType->id;
  1165. }
  1166. CArtifactSet::~CArtifactSet()
  1167. {
  1168. }
  1169. ArtSlotInfo & CArtifactSet::retreiveNewArtSlot(ArtifactPosition slot)
  1170. {
  1171. assert(!vstd::contains(artifactsWorn, slot));
  1172. ArtSlotInfo &ret = slot < GameConstants::BACKPACK_START
  1173. ? artifactsWorn[slot]
  1174. : *artifactsInBackpack.insert(artifactsInBackpack.begin() + (slot - GameConstants::BACKPACK_START), ArtSlotInfo());
  1175. return ret;
  1176. }
  1177. void CArtifactSet::setNewArtSlot(ArtifactPosition slot, CArtifactInstance *art, bool locked)
  1178. {
  1179. ArtSlotInfo &asi = retreiveNewArtSlot(slot);
  1180. asi.artifact = art;
  1181. asi.locked = locked;
  1182. }
  1183. void CArtifactSet::eraseArtSlot(ArtifactPosition slot)
  1184. {
  1185. if(slot < GameConstants::BACKPACK_START)
  1186. {
  1187. artifactsWorn.erase(slot);
  1188. }
  1189. else
  1190. {
  1191. slot = ArtifactPosition(slot - GameConstants::BACKPACK_START);
  1192. artifactsInBackpack.erase(artifactsInBackpack.begin() + slot);
  1193. }
  1194. }
  1195. void CArtifactSet::artDeserializationFix(CBonusSystemNode *node)
  1196. {
  1197. for(auto & elem : artifactsWorn)
  1198. if(elem.second.artifact && !elem.second.locked)
  1199. node->attachTo(elem.second.artifact);
  1200. }
  1201. void CArtifactSet::writeJson(JsonNode& json) const
  1202. {
  1203. }
  1204. void CArtifactSet::readJson(const JsonNode& json)
  1205. {
  1206. }