CArtHandler.cpp 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070
  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. case ArtifactID::GRAIL:
  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->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 = VLC->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. VLC->generaltexth->registerString(scope, art->getNameTextID(), text["name"]);
  374. VLC->generaltexth->registerString(scope, art->getDescriptionTextID(), text["description"]);
  375. VLC->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. VLC->identifiers()->requestIdentifier("creature", warMachine, [=](si32 id)
  398. {
  399. art->warMachine = CreatureID(id);
  400. //this assumes that creature object is stored before registration
  401. VLC->creh->objects.at(id)->warMachine = art->id;
  402. });
  403. }
  404. VLC->identifiers()->requestIdentifier(scope, "object", "artifact", [=](si32 index)
  405. {
  406. JsonNode conf;
  407. conf.setModScope(scope);
  408. VLC->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. VLC->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. VLC->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() && VLC->engineSettings()->getBoolean(EGameSettings::MODULE_STACK_ARTIFACT))
  568. return true;
  569. if(art->possibleSlots.count(ArtBearer::COMMANDER) && !art->possibleSlots.at(ArtBearer::COMMANDER).empty() && VLC->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. }
  594. CBonusSystemNode::treeHasChanged();
  595. }
  596. CArtifactInstance * CArtifactSet::getArt(const ArtifactPosition & pos, bool excludeLocked) const
  597. {
  598. if(const ArtSlotInfo * si = getSlot(pos))
  599. {
  600. if(si->artifact && (!excludeLocked || !si->locked))
  601. return si->artifact;
  602. }
  603. return nullptr;
  604. }
  605. ArtifactPosition CArtifactSet::getArtPos(const ArtifactID & aid, bool onlyWorn, bool allowLocked) const
  606. {
  607. for(const auto & [slot, slotInfo] : artifactsWorn)
  608. {
  609. if(slotInfo.artifact->getTypeId() == aid && (allowLocked || !slotInfo.locked))
  610. return slot;
  611. }
  612. if(!onlyWorn)
  613. {
  614. size_t backpackPositionIdx = ArtifactPosition::BACKPACK_START;
  615. for(const auto & artInfo : artifactsInBackpack)
  616. {
  617. const auto art = artInfo.getArt();
  618. if(art && art->artType->getId() == aid)
  619. return ArtifactPosition(backpackPositionIdx);
  620. backpackPositionIdx++;
  621. }
  622. }
  623. return ArtifactPosition::PRE_FIRST;
  624. }
  625. const CArtifactInstance * CArtifactSet::getArtByInstanceId(const ArtifactInstanceID & artInstId) const
  626. {
  627. for(const auto & i : artifactsWorn)
  628. if(i.second.artifact->getId() == artInstId)
  629. return i.second.artifact;
  630. for(const auto & i : artifactsInBackpack)
  631. if(i.artifact->getId() == artInstId)
  632. return i.artifact;
  633. return nullptr;
  634. }
  635. ArtifactPosition CArtifactSet::getArtPos(const CArtifactInstance * artInst) const
  636. {
  637. if(artInst)
  638. {
  639. for(const auto & slot : artInst->artType->getPossibleSlots().at(bearerType()))
  640. if(getArt(slot) == artInst)
  641. return slot;
  642. ArtifactPosition backpackSlot = ArtifactPosition::BACKPACK_START;
  643. for(auto & slotInfo : artifactsInBackpack)
  644. {
  645. if(slotInfo.getArt() == artInst)
  646. return backpackSlot;
  647. backpackSlot = ArtifactPosition(backpackSlot + 1);
  648. }
  649. }
  650. return ArtifactPosition::PRE_FIRST;
  651. }
  652. bool CArtifactSet::hasArt(const ArtifactID & aid, bool onlyWorn, bool searchCombinedParts) const
  653. {
  654. if(searchCombinedParts && getCombinedArtWithPart(aid))
  655. return true;
  656. if(getArtPos(aid, onlyWorn, searchCombinedParts) != ArtifactPosition::PRE_FIRST)
  657. return true;
  658. return false;
  659. }
  660. CArtifactSet::ArtPlacementMap CArtifactSet::putArtifact(const ArtifactPosition & slot, CArtifactInstance * art)
  661. {
  662. ArtPlacementMap resArtPlacement;
  663. const auto putToSlot = [this](const ArtifactPosition & targetSlot, CArtifactInstance * targetArt, bool locked)
  664. {
  665. ArtSlotInfo * slotInfo;
  666. if(targetSlot == ArtifactPosition::TRANSITION_POS)
  667. {
  668. slotInfo = &artifactsTransitionPos;
  669. }
  670. else if(ArtifactUtils::isSlotEquipment(targetSlot))
  671. {
  672. slotInfo = &artifactsWorn[targetSlot];
  673. }
  674. else
  675. {
  676. auto position = artifactsInBackpack.begin() + targetSlot - ArtifactPosition::BACKPACK_START;
  677. slotInfo = &(*artifactsInBackpack.emplace(position));
  678. }
  679. slotInfo->artifact = targetArt;
  680. slotInfo->locked = locked;
  681. };
  682. putToSlot(slot, art, false);
  683. if(art->artType->isCombined() && ArtifactUtils::isSlotEquipment(slot))
  684. {
  685. const CArtifactInstance * mainPart = nullptr;
  686. for(const auto & part : art->getPartsInfo())
  687. if(vstd::contains(part.art->artType->getPossibleSlots().at(bearerType()), slot)
  688. && (part.slot == ArtifactPosition::PRE_FIRST))
  689. {
  690. mainPart = part.art;
  691. break;
  692. }
  693. for(const auto & part : art->getPartsInfo())
  694. {
  695. if(part.art != mainPart)
  696. {
  697. auto partSlot = part.slot;
  698. if(!part.art->artType->canBePutAt(this, partSlot))
  699. partSlot = ArtifactUtils::getArtAnyPosition(this, part.art->getTypeId());
  700. assert(ArtifactUtils::isSlotEquipment(partSlot));
  701. putToSlot(partSlot, part.art, true);
  702. resArtPlacement.emplace(part.art, partSlot);
  703. }
  704. else
  705. {
  706. resArtPlacement.emplace(part.art, part.slot);
  707. }
  708. }
  709. }
  710. return resArtPlacement;
  711. }
  712. void CArtifactSet::removeArtifact(const ArtifactPosition & slot)
  713. {
  714. const auto eraseArtSlot = [this](const ArtifactPosition & slotForErase)
  715. {
  716. if(slotForErase == ArtifactPosition::TRANSITION_POS)
  717. {
  718. artifactsTransitionPos.artifact = nullptr;
  719. }
  720. else if(ArtifactUtils::isSlotBackpack(slotForErase))
  721. {
  722. auto backpackSlot = ArtifactPosition(slotForErase - ArtifactPosition::BACKPACK_START);
  723. assert(artifactsInBackpack.begin() + backpackSlot < artifactsInBackpack.end());
  724. artifactsInBackpack.erase(artifactsInBackpack.begin() + backpackSlot);
  725. }
  726. else
  727. {
  728. artifactsWorn.erase(slotForErase);
  729. }
  730. };
  731. if(const auto art = getArt(slot, false))
  732. {
  733. if(art->isCombined())
  734. {
  735. for(const auto & part : art->getPartsInfo())
  736. {
  737. if(part.slot != ArtifactPosition::PRE_FIRST)
  738. {
  739. assert(getArt(part.slot, false));
  740. assert(getArt(part.slot, false) == part.art);
  741. }
  742. eraseArtSlot(part.slot);
  743. }
  744. }
  745. eraseArtSlot(slot);
  746. }
  747. }
  748. const CArtifactInstance * CArtifactSet::getCombinedArtWithPart(const ArtifactID & partId) const
  749. {
  750. for(const auto & slot : artifactsInBackpack)
  751. {
  752. auto art = slot.artifact;
  753. if(art->isCombined())
  754. {
  755. for(auto & ci : art->getPartsInfo())
  756. {
  757. if(ci.art->getTypeId() == partId)
  758. return art;
  759. }
  760. }
  761. }
  762. return nullptr;
  763. }
  764. const ArtSlotInfo * CArtifactSet::getSlot(const ArtifactPosition & pos) const
  765. {
  766. if(pos == ArtifactPosition::TRANSITION_POS)
  767. return &artifactsTransitionPos;
  768. if(vstd::contains(artifactsWorn, pos))
  769. return &artifactsWorn.at(pos);
  770. if(ArtifactUtils::isSlotBackpack(pos))
  771. {
  772. auto backpackPos = pos - ArtifactPosition::BACKPACK_START;
  773. if(backpackPos < 0 || backpackPos >= artifactsInBackpack.size())
  774. return nullptr;
  775. else
  776. return &artifactsInBackpack[backpackPos];
  777. }
  778. return nullptr;
  779. }
  780. void CArtifactSet::lockSlot(const ArtifactPosition & pos)
  781. {
  782. if(pos == ArtifactPosition::TRANSITION_POS)
  783. artifactsTransitionPos.locked = true;
  784. else if(ArtifactUtils::isSlotEquipment(pos))
  785. artifactsWorn[pos].locked = true;
  786. else
  787. {
  788. assert(artifactsInBackpack.size() > pos - ArtifactPosition::BACKPACK_START);
  789. (artifactsInBackpack.begin() + pos - ArtifactPosition::BACKPACK_START)->locked = true;
  790. }
  791. }
  792. bool CArtifactSet::isPositionFree(const ArtifactPosition & pos, bool onlyLockCheck) const
  793. {
  794. if(bearerType() == ArtBearer::ALTAR)
  795. return artifactsInBackpack.size() < GameConstants::ALTAR_ARTIFACTS_SLOTS;
  796. if(const ArtSlotInfo *s = getSlot(pos))
  797. return (onlyLockCheck || !s->artifact) && !s->locked;
  798. return true; //no slot means not used
  799. }
  800. void CArtifactSet::artDeserializationFix(CBonusSystemNode *node)
  801. {
  802. for(auto & elem : artifactsWorn)
  803. if(elem.second.artifact && !elem.second.locked)
  804. node->attachTo(*elem.second.artifact);
  805. }
  806. void CArtifactSet::serializeJsonArtifacts(JsonSerializeFormat & handler, const std::string & fieldName)
  807. {
  808. //todo: creature and commander artifacts
  809. if(handler.saving && artifactsInBackpack.empty() && artifactsWorn.empty())
  810. return;
  811. if(!handler.saving)
  812. {
  813. artifactsInBackpack.clear();
  814. artifactsWorn.clear();
  815. }
  816. auto s = handler.enterStruct(fieldName);
  817. switch(bearerType())
  818. {
  819. case ArtBearer::HERO:
  820. serializeJsonHero(handler);
  821. break;
  822. case ArtBearer::CREATURE:
  823. serializeJsonCreature(handler);
  824. break;
  825. case ArtBearer::COMMANDER:
  826. serializeJsonCommander(handler);
  827. break;
  828. default:
  829. assert(false);
  830. break;
  831. }
  832. }
  833. void CArtifactSet::serializeJsonHero(JsonSerializeFormat & handler)
  834. {
  835. for(const auto & slot : ArtifactUtils::allWornSlots())
  836. {
  837. serializeJsonSlot(handler, slot);
  838. }
  839. std::vector<ArtifactID> backpackTemp;
  840. if(handler.saving)
  841. {
  842. backpackTemp.reserve(artifactsInBackpack.size());
  843. for(const ArtSlotInfo & info : artifactsInBackpack)
  844. backpackTemp.push_back(info.artifact->getTypeId());
  845. }
  846. handler.serializeIdArray(NArtifactPosition::backpack, backpackTemp);
  847. if(!handler.saving)
  848. {
  849. for(const ArtifactID & artifactID : backpackTemp)
  850. {
  851. auto * artifact = ArtifactUtils::createArtifact(artifactID);
  852. auto slot = ArtifactPosition::BACKPACK_START + artifactsInBackpack.size();
  853. if(artifact->artType->canBePutAt(this, slot))
  854. {
  855. auto artsMap = putArtifact(slot, artifact);
  856. artifact->addPlacementMap(artsMap);
  857. }
  858. }
  859. }
  860. }
  861. void CArtifactSet::serializeJsonCreature(JsonSerializeFormat & handler)
  862. {
  863. logGlobal->error("CArtifactSet::serializeJsonCreature not implemented");
  864. }
  865. void CArtifactSet::serializeJsonCommander(JsonSerializeFormat & handler)
  866. {
  867. logGlobal->error("CArtifactSet::serializeJsonCommander not implemented");
  868. }
  869. void CArtifactSet::serializeJsonSlot(JsonSerializeFormat & handler, const ArtifactPosition & slot)
  870. {
  871. ArtifactID artifactID;
  872. if(handler.saving)
  873. {
  874. const ArtSlotInfo * info = getSlot(slot);
  875. if(info != nullptr && !info->locked)
  876. {
  877. artifactID = info->artifact->getTypeId();
  878. handler.serializeId(NArtifactPosition::namesHero[slot.num], artifactID, ArtifactID::NONE);
  879. }
  880. }
  881. else
  882. {
  883. handler.serializeId(NArtifactPosition::namesHero[slot.num], artifactID, ArtifactID::NONE);
  884. if(artifactID != ArtifactID::NONE)
  885. {
  886. auto * artifact = ArtifactUtils::createArtifact(artifactID.toEnum());
  887. if(artifact->artType->canBePutAt(this, slot))
  888. {
  889. auto artsMap = putArtifact(slot, artifact);
  890. artifact->addPlacementMap(artsMap);
  891. }
  892. else
  893. {
  894. logGlobal->debug("Artifact can't be put at the specified location."); //TODO add more debugging information
  895. }
  896. }
  897. }
  898. }
  899. CArtifactFittingSet::CArtifactFittingSet(ArtBearer::ArtBearer bearer)
  900. : bearer(bearer)
  901. {
  902. }
  903. CArtifactFittingSet::CArtifactFittingSet(const CArtifactSet & artSet)
  904. : CArtifactFittingSet(artSet.bearerType())
  905. {
  906. artifactsWorn = artSet.artifactsWorn;
  907. artifactsInBackpack = artSet.artifactsInBackpack;
  908. artifactsTransitionPos = artSet.artifactsTransitionPos;
  909. }
  910. ArtBearer::ArtBearer CArtifactFittingSet::bearerType() const
  911. {
  912. return this->bearer;
  913. }
  914. VCMI_LIB_NAMESPACE_END