CArtHandler.cpp 28 KB

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