CArtHandler.cpp 33 KB

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