CArtHandler.cpp 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189
  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 "NetPacksBase.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. 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::getRandomArt(int flags)
  372. {
  373. return getArtSync(ran(), flags, true);
  374. }
  375. ArtifactID CArtHandler::getArtSync (ui32 rand, int flags, bool erasePicked)
  376. {
  377. auto getAllowedArts = [&](std::vector<ConstTransitivePtr<CArtifact> > &out, std::vector<CArtifact*> *arts, CArtifact::EartClass flag)
  378. {
  379. if (arts->empty()) //restock available arts
  380. fillList(*arts, flag);
  381. for (auto & arts_i : *arts)
  382. {
  383. CArtifact *art = arts_i;
  384. out.push_back(art);
  385. }
  386. };
  387. auto getAllowed = [&](std::vector<ConstTransitivePtr<CArtifact> > &out)
  388. {
  389. if (flags & CArtifact::ART_TREASURE)
  390. getAllowedArts (out, &treasures, CArtifact::ART_TREASURE);
  391. if (flags & CArtifact::ART_MINOR)
  392. getAllowedArts (out, &minors, CArtifact::ART_MINOR);
  393. if (flags & CArtifact::ART_MAJOR)
  394. getAllowedArts (out, &majors, CArtifact::ART_MAJOR);
  395. if (flags & CArtifact::ART_RELIC)
  396. getAllowedArts (out, &relics, CArtifact::ART_RELIC);
  397. if (!out.size()) //no artifact of specified rarity, we need to take another one
  398. {
  399. getAllowedArts (out, &treasures, CArtifact::ART_TREASURE);
  400. getAllowedArts (out, &minors, CArtifact::ART_MINOR);
  401. getAllowedArts (out, &majors, CArtifact::ART_MAJOR);
  402. getAllowedArts (out, &relics, CArtifact::ART_RELIC);
  403. }
  404. if (!out.size()) //no arts are available at all
  405. {
  406. out.resize (64);
  407. std::fill_n (out.begin(), 64, artifacts[2]); //Give Grail - this can't be banned (hopefully)
  408. }
  409. };
  410. std::vector<ConstTransitivePtr<CArtifact> > out;
  411. getAllowed(out);
  412. ArtifactID artID = out[rand % out.size()]->id;
  413. if(erasePicked)
  414. erasePickedArt (artID);
  415. return artID;
  416. }
  417. Bonus *createBonus(Bonus::BonusType type, int val, int subtype, Bonus::ValueType valType, shared_ptr<ILimiter> limiter = shared_ptr<ILimiter>(), int additionalInfo = 0)
  418. {
  419. auto added = new Bonus(Bonus::PERMANENT,type,Bonus::ARTIFACT,val,-1,subtype);
  420. added->additionalInfo = additionalInfo;
  421. added->valType = valType;
  422. added->limiter = limiter;
  423. return added;
  424. }
  425. Bonus *createBonus(Bonus::BonusType type, int val, int subtype, shared_ptr<IPropagator> propagator = shared_ptr<IPropagator>(), int additionalInfo = 0)
  426. {
  427. auto added = new Bonus(Bonus::PERMANENT,type,Bonus::ARTIFACT,val,-1,subtype);
  428. added->additionalInfo = additionalInfo;
  429. added->valType = Bonus::BASE_NUMBER;
  430. added->propagator = propagator;
  431. return added;
  432. }
  433. void CArtHandler::giveArtBonus( ArtifactID aid, Bonus::BonusType type, int val, int subtype, Bonus::ValueType valType, shared_ptr<ILimiter> limiter, int additionalInfo)
  434. {
  435. giveArtBonus(aid, createBonus(type, val, subtype, valType, limiter, additionalInfo));
  436. }
  437. void CArtHandler::giveArtBonus(ArtifactID aid, Bonus::BonusType type, int val, int subtype, shared_ptr<IPropagator> propagator /*= nullptr*/, int additionalInfo)
  438. {
  439. giveArtBonus(aid, createBonus(type, val, subtype, propagator, additionalInfo));
  440. }
  441. void CArtHandler::giveArtBonus(ArtifactID aid, Bonus *bonus)
  442. {
  443. bonus->sid = aid;
  444. if(bonus->subtype == Bonus::MORALE || bonus->type == Bonus::LUCK)
  445. bonus->description = artifacts[aid]->Name() + (bonus->val > 0 ? " +" : " ") + boost::lexical_cast<std::string>(bonus->val);
  446. else
  447. bonus->description = artifacts[aid]->Name();
  448. artifacts[aid]->addNewBonus(bonus);
  449. }
  450. void CArtHandler::makeItCreatureArt (CArtifact * a, bool onlyCreature /*=true*/)
  451. {
  452. if (onlyCreature)
  453. {
  454. a->possibleSlots[ArtBearer::HERO].clear();
  455. a->possibleSlots[ArtBearer::COMMANDER].clear();
  456. }
  457. a->possibleSlots[ArtBearer::CREATURE].push_back(ArtifactPosition::CREATURE_SLOT);
  458. }
  459. void CArtHandler::makeItCreatureArt (ArtifactID aid, bool onlyCreature /*=true*/)
  460. {
  461. CArtifact *a = artifacts[aid];
  462. makeItCreatureArt (a, onlyCreature);
  463. }
  464. void CArtHandler::makeItCommanderArt (CArtifact * a, bool onlyCommander /*= true*/ )
  465. {
  466. if (onlyCommander)
  467. {
  468. a->possibleSlots[ArtBearer::HERO].clear();
  469. a->possibleSlots[ArtBearer::CREATURE].clear();
  470. }
  471. for (int i = ArtifactPosition::COMMANDER1; i <= ArtifactPosition::COMMANDER6; ++i)
  472. a->possibleSlots[ArtBearer::COMMANDER].push_back(ArtifactPosition(i));
  473. }
  474. void CArtHandler::makeItCommanderArt( ArtifactID aid, bool onlyCommander /*= true*/ )
  475. {
  476. CArtifact *a = artifacts[aid];
  477. makeItCommanderArt (a, onlyCommander);
  478. }
  479. bool CArtHandler::legalArtifact(ArtifactID id)
  480. {
  481. auto art = artifacts[id];
  482. //assert ( (!art->constituents) || art->constituents->size() ); //artifacts is not combined or has some components
  483. return (art->possibleSlots[ArtBearer::HERO].size() ||
  484. (art->possibleSlots[ArtBearer::COMMANDER].size() && VLC->modh->modules.COMMANDERS) ||
  485. (art->possibleSlots[ArtBearer::CREATURE].size() && VLC->modh->modules.STACK_ARTIFACT)) &&
  486. !(art->constituents); //no combo artifacts spawning
  487. }
  488. void CArtHandler::initAllowedArtifactsList(const std::vector<bool> &allowed)
  489. {
  490. allowedArtifacts.clear();
  491. treasures.clear();
  492. minors.clear();
  493. majors.clear();
  494. relics.clear();
  495. for (ArtifactID i=ArtifactID::SPELLBOOK; i<ArtifactID::ART_SELECTION; i.advance(1))
  496. {
  497. if (allowed[i] && legalArtifact(i))
  498. allowedArtifacts.push_back(artifacts[i]);
  499. }
  500. if (VLC->modh->modules.COMMANDERS) //allow all commander artifacts for testing
  501. {
  502. for (int i = 146; i <= 155; ++i)
  503. {
  504. allowedArtifacts.push_back(artifacts[i]);
  505. }
  506. }
  507. for (int i = GameConstants::ARTIFACTS_QUANTITY; i < artifacts.size(); ++i) //allow all new artifacts by default
  508. {
  509. if (legalArtifact(ArtifactID(i)))
  510. allowedArtifacts.push_back(artifacts[i]);
  511. //keep im mind that artifact can be worn by more than one type of bearer
  512. }
  513. }
  514. std::vector<bool> CArtHandler::getDefaultAllowed() const
  515. {
  516. std::vector<bool> allowedArtifacts;
  517. allowedArtifacts.resize(127, true);
  518. allowedArtifacts.resize(141, false);
  519. allowedArtifacts.resize(GameConstants::ARTIFACTS_QUANTITY, true);
  520. return allowedArtifacts;
  521. }
  522. void CArtHandler::erasePickedArt(ArtifactID id)
  523. {
  524. CArtifact *art = artifacts[id];
  525. if(auto artifactList = listFromClass(art->aClass))
  526. {
  527. if(artifactList->empty())
  528. fillList(*artifactList, art->aClass);
  529. auto itr = vstd::find(*artifactList, art);
  530. if(itr != artifactList->end())
  531. {
  532. artifactList->erase(itr);
  533. }
  534. else
  535. logGlobal->warnStream() << "Problem: cannot erase artifact " << art->Name() << " from list, it was not present";
  536. }
  537. else
  538. logGlobal->warnStream() << "Problem: cannot find list for artifact " << art->Name() << ", strange class. (special?)";
  539. }
  540. boost::optional<std::vector<CArtifact*>&> CArtHandler::listFromClass( CArtifact::EartClass artifactClass )
  541. {
  542. switch(artifactClass)
  543. {
  544. case CArtifact::ART_TREASURE:
  545. return treasures;
  546. case CArtifact::ART_MINOR:
  547. return minors;
  548. case CArtifact::ART_MAJOR:
  549. return majors;
  550. case CArtifact::ART_RELIC:
  551. return relics;
  552. default: //special artifacts should not be erased
  553. return nullptr;
  554. }
  555. }
  556. void CArtHandler::fillList( std::vector<CArtifact*> &listToBeFilled, CArtifact::EartClass artifactClass )
  557. {
  558. assert(listToBeFilled.empty());
  559. for (auto & elem : allowedArtifacts)
  560. {
  561. if (elem->aClass == artifactClass)
  562. listToBeFilled.push_back(elem);
  563. }
  564. }
  565. void CArtHandler::afterLoadFinalization()
  566. {
  567. //All artifacts have their id, so we can properly update their bonuses' source ids.
  568. for(auto &art : artifacts)
  569. {
  570. for(auto &bonus : art->getExportedBonusList())
  571. {
  572. assert(art == artifacts[art->id]);
  573. assert(bonus->source == Bonus::ARTIFACT);
  574. bonus->sid = art->id;
  575. }
  576. }
  577. //Note: "10" is used here because H3 text files don't define any template for art with ID 0
  578. ObjectTemplate base = VLC->dobjinfo->pickCandidates(Obj::ARTIFACT, 10).front();
  579. for (CArtifact * art : artifacts)
  580. {
  581. if (!art->advMapDef.empty())
  582. {
  583. base.animationFile = art->advMapDef;
  584. base.subid = art->id;
  585. // replace existing (if any) and add new template.
  586. // Necessary for objects added via mods that don't have any templates in H3
  587. VLC->dobjinfo->eraseAll(Obj::ARTIFACT, art->id);
  588. VLC->dobjinfo->registerTemplate(base);
  589. }
  590. }
  591. }
  592. CArtifactInstance::CArtifactInstance()
  593. {
  594. init();
  595. }
  596. CArtifactInstance::CArtifactInstance( CArtifact *Art)
  597. {
  598. init();
  599. setType(Art);
  600. }
  601. void CArtifactInstance::setType( CArtifact *Art )
  602. {
  603. artType = Art;
  604. attachTo(Art);
  605. }
  606. std::string CArtifactInstance::nodeName() const
  607. {
  608. return "Artifact instance of " + (artType ? artType->Name() : std::string("uninitialized")) + " type";
  609. }
  610. CArtifactInstance * CArtifactInstance::createScroll( const CSpell *s)
  611. {
  612. auto ret = new CArtifactInstance(VLC->arth->artifacts[1]);
  613. auto b = new Bonus(Bonus::PERMANENT, Bonus::SPELL, Bonus::ARTIFACT_INSTANCE, -1, 1, s->id);
  614. ret->addNewBonus(b);
  615. return ret;
  616. }
  617. void CArtifactInstance::init()
  618. {
  619. id = ArtifactInstanceID();
  620. id = static_cast<ArtifactInstanceID>(ArtifactID::NONE); //to be randomized
  621. setNodeType(ARTIFACT_INSTANCE);
  622. }
  623. ArtifactPosition CArtifactInstance::firstAvailableSlot(const CArtifactSet *h) const
  624. {
  625. for(auto slot : artType->possibleSlots.at(h->bearerType()))
  626. {
  627. if(canBePutAt(h, slot)) //if(artType->fitsAt(h->artifWorn, slot))
  628. {
  629. //we've found a free suitable slot.
  630. return slot;
  631. }
  632. }
  633. //if haven't find proper slot, use backpack
  634. return firstBackpackSlot(h);
  635. }
  636. ArtifactPosition CArtifactInstance::firstBackpackSlot(const CArtifactSet *h) const
  637. {
  638. if(!artType->isBig()) //discard big artifact
  639. return ArtifactPosition(
  640. GameConstants::BACKPACK_START + h->artifactsInBackpack.size());
  641. return ArtifactPosition::PRE_FIRST;
  642. }
  643. bool CArtifactInstance::canBePutAt(const ArtifactLocation & al, bool assumeDestRemoved /*= false*/) const
  644. {
  645. return canBePutAt(al.getHolderArtSet(), al.slot, assumeDestRemoved);
  646. }
  647. bool CArtifactInstance::canBePutAt(const CArtifactSet *artSet, ArtifactPosition slot, bool assumeDestRemoved /*= false*/) const
  648. {
  649. if(slot >= GameConstants::BACKPACK_START)
  650. {
  651. if(artType->isBig())
  652. return false;
  653. //TODO backpack limit
  654. return true;
  655. }
  656. auto possibleSlots = artType->possibleSlots.find(artSet->bearerType());
  657. if(possibleSlots == artType->possibleSlots.end())
  658. {
  659. logGlobal->warnStream() << "Warning: artifact " << artType->Name() << " doesn't have defined allowed slots for bearer of type "
  660. << artSet->bearerType();
  661. return false;
  662. }
  663. if(!vstd::contains(possibleSlots->second, slot))
  664. return false;
  665. return artSet->isPositionFree(slot, assumeDestRemoved);
  666. }
  667. void CArtifactInstance::putAt(ArtifactLocation al)
  668. {
  669. assert(canBePutAt(al));
  670. al.getHolderArtSet()->setNewArtSlot(al.slot, this, false);
  671. if(al.slot < GameConstants::BACKPACK_START)
  672. al.getHolderNode()->attachTo(this);
  673. }
  674. void CArtifactInstance::removeFrom(ArtifactLocation al)
  675. {
  676. assert(al.getHolderArtSet()->getArt(al.slot) == this);
  677. al.getHolderArtSet()->eraseArtSlot(al.slot);
  678. if(al.slot < GameConstants::BACKPACK_START)
  679. al.getHolderNode()->detachFrom(this);
  680. //TODO delete me?
  681. }
  682. bool CArtifactInstance::canBeDisassembled() const
  683. {
  684. return bool(artType->constituents);
  685. }
  686. std::vector<const CArtifact *> CArtifactInstance::assemblyPossibilities(const CArtifactSet *h) const
  687. {
  688. std::vector<const CArtifact *> ret;
  689. if(artType->constituents) //combined artifact already: no combining of combined artifacts... for now.
  690. return ret;
  691. for(const CArtifact * artifact : artType->constituentOf)
  692. {
  693. assert(artifact->constituents);
  694. bool possible = true;
  695. for(const CArtifact * constituent : *artifact->constituents) //check if all constituents are available
  696. {
  697. if(!h->hasArt(constituent->id, true)) //constituent must be equipped
  698. {
  699. possible = false;
  700. break;
  701. }
  702. }
  703. if(possible)
  704. ret.push_back(artifact);
  705. }
  706. return ret;
  707. }
  708. void CArtifactInstance::move(ArtifactLocation src, ArtifactLocation dst)
  709. {
  710. removeFrom(src);
  711. putAt(dst);
  712. }
  713. CArtifactInstance * CArtifactInstance::createNewArtifactInstance(CArtifact *Art)
  714. {
  715. if(!Art->constituents)
  716. {
  717. auto ret = new CArtifactInstance(Art);
  718. if (dynamic_cast<CGrowingArtifact *>(Art))
  719. {
  720. auto bonus = new Bonus;
  721. bonus->type = Bonus::LEVEL_COUNTER;
  722. bonus->val = 0;
  723. ret->addNewBonus (bonus);
  724. }
  725. return ret;
  726. }
  727. else
  728. {
  729. auto ret = new CCombinedArtifactInstance(Art);
  730. ret->createConstituents();
  731. return ret;
  732. }
  733. }
  734. CArtifactInstance * CArtifactInstance::createNewArtifactInstance(int aid)
  735. {
  736. return createNewArtifactInstance(VLC->arth->artifacts[aid]);
  737. }
  738. void CArtifactInstance::deserializationFix()
  739. {
  740. setType(artType);
  741. }
  742. SpellID CArtifactInstance::getGivenSpellID() const
  743. {
  744. const Bonus * b = getBonusLocalFirst(Selector::type(Bonus::SPELL));
  745. if(!b)
  746. {
  747. logGlobal->warnStream() << "Warning: " << nodeName() << " doesn't bear any spell!";
  748. return SpellID::NONE;
  749. }
  750. return SpellID(b->subtype);
  751. }
  752. bool CArtifactInstance::isPart(const CArtifactInstance *supposedPart) const
  753. {
  754. return supposedPart == this;
  755. }
  756. bool CCombinedArtifactInstance::canBePutAt(const CArtifactSet *artSet, ArtifactPosition slot, bool assumeDestRemoved /*= false*/) const
  757. {
  758. bool canMainArtifactBePlaced = CArtifactInstance::canBePutAt(artSet, slot, assumeDestRemoved);
  759. if(!canMainArtifactBePlaced)
  760. return false; //no is no...
  761. if(slot >= GameConstants::BACKPACK_START)
  762. return true; //we can always remove combined art to the backapck
  763. assert(artType->constituents);
  764. std::vector<ConstituentInfo> constituentsToBePlaced = constituentsInfo; //we'll remove constituents from that list, as we find a suitable slot for them
  765. //it may be that we picked a combined artifact in hero screen (though technically it's still there) to move it
  766. //so we remove from the list all constituents that are already present on dst hero in the form of locks
  767. for(const ConstituentInfo &constituent : constituentsInfo)
  768. {
  769. if(constituent.art == artSet->getArt(constituent.slot, false)) //no need to worry about locked constituent
  770. constituentsToBePlaced -= constituent;
  771. }
  772. //we iterate over all active slots and check if constituents fits them
  773. for (int i = 0; i < GameConstants::BACKPACK_START; i++)
  774. {
  775. for(auto art = constituentsToBePlaced.begin(); art != constituentsToBePlaced.end(); art++)
  776. {
  777. 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
  778. {
  779. constituentsToBePlaced.erase(art);
  780. break;
  781. }
  782. }
  783. }
  784. return constituentsToBePlaced.empty();
  785. }
  786. bool CCombinedArtifactInstance::canBeDisassembled() const
  787. {
  788. return true;
  789. }
  790. CCombinedArtifactInstance::CCombinedArtifactInstance(CArtifact *Art)
  791. : CArtifactInstance(Art) //TODO: seems unued, but need to be written
  792. {
  793. }
  794. CCombinedArtifactInstance::CCombinedArtifactInstance()
  795. {
  796. }
  797. void CCombinedArtifactInstance::createConstituents()
  798. {
  799. assert(artType);
  800. assert(artType->constituents);
  801. for(const CArtifact * art : *artType->constituents)
  802. {
  803. addAsConstituent(CArtifactInstance::createNewArtifactInstance(art->id), ArtifactPosition::PRE_FIRST);
  804. }
  805. }
  806. void CCombinedArtifactInstance::addAsConstituent(CArtifactInstance *art, ArtifactPosition slot)
  807. {
  808. assert(vstd::contains(*artType->constituents, art->artType.get()));
  809. assert(art->getParentNodes().size() == 1 && art->getParentNodes().front() == art->artType);
  810. constituentsInfo.push_back(ConstituentInfo(art, slot));
  811. attachTo(art);
  812. }
  813. void CCombinedArtifactInstance::putAt(ArtifactLocation al)
  814. {
  815. if(al.slot >= GameConstants::BACKPACK_START)
  816. {
  817. CArtifactInstance::putAt(al);
  818. for(ConstituentInfo &ci : constituentsInfo)
  819. ci.slot = ArtifactPosition::PRE_FIRST;
  820. }
  821. else
  822. {
  823. CArtifactInstance *mainConstituent = figureMainConstituent(al); //it'll be replaced with combined artifact, not a lock
  824. CArtifactInstance::putAt(al); //puts combined art (this)
  825. for(ConstituentInfo &ci : constituentsInfo)
  826. {
  827. if(ci.art != mainConstituent)
  828. {
  829. const ArtifactLocation suggestedPos(al.artHolder, ci.slot);
  830. const bool inActiveSlot = vstd::isbetween(ci.slot, 0, GameConstants::BACKPACK_START);
  831. const bool suggestedPosValid = ci.art->canBePutAt(suggestedPos);
  832. ArtifactPosition pos = ArtifactPosition::PRE_FIRST;
  833. if(inActiveSlot && suggestedPosValid) //there is a valid suggestion where to place lock
  834. pos = ci.slot;
  835. else
  836. ci.slot = pos = ci.art->firstAvailableSlot(al.getHolderArtSet());
  837. assert(pos < GameConstants::BACKPACK_START);
  838. al.getHolderArtSet()->setNewArtSlot(pos, ci.art, true); //sets as lock
  839. }
  840. else
  841. {
  842. ci.slot = ArtifactPosition::PRE_FIRST;
  843. }
  844. }
  845. }
  846. }
  847. void CCombinedArtifactInstance::removeFrom(ArtifactLocation al)
  848. {
  849. if(al.slot >= GameConstants::BACKPACK_START)
  850. {
  851. CArtifactInstance::removeFrom(al);
  852. }
  853. else
  854. {
  855. for(ConstituentInfo &ci : constituentsInfo)
  856. {
  857. if(ci.slot >= 0)
  858. {
  859. al.getHolderArtSet()->eraseArtSlot(ci.slot);
  860. ci.slot = ArtifactPosition::PRE_FIRST;
  861. }
  862. else
  863. {
  864. //main constituent
  865. CArtifactInstance::removeFrom(al);
  866. }
  867. }
  868. }
  869. }
  870. CArtifactInstance * CCombinedArtifactInstance::figureMainConstituent(const ArtifactLocation al)
  871. {
  872. CArtifactInstance *mainConstituent = nullptr; //it'll be replaced with combined artifact, not a lock
  873. for(ConstituentInfo &ci : constituentsInfo)
  874. if(ci.slot == al.slot)
  875. mainConstituent = ci.art;
  876. if(!mainConstituent)
  877. {
  878. for(ConstituentInfo &ci : constituentsInfo)
  879. {
  880. if(vstd::contains(ci.art->artType->possibleSlots[al.getHolderArtSet()->bearerType()], al.slot))
  881. {
  882. mainConstituent = ci.art;
  883. }
  884. }
  885. }
  886. return mainConstituent;
  887. }
  888. void CCombinedArtifactInstance::deserializationFix()
  889. {
  890. for(ConstituentInfo &ci : constituentsInfo)
  891. attachTo(ci.art);
  892. }
  893. bool CCombinedArtifactInstance::isPart(const CArtifactInstance *supposedPart) const
  894. {
  895. bool me = CArtifactInstance::isPart(supposedPart);
  896. if(me)
  897. return true;
  898. //check for constituents
  899. for(const ConstituentInfo &constituent : constituentsInfo)
  900. if(constituent.art == supposedPart)
  901. return true;
  902. return false;
  903. }
  904. CCombinedArtifactInstance::ConstituentInfo::ConstituentInfo(CArtifactInstance *Art /*= nullptr*/, ArtifactPosition Slot /*= -1*/)
  905. {
  906. art = Art;
  907. slot = Slot;
  908. }
  909. bool CCombinedArtifactInstance::ConstituentInfo::operator==(const ConstituentInfo &rhs) const
  910. {
  911. return art == rhs.art && slot == rhs.slot;
  912. }
  913. const CArtifactInstance* CArtifactSet::getArt(ArtifactPosition pos, bool excludeLocked /*= true*/) const
  914. {
  915. if(const ArtSlotInfo *si = getSlot(pos))
  916. {
  917. if(si->artifact && (!excludeLocked || !si->locked))
  918. return si->artifact;
  919. }
  920. return nullptr;
  921. }
  922. CArtifactInstance* CArtifactSet::getArt(ArtifactPosition pos, bool excludeLocked /*= true*/)
  923. {
  924. return const_cast<CArtifactInstance*>((const_cast<const CArtifactSet*>(this))->getArt(pos, excludeLocked));
  925. }
  926. ArtifactPosition CArtifactSet::getArtPos(int aid, bool onlyWorn /*= true*/) const
  927. {
  928. for(auto i = artifactsWorn.cbegin(); i != artifactsWorn.cend(); i++)
  929. if(i->second.artifact->artType->id == aid)
  930. return i->first;
  931. if(onlyWorn)
  932. return ArtifactPosition::PRE_FIRST;
  933. for(int i = 0; i < artifactsInBackpack.size(); i++)
  934. if(artifactsInBackpack[i].artifact->artType->id == aid)
  935. return ArtifactPosition(GameConstants::BACKPACK_START + i);
  936. return ArtifactPosition::PRE_FIRST;
  937. }
  938. ArtifactPosition CArtifactSet::getArtPos(const CArtifactInstance *art) const
  939. {
  940. for(auto i : artifactsWorn)
  941. if(i.second.artifact == art)
  942. return i.first;
  943. for(int i = 0; i < artifactsInBackpack.size(); i++)
  944. if(artifactsInBackpack[i].artifact == art)
  945. return ArtifactPosition(GameConstants::BACKPACK_START + i);
  946. return ArtifactPosition::PRE_FIRST;
  947. }
  948. const CArtifactInstance * CArtifactSet::getArtByInstanceId( ArtifactInstanceID artInstId ) const
  949. {
  950. for(auto i : artifactsWorn)
  951. if(i.second.artifact->id == artInstId)
  952. return i.second.artifact;
  953. for(auto i : artifactsInBackpack)
  954. if(i.artifact->id == artInstId)
  955. return i.artifact;
  956. return nullptr;
  957. }
  958. bool CArtifactSet::hasArt(ui32 aid, bool onlyWorn /*= false*/) const
  959. {
  960. return getArtPos(aid, onlyWorn) != ArtifactPosition::PRE_FIRST;
  961. }
  962. const ArtSlotInfo * CArtifactSet::getSlot(ArtifactPosition pos) const
  963. {
  964. if(vstd::contains(artifactsWorn, pos))
  965. return &artifactsWorn.at(pos);
  966. if(pos >= ArtifactPosition::AFTER_LAST )
  967. {
  968. int backpackPos = (int)pos - GameConstants::BACKPACK_START;
  969. if(backpackPos < 0 || backpackPos >= artifactsInBackpack.size())
  970. return nullptr;
  971. else
  972. return &artifactsInBackpack[backpackPos];
  973. }
  974. return nullptr;
  975. }
  976. bool CArtifactSet::isPositionFree(ArtifactPosition pos, bool onlyLockCheck /*= false*/) const
  977. {
  978. if(const ArtSlotInfo *s = getSlot(pos))
  979. return (onlyLockCheck || !s->artifact) && !s->locked;
  980. return true; //no slot means not used
  981. }
  982. si32 CArtifactSet::getArtTypeId(ArtifactPosition pos) const
  983. {
  984. const CArtifactInstance * const a = getArt(pos);
  985. if(!a)
  986. {
  987. logGlobal->warnStream() << (dynamic_cast<const CGHeroInstance*>(this))->name << " has no artifact at " << pos << " (getArtTypeId)";
  988. return -1;
  989. }
  990. return a->artType->id;
  991. }
  992. CArtifactSet::~CArtifactSet()
  993. {
  994. }
  995. ArtSlotInfo & CArtifactSet::retreiveNewArtSlot(ArtifactPosition slot)
  996. {
  997. assert(!vstd::contains(artifactsWorn, slot));
  998. ArtSlotInfo &ret = slot < GameConstants::BACKPACK_START
  999. ? artifactsWorn[slot]
  1000. : *artifactsInBackpack.insert(artifactsInBackpack.begin() + (slot - GameConstants::BACKPACK_START), ArtSlotInfo());
  1001. return ret;
  1002. }
  1003. void CArtifactSet::setNewArtSlot(ArtifactPosition slot, CArtifactInstance *art, bool locked)
  1004. {
  1005. ArtSlotInfo &asi = retreiveNewArtSlot(slot);
  1006. asi.artifact = art;
  1007. asi.locked = locked;
  1008. }
  1009. void CArtifactSet::eraseArtSlot(ArtifactPosition slot)
  1010. {
  1011. if(slot < GameConstants::BACKPACK_START)
  1012. {
  1013. artifactsWorn.erase(slot);
  1014. }
  1015. else
  1016. {
  1017. slot = ArtifactPosition(slot - GameConstants::BACKPACK_START);
  1018. artifactsInBackpack.erase(artifactsInBackpack.begin() + slot);
  1019. }
  1020. }
  1021. void CArtifactSet::artDeserializationFix(CBonusSystemNode *node)
  1022. {
  1023. for(auto & elem : artifactsWorn)
  1024. if(elem.second.artifact && !elem.second.locked)
  1025. node->attachTo(elem.second.artifact);
  1026. }