CArtHandler.cpp 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594
  1. #include "StdInc.h"
  2. #include "CArtHandler.h"
  3. #include "Filesystem/CResourceLoader.h"
  4. #include "CGeneralTextHandler.h"
  5. #include <boost/random/linear_congruential.hpp>
  6. #include "VCMI_Lib.h"
  7. #include "CModHandler.h"
  8. #include "CSpellHandler.h"
  9. #include "CObjectHandler.h"
  10. #include "NetPacks.h"
  11. #include "JsonNode.h"
  12. using namespace boost::assign;
  13. /*
  14. * CArtHandler.cpp, part of VCMI engine
  15. *
  16. * Authors: listed in file AUTHORS in main folder
  17. *
  18. * License: GNU General Public License v2.0 or later
  19. * Full text of license available in license.txt file, in main folder
  20. *
  21. */
  22. extern boost::rand48 ran;
  23. extern Bonus * ParseBonus (const JsonVector &ability_vec);
  24. const std::string & CArtifact::Name() const
  25. {
  26. if(name.size())
  27. return name;
  28. else
  29. return VLC->generaltexth->artifNames[id];
  30. }
  31. const std::string & CArtifact::Description() const
  32. {
  33. if(description.size())
  34. return description;
  35. else
  36. return VLC->generaltexth->artifDescriptions[id];
  37. }
  38. bool CArtifact::isBig () const
  39. {
  40. return VLC->arth->isBigArtifact(id);
  41. }
  42. // /**
  43. // * Checks whether the artifact fits at a given slot.
  44. // * @param artifWorn A hero's set of worn artifacts.
  45. // */
  46. // bool CArtifact::fitsAt (const std::map<ui16, const CArtifact*> &artifWorn, ui16 slotID) const
  47. // {
  48. // if (!vstd::contains(possibleSlots, slotID))
  49. // return false;
  50. //
  51. // // Can't put an artifact in a locked slot.
  52. // std::map<ui16, const CArtifact*>::const_iterator it = artifWorn.find(slotID);
  53. // if (it != artifWorn.end() && it->second->id == 145)
  54. // return false;
  55. //
  56. // // Check if a combination artifact fits.
  57. // // TODO: Might want a more general algorithm?
  58. // // Assumes that misc & rings fits only in their slots, and others in only one slot and no duplicates.
  59. // if (constituents != NULL)
  60. // {
  61. // std::map<ui16, const CArtifact*> tempArtifWorn = artifWorn;
  62. // const ui16 ringSlots[] = {6, 7};
  63. // const ui16 miscSlots[] = {9, 10, 11, 12, 18};
  64. // int rings = 0;
  65. // int misc = 0;
  66. //
  67. // VLC->arth->unequipArtifact(tempArtifWorn, slotID);
  68. //
  69. // BOOST_FOREACH(ui32 constituentID, *constituents)
  70. // {
  71. // const CArtifact& constituent = *VLC->arth->artifacts[constituentID];
  72. // const int slot = constituent.possibleSlots[0];
  73. //
  74. // if (slot == 6 || slot == 7)
  75. // rings++;
  76. // else if ((slot >= 9 && slot <= 12) || slot == 18)
  77. // misc++;
  78. // else if (tempArtifWorn.find(slot) != tempArtifWorn.end())
  79. // return false;
  80. // }
  81. //
  82. // // Ensure enough ring slots are free
  83. // for (int i = 0; i < sizeof(ringSlots)/sizeof(*ringSlots); i++)
  84. // {
  85. // if (tempArtifWorn.find(ringSlots[i]) == tempArtifWorn.end() || ringSlots[i] == slotID)
  86. // rings--;
  87. // }
  88. // if (rings > 0)
  89. // return false;
  90. //
  91. // // Ensure enough misc slots are free.
  92. // for (int i = 0; i < sizeof(miscSlots)/sizeof(*miscSlots); i++)
  93. // {
  94. // if (tempArtifWorn.find(miscSlots[i]) == tempArtifWorn.end() || miscSlots[i] == slotID)
  95. // misc--;
  96. // }
  97. // if (misc > 0)
  98. // return false;
  99. // }
  100. //
  101. // return true;
  102. // }
  103. // bool CArtifact::canBeAssembledTo (const std::map<ui16, const CArtifact*> &artifWorn, ui32 artifactID) const
  104. // {
  105. // if (constituentOf == NULL || !vstd::contains(*constituentOf, artifactID))
  106. // return false;
  107. //
  108. // const CArtifact &artifact = *VLC->arth->artifacts[artifactID];
  109. // assert(artifact.constituents);
  110. //
  111. // BOOST_FOREACH(ui32 constituentID, *artifact.constituents)
  112. // {
  113. // bool found = false;
  114. // for (std::map<ui16, const CArtifact*>::const_iterator it = artifWorn.begin(); it != artifWorn.end(); ++it)
  115. // {
  116. // if (it->second->id == constituentID)
  117. // {
  118. // found = true;
  119. // break;
  120. // }
  121. // }
  122. // if (!found)
  123. // return false;
  124. // }
  125. //
  126. // return true;
  127. // }
  128. CArtifact::CArtifact()
  129. {
  130. setNodeType(ARTIFACT);
  131. }
  132. CArtifact::~CArtifact()
  133. {
  134. }
  135. int CArtifact::getArtClassSerial() const
  136. {
  137. if(id == 1)
  138. return 4;
  139. switch(aClass)
  140. {
  141. case ART_TREASURE:
  142. return 0;
  143. case ART_MINOR:
  144. return 1;
  145. case ART_MAJOR:
  146. return 2;
  147. case ART_RELIC:
  148. return 3;
  149. case ART_SPECIAL:
  150. return 5;
  151. }
  152. return -1;
  153. }
  154. std::string CArtifact::nodeName() const
  155. {
  156. return "Artifact: " + Name();
  157. }
  158. // void CArtifact::getParents(TCNodes &out, const CBonusSystemNode *root /*= NULL*/) const
  159. // {
  160. // //combined artifact carries bonuses from its parts
  161. // if(constituents)
  162. // {
  163. // BOOST_FOREACH(ui32 id, *constituents)
  164. // out.insert(VLC->arth->artifacts[id]);
  165. // }
  166. // }
  167. // void CScroll::Init()
  168. // {
  169. // // addNewBonus (Bonus (Bonus::PERMANENT, Bonus::SPELL, Bonus::ARTIFACT, 1, id, spellid, Bonus::INDEPENDENT_MAX));
  170. // // //boost::algorithm::replace_first(description, "[spell name]", VLC->spellh->spells[spellid].name);
  171. // }
  172. void CArtifact::setDescription (std::string desc)
  173. {
  174. description = desc;
  175. }
  176. void CGrowingArtifact::levelUpArtifact (CArtifactInstance * art)
  177. {
  178. Bonus b;
  179. b.type = Bonus::LEVEL_COUNTER;
  180. b.val = 1;
  181. b.duration = Bonus::COMMANDER_KILLED;
  182. art->accumulateBonus (b);
  183. BOOST_FOREACH (auto bonus, bonusesPerLevel)
  184. {
  185. if (art->valOfBonuses(Bonus::LEVEL_COUNTER) % bonus.first == 0) //every n levels
  186. {
  187. art->accumulateBonus (bonus.second);
  188. }
  189. }
  190. BOOST_FOREACH (auto bonus, thresholdBonuses)
  191. {
  192. if (art->valOfBonuses(Bonus::LEVEL_COUNTER) == bonus.first) //every n levels
  193. {
  194. art->addNewBonus (&bonus.second);
  195. }
  196. }
  197. }
  198. CArtHandler::CArtHandler()
  199. {
  200. VLC->arth = this;
  201. // War machines are the default big artifacts.
  202. for (ui32 i = 3; i <= 6; i++)
  203. bigArtifacts.insert(i);
  204. }
  205. CArtHandler::~CArtHandler()
  206. {
  207. for (std::vector< ConstTransitivePtr<CArtifact> >::iterator it = artifacts.begin(); it != artifacts.end(); ++it)
  208. {
  209. delete (*it)->constituents;
  210. delete (*it)->constituentOf;
  211. }
  212. }
  213. void CArtHandler::loadArtifacts(bool onlyTxt)
  214. {
  215. std::vector<ui16> slots;
  216. slots += 17, 16, 15, 14, 13, 18, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0;
  217. growingArtifacts += 146, 147, 148, 150, 151, 152, 153;
  218. static std::map<char, CArtifact::EartClass> classes =
  219. map_list_of('S',CArtifact::ART_SPECIAL)('T',CArtifact::ART_TREASURE)('N',CArtifact::ART_MINOR)('J',CArtifact::ART_MAJOR)('R',CArtifact::ART_RELIC);
  220. CLegacyConfigParser parser("DATA/ARTRAITS.TXT");
  221. parser.endLine(); // header
  222. parser.endLine();
  223. VLC->generaltexth->artifNames.resize(GameConstants::ARTIFACTS_QUANTITY);
  224. VLC->generaltexth->artifDescriptions.resize(GameConstants::ARTIFACTS_QUANTITY);
  225. std::map<ui32,ui8>::iterator itr;
  226. for (int i=0; i<GameConstants::ARTIFACTS_QUANTITY; i++)
  227. {
  228. CArtifact *art;
  229. if (vstd::contains (growingArtifacts, i))
  230. {
  231. art = new CGrowingArtifact();
  232. }
  233. else
  234. {
  235. art = new CArtifact();
  236. }
  237. CArtifact &nart = *art;
  238. nart.id=i;
  239. VLC->generaltexth->artifNames[i] = parser.readString();
  240. nart.price= parser.readNumber();
  241. nart.possibleSlots[ArtBearer::HERO]; //we want to generate map entry even if it will be empty
  242. nart.possibleSlots[ArtBearer::CREATURE]; //we want to generate map entry even if it will be empty
  243. nart.possibleSlots[ArtBearer::COMMANDER];
  244. for(int j=0;j<slots.size();j++)
  245. {
  246. if(parser.readString() == "x")
  247. nart.possibleSlots[ArtBearer::HERO].push_back(slots[j]);
  248. }
  249. nart.aClass = classes[parser.readString()[0]];
  250. //load description and remove quotation marks
  251. VLC->generaltexth->artifDescriptions[i] = parser.readString();
  252. parser.endLine();
  253. if(onlyTxt)
  254. continue;
  255. // Fill in information about combined artifacts. Should perhaps be moved to a config file?
  256. nart.constituentOf = NULL;
  257. switch (nart.id)
  258. {
  259. case 129: // Angelic Alliance
  260. nart.constituents = new std::vector<ui32>();
  261. *nart.constituents += 31, 32, 33, 34, 35, 36;
  262. break;
  263. case 130: // Cloak of the Undead King
  264. nart.constituents = new std::vector<ui32>();
  265. *nart.constituents += 54, 55, 56;
  266. break;
  267. case 131: // Elixir of Life
  268. nart.constituents = new std::vector<ui32>();
  269. *nart.constituents += 94, 95, 96;
  270. break;
  271. case 132: // Armor of the Damned
  272. nart.constituents = new std::vector<ui32>();
  273. *nart.constituents += 8, 14, 20, 26;
  274. break;
  275. case 133: // Statue of Legion
  276. nart.constituents = new std::vector<ui32>();
  277. *nart.constituents += 118, 119, 120, 121, 122;
  278. break;
  279. case 134: // Power of the Dragon Father
  280. nart.constituents = new std::vector<ui32>();
  281. *nart.constituents += 37, 38, 39, 40, 41, 42, 43, 44, 45;
  282. break;
  283. case 135: // Titan's Thunder
  284. nart.constituents = new std::vector<ui32>();
  285. *nart.constituents += 12, 18, 24, 30;
  286. break;
  287. case 136: // Admiral's Hat
  288. nart.constituents = new std::vector<ui32>();
  289. *nart.constituents += 71, 123;
  290. break;
  291. case 137: // Bow of the Sharpshooter
  292. nart.constituents = new std::vector<ui32>();
  293. *nart.constituents += 60, 61, 62;
  294. break;
  295. case 138: // Wizards' Well
  296. nart.constituents = new std::vector<ui32>();
  297. *nart.constituents += 73, 74, 75;
  298. break;
  299. case 139: // Ring of the Magi
  300. nart.constituents = new std::vector<ui32>();
  301. *nart.constituents += 76, 77, 78;
  302. break;
  303. case 140: // Cornucopia
  304. nart.constituents = new std::vector<ui32>();
  305. *nart.constituents += 109, 110, 111, 113;
  306. break;
  307. // TODO: WoG combinationals
  308. default:
  309. nart.constituents = NULL;
  310. break;
  311. }
  312. artifacts.push_back(&nart);
  313. }
  314. if (VLC->modh->modules.COMMANDERS)
  315. { //TODO: move all artifacts config to separate json file
  316. const JsonNode config(ResourceID("config/commanders.json"));
  317. BOOST_FOREACH(const JsonNode &artifact, config["artifacts"].Vector())
  318. {
  319. auto ga = dynamic_cast <CGrowingArtifact *>(artifacts[artifact["id"].Float()].get());
  320. BOOST_FOREACH (auto b, artifact["bonusesPerLevel"].Vector())
  321. {
  322. ga->bonusesPerLevel.push_back (std::pair <ui16, Bonus> (b["level"].Float(), *ParseBonus (b["bonus"].Vector())));
  323. }
  324. BOOST_FOREACH (auto b, artifact["thresholdBonuses"].Vector())
  325. {
  326. ga->thresholdBonuses.push_back (std::pair <ui16, Bonus> (b["level"].Float(), *ParseBonus (b["bonus"].Vector())));
  327. }
  328. }
  329. }
  330. sortArts();
  331. if(onlyTxt)
  332. return;
  333. addBonuses();
  334. // Populate reverse mappings of combinational artifacts.
  335. BOOST_FOREACH(CArtifact *artifact, artifacts)
  336. {
  337. if (artifact->constituents != NULL)
  338. {
  339. BOOST_FOREACH(ui32 constituentID, *artifact->constituents)
  340. {
  341. if (artifacts[constituentID]->constituentOf == NULL)
  342. artifacts[constituentID]->constituentOf = new std::vector<ui32>();
  343. artifacts[constituentID]->constituentOf->push_back(artifact->id);
  344. }
  345. }
  346. }
  347. }
  348. int CArtHandler::convertMachineID(int id, bool creToArt )
  349. {
  350. int dif = 142;
  351. if(creToArt)
  352. {
  353. switch (id)
  354. {
  355. case 147:
  356. dif--;
  357. break;
  358. case 148:
  359. dif++;
  360. break;
  361. }
  362. dif = -dif;
  363. }
  364. else
  365. {
  366. switch (id)
  367. {
  368. case 6:
  369. dif--;
  370. break;
  371. case 5:
  372. dif++;
  373. break;
  374. }
  375. }
  376. return id + dif;
  377. }
  378. void CArtHandler::sortArts()
  379. {
  380. //for (int i=0; i<allowedArtifacts.size(); ++i) //do 144, bo nie chcemy bzdurek
  381. //{
  382. // switch (allowedArtifacts[i]->aClass)
  383. // {
  384. // case CArtifact::ART_TREASURE:
  385. // treasures.push_back(allowedArtifacts[i]);
  386. // break;
  387. // case CArtifact::ART_MINOR:
  388. // minors.push_back(allowedArtifacts[i]);
  389. // break;
  390. // case CArtifact::ART_MAJOR:
  391. // majors.push_back(allowedArtifacts[i]);
  392. // break;
  393. // case CArtifact::ART_RELIC:
  394. // relics.push_back(allowedArtifacts[i]);
  395. // break;
  396. // }
  397. //}
  398. }
  399. void CArtHandler::erasePickedArt( TArtifactInstanceID id )
  400. {
  401. std::vector<CArtifact*>* ptr;
  402. CArtifact *art = artifacts[id];
  403. switch (art->aClass)
  404. {
  405. case CArtifact::ART_TREASURE:
  406. ptr = &treasures;
  407. break;
  408. case CArtifact::ART_MINOR:
  409. ptr = &minors;
  410. break;
  411. case CArtifact::ART_MAJOR:
  412. ptr = &majors;
  413. break;
  414. case CArtifact::ART_RELIC:
  415. ptr = &relics;
  416. break;
  417. default: //special artifacts should not be erased
  418. return;
  419. }
  420. ptr->erase (std::find(ptr->begin(), ptr->end(), art)); //remove the artifact from available list
  421. }
  422. ui16 CArtHandler::getRandomArt(int flags)
  423. {
  424. std::vector<ConstTransitivePtr<CArtifact> > out;
  425. getAllowed(out, flags);
  426. ui16 id = out[ran() % out.size()]->id;
  427. erasePickedArt (id);
  428. return id;
  429. }
  430. ui16 CArtHandler::getArtSync (ui32 rand, int flags)
  431. {
  432. std::vector<ConstTransitivePtr<CArtifact> > out;
  433. getAllowed(out, flags);
  434. CArtifact *art = out[rand % out.size()];
  435. return art->id;
  436. }
  437. void CArtHandler::getAllowed(std::vector<ConstTransitivePtr<CArtifact> > &out, int flags)
  438. {
  439. if (flags & CArtifact::ART_TREASURE)
  440. getAllowedArts (out, &treasures, CArtifact::ART_TREASURE);
  441. if (flags & CArtifact::ART_MINOR)
  442. getAllowedArts (out, &minors, CArtifact::ART_MINOR);
  443. if (flags & CArtifact::ART_MAJOR)
  444. getAllowedArts (out, &majors, CArtifact::ART_MAJOR);
  445. if (flags & CArtifact::ART_RELIC)
  446. getAllowedArts (out, &relics, CArtifact::ART_RELIC);
  447. if (!out.size()) //no artifact of specified rarity, we need to take another one
  448. {
  449. getAllowedArts (out, &treasures, CArtifact::ART_TREASURE);
  450. getAllowedArts (out, &minors, CArtifact::ART_MINOR);
  451. getAllowedArts (out, &majors, CArtifact::ART_MAJOR);
  452. getAllowedArts (out, &relics, CArtifact::ART_RELIC);
  453. }
  454. if (!out.size()) //no arts are available at all
  455. {
  456. out.resize (64);
  457. std::fill_n (out.begin(), 64, artifacts[2]); //Give Grail - this can't be banned (hopefully)
  458. }
  459. }
  460. void CArtHandler::getAllowedArts(std::vector<ConstTransitivePtr<CArtifact> > &out, std::vector<CArtifact*> *arts, int flag)
  461. {
  462. if (arts->empty()) //restock available arts
  463. {
  464. for (int i = 0; i < allowedArtifacts.size(); ++i)
  465. {
  466. if (allowedArtifacts[i]->aClass == flag)
  467. arts->push_back(allowedArtifacts[i]);
  468. }
  469. }
  470. for (int i = 0; i < arts->size(); ++i)
  471. {
  472. CArtifact *art = (*arts)[i];
  473. out.push_back(art);
  474. }
  475. }
  476. Bonus *createBonus(Bonus::BonusType type, int val, int subtype, int valType, shared_ptr<ILimiter> limiter = nullptr, int additionalInfo = 0)
  477. {
  478. Bonus *added = new Bonus(Bonus::PERMANENT,type,Bonus::ARTIFACT,val,-1,subtype);
  479. added->additionalInfo = additionalInfo;
  480. added->valType = valType;
  481. added->limiter = limiter;
  482. return added;
  483. }
  484. Bonus *createBonus(Bonus::BonusType type, int val, int subtype, shared_ptr<IPropagator> propagator = nullptr, int additionalInfo = 0)
  485. {
  486. Bonus *added = new Bonus(Bonus::PERMANENT,type,Bonus::ARTIFACT,val,-1,subtype);
  487. added->additionalInfo = additionalInfo;
  488. added->valType = Bonus::BASE_NUMBER;
  489. added->propagator = propagator;
  490. return added;
  491. }
  492. void CArtHandler::giveArtBonus( TArtifactID aid, Bonus::BonusType type, int val, int subtype, int valType, shared_ptr<ILimiter> limiter, int additionalInfo)
  493. {
  494. giveArtBonus(aid, createBonus(type, val, subtype, valType, limiter, additionalInfo));
  495. }
  496. void CArtHandler::giveArtBonus(TArtifactID aid, Bonus::BonusType type, int val, int subtype, shared_ptr<IPropagator> propagator /*= NULL*/, int additionalInfo)
  497. {
  498. giveArtBonus(aid, createBonus(type, val, subtype, propagator, additionalInfo));
  499. }
  500. void CArtHandler::giveArtBonus(TArtifactID aid, Bonus *bonus)
  501. {
  502. bonus->sid = aid;
  503. if(bonus->subtype == Bonus::MORALE || bonus->type == Bonus::LUCK)
  504. bonus->description = artifacts[aid]->Name() + (bonus->val > 0 ? " +" : " ") + boost::lexical_cast<std::string>(bonus->val);
  505. else
  506. bonus->description = artifacts[aid]->Name();
  507. artifacts[aid]->addNewBonus(bonus);
  508. }
  509. void CArtHandler::makeItCreatureArt (TArtifactInstanceID aid, bool onlyCreature /*=true*/)
  510. {
  511. CArtifact *a = artifacts[aid];
  512. if (onlyCreature)
  513. {
  514. a->possibleSlots[ArtBearer::HERO].clear();
  515. a->possibleSlots[ArtBearer::COMMANDER].clear();
  516. }
  517. a->possibleSlots[ArtBearer::CREATURE].push_back(ArtifactPosition::CREATURE_SLOT);
  518. }
  519. void CArtHandler::makeItCommanderArt( TArtifactInstanceID aid, bool onlyCommander /*= true*/ )
  520. {
  521. CArtifact *a = artifacts[aid];
  522. if (onlyCommander)
  523. {
  524. a->possibleSlots[ArtBearer::HERO].clear();
  525. a->possibleSlots[ArtBearer::CREATURE].clear();
  526. }
  527. for (int i = ArtifactPosition::COMMANDER1; i <= ArtifactPosition::COMMANDER6; ++i)
  528. a->possibleSlots[ArtBearer::COMMANDER].push_back(i);
  529. }
  530. void CArtHandler::addBonuses()
  531. {
  532. // #define ART_PRIM_SKILL(ID, whichSkill, val) giveArtBonus(ID,Bonus::PRIMARY_SKILL,val,whichSkill)
  533. // #define ART_MORALE(ID, val) giveArtBonus(ID,Bonus::MORALE,val)
  534. // #define ART_LUCK(ID, val) giveArtBonus(ID,Bonus::LUCK,val)
  535. // #define ART_MORALE_AND_LUCK(ID, val) giveArtBonus(ID,Bonus::MORALE_AND_LUCK,val)
  536. // #define ART_ALL_PRIM_SKILLS(ID, val) ART_PRIM_SKILL(ID,0,val); ART_PRIM_SKILL(ID,1,val); ART_PRIM_SKILL(ID,2,val); ART_PRIM_SKILL(ID,3,val)
  537. // #define ART_ATTACK_AND_DEFENSE(ID, val) ART_PRIM_SKILL(ID,0,val); ART_PRIM_SKILL(ID,1,val)
  538. // #define ART_POWER_AND_KNOWLEDGE(ID, val) ART_PRIM_SKILL(ID,2,val); ART_PRIM_SKILL(ID,3,val)
  539. //
  540. // //Propagators/limiters used more than once
  541. // auto battleWidePropagator = bonusPropagatorMap["BATTLE_WIDE"];
  542. // auto visitedTownPropagator = bonusPropagatorMap["VISITED_TOWN_AND_VISITOR"];
  543. //
  544. // auto shooterOnlyLimiter = bonusLimiterMap["SHOOTER_ONLY"];
  545. // auto dragonNatureLimiter = bonusLimiterMap["DRAGON_NATURE"];
  546. // auto isUndeadLimiter = bonusLimiterMap["IS_UNDEAD"];
  547. const JsonNode config(ResourceID("config/artifacts.json"));
  548. BOOST_FOREACH(const JsonNode &artifact, config["artifacts"].Vector())
  549. {
  550. auto ga = artifacts[artifact["id"].Float()].get();
  551. BOOST_FOREACH (auto b, artifact["bonuses"].Vector())
  552. {
  553. ga->addNewBonus(ParseBonus (b));
  554. }
  555. if(artifact["type"].String() == "Creature")
  556. makeItCreatureArt(ga->id);
  557. else if(artifact["type"].String() == "Commander")
  558. makeItCommanderArt(ga->id);
  559. }
  560. // //Attack bonus artifacts (Weapons)
  561. // ART_PRIM_SKILL(7,0,+2); //Centaur Axe
  562. // ART_PRIM_SKILL(8,0,+3); //Blackshard of the Dead Knight
  563. // ART_PRIM_SKILL(9,0,+4); //Greater Gnoll's Flail
  564. // ART_PRIM_SKILL(10,0,+5); //Ogre's Club of Havoc
  565. // ART_PRIM_SKILL(11,0,+6); //Sword of Hellfire
  566. // ART_PRIM_SKILL(12,0,+12); //Titan's Gladius
  567. // ART_PRIM_SKILL(12,1,-3); //Titan's Gladius
  568. //
  569. // //Defense bonus artifacts (Shields)
  570. // ART_PRIM_SKILL(13,1,+2); //Shield of the Dwarven Lords
  571. // ART_PRIM_SKILL(14,1,+3); //Shield of the Yawning Dead
  572. // ART_PRIM_SKILL(15,1,+4); //Buckler of the Gnoll King
  573. // ART_PRIM_SKILL(16,1,+5); //Targ of the Rampaging Ogre
  574. // ART_PRIM_SKILL(17,1,+6); //Shield of the Damned
  575. // ART_PRIM_SKILL(18,1,+12); //Sentinel's Shield
  576. // ART_PRIM_SKILL(18,0,-3); //Sentinel's Shield
  577. //
  578. // //Knowledge bonus artifacts (Helmets)
  579. // ART_PRIM_SKILL(19,3,+1); //Helm of the Alabaster Unicorn
  580. // ART_PRIM_SKILL(20,3,+2); //Skull Helmet
  581. // ART_PRIM_SKILL(21,3,+3); //Helm of Chaos
  582. // ART_PRIM_SKILL(22,3,+4); //Crown of the Supreme Magi
  583. // ART_PRIM_SKILL(23,3,+5); //Hellstorm Helmet
  584. // ART_PRIM_SKILL(24,3,+10); //Thunder Helmet
  585. // ART_PRIM_SKILL(24,2,-2); //Thunder Helmet
  586. //
  587. // //Spell power bonus artifacts (Armours)
  588. // ART_PRIM_SKILL(25,2,+1); //Breastplate of Petrified Wood
  589. // ART_PRIM_SKILL(26,2,+2); //Rib Cage
  590. // ART_PRIM_SKILL(27,2,+3); //Scales of the Greater Basilisk
  591. // ART_PRIM_SKILL(28,2,+4); //Tunic of the Cyclops King
  592. // ART_PRIM_SKILL(29,2,+5); //Breastplate of Brimstone
  593. // ART_PRIM_SKILL(30,2,+10); //Titan's Cuirass
  594. // ART_PRIM_SKILL(30,3,-2); //Titan's Cuirass
  595. //
  596. // //All primary skills (various)
  597. // ART_ALL_PRIM_SKILLS(31,+1); //Armor of Wonder
  598. // ART_ALL_PRIM_SKILLS(32,+2); //Sandals of the Saint
  599. // ART_ALL_PRIM_SKILLS(33,+3); //Celestial Necklace of Bliss
  600. // ART_ALL_PRIM_SKILLS(34,+4); //Lion's Shield of Courage
  601. // ART_ALL_PRIM_SKILLS(35,+5); //Sword of Judgement
  602. // ART_ALL_PRIM_SKILLS(36,+6); //Helm of Heavenly Enlightenment
  603. //
  604. // //Attack and Defense (various)
  605. // ART_ATTACK_AND_DEFENSE(37,+1); //Quiet Eye of the Dragon
  606. // ART_ATTACK_AND_DEFENSE(38,+2); //Red Dragon Flame Tongue
  607. // ART_ATTACK_AND_DEFENSE(39,+3); //Dragon Scale Shield
  608. // ART_ATTACK_AND_DEFENSE(40,+4); //Dragon Scale Armor
  609. //
  610. // //Spell power and Knowledge (various)
  611. // ART_POWER_AND_KNOWLEDGE(41,+1); //Dragonbone Greaves
  612. // ART_POWER_AND_KNOWLEDGE(42,+2); //Dragon Wing Tabard
  613. // ART_POWER_AND_KNOWLEDGE(43,+3); //Necklace of Dragonteeth
  614. // ART_POWER_AND_KNOWLEDGE(44,+4); //Crown of Dragontooth
  615. //
  616. // //Luck and morale
  617. // ART_MORALE(45,+1); //Still Eye of the Dragon
  618. // ART_LUCK(45,+1); //Still Eye of the Dragon
  619. // ART_LUCK(46,+1); //Clover of Fortune
  620. // ART_LUCK(47,+1); //Cards of Prophecy
  621. // ART_LUCK(48,+1); //Ladybird of Luck
  622. // ART_MORALE(49,+1); //Badge of Courage -> +1 morale
  623. // giveArtBonus(49,Bonus::MIND_IMMUNITY,0); //immunity to hostile mind spells:
  624. // ART_MORALE(50,+1); //Crest of Valor
  625. // ART_MORALE(51,+1); //Glyph of Gallantry
  626. //
  627. // giveArtBonus(52,Bonus::SIGHT_RADIOUS,+1);//Speculum
  628. // giveArtBonus(53,Bonus::SIGHT_RADIOUS,+1);//Spyglass
  629. //
  630. // //necromancy bonus
  631. // giveArtBonus(54,Bonus::SECONDARY_SKILL_PREMY,+5, CGHeroInstance::NECROMANCY, Bonus::ADDITIVE_VALUE);//Amulet of the Undertaker
  632. // giveArtBonus(55,Bonus::SECONDARY_SKILL_PREMY,+10, CGHeroInstance::NECROMANCY, Bonus::ADDITIVE_VALUE);//Vampire's Cowl
  633. // giveArtBonus(56,Bonus::SECONDARY_SKILL_PREMY,+15, CGHeroInstance::NECROMANCY, Bonus::ADDITIVE_VALUE);//Dead Man's Boots
  634. //
  635. // giveArtBonus(57,Bonus::MAGIC_RESISTANCE,+5, 0);//Garniture of Interference
  636. // giveArtBonus(58,Bonus::MAGIC_RESISTANCE,+10, 0);//Surcoat of Counterpoise
  637. // giveArtBonus(59,Bonus::MAGIC_RESISTANCE,+15, 0);//Boots of Polarity
  638. //
  639. // //archery bonus
  640. // giveArtBonus(60,Bonus::SECONDARY_SKILL_PREMY,+5, CGHeroInstance::ARCHERY, Bonus::ADDITIVE_VALUE);//Bow of Elven Cherrywood
  641. // giveArtBonus(61,Bonus::SECONDARY_SKILL_PREMY,+10,CGHeroInstance::ARCHERY, Bonus::ADDITIVE_VALUE);//Bowstring of the Unicorn's Mane
  642. // giveArtBonus(62,Bonus::SECONDARY_SKILL_PREMY,+15,CGHeroInstance::ARCHERY, Bonus::ADDITIVE_VALUE);//Angel Feather Arrows
  643. //
  644. // //eagle eye bonus
  645. // giveArtBonus(63,Bonus::SECONDARY_SKILL_PREMY,+5, CGHeroInstance::EAGLE_EYE, Bonus::ADDITIVE_VALUE);//Bird of Perception
  646. // giveArtBonus(64,Bonus::SECONDARY_SKILL_PREMY,+10, CGHeroInstance::EAGLE_EYE, Bonus::ADDITIVE_VALUE);//Stoic Watchman
  647. // giveArtBonus(65,Bonus::SECONDARY_SKILL_PREMY,+15, CGHeroInstance::EAGLE_EYE, Bonus::ADDITIVE_VALUE);//Emblem of Cognizance
  648. //
  649. // //reducing cost of surrendering
  650. // giveArtBonus(66,Bonus::SURRENDER_DISCOUNT,+10);//Statesman's Medal
  651. // giveArtBonus(67,Bonus::SURRENDER_DISCOUNT,+10);//Diplomat's Ring
  652. // giveArtBonus(68,Bonus::SURRENDER_DISCOUNT,+10);//Ambassador's Sash
  653. //
  654. // giveArtBonus(69,Bonus::STACKS_SPEED,+1);//Ring of the Wayfarer
  655. //
  656. // giveArtBonus(70,Bonus::LAND_MOVEMENT,+300);//Equestrian's Gloves
  657. // giveArtBonus(71,Bonus::SEA_MOVEMENT,+1000);//Necklace of Ocean Guidance
  658. // giveArtBonus(72,Bonus::FLYING_MOVEMENT, 0, 1);//Angel Wings
  659. //
  660. // giveArtBonus(73,Bonus::MANA_REGENERATION,+1);//Charm of Mana
  661. // giveArtBonus(74,Bonus::MANA_REGENERATION,+2);//Talisman of Mana
  662. // giveArtBonus(75,Bonus::MANA_REGENERATION,+3);//Mystic Orb of Mana
  663. //
  664. // giveArtBonus(76,Bonus::SPELL_DURATION,+1);//Collar of Conjuring
  665. // giveArtBonus(77,Bonus::SPELL_DURATION,+2);//Ring of Conjuring
  666. // giveArtBonus(78,Bonus::SPELL_DURATION,+3);//Cape of Conjuring
  667. //
  668. // giveArtBonus(79,Bonus::AIR_SPELL_DMG_PREMY,+50);//Orb of the Firmament
  669. // giveArtBonus(80,Bonus::EARTH_SPELL_DMG_PREMY,+50);//Orb of Silt
  670. // giveArtBonus(81,Bonus::FIRE_SPELL_DMG_PREMY,+50);//Orb of Tempestuous Fire
  671. // giveArtBonus(82,Bonus::WATER_SPELL_DMG_PREMY,+50);//Orb of Driving Rain
  672. //
  673. // giveArtBonus(83,createBonus(Bonus::BLOCK_MAGIC_ABOVE, 2, -1, Bonus::INDEPENDENT_MIN)->addPropagator(battleWidePropagator));//Recanter's Cloak
  674. // giveArtBonus(84,Bonus::BLOCK_MORALE,0);//Spirit of Oppression
  675. // giveArtBonus(85,Bonus::BLOCK_LUCK,0);//Hourglass of the Evil Hour
  676. //
  677. // giveArtBonus(86,Bonus::FIRE_SPELLS,0);//Tome of Fire Magic
  678. // giveArtBonus(87,Bonus::AIR_SPELLS,0);//Tome of Air Magic
  679. // giveArtBonus(88,Bonus::WATER_SPELLS,0);//Tome of Water Magic
  680. // giveArtBonus(89,Bonus::EARTH_SPELLS,0);//Tome of Earth Magic
  681. //
  682. // giveArtBonus(90,Bonus::WATER_WALKING, 0, 1);//Boots of Levitation
  683. // giveArtBonus(91,Bonus::NO_DISTANCE_PENALTY,0, 0, 0, shooterOnlyLimiter);//Golden Bow
  684. // giveArtBonus(91,Bonus::NO_WALL_PENALTY, 0, 0, 0, shooterOnlyLimiter);
  685. // giveArtBonus(92,Bonus::SPELL_IMMUNITY,0,35);//Sphere of Permanence
  686. // giveArtBonus(93,Bonus::NEGATE_ALL_NATURAL_IMMUNITIES,0);//Orb of Vulnerability
  687. //
  688. // giveArtBonus(94,Bonus::STACK_HEALTH,+1);//Ring of Vitality
  689. // giveArtBonus(95,Bonus::STACK_HEALTH,+1);//Ring of Life
  690. // giveArtBonus(96,Bonus::STACK_HEALTH,+2);//Vial of Lifeblood
  691. //
  692. // giveArtBonus(97,Bonus::STACKS_SPEED,+1);//Necklace of Swiftness
  693. // giveArtBonus(98,Bonus::LAND_MOVEMENT,+600);//Boots of Speed
  694. // giveArtBonus(99,Bonus::STACKS_SPEED,+2);//Cape of Velocity
  695. //
  696. // giveArtBonus(100,Bonus::SPELL_IMMUNITY,0,Spells::BERSERK);//Pendant of Dispassion
  697. // giveArtBonus(101,Bonus::SPELL_IMMUNITY,0,Spells::BLIND);//Pendant of Second Sight
  698. // giveArtBonus(102,Bonus::SPELL_IMMUNITY,0,Spells::CURSE);//Pendant of Holiness
  699. // giveArtBonus(103,Bonus::SPELL_IMMUNITY,0,Spells::DEATH_RIPPLE);//Pendant of Life
  700. // giveArtBonus(104,Bonus::SPELL_IMMUNITY,0,Spells::DESTROY_UNDEAD, 1, isUndeadLimiter);//Pendant of Death does not display info for living stacks
  701. // giveArtBonus(105,Bonus::SPELL_IMMUNITY,0,Spells::HYPNOTIZE);//Pendant of Free Will
  702. // giveArtBonus(106,Bonus::SPELL_IMMUNITY,0,Spells::LIGHTNING_BOLT);//Pendant of Negativity
  703. // giveArtBonus(106,Bonus::SPELL_IMMUNITY,0,Spells::CHAIN_LIGHTNING);//Pendant of Negativity
  704. // giveArtBonus(107,Bonus::SPELL_IMMUNITY,0,Spells::FORGETFULNESS);//Pendant of Total Recall
  705. // giveArtBonus(108,Bonus::MORALE,+3);//Pendant of Courage
  706. // giveArtBonus(108,Bonus::LUCK,+3);//Pendant of Courage
  707. //
  708. // giveArtBonus(109,Bonus::GENERATE_RESOURCE,+1,4); //Everflowing Crystal Cloak
  709. // giveArtBonus(110,Bonus::GENERATE_RESOURCE,+1,5); //Ring of Infinite Gems
  710. // giveArtBonus(111,Bonus::GENERATE_RESOURCE,+1,1); //Everpouring Vial of Mercury
  711. // giveArtBonus(112,Bonus::GENERATE_RESOURCE,+1,2); //Inexhaustible Cart of Ore
  712. // giveArtBonus(113,Bonus::GENERATE_RESOURCE,+1,3); //Eversmoking Ring of Sulfur
  713. // giveArtBonus(114,Bonus::GENERATE_RESOURCE,+1,0); //Inexhaustible Cart of Lumber
  714. // giveArtBonus(115,Bonus::GENERATE_RESOURCE,+1000, Res::GOLD); //Endless Sack of Gold
  715. // giveArtBonus(116,Bonus::GENERATE_RESOURCE,+750, Res::GOLD); //Endless Bag of Gold
  716. // giveArtBonus(117,Bonus::GENERATE_RESOURCE,+500, Res::GOLD); //Endless Purse of Gold
  717. //
  718. //
  719. // //Town will receive bonus if hero is visiting town or stays in its garrison.
  720. // giveArtBonus(118,Bonus::CREATURE_GROWTH,+5,1, visitedTownPropagator); //Legs of Legion
  721. // giveArtBonus(119,Bonus::CREATURE_GROWTH,+4,2, visitedTownPropagator); //Loins of Legion
  722. // giveArtBonus(120,Bonus::CREATURE_GROWTH,+3,3, visitedTownPropagator); //Torso of Legion
  723. // giveArtBonus(121,Bonus::CREATURE_GROWTH,+2,4, visitedTownPropagator); //Arms of Legion
  724. // giveArtBonus(122,Bonus::CREATURE_GROWTH,+1,5, visitedTownPropagator); //Head of Legion
  725. //
  726. // //Sea Captain's Hat
  727. // giveArtBonus(123,Bonus::WHIRLPOOL_PROTECTION,0);
  728. // giveArtBonus(123,Bonus::SEA_MOVEMENT,+500);
  729. // giveArtBonus(123,Bonus::SPELL,3,0, Bonus::INDEPENDENT_MAX);
  730. // giveArtBonus(123,Bonus::SPELL,3,1, Bonus::INDEPENDENT_MAX);
  731. //
  732. // giveArtBonus(124, Bonus::SPELLS_OF_LEVEL,3,1); //Spellbinder's Hat
  733. // giveArtBonus(125, Bonus::BATTLE_NO_FLEEING,0, 0, battleWidePropagator); //Shackles of War
  734. // giveArtBonus(126, Bonus::BLOCK_ALL_MAGIC, 0, -1, battleWidePropagator);//Orb of Inhibition
  735. //
  736. // //vial of dragon blood
  737. // giveArtBonus(127, Bonus::PRIMARY_SKILL, +5, PrimarySkill::ATTACK, Bonus::BASE_NUMBER, dragonNatureLimiter);
  738. // giveArtBonus(127, Bonus::PRIMARY_SKILL, +5, PrimarySkill::DEFENSE, Bonus::BASE_NUMBER, dragonNatureLimiter);
  739. //
  740. // //Armageddon's Blade
  741. // giveArtBonus(128, Bonus::SPELL, 3, 26, Bonus::INDEPENDENT_MAX);
  742. // giveArtBonus(128, Bonus::SPELL_IMMUNITY,0, 26);
  743. // ART_ATTACK_AND_DEFENSE(128, +3);
  744. // ART_PRIM_SKILL(128, 2, +3);
  745. // ART_PRIM_SKILL(128, 3, +6);
  746. //
  747. // //Angelic Alliance
  748. // giveArtBonus(129, Bonus::NONEVIL_ALIGNMENT_MIX, 0);
  749. // giveArtBonus(129, Bonus::OPENING_BATTLE_SPELL, 10, 48); // Prayer
  750. //
  751. // //Cloak of the Undead King
  752. // giveArtBonus(130, Bonus::IMPROVED_NECROMANCY, 0);
  753. //
  754. // //Elixir of Life
  755. // giveArtBonus(131, Bonus::STACK_HEALTH, +25, -1, Bonus::PERCENT_TO_BASE);
  756. // giveArtBonus(131, Bonus::HP_REGENERATION, +50);
  757. //
  758. // //Armor of the Damned
  759. // giveArtBonus(132, Bonus::OPENING_BATTLE_SPELL, 50, 54); // Slow
  760. // giveArtBonus(132, Bonus::OPENING_BATTLE_SPELL, 50, 47); // Disrupting Ray
  761. // giveArtBonus(132, Bonus::OPENING_BATTLE_SPELL, 50, 45); // Weakness
  762. // giveArtBonus(132, Bonus::OPENING_BATTLE_SPELL, 50, 52); // Misfortune
  763. //
  764. // // Statue of Legion - gives only 50% growth
  765. // giveArtBonus(133, Bonus::CREATURE_GROWTH_PERCENT, 50, -1, make_shared<CPropagatorNodeType>(CBonusSystemNode::PLAYER));
  766. //
  767. // //Power of the Dragon Father
  768. // giveArtBonus(134, Bonus::LEVEL_SPELL_IMMUNITY, 4, -1, Bonus::INDEPENDENT_MAX);
  769. //
  770. // //Titan's Thunder
  771. // giveArtBonus(135, Bonus::SPELL, 3, 57);
  772. //
  773. // //Admiral's Hat
  774. // giveArtBonus(136, Bonus::FREE_SHIP_BOARDING, 0);
  775. //
  776. // //Bow of the Sharpshooter
  777. // giveArtBonus(137, Bonus::NO_DISTANCE_PENALTY, 0, 0, 0, shooterOnlyLimiter);
  778. // giveArtBonus(137, Bonus::NO_WALL_PENALTY, 0, 0, 0, shooterOnlyLimiter);
  779. // giveArtBonus(137, Bonus::FREE_SHOOTING, 0, 0, 0, shooterOnlyLimiter);
  780. //
  781. // //Wizard's Well
  782. // giveArtBonus(138, Bonus::FULL_MANA_REGENERATION, 0);
  783. //
  784. // //Ring of the Magi
  785. // giveArtBonus(139, Bonus::SPELL_DURATION, +50);
  786. //
  787. // //Cornucopia
  788. // giveArtBonus(140, Bonus::GENERATE_RESOURCE, +4, Res::MERCURY);
  789. // giveArtBonus(140, Bonus::GENERATE_RESOURCE, +4, Res::SULFUR);
  790. // giveArtBonus(140, Bonus::GENERATE_RESOURCE, +4, Res::CRYSTAL);
  791. // giveArtBonus(140, Bonus::GENERATE_RESOURCE, +4, Res::GEMS);
  792. //
  793. //
  794. // //Stack artifact test
  795. // if (VLC->modh->modules.STACK_ARTIFACT)
  796. // {
  797. // makeItCreatureArt(141);
  798. // makeItCreatureArt(142);
  799. // makeItCreatureArt(143);
  800. // makeItCreatureArt(156);
  801. // //Magic Wand
  802. // giveArtBonus(141, Bonus::CASTS, 10);
  803. // giveArtBonus(141, Bonus::SPELLCASTER, 0, Spells::IMPLOSION);
  804. // giveArtBonus(141, Bonus::SPELLCASTER, 0, Spells::FIREBALL);
  805. // giveArtBonus(141, Bonus::RANDOM_SPELLCASTER, 0);
  806. // giveArtBonus(141, Bonus::DAEMON_SUMMONING, 10, 63); //rise vampire lords
  807. // giveArtBonus(141, Bonus::ENCHANTER, 0, Spells::LIGHTNING_BOLT, NULL, 2);
  808. // giveArtBonus(141, Bonus::REBIRTH, 1, 1);
  809. // giveArtBonus(141, Bonus::MANA_DRAIN, 10);
  810. // giveArtBonus(141, Bonus::HEALER, 25);
  811. // artifacts[141].get()->setDescription ("Casts Implosion / Fireball, random Genie spell, summons Vampire Lords from corpses, casts Lighthning Bolt every 2 turns, rebirths at least one creature, drains enemy mana and heals");
  812. // //Tower Arrow
  813. // giveArtBonus(142, Bonus::NO_DISTANCE_PENALTY, 0);
  814. // giveArtBonus(142, Bonus::ADDITIONAL_ATTACK, 2);
  815. // giveArtBonus(142, Bonus::SPELL_LIKE_ATTACK, 1, Spells::INFERNO);
  816. // giveArtBonus(142, Bonus::CATAPULT, 0);
  817. // giveArtBonus(142, Bonus::ACID_BREATH, 20);
  818. // giveArtBonus(142, Bonus::SHOTS, 200, 0, Bonus::PERCENT_TO_BASE);
  819. // giveArtBonus(142, Bonus::SPELL_BEFORE_ATTACK, 50, Spells::AGE, NULL, 1);
  820. // giveArtBonus(142, Bonus::SPELL_AFTER_ATTACK, 50, Spells::BERSERK, NULL, 1);
  821. // giveArtBonus(142, Bonus::SPELL_AFTER_ATTACK, 50, Spells::POISON, NULL, 1);
  822. // giveArtBonus(142, Bonus::SPELL_AFTER_ATTACK, 50, Spells::DISRUPTING_RAY, NULL, 1);
  823. // artifacts[142].get()->setDescription ("Triple shots, triple attack, casts various spells during attack, attacks have range of Inferno, no distance penalty, catapult");
  824. // //Monster's Power
  825. // giveArtBonus(143, Bonus::STACK_HEALTH, +100, -1, Bonus::PERCENT_TO_BASE);
  826. // giveArtBonus(143, Bonus::CREATURE_DAMAGE, +100, 2, Bonus::PERCENT_TO_ALL);
  827. // giveArtBonus(143, Bonus::HP_REGENERATION, 50);
  828. // giveArtBonus(143, Bonus::NO_RETALIATION, 0);
  829. // giveArtBonus(143, Bonus::RETURN_AFTER_STRIKE, 0);
  830. // giveArtBonus(143, Bonus::ATTACKS_ALL_ADJACENT, 0);
  831. // giveArtBonus(143, Bonus::SPELL_RESISTANCE_AURA, 100);
  832. // giveArtBonus(143, Bonus::DIRECT_DAMAGE_IMMUNITY, 0);
  833. // artifacts[143].get()->setDescription ("Double health, double max damage, hp regeneration, can't retaliate, return after strike, attack all around, 100% spell reisstance aura, immune to direct damage spells");
  834. // //Warlord's banner
  835. // giveArtBonus(156, Bonus::STACK_HEALTH, +2);
  836. // artifacts[156].get()->setDescription ("+2 stack HP");
  837. //
  838. // }
  839. // if (VLC->modh->modules.COMMANDERS)
  840. // {
  841. // for (int i = 146; i <= 155; ++i)
  842. // {
  843. // makeItCommanderArt (i);
  844. // }
  845. // ART_PRIM_SKILL (146, 0, +6); //Axe of Smashing
  846. // giveArtBonus(147, Bonus::STACK_HEALTH, +12, -1, Bonus::PERCENT_TO_ALL); //Mithril Mail
  847. // giveArtBonus(148, Bonus::CREATURE_DAMAGE, +12, 0, Bonus::PERCENT_TO_ALL); //Sword of Sharpness
  848. // giveArtBonus(150, Bonus::CASTS, 1); //Pendant of Sorcery
  849. // giveArtBonus(151, Bonus::STACKS_SPEED, 1); //Boots of haste
  850. // ART_PRIM_SKILL (154, 0, +6); //Hardened Shield
  851. // }
  852. //
  853. // JsonNode cfg;
  854. // JsonNode artifs;
  855. // BOOST_FOREACH(auto art, artifacts)
  856. // {
  857. // JsonNode jn;
  858. // jn["id"].Float() = art->id;
  859. // BOOST_FOREACH (auto b, art->getBonusList())
  860. // {
  861. // JsonNode bn;
  862. // UnparseBonus(bn, b);
  863. // jn["bonuses"].Vector().push_back(bn);
  864. // }
  865. //
  866. // if(art->id < 140 || art-> id == 144 || art->id == 145 || art->id > 156)
  867. // jn["type"].String() = "Hero";
  868. // else if((art->id >= 141 && art->id < 144) || art->id == 156)
  869. // jn["type"].String() = "Creature";
  870. // else if(art-> id >= 146 && art->id <= 155)
  871. // jn["type"].String() = "Commander";
  872. //
  873. // artifs.Vector().push_back(jn);
  874. // }
  875. // cfg["artifacts"] = artifs;
  876. // std::ofstream artconfigOutput("config/artifacts.json");
  877. // JsonWriter(artconfigOutput, cfg);
  878. }
  879. void CArtHandler::clear()
  880. {
  881. BOOST_FOREACH(CArtifact *art, artifacts)
  882. delete art;
  883. artifacts.clear();
  884. clearHlpLists();
  885. }
  886. // /**
  887. // * Locally equips an artifact to a hero's worn slots. Unequips an already present artifact.
  888. // * Does not test if the operation is legal.
  889. // * @param artifWorn A hero's set of worn artifacts.
  890. // * @param bonuses Optional list of bonuses to update.
  891. // */
  892. // void CArtHandler::equipArtifact( std::map<ui16, const CArtifact*> &artifWorn, ui16 slotID, const CArtifact* art ) const
  893. // {
  894. // unequipArtifact(artifWorn, slotID);
  895. //
  896. // if (art) //false when artifact is NULL -> slot set to empty
  897. // {
  898. // const CArtifact &artifact = *art;
  899. //
  900. // // Add artifact.
  901. // artifWorn[slotID] = art;
  902. //
  903. // // Add locks, in reverse order of being removed.
  904. // if (artifact.constituents != NULL)
  905. // {
  906. // bool destConsumed = false; // Determines which constituent that will be counted for together with the artifact.
  907. //
  908. // BOOST_FOREACH(ui32 constituentID, *artifact.constituents)
  909. // {
  910. // const CArtifact &constituent = *artifacts[constituentID];
  911. //
  912. // if (!destConsumed && vstd::contains(constituent.possibleSlots, slotID))
  913. // {
  914. // destConsumed = true;
  915. // }
  916. // else
  917. // {
  918. // BOOST_FOREACH(ui16 slot, constituent.possibleSlots)
  919. // {
  920. // if (!vstd::contains(artifWorn, slot))
  921. // {
  922. // artifWorn[slot] = VLC->arth->artifacts[145]; //lock
  923. // break;
  924. // }
  925. // }
  926. // }
  927. // }
  928. // }
  929. // }
  930. // }
  931. //
  932. // /**
  933. // * Locally unequips an artifact from a hero's worn slots.
  934. // * Does not test if the operation is legal.
  935. // * @param artifWorn A hero's set of worn artifacts.
  936. // * @param bonuses Optional list of bonuses to update.
  937. // */
  938. // void CArtHandler::unequipArtifact(std::map<ui16, const CArtifact*> &artifWorn, ui16 slotID) const
  939. // {
  940. // if (!vstd::contains(artifWorn, slotID))
  941. // return;
  942. //
  943. // const CArtifact &artifact = *artifWorn[slotID];
  944. //
  945. // // Remove artifact, if it's not already removed.
  946. // artifWorn.erase(slotID);
  947. //
  948. // // Remove locks, in reverse order of being added.
  949. // if (artifact.constituents != NULL)
  950. // {
  951. // bool destConsumed = false;
  952. //
  953. // BOOST_FOREACH(ui32 constituentID, *artifact.constituents)
  954. // {
  955. // const CArtifact &constituent = *artifacts[constituentID];
  956. //
  957. // if (!destConsumed && vstd::contains(constituent.possibleSlots, slotID))
  958. // {
  959. // destConsumed = true;
  960. // }
  961. // else
  962. // {
  963. // BOOST_REVERSE_FOREACH(ui16 slot, constituent.possibleSlots)
  964. // {
  965. // if (vstd::contains(artifWorn, slot) && artifWorn[slot]->id == 145)
  966. // {
  967. // artifWorn.erase(slot);
  968. // break;
  969. // }
  970. // }
  971. // }
  972. // }
  973. // }
  974. // }
  975. void CArtHandler::clearHlpLists()
  976. {
  977. treasures.clear();
  978. minors.clear();
  979. majors.clear();
  980. relics.clear();
  981. }
  982. void CArtHandler::initAllowedArtifactsList(const std::vector<ui8> &allowed)
  983. {
  984. allowedArtifacts.clear();
  985. clearHlpLists();
  986. for (int i=0; i<144; ++i) //yes, 144
  987. {
  988. if (allowed[i])
  989. allowedArtifacts.push_back(artifacts[i]);
  990. }
  991. if (VLC->modh->modules.COMMANDERS) //allow all commander artifacts for testing
  992. {
  993. for (int i = 146; i <= 155; ++i)
  994. {
  995. allowedArtifacts.push_back(artifacts[i]);
  996. }
  997. }
  998. }
  999. CArtifactInstance::CArtifactInstance()
  1000. {
  1001. init();
  1002. }
  1003. CArtifactInstance::CArtifactInstance( CArtifact *Art)
  1004. {
  1005. init();
  1006. setType(Art);
  1007. }
  1008. // CArtifactInstance::CArtifactInstance(int aid)
  1009. // {
  1010. // init();
  1011. // setType(VLC->arth->artifacts[aid]);
  1012. // }
  1013. void CArtifactInstance::setType( CArtifact *Art )
  1014. {
  1015. artType = Art;
  1016. attachTo(Art);
  1017. }
  1018. std::string CArtifactInstance::nodeName() const
  1019. {
  1020. return "Artifact instance of " + (artType ? artType->Name() : std::string("uninitialized")) + " type";
  1021. }
  1022. CArtifactInstance * CArtifactInstance::createScroll( const CSpell *s)
  1023. {
  1024. CArtifactInstance *ret = new CArtifactInstance(VLC->arth->artifacts[1]);
  1025. Bonus *b = new Bonus(Bonus::PERMANENT, Bonus::SPELL, Bonus::ARTIFACT_INSTANCE, -1, 1, s->id);
  1026. ret->addNewBonus(b);
  1027. return ret;
  1028. }
  1029. void CArtifactInstance::init()
  1030. {
  1031. id = -1;
  1032. setNodeType(ARTIFACT_INSTANCE);
  1033. }
  1034. int CArtifactInstance::firstAvailableSlot(const CArtifactSet *h) const
  1035. {
  1036. BOOST_FOREACH(ui16 slot, artType->possibleSlots[h->bearerType()])
  1037. {
  1038. if(canBePutAt(h, slot)) //if(artType->fitsAt(h->artifWorn, slot))
  1039. {
  1040. //we've found a free suitable slot.
  1041. return slot;
  1042. }
  1043. }
  1044. //if haven't find proper slot, use backpack
  1045. return firstBackpackSlot(h);
  1046. }
  1047. int CArtifactInstance::firstBackpackSlot(const CArtifactSet *h) const
  1048. {
  1049. if(!artType->isBig()) //discard big artifact
  1050. return GameConstants::BACKPACK_START + h->artifactsInBackpack.size();
  1051. return -1;
  1052. }
  1053. bool CArtifactInstance::canBePutAt(const ArtifactLocation al, bool assumeDestRemoved /*= false*/) const
  1054. {
  1055. return canBePutAt(al.getHolderArtSet(), al.slot, assumeDestRemoved);
  1056. }
  1057. bool CArtifactInstance::canBePutAt(const CArtifactSet *artSet, int slot, bool assumeDestRemoved /*= false*/) const
  1058. {
  1059. if(slot >= GameConstants::BACKPACK_START)
  1060. {
  1061. if(artType->isBig())
  1062. return false;
  1063. //TODO backpack limit
  1064. return true;
  1065. }
  1066. auto possibleSlots = artType->possibleSlots.find(artSet->bearerType());
  1067. if(possibleSlots == artType->possibleSlots.end())
  1068. {
  1069. tlog3 << "Warning: arrtifact " << artType->Name() << " doesn't have defined allowed slots for bearer of type "
  1070. << artSet->bearerType() << std::endl;
  1071. return false;
  1072. }
  1073. if(!vstd::contains(possibleSlots->second, slot))
  1074. return false;
  1075. return artSet->isPositionFree(slot, assumeDestRemoved);
  1076. }
  1077. void CArtifactInstance::putAt(ArtifactLocation al)
  1078. {
  1079. assert(canBePutAt(al));
  1080. al.getHolderArtSet()->setNewArtSlot(al.slot, this, false);
  1081. if(al.slot < GameConstants::BACKPACK_START)
  1082. al.getHolderNode()->attachTo(this);
  1083. }
  1084. void CArtifactInstance::removeFrom(ArtifactLocation al)
  1085. {
  1086. assert(al.getHolderArtSet()->getArt(al.slot) == this);
  1087. al.getHolderArtSet()->eraseArtSlot(al.slot);
  1088. if(al.slot < GameConstants::BACKPACK_START)
  1089. al.getHolderNode()->detachFrom(this);
  1090. //TODO delete me?
  1091. }
  1092. bool CArtifactInstance::canBeDisassembled() const
  1093. {
  1094. return artType->constituents && artType->constituentOf->size();
  1095. }
  1096. std::vector<const CArtifact *> CArtifactInstance::assemblyPossibilities(const CArtifactSet *h) const
  1097. {
  1098. std::vector<const CArtifact *> ret;
  1099. if(!artType->constituentOf //not a part of combined artifact
  1100. || artType->constituents) //combined artifact already: no combining of combined artifacts... for now.
  1101. return ret;
  1102. BOOST_FOREACH(ui32 possibleCombinedArt, *artType->constituentOf)
  1103. {
  1104. const CArtifact * const artifact = VLC->arth->artifacts[possibleCombinedArt];
  1105. assert(artifact->constituents);
  1106. bool possible = true;
  1107. BOOST_FOREACH(ui32 constituentID, *artifact->constituents) //check if all constituents are available
  1108. {
  1109. if(!h->hasArt(constituentID, true)) //constituent must be equipped
  1110. {
  1111. possible = false;
  1112. break;
  1113. }
  1114. }
  1115. if(possible)
  1116. ret.push_back(artifact);
  1117. }
  1118. return ret;
  1119. }
  1120. void CArtifactInstance::move(ArtifactLocation src, ArtifactLocation dst)
  1121. {
  1122. removeFrom(src);
  1123. putAt(dst);
  1124. }
  1125. CArtifactInstance * CArtifactInstance::createNewArtifactInstance(CArtifact *Art)
  1126. {
  1127. if(!Art->constituents)
  1128. {
  1129. auto ret = new CArtifactInstance(Art);
  1130. if (dynamic_cast<CGrowingArtifact *>(Art))
  1131. {
  1132. Bonus * bonus = new Bonus;
  1133. bonus->type = Bonus::LEVEL_COUNTER;
  1134. bonus->val = 0;
  1135. ret->addNewBonus (bonus);
  1136. }
  1137. return ret;
  1138. }
  1139. else
  1140. {
  1141. CCombinedArtifactInstance * ret = new CCombinedArtifactInstance(Art);
  1142. ret->createConstituents();
  1143. return ret;
  1144. }
  1145. }
  1146. CArtifactInstance * CArtifactInstance::createNewArtifactInstance(int aid)
  1147. {
  1148. return createNewArtifactInstance(VLC->arth->artifacts[aid]);
  1149. }
  1150. void CArtifactInstance::deserializationFix()
  1151. {
  1152. setType(artType);
  1153. }
  1154. int CArtifactInstance::getGivenSpellID() const
  1155. {
  1156. const Bonus * b = getBonusLocalFirst(Selector::type(Bonus::SPELL));
  1157. if(!b)
  1158. {
  1159. tlog3 << "Warning: " << nodeName() << " doesn't bear any spell!\n";
  1160. return -1;
  1161. }
  1162. return b->subtype;
  1163. }
  1164. bool CArtifactInstance::isPart(const CArtifactInstance *supposedPart) const
  1165. {
  1166. return supposedPart == this;
  1167. }
  1168. bool CCombinedArtifactInstance::canBePutAt(const CArtifactSet *artSet, int slot, bool assumeDestRemoved /*= false*/) const
  1169. {
  1170. bool canMainArtifactBePlaced = CArtifactInstance::canBePutAt(artSet, slot, assumeDestRemoved);
  1171. if(!canMainArtifactBePlaced)
  1172. return false; //no is no...
  1173. if(slot >= GameConstants::BACKPACK_START)
  1174. return true; //we can always remove combined art to the backapck
  1175. assert(artType->constituents);
  1176. std::vector<ConstituentInfo> constituentsToBePlaced = constituentsInfo; //we'll remove constituents from that list, as we find a suitable slot for them
  1177. //it may be that we picked a combined artifact in hero screen (though technically it's still there) to move it
  1178. //so we remove from the list all constituents that are already present on dst hero in the form of locks
  1179. BOOST_FOREACH(const ConstituentInfo &constituent, constituentsInfo)
  1180. {
  1181. if(constituent.art == artSet->getArt(constituent.slot, false)) //no need to worry about locked constituent
  1182. constituentsToBePlaced -= constituent;
  1183. }
  1184. //we iterate over all active slots and check if constituents fits them
  1185. for (int i = 0; i < GameConstants::BACKPACK_START; i++)
  1186. {
  1187. for(std::vector<ConstituentInfo>::iterator art = constituentsToBePlaced.begin(); art != constituentsToBePlaced.end(); art++)
  1188. {
  1189. 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
  1190. {
  1191. constituentsToBePlaced.erase(art);
  1192. break;
  1193. }
  1194. }
  1195. }
  1196. return constituentsToBePlaced.empty();
  1197. }
  1198. bool CCombinedArtifactInstance::canBeDisassembled() const
  1199. {
  1200. return true;
  1201. }
  1202. CCombinedArtifactInstance::CCombinedArtifactInstance(CArtifact *Art)
  1203. : CArtifactInstance(Art) //TODO: seems unued, but need to be written
  1204. {
  1205. }
  1206. CCombinedArtifactInstance::CCombinedArtifactInstance()
  1207. {
  1208. }
  1209. void CCombinedArtifactInstance::createConstituents()
  1210. {
  1211. assert(artType);
  1212. assert(artType->constituents);
  1213. BOOST_FOREACH(ui32 a, *artType->constituents)
  1214. {
  1215. addAsConstituent(CArtifactInstance::createNewArtifactInstance(a), -1);
  1216. }
  1217. }
  1218. void CCombinedArtifactInstance::addAsConstituent(CArtifactInstance *art, int slot)
  1219. {
  1220. assert(vstd::contains(*artType->constituents, art->artType->id));
  1221. assert(art->getParentNodes().size() == 1 && art->getParentNodes().front() == art->artType);
  1222. constituentsInfo.push_back(ConstituentInfo(art, slot));
  1223. attachTo(art);
  1224. }
  1225. void CCombinedArtifactInstance::putAt(ArtifactLocation al)
  1226. {
  1227. if(al.slot >= GameConstants::BACKPACK_START)
  1228. {
  1229. CArtifactInstance::putAt(al);
  1230. BOOST_FOREACH(ConstituentInfo &ci, constituentsInfo)
  1231. ci.slot = -1;
  1232. }
  1233. else
  1234. {
  1235. CArtifactInstance *mainConstituent = figureMainConstituent(al); //it'll be replaced with combined artifact, not a lock
  1236. CArtifactInstance::putAt(al); //puts combined art (this)
  1237. BOOST_FOREACH(ConstituentInfo &ci, constituentsInfo)
  1238. {
  1239. if(ci.art != mainConstituent)
  1240. {
  1241. const ArtifactLocation suggestedPos(al.artHolder, ci.slot);
  1242. const bool inActiveSlot = vstd::isbetween(ci.slot, 0, GameConstants::BACKPACK_START);
  1243. const bool suggestedPosValid = ci.art->canBePutAt(suggestedPos);
  1244. int pos = -1;
  1245. if(inActiveSlot && suggestedPosValid) //there is a valid suggestion where to place lock
  1246. pos = ci.slot;
  1247. else
  1248. ci.slot = pos = ci.art->firstAvailableSlot(al.getHolderArtSet());
  1249. assert(pos < GameConstants::BACKPACK_START);
  1250. al.getHolderArtSet()->setNewArtSlot(pos, ci.art, true); //sets as lock
  1251. }
  1252. else
  1253. {
  1254. ci.slot = -1;
  1255. }
  1256. }
  1257. }
  1258. }
  1259. void CCombinedArtifactInstance::removeFrom(ArtifactLocation al)
  1260. {
  1261. if(al.slot >= GameConstants::BACKPACK_START)
  1262. {
  1263. CArtifactInstance::removeFrom(al);
  1264. }
  1265. else
  1266. {
  1267. BOOST_FOREACH(ConstituentInfo &ci, constituentsInfo)
  1268. {
  1269. if(ci.slot >= 0)
  1270. {
  1271. al.getHolderArtSet()->eraseArtSlot(ci.slot);
  1272. ci.slot = -1;
  1273. }
  1274. else
  1275. {
  1276. //main constituent
  1277. CArtifactInstance::removeFrom(al);
  1278. }
  1279. }
  1280. }
  1281. }
  1282. CArtifactInstance * CCombinedArtifactInstance::figureMainConstituent(const ArtifactLocation al)
  1283. {
  1284. CArtifactInstance *mainConstituent = NULL; //it'll be replaced with combined artifact, not a lock
  1285. BOOST_FOREACH(ConstituentInfo &ci, constituentsInfo)
  1286. if(ci.slot == al.slot)
  1287. mainConstituent = ci.art;
  1288. if(!mainConstituent)
  1289. {
  1290. BOOST_FOREACH(ConstituentInfo &ci, constituentsInfo)
  1291. {
  1292. if(vstd::contains(ci.art->artType->possibleSlots[al.getHolderArtSet()->bearerType()], al.slot))
  1293. {
  1294. mainConstituent = ci.art;
  1295. }
  1296. }
  1297. }
  1298. return mainConstituent;
  1299. }
  1300. void CCombinedArtifactInstance::deserializationFix()
  1301. {
  1302. BOOST_FOREACH(ConstituentInfo &ci, constituentsInfo)
  1303. attachTo(ci.art);
  1304. }
  1305. bool CCombinedArtifactInstance::isPart(const CArtifactInstance *supposedPart) const
  1306. {
  1307. bool me = CArtifactInstance::isPart(supposedPart);
  1308. if(me)
  1309. return true;
  1310. //check for constituents
  1311. BOOST_FOREACH(const ConstituentInfo &constituent, constituentsInfo)
  1312. if(constituent.art == supposedPart)
  1313. return true;
  1314. return false;
  1315. }
  1316. CCombinedArtifactInstance::ConstituentInfo::ConstituentInfo(CArtifactInstance *Art /*= NULL*/, ui16 Slot /*= -1*/)
  1317. {
  1318. art = Art;
  1319. slot = Slot;
  1320. }
  1321. bool CCombinedArtifactInstance::ConstituentInfo::operator==(const ConstituentInfo &rhs) const
  1322. {
  1323. return art == rhs.art && slot == rhs.slot;
  1324. }
  1325. const CArtifactInstance* CArtifactSet::getArt(ui16 pos, bool excludeLocked /*= true*/) const
  1326. {
  1327. if(const ArtSlotInfo *si = getSlot(pos))
  1328. {
  1329. if(si->artifact && (!excludeLocked || !si->locked))
  1330. return si->artifact;
  1331. }
  1332. return NULL;
  1333. }
  1334. CArtifactInstance* CArtifactSet::getArt(ui16 pos, bool excludeLocked /*= true*/)
  1335. {
  1336. return const_cast<CArtifactInstance*>((const_cast<const CArtifactSet*>(this))->getArt(pos, excludeLocked));
  1337. }
  1338. si32 CArtifactSet::getArtPos(int aid, bool onlyWorn /*= true*/) const
  1339. {
  1340. for(std::map<ui16, ArtSlotInfo>::const_iterator i = artifactsWorn.begin(); i != artifactsWorn.end(); i++)
  1341. if(i->second.artifact->artType->id == aid)
  1342. return i->first;
  1343. if(onlyWorn)
  1344. return -1;
  1345. for(int i = 0; i < artifactsInBackpack.size(); i++)
  1346. if(artifactsInBackpack[i].artifact->artType->id == aid)
  1347. return GameConstants::BACKPACK_START + i;
  1348. return -1;
  1349. }
  1350. si32 CArtifactSet::getArtPos(const CArtifactInstance *art) const
  1351. {
  1352. for(std::map<ui16, ArtSlotInfo>::const_iterator i = artifactsWorn.begin(); i != artifactsWorn.end(); i++)
  1353. if(i->second.artifact == art)
  1354. return i->first;
  1355. for(int i = 0; i < artifactsInBackpack.size(); i++)
  1356. if(artifactsInBackpack[i].artifact == art)
  1357. return GameConstants::BACKPACK_START + i;
  1358. return -1;
  1359. }
  1360. const CArtifactInstance * CArtifactSet::getArtByInstanceId( TArtifactInstanceID artInstId ) const
  1361. {
  1362. for(std::map<ui16, ArtSlotInfo>::const_iterator i = artifactsWorn.begin(); i != artifactsWorn.end(); i++)
  1363. if(i->second.artifact->id == artInstId)
  1364. return i->second.artifact;
  1365. for(int i = 0; i < artifactsInBackpack.size(); i++)
  1366. if(artifactsInBackpack[i].artifact->id == artInstId)
  1367. return artifactsInBackpack[i].artifact;
  1368. return NULL;
  1369. }
  1370. bool CArtifactSet::hasArt(ui32 aid, bool onlyWorn /*= false*/) const
  1371. {
  1372. return getArtPos(aid, onlyWorn) != -1;
  1373. }
  1374. const ArtSlotInfo * CArtifactSet::getSlot(ui16 pos) const
  1375. {
  1376. if(vstd::contains(artifactsWorn, pos))
  1377. return &artifactsWorn[pos];
  1378. if(pos >= ArtifactPosition::AFTER_LAST )
  1379. {
  1380. int backpackPos = (int)pos - GameConstants::BACKPACK_START;
  1381. if(backpackPos < 0 || backpackPos >= artifactsInBackpack.size())
  1382. return NULL;
  1383. else
  1384. return &artifactsInBackpack[backpackPos];
  1385. }
  1386. return NULL;
  1387. }
  1388. bool CArtifactSet::isPositionFree(ui16 pos, bool onlyLockCheck /*= false*/) const
  1389. {
  1390. if(const ArtSlotInfo *s = getSlot(pos))
  1391. return (onlyLockCheck || !s->artifact) && !s->locked;
  1392. return true; //no slot means not used
  1393. }
  1394. si32 CArtifactSet::getArtTypeId(ui16 pos) const
  1395. {
  1396. const CArtifactInstance * const a = getArt(pos);
  1397. if(!a)
  1398. {
  1399. tlog2 << (dynamic_cast<const CGHeroInstance*>(this))->name << " has no artifact at " << pos << " (getArtTypeId)\n";
  1400. return -1;
  1401. }
  1402. return a->artType->id;
  1403. }
  1404. CArtifactSet::~CArtifactSet()
  1405. {
  1406. }
  1407. ArtSlotInfo & CArtifactSet::retreiveNewArtSlot(ui16 slot)
  1408. {
  1409. assert(!vstd::contains(artifactsWorn, slot));
  1410. ArtSlotInfo &ret = slot < GameConstants::BACKPACK_START
  1411. ? artifactsWorn[slot]
  1412. : *artifactsInBackpack.insert(artifactsInBackpack.begin() + (slot - GameConstants::BACKPACK_START), ArtSlotInfo());
  1413. return ret;
  1414. }
  1415. void CArtifactSet::setNewArtSlot(ui16 slot, CArtifactInstance *art, bool locked)
  1416. {
  1417. ArtSlotInfo &asi = retreiveNewArtSlot(slot);
  1418. asi.artifact = art;
  1419. asi.locked = locked;
  1420. }
  1421. void CArtifactSet::eraseArtSlot(ui16 slot)
  1422. {
  1423. if(slot < GameConstants::BACKPACK_START)
  1424. {
  1425. artifactsWorn.erase(slot);
  1426. }
  1427. else
  1428. {
  1429. slot -= GameConstants::BACKPACK_START;
  1430. artifactsInBackpack.erase(artifactsInBackpack.begin() + slot);
  1431. }
  1432. }
  1433. void CArtifactSet::artDeserializationFix(CBonusSystemNode *node)
  1434. {
  1435. for(bmap<ui16, ArtSlotInfo>::iterator i = artifactsWorn.begin(); i != artifactsWorn.end(); i++)
  1436. if(i->second.artifact && !i->second.locked)
  1437. node->attachTo(i->second.artifact);
  1438. }