CArtHandler.cpp 32 KB

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