CArtHandler.cpp 32 KB

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