CArtHandler.cpp 34 KB

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