CArtHandler.cpp 43 KB

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