CArtHandler.cpp 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079
  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->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->artifact && (!excludeLocked || !si->locked))
  601. return si->artifact;
  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->artifact && (!excludeLocked || !si->locked))
  610. return si->artifact->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.artifact->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.artifact->getId() == artInstId)
  638. return i.second.artifact;
  639. for(const auto & i : artifactsInBackpack)
  640. if(i.artifact->getId() == artInstId)
  641. return i.artifact;
  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 * slotInfo;
  675. if(targetSlot == ArtifactPosition::TRANSITION_POS)
  676. {
  677. slotInfo = &artifactsTransitionPos;
  678. }
  679. else if(ArtifactUtils::isSlotEquipment(targetSlot))
  680. {
  681. slotInfo = &artifactsWorn[targetSlot];
  682. }
  683. else
  684. {
  685. auto position = artifactsInBackpack.begin() + targetSlot - ArtifactPosition::BACKPACK_START;
  686. slotInfo = &(*artifactsInBackpack.emplace(position));
  687. }
  688. slotInfo->artifact = targetArt;
  689. slotInfo->locked = locked;
  690. };
  691. putToSlot(slot, art, false);
  692. if(art->getType()->isCombined() && ArtifactUtils::isSlotEquipment(slot))
  693. {
  694. const CArtifactInstance * mainPart = nullptr;
  695. for(const auto & part : art->getPartsInfo())
  696. if(vstd::contains(part.art->getType()->getPossibleSlots().at(bearerType()), slot)
  697. && (part.slot == ArtifactPosition::PRE_FIRST))
  698. {
  699. mainPart = part.art;
  700. break;
  701. }
  702. for(const auto & part : art->getPartsInfo())
  703. {
  704. if(part.art != mainPart)
  705. {
  706. auto partSlot = part.slot;
  707. if(!part.art->getType()->canBePutAt(this, partSlot))
  708. partSlot = ArtifactUtils::getArtAnyPosition(this, part.art->getTypeId());
  709. assert(ArtifactUtils::isSlotEquipment(partSlot));
  710. putToSlot(partSlot, part.art, true);
  711. resArtPlacement.emplace(part.art, partSlot);
  712. }
  713. else
  714. {
  715. resArtPlacement.emplace(part.art, part.slot);
  716. }
  717. }
  718. }
  719. return resArtPlacement;
  720. }
  721. void CArtifactSet::removeArtifact(const ArtifactPosition & slot)
  722. {
  723. const auto eraseArtSlot = [this](const ArtifactPosition & slotForErase)
  724. {
  725. if(slotForErase == ArtifactPosition::TRANSITION_POS)
  726. {
  727. artifactsTransitionPos.artifact = nullptr;
  728. }
  729. else if(ArtifactUtils::isSlotBackpack(slotForErase))
  730. {
  731. auto backpackSlot = ArtifactPosition(slotForErase - ArtifactPosition::BACKPACK_START);
  732. assert(artifactsInBackpack.begin() + backpackSlot < artifactsInBackpack.end());
  733. artifactsInBackpack.erase(artifactsInBackpack.begin() + backpackSlot);
  734. }
  735. else
  736. {
  737. artifactsWorn.erase(slotForErase);
  738. }
  739. };
  740. if(const auto art = getArt(slot, false))
  741. {
  742. if(art->isCombined())
  743. {
  744. for(const auto & part : art->getPartsInfo())
  745. {
  746. if(part.slot != ArtifactPosition::PRE_FIRST)
  747. {
  748. assert(getArt(part.slot, false));
  749. assert(getArt(part.slot, false) == part.art);
  750. }
  751. eraseArtSlot(part.slot);
  752. }
  753. }
  754. eraseArtSlot(slot);
  755. }
  756. }
  757. const CArtifactInstance * CArtifactSet::getCombinedArtWithPart(const ArtifactID & partId) const
  758. {
  759. for(const auto & slot : artifactsInBackpack)
  760. {
  761. auto art = slot.artifact;
  762. if(art->isCombined())
  763. {
  764. for(auto & ci : art->getPartsInfo())
  765. {
  766. if(ci.art->getTypeId() == partId)
  767. return art;
  768. }
  769. }
  770. }
  771. return nullptr;
  772. }
  773. const ArtSlotInfo * CArtifactSet::getSlot(const ArtifactPosition & pos) const
  774. {
  775. if(pos == ArtifactPosition::TRANSITION_POS)
  776. return &artifactsTransitionPos;
  777. if(vstd::contains(artifactsWorn, pos))
  778. return &artifactsWorn.at(pos);
  779. if(ArtifactUtils::isSlotBackpack(pos))
  780. {
  781. auto backpackPos = pos - ArtifactPosition::BACKPACK_START;
  782. if(backpackPos < 0 || backpackPos >= artifactsInBackpack.size())
  783. return nullptr;
  784. else
  785. return &artifactsInBackpack[backpackPos];
  786. }
  787. return nullptr;
  788. }
  789. void CArtifactSet::lockSlot(const ArtifactPosition & pos)
  790. {
  791. if(pos == ArtifactPosition::TRANSITION_POS)
  792. artifactsTransitionPos.locked = true;
  793. else if(ArtifactUtils::isSlotEquipment(pos))
  794. artifactsWorn[pos].locked = true;
  795. else
  796. {
  797. assert(artifactsInBackpack.size() > pos - ArtifactPosition::BACKPACK_START);
  798. (artifactsInBackpack.begin() + pos - ArtifactPosition::BACKPACK_START)->locked = true;
  799. }
  800. }
  801. bool CArtifactSet::isPositionFree(const ArtifactPosition & pos, bool onlyLockCheck) const
  802. {
  803. if(bearerType() == ArtBearer::ALTAR)
  804. return artifactsInBackpack.size() < GameConstants::ALTAR_ARTIFACTS_SLOTS;
  805. if(const ArtSlotInfo *s = getSlot(pos))
  806. return (onlyLockCheck || !s->artifact) && !s->locked;
  807. return true; //no slot means not used
  808. }
  809. void CArtifactSet::artDeserializationFix(CBonusSystemNode *node)
  810. {
  811. for(auto & elem : artifactsWorn)
  812. if(elem.second.artifact && !elem.second.locked)
  813. node->attachToSource(*elem.second.artifact);
  814. }
  815. void CArtifactSet::serializeJsonArtifacts(JsonSerializeFormat & handler, const std::string & fieldName, CMap * map)
  816. {
  817. //todo: creature and commander artifacts
  818. if(handler.saving && artifactsInBackpack.empty() && artifactsWorn.empty())
  819. return;
  820. if(!handler.saving)
  821. {
  822. artifactsInBackpack.clear();
  823. artifactsWorn.clear();
  824. }
  825. auto s = handler.enterStruct(fieldName);
  826. switch(bearerType())
  827. {
  828. case ArtBearer::HERO:
  829. serializeJsonHero(handler, map);
  830. break;
  831. case ArtBearer::CREATURE:
  832. serializeJsonCreature(handler);
  833. break;
  834. case ArtBearer::COMMANDER:
  835. serializeJsonCommander(handler);
  836. break;
  837. default:
  838. assert(false);
  839. break;
  840. }
  841. }
  842. void CArtifactSet::serializeJsonHero(JsonSerializeFormat & handler, CMap * map)
  843. {
  844. for(const auto & slot : ArtifactUtils::allWornSlots())
  845. {
  846. serializeJsonSlot(handler, slot, map);
  847. }
  848. std::vector<ArtifactID> backpackTemp;
  849. if(handler.saving)
  850. {
  851. backpackTemp.reserve(artifactsInBackpack.size());
  852. for(const ArtSlotInfo & info : artifactsInBackpack)
  853. backpackTemp.push_back(info.artifact->getTypeId());
  854. }
  855. handler.serializeIdArray(NArtifactPosition::backpack, backpackTemp);
  856. if(!handler.saving)
  857. {
  858. for(const ArtifactID & artifactID : backpackTemp)
  859. {
  860. auto * artifact = map->createArtifact(artifactID);
  861. auto slot = ArtifactPosition::BACKPACK_START + artifactsInBackpack.size();
  862. if(artifact->getType()->canBePutAt(this, slot))
  863. {
  864. auto artsMap = putArtifact(slot, artifact);
  865. artifact->addPlacementMap(artsMap);
  866. }
  867. }
  868. }
  869. }
  870. void CArtifactSet::serializeJsonCreature(JsonSerializeFormat & handler)
  871. {
  872. logGlobal->error("CArtifactSet::serializeJsonCreature not implemented");
  873. }
  874. void CArtifactSet::serializeJsonCommander(JsonSerializeFormat & handler)
  875. {
  876. logGlobal->error("CArtifactSet::serializeJsonCommander not implemented");
  877. }
  878. void CArtifactSet::serializeJsonSlot(JsonSerializeFormat & handler, const ArtifactPosition & slot, CMap * map)
  879. {
  880. ArtifactID artifactID;
  881. if(handler.saving)
  882. {
  883. const ArtSlotInfo * info = getSlot(slot);
  884. if(info != nullptr && !info->locked)
  885. {
  886. artifactID = info->artifact->getTypeId();
  887. handler.serializeId(NArtifactPosition::namesHero[slot.num], artifactID, ArtifactID::NONE);
  888. }
  889. }
  890. else
  891. {
  892. handler.serializeId(NArtifactPosition::namesHero[slot.num], artifactID, ArtifactID::NONE);
  893. if(artifactID != ArtifactID::NONE)
  894. {
  895. auto * artifact = map->createArtifact(artifactID.toEnum());
  896. if(artifact->getType()->canBePutAt(this, slot))
  897. {
  898. auto artsMap = putArtifact(slot, artifact);
  899. artifact->addPlacementMap(artsMap);
  900. }
  901. else
  902. {
  903. logGlobal->debug("Artifact can't be put at the specified location."); //TODO add more debugging information
  904. }
  905. }
  906. }
  907. }
  908. CArtifactFittingSet::CArtifactFittingSet(ArtBearer::ArtBearer bearer)
  909. : bearer(bearer)
  910. {
  911. }
  912. CArtifactFittingSet::CArtifactFittingSet(const CArtifactSet & artSet)
  913. : CArtifactFittingSet(artSet.bearerType())
  914. {
  915. artifactsWorn = artSet.artifactsWorn;
  916. artifactsInBackpack = artSet.artifactsInBackpack;
  917. artifactsTransitionPos = artSet.artifactsTransitionPos;
  918. }
  919. ArtBearer::ArtBearer CArtifactFittingSet::bearerType() const
  920. {
  921. return this->bearer;
  922. }
  923. VCMI_LIB_NAMESPACE_END