CArtHandler.cpp 32 KB

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