CArtHandler.cpp 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195
  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. //Note: "10" is used here because H3 text files don't define any template for art with ID 0
  584. ObjectTemplate base = VLC->dobjinfo->pickCandidates(Obj::ARTIFACT, 10).front();
  585. for (CArtifact * art : artifacts)
  586. {
  587. if (!art->advMapDef.empty())
  588. {
  589. base.animationFile = art->advMapDef;
  590. base.subid = art->id;
  591. // replace existing (if any) and add new template.
  592. // Necessary for objects added via mods that don't have any templates in H3
  593. VLC->dobjinfo->eraseAll(Obj::ARTIFACT, art->id);
  594. VLC->dobjinfo->registerTemplate(base);
  595. }
  596. }
  597. }
  598. CArtifactInstance::CArtifactInstance()
  599. {
  600. init();
  601. }
  602. CArtifactInstance::CArtifactInstance( CArtifact *Art)
  603. {
  604. init();
  605. setType(Art);
  606. }
  607. void CArtifactInstance::setType( CArtifact *Art )
  608. {
  609. artType = Art;
  610. attachTo(Art);
  611. }
  612. std::string CArtifactInstance::nodeName() const
  613. {
  614. return "Artifact instance of " + (artType ? artType->Name() : std::string("uninitialized")) + " type";
  615. }
  616. CArtifactInstance * CArtifactInstance::createScroll( const CSpell *s)
  617. {
  618. auto ret = new CArtifactInstance(VLC->arth->artifacts[1]);
  619. auto b = new Bonus(Bonus::PERMANENT, Bonus::SPELL, Bonus::ARTIFACT_INSTANCE, -1, 1, s->id);
  620. ret->addNewBonus(b);
  621. return ret;
  622. }
  623. void CArtifactInstance::init()
  624. {
  625. id = ArtifactInstanceID();
  626. id = static_cast<ArtifactInstanceID>(ArtifactID::NONE); //to be randomized
  627. setNodeType(ARTIFACT_INSTANCE);
  628. }
  629. ArtifactPosition CArtifactInstance::firstAvailableSlot(const CArtifactSet *h) const
  630. {
  631. for(auto slot : artType->possibleSlots.at(h->bearerType()))
  632. {
  633. if(canBePutAt(h, slot)) //if(artType->fitsAt(h->artifWorn, slot))
  634. {
  635. //we've found a free suitable slot.
  636. return slot;
  637. }
  638. }
  639. //if haven't find proper slot, use backpack
  640. return firstBackpackSlot(h);
  641. }
  642. ArtifactPosition CArtifactInstance::firstBackpackSlot(const CArtifactSet *h) const
  643. {
  644. if(!artType->isBig()) //discard big artifact
  645. return ArtifactPosition(
  646. GameConstants::BACKPACK_START + h->artifactsInBackpack.size());
  647. return ArtifactPosition::PRE_FIRST;
  648. }
  649. bool CArtifactInstance::canBePutAt(const ArtifactLocation & al, bool assumeDestRemoved /*= false*/) const
  650. {
  651. return canBePutAt(al.getHolderArtSet(), al.slot, assumeDestRemoved);
  652. }
  653. bool CArtifactInstance::canBePutAt(const CArtifactSet *artSet, ArtifactPosition slot, bool assumeDestRemoved /*= false*/) const
  654. {
  655. if(slot >= GameConstants::BACKPACK_START)
  656. {
  657. if(artType->isBig())
  658. return false;
  659. //TODO backpack limit
  660. return true;
  661. }
  662. auto possibleSlots = artType->possibleSlots.find(artSet->bearerType());
  663. if(possibleSlots == artType->possibleSlots.end())
  664. {
  665. logGlobal->warnStream() << "Warning: artifact " << artType->Name() << " doesn't have defined allowed slots for bearer of type "
  666. << artSet->bearerType();
  667. return false;
  668. }
  669. if(!vstd::contains(possibleSlots->second, slot))
  670. return false;
  671. return artSet->isPositionFree(slot, assumeDestRemoved);
  672. }
  673. void CArtifactInstance::putAt(ArtifactLocation al)
  674. {
  675. assert(canBePutAt(al));
  676. al.getHolderArtSet()->setNewArtSlot(al.slot, this, false);
  677. if(al.slot < GameConstants::BACKPACK_START)
  678. al.getHolderNode()->attachTo(this);
  679. }
  680. void CArtifactInstance::removeFrom(ArtifactLocation al)
  681. {
  682. assert(al.getHolderArtSet()->getArt(al.slot) == this);
  683. al.getHolderArtSet()->eraseArtSlot(al.slot);
  684. if(al.slot < GameConstants::BACKPACK_START)
  685. al.getHolderNode()->detachFrom(this);
  686. //TODO delete me?
  687. }
  688. bool CArtifactInstance::canBeDisassembled() const
  689. {
  690. return bool(artType->constituents);
  691. }
  692. std::vector<const CArtifact *> CArtifactInstance::assemblyPossibilities(const CArtifactSet *h) const
  693. {
  694. std::vector<const CArtifact *> ret;
  695. if(artType->constituents) //combined artifact already: no combining of combined artifacts... for now.
  696. return ret;
  697. for(const CArtifact * artifact : artType->constituentOf)
  698. {
  699. assert(artifact->constituents);
  700. bool possible = true;
  701. for(const CArtifact * constituent : *artifact->constituents) //check if all constituents are available
  702. {
  703. if(!h->hasArt(constituent->id, true)) //constituent must be equipped
  704. {
  705. possible = false;
  706. break;
  707. }
  708. }
  709. if(possible)
  710. ret.push_back(artifact);
  711. }
  712. return ret;
  713. }
  714. void CArtifactInstance::move(ArtifactLocation src, ArtifactLocation dst)
  715. {
  716. removeFrom(src);
  717. putAt(dst);
  718. }
  719. CArtifactInstance * CArtifactInstance::createNewArtifactInstance(CArtifact *Art)
  720. {
  721. if(!Art->constituents)
  722. {
  723. auto ret = new CArtifactInstance(Art);
  724. if (dynamic_cast<CGrowingArtifact *>(Art))
  725. {
  726. auto bonus = new Bonus;
  727. bonus->type = Bonus::LEVEL_COUNTER;
  728. bonus->val = 0;
  729. ret->addNewBonus (bonus);
  730. }
  731. return ret;
  732. }
  733. else
  734. {
  735. auto ret = new CCombinedArtifactInstance(Art);
  736. ret->createConstituents();
  737. return ret;
  738. }
  739. }
  740. CArtifactInstance * CArtifactInstance::createNewArtifactInstance(int aid)
  741. {
  742. return createNewArtifactInstance(VLC->arth->artifacts[aid]);
  743. }
  744. void CArtifactInstance::deserializationFix()
  745. {
  746. setType(artType);
  747. }
  748. SpellID CArtifactInstance::getGivenSpellID() const
  749. {
  750. const Bonus * b = getBonusLocalFirst(Selector::type(Bonus::SPELL));
  751. if(!b)
  752. {
  753. logGlobal->warnStream() << "Warning: " << nodeName() << " doesn't bear any spell!";
  754. return SpellID::NONE;
  755. }
  756. return SpellID(b->subtype);
  757. }
  758. bool CArtifactInstance::isPart(const CArtifactInstance *supposedPart) const
  759. {
  760. return supposedPart == this;
  761. }
  762. bool CCombinedArtifactInstance::canBePutAt(const CArtifactSet *artSet, ArtifactPosition slot, bool assumeDestRemoved /*= false*/) const
  763. {
  764. bool canMainArtifactBePlaced = CArtifactInstance::canBePutAt(artSet, slot, assumeDestRemoved);
  765. if(!canMainArtifactBePlaced)
  766. return false; //no is no...
  767. if(slot >= GameConstants::BACKPACK_START)
  768. return true; //we can always remove combined art to the backapck
  769. assert(artType->constituents);
  770. std::vector<ConstituentInfo> constituentsToBePlaced = constituentsInfo; //we'll remove constituents from that list, as we find a suitable slot for them
  771. //it may be that we picked a combined artifact in hero screen (though technically it's still there) to move it
  772. //so we remove from the list all constituents that are already present on dst hero in the form of locks
  773. for(const ConstituentInfo &constituent : constituentsInfo)
  774. {
  775. if(constituent.art == artSet->getArt(constituent.slot, false)) //no need to worry about locked constituent
  776. constituentsToBePlaced -= constituent;
  777. }
  778. //we iterate over all active slots and check if constituents fits them
  779. for (int i = 0; i < GameConstants::BACKPACK_START; i++)
  780. {
  781. for(auto art = constituentsToBePlaced.begin(); art != constituentsToBePlaced.end(); art++)
  782. {
  783. 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
  784. {
  785. constituentsToBePlaced.erase(art);
  786. break;
  787. }
  788. }
  789. }
  790. return constituentsToBePlaced.empty();
  791. }
  792. bool CCombinedArtifactInstance::canBeDisassembled() const
  793. {
  794. return true;
  795. }
  796. CCombinedArtifactInstance::CCombinedArtifactInstance(CArtifact *Art)
  797. : CArtifactInstance(Art) //TODO: seems unued, but need to be written
  798. {
  799. }
  800. CCombinedArtifactInstance::CCombinedArtifactInstance()
  801. {
  802. }
  803. void CCombinedArtifactInstance::createConstituents()
  804. {
  805. assert(artType);
  806. assert(artType->constituents);
  807. for(const CArtifact * art : *artType->constituents)
  808. {
  809. addAsConstituent(CArtifactInstance::createNewArtifactInstance(art->id), ArtifactPosition::PRE_FIRST);
  810. }
  811. }
  812. void CCombinedArtifactInstance::addAsConstituent(CArtifactInstance *art, ArtifactPosition slot)
  813. {
  814. assert(vstd::contains(*artType->constituents, art->artType.get()));
  815. assert(art->getParentNodes().size() == 1 && art->getParentNodes().front() == art->artType);
  816. constituentsInfo.push_back(ConstituentInfo(art, slot));
  817. attachTo(art);
  818. }
  819. void CCombinedArtifactInstance::putAt(ArtifactLocation al)
  820. {
  821. if(al.slot >= GameConstants::BACKPACK_START)
  822. {
  823. CArtifactInstance::putAt(al);
  824. for(ConstituentInfo &ci : constituentsInfo)
  825. ci.slot = ArtifactPosition::PRE_FIRST;
  826. }
  827. else
  828. {
  829. CArtifactInstance *mainConstituent = figureMainConstituent(al); //it'll be replaced with combined artifact, not a lock
  830. CArtifactInstance::putAt(al); //puts combined art (this)
  831. for(ConstituentInfo &ci : constituentsInfo)
  832. {
  833. if(ci.art != mainConstituent)
  834. {
  835. const ArtifactLocation suggestedPos(al.artHolder, ci.slot);
  836. const bool inActiveSlot = vstd::isbetween(ci.slot, 0, GameConstants::BACKPACK_START);
  837. const bool suggestedPosValid = ci.art->canBePutAt(suggestedPos);
  838. ArtifactPosition pos = ArtifactPosition::PRE_FIRST;
  839. if(inActiveSlot && suggestedPosValid) //there is a valid suggestion where to place lock
  840. pos = ci.slot;
  841. else
  842. ci.slot = pos = ci.art->firstAvailableSlot(al.getHolderArtSet());
  843. assert(pos < GameConstants::BACKPACK_START);
  844. al.getHolderArtSet()->setNewArtSlot(pos, ci.art, true); //sets as lock
  845. }
  846. else
  847. {
  848. ci.slot = ArtifactPosition::PRE_FIRST;
  849. }
  850. }
  851. }
  852. }
  853. void CCombinedArtifactInstance::removeFrom(ArtifactLocation al)
  854. {
  855. if(al.slot >= GameConstants::BACKPACK_START)
  856. {
  857. CArtifactInstance::removeFrom(al);
  858. }
  859. else
  860. {
  861. for(ConstituentInfo &ci : constituentsInfo)
  862. {
  863. if(ci.slot >= 0)
  864. {
  865. al.getHolderArtSet()->eraseArtSlot(ci.slot);
  866. ci.slot = ArtifactPosition::PRE_FIRST;
  867. }
  868. else
  869. {
  870. //main constituent
  871. CArtifactInstance::removeFrom(al);
  872. }
  873. }
  874. }
  875. }
  876. CArtifactInstance * CCombinedArtifactInstance::figureMainConstituent(const ArtifactLocation al)
  877. {
  878. CArtifactInstance *mainConstituent = nullptr; //it'll be replaced with combined artifact, not a lock
  879. for(ConstituentInfo &ci : constituentsInfo)
  880. if(ci.slot == al.slot)
  881. mainConstituent = ci.art;
  882. if(!mainConstituent)
  883. {
  884. for(ConstituentInfo &ci : constituentsInfo)
  885. {
  886. if(vstd::contains(ci.art->artType->possibleSlots[al.getHolderArtSet()->bearerType()], al.slot))
  887. {
  888. mainConstituent = ci.art;
  889. }
  890. }
  891. }
  892. return mainConstituent;
  893. }
  894. void CCombinedArtifactInstance::deserializationFix()
  895. {
  896. for(ConstituentInfo &ci : constituentsInfo)
  897. attachTo(ci.art);
  898. }
  899. bool CCombinedArtifactInstance::isPart(const CArtifactInstance *supposedPart) const
  900. {
  901. bool me = CArtifactInstance::isPart(supposedPart);
  902. if(me)
  903. return true;
  904. //check for constituents
  905. for(const ConstituentInfo &constituent : constituentsInfo)
  906. if(constituent.art == supposedPart)
  907. return true;
  908. return false;
  909. }
  910. CCombinedArtifactInstance::ConstituentInfo::ConstituentInfo(CArtifactInstance *Art /*= nullptr*/, ArtifactPosition Slot /*= -1*/)
  911. {
  912. art = Art;
  913. slot = Slot;
  914. }
  915. bool CCombinedArtifactInstance::ConstituentInfo::operator==(const ConstituentInfo &rhs) const
  916. {
  917. return art == rhs.art && slot == rhs.slot;
  918. }
  919. const CArtifactInstance* CArtifactSet::getArt(ArtifactPosition pos, bool excludeLocked /*= true*/) const
  920. {
  921. if(const ArtSlotInfo *si = getSlot(pos))
  922. {
  923. if(si->artifact && (!excludeLocked || !si->locked))
  924. return si->artifact;
  925. }
  926. return nullptr;
  927. }
  928. CArtifactInstance* CArtifactSet::getArt(ArtifactPosition pos, bool excludeLocked /*= true*/)
  929. {
  930. return const_cast<CArtifactInstance*>((const_cast<const CArtifactSet*>(this))->getArt(pos, excludeLocked));
  931. }
  932. ArtifactPosition CArtifactSet::getArtPos(int aid, bool onlyWorn /*= true*/) const
  933. {
  934. for(auto i = artifactsWorn.cbegin(); i != artifactsWorn.cend(); i++)
  935. if(i->second.artifact->artType->id == aid)
  936. return i->first;
  937. if(onlyWorn)
  938. return ArtifactPosition::PRE_FIRST;
  939. for(int i = 0; i < artifactsInBackpack.size(); i++)
  940. if(artifactsInBackpack[i].artifact->artType->id == aid)
  941. return ArtifactPosition(GameConstants::BACKPACK_START + i);
  942. return ArtifactPosition::PRE_FIRST;
  943. }
  944. ArtifactPosition CArtifactSet::getArtPos(const CArtifactInstance *art) const
  945. {
  946. for(auto i : artifactsWorn)
  947. if(i.second.artifact == art)
  948. return i.first;
  949. for(int i = 0; i < artifactsInBackpack.size(); i++)
  950. if(artifactsInBackpack[i].artifact == art)
  951. return ArtifactPosition(GameConstants::BACKPACK_START + i);
  952. return ArtifactPosition::PRE_FIRST;
  953. }
  954. const CArtifactInstance * CArtifactSet::getArtByInstanceId( ArtifactInstanceID artInstId ) const
  955. {
  956. for(auto i : artifactsWorn)
  957. if(i.second.artifact->id == artInstId)
  958. return i.second.artifact;
  959. for(auto i : artifactsInBackpack)
  960. if(i.artifact->id == artInstId)
  961. return i.artifact;
  962. return nullptr;
  963. }
  964. bool CArtifactSet::hasArt(ui32 aid, bool onlyWorn /*= false*/) const
  965. {
  966. return getArtPos(aid, onlyWorn) != ArtifactPosition::PRE_FIRST;
  967. }
  968. const ArtSlotInfo * CArtifactSet::getSlot(ArtifactPosition pos) const
  969. {
  970. if(vstd::contains(artifactsWorn, pos))
  971. return &artifactsWorn.at(pos);
  972. if(pos >= ArtifactPosition::AFTER_LAST )
  973. {
  974. int backpackPos = (int)pos - GameConstants::BACKPACK_START;
  975. if(backpackPos < 0 || backpackPos >= artifactsInBackpack.size())
  976. return nullptr;
  977. else
  978. return &artifactsInBackpack[backpackPos];
  979. }
  980. return nullptr;
  981. }
  982. bool CArtifactSet::isPositionFree(ArtifactPosition pos, bool onlyLockCheck /*= false*/) const
  983. {
  984. if(const ArtSlotInfo *s = getSlot(pos))
  985. return (onlyLockCheck || !s->artifact) && !s->locked;
  986. return true; //no slot means not used
  987. }
  988. si32 CArtifactSet::getArtTypeId(ArtifactPosition pos) const
  989. {
  990. const CArtifactInstance * const a = getArt(pos);
  991. if(!a)
  992. {
  993. logGlobal->warnStream() << (dynamic_cast<const CGHeroInstance*>(this))->name << " has no artifact at " << pos << " (getArtTypeId)";
  994. return -1;
  995. }
  996. return a->artType->id;
  997. }
  998. CArtifactSet::~CArtifactSet()
  999. {
  1000. }
  1001. ArtSlotInfo & CArtifactSet::retreiveNewArtSlot(ArtifactPosition slot)
  1002. {
  1003. assert(!vstd::contains(artifactsWorn, slot));
  1004. ArtSlotInfo &ret = slot < GameConstants::BACKPACK_START
  1005. ? artifactsWorn[slot]
  1006. : *artifactsInBackpack.insert(artifactsInBackpack.begin() + (slot - GameConstants::BACKPACK_START), ArtSlotInfo());
  1007. return ret;
  1008. }
  1009. void CArtifactSet::setNewArtSlot(ArtifactPosition slot, CArtifactInstance *art, bool locked)
  1010. {
  1011. ArtSlotInfo &asi = retreiveNewArtSlot(slot);
  1012. asi.artifact = art;
  1013. asi.locked = locked;
  1014. }
  1015. void CArtifactSet::eraseArtSlot(ArtifactPosition slot)
  1016. {
  1017. if(slot < GameConstants::BACKPACK_START)
  1018. {
  1019. artifactsWorn.erase(slot);
  1020. }
  1021. else
  1022. {
  1023. slot = ArtifactPosition(slot - GameConstants::BACKPACK_START);
  1024. artifactsInBackpack.erase(artifactsInBackpack.begin() + slot);
  1025. }
  1026. }
  1027. void CArtifactSet::artDeserializationFix(CBonusSystemNode *node)
  1028. {
  1029. for(auto & elem : artifactsWorn)
  1030. if(elem.second.artifact && !elem.second.locked)
  1031. node->attachTo(elem.second.artifact);
  1032. }