CArtHandler.cpp 33 KB

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