CArtHandler.cpp 32 KB

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