CArtHandler.cpp 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165
  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. artifacts[numeric] = loadArtifact(artData);
  176. artifacts[numeric]->id = ArtifactID(numeric);
  177. VLC->modh->identifiers.registerObject ("artifact." + node.first, numeric);
  178. }
  179. for (size_t i=0; i < artifacts.size(); i++)
  180. {
  181. if (artifacts[i] == nullptr)
  182. tlog0 << "Warning: artifact with id " << i << " is missing!\n";
  183. }
  184. }
  185. void CArtHandler::load(std::string objectID, const JsonNode & node)
  186. {
  187. CArtifact * art = loadArtifact(node);
  188. art->id = ArtifactID(artifacts.size());
  189. artifacts.push_back(art);
  190. tlog5 << "Added artifact: " << objectID << "\n";
  191. VLC->modh->identifiers.registerObject ("artifact." + objectID, art->id);
  192. }
  193. CArtifact * CArtHandler::loadArtifact(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->iconIndex = graphics["iconIndex"].Float();
  210. art->image = graphics["image"].String();
  211. if (!graphics["large"].isNull())
  212. art->large = graphics["large"].String();
  213. else
  214. art->large = art->image;
  215. art->advMapDef = graphics["map"].String();
  216. art->price = node["value"].Float();
  217. loadSlots(art, node);
  218. loadClass(art, node);
  219. loadType(art, node);
  220. loadComponents(art, node);
  221. BOOST_FOREACH (auto b, node["bonuses"].Vector())
  222. {
  223. auto bonus = JsonUtils::parseBonus (b);
  224. bonus->sid = art->id;
  225. art->addNewBonus (bonus);
  226. }
  227. return art;
  228. }
  229. void CArtHandler::addSlot(CArtifact * art, const std::string & slotID)
  230. {
  231. #define ART_POS(x) ( #x, ArtifactPosition::x )
  232. static const std::map<std::string, ArtifactPosition> artifactPositionMap = boost::assign::map_list_of ART_POS_LIST;
  233. #undef ART_POS
  234. if (slotID == "MISC")
  235. {
  236. art->possibleSlots[ArtBearer::HERO] += ArtifactPosition::MISC1, ArtifactPosition::MISC2, ArtifactPosition::MISC3, ArtifactPosition::MISC4, ArtifactPosition::MISC5;
  237. }
  238. else if (slotID == "RING")
  239. {
  240. art->possibleSlots[ArtBearer::HERO] += ArtifactPosition::LEFT_RING, ArtifactPosition::RIGHT_RING;
  241. }
  242. else
  243. {
  244. auto it = artifactPositionMap.find (slotID);
  245. if (it != artifactPositionMap.end())
  246. {
  247. auto slot = it->second;
  248. art->possibleSlots[ArtBearer::HERO].push_back (slot);
  249. }
  250. else
  251. tlog2 << "Warning! Artifact slot " << slotID << " not recognized!";
  252. }
  253. }
  254. void CArtHandler::loadSlots(CArtifact * art, const JsonNode & node)
  255. {
  256. if (!node["slot"].isNull()) //we assume non-hero slots are irrelevant?
  257. {
  258. if (node["slot"].getType() == JsonNode::DATA_STRING)
  259. addSlot(art, node["slot"].String());
  260. else
  261. {
  262. BOOST_FOREACH (const JsonNode & slot, node["slot"].Vector())
  263. addSlot(art, slot.String());
  264. }
  265. }
  266. }
  267. void CArtHandler::loadClass(CArtifact * art, const JsonNode & node)
  268. {
  269. static const std::map<std::string, CArtifact::EartClass> artifactClassMap = boost::assign::map_list_of
  270. ("TREASURE", CArtifact::ART_TREASURE)
  271. ("MINOR", CArtifact::ART_MINOR)
  272. ("MAJOR", CArtifact::ART_MAJOR)
  273. ("RELIC", CArtifact::ART_RELIC)
  274. ("SPECIAL", CArtifact::ART_SPECIAL);
  275. auto it = artifactClassMap.find (node["class"].String());
  276. if (it != artifactClassMap.end())
  277. {
  278. art->aClass = it->second;
  279. }
  280. else
  281. {
  282. tlog2 << "Warning! Artifact rarity " << node["class"].String() << " not recognized!";
  283. art->aClass = CArtifact::ART_SPECIAL;
  284. }
  285. }
  286. void CArtHandler::loadType(CArtifact * art, const JsonNode & node)
  287. {
  288. #define ART_BEARER(x) ( #x, ArtBearer::x )
  289. static const std::map<std::string, int> artifactBearerMap = boost::assign::map_list_of ART_BEARER_LIST;
  290. #undef ART_BEARER
  291. BOOST_FOREACH (const JsonNode & b, node["type"].Vector())
  292. {
  293. auto it = artifactBearerMap.find (b.String());
  294. if (it != artifactBearerMap.end())
  295. {
  296. int bearerType = it->second;
  297. switch (bearerType)
  298. {
  299. case ArtBearer::HERO://TODO: allow arts having several possible bearers
  300. break;
  301. case ArtBearer::COMMANDER:
  302. makeItCommanderArt (art); //original artifacts should have only one bearer type
  303. break;
  304. case ArtBearer::CREATURE:
  305. makeItCreatureArt (art);
  306. break;
  307. }
  308. }
  309. else
  310. tlog2 << "Warning! Artifact type " << b.String() << " not recognized!";
  311. }
  312. }
  313. void CArtHandler::loadComponents(CArtifact * art, const JsonNode & node)
  314. {
  315. if (!node["components"].isNull())
  316. {
  317. art->constituents.reset(new std::vector<CArtifact *>());
  318. BOOST_FOREACH (auto component, node["components"].Vector())
  319. {
  320. VLC->modh->identifiers.requestIdentifier("artifact." + component.String(), [=](si32 id)
  321. {
  322. // when this code is called both combinational art as well as component are loaded
  323. // so it is safe to access any of them
  324. art->constituents->push_back(VLC->arth->artifacts[id]);
  325. VLC->arth->artifacts[id]->constituentOf.push_back(art);
  326. });
  327. }
  328. }
  329. }
  330. void CArtHandler::loadGrowingArt(CGrowingArtifact * art, const JsonNode & node)
  331. {
  332. BOOST_FOREACH (auto b, node["growing"]["bonusesPerLevel"].Vector())
  333. {
  334. art->bonusesPerLevel.push_back (std::pair <ui16, Bonus> (b["level"].Float(), *JsonUtils::parseBonus (b["bonus"].Vector())));
  335. }
  336. BOOST_FOREACH (auto b, node["growing"]["thresholdBonuses"].Vector())
  337. {
  338. art->thresholdBonuses.push_back (std::pair <ui16, Bonus> (b["level"].Float(), *JsonUtils::parseBonus (b["bonus"].Vector())));
  339. }
  340. }
  341. ArtifactID CArtHandler::creatureToMachineID(CreatureID id)
  342. {
  343. int dif = 142;
  344. switch (id)
  345. {
  346. case 147:
  347. dif--;
  348. break;
  349. case 148:
  350. dif++;
  351. break;
  352. }
  353. dif = -dif;
  354. return ArtifactID(id+dif);
  355. }
  356. CreatureID CArtHandler::machineIDToCreature(ArtifactID id)
  357. {
  358. int dif = 142;
  359. switch (id)
  360. {
  361. case 6:
  362. dif--;
  363. break;
  364. case 5:
  365. dif++;
  366. break;
  367. }
  368. return CreatureID(id + dif);
  369. }
  370. ArtifactID CArtHandler::getRandomArt(int flags)
  371. {
  372. return getArtSync(ran(), flags, true);
  373. }
  374. ArtifactID CArtHandler::getArtSync (ui32 rand, int flags, bool erasePicked)
  375. {
  376. auto getAllowedArts = [&](std::vector<ConstTransitivePtr<CArtifact> > &out, std::vector<CArtifact*> *arts, CArtifact::EartClass flag)
  377. {
  378. if (arts->empty()) //restock available arts
  379. fillList(*arts, flag);
  380. for (int i = 0; i < arts->size(); ++i)
  381. {
  382. CArtifact *art = (*arts)[i];
  383. out.push_back(art);
  384. }
  385. };
  386. auto getAllowed = [&](std::vector<ConstTransitivePtr<CArtifact> > &out)
  387. {
  388. if (flags & CArtifact::ART_TREASURE)
  389. getAllowedArts (out, &treasures, CArtifact::ART_TREASURE);
  390. if (flags & CArtifact::ART_MINOR)
  391. getAllowedArts (out, &minors, CArtifact::ART_MINOR);
  392. if (flags & CArtifact::ART_MAJOR)
  393. getAllowedArts (out, &majors, CArtifact::ART_MAJOR);
  394. if (flags & CArtifact::ART_RELIC)
  395. getAllowedArts (out, &relics, CArtifact::ART_RELIC);
  396. if (!out.size()) //no artifact of specified rarity, we need to take another one
  397. {
  398. getAllowedArts (out, &treasures, CArtifact::ART_TREASURE);
  399. getAllowedArts (out, &minors, CArtifact::ART_MINOR);
  400. getAllowedArts (out, &majors, CArtifact::ART_MAJOR);
  401. getAllowedArts (out, &relics, CArtifact::ART_RELIC);
  402. }
  403. if (!out.size()) //no arts are available at all
  404. {
  405. out.resize (64);
  406. std::fill_n (out.begin(), 64, artifacts[2]); //Give Grail - this can't be banned (hopefully)
  407. }
  408. //tlog0 << "Treasure count: " << treasures.size() << std::endl;
  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. tlog2 << "Problem: cannot erase artifact " << art->Name() << " from list, it was not present\n";
  536. }
  537. else
  538. tlog2 << "Problem: cannot find list for artifact " << art->Name() << ", strange class. (special?)\n";
  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. tlog3 << "Warning: artifact " << artType->Name() << " doesn't have defined allowed slots for bearer of type "
  633. << artSet->bearerType() << std::endl;
  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. tlog3 << "Warning: " << nodeName() << " doesn't bear any spell!\n";
  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. tlog2 << (dynamic_cast<const CGHeroInstance*>(this))->name << " has no artifact at " << pos << " (getArtTypeId)\n";
  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. }