CArtHandler.cpp 35 KB

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