2
0

CArtHandler.cpp 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069
  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 "ArtifactUtils.h"
  12. #include "ExceptionsCommon.h"
  13. #include "IGameSettings.h"
  14. #include "mapObjects/MapObjects.h"
  15. #include "constants/StringConstants.h"
  16. #include "json/JsonBonus.h"
  17. #include "mapObjectConstructors/AObjectTypeHandler.h"
  18. #include "mapObjectConstructors/CObjectClassesHandler.h"
  19. #include "serializer/JsonSerializeFormat.h"
  20. #include "texts/CGeneralTextHandler.h"
  21. #include "texts/CLegacyConfigParser.h"
  22. // Note: list must match entries in ArtTraits.txt
  23. #define ART_POS_LIST \
  24. ART_POS(SPELLBOOK) \
  25. ART_POS(MACH4) \
  26. ART_POS(MACH3) \
  27. ART_POS(MACH2) \
  28. ART_POS(MACH1) \
  29. ART_POS(MISC5) \
  30. ART_POS(MISC4) \
  31. ART_POS(MISC3) \
  32. ART_POS(MISC2) \
  33. ART_POS(MISC1) \
  34. ART_POS(FEET) \
  35. ART_POS(LEFT_RING) \
  36. ART_POS(RIGHT_RING) \
  37. ART_POS(TORSO) \
  38. ART_POS(LEFT_HAND) \
  39. ART_POS(RIGHT_HAND) \
  40. ART_POS(NECK) \
  41. ART_POS(SHOULDERS) \
  42. ART_POS(HEAD)
  43. VCMI_LIB_NAMESPACE_BEGIN
  44. bool CCombinedArtifact::isCombined() const
  45. {
  46. return !(constituents.empty());
  47. }
  48. const std::vector<const CArtifact*> & CCombinedArtifact::getConstituents() const
  49. {
  50. return constituents;
  51. }
  52. const std::set<const CArtifact*> & CCombinedArtifact::getPartOf() const
  53. {
  54. return partOf;
  55. }
  56. void CCombinedArtifact::setFused(bool isFused)
  57. {
  58. fused = isFused;
  59. }
  60. bool CCombinedArtifact::isFused() const
  61. {
  62. return fused;
  63. }
  64. bool CCombinedArtifact::hasParts() const
  65. {
  66. return isCombined() && !isFused();
  67. }
  68. bool CScrollArtifact::isScroll() const
  69. {
  70. return static_cast<const CArtifact*>(this)->getId() == ArtifactID::SPELL_SCROLL;
  71. }
  72. bool CGrowingArtifact::isGrowing() const
  73. {
  74. return !bonusesPerLevel.empty() || !thresholdBonuses.empty();
  75. }
  76. std::vector <std::pair<ui16, Bonus>> & CGrowingArtifact::getBonusesPerLevel()
  77. {
  78. return bonusesPerLevel;
  79. }
  80. const std::vector <std::pair<ui16, Bonus>> & CGrowingArtifact::getBonusesPerLevel() const
  81. {
  82. return bonusesPerLevel;
  83. }
  84. std::vector <std::pair<ui16, Bonus>> & CGrowingArtifact::getThresholdBonuses()
  85. {
  86. return thresholdBonuses;
  87. }
  88. const std::vector <std::pair<ui16, Bonus>> & CGrowingArtifact::getThresholdBonuses() const
  89. {
  90. return thresholdBonuses;
  91. }
  92. int32_t CArtifact::getIndex() const
  93. {
  94. return id.toEnum();
  95. }
  96. int32_t CArtifact::getIconIndex() const
  97. {
  98. return iconIndex;
  99. }
  100. std::string CArtifact::getJsonKey() const
  101. {
  102. return modScope + ':' + identifier;
  103. }
  104. std::string CArtifact::getModScope() const
  105. {
  106. return modScope;
  107. }
  108. void CArtifact::registerIcons(const IconRegistar & cb) const
  109. {
  110. cb(getIconIndex(), 0, "ARTIFACT", image);
  111. cb(getIconIndex(), 0, "ARTIFACTLARGE", large);
  112. }
  113. ArtifactID CArtifact::getId() const
  114. {
  115. return id;
  116. }
  117. const IBonusBearer * CArtifact::getBonusBearer() const
  118. {
  119. return this;
  120. }
  121. std::string CArtifact::getDescriptionTranslated() const
  122. {
  123. return VLC->generaltexth->translate(getDescriptionTextID());
  124. }
  125. std::string CArtifact::getEventTranslated() const
  126. {
  127. return VLC->generaltexth->translate(getEventTextID());
  128. }
  129. std::string CArtifact::getNameTranslated() const
  130. {
  131. return VLC->generaltexth->translate(getNameTextID());
  132. }
  133. std::string CArtifact::getDescriptionTextID() const
  134. {
  135. return TextIdentifier("artifact", modScope, identifier, "description").get();
  136. }
  137. std::string CArtifact::getEventTextID() const
  138. {
  139. return TextIdentifier("artifact", modScope, identifier, "event").get();
  140. }
  141. std::string CArtifact::getNameTextID() const
  142. {
  143. return TextIdentifier("artifact", modScope, identifier, "name").get();
  144. }
  145. uint32_t CArtifact::getPrice() const
  146. {
  147. return price;
  148. }
  149. CreatureID CArtifact::getWarMachine() const
  150. {
  151. return warMachine;
  152. }
  153. bool CArtifact::isBig() const
  154. {
  155. return warMachine != CreatureID::NONE;
  156. }
  157. bool CArtifact::isTradable() const
  158. {
  159. switch(id.toEnum())
  160. {
  161. case ArtifactID::SPELLBOOK:
  162. return false;
  163. default:
  164. return !isBig();
  165. }
  166. }
  167. bool CArtifact::canBePutAt(const CArtifactSet * artSet, ArtifactPosition slot, bool assumeDestRemoved) const
  168. {
  169. auto simpleArtCanBePutAt = [this](const CArtifactSet * artSet, ArtifactPosition slot, bool assumeDestRemoved) -> bool
  170. {
  171. if(artSet->bearerType() == ArtBearer::HERO && ArtifactUtils::isSlotBackpack(slot))
  172. {
  173. if(isBig() || (!assumeDestRemoved && !ArtifactUtils::isBackpackFreeSlots(artSet)))
  174. return false;
  175. return true;
  176. }
  177. if(!vstd::contains(possibleSlots.at(artSet->bearerType()), slot))
  178. return false;
  179. return artSet->isPositionFree(slot, assumeDestRemoved);
  180. };
  181. auto artCanBePutAt = [this, simpleArtCanBePutAt](const CArtifactSet * artSet, ArtifactPosition slot, bool assumeDestRemoved) -> bool
  182. {
  183. if(hasParts())
  184. {
  185. if(!simpleArtCanBePutAt(artSet, slot, assumeDestRemoved))
  186. return false;
  187. if(ArtifactUtils::isSlotBackpack(slot))
  188. return true;
  189. CArtifactFittingSet fittingSet(artSet->bearerType());
  190. fittingSet.artifactsWorn = artSet->artifactsWorn;
  191. if(assumeDestRemoved)
  192. fittingSet.removeArtifact(slot);
  193. for(const auto art : constituents)
  194. {
  195. auto possibleSlot = ArtifactUtils::getArtAnyPosition(&fittingSet, art->getId());
  196. if(ArtifactUtils::isSlotEquipment(possibleSlot))
  197. {
  198. fittingSet.lockSlot(possibleSlot);
  199. }
  200. else
  201. {
  202. return false;
  203. }
  204. }
  205. return true;
  206. }
  207. else
  208. {
  209. return simpleArtCanBePutAt(artSet, slot, assumeDestRemoved);
  210. }
  211. };
  212. if(slot == ArtifactPosition::TRANSITION_POS)
  213. return true;
  214. if(slot == ArtifactPosition::FIRST_AVAILABLE)
  215. {
  216. for(const auto & slot : possibleSlots.at(artSet->bearerType()))
  217. {
  218. if(artCanBePutAt(artSet, slot, assumeDestRemoved))
  219. return true;
  220. }
  221. return artCanBePutAt(artSet, ArtifactPosition::BACKPACK_START, assumeDestRemoved);
  222. }
  223. else if(ArtifactUtils::isSlotBackpack(slot))
  224. {
  225. return artCanBePutAt(artSet, ArtifactPosition::BACKPACK_START, assumeDestRemoved);
  226. }
  227. else
  228. {
  229. return artCanBePutAt(artSet, slot, assumeDestRemoved);
  230. }
  231. }
  232. CArtifact::CArtifact()
  233. : iconIndex(ArtifactID::NONE),
  234. price(0)
  235. {
  236. setNodeType(ARTIFACT);
  237. possibleSlots[ArtBearer::HERO]; //we want to generate map entry even if it will be empty
  238. possibleSlots[ArtBearer::CREATURE]; //we want to generate map entry even if it will be empty
  239. possibleSlots[ArtBearer::COMMANDER];
  240. possibleSlots[ArtBearer::ALTAR];
  241. }
  242. //This destructor should be placed here to avoid side effects
  243. CArtifact::~CArtifact() = default;
  244. int CArtifact::getArtClassSerial() const
  245. {
  246. if(id == ArtifactID::SPELL_SCROLL)
  247. return 4;
  248. switch(aClass)
  249. {
  250. case ART_TREASURE:
  251. return 0;
  252. case ART_MINOR:
  253. return 1;
  254. case ART_MAJOR:
  255. return 2;
  256. case ART_RELIC:
  257. return 3;
  258. case ART_SPECIAL:
  259. return 5;
  260. }
  261. return -1;
  262. }
  263. std::string CArtifact::nodeName() const
  264. {
  265. return "Artifact: " + getNameTranslated();
  266. }
  267. void CArtifact::addNewBonus(const std::shared_ptr<Bonus>& b)
  268. {
  269. b->source = BonusSource::ARTIFACT;
  270. b->duration = BonusDuration::PERMANENT;
  271. b->description.appendTextID(getNameTextID());
  272. b->description.appendRawString(" %+d");
  273. CBonusSystemNode::addNewBonus(b);
  274. }
  275. const std::map<ArtBearer::ArtBearer, std::vector<ArtifactPosition>> & CArtifact::getPossibleSlots() const
  276. {
  277. return possibleSlots;
  278. }
  279. void CArtifact::updateFrom(const JsonNode& data)
  280. {
  281. //TODO:CArtifact::updateFrom
  282. }
  283. void CArtifact::setImage(int32_t iconIndex, std::string image, std::string large)
  284. {
  285. this->iconIndex = iconIndex;
  286. this->image = image;
  287. this->large = large;
  288. }
  289. CArtHandler::~CArtHandler() = default;
  290. std::vector<JsonNode> CArtHandler::loadLegacyData()
  291. {
  292. size_t dataSize = VLC->engineSettings()->getInteger(EGameSettings::TEXTS_ARTIFACT);
  293. objects.resize(dataSize);
  294. std::vector<JsonNode> h3Data;
  295. h3Data.reserve(dataSize);
  296. #define ART_POS(x) #x ,
  297. const std::vector<std::string> artSlots = { ART_POS_LIST };
  298. #undef ART_POS
  299. static const std::map<char, std::string> classes =
  300. {{'S',"SPECIAL"}, {'T',"TREASURE"},{'N',"MINOR"},{'J',"MAJOR"},{'R',"RELIC"},};
  301. CLegacyConfigParser parser(TextPath::builtin("DATA/ARTRAITS.TXT"));
  302. CLegacyConfigParser events(TextPath::builtin("DATA/ARTEVENT.TXT"));
  303. parser.endLine(); // header
  304. parser.endLine();
  305. for (size_t i = 0; i < dataSize; i++)
  306. {
  307. JsonNode artData;
  308. artData["text"]["name"].String() = parser.readString();
  309. artData["text"]["event"].String() = events.readString();
  310. artData["value"].Float() = parser.readNumber();
  311. for(const auto & artSlot : artSlots)
  312. {
  313. if(parser.readString() == "x")
  314. {
  315. artData["slot"].Vector().emplace_back(artSlot);
  316. }
  317. }
  318. std::string artClass = parser.readString();
  319. if (classes.count(artClass[0]))
  320. artData["class"].String() = classes.at(artClass[0]);
  321. else
  322. throw DataLoadingException("File ARTRAITS.TXT is invalid or corrupted! Please reinstall Heroes III data files");
  323. artData["text"]["description"].String() = parser.readString();
  324. parser.endLine();
  325. events.endLine();
  326. h3Data.push_back(artData);
  327. }
  328. return h3Data;
  329. }
  330. void CArtHandler::loadObject(std::string scope, std::string name, const JsonNode & data)
  331. {
  332. auto object = loadFromJson(scope, data, name, objects.size());
  333. object->iconIndex = object->getIndex() + 5;
  334. objects.emplace_back(object);
  335. registerObject(scope, "artifact", name, object->id.getNum());
  336. }
  337. void CArtHandler::loadObject(std::string scope, std::string name, const JsonNode & data, size_t index)
  338. {
  339. auto object = loadFromJson(scope, data, name, index);
  340. object->iconIndex = object->getIndex();
  341. assert(objects[index] == nullptr); // ensure that this id was not loaded before
  342. objects[index] = object;
  343. registerObject(scope, "artifact", name, object->id.getNum());
  344. }
  345. const std::vector<std::string> & CArtHandler::getTypeNames() const
  346. {
  347. static const std::vector<std::string> typeNames = { "artifact" };
  348. return typeNames;
  349. }
  350. std::shared_ptr<CArtifact> CArtHandler::loadFromJson(const std::string & scope, const JsonNode & node, const std::string & identifier, size_t index)
  351. {
  352. assert(identifier.find(':') == std::string::npos);
  353. assert(!scope.empty());
  354. auto art = std::make_shared<CArtifact>();
  355. if(!node["growing"].isNull())
  356. {
  357. for(auto bonus : node["growing"]["bonusesPerLevel"].Vector())
  358. {
  359. art->bonusesPerLevel.emplace_back(static_cast<ui16>(bonus["level"].Float()), Bonus());
  360. JsonUtils::parseBonus(bonus["bonus"], &art->bonusesPerLevel.back().second);
  361. }
  362. for(auto bonus : node["growing"]["thresholdBonuses"].Vector())
  363. {
  364. art->thresholdBonuses.emplace_back(static_cast<ui16>(bonus["level"].Float()), Bonus());
  365. JsonUtils::parseBonus(bonus["bonus"], &art->thresholdBonuses.back().second);
  366. }
  367. }
  368. art->id = ArtifactID(index);
  369. art->identifier = identifier;
  370. art->modScope = scope;
  371. const JsonNode & text = node["text"];
  372. VLC->generaltexth->registerString(scope, art->getNameTextID(), text["name"]);
  373. VLC->generaltexth->registerString(scope, art->getDescriptionTextID(), text["description"]);
  374. VLC->generaltexth->registerString(scope, art->getEventTextID(), text["event"]);
  375. const JsonNode & graphics = node["graphics"];
  376. art->image = graphics["image"].String();
  377. if(!graphics["large"].isNull())
  378. art->large = graphics["large"].String();
  379. else
  380. art->large = art->image;
  381. art->advMapDef = graphics["map"].String();
  382. art->price = static_cast<ui32>(node["value"].Float());
  383. art->onlyOnWaterMap = node["onlyOnWaterMap"].Bool();
  384. loadSlots(art.get(), node);
  385. loadClass(art.get(), node);
  386. loadType(art.get(), node);
  387. loadComponents(art.get(), node);
  388. for(const auto & b : node["bonuses"].Vector())
  389. {
  390. auto bonus = JsonUtils::parseBonus(b);
  391. art->addNewBonus(bonus);
  392. }
  393. const JsonNode & warMachine = node["warMachine"];
  394. if(!warMachine.isNull())
  395. {
  396. VLC->identifiers()->requestIdentifier("creature", warMachine, [=](si32 id)
  397. {
  398. art->warMachine = CreatureID(id);
  399. //this assumes that creature object is stored before registration
  400. VLC->creh->objects.at(id)->warMachine = art->id;
  401. });
  402. }
  403. VLC->identifiers()->requestIdentifier(scope, "object", "artifact", [=](si32 index)
  404. {
  405. JsonNode conf;
  406. conf.setModScope(scope);
  407. VLC->objtypeh->loadSubObject(art->identifier, conf, Obj::ARTIFACT, art->getIndex());
  408. if(!art->advMapDef.empty())
  409. {
  410. JsonNode templ;
  411. templ["animation"].String() = art->advMapDef;
  412. templ.setModScope(scope);
  413. // add new template.
  414. // Necessary for objects added via mods that don't have any templates in H3
  415. VLC->objtypeh->getHandlerFor(Obj::ARTIFACT, art->getIndex())->addTemplate(templ);
  416. }
  417. });
  418. if(art->isTradable())
  419. art->possibleSlots.at(ArtBearer::ALTAR).push_back(ArtifactPosition::ALTAR);
  420. return art;
  421. }
  422. int32_t ArtifactPositionBase::decode(const std::string & slotName)
  423. {
  424. #define ART_POS(x) { #x, ArtifactPosition::x },
  425. static const std::map<std::string, ArtifactPosition> artifactPositionMap = { ART_POS_LIST };
  426. #undef ART_POS
  427. auto it = artifactPositionMap.find (slotName);
  428. if (it != artifactPositionMap.end())
  429. return it->second;
  430. else
  431. return PRE_FIRST;
  432. }
  433. void CArtHandler::addSlot(CArtifact * art, const std::string & slotID) const
  434. {
  435. static const std::vector<ArtifactPosition> miscSlots =
  436. {
  437. ArtifactPosition::MISC1, ArtifactPosition::MISC2, ArtifactPosition::MISC3, ArtifactPosition::MISC4, ArtifactPosition::MISC5
  438. };
  439. static const std::vector<ArtifactPosition> ringSlots =
  440. {
  441. ArtifactPosition::RIGHT_RING, ArtifactPosition::LEFT_RING
  442. };
  443. if (slotID == "MISC")
  444. {
  445. vstd::concatenate(art->possibleSlots[ArtBearer::HERO], miscSlots);
  446. }
  447. else if (slotID == "RING")
  448. {
  449. vstd::concatenate(art->possibleSlots[ArtBearer::HERO], ringSlots);
  450. }
  451. else
  452. {
  453. auto slot = ArtifactPosition::decode(slotID);
  454. if (slot != ArtifactPosition::PRE_FIRST)
  455. art->possibleSlots[ArtBearer::HERO].push_back(slot);
  456. }
  457. }
  458. void CArtHandler::loadSlots(CArtifact * art, const JsonNode & node) const
  459. {
  460. if (!node["slot"].isNull()) //we assume non-hero slots are irrelevant?
  461. {
  462. if (node["slot"].getType() == JsonNode::JsonType::DATA_STRING)
  463. addSlot(art, node["slot"].String());
  464. else
  465. {
  466. for (const JsonNode & slot : node["slot"].Vector())
  467. addSlot(art, slot.String());
  468. }
  469. std::sort(art->possibleSlots.at(ArtBearer::HERO).begin(), art->possibleSlots.at(ArtBearer::HERO).end());
  470. }
  471. }
  472. CArtifact::EartClass CArtHandler::stringToClass(const std::string & className)
  473. {
  474. static const std::map<std::string, CArtifact::EartClass> artifactClassMap =
  475. {
  476. {"TREASURE", CArtifact::ART_TREASURE},
  477. {"MINOR", CArtifact::ART_MINOR},
  478. {"MAJOR", CArtifact::ART_MAJOR},
  479. {"RELIC", CArtifact::ART_RELIC},
  480. {"SPECIAL", CArtifact::ART_SPECIAL}
  481. };
  482. auto it = artifactClassMap.find (className);
  483. if (it != artifactClassMap.end())
  484. return it->second;
  485. logMod->warn("Warning! Artifact rarity %s not recognized!", className);
  486. return CArtifact::ART_SPECIAL;
  487. }
  488. void CArtHandler::loadClass(CArtifact * art, const JsonNode & node) const
  489. {
  490. art->aClass = stringToClass(node["class"].String());
  491. }
  492. void CArtHandler::loadType(CArtifact * art, const JsonNode & node) const
  493. {
  494. #define ART_BEARER(x) { #x, ArtBearer::x },
  495. static const std::map<std::string, int> artifactBearerMap = { ART_BEARER_LIST };
  496. #undef ART_BEARER
  497. for (const JsonNode & b : node["type"].Vector())
  498. {
  499. auto it = artifactBearerMap.find (b.String());
  500. if (it != artifactBearerMap.end())
  501. {
  502. int bearerType = it->second;
  503. switch (bearerType)
  504. {
  505. case ArtBearer::HERO://TODO: allow arts having several possible bearers
  506. break;
  507. case ArtBearer::COMMANDER:
  508. makeItCommanderArt (art); //original artifacts should have only one bearer type
  509. break;
  510. case ArtBearer::CREATURE:
  511. makeItCreatureArt (art);
  512. break;
  513. }
  514. }
  515. else
  516. logMod->warn("Warning! Artifact type %s not recognized!", b.String());
  517. }
  518. }
  519. void CArtHandler::loadComponents(CArtifact * art, const JsonNode & node)
  520. {
  521. if(!node["components"].isNull())
  522. {
  523. for(const auto & component : node["components"].Vector())
  524. {
  525. VLC->identifiers()->requestIdentifier("artifact", component, [this, art](int32_t id)
  526. {
  527. // when this code is called both combinational art as well as component are loaded
  528. // so it is safe to access any of them
  529. art->constituents.push_back(ArtifactID(id).toArtifact());
  530. objects[id]->partOf.insert(art);
  531. });
  532. }
  533. }
  534. if(!node["fusedComponents"].isNull())
  535. art->setFused(node["fusedComponents"].Bool());
  536. }
  537. void CArtHandler::makeItCreatureArt(CArtifact * a, bool onlyCreature)
  538. {
  539. if (onlyCreature)
  540. {
  541. a->possibleSlots[ArtBearer::HERO].clear();
  542. a->possibleSlots[ArtBearer::COMMANDER].clear();
  543. }
  544. a->possibleSlots[ArtBearer::CREATURE].push_back(ArtifactPosition::CREATURE_SLOT);
  545. }
  546. void CArtHandler::makeItCommanderArt(CArtifact * a, bool onlyCommander)
  547. {
  548. if (onlyCommander)
  549. {
  550. a->possibleSlots[ArtBearer::HERO].clear();
  551. a->possibleSlots[ArtBearer::CREATURE].clear();
  552. }
  553. for(const auto & slot : ArtifactUtils::commanderSlots())
  554. a->possibleSlots[ArtBearer::COMMANDER].push_back(ArtifactPosition(slot));
  555. }
  556. bool CArtHandler::legalArtifact(const ArtifactID & id) const
  557. {
  558. auto art = id.toArtifact();
  559. //assert ( (!art->constituents) || art->constituents->size() ); //artifacts is not combined or has some components
  560. if(art->isCombined())
  561. return false; //no combo artifacts spawning
  562. if(art->aClass < CArtifact::ART_TREASURE || art->aClass > CArtifact::ART_RELIC)
  563. return false; // invalid class
  564. if(art->possibleSlots.count(ArtBearer::HERO) && !art->possibleSlots.at(ArtBearer::HERO).empty())
  565. return true;
  566. if(art->possibleSlots.count(ArtBearer::CREATURE) && !art->possibleSlots.at(ArtBearer::CREATURE).empty() && VLC->engineSettings()->getBoolean(EGameSettings::MODULE_STACK_ARTIFACT))
  567. return true;
  568. if(art->possibleSlots.count(ArtBearer::COMMANDER) && !art->possibleSlots.at(ArtBearer::COMMANDER).empty() && VLC->engineSettings()->getBoolean(EGameSettings::MODULE_COMMANDERS))
  569. return true;
  570. return false;
  571. }
  572. std::set<ArtifactID> CArtHandler::getDefaultAllowed() const
  573. {
  574. std::set<ArtifactID> allowedArtifacts;
  575. for (const auto & artifact : objects)
  576. {
  577. if (!artifact->isCombined())
  578. allowedArtifacts.insert(artifact->getId());
  579. }
  580. return allowedArtifacts;
  581. }
  582. void CArtHandler::afterLoadFinalization()
  583. {
  584. //All artifacts have their id, so we can properly update their bonuses' source ids.
  585. for(auto &art : objects)
  586. {
  587. for(auto &bonus : art->getExportedBonusList())
  588. {
  589. assert(bonus->source == BonusSource::ARTIFACT);
  590. bonus->sid = BonusSourceID(art->id);
  591. }
  592. }
  593. CBonusSystemNode::treeHasChanged();
  594. }
  595. CArtifactInstance * CArtifactSet::getArt(const ArtifactPosition & pos, bool excludeLocked) const
  596. {
  597. if(const ArtSlotInfo * si = getSlot(pos))
  598. {
  599. if(si->artifact && (!excludeLocked || !si->locked))
  600. return si->artifact;
  601. }
  602. return nullptr;
  603. }
  604. ArtifactPosition CArtifactSet::getArtPos(const ArtifactID & aid, bool onlyWorn, bool allowLocked) const
  605. {
  606. for(const auto & [slot, slotInfo] : artifactsWorn)
  607. {
  608. if(slotInfo.artifact->getTypeId() == aid && (allowLocked || !slotInfo.locked))
  609. return slot;
  610. }
  611. if(!onlyWorn)
  612. {
  613. size_t backpackPositionIdx = ArtifactPosition::BACKPACK_START;
  614. for(const auto & artInfo : artifactsInBackpack)
  615. {
  616. const auto art = artInfo.getArt();
  617. if(art && art->getType()->getId() == aid)
  618. return ArtifactPosition(backpackPositionIdx);
  619. backpackPositionIdx++;
  620. }
  621. }
  622. return ArtifactPosition::PRE_FIRST;
  623. }
  624. const CArtifactInstance * CArtifactSet::getArtByInstanceId(const ArtifactInstanceID & artInstId) const
  625. {
  626. for(const auto & i : artifactsWorn)
  627. if(i.second.artifact->getId() == artInstId)
  628. return i.second.artifact;
  629. for(const auto & i : artifactsInBackpack)
  630. if(i.artifact->getId() == artInstId)
  631. return i.artifact;
  632. return nullptr;
  633. }
  634. ArtifactPosition CArtifactSet::getArtPos(const CArtifactInstance * artInst) const
  635. {
  636. if(artInst)
  637. {
  638. for(const auto & slot : artInst->getType()->getPossibleSlots().at(bearerType()))
  639. if(getArt(slot) == artInst)
  640. return slot;
  641. ArtifactPosition backpackSlot = ArtifactPosition::BACKPACK_START;
  642. for(auto & slotInfo : artifactsInBackpack)
  643. {
  644. if(slotInfo.getArt() == artInst)
  645. return backpackSlot;
  646. backpackSlot = ArtifactPosition(backpackSlot + 1);
  647. }
  648. }
  649. return ArtifactPosition::PRE_FIRST;
  650. }
  651. bool CArtifactSet::hasArt(const ArtifactID & aid, bool onlyWorn, bool searchCombinedParts) const
  652. {
  653. if(searchCombinedParts && getCombinedArtWithPart(aid))
  654. return true;
  655. if(getArtPos(aid, onlyWorn, searchCombinedParts) != ArtifactPosition::PRE_FIRST)
  656. return true;
  657. return false;
  658. }
  659. CArtifactSet::ArtPlacementMap CArtifactSet::putArtifact(const ArtifactPosition & slot, CArtifactInstance * art)
  660. {
  661. ArtPlacementMap resArtPlacement;
  662. const auto putToSlot = [this](const ArtifactPosition & targetSlot, CArtifactInstance * targetArt, bool locked)
  663. {
  664. ArtSlotInfo * slotInfo;
  665. if(targetSlot == ArtifactPosition::TRANSITION_POS)
  666. {
  667. slotInfo = &artifactsTransitionPos;
  668. }
  669. else if(ArtifactUtils::isSlotEquipment(targetSlot))
  670. {
  671. slotInfo = &artifactsWorn[targetSlot];
  672. }
  673. else
  674. {
  675. auto position = artifactsInBackpack.begin() + targetSlot - ArtifactPosition::BACKPACK_START;
  676. slotInfo = &(*artifactsInBackpack.emplace(position));
  677. }
  678. slotInfo->artifact = targetArt;
  679. slotInfo->locked = locked;
  680. };
  681. putToSlot(slot, art, false);
  682. if(art->getType()->isCombined() && ArtifactUtils::isSlotEquipment(slot))
  683. {
  684. const CArtifactInstance * mainPart = nullptr;
  685. for(const auto & part : art->getPartsInfo())
  686. if(vstd::contains(part.art->getType()->getPossibleSlots().at(bearerType()), slot)
  687. && (part.slot == ArtifactPosition::PRE_FIRST))
  688. {
  689. mainPart = part.art;
  690. break;
  691. }
  692. for(const auto & part : art->getPartsInfo())
  693. {
  694. if(part.art != mainPart)
  695. {
  696. auto partSlot = part.slot;
  697. if(!part.art->getType()->canBePutAt(this, partSlot))
  698. partSlot = ArtifactUtils::getArtAnyPosition(this, part.art->getTypeId());
  699. assert(ArtifactUtils::isSlotEquipment(partSlot));
  700. putToSlot(partSlot, part.art, true);
  701. resArtPlacement.emplace(part.art, partSlot);
  702. }
  703. else
  704. {
  705. resArtPlacement.emplace(part.art, part.slot);
  706. }
  707. }
  708. }
  709. return resArtPlacement;
  710. }
  711. void CArtifactSet::removeArtifact(const ArtifactPosition & slot)
  712. {
  713. const auto eraseArtSlot = [this](const ArtifactPosition & slotForErase)
  714. {
  715. if(slotForErase == ArtifactPosition::TRANSITION_POS)
  716. {
  717. artifactsTransitionPos.artifact = nullptr;
  718. }
  719. else if(ArtifactUtils::isSlotBackpack(slotForErase))
  720. {
  721. auto backpackSlot = ArtifactPosition(slotForErase - ArtifactPosition::BACKPACK_START);
  722. assert(artifactsInBackpack.begin() + backpackSlot < artifactsInBackpack.end());
  723. artifactsInBackpack.erase(artifactsInBackpack.begin() + backpackSlot);
  724. }
  725. else
  726. {
  727. artifactsWorn.erase(slotForErase);
  728. }
  729. };
  730. if(const auto art = getArt(slot, false))
  731. {
  732. if(art->isCombined())
  733. {
  734. for(const auto & part : art->getPartsInfo())
  735. {
  736. if(part.slot != ArtifactPosition::PRE_FIRST)
  737. {
  738. assert(getArt(part.slot, false));
  739. assert(getArt(part.slot, false) == part.art);
  740. }
  741. eraseArtSlot(part.slot);
  742. }
  743. }
  744. eraseArtSlot(slot);
  745. }
  746. }
  747. const CArtifactInstance * CArtifactSet::getCombinedArtWithPart(const ArtifactID & partId) const
  748. {
  749. for(const auto & slot : artifactsInBackpack)
  750. {
  751. auto art = slot.artifact;
  752. if(art->isCombined())
  753. {
  754. for(auto & ci : art->getPartsInfo())
  755. {
  756. if(ci.art->getTypeId() == partId)
  757. return art;
  758. }
  759. }
  760. }
  761. return nullptr;
  762. }
  763. const ArtSlotInfo * CArtifactSet::getSlot(const ArtifactPosition & pos) const
  764. {
  765. if(pos == ArtifactPosition::TRANSITION_POS)
  766. return &artifactsTransitionPos;
  767. if(vstd::contains(artifactsWorn, pos))
  768. return &artifactsWorn.at(pos);
  769. if(ArtifactUtils::isSlotBackpack(pos))
  770. {
  771. auto backpackPos = pos - ArtifactPosition::BACKPACK_START;
  772. if(backpackPos < 0 || backpackPos >= artifactsInBackpack.size())
  773. return nullptr;
  774. else
  775. return &artifactsInBackpack[backpackPos];
  776. }
  777. return nullptr;
  778. }
  779. void CArtifactSet::lockSlot(const ArtifactPosition & pos)
  780. {
  781. if(pos == ArtifactPosition::TRANSITION_POS)
  782. artifactsTransitionPos.locked = true;
  783. else if(ArtifactUtils::isSlotEquipment(pos))
  784. artifactsWorn[pos].locked = true;
  785. else
  786. {
  787. assert(artifactsInBackpack.size() > pos - ArtifactPosition::BACKPACK_START);
  788. (artifactsInBackpack.begin() + pos - ArtifactPosition::BACKPACK_START)->locked = true;
  789. }
  790. }
  791. bool CArtifactSet::isPositionFree(const ArtifactPosition & pos, bool onlyLockCheck) const
  792. {
  793. if(bearerType() == ArtBearer::ALTAR)
  794. return artifactsInBackpack.size() < GameConstants::ALTAR_ARTIFACTS_SLOTS;
  795. if(const ArtSlotInfo *s = getSlot(pos))
  796. return (onlyLockCheck || !s->artifact) && !s->locked;
  797. return true; //no slot means not used
  798. }
  799. void CArtifactSet::artDeserializationFix(CBonusSystemNode *node)
  800. {
  801. for(auto & elem : artifactsWorn)
  802. if(elem.second.artifact && !elem.second.locked)
  803. node->attachTo(*elem.second.artifact);
  804. }
  805. void CArtifactSet::serializeJsonArtifacts(JsonSerializeFormat & handler, const std::string & fieldName)
  806. {
  807. //todo: creature and commander artifacts
  808. if(handler.saving && artifactsInBackpack.empty() && artifactsWorn.empty())
  809. return;
  810. if(!handler.saving)
  811. {
  812. artifactsInBackpack.clear();
  813. artifactsWorn.clear();
  814. }
  815. auto s = handler.enterStruct(fieldName);
  816. switch(bearerType())
  817. {
  818. case ArtBearer::HERO:
  819. serializeJsonHero(handler);
  820. break;
  821. case ArtBearer::CREATURE:
  822. serializeJsonCreature(handler);
  823. break;
  824. case ArtBearer::COMMANDER:
  825. serializeJsonCommander(handler);
  826. break;
  827. default:
  828. assert(false);
  829. break;
  830. }
  831. }
  832. void CArtifactSet::serializeJsonHero(JsonSerializeFormat & handler)
  833. {
  834. for(const auto & slot : ArtifactUtils::allWornSlots())
  835. {
  836. serializeJsonSlot(handler, slot);
  837. }
  838. std::vector<ArtifactID> backpackTemp;
  839. if(handler.saving)
  840. {
  841. backpackTemp.reserve(artifactsInBackpack.size());
  842. for(const ArtSlotInfo & info : artifactsInBackpack)
  843. backpackTemp.push_back(info.artifact->getTypeId());
  844. }
  845. handler.serializeIdArray(NArtifactPosition::backpack, backpackTemp);
  846. if(!handler.saving)
  847. {
  848. for(const ArtifactID & artifactID : backpackTemp)
  849. {
  850. auto * artifact = ArtifactUtils::createArtifact(artifactID);
  851. auto slot = ArtifactPosition::BACKPACK_START + artifactsInBackpack.size();
  852. if(artifact->getType()->canBePutAt(this, slot))
  853. {
  854. auto artsMap = putArtifact(slot, artifact);
  855. artifact->addPlacementMap(artsMap);
  856. }
  857. }
  858. }
  859. }
  860. void CArtifactSet::serializeJsonCreature(JsonSerializeFormat & handler)
  861. {
  862. logGlobal->error("CArtifactSet::serializeJsonCreature not implemented");
  863. }
  864. void CArtifactSet::serializeJsonCommander(JsonSerializeFormat & handler)
  865. {
  866. logGlobal->error("CArtifactSet::serializeJsonCommander not implemented");
  867. }
  868. void CArtifactSet::serializeJsonSlot(JsonSerializeFormat & handler, const ArtifactPosition & slot)
  869. {
  870. ArtifactID artifactID;
  871. if(handler.saving)
  872. {
  873. const ArtSlotInfo * info = getSlot(slot);
  874. if(info != nullptr && !info->locked)
  875. {
  876. artifactID = info->artifact->getTypeId();
  877. handler.serializeId(NArtifactPosition::namesHero[slot.num], artifactID, ArtifactID::NONE);
  878. }
  879. }
  880. else
  881. {
  882. handler.serializeId(NArtifactPosition::namesHero[slot.num], artifactID, ArtifactID::NONE);
  883. if(artifactID != ArtifactID::NONE)
  884. {
  885. auto * artifact = ArtifactUtils::createArtifact(artifactID.toEnum());
  886. if(artifact->getType()->canBePutAt(this, slot))
  887. {
  888. auto artsMap = putArtifact(slot, artifact);
  889. artifact->addPlacementMap(artsMap);
  890. }
  891. else
  892. {
  893. logGlobal->debug("Artifact can't be put at the specified location."); //TODO add more debugging information
  894. }
  895. }
  896. }
  897. }
  898. CArtifactFittingSet::CArtifactFittingSet(ArtBearer::ArtBearer bearer)
  899. : bearer(bearer)
  900. {
  901. }
  902. CArtifactFittingSet::CArtifactFittingSet(const CArtifactSet & artSet)
  903. : CArtifactFittingSet(artSet.bearerType())
  904. {
  905. artifactsWorn = artSet.artifactsWorn;
  906. artifactsInBackpack = artSet.artifactsInBackpack;
  907. artifactsTransitionPos = artSet.artifactsTransitionPos;
  908. }
  909. ArtBearer::ArtBearer CArtifactFittingSet::bearerType() const
  910. {
  911. return this->bearer;
  912. }
  913. VCMI_LIB_NAMESPACE_END