CArtHandler.cpp 36 KB

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