CArtHandler.cpp 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376
  1. #include "StdInc.h"
  2. #include "CArtHandler.h"
  3. #include "Filesystem/CResourceLoader.h"
  4. #include "CGeneralTextHandler.h"
  5. #include <boost/random/linear_congruential.hpp>
  6. #include "VCMI_Lib.h"
  7. #include "CModHandler.h"
  8. #include "CSpellHandler.h"
  9. #include "CObjectHandler.h"
  10. #include "NetPacks.h"
  11. #include "GameConstants.h"
  12. using namespace boost::assign;
  13. /*
  14. * CArtHandler.cpp, part of VCMI engine
  15. *
  16. * Authors: listed in file AUTHORS in main folder
  17. *
  18. * License: GNU General Public License v2.0 or later
  19. * Full text of license available in license.txt file, in main folder
  20. *
  21. */
  22. extern boost::rand48 ran;
  23. const std::map<std::string, CArtifact::EartClass> artifactClassMap = boost::assign::map_list_of
  24. ("TREASURE", CArtifact::ART_TREASURE)
  25. ("MINOR", CArtifact::ART_MINOR)
  26. ("MAJOR", CArtifact::ART_MAJOR)
  27. ("RELIC", CArtifact::ART_RELIC)
  28. ("SPECIAL", CArtifact::ART_SPECIAL);
  29. #define ART_BEARER(x) ( #x, ArtBearer::x )
  30. const std::map<std::string, int> artifactBearerMap = boost::assign::map_list_of ART_BEARER_LIST;
  31. #undef ART_BEARER
  32. #define ART_POS(x) ( #x, ArtifactPosition::x )
  33. const std::map<std::string, ArtifactPosition> artifactPositionMap = boost::assign::map_list_of
  34. ART_POS(HEAD)
  35. ART_POS(SHOULDERS)
  36. ART_POS(NECK)
  37. ART_POS(RIGHT_HAND)
  38. ART_POS(LEFT_HAND)
  39. ART_POS(TORSO)
  40. ART_POS(RIGHT_RING)
  41. ART_POS(LEFT_RING)
  42. ART_POS(FEET)
  43. ART_POS(MISC1)
  44. ART_POS(MISC2)
  45. ART_POS(MISC3)
  46. ART_POS(MISC4)
  47. ART_POS(MISC5)
  48. ART_POS(MACH1)
  49. ART_POS(MACH2)
  50. ART_POS(MACH3)
  51. ART_POS(MACH4)
  52. ART_POS(SPELLBOOK); //no need to specify commander / stack position?
  53. const std::string & CArtifact::Name() const
  54. {
  55. return name;
  56. }
  57. const std::string & CArtifact::Description() const
  58. {
  59. return description;
  60. }
  61. const std::string & CArtifact::EventText() const
  62. {
  63. return eventText;
  64. }
  65. bool CArtifact::isBig () const
  66. {
  67. return VLC->arth->isBigArtifact(id);
  68. }
  69. // /**
  70. // * Checks whether the artifact fits at a given slot.
  71. // * @param artifWorn A hero's set of worn artifacts.
  72. // */
  73. // bool CArtifact::fitsAt (const std::map<ui16, const CArtifact*> &artifWorn, ui16 slotID) const
  74. // {
  75. // if (!vstd::contains(possibleSlots, slotID))
  76. // return false;
  77. //
  78. // // Can't put an artifact in a locked slot.
  79. // std::map<ui16, const CArtifact*>::const_iterator it = artifWorn.find(slotID);
  80. // if (it != artifWorn.end() && it->second->id == 145)
  81. // return false;
  82. //
  83. // // Check if a combination artifact fits.
  84. // // TODO: Might want a more general algorithm?
  85. // // Assumes that misc & rings fits only in their slots, and others in only one slot and no duplicates.
  86. // if (constituents != NULL)
  87. // {
  88. // std::map<ui16, const CArtifact*> tempArtifWorn = artifWorn;
  89. // const ui16 ringSlots[] = {6, 7};
  90. // const ui16 miscSlots[] = {9, 10, 11, 12, 18};
  91. // int rings = 0;
  92. // int misc = 0;
  93. //
  94. // VLC->arth->unequipArtifact(tempArtifWorn, slotID);
  95. //
  96. // BOOST_FOREACH(ui32 constituentID, *constituents)
  97. // {
  98. // const CArtifact& constituent = *VLC->arth->artifacts[constituentID];
  99. // const int slot = constituent.possibleSlots[0];
  100. //
  101. // if (slot == 6 || slot == 7)
  102. // rings++;
  103. // else if ((slot >= 9 && slot <= 12) || slot == 18)
  104. // misc++;
  105. // else if (tempArtifWorn.find(slot) != tempArtifWorn.end())
  106. // return false;
  107. // }
  108. //
  109. // // Ensure enough ring slots are free
  110. // for (int i = 0; i < sizeof(ringSlots)/sizeof(*ringSlots); i++)
  111. // {
  112. // if (tempArtifWorn.find(ringSlots[i]) == tempArtifWorn.end() || ringSlots[i] == slotID)
  113. // rings--;
  114. // }
  115. // if (rings > 0)
  116. // return false;
  117. //
  118. // // Ensure enough misc slots are free.
  119. // for (int i = 0; i < sizeof(miscSlots)/sizeof(*miscSlots); i++)
  120. // {
  121. // if (tempArtifWorn.find(miscSlots[i]) == tempArtifWorn.end() || miscSlots[i] == slotID)
  122. // misc--;
  123. // }
  124. // if (misc > 0)
  125. // return false;
  126. // }
  127. //
  128. // return true;
  129. // }
  130. // bool CArtifact::canBeAssembledTo (const std::map<ui16, const CArtifact*> &artifWorn, ui32 artifactID) const
  131. // {
  132. // if (constituentOf == NULL || !vstd::contains(*constituentOf, artifactID))
  133. // return false;
  134. //
  135. // const CArtifact &artifact = *VLC->arth->artifacts[artifactID];
  136. // assert(artifact.constituents);
  137. //
  138. // BOOST_FOREACH(ui32 constituentID, *artifact.constituents)
  139. // {
  140. // bool found = false;
  141. // for (std::map<ui16, const CArtifact*>::const_iterator it = artifWorn.begin(); it != artifWorn.end(); ++it)
  142. // {
  143. // if (it->second->id == constituentID)
  144. // {
  145. // found = true;
  146. // break;
  147. // }
  148. // }
  149. // if (!found)
  150. // return false;
  151. // }
  152. //
  153. // return true;
  154. // }
  155. CArtifact::CArtifact()
  156. {
  157. setNodeType(ARTIFACT);
  158. possibleSlots[ArtBearer::HERO]; //we want to generate map entry even if it will be empty
  159. possibleSlots[ArtBearer::CREATURE]; //we want to generate map entry even if it will be empty
  160. possibleSlots[ArtBearer::COMMANDER];
  161. constituents = NULL; //default pointer to zero
  162. constituentOf = NULL;
  163. }
  164. CArtifact::~CArtifact()
  165. {
  166. }
  167. int CArtifact::getArtClassSerial() const
  168. {
  169. if(id == 1)
  170. return 4;
  171. switch(aClass)
  172. {
  173. case ART_TREASURE:
  174. return 0;
  175. case ART_MINOR:
  176. return 1;
  177. case ART_MAJOR:
  178. return 2;
  179. case ART_RELIC:
  180. return 3;
  181. case ART_SPECIAL:
  182. return 5;
  183. }
  184. return -1;
  185. }
  186. std::string CArtifact::nodeName() const
  187. {
  188. return "Artifact: " + Name();
  189. }
  190. // void CArtifact::getParents(TCNodes &out, const CBonusSystemNode *root /*= NULL*/) const
  191. // {
  192. // //combined artifact carries bonuses from its parts
  193. // if(constituents)
  194. // {
  195. // BOOST_FOREACH(ui32 id, *constituents)
  196. // out.insert(VLC->arth->artifacts[id]);
  197. // }
  198. // }
  199. // void CScroll::Init()
  200. // {
  201. // // addNewBonus (Bonus (Bonus::PERMANENT, Bonus::SPELL, Bonus::ARTIFACT, 1, id, spellid, Bonus::INDEPENDENT_MAX));
  202. // // //boost::algorithm::replace_first(description, "[spell name]", VLC->spellh->spells[spellid].name);
  203. // }
  204. void CArtifact::addNewBonus(Bonus *b)
  205. {
  206. b->source = Bonus::ARTIFACT;
  207. b->duration = Bonus::PERMANENT;
  208. b->description = name;
  209. CBonusSystemNode::addNewBonus(b);
  210. }
  211. void CArtifact::setName (std::string desc)
  212. {
  213. name = desc;
  214. }
  215. void CArtifact::setDescription (std::string desc)
  216. {
  217. description = desc;
  218. }
  219. void CArtifact::setEventText (std::string desc)
  220. {
  221. eventText = desc;
  222. }
  223. void CGrowingArtifact::levelUpArtifact (CArtifactInstance * art)
  224. {
  225. Bonus b;
  226. b.type = Bonus::LEVEL_COUNTER;
  227. b.val = 1;
  228. b.duration = Bonus::COMMANDER_KILLED;
  229. art->accumulateBonus (b);
  230. BOOST_FOREACH (auto bonus, bonusesPerLevel)
  231. {
  232. if (art->valOfBonuses(Bonus::LEVEL_COUNTER) % bonus.first == 0) //every n levels
  233. {
  234. art->accumulateBonus (bonus.second);
  235. }
  236. }
  237. BOOST_FOREACH (auto bonus, thresholdBonuses)
  238. {
  239. if (art->valOfBonuses(Bonus::LEVEL_COUNTER) == bonus.first) //every n levels
  240. {
  241. art->addNewBonus (&bonus.second);
  242. }
  243. }
  244. }
  245. CArtHandler::CArtHandler()
  246. {
  247. //VLC->arth = this;
  248. // War machines are the default big artifacts.
  249. for (ArtifactID i = ArtifactID::CATAPULT; i <= ArtifactID::FIRST_AID_TENT; i.advance(1))
  250. bigArtifacts.insert(i);
  251. }
  252. CArtHandler::~CArtHandler()
  253. {
  254. for (std::vector< ConstTransitivePtr<CArtifact> >::iterator it = artifacts.begin(); it != artifacts.end(); ++it)
  255. {
  256. delete (*it)->constituents;
  257. delete (*it)->constituentOf;
  258. }
  259. }
  260. void CArtHandler::loadArtifacts(bool onlyTxt)
  261. {
  262. std::vector<ui16> slots;
  263. slots += 17, 16, 15, 14, 13, 18, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0;
  264. growingArtifacts += ArtifactID::AXE_OF_SMASHING, ArtifactID::MITHRIL_MAIL,
  265. ArtifactID::SWORD_OF_SHARPNESS, ArtifactID::PENDANT_OF_SORCERY, ArtifactID::BOOTS_OF_HASTE,
  266. ArtifactID::BOW_OF_SEEKING, ArtifactID::DRAGON_EYE_RING;
  267. static std::map<char, CArtifact::EartClass> classes =
  268. map_list_of('S',CArtifact::ART_SPECIAL)('T',CArtifact::ART_TREASURE)('N',CArtifact::ART_MINOR)('J',CArtifact::ART_MAJOR)('R',CArtifact::ART_RELIC);
  269. CLegacyConfigParser parser("DATA/ARTRAITS.TXT");
  270. CLegacyConfigParser events("DATA/ARTEVENT.TXT");
  271. parser.endLine(); // header
  272. parser.endLine();
  273. std::map<ui32,ui8>::iterator itr;
  274. for (ArtifactID i=ArtifactID(0); i<GameConstants::ARTIFACTS_QUANTITY; i.advance(1))
  275. {
  276. CArtifact *art;
  277. if (vstd::contains (growingArtifacts, i))
  278. {
  279. art = new CGrowingArtifact();
  280. }
  281. else
  282. {
  283. art = new CArtifact();
  284. }
  285. CArtifact &nart = *art;
  286. nart.id=i;
  287. nart.iconIndex=i;
  288. nart.setName (parser.readString());
  289. nart.setEventText (events.readString());
  290. events.endLine();
  291. nart.price= parser.readNumber();
  292. for(int j=0;j<slots.size();j++)
  293. {
  294. if(parser.readString() == "x")
  295. nart.possibleSlots[ArtBearer::HERO].push_back(ArtifactPosition(slots[j]));
  296. }
  297. nart.aClass = classes[parser.readString()[0]];
  298. //load description and remove quotation marks
  299. nart.setDescription (parser.readString());
  300. parser.endLine();
  301. if(onlyTxt)
  302. continue;
  303. artifacts.push_back(&nart);
  304. }
  305. if (VLC->modh->modules.COMMANDERS)
  306. { //TODO: move all artifacts config to separate json file
  307. const JsonNode config(ResourceID("config/commanders.json"));
  308. BOOST_FOREACH(const JsonNode &artifact, config["artifacts"].Vector())
  309. {
  310. auto ga = dynamic_cast <CGrowingArtifact *>(artifacts[artifact["id"].Float()].get());
  311. BOOST_FOREACH (auto b, artifact["bonusesPerLevel"].Vector())
  312. {
  313. ga->bonusesPerLevel.push_back (std::pair <ui16, Bonus> (b["level"].Float(), *JsonUtils::parseBonus (b["bonus"].Vector())));
  314. }
  315. BOOST_FOREACH (auto b, artifact["thresholdBonuses"].Vector())
  316. {
  317. ga->thresholdBonuses.push_back (std::pair <ui16, Bonus> (b["level"].Float(), *JsonUtils::parseBonus (b["bonus"].Vector())));
  318. }
  319. }
  320. }
  321. sortArts();
  322. if(onlyTxt)
  323. return;
  324. addBonuses();
  325. }
  326. void CArtHandler::reverseMapArtifactConstituents() // Populate reverse mappings of combinational artifacts.
  327. {
  328. BOOST_FOREACH(CArtifact *artifact, artifacts)
  329. {
  330. if (artifact->constituents != NULL)
  331. {
  332. BOOST_FOREACH(ui32 constituentID, *artifact->constituents)
  333. {
  334. if (artifacts[constituentID]->constituentOf == NULL)
  335. artifacts[constituentID]->constituentOf = new std::vector<ArtifactID>();
  336. artifacts[constituentID]->constituentOf->push_back(artifact->id);
  337. }
  338. }
  339. }
  340. }
  341. void CArtHandler::load(const JsonNode & node)
  342. {
  343. BOOST_FOREACH(auto & entry, node.Struct())
  344. {
  345. if (!entry.second.isNull()) // may happens if mod removed creature by setting json entry to null
  346. {
  347. CArtifact * art = loadArtifact(entry.second);
  348. art->id = ArtifactID(artifacts.size());
  349. artifacts.push_back(art);
  350. tlog5 << "Added artifact: " << entry.first << "\n";
  351. VLC->modh->identifiers.registerObject (std::string("artifact.") + entry.first, art->id);
  352. }
  353. }
  354. }
  355. CArtifact * CArtHandler::loadArtifact(const JsonNode & node)
  356. {
  357. CArtifact * art = new CArtifact;
  358. const JsonNode *value;
  359. const JsonNode & text = node["text"];
  360. art->setName (text["name"].String());
  361. art->setDescription (text["description"].String());
  362. art->setEventText (text["event"].String());
  363. const JsonNode & graphics = node["graphics"];
  364. art->iconIndex = graphics["iconIndex"].Float();
  365. art->image = graphics["image"].String();
  366. value = &graphics["large"];
  367. if (!value->isNull())
  368. art->large = value->String();
  369. art->advMapDef = graphics["map"].String();
  370. art->price = node["value"].Float();
  371. {
  372. auto it = artifactClassMap.find (node["class"].String());
  373. if (it != artifactClassMap.end())
  374. {
  375. art->aClass = it->second;
  376. }
  377. else
  378. {
  379. tlog2 << "Warning! Artifact rarity " << value->String() << " not recognized!";
  380. art->aClass = CArtifact::ART_SPECIAL;
  381. }
  382. }
  383. int bearerType = -1;
  384. bool heroArt = false;
  385. {
  386. const JsonNode & bearer = node["type"];
  387. BOOST_FOREACH (const JsonNode & b, bearer.Vector())
  388. {
  389. auto it = artifactBearerMap.find (b.String());
  390. if (it != artifactBearerMap.end())
  391. {
  392. bearerType = it->second;
  393. switch (bearerType)
  394. {
  395. case ArtBearer::HERO: //TODO: allow arts having several possible bearers
  396. heroArt = true;
  397. break;
  398. case ArtBearer::COMMANDER:
  399. makeItCommanderArt (art, false); //do not erase already existing slots
  400. break;
  401. case ArtBearer::CREATURE:
  402. makeItCreatureArt (art, false);
  403. break;
  404. }
  405. }
  406. else
  407. tlog2 << "Warning! Artifact type " << b.String() << " not recognized!";
  408. }
  409. }
  410. value = &node["slot"];
  411. if (!value->isNull() && heroArt) //we assume non-hero slots are irrelevant?
  412. {
  413. std::string slotName = value->String();
  414. if (slotName == "MISC")
  415. {
  416. //unfortunatelly slot ids aare not continuous
  417. art->possibleSlots[ArtBearer::HERO] += ArtifactPosition::MISC1, ArtifactPosition::MISC2, ArtifactPosition::MISC3, ArtifactPosition::MISC4, ArtifactPosition::MISC5;
  418. }
  419. else if (slotName == "RING")
  420. {
  421. art->possibleSlots[ArtBearer::HERO] += ArtifactPosition::LEFT_RING, ArtifactPosition::RIGHT_RING;
  422. }
  423. else
  424. {
  425. auto it = artifactPositionMap.find (slotName);
  426. if (it != artifactPositionMap.end())
  427. {
  428. auto slot = it->second;
  429. art->possibleSlots[ArtBearer::HERO].push_back (slot);
  430. }
  431. else
  432. tlog2 << "Warning! Artifact slot " << value->String() << " not recognized!";
  433. }
  434. }
  435. readComponents (node, art);
  436. BOOST_FOREACH (const JsonNode &bonus, node["bonuses"].Vector())
  437. {
  438. auto b = JsonUtils::parseBonus(bonus);
  439. //TODO: bonus->sid = art->id;
  440. art->addNewBonus(b);
  441. }
  442. return art;
  443. }
  444. void CArtifact::addConstituent (ArtifactID component)
  445. {
  446. assert (constituents);
  447. constituents->push_back (component);
  448. }
  449. ArtifactID CArtHandler::creatureToMachineID(CreatureID id)
  450. {
  451. int dif = 142;
  452. switch (id)
  453. {
  454. case 147:
  455. dif--;
  456. break;
  457. case 148:
  458. dif++;
  459. break;
  460. }
  461. dif = -dif;
  462. return ArtifactID(id+dif);
  463. }
  464. CreatureID CArtHandler::machineIDToCreature(ArtifactID id)
  465. {
  466. int dif = 142;
  467. switch (id)
  468. {
  469. case 6:
  470. dif--;
  471. break;
  472. case 5:
  473. dif++;
  474. break;
  475. }
  476. return CreatureID(id + dif);
  477. }
  478. void CArtHandler::sortArts()
  479. {
  480. //for (int i=0; i<allowedArtifacts.size(); ++i) //do 144, bo nie chcemy bzdurek
  481. //{
  482. // switch (allowedArtifacts[i]->aClass)
  483. // {
  484. // case CArtifact::ART_TREASURE:
  485. // treasures.push_back(allowedArtifacts[i]);
  486. // break;
  487. // case CArtifact::ART_MINOR:
  488. // minors.push_back(allowedArtifacts[i]);
  489. // break;
  490. // case CArtifact::ART_MAJOR:
  491. // majors.push_back(allowedArtifacts[i]);
  492. // break;
  493. // case CArtifact::ART_RELIC:
  494. // relics.push_back(allowedArtifacts[i]);
  495. // break;
  496. // }
  497. //}
  498. }
  499. ArtifactID CArtHandler::getRandomArt(int flags)
  500. {
  501. return getArtSync(ran(), flags, true);
  502. }
  503. ArtifactID CArtHandler::getArtSync (ui32 rand, int flags, bool erasePicked)
  504. {
  505. auto getAllowedArts = [&](std::vector<ConstTransitivePtr<CArtifact> > &out, std::vector<CArtifact*> *arts, CArtifact::EartClass flag)
  506. {
  507. if (arts->empty()) //restock available arts
  508. fillList(*arts, flag);
  509. for (int i = 0; i < arts->size(); ++i)
  510. {
  511. CArtifact *art = (*arts)[i];
  512. out.push_back(art);
  513. }
  514. };
  515. auto getAllowed = [&](std::vector<ConstTransitivePtr<CArtifact> > &out)
  516. {
  517. if (flags & CArtifact::ART_TREASURE)
  518. getAllowedArts (out, &treasures, CArtifact::ART_TREASURE);
  519. if (flags & CArtifact::ART_MINOR)
  520. getAllowedArts (out, &minors, CArtifact::ART_MINOR);
  521. if (flags & CArtifact::ART_MAJOR)
  522. getAllowedArts (out, &majors, CArtifact::ART_MAJOR);
  523. if (flags & CArtifact::ART_RELIC)
  524. getAllowedArts (out, &relics, CArtifact::ART_RELIC);
  525. if (!out.size()) //no artifact of specified rarity, we need to take another one
  526. {
  527. getAllowedArts (out, &treasures, CArtifact::ART_TREASURE);
  528. getAllowedArts (out, &minors, CArtifact::ART_MINOR);
  529. getAllowedArts (out, &majors, CArtifact::ART_MAJOR);
  530. getAllowedArts (out, &relics, CArtifact::ART_RELIC);
  531. }
  532. if (!out.size()) //no arts are available at all
  533. {
  534. out.resize (64);
  535. std::fill_n (out.begin(), 64, artifacts[2]); //Give Grail - this can't be banned (hopefully)
  536. }
  537. //tlog0 << "Treasure count: " << treasures.size() << std::endl;
  538. };
  539. std::vector<ConstTransitivePtr<CArtifact> > out;
  540. getAllowed(out);
  541. ArtifactID artID = out[rand % out.size()]->id;
  542. if(erasePicked)
  543. erasePickedArt (artID);
  544. return artID;
  545. }
  546. Bonus *createBonus(Bonus::BonusType type, int val, int subtype, Bonus::ValueType valType, shared_ptr<ILimiter> limiter = shared_ptr<ILimiter>(), int additionalInfo = 0)
  547. {
  548. Bonus *added = new Bonus(Bonus::PERMANENT,type,Bonus::ARTIFACT,val,-1,subtype);
  549. added->additionalInfo = additionalInfo;
  550. added->valType = valType;
  551. added->limiter = limiter;
  552. return added;
  553. }
  554. Bonus *createBonus(Bonus::BonusType type, int val, int subtype, shared_ptr<IPropagator> propagator = shared_ptr<IPropagator>(), int additionalInfo = 0)
  555. {
  556. Bonus *added = new Bonus(Bonus::PERMANENT,type,Bonus::ARTIFACT,val,-1,subtype);
  557. added->additionalInfo = additionalInfo;
  558. added->valType = Bonus::BASE_NUMBER;
  559. added->propagator = propagator;
  560. return added;
  561. }
  562. void CArtHandler::giveArtBonus( ArtifactID aid, Bonus::BonusType type, int val, int subtype, Bonus::ValueType valType, shared_ptr<ILimiter> limiter, int additionalInfo)
  563. {
  564. giveArtBonus(aid, createBonus(type, val, subtype, valType, limiter, additionalInfo));
  565. }
  566. void CArtHandler::giveArtBonus(ArtifactID aid, Bonus::BonusType type, int val, int subtype, shared_ptr<IPropagator> propagator /*= NULL*/, int additionalInfo)
  567. {
  568. giveArtBonus(aid, createBonus(type, val, subtype, propagator, additionalInfo));
  569. }
  570. void CArtHandler::giveArtBonus(ArtifactID aid, Bonus *bonus)
  571. {
  572. bonus->sid = aid;
  573. if(bonus->subtype == Bonus::MORALE || bonus->type == Bonus::LUCK)
  574. bonus->description = artifacts[aid]->Name() + (bonus->val > 0 ? " +" : " ") + boost::lexical_cast<std::string>(bonus->val);
  575. else
  576. bonus->description = artifacts[aid]->Name();
  577. artifacts[aid]->addNewBonus(bonus);
  578. }
  579. void CArtHandler::makeItCreatureArt (CArtifact * a, bool onlyCreature /*=true*/)
  580. {
  581. if (onlyCreature)
  582. {
  583. a->possibleSlots[ArtBearer::HERO].clear();
  584. a->possibleSlots[ArtBearer::COMMANDER].clear();
  585. }
  586. a->possibleSlots[ArtBearer::CREATURE].push_back(ArtifactPosition::CREATURE_SLOT);
  587. }
  588. void CArtHandler::makeItCreatureArt (ArtifactID aid, bool onlyCreature /*=true*/)
  589. {
  590. CArtifact *a = artifacts[aid];
  591. makeItCreatureArt (a, onlyCreature);
  592. }
  593. void CArtHandler::makeItCommanderArt (CArtifact * a, bool onlyCommander /*= true*/ )
  594. {
  595. if (onlyCommander)
  596. {
  597. a->possibleSlots[ArtBearer::HERO].clear();
  598. a->possibleSlots[ArtBearer::CREATURE].clear();
  599. }
  600. for (int i = ArtifactPosition::COMMANDER1; i <= ArtifactPosition::COMMANDER6; ++i)
  601. a->possibleSlots[ArtBearer::COMMANDER].push_back(ArtifactPosition(i));
  602. }
  603. void CArtHandler::makeItCommanderArt( ArtifactID aid, bool onlyCommander /*= true*/ )
  604. {
  605. CArtifact *a = artifacts[aid];
  606. makeItCommanderArt (a, onlyCommander);
  607. }
  608. void CArtHandler::addBonuses()
  609. {
  610. const JsonNode config(ResourceID("config/artifacts.json"));
  611. BOOST_FOREACH (auto & artifact, config["artifacts"].Struct()) //pair <string, JsonNode> (id, properties)
  612. {
  613. auto ga = artifacts[artifact.second["id"].Float()].get();
  614. BOOST_FOREACH (auto b, artifact.second["bonuses"].Vector())
  615. {
  616. auto bonus = JsonUtils::parseBonus (b);
  617. bonus->sid = ga->id;
  618. ga->addNewBonus (bonus);
  619. }
  620. BOOST_FOREACH (const JsonNode & b, artifact.second["type"].Vector()) //TODO: remove duplicate code
  621. {
  622. auto it = artifactBearerMap.find (b.String());
  623. if (it != artifactBearerMap.end())
  624. {
  625. int bearerType = it->second;
  626. switch (bearerType)
  627. {
  628. case ArtBearer::HERO:
  629. break;
  630. case ArtBearer::COMMANDER:
  631. makeItCommanderArt (ga); //original artifacts should have only one bearer type
  632. break;
  633. case ArtBearer::CREATURE:
  634. makeItCreatureArt (ga);
  635. break;
  636. }
  637. }
  638. else
  639. tlog2 << "Warning! Artifact type " << b.String() << " not recognized!";
  640. }
  641. readComponents (artifact.second, ga);
  642. VLC->modh->identifiers.registerObject ("artifact." + artifact.first, ga->id);
  643. }
  644. }
  645. void CArtHandler::readComponents (const JsonNode & node, CArtifact * art)
  646. {
  647. const JsonNode *value;
  648. value = &node["components"];
  649. if (!value->isNull())
  650. {
  651. art->constituents = new std::vector<ArtifactID>();
  652. BOOST_FOREACH (auto component, value->Vector())
  653. {
  654. VLC->modh->identifiers.requestIdentifier(std::string("artifact.") + component.String(),
  655. [art](si32 id)
  656. {
  657. art->addConstituent(ArtifactID(id));
  658. }
  659. );
  660. }
  661. }
  662. }
  663. void CArtHandler::clear()
  664. {
  665. BOOST_FOREACH(CArtifact *art, artifacts)
  666. delete art;
  667. artifacts.clear();
  668. clearHlpLists();
  669. }
  670. void CArtHandler::clearHlpLists()
  671. {
  672. treasures.clear();
  673. minors.clear();
  674. majors.clear();
  675. relics.clear();
  676. }
  677. bool CArtHandler::legalArtifact(ArtifactID id)
  678. {
  679. auto art = artifacts[id];
  680. //assert ( (!art->constituents) || art->constituents->size() ); //artifacts is not combined or has some components
  681. return (art->possibleSlots[ArtBearer::HERO].size() ||
  682. (art->possibleSlots[ArtBearer::COMMANDER].size() && VLC->modh->modules.COMMANDERS) ||
  683. (art->possibleSlots[ArtBearer::CREATURE].size() && VLC->modh->modules.STACK_ARTIFACT)) &&
  684. !(art->constituents); //no combo artifacts spawning
  685. }
  686. void CArtHandler::initAllowedArtifactsList(const std::vector<bool> &allowed)
  687. {
  688. allowedArtifacts.clear();
  689. clearHlpLists();
  690. for (ArtifactID i=ArtifactID::SPELLBOOK; i<ArtifactID::ART_SELECTION; i.advance(1))
  691. {
  692. if (allowed[i] && legalArtifact(i))
  693. allowedArtifacts.push_back(artifacts[i]);
  694. }
  695. if (VLC->modh->modules.COMMANDERS) //allow all commander artifacts for testing
  696. {
  697. for (int i = 146; i <= 155; ++i)
  698. {
  699. allowedArtifacts.push_back(artifacts[i]);
  700. }
  701. }
  702. for (int i = GameConstants::ARTIFACTS_QUANTITY; i < artifacts.size(); ++i) //allow all new artifacts by default
  703. {
  704. if (legalArtifact(ArtifactID(i)))
  705. allowedArtifacts.push_back(artifacts[i]);
  706. //keep im mind that artifact can be worn by more than one type of bearer
  707. }
  708. }
  709. std::vector<bool> CArtHandler::getDefaultAllowedArtifacts() const
  710. {
  711. std::vector<bool> allowedArtifacts;
  712. allowedArtifacts.resize(127, true);
  713. allowedArtifacts.resize(141, false);
  714. allowedArtifacts.resize(GameConstants::ARTIFACTS_QUANTITY, true);
  715. return allowedArtifacts;
  716. }
  717. void CArtHandler::erasePickedArt(ArtifactID id)
  718. {
  719. CArtifact *art = artifacts[id];
  720. if(auto artifactList = listFromClass(art->aClass))
  721. {
  722. if(artifactList->empty())
  723. fillList(*artifactList, art->aClass);
  724. auto itr = vstd::find(*artifactList, art);
  725. if(itr != artifactList->end())
  726. {
  727. artifactList->erase(itr);
  728. }
  729. else
  730. tlog2 << "Problem: cannot erase artifact " << art->Name() << " from list, it was not present\n";
  731. }
  732. else
  733. tlog2 << "Problem: cannot find list for artifact " << art->Name() << ", strange class. (special?)\n";
  734. }
  735. boost::optional<std::vector<CArtifact*>&> CArtHandler::listFromClass( CArtifact::EartClass artifactClass )
  736. {
  737. switch(artifactClass)
  738. {
  739. case CArtifact::ART_TREASURE:
  740. return treasures;
  741. case CArtifact::ART_MINOR:
  742. return minors;
  743. case CArtifact::ART_MAJOR:
  744. return majors;
  745. case CArtifact::ART_RELIC:
  746. return relics;
  747. default: //special artifacts should not be erased
  748. return 0;
  749. }
  750. }
  751. void CArtHandler::fillList( std::vector<CArtifact*> &listToBeFilled, CArtifact::EartClass artifactClass )
  752. {
  753. assert(listToBeFilled.empty());
  754. for (int i = 0; i < allowedArtifacts.size(); ++i)
  755. {
  756. if (allowedArtifacts[i]->aClass == artifactClass)
  757. listToBeFilled.push_back(allowedArtifacts[i]);
  758. }
  759. }
  760. CArtifactInstance::CArtifactInstance()
  761. {
  762. init();
  763. }
  764. CArtifactInstance::CArtifactInstance( CArtifact *Art)
  765. {
  766. init();
  767. setType(Art);
  768. }
  769. // CArtifactInstance::CArtifactInstance(int aid)
  770. // {
  771. // init();
  772. // setType(VLC->arth->artifacts[aid]);
  773. // }
  774. void CArtifactInstance::setType( CArtifact *Art )
  775. {
  776. artType = Art;
  777. attachTo(Art);
  778. }
  779. std::string CArtifactInstance::nodeName() const
  780. {
  781. return "Artifact instance of " + (artType ? artType->Name() : std::string("uninitialized")) + " type";
  782. }
  783. CArtifactInstance * CArtifactInstance::createScroll( const CSpell *s)
  784. {
  785. CArtifactInstance *ret = new CArtifactInstance(VLC->arth->artifacts[1]);
  786. Bonus *b = new Bonus(Bonus::PERMANENT, Bonus::SPELL, Bonus::ARTIFACT_INSTANCE, -1, 1, s->id);
  787. ret->addNewBonus(b);
  788. return ret;
  789. }
  790. void CArtifactInstance::init()
  791. {
  792. id = ArtifactInstanceID();
  793. setNodeType(ARTIFACT_INSTANCE);
  794. }
  795. ArtifactPosition CArtifactInstance::firstAvailableSlot(const CArtifactSet *h) const
  796. {
  797. BOOST_FOREACH(auto slot, artType->possibleSlots[h->bearerType()])
  798. {
  799. if(canBePutAt(h, slot)) //if(artType->fitsAt(h->artifWorn, slot))
  800. {
  801. //we've found a free suitable slot.
  802. return slot;
  803. }
  804. }
  805. //if haven't find proper slot, use backpack
  806. return firstBackpackSlot(h);
  807. }
  808. ArtifactPosition CArtifactInstance::firstBackpackSlot(const CArtifactSet *h) const
  809. {
  810. if(!artType->isBig()) //discard big artifact
  811. return ArtifactPosition(
  812. GameConstants::BACKPACK_START + h->artifactsInBackpack.size());
  813. return ArtifactPosition::PRE_FIRST;
  814. }
  815. bool CArtifactInstance::canBePutAt(const ArtifactLocation al, bool assumeDestRemoved /*= false*/) const
  816. {
  817. return canBePutAt(al.getHolderArtSet(), al.slot, assumeDestRemoved);
  818. }
  819. bool CArtifactInstance::canBePutAt(const CArtifactSet *artSet, ArtifactPosition slot, bool assumeDestRemoved /*= false*/) const
  820. {
  821. if(slot >= GameConstants::BACKPACK_START)
  822. {
  823. if(artType->isBig())
  824. return false;
  825. //TODO backpack limit
  826. return true;
  827. }
  828. auto possibleSlots = artType->possibleSlots.find(artSet->bearerType());
  829. if(possibleSlots == artType->possibleSlots.end())
  830. {
  831. tlog3 << "Warning: artifact " << artType->Name() << " doesn't have defined allowed slots for bearer of type "
  832. << artSet->bearerType() << std::endl;
  833. return false;
  834. }
  835. if(!vstd::contains(possibleSlots->second, slot))
  836. return false;
  837. return artSet->isPositionFree(slot, assumeDestRemoved);
  838. }
  839. void CArtifactInstance::putAt(ArtifactLocation al)
  840. {
  841. assert(canBePutAt(al));
  842. al.getHolderArtSet()->setNewArtSlot(al.slot, this, false);
  843. if(al.slot < GameConstants::BACKPACK_START)
  844. al.getHolderNode()->attachTo(this);
  845. }
  846. void CArtifactInstance::removeFrom(ArtifactLocation al)
  847. {
  848. assert(al.getHolderArtSet()->getArt(al.slot) == this);
  849. al.getHolderArtSet()->eraseArtSlot(al.slot);
  850. if(al.slot < GameConstants::BACKPACK_START)
  851. al.getHolderNode()->detachFrom(this);
  852. //TODO delete me?
  853. }
  854. bool CArtifactInstance::canBeDisassembled() const
  855. {
  856. return artType->constituents && artType->constituentOf->size();
  857. }
  858. std::vector<const CArtifact *> CArtifactInstance::assemblyPossibilities(const CArtifactSet *h) const
  859. {
  860. std::vector<const CArtifact *> ret;
  861. if(!artType->constituentOf //not a part of combined artifact
  862. || artType->constituents) //combined artifact already: no combining of combined artifacts... for now.
  863. return ret;
  864. BOOST_FOREACH(ui32 possibleCombinedArt, *artType->constituentOf)
  865. {
  866. const CArtifact * const artifact = VLC->arth->artifacts[possibleCombinedArt];
  867. assert(artifact->constituents);
  868. bool possible = true;
  869. BOOST_FOREACH(ui32 constituentID, *artifact->constituents) //check if all constituents are available
  870. {
  871. if(!h->hasArt(constituentID, true)) //constituent must be equipped
  872. {
  873. possible = false;
  874. break;
  875. }
  876. }
  877. if(possible)
  878. ret.push_back(artifact);
  879. }
  880. return ret;
  881. }
  882. void CArtifactInstance::move(ArtifactLocation src, ArtifactLocation dst)
  883. {
  884. removeFrom(src);
  885. putAt(dst);
  886. }
  887. CArtifactInstance * CArtifactInstance::createNewArtifactInstance(CArtifact *Art)
  888. {
  889. if(!Art->constituents)
  890. {
  891. auto ret = new CArtifactInstance(Art);
  892. if (dynamic_cast<CGrowingArtifact *>(Art))
  893. {
  894. Bonus * bonus = new Bonus;
  895. bonus->type = Bonus::LEVEL_COUNTER;
  896. bonus->val = 0;
  897. ret->addNewBonus (bonus);
  898. }
  899. return ret;
  900. }
  901. else
  902. {
  903. CCombinedArtifactInstance * ret = new CCombinedArtifactInstance(Art);
  904. ret->createConstituents();
  905. return ret;
  906. }
  907. }
  908. CArtifactInstance * CArtifactInstance::createNewArtifactInstance(int aid)
  909. {
  910. return createNewArtifactInstance(VLC->arth->artifacts[aid]);
  911. }
  912. void CArtifactInstance::deserializationFix()
  913. {
  914. setType(artType);
  915. }
  916. SpellID CArtifactInstance::getGivenSpellID() const
  917. {
  918. const Bonus * b = getBonusLocalFirst(Selector::type(Bonus::SPELL));
  919. if(!b)
  920. {
  921. tlog3 << "Warning: " << nodeName() << " doesn't bear any spell!\n";
  922. return SpellID::NONE;
  923. }
  924. return SpellID(b->subtype);
  925. }
  926. bool CArtifactInstance::isPart(const CArtifactInstance *supposedPart) const
  927. {
  928. return supposedPart == this;
  929. }
  930. bool CCombinedArtifactInstance::canBePutAt(const CArtifactSet *artSet, ArtifactPosition slot, bool assumeDestRemoved /*= false*/) const
  931. {
  932. bool canMainArtifactBePlaced = CArtifactInstance::canBePutAt(artSet, slot, assumeDestRemoved);
  933. if(!canMainArtifactBePlaced)
  934. return false; //no is no...
  935. if(slot >= GameConstants::BACKPACK_START)
  936. return true; //we can always remove combined art to the backapck
  937. assert(artType->constituents);
  938. std::vector<ConstituentInfo> constituentsToBePlaced = constituentsInfo; //we'll remove constituents from that list, as we find a suitable slot for them
  939. //it may be that we picked a combined artifact in hero screen (though technically it's still there) to move it
  940. //so we remove from the list all constituents that are already present on dst hero in the form of locks
  941. BOOST_FOREACH(const ConstituentInfo &constituent, constituentsInfo)
  942. {
  943. if(constituent.art == artSet->getArt(constituent.slot, false)) //no need to worry about locked constituent
  944. constituentsToBePlaced -= constituent;
  945. }
  946. //we iterate over all active slots and check if constituents fits them
  947. for (int i = 0; i < GameConstants::BACKPACK_START; i++)
  948. {
  949. for(auto art = constituentsToBePlaced.begin(); art != constituentsToBePlaced.end(); art++)
  950. {
  951. 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
  952. {
  953. constituentsToBePlaced.erase(art);
  954. break;
  955. }
  956. }
  957. }
  958. return constituentsToBePlaced.empty();
  959. }
  960. bool CCombinedArtifactInstance::canBeDisassembled() const
  961. {
  962. return true;
  963. }
  964. CCombinedArtifactInstance::CCombinedArtifactInstance(CArtifact *Art)
  965. : CArtifactInstance(Art) //TODO: seems unued, but need to be written
  966. {
  967. }
  968. CCombinedArtifactInstance::CCombinedArtifactInstance()
  969. {
  970. }
  971. void CCombinedArtifactInstance::createConstituents()
  972. {
  973. assert(artType);
  974. assert(artType->constituents);
  975. BOOST_FOREACH(ui32 a, *artType->constituents)
  976. {
  977. addAsConstituent(CArtifactInstance::createNewArtifactInstance(a), ArtifactPosition::PRE_FIRST);
  978. }
  979. }
  980. void CCombinedArtifactInstance::addAsConstituent(CArtifactInstance *art, ArtifactPosition slot)
  981. {
  982. assert(vstd::contains(*artType->constituents, art->artType->id));
  983. assert(art->getParentNodes().size() == 1 && art->getParentNodes().front() == art->artType);
  984. constituentsInfo.push_back(ConstituentInfo(art, slot));
  985. attachTo(art);
  986. }
  987. void CCombinedArtifactInstance::putAt(ArtifactLocation al)
  988. {
  989. if(al.slot >= GameConstants::BACKPACK_START)
  990. {
  991. CArtifactInstance::putAt(al);
  992. BOOST_FOREACH(ConstituentInfo &ci, constituentsInfo)
  993. ci.slot = ArtifactPosition::PRE_FIRST;
  994. }
  995. else
  996. {
  997. CArtifactInstance *mainConstituent = figureMainConstituent(al); //it'll be replaced with combined artifact, not a lock
  998. CArtifactInstance::putAt(al); //puts combined art (this)
  999. BOOST_FOREACH(ConstituentInfo &ci, constituentsInfo)
  1000. {
  1001. if(ci.art != mainConstituent)
  1002. {
  1003. const ArtifactLocation suggestedPos(al.artHolder, ci.slot);
  1004. const bool inActiveSlot = vstd::isbetween(ci.slot, 0, GameConstants::BACKPACK_START);
  1005. const bool suggestedPosValid = ci.art->canBePutAt(suggestedPos);
  1006. ArtifactPosition pos = ArtifactPosition::PRE_FIRST;
  1007. if(inActiveSlot && suggestedPosValid) //there is a valid suggestion where to place lock
  1008. pos = ci.slot;
  1009. else
  1010. ci.slot = pos = ci.art->firstAvailableSlot(al.getHolderArtSet());
  1011. assert(pos < GameConstants::BACKPACK_START);
  1012. al.getHolderArtSet()->setNewArtSlot(pos, ci.art, true); //sets as lock
  1013. }
  1014. else
  1015. {
  1016. ci.slot = ArtifactPosition::PRE_FIRST;
  1017. }
  1018. }
  1019. }
  1020. }
  1021. void CCombinedArtifactInstance::removeFrom(ArtifactLocation al)
  1022. {
  1023. if(al.slot >= GameConstants::BACKPACK_START)
  1024. {
  1025. CArtifactInstance::removeFrom(al);
  1026. }
  1027. else
  1028. {
  1029. BOOST_FOREACH(ConstituentInfo &ci, constituentsInfo)
  1030. {
  1031. if(ci.slot >= 0)
  1032. {
  1033. al.getHolderArtSet()->eraseArtSlot(ci.slot);
  1034. ci.slot = ArtifactPosition::PRE_FIRST;
  1035. }
  1036. else
  1037. {
  1038. //main constituent
  1039. CArtifactInstance::removeFrom(al);
  1040. }
  1041. }
  1042. }
  1043. }
  1044. CArtifactInstance * CCombinedArtifactInstance::figureMainConstituent(const ArtifactLocation al)
  1045. {
  1046. CArtifactInstance *mainConstituent = NULL; //it'll be replaced with combined artifact, not a lock
  1047. BOOST_FOREACH(ConstituentInfo &ci, constituentsInfo)
  1048. if(ci.slot == al.slot)
  1049. mainConstituent = ci.art;
  1050. if(!mainConstituent)
  1051. {
  1052. BOOST_FOREACH(ConstituentInfo &ci, constituentsInfo)
  1053. {
  1054. if(vstd::contains(ci.art->artType->possibleSlots[al.getHolderArtSet()->bearerType()], al.slot))
  1055. {
  1056. mainConstituent = ci.art;
  1057. }
  1058. }
  1059. }
  1060. return mainConstituent;
  1061. }
  1062. void CCombinedArtifactInstance::deserializationFix()
  1063. {
  1064. BOOST_FOREACH(ConstituentInfo &ci, constituentsInfo)
  1065. attachTo(ci.art);
  1066. }
  1067. bool CCombinedArtifactInstance::isPart(const CArtifactInstance *supposedPart) const
  1068. {
  1069. bool me = CArtifactInstance::isPart(supposedPart);
  1070. if(me)
  1071. return true;
  1072. //check for constituents
  1073. BOOST_FOREACH(const ConstituentInfo &constituent, constituentsInfo)
  1074. if(constituent.art == supposedPart)
  1075. return true;
  1076. return false;
  1077. }
  1078. CCombinedArtifactInstance::ConstituentInfo::ConstituentInfo(CArtifactInstance *Art /*= NULL*/, ArtifactPosition Slot /*= -1*/)
  1079. {
  1080. art = Art;
  1081. slot = Slot;
  1082. }
  1083. bool CCombinedArtifactInstance::ConstituentInfo::operator==(const ConstituentInfo &rhs) const
  1084. {
  1085. return art == rhs.art && slot == rhs.slot;
  1086. }
  1087. const CArtifactInstance* CArtifactSet::getArt(ArtifactPosition pos, bool excludeLocked /*= true*/) const
  1088. {
  1089. if(const ArtSlotInfo *si = getSlot(pos))
  1090. {
  1091. if(si->artifact && (!excludeLocked || !si->locked))
  1092. return si->artifact;
  1093. }
  1094. return NULL;
  1095. }
  1096. CArtifactInstance* CArtifactSet::getArt(ArtifactPosition pos, bool excludeLocked /*= true*/)
  1097. {
  1098. return const_cast<CArtifactInstance*>((const_cast<const CArtifactSet*>(this))->getArt(pos, excludeLocked));
  1099. }
  1100. ArtifactPosition CArtifactSet::getArtPos(int aid, bool onlyWorn /*= true*/) const
  1101. {
  1102. for(auto i = artifactsWorn.cbegin(); i != artifactsWorn.cend(); i++)
  1103. if(i->second.artifact->artType->id == aid)
  1104. return i->first;
  1105. if(onlyWorn)
  1106. return ArtifactPosition::PRE_FIRST;
  1107. for(int i = 0; i < artifactsInBackpack.size(); i++)
  1108. if(artifactsInBackpack[i].artifact->artType->id == aid)
  1109. return ArtifactPosition(GameConstants::BACKPACK_START + i);
  1110. return ArtifactPosition::PRE_FIRST;
  1111. }
  1112. ArtifactPosition CArtifactSet::getArtPos(const CArtifactInstance *art) const
  1113. {
  1114. BOOST_FOREACH(auto i, artifactsWorn)
  1115. if(i.second.artifact == art)
  1116. return i.first;
  1117. for(int i = 0; i < artifactsInBackpack.size(); i++)
  1118. if(artifactsInBackpack[i].artifact == art)
  1119. return ArtifactPosition(GameConstants::BACKPACK_START + i);
  1120. return ArtifactPosition::PRE_FIRST;
  1121. }
  1122. const CArtifactInstance * CArtifactSet::getArtByInstanceId( ArtifactInstanceID artInstId ) const
  1123. {
  1124. BOOST_FOREACH(auto i, artifactsWorn)
  1125. if(i.second.artifact->id == artInstId)
  1126. return i.second.artifact;
  1127. BOOST_FOREACH(auto i, artifactsInBackpack)
  1128. if(i.artifact->id == artInstId)
  1129. return i.artifact;
  1130. return NULL;
  1131. }
  1132. bool CArtifactSet::hasArt(ui32 aid, bool onlyWorn /*= false*/) const
  1133. {
  1134. return getArtPos(aid, onlyWorn) != ArtifactPosition::PRE_FIRST;
  1135. }
  1136. const ArtSlotInfo * CArtifactSet::getSlot(ArtifactPosition pos) const
  1137. {
  1138. if(vstd::contains(artifactsWorn, pos))
  1139. return &artifactsWorn[pos];
  1140. if(pos >= ArtifactPosition::AFTER_LAST )
  1141. {
  1142. int backpackPos = (int)pos - GameConstants::BACKPACK_START;
  1143. if(backpackPos < 0 || backpackPos >= artifactsInBackpack.size())
  1144. return NULL;
  1145. else
  1146. return &artifactsInBackpack[backpackPos];
  1147. }
  1148. return NULL;
  1149. }
  1150. bool CArtifactSet::isPositionFree(ArtifactPosition pos, bool onlyLockCheck /*= false*/) const
  1151. {
  1152. if(const ArtSlotInfo *s = getSlot(pos))
  1153. return (onlyLockCheck || !s->artifact) && !s->locked;
  1154. return true; //no slot means not used
  1155. }
  1156. si32 CArtifactSet::getArtTypeId(ArtifactPosition pos) const
  1157. {
  1158. const CArtifactInstance * const a = getArt(pos);
  1159. if(!a)
  1160. {
  1161. tlog2 << (dynamic_cast<const CGHeroInstance*>(this))->name << " has no artifact at " << pos << " (getArtTypeId)\n";
  1162. return -1;
  1163. }
  1164. return a->artType->id;
  1165. }
  1166. CArtifactSet::~CArtifactSet()
  1167. {
  1168. }
  1169. ArtSlotInfo & CArtifactSet::retreiveNewArtSlot(ArtifactPosition slot)
  1170. {
  1171. assert(!vstd::contains(artifactsWorn, slot));
  1172. ArtSlotInfo &ret = slot < GameConstants::BACKPACK_START
  1173. ? artifactsWorn[slot]
  1174. : *artifactsInBackpack.insert(artifactsInBackpack.begin() + (slot - GameConstants::BACKPACK_START), ArtSlotInfo());
  1175. return ret;
  1176. }
  1177. void CArtifactSet::setNewArtSlot(ArtifactPosition slot, CArtifactInstance *art, bool locked)
  1178. {
  1179. ArtSlotInfo &asi = retreiveNewArtSlot(slot);
  1180. asi.artifact = art;
  1181. asi.locked = locked;
  1182. }
  1183. void CArtifactSet::eraseArtSlot(ArtifactPosition slot)
  1184. {
  1185. if(slot < GameConstants::BACKPACK_START)
  1186. {
  1187. artifactsWorn.erase(slot);
  1188. }
  1189. else
  1190. {
  1191. slot = ArtifactPosition(slot - GameConstants::BACKPACK_START);
  1192. artifactsInBackpack.erase(artifactsInBackpack.begin() + slot);
  1193. }
  1194. }
  1195. void CArtifactSet::artDeserializationFix(CBonusSystemNode *node)
  1196. {
  1197. for(auto i = artifactsWorn.begin(); i != artifactsWorn.end(); i++)
  1198. if(i->second.artifact && !i->second.locked)
  1199. node->attachTo(i->second.artifact);
  1200. }