CArtHandler.cpp 31 KB

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