CArtHandler.cpp 34 KB

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