CArtHandler.cpp 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234
  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. art->aClass >= CArtifact::ART_TREASURE &&
  510. art->aClass <= CArtifact::ART_RELIC);
  511. }
  512. bool CArtHandler::isTradableArtifact(ArtifactID id) const
  513. {
  514. switch (id)
  515. {
  516. case ArtifactID::SPELLBOOK:
  517. case ArtifactID::GRAIL:
  518. case ArtifactID::CATAPULT:
  519. case ArtifactID::BALLISTA:
  520. case ArtifactID::AMMO_CART:
  521. case ArtifactID::FIRST_AID_TENT:
  522. return false;
  523. default:
  524. return true;
  525. }
  526. }
  527. void CArtHandler::initAllowedArtifactsList(const std::vector<bool> &allowed)
  528. {
  529. allowedArtifacts.clear();
  530. treasures.clear();
  531. minors.clear();
  532. majors.clear();
  533. relics.clear();
  534. for (ArtifactID i=ArtifactID::SPELLBOOK; i<ArtifactID::ART_SELECTION; i.advance(1))
  535. {
  536. //check artifacts allowed on a map
  537. //TODO: This line will be different when custom map format is implemented
  538. if (allowed[i] && legalArtifact(i))
  539. allowedArtifacts.push_back(artifacts[i]);
  540. }
  541. for (ArtifactID i = ArtifactID::ART_SELECTION; i<ArtifactID(artifacts.size()); i.advance(1)) //try to allow all artifacts added by mods
  542. {
  543. if (legalArtifact(ArtifactID(i)))
  544. allowedArtifacts.push_back(artifacts[i]);
  545. //keep im mind that artifact can be worn by more than one type of bearer
  546. }
  547. }
  548. std::vector<bool> CArtHandler::getDefaultAllowed() const
  549. {
  550. std::vector<bool> allowedArtifacts;
  551. allowedArtifacts.resize(127, true);
  552. allowedArtifacts.resize(141, false);
  553. allowedArtifacts.resize(GameConstants::ARTIFACTS_QUANTITY, true);
  554. return allowedArtifacts;
  555. }
  556. void CArtHandler::erasePickedArt(ArtifactID id)
  557. {
  558. CArtifact *art = artifacts[id];
  559. if(auto artifactList = listFromClass(art->aClass))
  560. {
  561. if(artifactList->empty())
  562. fillList(*artifactList, art->aClass);
  563. auto itr = vstd::find(*artifactList, art);
  564. if(itr != artifactList->end())
  565. {
  566. artifactList->erase(itr);
  567. }
  568. else
  569. logGlobal->warnStream() << "Problem: cannot erase artifact " << art->Name() << " from list, it was not present";
  570. }
  571. else
  572. logGlobal->warnStream() << "Problem: cannot find list for artifact " << art->Name() << ", strange class. (special?)";
  573. }
  574. boost::optional<std::vector<CArtifact*>&> CArtHandler::listFromClass( CArtifact::EartClass artifactClass )
  575. {
  576. switch(artifactClass)
  577. {
  578. case CArtifact::ART_TREASURE:
  579. return treasures;
  580. case CArtifact::ART_MINOR:
  581. return minors;
  582. case CArtifact::ART_MAJOR:
  583. return majors;
  584. case CArtifact::ART_RELIC:
  585. return relics;
  586. default: //special artifacts should not be erased
  587. return boost::optional<std::vector<CArtifact*>&>();
  588. }
  589. }
  590. void CArtHandler::fillList( std::vector<CArtifact*> &listToBeFilled, CArtifact::EartClass artifactClass )
  591. {
  592. assert(listToBeFilled.empty());
  593. for (auto & elem : allowedArtifacts)
  594. {
  595. if (elem->aClass == artifactClass)
  596. listToBeFilled.push_back(elem);
  597. }
  598. }
  599. void CArtHandler::afterLoadFinalization()
  600. {
  601. //All artifacts have their id, so we can properly update their bonuses' source ids.
  602. for(auto &art : artifacts)
  603. {
  604. for(auto &bonus : art->getExportedBonusList())
  605. {
  606. assert(art == artifacts[art->id]);
  607. assert(bonus->source == Bonus::ARTIFACT);
  608. bonus->sid = art->id;
  609. }
  610. }
  611. for (CArtifact * art : artifacts)
  612. {
  613. VLC->objtypeh->loadSubObject(art->Name(), JsonNode(), Obj::ARTIFACT, art->id.num);
  614. if (!art->advMapDef.empty())
  615. {
  616. JsonNode templ;
  617. templ["animation"].String() = art->advMapDef;
  618. // add new template.
  619. // Necessary for objects added via mods that don't have any templates in H3
  620. VLC->objtypeh->getHandlerFor(Obj::ARTIFACT, art->id)->addTemplate(templ);
  621. }
  622. // object does not have any templates - this is not usable object (e.g. pseudo-art like lock)
  623. if (VLC->objtypeh->getHandlerFor(Obj::ARTIFACT, art->id)->getTemplates().empty())
  624. VLC->objtypeh->removeSubObject(Obj::ARTIFACT, art->id);
  625. }
  626. }
  627. CArtifactInstance::CArtifactInstance()
  628. {
  629. init();
  630. }
  631. CArtifactInstance::CArtifactInstance( CArtifact *Art)
  632. {
  633. init();
  634. setType(Art);
  635. }
  636. void CArtifactInstance::setType( CArtifact *Art )
  637. {
  638. artType = Art;
  639. attachTo(Art);
  640. }
  641. std::string CArtifactInstance::nodeName() const
  642. {
  643. return "Artifact instance of " + (artType ? artType->Name() : std::string("uninitialized")) + " type";
  644. }
  645. CArtifactInstance * CArtifactInstance::createScroll( const CSpell *s)
  646. {
  647. auto ret = new CArtifactInstance(VLC->arth->artifacts[1]);
  648. auto b = new Bonus(Bonus::PERMANENT, Bonus::SPELL, Bonus::ARTIFACT_INSTANCE, -1, 1, s->id);
  649. ret->addNewBonus(b);
  650. return ret;
  651. }
  652. void CArtifactInstance::init()
  653. {
  654. id = ArtifactInstanceID();
  655. id = static_cast<ArtifactInstanceID>(ArtifactID::NONE); //to be randomized
  656. setNodeType(ARTIFACT_INSTANCE);
  657. }
  658. ArtifactPosition CArtifactInstance::firstAvailableSlot(const CArtifactSet *h) const
  659. {
  660. for(auto slot : artType->possibleSlots.at(h->bearerType()))
  661. {
  662. if(canBePutAt(h, slot)) //if(artType->fitsAt(h->artifWorn, slot))
  663. {
  664. //we've found a free suitable slot.
  665. return slot;
  666. }
  667. }
  668. //if haven't find proper slot, use backpack
  669. return firstBackpackSlot(h);
  670. }
  671. ArtifactPosition CArtifactInstance::firstBackpackSlot(const CArtifactSet *h) const
  672. {
  673. if(!artType->isBig()) //discard big artifact
  674. return ArtifactPosition(
  675. GameConstants::BACKPACK_START + h->artifactsInBackpack.size());
  676. return ArtifactPosition::PRE_FIRST;
  677. }
  678. bool CArtifactInstance::canBePutAt(const ArtifactLocation & al, bool assumeDestRemoved /*= false*/) const
  679. {
  680. return canBePutAt(al.getHolderArtSet(), al.slot, assumeDestRemoved);
  681. }
  682. bool CArtifactInstance::canBePutAt(const CArtifactSet *artSet, ArtifactPosition slot, bool assumeDestRemoved /*= false*/) const
  683. {
  684. if(slot >= GameConstants::BACKPACK_START)
  685. {
  686. if(artType->isBig())
  687. return false;
  688. //TODO backpack limit
  689. return true;
  690. }
  691. auto possibleSlots = artType->possibleSlots.find(artSet->bearerType());
  692. if(possibleSlots == artType->possibleSlots.end())
  693. {
  694. logGlobal->warnStream() << "Warning: artifact " << artType->Name() << " doesn't have defined allowed slots for bearer of type "
  695. << artSet->bearerType();
  696. return false;
  697. }
  698. if(!vstd::contains(possibleSlots->second, slot))
  699. return false;
  700. return artSet->isPositionFree(slot, assumeDestRemoved);
  701. }
  702. void CArtifactInstance::putAt(ArtifactLocation al)
  703. {
  704. assert(canBePutAt(al));
  705. al.getHolderArtSet()->setNewArtSlot(al.slot, this, false);
  706. if(al.slot < GameConstants::BACKPACK_START)
  707. al.getHolderNode()->attachTo(this);
  708. }
  709. void CArtifactInstance::removeFrom(ArtifactLocation al)
  710. {
  711. assert(al.getHolderArtSet()->getArt(al.slot) == this);
  712. al.getHolderArtSet()->eraseArtSlot(al.slot);
  713. if(al.slot < GameConstants::BACKPACK_START)
  714. al.getHolderNode()->detachFrom(this);
  715. //TODO delete me?
  716. }
  717. bool CArtifactInstance::canBeDisassembled() const
  718. {
  719. return bool(artType->constituents);
  720. }
  721. std::vector<const CArtifact *> CArtifactInstance::assemblyPossibilities(const CArtifactSet *h) const
  722. {
  723. std::vector<const CArtifact *> ret;
  724. if(artType->constituents) //combined artifact already: no combining of combined artifacts... for now.
  725. return ret;
  726. for(const CArtifact * artifact : artType->constituentOf)
  727. {
  728. assert(artifact->constituents);
  729. bool possible = true;
  730. for(const CArtifact * constituent : *artifact->constituents) //check if all constituents are available
  731. {
  732. if(!h->hasArt(constituent->id, true)) //constituent must be equipped
  733. {
  734. possible = false;
  735. break;
  736. }
  737. }
  738. if(possible)
  739. ret.push_back(artifact);
  740. }
  741. return ret;
  742. }
  743. void CArtifactInstance::move(ArtifactLocation src, ArtifactLocation dst)
  744. {
  745. removeFrom(src);
  746. putAt(dst);
  747. }
  748. CArtifactInstance * CArtifactInstance::createNewArtifactInstance(CArtifact *Art)
  749. {
  750. if(!Art->constituents)
  751. {
  752. auto ret = new CArtifactInstance(Art);
  753. if (dynamic_cast<CGrowingArtifact *>(Art))
  754. {
  755. auto bonus = new Bonus;
  756. bonus->type = Bonus::LEVEL_COUNTER;
  757. bonus->val = 0;
  758. ret->addNewBonus (bonus);
  759. }
  760. return ret;
  761. }
  762. else
  763. {
  764. auto ret = new CCombinedArtifactInstance(Art);
  765. ret->createConstituents();
  766. return ret;
  767. }
  768. }
  769. CArtifactInstance * CArtifactInstance::createNewArtifactInstance(int aid)
  770. {
  771. return createNewArtifactInstance(VLC->arth->artifacts[aid]);
  772. }
  773. void CArtifactInstance::deserializationFix()
  774. {
  775. setType(artType);
  776. }
  777. SpellID CArtifactInstance::getGivenSpellID() const
  778. {
  779. const Bonus * b = getBonusLocalFirst(Selector::type(Bonus::SPELL));
  780. if(!b)
  781. {
  782. logGlobal->warnStream() << "Warning: " << nodeName() << " doesn't bear any spell!";
  783. return SpellID::NONE;
  784. }
  785. return SpellID(b->subtype);
  786. }
  787. bool CArtifactInstance::isPart(const CArtifactInstance *supposedPart) const
  788. {
  789. return supposedPart == this;
  790. }
  791. bool CCombinedArtifactInstance::canBePutAt(const CArtifactSet *artSet, ArtifactPosition slot, bool assumeDestRemoved /*= false*/) const
  792. {
  793. bool canMainArtifactBePlaced = CArtifactInstance::canBePutAt(artSet, slot, assumeDestRemoved);
  794. if(!canMainArtifactBePlaced)
  795. return false; //no is no...
  796. if(slot >= GameConstants::BACKPACK_START)
  797. return true; //we can always remove combined art to the backapck
  798. assert(artType->constituents);
  799. std::vector<ConstituentInfo> constituentsToBePlaced = constituentsInfo; //we'll remove constituents from that list, as we find a suitable slot for them
  800. //it may be that we picked a combined artifact in hero screen (though technically it's still there) to move it
  801. //so we remove from the list all constituents that are already present on dst hero in the form of locks
  802. for(const ConstituentInfo &constituent : constituentsInfo)
  803. {
  804. if(constituent.art == artSet->getArt(constituent.slot, false)) //no need to worry about locked constituent
  805. constituentsToBePlaced -= constituent;
  806. }
  807. //we iterate over all active slots and check if constituents fits them
  808. for (int i = 0; i < GameConstants::BACKPACK_START; i++)
  809. {
  810. for(auto art = constituentsToBePlaced.begin(); art != constituentsToBePlaced.end(); art++)
  811. {
  812. 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
  813. {
  814. constituentsToBePlaced.erase(art);
  815. break;
  816. }
  817. }
  818. }
  819. return constituentsToBePlaced.empty();
  820. }
  821. bool CCombinedArtifactInstance::canBeDisassembled() const
  822. {
  823. return true;
  824. }
  825. CCombinedArtifactInstance::CCombinedArtifactInstance(CArtifact *Art)
  826. : CArtifactInstance(Art) //TODO: seems unued, but need to be written
  827. {
  828. }
  829. CCombinedArtifactInstance::CCombinedArtifactInstance()
  830. {
  831. }
  832. void CCombinedArtifactInstance::createConstituents()
  833. {
  834. assert(artType);
  835. assert(artType->constituents);
  836. for(const CArtifact * art : *artType->constituents)
  837. {
  838. addAsConstituent(CArtifactInstance::createNewArtifactInstance(art->id), ArtifactPosition::PRE_FIRST);
  839. }
  840. }
  841. void CCombinedArtifactInstance::addAsConstituent(CArtifactInstance *art, ArtifactPosition slot)
  842. {
  843. assert(vstd::contains(*artType->constituents, art->artType.get()));
  844. assert(art->getParentNodes().size() == 1 && art->getParentNodes().front() == art->artType);
  845. constituentsInfo.push_back(ConstituentInfo(art, slot));
  846. attachTo(art);
  847. }
  848. void CCombinedArtifactInstance::putAt(ArtifactLocation al)
  849. {
  850. if(al.slot >= GameConstants::BACKPACK_START)
  851. {
  852. CArtifactInstance::putAt(al);
  853. for(ConstituentInfo &ci : constituentsInfo)
  854. ci.slot = ArtifactPosition::PRE_FIRST;
  855. }
  856. else
  857. {
  858. CArtifactInstance *mainConstituent = figureMainConstituent(al); //it'll be replaced with combined artifact, not a lock
  859. CArtifactInstance::putAt(al); //puts combined art (this)
  860. for(ConstituentInfo &ci : constituentsInfo)
  861. {
  862. if(ci.art != mainConstituent)
  863. {
  864. const ArtifactLocation suggestedPos(al.artHolder, ci.slot);
  865. const bool inActiveSlot = vstd::isbetween(ci.slot, 0, GameConstants::BACKPACK_START);
  866. const bool suggestedPosValid = ci.art->canBePutAt(suggestedPos);
  867. ArtifactPosition pos = ArtifactPosition::PRE_FIRST;
  868. if(inActiveSlot && suggestedPosValid) //there is a valid suggestion where to place lock
  869. pos = ci.slot;
  870. else
  871. ci.slot = pos = ci.art->firstAvailableSlot(al.getHolderArtSet());
  872. assert(pos < GameConstants::BACKPACK_START);
  873. al.getHolderArtSet()->setNewArtSlot(pos, ci.art, true); //sets as lock
  874. }
  875. else
  876. {
  877. ci.slot = ArtifactPosition::PRE_FIRST;
  878. }
  879. }
  880. }
  881. }
  882. void CCombinedArtifactInstance::removeFrom(ArtifactLocation al)
  883. {
  884. if(al.slot >= GameConstants::BACKPACK_START)
  885. {
  886. CArtifactInstance::removeFrom(al);
  887. }
  888. else
  889. {
  890. for(ConstituentInfo &ci : constituentsInfo)
  891. {
  892. if(ci.slot >= 0)
  893. {
  894. al.getHolderArtSet()->eraseArtSlot(ci.slot);
  895. ci.slot = ArtifactPosition::PRE_FIRST;
  896. }
  897. else
  898. {
  899. //main constituent
  900. CArtifactInstance::removeFrom(al);
  901. }
  902. }
  903. }
  904. }
  905. CArtifactInstance * CCombinedArtifactInstance::figureMainConstituent(const ArtifactLocation al)
  906. {
  907. CArtifactInstance *mainConstituent = nullptr; //it'll be replaced with combined artifact, not a lock
  908. for(ConstituentInfo &ci : constituentsInfo)
  909. if(ci.slot == al.slot)
  910. mainConstituent = ci.art;
  911. if(!mainConstituent)
  912. {
  913. for(ConstituentInfo &ci : constituentsInfo)
  914. {
  915. if(vstd::contains(ci.art->artType->possibleSlots[al.getHolderArtSet()->bearerType()], al.slot))
  916. {
  917. mainConstituent = ci.art;
  918. }
  919. }
  920. }
  921. return mainConstituent;
  922. }
  923. void CCombinedArtifactInstance::deserializationFix()
  924. {
  925. for(ConstituentInfo &ci : constituentsInfo)
  926. attachTo(ci.art);
  927. }
  928. bool CCombinedArtifactInstance::isPart(const CArtifactInstance *supposedPart) const
  929. {
  930. bool me = CArtifactInstance::isPart(supposedPart);
  931. if(me)
  932. return true;
  933. //check for constituents
  934. for(const ConstituentInfo &constituent : constituentsInfo)
  935. if(constituent.art == supposedPart)
  936. return true;
  937. return false;
  938. }
  939. CCombinedArtifactInstance::ConstituentInfo::ConstituentInfo(CArtifactInstance *Art /*= nullptr*/, ArtifactPosition Slot /*= -1*/)
  940. {
  941. art = Art;
  942. slot = Slot;
  943. }
  944. bool CCombinedArtifactInstance::ConstituentInfo::operator==(const ConstituentInfo &rhs) const
  945. {
  946. return art == rhs.art && slot == rhs.slot;
  947. }
  948. const CArtifactInstance* CArtifactSet::getArt(ArtifactPosition pos, bool excludeLocked /*= true*/) const
  949. {
  950. if(const ArtSlotInfo *si = getSlot(pos))
  951. {
  952. if(si->artifact && (!excludeLocked || !si->locked))
  953. return si->artifact;
  954. }
  955. return nullptr;
  956. }
  957. CArtifactInstance* CArtifactSet::getArt(ArtifactPosition pos, bool excludeLocked /*= true*/)
  958. {
  959. return const_cast<CArtifactInstance*>((const_cast<const CArtifactSet*>(this))->getArt(pos, excludeLocked));
  960. }
  961. ArtifactPosition CArtifactSet::getArtPos(int aid, bool onlyWorn /*= true*/) const
  962. {
  963. for(auto i = artifactsWorn.cbegin(); i != artifactsWorn.cend(); i++)
  964. if(i->second.artifact->artType->id == aid)
  965. return i->first;
  966. if(onlyWorn)
  967. return ArtifactPosition::PRE_FIRST;
  968. for(int i = 0; i < artifactsInBackpack.size(); i++)
  969. if(artifactsInBackpack[i].artifact->artType->id == aid)
  970. return ArtifactPosition(GameConstants::BACKPACK_START + i);
  971. return ArtifactPosition::PRE_FIRST;
  972. }
  973. ArtifactPosition CArtifactSet::getArtPos(const CArtifactInstance *art) const
  974. {
  975. for(auto i : artifactsWorn)
  976. if(i.second.artifact == art)
  977. return i.first;
  978. for(int i = 0; i < artifactsInBackpack.size(); i++)
  979. if(artifactsInBackpack[i].artifact == art)
  980. return ArtifactPosition(GameConstants::BACKPACK_START + i);
  981. return ArtifactPosition::PRE_FIRST;
  982. }
  983. const CArtifactInstance * CArtifactSet::getArtByInstanceId( ArtifactInstanceID artInstId ) const
  984. {
  985. for(auto i : artifactsWorn)
  986. if(i.second.artifact->id == artInstId)
  987. return i.second.artifact;
  988. for(auto i : artifactsInBackpack)
  989. if(i.artifact->id == artInstId)
  990. return i.artifact;
  991. return nullptr;
  992. }
  993. bool CArtifactSet::hasArt(ui32 aid, bool onlyWorn /*= false*/) const
  994. {
  995. return getArtPos(aid, onlyWorn) != ArtifactPosition::PRE_FIRST;
  996. }
  997. const ArtSlotInfo * CArtifactSet::getSlot(ArtifactPosition pos) const
  998. {
  999. if(vstd::contains(artifactsWorn, pos))
  1000. return &artifactsWorn.at(pos);
  1001. if(pos >= ArtifactPosition::AFTER_LAST )
  1002. {
  1003. int backpackPos = (int)pos - GameConstants::BACKPACK_START;
  1004. if(backpackPos < 0 || backpackPos >= artifactsInBackpack.size())
  1005. return nullptr;
  1006. else
  1007. return &artifactsInBackpack[backpackPos];
  1008. }
  1009. return nullptr;
  1010. }
  1011. bool CArtifactSet::isPositionFree(ArtifactPosition pos, bool onlyLockCheck /*= false*/) const
  1012. {
  1013. if(const ArtSlotInfo *s = getSlot(pos))
  1014. return (onlyLockCheck || !s->artifact) && !s->locked;
  1015. return true; //no slot means not used
  1016. }
  1017. si32 CArtifactSet::getArtTypeId(ArtifactPosition pos) const
  1018. {
  1019. const CArtifactInstance * const a = getArt(pos);
  1020. if(!a)
  1021. {
  1022. logGlobal->warnStream() << (dynamic_cast<const CGHeroInstance*>(this))->name << " has no artifact at " << pos << " (getArtTypeId)";
  1023. return -1;
  1024. }
  1025. return a->artType->id;
  1026. }
  1027. CArtifactSet::~CArtifactSet()
  1028. {
  1029. }
  1030. ArtSlotInfo & CArtifactSet::retreiveNewArtSlot(ArtifactPosition slot)
  1031. {
  1032. assert(!vstd::contains(artifactsWorn, slot));
  1033. ArtSlotInfo &ret = slot < GameConstants::BACKPACK_START
  1034. ? artifactsWorn[slot]
  1035. : *artifactsInBackpack.insert(artifactsInBackpack.begin() + (slot - GameConstants::BACKPACK_START), ArtSlotInfo());
  1036. return ret;
  1037. }
  1038. void CArtifactSet::setNewArtSlot(ArtifactPosition slot, CArtifactInstance *art, bool locked)
  1039. {
  1040. ArtSlotInfo &asi = retreiveNewArtSlot(slot);
  1041. asi.artifact = art;
  1042. asi.locked = locked;
  1043. }
  1044. void CArtifactSet::eraseArtSlot(ArtifactPosition slot)
  1045. {
  1046. if(slot < GameConstants::BACKPACK_START)
  1047. {
  1048. artifactsWorn.erase(slot);
  1049. }
  1050. else
  1051. {
  1052. slot = ArtifactPosition(slot - GameConstants::BACKPACK_START);
  1053. artifactsInBackpack.erase(artifactsInBackpack.begin() + slot);
  1054. }
  1055. }
  1056. void CArtifactSet::artDeserializationFix(CBonusSystemNode *node)
  1057. {
  1058. for(auto & elem : artifactsWorn)
  1059. if(elem.second.artifact && !elem.second.locked)
  1060. node->attachTo(elem.second.artifact);
  1061. }