CArtHandler.cpp 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401
  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(const std::shared_ptr<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. auto b = std::make_shared<Bonus>();
  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(std::make_shared<Bonus>(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(std::make_shared<Bonus>(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. auto bonus = JsonUtils::parseBonus (b["bonus"]);
  380. art->bonusesPerLevel.push_back (std::pair <ui16, Bonus> (b["level"].Float(), *bonus));
  381. }
  382. for (auto b : node["growing"]["thresholdBonuses"].Vector())
  383. {
  384. auto bonus = JsonUtils::parseBonus (b["bonus"]);
  385. art->thresholdBonuses.push_back (std::pair <ui16, Bonus> (b["level"].Float(), *bonus));
  386. }
  387. }
  388. //TODO: use bimap
  389. ArtifactID CArtHandler::creatureToMachineID(CreatureID id)
  390. {
  391. switch (id)
  392. {
  393. case CreatureID::CATAPULT: //Catapult
  394. return ArtifactID::CATAPULT;
  395. break;
  396. case CreatureID::BALLISTA: //Ballista
  397. return ArtifactID::BALLISTA;
  398. break;
  399. case CreatureID::FIRST_AID_TENT: //First Aid tent
  400. return ArtifactID::FIRST_AID_TENT;
  401. break;
  402. case CreatureID::AMMO_CART: //Ammo cart
  403. return ArtifactID::AMMO_CART;
  404. break;
  405. }
  406. return ArtifactID::NONE; //this creature is not artifact
  407. }
  408. CreatureID CArtHandler::machineIDToCreature(ArtifactID id)
  409. {
  410. switch (id)
  411. {
  412. case ArtifactID::CATAPULT:
  413. return CreatureID::CATAPULT;
  414. break;
  415. case ArtifactID::BALLISTA:
  416. return CreatureID::BALLISTA;
  417. break;
  418. case ArtifactID::FIRST_AID_TENT:
  419. return CreatureID::FIRST_AID_TENT;
  420. break;
  421. case ArtifactID::AMMO_CART:
  422. return CreatureID::AMMO_CART;
  423. break;
  424. }
  425. return CreatureID::NONE; //this artifact is not a creature
  426. }
  427. ArtifactID CArtHandler::pickRandomArtifact(CRandomGenerator & rand, int flags, std::function<bool(ArtifactID)> accepts)
  428. {
  429. auto getAllowedArts = [&](std::vector<ConstTransitivePtr<CArtifact> > &out, std::vector<CArtifact*> *arts, CArtifact::EartClass flag)
  430. {
  431. if (arts->empty()) //restock available arts
  432. fillList(*arts, flag);
  433. for (auto & arts_i : *arts)
  434. {
  435. if (accepts(arts_i->id))
  436. {
  437. CArtifact *art = arts_i;
  438. out.push_back(art);
  439. }
  440. }
  441. };
  442. auto getAllowed = [&](std::vector<ConstTransitivePtr<CArtifact> > &out)
  443. {
  444. if (flags & CArtifact::ART_TREASURE)
  445. getAllowedArts (out, &treasures, CArtifact::ART_TREASURE);
  446. if (flags & CArtifact::ART_MINOR)
  447. getAllowedArts (out, &minors, CArtifact::ART_MINOR);
  448. if (flags & CArtifact::ART_MAJOR)
  449. getAllowedArts (out, &majors, CArtifact::ART_MAJOR);
  450. if (flags & CArtifact::ART_RELIC)
  451. getAllowedArts (out, &relics, CArtifact::ART_RELIC);
  452. if (!out.size()) //no artifact of specified rarity, we need to take another one
  453. {
  454. getAllowedArts (out, &treasures, CArtifact::ART_TREASURE);
  455. getAllowedArts (out, &minors, CArtifact::ART_MINOR);
  456. getAllowedArts (out, &majors, CArtifact::ART_MAJOR);
  457. getAllowedArts (out, &relics, CArtifact::ART_RELIC);
  458. }
  459. if (!out.size()) //no arts are available at all
  460. {
  461. out.resize (64);
  462. std::fill_n (out.begin(), 64, artifacts[2]); //Give Grail - this can't be banned (hopefully)
  463. }
  464. };
  465. std::vector<ConstTransitivePtr<CArtifact> > out;
  466. getAllowed(out);
  467. ArtifactID artID = (*RandomGeneratorUtil::nextItem(out, rand))->id;
  468. erasePickedArt(artID);
  469. return artID;
  470. }
  471. ArtifactID CArtHandler::pickRandomArtifact(CRandomGenerator & rand, std::function<bool(ArtifactID)> accepts)
  472. {
  473. return pickRandomArtifact(rand, 0xff, accepts);
  474. }
  475. ArtifactID CArtHandler::pickRandomArtifact(CRandomGenerator & rand, int flags)
  476. {
  477. return pickRandomArtifact(rand, flags, [](ArtifactID){ return true;});
  478. }
  479. std::shared_ptr<Bonus> createBonus(Bonus::BonusType type, int val, int subtype, Bonus::ValueType valType, std::shared_ptr<ILimiter> limiter = std::shared_ptr<ILimiter>(), int additionalInfo = 0)
  480. {
  481. auto added = std::make_shared<Bonus>(Bonus::PERMANENT,type,Bonus::ARTIFACT,val,-1,subtype);
  482. added->additionalInfo = additionalInfo;
  483. added->valType = valType;
  484. added->limiter = limiter;
  485. return added;
  486. }
  487. std::shared_ptr<Bonus> createBonus(Bonus::BonusType type, int val, int subtype, std::shared_ptr<IPropagator> propagator = std::shared_ptr<IPropagator>(), int additionalInfo = 0)
  488. {
  489. auto added = std::make_shared<Bonus>(Bonus::PERMANENT,type,Bonus::ARTIFACT,val,-1,subtype);
  490. added->additionalInfo = additionalInfo;
  491. added->valType = Bonus::BASE_NUMBER;
  492. added->propagator = propagator;
  493. return added;
  494. }
  495. void CArtHandler::giveArtBonus( ArtifactID aid, Bonus::BonusType type, int val, int subtype, Bonus::ValueType valType, std::shared_ptr<ILimiter> limiter, int additionalInfo)
  496. {
  497. giveArtBonus(aid, createBonus(type, val, subtype, valType, limiter, additionalInfo));
  498. }
  499. void CArtHandler::giveArtBonus(ArtifactID aid, Bonus::BonusType type, int val, int subtype, std::shared_ptr<IPropagator> propagator /*= nullptr*/, int additionalInfo)
  500. {
  501. giveArtBonus(aid, createBonus(type, val, subtype, propagator, additionalInfo));
  502. }
  503. void CArtHandler::giveArtBonus(ArtifactID aid, std::shared_ptr<Bonus> bonus)
  504. {
  505. bonus->sid = aid;
  506. if(bonus->subtype == Bonus::MORALE || bonus->type == Bonus::LUCK)
  507. bonus->description = artifacts[aid]->Name() + (bonus->val > 0 ? " +" : " ") + boost::lexical_cast<std::string>(bonus->val);
  508. else
  509. bonus->description = artifacts[aid]->Name();
  510. artifacts[aid]->addNewBonus(bonus);
  511. }
  512. void CArtHandler::makeItCreatureArt (CArtifact * a, bool onlyCreature /*=true*/)
  513. {
  514. if (onlyCreature)
  515. {
  516. a->possibleSlots[ArtBearer::HERO].clear();
  517. a->possibleSlots[ArtBearer::COMMANDER].clear();
  518. }
  519. a->possibleSlots[ArtBearer::CREATURE].push_back(ArtifactPosition::CREATURE_SLOT);
  520. }
  521. void CArtHandler::makeItCreatureArt (ArtifactID aid, bool onlyCreature /*=true*/)
  522. {
  523. CArtifact *a = artifacts[aid];
  524. makeItCreatureArt (a, onlyCreature);
  525. }
  526. void CArtHandler::makeItCommanderArt (CArtifact * a, bool onlyCommander /*= true*/ )
  527. {
  528. if (onlyCommander)
  529. {
  530. a->possibleSlots[ArtBearer::HERO].clear();
  531. a->possibleSlots[ArtBearer::CREATURE].clear();
  532. }
  533. for (int i = ArtifactPosition::COMMANDER1; i <= ArtifactPosition::COMMANDER6; ++i)
  534. a->possibleSlots[ArtBearer::COMMANDER].push_back(ArtifactPosition(i));
  535. }
  536. void CArtHandler::makeItCommanderArt( ArtifactID aid, bool onlyCommander /*= true*/ )
  537. {
  538. CArtifact *a = artifacts[aid];
  539. makeItCommanderArt (a, onlyCommander);
  540. }
  541. bool CArtHandler::legalArtifact(ArtifactID id)
  542. {
  543. auto art = artifacts[id];
  544. //assert ( (!art->constituents) || art->constituents->size() ); //artifacts is not combined or has some components
  545. return ((art->possibleSlots[ArtBearer::HERO].size() ||
  546. (art->possibleSlots[ArtBearer::COMMANDER].size() && VLC->modh->modules.COMMANDERS) ||
  547. (art->possibleSlots[ArtBearer::CREATURE].size() && VLC->modh->modules.STACK_ARTIFACT)) &&
  548. !(art->constituents) && //no combo artifacts spawning
  549. art->aClass >= CArtifact::ART_TREASURE &&
  550. art->aClass <= CArtifact::ART_RELIC);
  551. }
  552. bool CArtHandler::isTradableArtifact(ArtifactID id) const
  553. {
  554. switch (id)
  555. {
  556. case ArtifactID::SPELLBOOK:
  557. case ArtifactID::GRAIL:
  558. case ArtifactID::CATAPULT:
  559. case ArtifactID::BALLISTA:
  560. case ArtifactID::AMMO_CART:
  561. case ArtifactID::FIRST_AID_TENT:
  562. return false;
  563. default:
  564. return true;
  565. }
  566. }
  567. void CArtHandler::initAllowedArtifactsList(const std::vector<bool> &allowed)
  568. {
  569. allowedArtifacts.clear();
  570. treasures.clear();
  571. minors.clear();
  572. majors.clear();
  573. relics.clear();
  574. for (ArtifactID i=ArtifactID::SPELLBOOK; i<ArtifactID::ART_SELECTION; i.advance(1))
  575. {
  576. //check artifacts allowed on a map
  577. //TODO: This line will be different when custom map format is implemented
  578. if (allowed[i] && legalArtifact(i))
  579. allowedArtifacts.push_back(artifacts[i]);
  580. }
  581. for (ArtifactID i = ArtifactID::ART_SELECTION; i<ArtifactID(artifacts.size()); i.advance(1)) //try to allow all artifacts added by mods
  582. {
  583. if (legalArtifact(ArtifactID(i)))
  584. allowedArtifacts.push_back(artifacts[i]);
  585. //keep im mind that artifact can be worn by more than one type of bearer
  586. }
  587. }
  588. std::vector<bool> CArtHandler::getDefaultAllowed() const
  589. {
  590. std::vector<bool> allowedArtifacts;
  591. allowedArtifacts.resize(127, true);
  592. allowedArtifacts.resize(141, false);
  593. allowedArtifacts.resize(artifacts.size(), true);
  594. return allowedArtifacts;
  595. }
  596. void CArtHandler::erasePickedArt(ArtifactID id)
  597. {
  598. CArtifact *art = artifacts[id];
  599. if(auto artifactList = listFromClass(art->aClass))
  600. {
  601. if(artifactList->empty())
  602. fillList(*artifactList, art->aClass);
  603. auto itr = vstd::find(*artifactList, art);
  604. if(itr != artifactList->end())
  605. {
  606. artifactList->erase(itr);
  607. }
  608. else
  609. logGlobal->warnStream() << "Problem: cannot erase artifact " << art->Name() << " from list, it was not present";
  610. }
  611. else
  612. logGlobal->warnStream() << "Problem: cannot find list for artifact " << art->Name() << ", strange class. (special?)";
  613. }
  614. boost::optional<std::vector<CArtifact*>&> CArtHandler::listFromClass( CArtifact::EartClass artifactClass )
  615. {
  616. switch(artifactClass)
  617. {
  618. case CArtifact::ART_TREASURE:
  619. return treasures;
  620. case CArtifact::ART_MINOR:
  621. return minors;
  622. case CArtifact::ART_MAJOR:
  623. return majors;
  624. case CArtifact::ART_RELIC:
  625. return relics;
  626. default: //special artifacts should not be erased
  627. return boost::optional<std::vector<CArtifact*>&>();
  628. }
  629. }
  630. void CArtHandler::fillList( std::vector<CArtifact*> &listToBeFilled, CArtifact::EartClass artifactClass )
  631. {
  632. assert(listToBeFilled.empty());
  633. for (auto & elem : allowedArtifacts)
  634. {
  635. if (elem->aClass == artifactClass)
  636. listToBeFilled.push_back(elem);
  637. }
  638. }
  639. void CArtHandler::afterLoadFinalization()
  640. {
  641. //All artifacts have their id, so we can properly update their bonuses' source ids.
  642. for(auto &art : artifacts)
  643. {
  644. for(auto &bonus : art->getExportedBonusList())
  645. {
  646. assert(art == artifacts[art->id]);
  647. assert(bonus->source == Bonus::ARTIFACT);
  648. bonus->sid = art->id;
  649. }
  650. }
  651. CBonusSystemNode::treeHasChanged();
  652. }
  653. si32 CArtHandler::decodeArfifact(const std::string& identifier)
  654. {
  655. auto rawId = VLC->modh->identifiers.getIdentifier("core", "artifact", identifier);
  656. if(rawId)
  657. return rawId.get();
  658. else
  659. return -1;
  660. }
  661. std::string CArtHandler::encodeArtifact(const si32 index)
  662. {
  663. return VLC->arth->artifacts[index]->identifier;
  664. }
  665. CArtifactInstance::CArtifactInstance()
  666. {
  667. init();
  668. }
  669. CArtifactInstance::CArtifactInstance( CArtifact *Art)
  670. {
  671. init();
  672. setType(Art);
  673. }
  674. void CArtifactInstance::setType( CArtifact *Art )
  675. {
  676. artType = Art;
  677. attachTo(Art);
  678. }
  679. std::string CArtifactInstance::nodeName() const
  680. {
  681. return "Artifact instance of " + (artType ? artType->Name() : std::string("uninitialized")) + " type";
  682. }
  683. CArtifactInstance * CArtifactInstance::createScroll( const CSpell *s)
  684. {
  685. return createScroll(s->id);
  686. }
  687. CArtifactInstance *CArtifactInstance::createScroll(SpellID sid)
  688. {
  689. auto ret = new CArtifactInstance(VLC->arth->artifacts[ArtifactID::SPELL_SCROLL]);
  690. auto b = std::make_shared<Bonus>(Bonus::PERMANENT, Bonus::SPELL, Bonus::ARTIFACT_INSTANCE, -1, ArtifactID::SPELL_SCROLL, sid);
  691. ret->addNewBonus(b);
  692. return ret;
  693. }
  694. void CArtifactInstance::init()
  695. {
  696. id = ArtifactInstanceID();
  697. id = static_cast<ArtifactInstanceID>(ArtifactID::NONE); //to be randomized
  698. setNodeType(ARTIFACT_INSTANCE);
  699. }
  700. std::string CArtifactInstance::getEffectiveDescription(
  701. const CGHeroInstance *hero) const
  702. {
  703. std::string text = artType->Description();
  704. if (!vstd::contains(text, '{'))
  705. text = '{' + artType->Name() + "}\n\n" + text; //workaround for new artifacts with single name, turns it to H3-style
  706. if(artType->id == ArtifactID::SPELL_SCROLL)
  707. {
  708. // 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.
  709. // so we want to replace text in [...] with a spell name
  710. // however other language versions don't have name placeholder at all, so we have to be careful
  711. int spellID = getGivenSpellID();
  712. size_t nameStart = text.find_first_of('[');
  713. size_t nameEnd = text.find_first_of(']', nameStart);
  714. if(spellID >= 0)
  715. {
  716. if(nameStart != std::string::npos && nameEnd != std::string::npos)
  717. text = text.replace(nameStart, nameEnd - nameStart + 1, VLC->spellh->objects[spellID]->name);
  718. }
  719. }
  720. else if (hero && artType->constituentOf.size()) //display info about set
  721. {
  722. std::string artList;
  723. auto combinedArt = artType->constituentOf[0];
  724. text += "\n\n";
  725. text += "{" + combinedArt->Name() + "}";
  726. int wornArtifacts = 0;
  727. for (auto a : *combinedArt->constituents) //TODO: can the artifact be a part of more than one set?
  728. {
  729. artList += "\n" + a->Name();
  730. if (hero->hasArt(a->id, true))
  731. wornArtifacts++;
  732. }
  733. text += " (" + boost::str(boost::format("%d") % wornArtifacts) + " / " +
  734. boost::str(boost::format("%d") % combinedArt->constituents->size()) + ")" + artList;
  735. //TODO: fancy colors and fonts for this text
  736. }
  737. return text;
  738. }
  739. ArtifactPosition CArtifactInstance::firstAvailableSlot(const CArtifactSet *h) const
  740. {
  741. for(auto slot : artType->possibleSlots.at(h->bearerType()))
  742. {
  743. if(canBePutAt(h, slot)) //if(artType->fitsAt(h->artifWorn, slot))
  744. {
  745. //we've found a free suitable slot.
  746. return slot;
  747. }
  748. }
  749. //if haven't find proper slot, use backpack
  750. return firstBackpackSlot(h);
  751. }
  752. ArtifactPosition CArtifactInstance::firstBackpackSlot(const CArtifactSet *h) const
  753. {
  754. if(!artType->isBig()) //discard big artifact
  755. return ArtifactPosition(
  756. GameConstants::BACKPACK_START + h->artifactsInBackpack.size());
  757. return ArtifactPosition::PRE_FIRST;
  758. }
  759. bool CArtifactInstance::canBePutAt(const ArtifactLocation & al, bool assumeDestRemoved /*= false*/) const
  760. {
  761. return canBePutAt(al.getHolderArtSet(), al.slot, assumeDestRemoved);
  762. }
  763. bool CArtifactInstance::canBePutAt(const CArtifactSet *artSet, ArtifactPosition slot, bool assumeDestRemoved /*= false*/) const
  764. {
  765. if(slot >= GameConstants::BACKPACK_START)
  766. {
  767. if(artType->isBig())
  768. return false;
  769. //TODO backpack limit
  770. return true;
  771. }
  772. auto possibleSlots = artType->possibleSlots.find(artSet->bearerType());
  773. if(possibleSlots == artType->possibleSlots.end())
  774. {
  775. logGlobal->warnStream() << "Warning: artifact " << artType->Name() << " doesn't have defined allowed slots for bearer of type "
  776. << artSet->bearerType();
  777. return false;
  778. }
  779. if(!vstd::contains(possibleSlots->second, slot))
  780. return false;
  781. return artSet->isPositionFree(slot, assumeDestRemoved);
  782. }
  783. void CArtifactInstance::putAt(ArtifactLocation al)
  784. {
  785. assert(canBePutAt(al));
  786. al.getHolderArtSet()->setNewArtSlot(al.slot, this, false);
  787. if(al.slot < GameConstants::BACKPACK_START)
  788. al.getHolderNode()->attachTo(this);
  789. }
  790. void CArtifactInstance::removeFrom(ArtifactLocation al)
  791. {
  792. assert(al.getHolderArtSet()->getArt(al.slot) == this);
  793. al.getHolderArtSet()->eraseArtSlot(al.slot);
  794. if(al.slot < GameConstants::BACKPACK_START)
  795. al.getHolderNode()->detachFrom(this);
  796. //TODO delete me?
  797. }
  798. bool CArtifactInstance::canBeDisassembled() const
  799. {
  800. return bool(artType->constituents);
  801. }
  802. std::vector<const CArtifact *> CArtifactInstance::assemblyPossibilities(const CArtifactSet *h) const
  803. {
  804. std::vector<const CArtifact *> ret;
  805. if(artType->constituents) //combined artifact already: no combining of combined artifacts... for now.
  806. return ret;
  807. for(const CArtifact * artifact : artType->constituentOf)
  808. {
  809. assert(artifact->constituents);
  810. bool possible = true;
  811. for(const CArtifact * constituent : *artifact->constituents) //check if all constituents are available
  812. {
  813. if(!h->hasArt(constituent->id, true)) //constituent must be equipped
  814. {
  815. possible = false;
  816. break;
  817. }
  818. }
  819. if(possible)
  820. ret.push_back(artifact);
  821. }
  822. return ret;
  823. }
  824. void CArtifactInstance::move(ArtifactLocation src, ArtifactLocation dst)
  825. {
  826. removeFrom(src);
  827. putAt(dst);
  828. }
  829. CArtifactInstance * CArtifactInstance::createNewArtifactInstance(CArtifact *Art)
  830. {
  831. if(!Art->constituents)
  832. {
  833. auto ret = new CArtifactInstance(Art);
  834. if (dynamic_cast<CGrowingArtifact *>(Art))
  835. {
  836. auto bonus = std::make_shared<Bonus>();
  837. bonus->type = Bonus::LEVEL_COUNTER;
  838. bonus->val = 0;
  839. ret->addNewBonus (bonus);
  840. }
  841. return ret;
  842. }
  843. else
  844. {
  845. auto ret = new CCombinedArtifactInstance(Art);
  846. ret->createConstituents();
  847. return ret;
  848. }
  849. }
  850. CArtifactInstance * CArtifactInstance::createNewArtifactInstance(int aid)
  851. {
  852. return createNewArtifactInstance(VLC->arth->artifacts[aid]);
  853. }
  854. CArtifactInstance * CArtifactInstance::createArtifact(CMap * map, int aid, int spellID)
  855. {
  856. CArtifactInstance * a = nullptr;
  857. if(aid >= 0)
  858. {
  859. if(spellID < 0)
  860. {
  861. a = CArtifactInstance::createNewArtifactInstance(aid);
  862. }
  863. else
  864. {
  865. a = CArtifactInstance::createScroll(SpellID(spellID).toSpell());
  866. }
  867. }
  868. else //FIXME: create combined artifact instance for random combined artifacts, just in case
  869. {
  870. a = new CArtifactInstance(); //random, empty
  871. }
  872. map->addNewArtifactInstance(a);
  873. //TODO make it nicer
  874. if(a->artType && (!!a->artType->constituents))
  875. {
  876. CCombinedArtifactInstance * comb = dynamic_cast<CCombinedArtifactInstance *>(a);
  877. for(CCombinedArtifactInstance::ConstituentInfo & ci : comb->constituentsInfo)
  878. {
  879. map->addNewArtifactInstance(ci.art);
  880. }
  881. }
  882. return a;
  883. }
  884. void CArtifactInstance::deserializationFix()
  885. {
  886. setType(artType);
  887. }
  888. SpellID CArtifactInstance::getGivenSpellID() const
  889. {
  890. const auto b = getBonusLocalFirst(Selector::type(Bonus::SPELL));
  891. if(!b)
  892. {
  893. logGlobal->warnStream() << "Warning: " << nodeName() << " doesn't bear any spell!";
  894. return SpellID::NONE;
  895. }
  896. return SpellID(b->subtype);
  897. }
  898. bool CArtifactInstance::isPart(const CArtifactInstance *supposedPart) const
  899. {
  900. return supposedPart == this;
  901. }
  902. bool CCombinedArtifactInstance::canBePutAt(const CArtifactSet *artSet, ArtifactPosition slot, bool assumeDestRemoved /*= false*/) const
  903. {
  904. bool canMainArtifactBePlaced = CArtifactInstance::canBePutAt(artSet, slot, assumeDestRemoved);
  905. if(!canMainArtifactBePlaced)
  906. return false; //no is no...
  907. if(slot >= GameConstants::BACKPACK_START)
  908. return true; //we can always remove combined art to the backapck
  909. assert(artType->constituents);
  910. std::vector<ConstituentInfo> constituentsToBePlaced = constituentsInfo; //we'll remove constituents from that list, as we find a suitable slot for them
  911. //it may be that we picked a combined artifact in hero screen (though technically it's still there) to move it
  912. //so we remove from the list all constituents that are already present on dst hero in the form of locks
  913. for(const ConstituentInfo &constituent : constituentsInfo)
  914. {
  915. if(constituent.art == artSet->getArt(constituent.slot, false)) //no need to worry about locked constituent
  916. constituentsToBePlaced -= constituent;
  917. }
  918. //we iterate over all active slots and check if constituents fits them
  919. for (int i = 0; i < GameConstants::BACKPACK_START; i++)
  920. {
  921. for(auto art = constituentsToBePlaced.begin(); art != constituentsToBePlaced.end(); art++)
  922. {
  923. 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
  924. {
  925. constituentsToBePlaced.erase(art);
  926. break;
  927. }
  928. }
  929. }
  930. return constituentsToBePlaced.empty();
  931. }
  932. bool CCombinedArtifactInstance::canBeDisassembled() const
  933. {
  934. return true;
  935. }
  936. CCombinedArtifactInstance::CCombinedArtifactInstance(CArtifact *Art)
  937. : CArtifactInstance(Art) //TODO: seems unued, but need to be written
  938. {
  939. }
  940. CCombinedArtifactInstance::CCombinedArtifactInstance()
  941. {
  942. }
  943. void CCombinedArtifactInstance::createConstituents()
  944. {
  945. assert(artType);
  946. assert(artType->constituents);
  947. for(const CArtifact * art : *artType->constituents)
  948. {
  949. addAsConstituent(CArtifactInstance::createNewArtifactInstance(art->id), ArtifactPosition::PRE_FIRST);
  950. }
  951. }
  952. void CCombinedArtifactInstance::addAsConstituent(CArtifactInstance *art, ArtifactPosition slot)
  953. {
  954. assert(vstd::contains(*artType->constituents, art->artType.get()));
  955. assert(art->getParentNodes().size() == 1 && art->getParentNodes().front() == art->artType);
  956. constituentsInfo.push_back(ConstituentInfo(art, slot));
  957. attachTo(art);
  958. }
  959. void CCombinedArtifactInstance::putAt(ArtifactLocation al)
  960. {
  961. if(al.slot >= GameConstants::BACKPACK_START)
  962. {
  963. CArtifactInstance::putAt(al);
  964. for(ConstituentInfo &ci : constituentsInfo)
  965. ci.slot = ArtifactPosition::PRE_FIRST;
  966. }
  967. else
  968. {
  969. CArtifactInstance *mainConstituent = figureMainConstituent(al); //it'll be replaced with combined artifact, not a lock
  970. CArtifactInstance::putAt(al); //puts combined art (this)
  971. for(ConstituentInfo &ci : constituentsInfo)
  972. {
  973. if(ci.art != mainConstituent)
  974. {
  975. const ArtifactLocation suggestedPos(al.artHolder, ci.slot);
  976. const bool inActiveSlot = vstd::isbetween(ci.slot, 0, GameConstants::BACKPACK_START);
  977. const bool suggestedPosValid = ci.art->canBePutAt(suggestedPos);
  978. ArtifactPosition pos = ArtifactPosition::PRE_FIRST;
  979. if(inActiveSlot && suggestedPosValid) //there is a valid suggestion where to place lock
  980. pos = ci.slot;
  981. else
  982. ci.slot = pos = ci.art->firstAvailableSlot(al.getHolderArtSet());
  983. assert(pos < GameConstants::BACKPACK_START);
  984. al.getHolderArtSet()->setNewArtSlot(pos, ci.art, true); //sets as lock
  985. }
  986. else
  987. {
  988. ci.slot = ArtifactPosition::PRE_FIRST;
  989. }
  990. }
  991. }
  992. }
  993. void CCombinedArtifactInstance::removeFrom(ArtifactLocation al)
  994. {
  995. if(al.slot >= GameConstants::BACKPACK_START)
  996. {
  997. CArtifactInstance::removeFrom(al);
  998. }
  999. else
  1000. {
  1001. for(ConstituentInfo &ci : constituentsInfo)
  1002. {
  1003. if(ci.slot >= 0)
  1004. {
  1005. al.getHolderArtSet()->eraseArtSlot(ci.slot);
  1006. ci.slot = ArtifactPosition::PRE_FIRST;
  1007. }
  1008. else
  1009. {
  1010. //main constituent
  1011. CArtifactInstance::removeFrom(al);
  1012. }
  1013. }
  1014. }
  1015. }
  1016. CArtifactInstance * CCombinedArtifactInstance::figureMainConstituent(const ArtifactLocation al)
  1017. {
  1018. CArtifactInstance *mainConstituent = nullptr; //it'll be replaced with combined artifact, not a lock
  1019. for(ConstituentInfo &ci : constituentsInfo)
  1020. if(ci.slot == al.slot)
  1021. mainConstituent = ci.art;
  1022. if(!mainConstituent)
  1023. {
  1024. for(ConstituentInfo &ci : constituentsInfo)
  1025. {
  1026. if(vstd::contains(ci.art->artType->possibleSlots[al.getHolderArtSet()->bearerType()], al.slot))
  1027. {
  1028. mainConstituent = ci.art;
  1029. }
  1030. }
  1031. }
  1032. return mainConstituent;
  1033. }
  1034. void CCombinedArtifactInstance::deserializationFix()
  1035. {
  1036. for(ConstituentInfo &ci : constituentsInfo)
  1037. attachTo(ci.art);
  1038. }
  1039. bool CCombinedArtifactInstance::isPart(const CArtifactInstance *supposedPart) const
  1040. {
  1041. bool me = CArtifactInstance::isPart(supposedPart);
  1042. if(me)
  1043. return true;
  1044. //check for constituents
  1045. for(const ConstituentInfo &constituent : constituentsInfo)
  1046. if(constituent.art == supposedPart)
  1047. return true;
  1048. return false;
  1049. }
  1050. CCombinedArtifactInstance::ConstituentInfo::ConstituentInfo(CArtifactInstance *Art /*= nullptr*/, ArtifactPosition Slot /*= -1*/)
  1051. {
  1052. art = Art;
  1053. slot = Slot;
  1054. }
  1055. bool CCombinedArtifactInstance::ConstituentInfo::operator==(const ConstituentInfo &rhs) const
  1056. {
  1057. return art == rhs.art && slot == rhs.slot;
  1058. }
  1059. const CArtifactInstance* CArtifactSet::getArt(ArtifactPosition pos, bool excludeLocked /*= true*/) const
  1060. {
  1061. if(const ArtSlotInfo *si = getSlot(pos))
  1062. {
  1063. if(si->artifact && (!excludeLocked || !si->locked))
  1064. return si->artifact;
  1065. }
  1066. return nullptr;
  1067. }
  1068. CArtifactInstance* CArtifactSet::getArt(ArtifactPosition pos, bool excludeLocked /*= true*/)
  1069. {
  1070. return const_cast<CArtifactInstance*>((const_cast<const CArtifactSet*>(this))->getArt(pos, excludeLocked));
  1071. }
  1072. ArtifactPosition CArtifactSet::getArtPos(int aid, bool onlyWorn /*= true*/) const
  1073. {
  1074. for(auto i = artifactsWorn.cbegin(); i != artifactsWorn.cend(); i++)
  1075. if(i->second.artifact->artType->id == aid)
  1076. return i->first;
  1077. if(onlyWorn)
  1078. return ArtifactPosition::PRE_FIRST;
  1079. for(int i = 0; i < artifactsInBackpack.size(); i++)
  1080. if(artifactsInBackpack[i].artifact->artType->id == aid)
  1081. return ArtifactPosition(GameConstants::BACKPACK_START + i);
  1082. return ArtifactPosition::PRE_FIRST;
  1083. }
  1084. ArtifactPosition CArtifactSet::getArtPos(const CArtifactInstance *art) const
  1085. {
  1086. for(auto i : artifactsWorn)
  1087. if(i.second.artifact == art)
  1088. return i.first;
  1089. for(int i = 0; i < artifactsInBackpack.size(); i++)
  1090. if(artifactsInBackpack[i].artifact == art)
  1091. return ArtifactPosition(GameConstants::BACKPACK_START + i);
  1092. return ArtifactPosition::PRE_FIRST;
  1093. }
  1094. const CArtifactInstance * CArtifactSet::getArtByInstanceId( ArtifactInstanceID artInstId ) const
  1095. {
  1096. for(auto i : artifactsWorn)
  1097. if(i.second.artifact->id == artInstId)
  1098. return i.second.artifact;
  1099. for(auto i : artifactsInBackpack)
  1100. if(i.artifact->id == artInstId)
  1101. return i.artifact;
  1102. return nullptr;
  1103. }
  1104. bool CArtifactSet::hasArt(ui32 aid, bool onlyWorn /*= false*/,
  1105. bool searchBackpackAssemblies /*= false*/) const
  1106. {
  1107. return getArtPos(aid, onlyWorn) != ArtifactPosition::PRE_FIRST ||
  1108. (searchBackpackAssemblies && getHiddenArt(aid));
  1109. }
  1110. std::pair<const CCombinedArtifactInstance *, const CArtifactInstance *>
  1111. CArtifactSet::searchForConstituent(int aid) const
  1112. {
  1113. for(auto & slot : artifactsInBackpack)
  1114. {
  1115. auto art = slot.artifact;
  1116. if(art->canBeDisassembled())
  1117. {
  1118. auto ass = static_cast<CCombinedArtifactInstance *>(art.get());
  1119. for(auto& ci : ass->constituentsInfo)
  1120. {
  1121. if(ci.art->artType->id == aid)
  1122. {
  1123. return {ass, ci.art};
  1124. }
  1125. }
  1126. }
  1127. }
  1128. return {nullptr, nullptr};
  1129. }
  1130. const CArtifactInstance *CArtifactSet::getHiddenArt(int aid) const
  1131. {
  1132. return searchForConstituent(aid).second;
  1133. }
  1134. const CCombinedArtifactInstance *CArtifactSet::getAssemblyByConstituent(int aid) const
  1135. {
  1136. return searchForConstituent(aid).first;
  1137. }
  1138. const ArtSlotInfo * CArtifactSet::getSlot(ArtifactPosition pos) const
  1139. {
  1140. if(vstd::contains(artifactsWorn, pos))
  1141. return &artifactsWorn.at(pos);
  1142. if(pos >= ArtifactPosition::AFTER_LAST )
  1143. {
  1144. int backpackPos = (int)pos - GameConstants::BACKPACK_START;
  1145. if(backpackPos < 0 || backpackPos >= artifactsInBackpack.size())
  1146. return nullptr;
  1147. else
  1148. return &artifactsInBackpack[backpackPos];
  1149. }
  1150. return nullptr;
  1151. }
  1152. bool CArtifactSet::isPositionFree(ArtifactPosition pos, bool onlyLockCheck /*= false*/) const
  1153. {
  1154. if(const ArtSlotInfo *s = getSlot(pos))
  1155. return (onlyLockCheck || !s->artifact) && !s->locked;
  1156. return true; //no slot means not used
  1157. }
  1158. si32 CArtifactSet::getArtTypeId(ArtifactPosition pos) const
  1159. {
  1160. const CArtifactInstance * const a = getArt(pos);
  1161. if(!a)
  1162. {
  1163. logGlobal->warnStream() << (dynamic_cast<const CGHeroInstance*>(this))->name << " has no artifact at " << pos << " (getArtTypeId)";
  1164. return -1;
  1165. }
  1166. return a->artType->id;
  1167. }
  1168. CArtifactSet::~CArtifactSet()
  1169. {
  1170. }
  1171. ArtSlotInfo & CArtifactSet::retreiveNewArtSlot(ArtifactPosition slot)
  1172. {
  1173. assert(!vstd::contains(artifactsWorn, slot));
  1174. ArtSlotInfo &ret = slot < GameConstants::BACKPACK_START
  1175. ? artifactsWorn[slot]
  1176. : *artifactsInBackpack.insert(artifactsInBackpack.begin() + (slot - GameConstants::BACKPACK_START), ArtSlotInfo());
  1177. return ret;
  1178. }
  1179. void CArtifactSet::setNewArtSlot(ArtifactPosition slot, CArtifactInstance *art, bool locked)
  1180. {
  1181. ArtSlotInfo &asi = retreiveNewArtSlot(slot);
  1182. asi.artifact = art;
  1183. asi.locked = locked;
  1184. }
  1185. void CArtifactSet::eraseArtSlot(ArtifactPosition slot)
  1186. {
  1187. if(slot < GameConstants::BACKPACK_START)
  1188. {
  1189. artifactsWorn.erase(slot);
  1190. }
  1191. else
  1192. {
  1193. slot = ArtifactPosition(slot - GameConstants::BACKPACK_START);
  1194. artifactsInBackpack.erase(artifactsInBackpack.begin() + slot);
  1195. }
  1196. }
  1197. void CArtifactSet::artDeserializationFix(CBonusSystemNode *node)
  1198. {
  1199. for(auto & elem : artifactsWorn)
  1200. if(elem.second.artifact && !elem.second.locked)
  1201. node->attachTo(elem.second.artifact);
  1202. }
  1203. void CArtifactSet::writeJson(JsonNode& json) const
  1204. {
  1205. }
  1206. void CArtifactSet::readJson(const JsonNode& json)
  1207. {
  1208. }