CArtHandler.cpp 28 KB

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