CArtHandler.cpp 32 KB

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