CArtHandler.cpp 33 KB

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