CArtHandler.cpp 28 KB

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