CArtHandler.cpp 32 KB

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