2
0

CArtHandler.cpp 32 KB

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