CArtHandler.cpp 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206
  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 "filesystem/Filesystem.h"
  13. #include "CGeneralTextHandler.h"
  14. #include "VCMI_Lib.h"
  15. #include "CModHandler.h"
  16. #include "CSpellHandler.h"
  17. #include "mapObjects/MapObjects.h"
  18. #include "NetPacksBase.h"
  19. #include "GameConstants.h"
  20. #include "CRandomGenerator.h"
  21. #include "mapObjects/CObjectClassesHandler.h"
  22. using namespace boost::assign;
  23. // Note: list must match entries in ArtTraits.txt
  24. #define ART_POS_LIST \
  25. ART_POS(SPELLBOOK) \
  26. ART_POS(MACH4) \
  27. ART_POS(MACH3) \
  28. ART_POS(MACH2) \
  29. ART_POS(MACH1) \
  30. ART_POS(MISC5) \
  31. ART_POS(MISC4) \
  32. ART_POS(MISC3) \
  33. ART_POS(MISC2) \
  34. ART_POS(MISC1) \
  35. ART_POS(FEET) \
  36. ART_POS(LEFT_RING) \
  37. ART_POS(RIGHT_RING) \
  38. ART_POS(TORSO) \
  39. ART_POS(LEFT_HAND) \
  40. ART_POS(RIGHT_HAND) \
  41. ART_POS(NECK) \
  42. ART_POS(SHOULDERS) \
  43. ART_POS(HEAD);
  44. const std::string & CArtifact::Name() const
  45. {
  46. return name;
  47. }
  48. const std::string & CArtifact::Description() const
  49. {
  50. return description;
  51. }
  52. const std::string & CArtifact::EventText() const
  53. {
  54. return eventText;
  55. }
  56. bool CArtifact::isBig () const
  57. {
  58. return VLC->arth->isBigArtifact(id);
  59. }
  60. CArtifact::CArtifact()
  61. {
  62. setNodeType(ARTIFACT);
  63. possibleSlots[ArtBearer::HERO]; //we want to generate map entry even if it will be empty
  64. possibleSlots[ArtBearer::CREATURE]; //we want to generate map entry even if it will be empty
  65. possibleSlots[ArtBearer::COMMANDER];
  66. }
  67. CArtifact::~CArtifact()
  68. {
  69. }
  70. int CArtifact::getArtClassSerial() const
  71. {
  72. if(id == 1)
  73. return 4;
  74. switch(aClass)
  75. {
  76. case ART_TREASURE:
  77. return 0;
  78. case ART_MINOR:
  79. return 1;
  80. case ART_MAJOR:
  81. return 2;
  82. case ART_RELIC:
  83. return 3;
  84. case ART_SPECIAL:
  85. return 5;
  86. }
  87. return -1;
  88. }
  89. std::string CArtifact::nodeName() const
  90. {
  91. return "Artifact: " + Name();
  92. }
  93. void CArtifact::addNewBonus(Bonus *b)
  94. {
  95. b->source = Bonus::ARTIFACT;
  96. b->duration = Bonus::PERMANENT;
  97. b->description = name;
  98. CBonusSystemNode::addNewBonus(b);
  99. }
  100. void CGrowingArtifact::levelUpArtifact (CArtifactInstance * art)
  101. {
  102. Bonus b;
  103. b.type = Bonus::LEVEL_COUNTER;
  104. b.val = 1;
  105. b.duration = Bonus::COMMANDER_KILLED;
  106. art->accumulateBonus (b);
  107. for (auto bonus : bonusesPerLevel)
  108. {
  109. if (art->valOfBonuses(Bonus::LEVEL_COUNTER) % bonus.first == 0) //every n levels
  110. {
  111. art->accumulateBonus (bonus.second);
  112. }
  113. }
  114. for (auto bonus : thresholdBonuses)
  115. {
  116. if (art->valOfBonuses(Bonus::LEVEL_COUNTER) == bonus.first) //every n levels
  117. {
  118. art->addNewBonus (&bonus.second);
  119. }
  120. }
  121. }
  122. CArtHandler::CArtHandler()
  123. {
  124. //VLC->arth = this;
  125. // War machines are the default big artifacts.
  126. for (ArtifactID i = ArtifactID::CATAPULT; i <= ArtifactID::FIRST_AID_TENT; i.advance(1))
  127. bigArtifacts.insert(i);
  128. }
  129. CArtHandler::~CArtHandler()
  130. {
  131. for(CArtifact * art : artifacts)
  132. delete art;
  133. }
  134. std::vector<JsonNode> CArtHandler::loadLegacyData(size_t dataSize)
  135. {
  136. artifacts.resize(dataSize);
  137. std::vector<JsonNode> h3Data;
  138. h3Data.reserve(dataSize);
  139. #define ART_POS(x) ( #x)
  140. const std::vector<std::string> artSlots = boost::assign::list_of ART_POS_LIST;
  141. #undef ART_POS
  142. static std::map<char, std::string> classes =
  143. map_list_of('S',"SPECIAL")('T',"TREASURE")('N',"MINOR")('J',"MAJOR")('R',"RELIC");
  144. CLegacyConfigParser parser("DATA/ARTRAITS.TXT");
  145. CLegacyConfigParser events("DATA/ARTEVENT.TXT");
  146. parser.endLine(); // header
  147. parser.endLine();
  148. for (size_t i = 0; i < dataSize; i++)
  149. {
  150. JsonNode artData;
  151. artData["text"]["name"].String() = parser.readString();
  152. artData["text"]["event"].String() = events.readString();
  153. artData["value"].Float() = parser.readNumber();
  154. for(auto & artSlot : artSlots)
  155. {
  156. if(parser.readString() == "x")
  157. {
  158. artData["slot"].Vector().push_back(JsonNode());
  159. artData["slot"].Vector().back().String() = artSlot;
  160. }
  161. }
  162. artData["class"].String() = classes[parser.readString()[0]];
  163. artData["text"]["description"].String() = parser.readString();
  164. parser.endLine();
  165. events.endLine();
  166. h3Data.push_back(artData);
  167. }
  168. return h3Data;
  169. }
  170. void CArtHandler::loadObject(std::string scope, std::string name, const JsonNode & data)
  171. {
  172. auto object = loadFromJson(data);
  173. object->id = ArtifactID(artifacts.size());
  174. object->iconIndex = object->id + 5;
  175. artifacts.push_back(object);
  176. VLC->modh->identifiers.registerObject(scope, "artifact", name, object->id);
  177. }
  178. void CArtHandler::loadObject(std::string scope, std::string name, const JsonNode & data, size_t index)
  179. {
  180. auto object = loadFromJson(data);
  181. object->id = ArtifactID(index);
  182. object->iconIndex = object->id;
  183. assert(artifacts[index] == nullptr); // ensure that this id was not loaded before
  184. artifacts[index] = object;
  185. VLC->modh->identifiers.registerObject(scope, "artifact", name, object->id);
  186. }
  187. CArtifact * CArtHandler::loadFromJson(const JsonNode & node)
  188. {
  189. CArtifact * art;
  190. if (!VLC->modh->modules.COMMANDERS || node["growing"].isNull())
  191. art = new CArtifact();
  192. else
  193. {
  194. auto growing = new CGrowingArtifact();
  195. loadGrowingArt(growing, node);
  196. art = growing;
  197. }
  198. const JsonNode & text = node["text"];
  199. art->name = text["name"].String();
  200. art->description = text["description"].String();
  201. art->eventText = text["event"].String();
  202. const JsonNode & graphics = node["graphics"];
  203. art->image = graphics["image"].String();
  204. if (!graphics["large"].isNull())
  205. art->large = graphics["large"].String();
  206. else
  207. art->large = art->image;
  208. art->advMapDef = graphics["map"].String();
  209. art->price = node["value"].Float();
  210. loadSlots(art, node);
  211. loadClass(art, node);
  212. loadType(art, node);
  213. loadComponents(art, node);
  214. for (auto b : node["bonuses"].Vector())
  215. {
  216. auto bonus = JsonUtils::parseBonus (b);
  217. art->addNewBonus(bonus);
  218. }
  219. return art;
  220. }
  221. ArtifactPosition CArtHandler::stringToSlot(std::string slotName)
  222. {
  223. #define ART_POS(x) ( #x, ArtifactPosition::x )
  224. static const std::map<std::string, ArtifactPosition> artifactPositionMap = boost::assign::map_list_of ART_POS_LIST;
  225. #undef ART_POS
  226. auto it = artifactPositionMap.find (slotName);
  227. if (it != artifactPositionMap.end())
  228. return it->second;
  229. logGlobal->warnStream() << "Warning! Artifact slot " << slotName << " not recognized!";
  230. return ArtifactPosition::PRE_FIRST;
  231. }
  232. void CArtHandler::addSlot(CArtifact * art, const std::string & slotID)
  233. {
  234. if (slotID == "MISC")
  235. {
  236. art->possibleSlots[ArtBearer::HERO] += ArtifactPosition::MISC1, ArtifactPosition::MISC2, ArtifactPosition::MISC3, ArtifactPosition::MISC4, ArtifactPosition::MISC5;
  237. }
  238. else if (slotID == "RING")
  239. {
  240. art->possibleSlots[ArtBearer::HERO] += ArtifactPosition::LEFT_RING, ArtifactPosition::RIGHT_RING;
  241. }
  242. else
  243. {
  244. auto slot = stringToSlot(slotID);
  245. if (slot != ArtifactPosition::PRE_FIRST)
  246. art->possibleSlots[ArtBearer::HERO].push_back (slot);
  247. }
  248. }
  249. void CArtHandler::loadSlots(CArtifact * art, const JsonNode & node)
  250. {
  251. if (!node["slot"].isNull()) //we assume non-hero slots are irrelevant?
  252. {
  253. if (node["slot"].getType() == JsonNode::DATA_STRING)
  254. addSlot(art, node["slot"].String());
  255. else
  256. {
  257. for (const JsonNode & slot : node["slot"].Vector())
  258. addSlot(art, slot.String());
  259. }
  260. }
  261. }
  262. CArtifact::EartClass CArtHandler::stringToClass(std::string className)
  263. {
  264. static const std::map<std::string, CArtifact::EartClass> artifactClassMap = boost::assign::map_list_of
  265. ("TREASURE", CArtifact::ART_TREASURE)
  266. ("MINOR", CArtifact::ART_MINOR)
  267. ("MAJOR", CArtifact::ART_MAJOR)
  268. ("RELIC", CArtifact::ART_RELIC)
  269. ("SPECIAL", CArtifact::ART_SPECIAL);
  270. auto it = artifactClassMap.find (className);
  271. if (it != artifactClassMap.end())
  272. return it->second;
  273. logGlobal->warnStream() << "Warning! Artifact rarity " << className << " not recognized!";
  274. return CArtifact::ART_SPECIAL;
  275. }
  276. void CArtHandler::loadClass(CArtifact * art, const JsonNode & node)
  277. {
  278. art->aClass = stringToClass(node["class"].String());
  279. }
  280. void CArtHandler::loadType(CArtifact * art, const JsonNode & node)
  281. {
  282. #define ART_BEARER(x) ( #x, ArtBearer::x )
  283. static const std::map<std::string, int> artifactBearerMap = boost::assign::map_list_of ART_BEARER_LIST;
  284. #undef ART_BEARER
  285. for (const JsonNode & b : node["type"].Vector())
  286. {
  287. auto it = artifactBearerMap.find (b.String());
  288. if (it != artifactBearerMap.end())
  289. {
  290. int bearerType = it->second;
  291. switch (bearerType)
  292. {
  293. case ArtBearer::HERO://TODO: allow arts having several possible bearers
  294. break;
  295. case ArtBearer::COMMANDER:
  296. makeItCommanderArt (art); //original artifacts should have only one bearer type
  297. break;
  298. case ArtBearer::CREATURE:
  299. makeItCreatureArt (art);
  300. break;
  301. }
  302. }
  303. else
  304. logGlobal->warnStream() << "Warning! Artifact type " << b.String() << " not recognized!";
  305. }
  306. }
  307. void CArtHandler::loadComponents(CArtifact * art, const JsonNode & node)
  308. {
  309. if (!node["components"].isNull())
  310. {
  311. art->constituents.reset(new std::vector<CArtifact *>());
  312. for (auto component : node["components"].Vector())
  313. {
  314. VLC->modh->identifiers.requestIdentifier("artifact", component, [=](si32 id)
  315. {
  316. // when this code is called both combinational art as well as component are loaded
  317. // so it is safe to access any of them
  318. art->constituents->push_back(VLC->arth->artifacts[id]);
  319. VLC->arth->artifacts[id]->constituentOf.push_back(art);
  320. });
  321. }
  322. }
  323. }
  324. void CArtHandler::loadGrowingArt(CGrowingArtifact * art, const JsonNode & node)
  325. {
  326. for (auto b : node["growing"]["bonusesPerLevel"].Vector())
  327. {
  328. art->bonusesPerLevel.push_back (std::pair <ui16, Bonus> (b["level"].Float(), *JsonUtils::parseBonus (b["bonus"])));
  329. }
  330. for (auto b : node["growing"]["thresholdBonuses"].Vector())
  331. {
  332. art->thresholdBonuses.push_back (std::pair <ui16, Bonus> (b["level"].Float(), *JsonUtils::parseBonus (b["bonus"])));
  333. }
  334. }
  335. //TODO: use bimap
  336. ArtifactID CArtHandler::creatureToMachineID(CreatureID id)
  337. {
  338. switch (id)
  339. {
  340. case CreatureID::CATAPULT: //Catapult
  341. return ArtifactID::CATAPULT;
  342. break;
  343. case CreatureID::BALLISTA: //Ballista
  344. return ArtifactID::BALLISTA;
  345. break;
  346. case CreatureID::FIRST_AID_TENT: //First Aid tent
  347. return ArtifactID::FIRST_AID_TENT;
  348. break;
  349. case CreatureID::AMMO_CART: //Ammo cart
  350. return ArtifactID::AMMO_CART;
  351. break;
  352. }
  353. return ArtifactID::NONE; //this creature is not artifact
  354. }
  355. CreatureID CArtHandler::machineIDToCreature(ArtifactID id)
  356. {
  357. switch (id)
  358. {
  359. case ArtifactID::CATAPULT:
  360. return CreatureID::CATAPULT;
  361. break;
  362. case ArtifactID::BALLISTA:
  363. return CreatureID::BALLISTA;
  364. break;
  365. case ArtifactID::FIRST_AID_TENT:
  366. return CreatureID::FIRST_AID_TENT;
  367. break;
  368. case ArtifactID::AMMO_CART:
  369. return CreatureID::AMMO_CART;
  370. break;
  371. }
  372. return CreatureID::NONE; //this artifact is not a creature
  373. }
  374. ArtifactID CArtHandler::pickRandomArtifact(CRandomGenerator & rand, int flags, std::function<bool(ArtifactID)> accepts)
  375. {
  376. auto getAllowedArts = [&](std::vector<ConstTransitivePtr<CArtifact> > &out, std::vector<CArtifact*> *arts, CArtifact::EartClass flag)
  377. {
  378. if (arts->empty()) //restock available arts
  379. fillList(*arts, flag);
  380. for (auto & arts_i : *arts)
  381. {
  382. if (accepts(arts_i->id))
  383. {
  384. CArtifact *art = arts_i;
  385. out.push_back(art);
  386. }
  387. }
  388. };
  389. auto getAllowed = [&](std::vector<ConstTransitivePtr<CArtifact> > &out)
  390. {
  391. if (flags & CArtifact::ART_TREASURE)
  392. getAllowedArts (out, &treasures, CArtifact::ART_TREASURE);
  393. if (flags & CArtifact::ART_MINOR)
  394. getAllowedArts (out, &minors, CArtifact::ART_MINOR);
  395. if (flags & CArtifact::ART_MAJOR)
  396. getAllowedArts (out, &majors, CArtifact::ART_MAJOR);
  397. if (flags & CArtifact::ART_RELIC)
  398. getAllowedArts (out, &relics, CArtifact::ART_RELIC);
  399. if (!out.size()) //no artifact of specified rarity, we need to take another one
  400. {
  401. getAllowedArts (out, &treasures, CArtifact::ART_TREASURE);
  402. getAllowedArts (out, &minors, CArtifact::ART_MINOR);
  403. getAllowedArts (out, &majors, CArtifact::ART_MAJOR);
  404. getAllowedArts (out, &relics, CArtifact::ART_RELIC);
  405. }
  406. if (!out.size()) //no arts are available at all
  407. {
  408. out.resize (64);
  409. std::fill_n (out.begin(), 64, artifacts[2]); //Give Grail - this can't be banned (hopefully)
  410. }
  411. };
  412. std::vector<ConstTransitivePtr<CArtifact> > out;
  413. getAllowed(out);
  414. ArtifactID artID = (*RandomGeneratorUtil::nextItem(out, rand))->id;
  415. erasePickedArt(artID);
  416. return artID;
  417. }
  418. ArtifactID CArtHandler::pickRandomArtifact(CRandomGenerator & rand, std::function<bool(ArtifactID)> accepts)
  419. {
  420. return pickRandomArtifact(rand, 0xff, accepts);
  421. }
  422. ArtifactID CArtHandler::pickRandomArtifact(CRandomGenerator & rand, int flags)
  423. {
  424. return pickRandomArtifact(rand, flags, [](ArtifactID){ return true;});
  425. }
  426. Bonus *createBonus(Bonus::BonusType type, int val, int subtype, Bonus::ValueType valType, shared_ptr<ILimiter> limiter = shared_ptr<ILimiter>(), int additionalInfo = 0)
  427. {
  428. auto added = new Bonus(Bonus::PERMANENT,type,Bonus::ARTIFACT,val,-1,subtype);
  429. added->additionalInfo = additionalInfo;
  430. added->valType = valType;
  431. added->limiter = limiter;
  432. return added;
  433. }
  434. Bonus *createBonus(Bonus::BonusType type, int val, int subtype, shared_ptr<IPropagator> propagator = shared_ptr<IPropagator>(), int additionalInfo = 0)
  435. {
  436. auto added = new Bonus(Bonus::PERMANENT,type,Bonus::ARTIFACT,val,-1,subtype);
  437. added->additionalInfo = additionalInfo;
  438. added->valType = Bonus::BASE_NUMBER;
  439. added->propagator = propagator;
  440. return added;
  441. }
  442. void CArtHandler::giveArtBonus( ArtifactID aid, Bonus::BonusType type, int val, int subtype, Bonus::ValueType valType, shared_ptr<ILimiter> limiter, int additionalInfo)
  443. {
  444. giveArtBonus(aid, createBonus(type, val, subtype, valType, limiter, additionalInfo));
  445. }
  446. void CArtHandler::giveArtBonus(ArtifactID aid, Bonus::BonusType type, int val, int subtype, shared_ptr<IPropagator> propagator /*= nullptr*/, int additionalInfo)
  447. {
  448. giveArtBonus(aid, createBonus(type, val, subtype, propagator, additionalInfo));
  449. }
  450. void CArtHandler::giveArtBonus(ArtifactID aid, Bonus *bonus)
  451. {
  452. bonus->sid = aid;
  453. if(bonus->subtype == Bonus::MORALE || bonus->type == Bonus::LUCK)
  454. bonus->description = artifacts[aid]->Name() + (bonus->val > 0 ? " +" : " ") + boost::lexical_cast<std::string>(bonus->val);
  455. else
  456. bonus->description = artifacts[aid]->Name();
  457. artifacts[aid]->addNewBonus(bonus);
  458. }
  459. void CArtHandler::makeItCreatureArt (CArtifact * a, bool onlyCreature /*=true*/)
  460. {
  461. if (onlyCreature)
  462. {
  463. a->possibleSlots[ArtBearer::HERO].clear();
  464. a->possibleSlots[ArtBearer::COMMANDER].clear();
  465. }
  466. a->possibleSlots[ArtBearer::CREATURE].push_back(ArtifactPosition::CREATURE_SLOT);
  467. }
  468. void CArtHandler::makeItCreatureArt (ArtifactID aid, bool onlyCreature /*=true*/)
  469. {
  470. CArtifact *a = artifacts[aid];
  471. makeItCreatureArt (a, onlyCreature);
  472. }
  473. void CArtHandler::makeItCommanderArt (CArtifact * a, bool onlyCommander /*= true*/ )
  474. {
  475. if (onlyCommander)
  476. {
  477. a->possibleSlots[ArtBearer::HERO].clear();
  478. a->possibleSlots[ArtBearer::CREATURE].clear();
  479. }
  480. for (int i = ArtifactPosition::COMMANDER1; i <= ArtifactPosition::COMMANDER6; ++i)
  481. a->possibleSlots[ArtBearer::COMMANDER].push_back(ArtifactPosition(i));
  482. }
  483. void CArtHandler::makeItCommanderArt( ArtifactID aid, bool onlyCommander /*= true*/ )
  484. {
  485. CArtifact *a = artifacts[aid];
  486. makeItCommanderArt (a, onlyCommander);
  487. }
  488. bool CArtHandler::legalArtifact(ArtifactID id)
  489. {
  490. auto art = artifacts[id];
  491. //assert ( (!art->constituents) || art->constituents->size() ); //artifacts is not combined or has some components
  492. return (art->possibleSlots[ArtBearer::HERO].size() ||
  493. (art->possibleSlots[ArtBearer::COMMANDER].size() && VLC->modh->modules.COMMANDERS) ||
  494. (art->possibleSlots[ArtBearer::CREATURE].size() && VLC->modh->modules.STACK_ARTIFACT)) &&
  495. !(art->constituents); //no combo artifacts spawning
  496. }
  497. void CArtHandler::initAllowedArtifactsList(const std::vector<bool> &allowed)
  498. {
  499. allowedArtifacts.clear();
  500. treasures.clear();
  501. minors.clear();
  502. majors.clear();
  503. relics.clear();
  504. for (ArtifactID i=ArtifactID::SPELLBOOK; i<ArtifactID::ART_SELECTION; i.advance(1))
  505. {
  506. if (allowed[i] && legalArtifact(i))
  507. allowedArtifacts.push_back(artifacts[i]);
  508. }
  509. if (VLC->modh->modules.COMMANDERS) //allow all commander artifacts for testing
  510. {
  511. for (int i = 146; i <= 155; ++i)
  512. {
  513. allowedArtifacts.push_back(artifacts[i]);
  514. }
  515. }
  516. for (int i = GameConstants::ARTIFACTS_QUANTITY; i < artifacts.size(); ++i) //allow all new artifacts by default
  517. {
  518. if (legalArtifact(ArtifactID(i)))
  519. allowedArtifacts.push_back(artifacts[i]);
  520. //keep im mind that artifact can be worn by more than one type of bearer
  521. }
  522. }
  523. std::vector<bool> CArtHandler::getDefaultAllowed() const
  524. {
  525. std::vector<bool> allowedArtifacts;
  526. allowedArtifacts.resize(127, true);
  527. allowedArtifacts.resize(141, false);
  528. allowedArtifacts.resize(GameConstants::ARTIFACTS_QUANTITY, true);
  529. return allowedArtifacts;
  530. }
  531. void CArtHandler::erasePickedArt(ArtifactID id)
  532. {
  533. CArtifact *art = artifacts[id];
  534. if(auto artifactList = listFromClass(art->aClass))
  535. {
  536. if(artifactList->empty())
  537. fillList(*artifactList, art->aClass);
  538. auto itr = vstd::find(*artifactList, art);
  539. if(itr != artifactList->end())
  540. {
  541. artifactList->erase(itr);
  542. }
  543. else
  544. logGlobal->warnStream() << "Problem: cannot erase artifact " << art->Name() << " from list, it was not present";
  545. }
  546. else
  547. logGlobal->warnStream() << "Problem: cannot find list for artifact " << art->Name() << ", strange class. (special?)";
  548. }
  549. boost::optional<std::vector<CArtifact*>&> CArtHandler::listFromClass( CArtifact::EartClass artifactClass )
  550. {
  551. switch(artifactClass)
  552. {
  553. case CArtifact::ART_TREASURE:
  554. return treasures;
  555. case CArtifact::ART_MINOR:
  556. return minors;
  557. case CArtifact::ART_MAJOR:
  558. return majors;
  559. case CArtifact::ART_RELIC:
  560. return relics;
  561. default: //special artifacts should not be erased
  562. return nullptr;
  563. }
  564. }
  565. void CArtHandler::fillList( std::vector<CArtifact*> &listToBeFilled, CArtifact::EartClass artifactClass )
  566. {
  567. assert(listToBeFilled.empty());
  568. for (auto & elem : allowedArtifacts)
  569. {
  570. if (elem->aClass == artifactClass)
  571. listToBeFilled.push_back(elem);
  572. }
  573. }
  574. void CArtHandler::afterLoadFinalization()
  575. {
  576. //All artifacts have their id, so we can properly update their bonuses' source ids.
  577. for(auto &art : artifacts)
  578. {
  579. for(auto &bonus : art->getExportedBonusList())
  580. {
  581. assert(art == artifacts[art->id]);
  582. assert(bonus->source == Bonus::ARTIFACT);
  583. bonus->sid = art->id;
  584. }
  585. }
  586. for (CArtifact * art : artifacts)
  587. {
  588. VLC->objtypeh->loadSubObject(art->Name(), JsonNode(), Obj::ARTIFACT, art->id.num);
  589. if (!art->advMapDef.empty())
  590. {
  591. JsonNode templ;
  592. templ["animation"].String() = art->advMapDef;
  593. // add new template.
  594. // Necessary for objects added via mods that don't have any templates in H3
  595. VLC->objtypeh->getHandlerFor(Obj::ARTIFACT, art->id)->addTemplate(templ);
  596. }
  597. // object does not have any templates - this is not usable object (e.g. pseudo-art like lock)
  598. if (VLC->objtypeh->getHandlerFor(Obj::ARTIFACT, art->id)->getTemplates().empty())
  599. VLC->objtypeh->removeSubObject(Obj::ARTIFACT, art->id);
  600. }
  601. }
  602. CArtifactInstance::CArtifactInstance()
  603. {
  604. init();
  605. }
  606. CArtifactInstance::CArtifactInstance( CArtifact *Art)
  607. {
  608. init();
  609. setType(Art);
  610. }
  611. void CArtifactInstance::setType( CArtifact *Art )
  612. {
  613. artType = Art;
  614. attachTo(Art);
  615. }
  616. std::string CArtifactInstance::nodeName() const
  617. {
  618. return "Artifact instance of " + (artType ? artType->Name() : std::string("uninitialized")) + " type";
  619. }
  620. CArtifactInstance * CArtifactInstance::createScroll( const CSpell *s)
  621. {
  622. auto ret = new CArtifactInstance(VLC->arth->artifacts[1]);
  623. auto b = new Bonus(Bonus::PERMANENT, Bonus::SPELL, Bonus::ARTIFACT_INSTANCE, -1, 1, s->id);
  624. ret->addNewBonus(b);
  625. return ret;
  626. }
  627. void CArtifactInstance::init()
  628. {
  629. id = ArtifactInstanceID();
  630. id = static_cast<ArtifactInstanceID>(ArtifactID::NONE); //to be randomized
  631. setNodeType(ARTIFACT_INSTANCE);
  632. }
  633. ArtifactPosition CArtifactInstance::firstAvailableSlot(const CArtifactSet *h) const
  634. {
  635. for(auto slot : artType->possibleSlots.at(h->bearerType()))
  636. {
  637. if(canBePutAt(h, slot)) //if(artType->fitsAt(h->artifWorn, slot))
  638. {
  639. //we've found a free suitable slot.
  640. return slot;
  641. }
  642. }
  643. //if haven't find proper slot, use backpack
  644. return firstBackpackSlot(h);
  645. }
  646. ArtifactPosition CArtifactInstance::firstBackpackSlot(const CArtifactSet *h) const
  647. {
  648. if(!artType->isBig()) //discard big artifact
  649. return ArtifactPosition(
  650. GameConstants::BACKPACK_START + h->artifactsInBackpack.size());
  651. return ArtifactPosition::PRE_FIRST;
  652. }
  653. bool CArtifactInstance::canBePutAt(const ArtifactLocation & al, bool assumeDestRemoved /*= false*/) const
  654. {
  655. return canBePutAt(al.getHolderArtSet(), al.slot, assumeDestRemoved);
  656. }
  657. bool CArtifactInstance::canBePutAt(const CArtifactSet *artSet, ArtifactPosition slot, bool assumeDestRemoved /*= false*/) const
  658. {
  659. if(slot >= GameConstants::BACKPACK_START)
  660. {
  661. if(artType->isBig())
  662. return false;
  663. //TODO backpack limit
  664. return true;
  665. }
  666. auto possibleSlots = artType->possibleSlots.find(artSet->bearerType());
  667. if(possibleSlots == artType->possibleSlots.end())
  668. {
  669. logGlobal->warnStream() << "Warning: artifact " << artType->Name() << " doesn't have defined allowed slots for bearer of type "
  670. << artSet->bearerType();
  671. return false;
  672. }
  673. if(!vstd::contains(possibleSlots->second, slot))
  674. return false;
  675. return artSet->isPositionFree(slot, assumeDestRemoved);
  676. }
  677. void CArtifactInstance::putAt(ArtifactLocation al)
  678. {
  679. assert(canBePutAt(al));
  680. al.getHolderArtSet()->setNewArtSlot(al.slot, this, false);
  681. if(al.slot < GameConstants::BACKPACK_START)
  682. al.getHolderNode()->attachTo(this);
  683. }
  684. void CArtifactInstance::removeFrom(ArtifactLocation al)
  685. {
  686. assert(al.getHolderArtSet()->getArt(al.slot) == this);
  687. al.getHolderArtSet()->eraseArtSlot(al.slot);
  688. if(al.slot < GameConstants::BACKPACK_START)
  689. al.getHolderNode()->detachFrom(this);
  690. //TODO delete me?
  691. }
  692. bool CArtifactInstance::canBeDisassembled() const
  693. {
  694. return bool(artType->constituents);
  695. }
  696. std::vector<const CArtifact *> CArtifactInstance::assemblyPossibilities(const CArtifactSet *h) const
  697. {
  698. std::vector<const CArtifact *> ret;
  699. if(artType->constituents) //combined artifact already: no combining of combined artifacts... for now.
  700. return ret;
  701. for(const CArtifact * artifact : artType->constituentOf)
  702. {
  703. assert(artifact->constituents);
  704. bool possible = true;
  705. for(const CArtifact * constituent : *artifact->constituents) //check if all constituents are available
  706. {
  707. if(!h->hasArt(constituent->id, true)) //constituent must be equipped
  708. {
  709. possible = false;
  710. break;
  711. }
  712. }
  713. if(possible)
  714. ret.push_back(artifact);
  715. }
  716. return ret;
  717. }
  718. void CArtifactInstance::move(ArtifactLocation src, ArtifactLocation dst)
  719. {
  720. removeFrom(src);
  721. putAt(dst);
  722. }
  723. CArtifactInstance * CArtifactInstance::createNewArtifactInstance(CArtifact *Art)
  724. {
  725. if(!Art->constituents)
  726. {
  727. auto ret = new CArtifactInstance(Art);
  728. if (dynamic_cast<CGrowingArtifact *>(Art))
  729. {
  730. auto bonus = new Bonus;
  731. bonus->type = Bonus::LEVEL_COUNTER;
  732. bonus->val = 0;
  733. ret->addNewBonus (bonus);
  734. }
  735. return ret;
  736. }
  737. else
  738. {
  739. auto ret = new CCombinedArtifactInstance(Art);
  740. ret->createConstituents();
  741. return ret;
  742. }
  743. }
  744. CArtifactInstance * CArtifactInstance::createNewArtifactInstance(int aid)
  745. {
  746. return createNewArtifactInstance(VLC->arth->artifacts[aid]);
  747. }
  748. void CArtifactInstance::deserializationFix()
  749. {
  750. setType(artType);
  751. }
  752. SpellID CArtifactInstance::getGivenSpellID() const
  753. {
  754. const Bonus * b = getBonusLocalFirst(Selector::type(Bonus::SPELL));
  755. if(!b)
  756. {
  757. logGlobal->warnStream() << "Warning: " << nodeName() << " doesn't bear any spell!";
  758. return SpellID::NONE;
  759. }
  760. return SpellID(b->subtype);
  761. }
  762. bool CArtifactInstance::isPart(const CArtifactInstance *supposedPart) const
  763. {
  764. return supposedPart == this;
  765. }
  766. bool CCombinedArtifactInstance::canBePutAt(const CArtifactSet *artSet, ArtifactPosition slot, bool assumeDestRemoved /*= false*/) const
  767. {
  768. bool canMainArtifactBePlaced = CArtifactInstance::canBePutAt(artSet, slot, assumeDestRemoved);
  769. if(!canMainArtifactBePlaced)
  770. return false; //no is no...
  771. if(slot >= GameConstants::BACKPACK_START)
  772. return true; //we can always remove combined art to the backapck
  773. assert(artType->constituents);
  774. std::vector<ConstituentInfo> constituentsToBePlaced = constituentsInfo; //we'll remove constituents from that list, as we find a suitable slot for them
  775. //it may be that we picked a combined artifact in hero screen (though technically it's still there) to move it
  776. //so we remove from the list all constituents that are already present on dst hero in the form of locks
  777. for(const ConstituentInfo &constituent : constituentsInfo)
  778. {
  779. if(constituent.art == artSet->getArt(constituent.slot, false)) //no need to worry about locked constituent
  780. constituentsToBePlaced -= constituent;
  781. }
  782. //we iterate over all active slots and check if constituents fits them
  783. for (int i = 0; i < GameConstants::BACKPACK_START; i++)
  784. {
  785. for(auto art = constituentsToBePlaced.begin(); art != constituentsToBePlaced.end(); art++)
  786. {
  787. if(art->art->canBePutAt(artSet, ArtifactPosition(i), i == slot)) // i == al.slot because we can remove already worn artifact only from that slot that is our main destination
  788. {
  789. constituentsToBePlaced.erase(art);
  790. break;
  791. }
  792. }
  793. }
  794. return constituentsToBePlaced.empty();
  795. }
  796. bool CCombinedArtifactInstance::canBeDisassembled() const
  797. {
  798. return true;
  799. }
  800. CCombinedArtifactInstance::CCombinedArtifactInstance(CArtifact *Art)
  801. : CArtifactInstance(Art) //TODO: seems unued, but need to be written
  802. {
  803. }
  804. CCombinedArtifactInstance::CCombinedArtifactInstance()
  805. {
  806. }
  807. void CCombinedArtifactInstance::createConstituents()
  808. {
  809. assert(artType);
  810. assert(artType->constituents);
  811. for(const CArtifact * art : *artType->constituents)
  812. {
  813. addAsConstituent(CArtifactInstance::createNewArtifactInstance(art->id), ArtifactPosition::PRE_FIRST);
  814. }
  815. }
  816. void CCombinedArtifactInstance::addAsConstituent(CArtifactInstance *art, ArtifactPosition slot)
  817. {
  818. assert(vstd::contains(*artType->constituents, art->artType.get()));
  819. assert(art->getParentNodes().size() == 1 && art->getParentNodes().front() == art->artType);
  820. constituentsInfo.push_back(ConstituentInfo(art, slot));
  821. attachTo(art);
  822. }
  823. void CCombinedArtifactInstance::putAt(ArtifactLocation al)
  824. {
  825. if(al.slot >= GameConstants::BACKPACK_START)
  826. {
  827. CArtifactInstance::putAt(al);
  828. for(ConstituentInfo &ci : constituentsInfo)
  829. ci.slot = ArtifactPosition::PRE_FIRST;
  830. }
  831. else
  832. {
  833. CArtifactInstance *mainConstituent = figureMainConstituent(al); //it'll be replaced with combined artifact, not a lock
  834. CArtifactInstance::putAt(al); //puts combined art (this)
  835. for(ConstituentInfo &ci : constituentsInfo)
  836. {
  837. if(ci.art != mainConstituent)
  838. {
  839. const ArtifactLocation suggestedPos(al.artHolder, ci.slot);
  840. const bool inActiveSlot = vstd::isbetween(ci.slot, 0, GameConstants::BACKPACK_START);
  841. const bool suggestedPosValid = ci.art->canBePutAt(suggestedPos);
  842. ArtifactPosition pos = ArtifactPosition::PRE_FIRST;
  843. if(inActiveSlot && suggestedPosValid) //there is a valid suggestion where to place lock
  844. pos = ci.slot;
  845. else
  846. ci.slot = pos = ci.art->firstAvailableSlot(al.getHolderArtSet());
  847. assert(pos < GameConstants::BACKPACK_START);
  848. al.getHolderArtSet()->setNewArtSlot(pos, ci.art, true); //sets as lock
  849. }
  850. else
  851. {
  852. ci.slot = ArtifactPosition::PRE_FIRST;
  853. }
  854. }
  855. }
  856. }
  857. void CCombinedArtifactInstance::removeFrom(ArtifactLocation al)
  858. {
  859. if(al.slot >= GameConstants::BACKPACK_START)
  860. {
  861. CArtifactInstance::removeFrom(al);
  862. }
  863. else
  864. {
  865. for(ConstituentInfo &ci : constituentsInfo)
  866. {
  867. if(ci.slot >= 0)
  868. {
  869. al.getHolderArtSet()->eraseArtSlot(ci.slot);
  870. ci.slot = ArtifactPosition::PRE_FIRST;
  871. }
  872. else
  873. {
  874. //main constituent
  875. CArtifactInstance::removeFrom(al);
  876. }
  877. }
  878. }
  879. }
  880. CArtifactInstance * CCombinedArtifactInstance::figureMainConstituent(const ArtifactLocation al)
  881. {
  882. CArtifactInstance *mainConstituent = nullptr; //it'll be replaced with combined artifact, not a lock
  883. for(ConstituentInfo &ci : constituentsInfo)
  884. if(ci.slot == al.slot)
  885. mainConstituent = ci.art;
  886. if(!mainConstituent)
  887. {
  888. for(ConstituentInfo &ci : constituentsInfo)
  889. {
  890. if(vstd::contains(ci.art->artType->possibleSlots[al.getHolderArtSet()->bearerType()], al.slot))
  891. {
  892. mainConstituent = ci.art;
  893. }
  894. }
  895. }
  896. return mainConstituent;
  897. }
  898. void CCombinedArtifactInstance::deserializationFix()
  899. {
  900. for(ConstituentInfo &ci : constituentsInfo)
  901. attachTo(ci.art);
  902. }
  903. bool CCombinedArtifactInstance::isPart(const CArtifactInstance *supposedPart) const
  904. {
  905. bool me = CArtifactInstance::isPart(supposedPart);
  906. if(me)
  907. return true;
  908. //check for constituents
  909. for(const ConstituentInfo &constituent : constituentsInfo)
  910. if(constituent.art == supposedPart)
  911. return true;
  912. return false;
  913. }
  914. CCombinedArtifactInstance::ConstituentInfo::ConstituentInfo(CArtifactInstance *Art /*= nullptr*/, ArtifactPosition Slot /*= -1*/)
  915. {
  916. art = Art;
  917. slot = Slot;
  918. }
  919. bool CCombinedArtifactInstance::ConstituentInfo::operator==(const ConstituentInfo &rhs) const
  920. {
  921. return art == rhs.art && slot == rhs.slot;
  922. }
  923. const CArtifactInstance* CArtifactSet::getArt(ArtifactPosition pos, bool excludeLocked /*= true*/) const
  924. {
  925. if(const ArtSlotInfo *si = getSlot(pos))
  926. {
  927. if(si->artifact && (!excludeLocked || !si->locked))
  928. return si->artifact;
  929. }
  930. return nullptr;
  931. }
  932. CArtifactInstance* CArtifactSet::getArt(ArtifactPosition pos, bool excludeLocked /*= true*/)
  933. {
  934. return const_cast<CArtifactInstance*>((const_cast<const CArtifactSet*>(this))->getArt(pos, excludeLocked));
  935. }
  936. ArtifactPosition CArtifactSet::getArtPos(int aid, bool onlyWorn /*= true*/) const
  937. {
  938. for(auto i = artifactsWorn.cbegin(); i != artifactsWorn.cend(); i++)
  939. if(i->second.artifact->artType->id == aid)
  940. return i->first;
  941. if(onlyWorn)
  942. return ArtifactPosition::PRE_FIRST;
  943. for(int i = 0; i < artifactsInBackpack.size(); i++)
  944. if(artifactsInBackpack[i].artifact->artType->id == aid)
  945. return ArtifactPosition(GameConstants::BACKPACK_START + i);
  946. return ArtifactPosition::PRE_FIRST;
  947. }
  948. ArtifactPosition CArtifactSet::getArtPos(const CArtifactInstance *art) const
  949. {
  950. for(auto i : artifactsWorn)
  951. if(i.second.artifact == art)
  952. return i.first;
  953. for(int i = 0; i < artifactsInBackpack.size(); i++)
  954. if(artifactsInBackpack[i].artifact == art)
  955. return ArtifactPosition(GameConstants::BACKPACK_START + i);
  956. return ArtifactPosition::PRE_FIRST;
  957. }
  958. const CArtifactInstance * CArtifactSet::getArtByInstanceId( ArtifactInstanceID artInstId ) const
  959. {
  960. for(auto i : artifactsWorn)
  961. if(i.second.artifact->id == artInstId)
  962. return i.second.artifact;
  963. for(auto i : artifactsInBackpack)
  964. if(i.artifact->id == artInstId)
  965. return i.artifact;
  966. return nullptr;
  967. }
  968. bool CArtifactSet::hasArt(ui32 aid, bool onlyWorn /*= false*/) const
  969. {
  970. return getArtPos(aid, onlyWorn) != ArtifactPosition::PRE_FIRST;
  971. }
  972. const ArtSlotInfo * CArtifactSet::getSlot(ArtifactPosition pos) const
  973. {
  974. if(vstd::contains(artifactsWorn, pos))
  975. return &artifactsWorn.at(pos);
  976. if(pos >= ArtifactPosition::AFTER_LAST )
  977. {
  978. int backpackPos = (int)pos - GameConstants::BACKPACK_START;
  979. if(backpackPos < 0 || backpackPos >= artifactsInBackpack.size())
  980. return nullptr;
  981. else
  982. return &artifactsInBackpack[backpackPos];
  983. }
  984. return nullptr;
  985. }
  986. bool CArtifactSet::isPositionFree(ArtifactPosition pos, bool onlyLockCheck /*= false*/) const
  987. {
  988. if(const ArtSlotInfo *s = getSlot(pos))
  989. return (onlyLockCheck || !s->artifact) && !s->locked;
  990. return true; //no slot means not used
  991. }
  992. si32 CArtifactSet::getArtTypeId(ArtifactPosition pos) const
  993. {
  994. const CArtifactInstance * const a = getArt(pos);
  995. if(!a)
  996. {
  997. logGlobal->warnStream() << (dynamic_cast<const CGHeroInstance*>(this))->name << " has no artifact at " << pos << " (getArtTypeId)";
  998. return -1;
  999. }
  1000. return a->artType->id;
  1001. }
  1002. CArtifactSet::~CArtifactSet()
  1003. {
  1004. }
  1005. ArtSlotInfo & CArtifactSet::retreiveNewArtSlot(ArtifactPosition slot)
  1006. {
  1007. assert(!vstd::contains(artifactsWorn, slot));
  1008. ArtSlotInfo &ret = slot < GameConstants::BACKPACK_START
  1009. ? artifactsWorn[slot]
  1010. : *artifactsInBackpack.insert(artifactsInBackpack.begin() + (slot - GameConstants::BACKPACK_START), ArtSlotInfo());
  1011. return ret;
  1012. }
  1013. void CArtifactSet::setNewArtSlot(ArtifactPosition slot, CArtifactInstance *art, bool locked)
  1014. {
  1015. ArtSlotInfo &asi = retreiveNewArtSlot(slot);
  1016. asi.artifact = art;
  1017. asi.locked = locked;
  1018. }
  1019. void CArtifactSet::eraseArtSlot(ArtifactPosition slot)
  1020. {
  1021. if(slot < GameConstants::BACKPACK_START)
  1022. {
  1023. artifactsWorn.erase(slot);
  1024. }
  1025. else
  1026. {
  1027. slot = ArtifactPosition(slot - GameConstants::BACKPACK_START);
  1028. artifactsInBackpack.erase(artifactsInBackpack.begin() + slot);
  1029. }
  1030. }
  1031. void CArtifactSet::artDeserializationFix(CBonusSystemNode *node)
  1032. {
  1033. for(auto & elem : artifactsWorn)
  1034. if(elem.second.artifact && !elem.second.locked)
  1035. node->attachTo(elem.second.artifact);
  1036. }