CArtHandler.cpp 39 KB

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