CArtHandler.cpp 39 KB

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