CArtHandler.cpp 32 KB

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