CArtHandler.cpp 35 KB

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