CArtHandler.cpp 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310
  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. artifacts.push_back(&nart);
  301. }
  302. if (VLC->modh->modules.COMMANDERS)
  303. { //TODO: move all artifacts config to separate json file
  304. const JsonNode config(ResourceID("config/commanders.json"));
  305. BOOST_FOREACH(const JsonNode &artifact, config["artifacts"].Vector())
  306. {
  307. auto ga = dynamic_cast <CGrowingArtifact *>(artifacts[artifact["id"].Float()].get());
  308. BOOST_FOREACH (auto b, artifact["bonusesPerLevel"].Vector())
  309. {
  310. ga->bonusesPerLevel.push_back (std::pair <ui16, Bonus> (b["level"].Float(), *JsonUtils::parseBonus (b["bonus"].Vector())));
  311. }
  312. BOOST_FOREACH (auto b, artifact["thresholdBonuses"].Vector())
  313. {
  314. ga->thresholdBonuses.push_back (std::pair <ui16, Bonus> (b["level"].Float(), *JsonUtils::parseBonus (b["bonus"].Vector())));
  315. }
  316. }
  317. }
  318. sortArts();
  319. if(onlyTxt)
  320. return;
  321. addBonuses();
  322. // Populate reverse mappings of combinational artifacts.
  323. //TODO: do that also for new artifacts read from mods
  324. BOOST_FOREACH(CArtifact *artifact, artifacts)
  325. {
  326. if (artifact->constituents != NULL)
  327. {
  328. BOOST_FOREACH(ui32 constituentID, *artifact->constituents)
  329. {
  330. if (artifacts[constituentID]->constituentOf == NULL)
  331. artifacts[constituentID]->constituentOf = new std::vector<ui32>();
  332. artifacts[constituentID]->constituentOf->push_back(artifact->id);
  333. }
  334. }
  335. }
  336. }
  337. void CArtHandler::load(const JsonNode & node)
  338. {
  339. BOOST_FOREACH(auto & entry, node.Struct())
  340. {
  341. if (!entry.second.isNull()) // may happens if mod removed creature by setting json entry to null
  342. {
  343. CArtifact * art = loadArtifact(entry.second);
  344. art->setName (entry.first);
  345. art->id = artifacts.size();
  346. artifacts.push_back(art);
  347. tlog3 << "Added artifact: " << entry.first << "\n";
  348. VLC->modh->identifiers.registerObject (std::string("artifact.") + art->Name(), art->id);
  349. }
  350. }
  351. }
  352. CArtifact * CArtHandler::loadArtifact(const JsonNode & node)
  353. {
  354. CArtifact * art = new CArtifact;
  355. const JsonNode *value;
  356. const JsonNode & text = node["text"];
  357. art->setName (text["name"].String());
  358. art->setDescription (text["description"].String());
  359. art->setEventText (text["event"].String());
  360. const JsonNode & graphics = node["graphics"];
  361. art->iconIndex = graphics["iconIndex"].Float();
  362. art->image = graphics["image"].String();
  363. value = &graphics["large"];
  364. if (!value->isNull())
  365. art->large = value->String();
  366. art->advMapDef = graphics["map"].String();
  367. art->price = node["value"].Float();
  368. {
  369. auto it = artifactClassMap.find (node["class"].String());
  370. if (it != artifactClassMap.end())
  371. {
  372. art->aClass = it->second;
  373. }
  374. else
  375. {
  376. tlog2 << "Warning! Artifact rarity " << value->String() << " not recognized!";
  377. art->aClass = CArtifact::ART_SPECIAL;
  378. }
  379. }
  380. int bearerType = -1;
  381. {
  382. auto it = artifactBearerMap.find (node["type"].String());
  383. if (it != artifactBearerMap.end())
  384. {
  385. bearerType = it->second;
  386. switch (bearerType)
  387. {
  388. case ArtBearer::HERO: //TODO: allow arts having several possible bearers
  389. break;
  390. case ArtBearer::COMMANDER:
  391. makeItCommanderArt (art, false); //do not erase already existing slots
  392. break;
  393. case ArtBearer::CREATURE:
  394. makeItCreatureArt (art, false);
  395. break;
  396. }
  397. }
  398. else
  399. tlog2 << "Warning! Artifact type " << value->String() << " not recognized!";
  400. }
  401. value = &node["slot"];
  402. if (!value->isNull() && bearerType == ArtBearer::HERO) //we assume non-hero slots are irrelevant?
  403. {
  404. std::string slotName = value->String();
  405. if (slotName == "MISC")
  406. {
  407. //unfortunatelly slot ids aare not continuous
  408. art->possibleSlots[ArtBearer::HERO] += ArtifactPosition::MISC1, ArtifactPosition::MISC2, ArtifactPosition::MISC3, ArtifactPosition::MISC4, ArtifactPosition::MISC5;
  409. }
  410. else if (slotName == "RING")
  411. {
  412. art->possibleSlots[ArtBearer::HERO] += ArtifactPosition::LEFT_RING, ArtifactPosition::RIGHT_RING;
  413. }
  414. else
  415. {
  416. auto it = artifactPositionMap.find (slotName);
  417. if (it != artifactPositionMap.end())
  418. {
  419. auto slot = it->second;
  420. art->possibleSlots[ArtBearer::HERO].push_back (slot);
  421. }
  422. else
  423. tlog2 << "Warning! Artifact slot " << value->String() << " not recognized!";
  424. }
  425. }
  426. readComponents (node, art);
  427. BOOST_FOREACH (const JsonNode &bonus, node["bonuses"].Vector())
  428. {
  429. auto b = JsonUtils::parseBonus(bonus);
  430. //TODO: bonus->sid = art->id;
  431. art->addNewBonus(b);
  432. }
  433. return art;
  434. }
  435. void CArtifact::addConstituent (ui32 component)
  436. {
  437. assert (constituents);
  438. constituents->push_back (component);
  439. }
  440. int CArtHandler::convertMachineID(int id, bool creToArt )
  441. {
  442. int dif = 142;
  443. if(creToArt)
  444. {
  445. switch (id)
  446. {
  447. case 147:
  448. dif--;
  449. break;
  450. case 148:
  451. dif++;
  452. break;
  453. }
  454. dif = -dif;
  455. }
  456. else
  457. {
  458. switch (id)
  459. {
  460. case 6:
  461. dif--;
  462. break;
  463. case 5:
  464. dif++;
  465. break;
  466. }
  467. }
  468. return id + dif;
  469. }
  470. void CArtHandler::sortArts()
  471. {
  472. //for (int i=0; i<allowedArtifacts.size(); ++i) //do 144, bo nie chcemy bzdurek
  473. //{
  474. // switch (allowedArtifacts[i]->aClass)
  475. // {
  476. // case CArtifact::ART_TREASURE:
  477. // treasures.push_back(allowedArtifacts[i]);
  478. // break;
  479. // case CArtifact::ART_MINOR:
  480. // minors.push_back(allowedArtifacts[i]);
  481. // break;
  482. // case CArtifact::ART_MAJOR:
  483. // majors.push_back(allowedArtifacts[i]);
  484. // break;
  485. // case CArtifact::ART_RELIC:
  486. // relics.push_back(allowedArtifacts[i]);
  487. // break;
  488. // }
  489. //}
  490. }
  491. void CArtHandler::erasePickedArt( TArtifactInstanceID id )
  492. {
  493. std::vector<CArtifact*>* ptr;
  494. CArtifact *art = artifacts[id];
  495. switch (art->aClass)
  496. {
  497. case CArtifact::ART_TREASURE:
  498. ptr = &treasures;
  499. break;
  500. case CArtifact::ART_MINOR:
  501. ptr = &minors;
  502. break;
  503. case CArtifact::ART_MAJOR:
  504. ptr = &majors;
  505. break;
  506. case CArtifact::ART_RELIC:
  507. ptr = &relics;
  508. break;
  509. default: //special artifacts should not be erased
  510. return;
  511. }
  512. ptr->erase (std::find(ptr->begin(), ptr->end(), art)); //remove the artifact from available list
  513. }
  514. ui16 CArtHandler::getRandomArt(int flags)
  515. {
  516. std::vector<ConstTransitivePtr<CArtifact> > out;
  517. getAllowed(out, flags);
  518. ui16 id = out[ran() % out.size()]->id;
  519. erasePickedArt (id);
  520. return id;
  521. }
  522. ui16 CArtHandler::getArtSync (ui32 rand, int flags)
  523. {
  524. std::vector<ConstTransitivePtr<CArtifact> > out;
  525. getAllowed(out, flags);
  526. CArtifact *art = out[rand % out.size()];
  527. return art->id;
  528. }
  529. void CArtHandler::getAllowed(std::vector<ConstTransitivePtr<CArtifact> > &out, int flags)
  530. {
  531. if (flags & CArtifact::ART_TREASURE)
  532. getAllowedArts (out, &treasures, CArtifact::ART_TREASURE);
  533. if (flags & CArtifact::ART_MINOR)
  534. getAllowedArts (out, &minors, CArtifact::ART_MINOR);
  535. if (flags & CArtifact::ART_MAJOR)
  536. getAllowedArts (out, &majors, CArtifact::ART_MAJOR);
  537. if (flags & CArtifact::ART_RELIC)
  538. getAllowedArts (out, &relics, CArtifact::ART_RELIC);
  539. if (!out.size()) //no artifact of specified rarity, we need to take another one
  540. {
  541. getAllowedArts (out, &treasures, CArtifact::ART_TREASURE);
  542. getAllowedArts (out, &minors, CArtifact::ART_MINOR);
  543. getAllowedArts (out, &majors, CArtifact::ART_MAJOR);
  544. getAllowedArts (out, &relics, CArtifact::ART_RELIC);
  545. }
  546. if (!out.size()) //no arts are available at all
  547. {
  548. out.resize (64);
  549. std::fill_n (out.begin(), 64, artifacts[2]); //Give Grail - this can't be banned (hopefully)
  550. }
  551. }
  552. void CArtHandler::getAllowedArts(std::vector<ConstTransitivePtr<CArtifact> > &out, std::vector<CArtifact*> *arts, int flag)
  553. {
  554. if (arts->empty()) //restock available arts
  555. {
  556. for (int i = 0; i < allowedArtifacts.size(); ++i)
  557. {
  558. if (allowedArtifacts[i]->aClass == flag)
  559. arts->push_back(allowedArtifacts[i]);
  560. }
  561. }
  562. for (int i = 0; i < arts->size(); ++i)
  563. {
  564. CArtifact *art = (*arts)[i];
  565. out.push_back(art);
  566. }
  567. }
  568. Bonus *createBonus(Bonus::BonusType type, int val, int subtype, int valType, shared_ptr<ILimiter> limiter = shared_ptr<ILimiter>(), int additionalInfo = 0)
  569. {
  570. Bonus *added = new Bonus(Bonus::PERMANENT,type,Bonus::ARTIFACT,val,-1,subtype);
  571. added->additionalInfo = additionalInfo;
  572. added->valType = valType;
  573. added->limiter = limiter;
  574. return added;
  575. }
  576. Bonus *createBonus(Bonus::BonusType type, int val, int subtype, shared_ptr<IPropagator> propagator = shared_ptr<IPropagator>(), int additionalInfo = 0)
  577. {
  578. Bonus *added = new Bonus(Bonus::PERMANENT,type,Bonus::ARTIFACT,val,-1,subtype);
  579. added->additionalInfo = additionalInfo;
  580. added->valType = Bonus::BASE_NUMBER;
  581. added->propagator = propagator;
  582. return added;
  583. }
  584. void CArtHandler::giveArtBonus( TArtifactID aid, Bonus::BonusType type, int val, int subtype, int valType, shared_ptr<ILimiter> limiter, int additionalInfo)
  585. {
  586. giveArtBonus(aid, createBonus(type, val, subtype, valType, limiter, additionalInfo));
  587. }
  588. void CArtHandler::giveArtBonus(TArtifactID aid, Bonus::BonusType type, int val, int subtype, shared_ptr<IPropagator> propagator /*= NULL*/, int additionalInfo)
  589. {
  590. giveArtBonus(aid, createBonus(type, val, subtype, propagator, additionalInfo));
  591. }
  592. void CArtHandler::giveArtBonus(TArtifactID aid, Bonus *bonus)
  593. {
  594. bonus->sid = aid;
  595. if(bonus->subtype == Bonus::MORALE || bonus->type == Bonus::LUCK)
  596. bonus->description = artifacts[aid]->Name() + (bonus->val > 0 ? " +" : " ") + boost::lexical_cast<std::string>(bonus->val);
  597. else
  598. bonus->description = artifacts[aid]->Name();
  599. artifacts[aid]->addNewBonus(bonus);
  600. }
  601. void CArtHandler::makeItCreatureArt (CArtifact * a, bool onlyCreature /*=true*/)
  602. {
  603. if (onlyCreature)
  604. {
  605. a->possibleSlots[ArtBearer::HERO].clear();
  606. a->possibleSlots[ArtBearer::COMMANDER].clear();
  607. }
  608. a->possibleSlots[ArtBearer::CREATURE].push_back(ArtifactPosition::CREATURE_SLOT);
  609. }
  610. void CArtHandler::makeItCreatureArt (TArtifactInstanceID aid, bool onlyCreature /*=true*/)
  611. {
  612. CArtifact *a = artifacts[aid];
  613. makeItCreatureArt (a, onlyCreature);
  614. }
  615. void CArtHandler::makeItCommanderArt (CArtifact * a, bool onlyCommander /*= true*/ )
  616. {
  617. if (onlyCommander)
  618. {
  619. a->possibleSlots[ArtBearer::HERO].clear();
  620. a->possibleSlots[ArtBearer::CREATURE].clear();
  621. }
  622. for (int i = ArtifactPosition::COMMANDER1; i <= ArtifactPosition::COMMANDER6; ++i)
  623. a->possibleSlots[ArtBearer::COMMANDER].push_back(i);
  624. }
  625. void CArtHandler::makeItCommanderArt( TArtifactInstanceID aid, bool onlyCommander /*= true*/ )
  626. {
  627. CArtifact *a = artifacts[aid];
  628. makeItCommanderArt (a, onlyCommander);
  629. }
  630. void CArtHandler::addBonuses()
  631. {
  632. const JsonNode config(ResourceID("config/artifacts.json"));
  633. BOOST_FOREACH (auto & artifact, config["artifacts"].Struct()) //pair <string, JsonNode> (id, properties)
  634. {
  635. auto ga = artifacts[artifact.second["id"].Float()].get();
  636. BOOST_FOREACH (auto b, artifact.second["bonuses"].Vector())
  637. {
  638. auto bonus = JsonUtils::parseBonus (b);
  639. bonus->sid = ga->id;
  640. ga->addNewBonus (bonus);
  641. }
  642. if(artifact.second["type"].String() == "Creature")
  643. makeItCreatureArt(ga->id);
  644. else if(artifact.second["type"].String() == "Commander")
  645. makeItCommanderArt(ga->id);
  646. readComponents (artifact.second, ga);
  647. VLC->modh->identifiers.registerObject ("artifact." + artifact.first, ga->id);
  648. }
  649. }
  650. void CArtHandler::readComponents (const JsonNode & node, CArtifact * art)
  651. {
  652. const JsonNode *value;
  653. value = &node["components"];
  654. if (!value->isNull())
  655. {
  656. art->constituents = new std::vector<ui32>();
  657. BOOST_FOREACH (auto component, value->Vector())
  658. {
  659. VLC->modh->identifiers.requestIdentifier(std::string("artifact.") + component.String(),
  660. boost::bind (&CArtifact::addConstituent, art, _1)
  661. );
  662. }
  663. }
  664. }
  665. void CArtHandler::clear()
  666. {
  667. BOOST_FOREACH(CArtifact *art, artifacts)
  668. delete art;
  669. artifacts.clear();
  670. clearHlpLists();
  671. }
  672. void CArtHandler::clearHlpLists()
  673. {
  674. treasures.clear();
  675. minors.clear();
  676. majors.clear();
  677. relics.clear();
  678. }
  679. void CArtHandler::initAllowedArtifactsList(const std::vector<ui8> &allowed)
  680. {
  681. allowedArtifacts.clear();
  682. clearHlpLists();
  683. for (int i=0; i<144; ++i) //yes, 144
  684. {
  685. if (allowed[i])
  686. allowedArtifacts.push_back(artifacts[i]);
  687. }
  688. if (VLC->modh->modules.COMMANDERS) //allow all commander artifacts for testing
  689. {
  690. for (int i = 146; i <= 155; ++i)
  691. {
  692. allowedArtifacts.push_back(artifacts[i]);
  693. }
  694. }
  695. for (int i = GameConstants::ARTIFACTS_QUANTITY; i < artifacts.size(); ++i) //allow all new artifacts by default
  696. {
  697. if (artifacts[i]->possibleSlots[ArtBearer::HERO].size())
  698. allowedArtifacts.push_back(artifacts[i]);
  699. else //check if active modules allow artifact to be every used
  700. {
  701. if (artifacts[i]->possibleSlots[ArtBearer::COMMANDER].size() && VLC->modh->modules.COMMANDERS ||
  702. artifacts[i]->possibleSlots[ArtBearer::CREATURE].size() && VLC->modh->modules.STACK_ARTIFACT)
  703. allowedArtifacts.push_back(artifacts[i]);
  704. //keep im mind that artifact can be worn by more than one type of bearer
  705. }
  706. }
  707. }
  708. CArtifactInstance::CArtifactInstance()
  709. {
  710. init();
  711. }
  712. CArtifactInstance::CArtifactInstance( CArtifact *Art)
  713. {
  714. init();
  715. setType(Art);
  716. }
  717. // CArtifactInstance::CArtifactInstance(int aid)
  718. // {
  719. // init();
  720. // setType(VLC->arth->artifacts[aid]);
  721. // }
  722. void CArtifactInstance::setType( CArtifact *Art )
  723. {
  724. artType = Art;
  725. attachTo(Art);
  726. }
  727. std::string CArtifactInstance::nodeName() const
  728. {
  729. return "Artifact instance of " + (artType ? artType->Name() : std::string("uninitialized")) + " type";
  730. }
  731. CArtifactInstance * CArtifactInstance::createScroll( const CSpell *s)
  732. {
  733. CArtifactInstance *ret = new CArtifactInstance(VLC->arth->artifacts[1]);
  734. Bonus *b = new Bonus(Bonus::PERMANENT, Bonus::SPELL, Bonus::ARTIFACT_INSTANCE, -1, 1, s->id);
  735. ret->addNewBonus(b);
  736. return ret;
  737. }
  738. void CArtifactInstance::init()
  739. {
  740. id = -1;
  741. setNodeType(ARTIFACT_INSTANCE);
  742. }
  743. int CArtifactInstance::firstAvailableSlot(const CArtifactSet *h) const
  744. {
  745. BOOST_FOREACH(ui16 slot, artType->possibleSlots[h->bearerType()])
  746. {
  747. if(canBePutAt(h, slot)) //if(artType->fitsAt(h->artifWorn, slot))
  748. {
  749. //we've found a free suitable slot.
  750. return slot;
  751. }
  752. }
  753. //if haven't find proper slot, use backpack
  754. return firstBackpackSlot(h);
  755. }
  756. int CArtifactInstance::firstBackpackSlot(const CArtifactSet *h) const
  757. {
  758. if(!artType->isBig()) //discard big artifact
  759. return GameConstants::BACKPACK_START + h->artifactsInBackpack.size();
  760. return -1;
  761. }
  762. bool CArtifactInstance::canBePutAt(const ArtifactLocation al, bool assumeDestRemoved /*= false*/) const
  763. {
  764. return canBePutAt(al.getHolderArtSet(), al.slot, assumeDestRemoved);
  765. }
  766. bool CArtifactInstance::canBePutAt(const CArtifactSet *artSet, int slot, bool assumeDestRemoved /*= false*/) const
  767. {
  768. if(slot >= GameConstants::BACKPACK_START)
  769. {
  770. if(artType->isBig())
  771. return false;
  772. //TODO backpack limit
  773. return true;
  774. }
  775. auto possibleSlots = artType->possibleSlots.find(artSet->bearerType());
  776. if(possibleSlots == artType->possibleSlots.end())
  777. {
  778. tlog3 << "Warning: arrtifact " << artType->Name() << " doesn't have defined allowed slots for bearer of type "
  779. << artSet->bearerType() << std::endl;
  780. return false;
  781. }
  782. if(!vstd::contains(possibleSlots->second, slot))
  783. return false;
  784. return artSet->isPositionFree(slot, assumeDestRemoved);
  785. }
  786. void CArtifactInstance::putAt(ArtifactLocation al)
  787. {
  788. assert(canBePutAt(al));
  789. al.getHolderArtSet()->setNewArtSlot(al.slot, this, false);
  790. if(al.slot < GameConstants::BACKPACK_START)
  791. al.getHolderNode()->attachTo(this);
  792. }
  793. void CArtifactInstance::removeFrom(ArtifactLocation al)
  794. {
  795. assert(al.getHolderArtSet()->getArt(al.slot) == this);
  796. al.getHolderArtSet()->eraseArtSlot(al.slot);
  797. if(al.slot < GameConstants::BACKPACK_START)
  798. al.getHolderNode()->detachFrom(this);
  799. //TODO delete me?
  800. }
  801. bool CArtifactInstance::canBeDisassembled() const
  802. {
  803. return artType->constituents && artType->constituentOf->size();
  804. }
  805. std::vector<const CArtifact *> CArtifactInstance::assemblyPossibilities(const CArtifactSet *h) const
  806. {
  807. std::vector<const CArtifact *> ret;
  808. if(!artType->constituentOf //not a part of combined artifact
  809. || artType->constituents) //combined artifact already: no combining of combined artifacts... for now.
  810. return ret;
  811. BOOST_FOREACH(ui32 possibleCombinedArt, *artType->constituentOf)
  812. {
  813. const CArtifact * const artifact = VLC->arth->artifacts[possibleCombinedArt];
  814. assert(artifact->constituents);
  815. bool possible = true;
  816. BOOST_FOREACH(ui32 constituentID, *artifact->constituents) //check if all constituents are available
  817. {
  818. if(!h->hasArt(constituentID, true)) //constituent must be equipped
  819. {
  820. possible = false;
  821. break;
  822. }
  823. }
  824. if(possible)
  825. ret.push_back(artifact);
  826. }
  827. return ret;
  828. }
  829. void CArtifactInstance::move(ArtifactLocation src, ArtifactLocation dst)
  830. {
  831. removeFrom(src);
  832. putAt(dst);
  833. }
  834. CArtifactInstance * CArtifactInstance::createNewArtifactInstance(CArtifact *Art)
  835. {
  836. if(!Art->constituents)
  837. {
  838. auto ret = new CArtifactInstance(Art);
  839. if (dynamic_cast<CGrowingArtifact *>(Art))
  840. {
  841. Bonus * bonus = new Bonus;
  842. bonus->type = Bonus::LEVEL_COUNTER;
  843. bonus->val = 0;
  844. ret->addNewBonus (bonus);
  845. }
  846. return ret;
  847. }
  848. else
  849. {
  850. CCombinedArtifactInstance * ret = new CCombinedArtifactInstance(Art);
  851. ret->createConstituents();
  852. return ret;
  853. }
  854. }
  855. CArtifactInstance * CArtifactInstance::createNewArtifactInstance(int aid)
  856. {
  857. return createNewArtifactInstance(VLC->arth->artifacts[aid]);
  858. }
  859. void CArtifactInstance::deserializationFix()
  860. {
  861. setType(artType);
  862. }
  863. int CArtifactInstance::getGivenSpellID() const
  864. {
  865. const Bonus * b = getBonusLocalFirst(Selector::type(Bonus::SPELL));
  866. if(!b)
  867. {
  868. tlog3 << "Warning: " << nodeName() << " doesn't bear any spell!\n";
  869. return -1;
  870. }
  871. return b->subtype;
  872. }
  873. bool CArtifactInstance::isPart(const CArtifactInstance *supposedPart) const
  874. {
  875. return supposedPart == this;
  876. }
  877. bool CCombinedArtifactInstance::canBePutAt(const CArtifactSet *artSet, int slot, bool assumeDestRemoved /*= false*/) const
  878. {
  879. bool canMainArtifactBePlaced = CArtifactInstance::canBePutAt(artSet, slot, assumeDestRemoved);
  880. if(!canMainArtifactBePlaced)
  881. return false; //no is no...
  882. if(slot >= GameConstants::BACKPACK_START)
  883. return true; //we can always remove combined art to the backapck
  884. assert(artType->constituents);
  885. std::vector<ConstituentInfo> constituentsToBePlaced = constituentsInfo; //we'll remove constituents from that list, as we find a suitable slot for them
  886. //it may be that we picked a combined artifact in hero screen (though technically it's still there) to move it
  887. //so we remove from the list all constituents that are already present on dst hero in the form of locks
  888. BOOST_FOREACH(const ConstituentInfo &constituent, constituentsInfo)
  889. {
  890. if(constituent.art == artSet->getArt(constituent.slot, false)) //no need to worry about locked constituent
  891. constituentsToBePlaced -= constituent;
  892. }
  893. //we iterate over all active slots and check if constituents fits them
  894. for (int i = 0; i < GameConstants::BACKPACK_START; i++)
  895. {
  896. for(std::vector<ConstituentInfo>::iterator art = constituentsToBePlaced.begin(); art != constituentsToBePlaced.end(); art++)
  897. {
  898. 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
  899. {
  900. constituentsToBePlaced.erase(art);
  901. break;
  902. }
  903. }
  904. }
  905. return constituentsToBePlaced.empty();
  906. }
  907. bool CCombinedArtifactInstance::canBeDisassembled() const
  908. {
  909. return true;
  910. }
  911. CCombinedArtifactInstance::CCombinedArtifactInstance(CArtifact *Art)
  912. : CArtifactInstance(Art) //TODO: seems unued, but need to be written
  913. {
  914. }
  915. CCombinedArtifactInstance::CCombinedArtifactInstance()
  916. {
  917. }
  918. void CCombinedArtifactInstance::createConstituents()
  919. {
  920. assert(artType);
  921. assert(artType->constituents);
  922. BOOST_FOREACH(ui32 a, *artType->constituents)
  923. {
  924. addAsConstituent(CArtifactInstance::createNewArtifactInstance(a), -1);
  925. }
  926. }
  927. void CCombinedArtifactInstance::addAsConstituent(CArtifactInstance *art, int slot)
  928. {
  929. assert(vstd::contains(*artType->constituents, art->artType->id));
  930. assert(art->getParentNodes().size() == 1 && art->getParentNodes().front() == art->artType);
  931. constituentsInfo.push_back(ConstituentInfo(art, slot));
  932. attachTo(art);
  933. }
  934. void CCombinedArtifactInstance::putAt(ArtifactLocation al)
  935. {
  936. if(al.slot >= GameConstants::BACKPACK_START)
  937. {
  938. CArtifactInstance::putAt(al);
  939. BOOST_FOREACH(ConstituentInfo &ci, constituentsInfo)
  940. ci.slot = -1;
  941. }
  942. else
  943. {
  944. CArtifactInstance *mainConstituent = figureMainConstituent(al); //it'll be replaced with combined artifact, not a lock
  945. CArtifactInstance::putAt(al); //puts combined art (this)
  946. BOOST_FOREACH(ConstituentInfo &ci, constituentsInfo)
  947. {
  948. if(ci.art != mainConstituent)
  949. {
  950. const ArtifactLocation suggestedPos(al.artHolder, ci.slot);
  951. const bool inActiveSlot = vstd::isbetween(ci.slot, 0, GameConstants::BACKPACK_START);
  952. const bool suggestedPosValid = ci.art->canBePutAt(suggestedPos);
  953. int pos = -1;
  954. if(inActiveSlot && suggestedPosValid) //there is a valid suggestion where to place lock
  955. pos = ci.slot;
  956. else
  957. ci.slot = pos = ci.art->firstAvailableSlot(al.getHolderArtSet());
  958. assert(pos < GameConstants::BACKPACK_START);
  959. al.getHolderArtSet()->setNewArtSlot(pos, ci.art, true); //sets as lock
  960. }
  961. else
  962. {
  963. ci.slot = -1;
  964. }
  965. }
  966. }
  967. }
  968. void CCombinedArtifactInstance::removeFrom(ArtifactLocation al)
  969. {
  970. if(al.slot >= GameConstants::BACKPACK_START)
  971. {
  972. CArtifactInstance::removeFrom(al);
  973. }
  974. else
  975. {
  976. BOOST_FOREACH(ConstituentInfo &ci, constituentsInfo)
  977. {
  978. if(ci.slot >= 0)
  979. {
  980. al.getHolderArtSet()->eraseArtSlot(ci.slot);
  981. ci.slot = -1;
  982. }
  983. else
  984. {
  985. //main constituent
  986. CArtifactInstance::removeFrom(al);
  987. }
  988. }
  989. }
  990. }
  991. CArtifactInstance * CCombinedArtifactInstance::figureMainConstituent(const ArtifactLocation al)
  992. {
  993. CArtifactInstance *mainConstituent = NULL; //it'll be replaced with combined artifact, not a lock
  994. BOOST_FOREACH(ConstituentInfo &ci, constituentsInfo)
  995. if(ci.slot == al.slot)
  996. mainConstituent = ci.art;
  997. if(!mainConstituent)
  998. {
  999. BOOST_FOREACH(ConstituentInfo &ci, constituentsInfo)
  1000. {
  1001. if(vstd::contains(ci.art->artType->possibleSlots[al.getHolderArtSet()->bearerType()], al.slot))
  1002. {
  1003. mainConstituent = ci.art;
  1004. }
  1005. }
  1006. }
  1007. return mainConstituent;
  1008. }
  1009. void CCombinedArtifactInstance::deserializationFix()
  1010. {
  1011. BOOST_FOREACH(ConstituentInfo &ci, constituentsInfo)
  1012. attachTo(ci.art);
  1013. }
  1014. bool CCombinedArtifactInstance::isPart(const CArtifactInstance *supposedPart) const
  1015. {
  1016. bool me = CArtifactInstance::isPart(supposedPart);
  1017. if(me)
  1018. return true;
  1019. //check for constituents
  1020. BOOST_FOREACH(const ConstituentInfo &constituent, constituentsInfo)
  1021. if(constituent.art == supposedPart)
  1022. return true;
  1023. return false;
  1024. }
  1025. CCombinedArtifactInstance::ConstituentInfo::ConstituentInfo(CArtifactInstance *Art /*= NULL*/, ui16 Slot /*= -1*/)
  1026. {
  1027. art = Art;
  1028. slot = Slot;
  1029. }
  1030. bool CCombinedArtifactInstance::ConstituentInfo::operator==(const ConstituentInfo &rhs) const
  1031. {
  1032. return art == rhs.art && slot == rhs.slot;
  1033. }
  1034. const CArtifactInstance* CArtifactSet::getArt(ui16 pos, bool excludeLocked /*= true*/) const
  1035. {
  1036. if(const ArtSlotInfo *si = getSlot(pos))
  1037. {
  1038. if(si->artifact && (!excludeLocked || !si->locked))
  1039. return si->artifact;
  1040. }
  1041. return NULL;
  1042. }
  1043. CArtifactInstance* CArtifactSet::getArt(ui16 pos, bool excludeLocked /*= true*/)
  1044. {
  1045. return const_cast<CArtifactInstance*>((const_cast<const CArtifactSet*>(this))->getArt(pos, excludeLocked));
  1046. }
  1047. si32 CArtifactSet::getArtPos(int aid, bool onlyWorn /*= true*/) const
  1048. {
  1049. for(std::map<ui16, ArtSlotInfo>::const_iterator i = artifactsWorn.begin(); i != artifactsWorn.end(); i++)
  1050. if(i->second.artifact->artType->id == aid)
  1051. return i->first;
  1052. if(onlyWorn)
  1053. return -1;
  1054. for(int i = 0; i < artifactsInBackpack.size(); i++)
  1055. if(artifactsInBackpack[i].artifact->artType->id == aid)
  1056. return GameConstants::BACKPACK_START + i;
  1057. return -1;
  1058. }
  1059. si32 CArtifactSet::getArtPos(const CArtifactInstance *art) const
  1060. {
  1061. for(std::map<ui16, ArtSlotInfo>::const_iterator i = artifactsWorn.begin(); i != artifactsWorn.end(); i++)
  1062. if(i->second.artifact == art)
  1063. return i->first;
  1064. for(int i = 0; i < artifactsInBackpack.size(); i++)
  1065. if(artifactsInBackpack[i].artifact == art)
  1066. return GameConstants::BACKPACK_START + i;
  1067. return -1;
  1068. }
  1069. const CArtifactInstance * CArtifactSet::getArtByInstanceId( TArtifactInstanceID artInstId ) const
  1070. {
  1071. for(std::map<ui16, ArtSlotInfo>::const_iterator i = artifactsWorn.begin(); i != artifactsWorn.end(); i++)
  1072. if(i->second.artifact->id == artInstId)
  1073. return i->second.artifact;
  1074. for(int i = 0; i < artifactsInBackpack.size(); i++)
  1075. if(artifactsInBackpack[i].artifact->id == artInstId)
  1076. return artifactsInBackpack[i].artifact;
  1077. return NULL;
  1078. }
  1079. bool CArtifactSet::hasArt(ui32 aid, bool onlyWorn /*= false*/) const
  1080. {
  1081. return getArtPos(aid, onlyWorn) != -1;
  1082. }
  1083. const ArtSlotInfo * CArtifactSet::getSlot(ui16 pos) const
  1084. {
  1085. if(vstd::contains(artifactsWorn, pos))
  1086. return &artifactsWorn[pos];
  1087. if(pos >= ArtifactPosition::AFTER_LAST )
  1088. {
  1089. int backpackPos = (int)pos - GameConstants::BACKPACK_START;
  1090. if(backpackPos < 0 || backpackPos >= artifactsInBackpack.size())
  1091. return NULL;
  1092. else
  1093. return &artifactsInBackpack[backpackPos];
  1094. }
  1095. return NULL;
  1096. }
  1097. bool CArtifactSet::isPositionFree(ui16 pos, bool onlyLockCheck /*= false*/) const
  1098. {
  1099. if(const ArtSlotInfo *s = getSlot(pos))
  1100. return (onlyLockCheck || !s->artifact) && !s->locked;
  1101. return true; //no slot means not used
  1102. }
  1103. si32 CArtifactSet::getArtTypeId(ui16 pos) const
  1104. {
  1105. const CArtifactInstance * const a = getArt(pos);
  1106. if(!a)
  1107. {
  1108. tlog2 << (dynamic_cast<const CGHeroInstance*>(this))->name << " has no artifact at " << pos << " (getArtTypeId)\n";
  1109. return -1;
  1110. }
  1111. return a->artType->id;
  1112. }
  1113. CArtifactSet::~CArtifactSet()
  1114. {
  1115. }
  1116. ArtSlotInfo & CArtifactSet::retreiveNewArtSlot(ui16 slot)
  1117. {
  1118. assert(!vstd::contains(artifactsWorn, slot));
  1119. ArtSlotInfo &ret = slot < GameConstants::BACKPACK_START
  1120. ? artifactsWorn[slot]
  1121. : *artifactsInBackpack.insert(artifactsInBackpack.begin() + (slot - GameConstants::BACKPACK_START), ArtSlotInfo());
  1122. return ret;
  1123. }
  1124. void CArtifactSet::setNewArtSlot(ui16 slot, CArtifactInstance *art, bool locked)
  1125. {
  1126. ArtSlotInfo &asi = retreiveNewArtSlot(slot);
  1127. asi.artifact = art;
  1128. asi.locked = locked;
  1129. }
  1130. void CArtifactSet::eraseArtSlot(ui16 slot)
  1131. {
  1132. if(slot < GameConstants::BACKPACK_START)
  1133. {
  1134. artifactsWorn.erase(slot);
  1135. }
  1136. else
  1137. {
  1138. slot -= GameConstants::BACKPACK_START;
  1139. artifactsInBackpack.erase(artifactsInBackpack.begin() + slot);
  1140. }
  1141. }
  1142. void CArtifactSet::artDeserializationFix(CBonusSystemNode *node)
  1143. {
  1144. for(bmap<ui16, ArtSlotInfo>::iterator i = artifactsWorn.begin(); i != artifactsWorn.end(); i++)
  1145. if(i->second.artifact && !i->second.locked)
  1146. node->attachTo(i->second.artifact);
  1147. }