CArtHandler.cpp 33 KB

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