CArtHandler.cpp 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527
  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 "CCreatureSet.h"
  10. #include "NetPacks.h"
  11. extern CLodHandler *bitmaph;
  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. const std::string & CArtifact::Name() const
  24. {
  25. if(name.size())
  26. return name;
  27. else
  28. return VLC->generaltexth->artifNames[id];
  29. }
  30. const std::string & CArtifact::Description() const
  31. {
  32. if(description.size())
  33. return description;
  34. else
  35. return VLC->generaltexth->artifDescriptions[id];
  36. }
  37. bool CArtifact::isBig () const
  38. {
  39. return VLC->arth->isBigArtifact(id);
  40. }
  41. //
  42. // bool CArtifact::isModable () const
  43. // {
  44. // return (bool)dynamic_cast<const IModableArt *>(this);
  45. // }
  46. // /**
  47. // * Checks whether the artifact fits at a given slot.
  48. // * @param artifWorn A hero's set of worn artifacts.
  49. // */
  50. // bool CArtifact::fitsAt (const std::map<ui16, const CArtifact*> &artifWorn, ui16 slotID) const
  51. // {
  52. // if (!vstd::contains(possibleSlots, slotID))
  53. // return false;
  54. //
  55. // // Can't put an artifact in a locked slot.
  56. // std::map<ui16, const CArtifact*>::const_iterator it = artifWorn.find(slotID);
  57. // if (it != artifWorn.end() && it->second->id == 145)
  58. // return false;
  59. //
  60. // // Check if a combination artifact fits.
  61. // // TODO: Might want a more general algorithm?
  62. // // Assumes that misc & rings fits only in their slots, and others in only one slot and no duplicates.
  63. // if (constituents != NULL)
  64. // {
  65. // std::map<ui16, const CArtifact*> tempArtifWorn = artifWorn;
  66. // const ui16 ringSlots[] = {6, 7};
  67. // const ui16 miscSlots[] = {9, 10, 11, 12, 18};
  68. // int rings = 0;
  69. // int misc = 0;
  70. //
  71. // VLC->arth->unequipArtifact(tempArtifWorn, slotID);
  72. //
  73. // BOOST_FOREACH(ui32 constituentID, *constituents)
  74. // {
  75. // const CArtifact& constituent = *VLC->arth->artifacts[constituentID];
  76. // const int slot = constituent.possibleSlots[0];
  77. //
  78. // if (slot == 6 || slot == 7)
  79. // rings++;
  80. // else if ((slot >= 9 && slot <= 12) || slot == 18)
  81. // misc++;
  82. // else if (tempArtifWorn.find(slot) != tempArtifWorn.end())
  83. // return false;
  84. // }
  85. //
  86. // // Ensure enough ring slots are free
  87. // for (int i = 0; i < sizeof(ringSlots)/sizeof(*ringSlots); i++)
  88. // {
  89. // if (tempArtifWorn.find(ringSlots[i]) == tempArtifWorn.end() || ringSlots[i] == slotID)
  90. // rings--;
  91. // }
  92. // if (rings > 0)
  93. // return false;
  94. //
  95. // // Ensure enough misc slots are free.
  96. // for (int i = 0; i < sizeof(miscSlots)/sizeof(*miscSlots); i++)
  97. // {
  98. // if (tempArtifWorn.find(miscSlots[i]) == tempArtifWorn.end() || miscSlots[i] == slotID)
  99. // misc--;
  100. // }
  101. // if (misc > 0)
  102. // return false;
  103. // }
  104. //
  105. // return true;
  106. // }
  107. // bool CArtifact::canBeAssembledTo (const std::map<ui16, const CArtifact*> &artifWorn, ui32 artifactID) const
  108. // {
  109. // if (constituentOf == NULL || !vstd::contains(*constituentOf, artifactID))
  110. // return false;
  111. //
  112. // const CArtifact &artifact = *VLC->arth->artifacts[artifactID];
  113. // assert(artifact.constituents);
  114. //
  115. // BOOST_FOREACH(ui32 constituentID, *artifact.constituents)
  116. // {
  117. // bool found = false;
  118. // for (std::map<ui16, const CArtifact*>::const_iterator it = artifWorn.begin(); it != artifWorn.end(); ++it)
  119. // {
  120. // if (it->second->id == constituentID)
  121. // {
  122. // found = true;
  123. // break;
  124. // }
  125. // }
  126. // if (!found)
  127. // return false;
  128. // }
  129. //
  130. // return true;
  131. // }
  132. CArtifact::CArtifact()
  133. {
  134. setNodeType(ARTIFACT);
  135. }
  136. CArtifact::~CArtifact()
  137. {
  138. }
  139. int CArtifact::getArtClassSerial() const
  140. {
  141. if(id == 1)
  142. return 4;
  143. switch(aClass)
  144. {
  145. case ART_TREASURE:
  146. return 0;
  147. case ART_MINOR:
  148. return 1;
  149. case ART_MAJOR:
  150. return 2;
  151. case ART_RELIC:
  152. return 3;
  153. case ART_SPECIAL:
  154. return 5;
  155. }
  156. return -1;
  157. }
  158. std::string CArtifact::nodeName() const
  159. {
  160. return "Artifact: " + Name();
  161. }
  162. // void CArtifact::getParents(TCNodes &out, const CBonusSystemNode *root /*= NULL*/) const
  163. // {
  164. // //combined artifact carries bonuses from its parts
  165. // if(constituents)
  166. // {
  167. // BOOST_FOREACH(ui32 id, *constituents)
  168. // out.insert(VLC->arth->artifacts[id]);
  169. // }
  170. // }
  171. // void CScroll::Init()
  172. // {
  173. // // addNewBonus (Bonus (Bonus::PERMANENT, Bonus::SPELL, Bonus::ARTIFACT, 1, id, spellid, Bonus::INDEPENDENT_MAX));
  174. // // //boost::algorithm::replace_first(description, "[spell name]", VLC->spellh->spells[spellid].name);
  175. // }
  176. CArtHandler::CArtHandler()
  177. {
  178. VLC->arth = this;
  179. // War machines are the default big artifacts.
  180. for (ui32 i = 3; i <= 6; i++)
  181. bigArtifacts.insert(i);
  182. if (GameConstants::STACK_ARTIFACT)
  183. creatureArtifacts += 141, 142, 143, 156; //basic Wog arts and Warlord's banner
  184. }
  185. CArtHandler::~CArtHandler()
  186. {
  187. for (std::vector< ConstTransitivePtr<CArtifact> >::iterator it = artifacts.begin(); it != artifacts.end(); ++it)
  188. {
  189. delete (*it)->constituents;
  190. delete (*it)->constituentOf;
  191. }
  192. }
  193. void CArtHandler::loadArtifacts(bool onlyTxt)
  194. {
  195. std::vector<ui16> slots;
  196. slots += 17, 16, 15, 14, 13, 18, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0;
  197. static std::map<char, CArtifact::EartClass> classes =
  198. map_list_of('S',CArtifact::ART_SPECIAL)('T',CArtifact::ART_TREASURE)('N',CArtifact::ART_MINOR)('J',CArtifact::ART_MAJOR)('R',CArtifact::ART_RELIC);
  199. std::string buf = bitmaph->getTextFile("ARTRAITS.TXT"), dump, pom;
  200. int it=0;
  201. for(int i=0; i<2; ++i)
  202. {
  203. loadToIt(dump,buf,it,3);
  204. }
  205. VLC->generaltexth->artifNames.resize(GameConstants::ARTIFACTS_QUANTITY);
  206. VLC->generaltexth->artifDescriptions.resize(GameConstants::ARTIFACTS_QUANTITY);
  207. std::map<ui32,ui8>::iterator itr;
  208. for (int i=0; i<GameConstants::ARTIFACTS_QUANTITY; i++)
  209. {
  210. CArtifact *art = new CArtifact();
  211. CArtifact &nart = *art;
  212. nart.id=i;
  213. loadToIt(VLC->generaltexth->artifNames[i],buf,it,4);
  214. loadToIt(pom,buf,it,4);
  215. nart.price=atoi(pom.c_str());
  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.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. }
  655. void CArtHandler::clear()
  656. {
  657. BOOST_FOREACH(CArtifact *art, artifacts)
  658. delete art;
  659. artifacts.clear();
  660. clearHlpLists();
  661. }
  662. // /**
  663. // * Locally equips an artifact to a hero's worn slots. Unequips an already present artifact.
  664. // * Does not test if the operation is legal.
  665. // * @param artifWorn A hero's set of worn artifacts.
  666. // * @param bonuses Optional list of bonuses to update.
  667. // */
  668. // void CArtHandler::equipArtifact( std::map<ui16, const CArtifact*> &artifWorn, ui16 slotID, const CArtifact* art ) const
  669. // {
  670. // unequipArtifact(artifWorn, slotID);
  671. //
  672. // if (art) //false when artifact is NULL -> slot set to empty
  673. // {
  674. // const CArtifact &artifact = *art;
  675. //
  676. // // Add artifact.
  677. // artifWorn[slotID] = art;
  678. //
  679. // // Add locks, in reverse order of being removed.
  680. // if (artifact.constituents != NULL)
  681. // {
  682. // bool destConsumed = false; // Determines which constituent that will be counted for together with the artifact.
  683. //
  684. // BOOST_FOREACH(ui32 constituentID, *artifact.constituents)
  685. // {
  686. // const CArtifact &constituent = *artifacts[constituentID];
  687. //
  688. // if (!destConsumed && vstd::contains(constituent.possibleSlots, slotID))
  689. // {
  690. // destConsumed = true;
  691. // }
  692. // else
  693. // {
  694. // BOOST_FOREACH(ui16 slot, constituent.possibleSlots)
  695. // {
  696. // if (!vstd::contains(artifWorn, slot))
  697. // {
  698. // artifWorn[slot] = VLC->arth->artifacts[145]; //lock
  699. // break;
  700. // }
  701. // }
  702. // }
  703. // }
  704. // }
  705. // }
  706. // }
  707. //
  708. // /**
  709. // * Locally unequips an artifact from a hero's worn slots.
  710. // * Does not test if the operation is legal.
  711. // * @param artifWorn A hero's set of worn artifacts.
  712. // * @param bonuses Optional list of bonuses to update.
  713. // */
  714. // void CArtHandler::unequipArtifact(std::map<ui16, const CArtifact*> &artifWorn, ui16 slotID) const
  715. // {
  716. // if (!vstd::contains(artifWorn, slotID))
  717. // return;
  718. //
  719. // const CArtifact &artifact = *artifWorn[slotID];
  720. //
  721. // // Remove artifact, if it's not already removed.
  722. // artifWorn.erase(slotID);
  723. //
  724. // // Remove locks, in reverse order of being added.
  725. // if (artifact.constituents != NULL)
  726. // {
  727. // bool destConsumed = false;
  728. //
  729. // BOOST_FOREACH(ui32 constituentID, *artifact.constituents)
  730. // {
  731. // const CArtifact &constituent = *artifacts[constituentID];
  732. //
  733. // if (!destConsumed && vstd::contains(constituent.possibleSlots, slotID))
  734. // {
  735. // destConsumed = true;
  736. // }
  737. // else
  738. // {
  739. // BOOST_REVERSE_FOREACH(ui16 slot, constituent.possibleSlots)
  740. // {
  741. // if (vstd::contains(artifWorn, slot) && artifWorn[slot]->id == 145)
  742. // {
  743. // artifWorn.erase(slot);
  744. // break;
  745. // }
  746. // }
  747. // }
  748. // }
  749. // }
  750. // }
  751. void CArtHandler::clearHlpLists()
  752. {
  753. treasures.clear();
  754. minors.clear();
  755. majors.clear();
  756. relics.clear();
  757. }
  758. void CArtHandler::initAllowedArtifactsList(const std::vector<ui8> &allowed)
  759. {
  760. allowedArtifacts.clear();
  761. clearHlpLists();
  762. for (int i=0; i<144; ++i) //yes, 144
  763. {
  764. if (allowed[i])
  765. allowedArtifacts.push_back(artifacts[i]);
  766. }
  767. }
  768. CArtifactInstance::CArtifactInstance()
  769. {
  770. init();
  771. }
  772. CArtifactInstance::CArtifactInstance( CArtifact *Art)
  773. {
  774. init();
  775. setType(Art);
  776. }
  777. // CArtifactInstance::CArtifactInstance(int aid)
  778. // {
  779. // init();
  780. // setType(VLC->arth->artifacts[aid]);
  781. // }
  782. void CArtifactInstance::setType( CArtifact *Art )
  783. {
  784. artType = Art;
  785. attachTo(Art);
  786. }
  787. std::string CArtifactInstance::nodeName() const
  788. {
  789. return "Artifact instance of " + (artType ? artType->Name() : std::string("uninitialized")) + " type";
  790. }
  791. CArtifactInstance * CArtifactInstance::createScroll( const CSpell *s)
  792. {
  793. CArtifactInstance *ret = new CArtifactInstance(VLC->arth->artifacts[1]);
  794. Bonus *b = new Bonus(Bonus::PERMANENT, Bonus::SPELL, Bonus::ARTIFACT_INSTANCE, -1, 1, s->id);
  795. ret->addNewBonus(b);
  796. return ret;
  797. }
  798. void CArtifactInstance::init()
  799. {
  800. id = -1;
  801. setNodeType(ARTIFACT_INSTANCE);
  802. }
  803. int CArtifactInstance::firstAvailableSlot(const CGHeroInstance *h) const
  804. {
  805. BOOST_FOREACH(ui16 slot, artType->possibleSlots)
  806. {
  807. if(canBePutAt(ArtifactLocation(h, slot))) //if(artType->fitsAt(h->artifWorn, slot))
  808. {
  809. //we've found a free suitable slot.
  810. return slot;
  811. }
  812. }
  813. //if haven't find proper slot, use backpack
  814. return firstBackpackSlot(h);
  815. }
  816. int CArtifactInstance::firstBackpackSlot(const CGHeroInstance *h) const
  817. {
  818. if(!artType->isBig()) //discard big artifact
  819. return GameConstants::BACKPACK_START + h->artifactsInBackpack.size();
  820. return -1;
  821. }
  822. bool CArtifactInstance::canBePutAt(const ArtifactLocation &al, bool assumeDestRemoved /*= false*/) const
  823. {
  824. if (al.hero)
  825. {
  826. if(al.slot >= GameConstants::BACKPACK_START)
  827. {
  828. if(artType->isBig())
  829. return false;
  830. //TODO backpack limit
  831. return true;
  832. }
  833. if(!vstd::contains(artType->possibleSlots, al.slot))
  834. return false;
  835. return al.hero->isPositionFree(al.slot, assumeDestRemoved);
  836. }
  837. else
  838. return false;
  839. }
  840. void CArtifactInstance::putAt(CGHeroInstance *h, ui16 slot)
  841. {
  842. assert(canBePutAt(ArtifactLocation(h, slot)));
  843. h->setNewArtSlot(slot, this, false);
  844. if(slot < GameConstants::BACKPACK_START)
  845. h->attachTo(this);
  846. }
  847. void CArtifactInstance::removeFrom(CGHeroInstance *h, ui16 slot)
  848. {
  849. assert(h->CArtifactSet::getArt(slot) == this);
  850. h->eraseArtSlot(slot);
  851. if(slot < GameConstants::BACKPACK_START)
  852. h->detachFrom(this);
  853. //TODO delete me?
  854. }
  855. void CArtifactInstance::putAt(CStackInstance *s, ui16 slot)
  856. {
  857. tlog2 <<"Hero artifacts shouldn't be put on creatures!\n";
  858. }
  859. void CArtifactInstance::removeFrom(CStackInstance *s, ui16 slot)
  860. {
  861. tlog2 <<"Strange, we try to remove hero artifact from CStackInstance\n";
  862. }
  863. bool CArtifactInstance::canBeDisassembled() const
  864. {
  865. return artType->constituents && artType->constituentOf->size();
  866. }
  867. std::vector<const CArtifact *> CArtifactInstance::assemblyPossibilities(const CGHeroInstance *h) const
  868. {
  869. std::vector<const CArtifact *> ret;
  870. if(!artType->constituentOf //not a part of combined artifact
  871. || artType->constituents) //combined artifact already: no combining of combined artifacts... for now.
  872. return ret;
  873. BOOST_FOREACH(ui32 possibleCombinedArt, *artType->constituentOf)
  874. {
  875. const CArtifact * const artifact = VLC->arth->artifacts[possibleCombinedArt];
  876. assert(artifact->constituents);
  877. bool possible = true;
  878. BOOST_FOREACH(ui32 constituentID, *artifact->constituents) //check if all constituents are available
  879. {
  880. if(!h->hasArt(constituentID, true)) //constituent must be equipped
  881. {
  882. possible = false;
  883. break;
  884. }
  885. }
  886. if(possible)
  887. ret.push_back(artifact);
  888. }
  889. return ret;
  890. }
  891. void CArtifactInstance::move(ArtifactLocation &src, ArtifactLocation &dst)
  892. {
  893. if (src.hero)
  894. removeFrom(src.hero, src.slot);
  895. else if (src.stack)
  896. removeFrom(src.stack, src.slot);
  897. else
  898. tlog1 << "No source for moved artifact found!\n";
  899. if (dst.hero)
  900. {
  901. putAt(dst.hero, dst.slot);
  902. if (artType->id == 135 && dst.slot == ArtifactPosition::RIGHT_HAND && !dst.hero->hasSpellbook()) //Titan's Thunder creates new spellbook on equip
  903. dst.hero->giveArtifact(0);
  904. }
  905. else if (dst.stack)
  906. putAt(dst.stack, dst.slot);
  907. else
  908. tlog1 << "No destination for moved artifact found!\n";
  909. }
  910. CArtifactInstance * CArtifactInstance::createNewArtifactInstance(CArtifact *Art)
  911. {
  912. if(!Art->constituents)
  913. {
  914. if (vstd::contains(VLC->arth->creatureArtifacts, Art->id))
  915. return new CCreatureArtifactInstance(Art);
  916. else
  917. return new CArtifactInstance(Art);
  918. }
  919. else
  920. {
  921. CCombinedArtifactInstance * ret = new CCombinedArtifactInstance(Art);
  922. ret->createConstituents();
  923. return ret;
  924. }
  925. }
  926. CArtifactInstance * CArtifactInstance::createNewArtifactInstance(int aid)
  927. {
  928. return createNewArtifactInstance(VLC->arth->artifacts[aid]);
  929. }
  930. void CArtifactInstance::deserializationFix()
  931. {
  932. setType(artType);
  933. }
  934. int CArtifactInstance::getGivenSpellID() const
  935. {
  936. const Bonus * b = getBonus(Selector::type(Bonus::SPELL));
  937. if(!b)
  938. {
  939. tlog3 << "Warning: " << nodeName() << " doesn't bear any spell!\n";
  940. return -1;
  941. }
  942. return b->subtype;
  943. }
  944. bool CArtifactInstance::isPart(const CArtifactInstance *supposedPart) const
  945. {
  946. return supposedPart == this;
  947. }
  948. bool CCombinedArtifactInstance::canBePutAt(const ArtifactLocation &al, bool assumeDestRemoved /*= false*/) const
  949. {
  950. bool canMainArtifactBePlaced = CArtifactInstance::canBePutAt(al, assumeDestRemoved);
  951. if(!canMainArtifactBePlaced)
  952. return false; //no is no...
  953. if(al.slot >= GameConstants::BACKPACK_START)
  954. return true; //we can always remove combined art to the backapck
  955. assert(artType->constituents);
  956. std::vector<ConstituentInfo> constituentsToBePlaced = constituentsInfo; //we'll remove constituents from that list, as we find a suitable slot for them
  957. //it may be that we picked a combined artifact in hero screen (though technically it's still there) to move it
  958. //so we remove from the list all constituents that are already present on dst hero in the form of locks
  959. BOOST_FOREACH(const ConstituentInfo &constituent, constituentsInfo)
  960. {
  961. if(constituent.art == al.hero->getArt(constituent.slot, false)) //no need to worry about locked constituent
  962. constituentsToBePlaced -= constituent;
  963. }
  964. //we iterate over all active slots and check if constituents fits them
  965. for (int i = 0; i < GameConstants::BACKPACK_START; i++)
  966. {
  967. for(std::vector<ConstituentInfo>::iterator art = constituentsToBePlaced.begin(); art != constituentsToBePlaced.end(); art++)
  968. {
  969. if(art->art->canBePutAt(ArtifactLocation(al.hero, i), i == al.slot)) // i == al.slot because we can remove already worn artifact only from that slot that is our main destination
  970. {
  971. constituentsToBePlaced.erase(art);
  972. break;
  973. }
  974. }
  975. }
  976. return constituentsToBePlaced.empty();
  977. }
  978. bool CCombinedArtifactInstance::canBeDisassembled() const
  979. {
  980. return true;
  981. }
  982. CCombinedArtifactInstance::CCombinedArtifactInstance(CArtifact *Art)
  983. : CArtifactInstance(Art) //TODO: seems unued, but need to be written
  984. {
  985. }
  986. CCombinedArtifactInstance::CCombinedArtifactInstance()
  987. {
  988. }
  989. void CCombinedArtifactInstance::createConstituents()
  990. {
  991. assert(artType);
  992. assert(artType->constituents);
  993. BOOST_FOREACH(ui32 a, *artType->constituents)
  994. {
  995. addAsConstituent(CArtifactInstance::createNewArtifactInstance(a), -1);
  996. }
  997. }
  998. void CCombinedArtifactInstance::addAsConstituent(CArtifactInstance *art, int slot)
  999. {
  1000. assert(vstd::contains(*artType->constituents, art->artType->id));
  1001. assert(art->getParentNodes().size() == 1 && art->getParentNodes().front() == art->artType);
  1002. constituentsInfo.push_back(ConstituentInfo(art, slot));
  1003. attachTo(art);
  1004. }
  1005. void CCombinedArtifactInstance::putAt(CGHeroInstance *h, ui16 slot)
  1006. {
  1007. if(slot >= GameConstants::BACKPACK_START)
  1008. {
  1009. CArtifactInstance::putAt(h, slot);
  1010. BOOST_FOREACH(ConstituentInfo &ci, constituentsInfo)
  1011. ci.slot = -1;
  1012. }
  1013. else
  1014. {
  1015. CArtifactInstance *mainConstituent = figureMainConstituent(slot); //it'll be replaced with combined artifact, not a lock
  1016. CArtifactInstance::putAt(h, slot); //puts combined art (this)
  1017. BOOST_FOREACH(ConstituentInfo &ci, constituentsInfo)
  1018. {
  1019. if(ci.art != mainConstituent)
  1020. {
  1021. int pos = -1;
  1022. if(vstd::isbetween(ci.slot, 0, GameConstants::BACKPACK_START) && ci.art->canBePutAt(ArtifactLocation(h, ci.slot))) //there is a valid suggestion where to place lock
  1023. pos = ci.slot;
  1024. else
  1025. ci.slot = pos = ci.art->firstAvailableSlot(h);
  1026. assert(pos < GameConstants::BACKPACK_START);
  1027. h->setNewArtSlot(pos, ci.art, true); //sets as lock
  1028. }
  1029. else
  1030. {
  1031. ci.slot = -1;
  1032. }
  1033. }
  1034. }
  1035. }
  1036. void CCombinedArtifactInstance::removeFrom(CGHeroInstance *h, ui16 slot)
  1037. {
  1038. if(slot >= GameConstants::BACKPACK_START)
  1039. {
  1040. CArtifactInstance::removeFrom(h, slot);
  1041. }
  1042. else
  1043. {
  1044. BOOST_FOREACH(ConstituentInfo &ci, constituentsInfo)
  1045. {
  1046. if(ci.slot >= 0)
  1047. {
  1048. h->eraseArtSlot(ci.slot);
  1049. ci.slot = -1;
  1050. }
  1051. else
  1052. {
  1053. //main constituent
  1054. CArtifactInstance::removeFrom(h, slot);
  1055. }
  1056. }
  1057. }
  1058. }
  1059. CArtifactInstance * CCombinedArtifactInstance::figureMainConstituent(ui16 slot)
  1060. {
  1061. CArtifactInstance *mainConstituent = NULL; //it'll be replaced with combined artifact, not a lock
  1062. BOOST_FOREACH(ConstituentInfo &ci, constituentsInfo)
  1063. if(ci.slot == slot)
  1064. mainConstituent = ci.art;
  1065. if(!mainConstituent)
  1066. {
  1067. BOOST_FOREACH(ConstituentInfo &ci, constituentsInfo)
  1068. {
  1069. if(vstd::contains(ci.art->artType->possibleSlots, slot))
  1070. {
  1071. mainConstituent = ci.art;
  1072. }
  1073. }
  1074. }
  1075. return mainConstituent;
  1076. }
  1077. void CCombinedArtifactInstance::deserializationFix()
  1078. {
  1079. BOOST_FOREACH(ConstituentInfo &ci, constituentsInfo)
  1080. attachTo(ci.art);
  1081. }
  1082. bool CCombinedArtifactInstance::isPart(const CArtifactInstance *supposedPart) const
  1083. {
  1084. bool me = CArtifactInstance::isPart(supposedPart);
  1085. if(me)
  1086. return true;
  1087. //check for constituents
  1088. BOOST_FOREACH(const ConstituentInfo &constituent, constituentsInfo)
  1089. if(constituent.art == supposedPart)
  1090. return true;
  1091. return false;
  1092. }
  1093. CCombinedArtifactInstance::ConstituentInfo::ConstituentInfo(CArtifactInstance *Art /*= NULL*/, ui16 Slot /*= -1*/)
  1094. {
  1095. art = Art;
  1096. slot = Slot;
  1097. }
  1098. bool CCombinedArtifactInstance::ConstituentInfo::operator==(const ConstituentInfo &rhs) const
  1099. {
  1100. return art == rhs.art && slot == rhs.slot;
  1101. }
  1102. CCreatureArtifactInstance::CCreatureArtifactInstance()
  1103. {
  1104. init();
  1105. }
  1106. CCreatureArtifactInstance::CCreatureArtifactInstance(CArtifact *Art)
  1107. {
  1108. init();
  1109. setType(Art);
  1110. }
  1111. bool CCreatureArtifactInstance::isPart(const CArtifactInstance *supposedPart) const
  1112. {
  1113. return false; //TODO: any other proposals?
  1114. }
  1115. std::string CCreatureArtifactInstance::nodeName() const
  1116. {
  1117. return "Creature artifact instance of " + (artType ? artType->Name() : std::string("uninitialized")) + " type";
  1118. }
  1119. bool CCreatureArtifactInstance::canBePutAt(const ArtifactLocation &al, bool assumeDestRemoved /*= false*/) const
  1120. {
  1121. if (al.stack)
  1122. {
  1123. return true; //all artifacts should fit on creature
  1124. }
  1125. else if(al.slot >= GameConstants::BACKPACK_START)
  1126. { //TODO backpack limit?
  1127. return true;
  1128. }
  1129. return false; //hero can't wear creature art
  1130. }
  1131. void CCreatureArtifactInstance::putAt(CStackInstance *s, ui16 slot)
  1132. {
  1133. assert(canBePutAt(ArtifactLocation(s, slot)));
  1134. s->setNewArtSlot(slot, this, false);
  1135. if(slot == GameConstants::CREATURE_ART)
  1136. s->attachTo(this);
  1137. }
  1138. void CCreatureArtifactInstance::removeFrom(CStackInstance *s, ui16 slot)
  1139. {
  1140. assert(s->CCreatureArtifactSet::getArt(slot) == this);
  1141. s->eraseArtSlot(slot);
  1142. if(slot == GameConstants::CREATURE_ART) //we remove worn artifact
  1143. s->detachFrom(this);
  1144. }
  1145. const CArtifactInstance* IArtifactSetBase::getArt(ui16 pos, bool excludeLocked) const
  1146. {
  1147. if(const ArtSlotInfo *si = getSlot(pos))
  1148. {
  1149. if(si->artifact && (!excludeLocked || !si->locked))
  1150. return si->artifact;
  1151. }
  1152. return NULL;
  1153. }
  1154. bool IArtifactSetBase::hasArt(ui32 aid, bool onlyWorn) const
  1155. {
  1156. return getArtPos(aid, onlyWorn) != -1;
  1157. }
  1158. bool IArtifactSetBase::isPositionFree(ui16 pos, bool onlyLockCheck) const
  1159. {
  1160. if(const ArtSlotInfo *s = getSlot(pos))
  1161. return (onlyLockCheck || !s->artifact) && !s->locked;
  1162. return true; //no slot means not used
  1163. }
  1164. void IArtifactSetBase::setNewArtSlot(ui16 slot, CArtifactInstance *art, bool locked)
  1165. {
  1166. ArtSlotInfo &asi = retreiveNewArtSlot(slot);
  1167. asi.artifact = art;
  1168. asi.locked = locked;
  1169. }
  1170. CArtifactInstance* IArtifactSetBase::getArt(ui16 pos, bool excludeLocked /*= true*/)
  1171. {
  1172. return const_cast<CArtifactInstance*>((const_cast<const IArtifactSetBase*>(this))->getArt(pos, excludeLocked));
  1173. }
  1174. si32 CArtifactSet::getArtPos(int aid, bool onlyWorn /*= true*/) const
  1175. {
  1176. for(std::map<ui16, ArtSlotInfo>::const_iterator i = artifactsWorn.begin(); i != artifactsWorn.end(); i++)
  1177. if(i->second.artifact->artType->id == aid)
  1178. return i->first;
  1179. if(onlyWorn)
  1180. return -1;
  1181. for(int i = 0; i < artifactsInBackpack.size(); i++)
  1182. if(artifactsInBackpack[i].artifact->artType->id == aid)
  1183. return GameConstants::BACKPACK_START + i;
  1184. return -1;
  1185. }
  1186. si32 CArtifactSet::getArtPos(const CArtifactInstance *art) const
  1187. {
  1188. for(std::map<ui16, ArtSlotInfo>::const_iterator i = artifactsWorn.begin(); i != artifactsWorn.end(); i++)
  1189. if(i->second.artifact == art)
  1190. return i->first;
  1191. for(int i = 0; i < artifactsInBackpack.size(); i++)
  1192. if(artifactsInBackpack[i].artifact == art)
  1193. return GameConstants::BACKPACK_START + i;
  1194. return -1;
  1195. }
  1196. const CArtifactInstance * CArtifactSet::getArtByInstanceId(int artInstId) const
  1197. {
  1198. for(std::map<ui16, ArtSlotInfo>::const_iterator i = artifactsWorn.begin(); i != artifactsWorn.end(); i++)
  1199. if(i->second.artifact->id == artInstId)
  1200. return i->second.artifact;
  1201. for(int i = 0; i < artifactsInBackpack.size(); i++)
  1202. if(artifactsInBackpack[i].artifact->id == artInstId)
  1203. return artifactsInBackpack[i].artifact;
  1204. return NULL;
  1205. }
  1206. const ArtSlotInfo * CArtifactSet::getSlot(ui16 pos) const
  1207. {
  1208. if(vstd::contains(artifactsWorn, pos))
  1209. return &artifactsWorn[pos];
  1210. if(pos >= ArtifactPosition::AFTER_LAST )
  1211. {
  1212. int backpackPos = (int)pos - GameConstants::BACKPACK_START;
  1213. if(backpackPos < 0 || backpackPos >= artifactsInBackpack.size())
  1214. return NULL;
  1215. else
  1216. return &artifactsInBackpack[backpackPos];
  1217. }
  1218. return NULL;
  1219. }
  1220. si32 CArtifactSet::getArtTypeId(ui16 pos) const
  1221. {
  1222. const CArtifactInstance * const a = getArt(pos);
  1223. if(!a)
  1224. {
  1225. tlog2 << (dynamic_cast<const CGHeroInstance*>(this))->name << " has no artifact at " << pos << " (getArtTypeId)\n";
  1226. return -1;
  1227. }
  1228. return a->artType->id;
  1229. }
  1230. CArtifactSet::~CArtifactSet()
  1231. {
  1232. }
  1233. ArtSlotInfo & CArtifactSet::retreiveNewArtSlot(ui16 slot)
  1234. {
  1235. assert(!vstd::contains(artifactsWorn, slot));
  1236. ArtSlotInfo &ret = slot < GameConstants::BACKPACK_START
  1237. ? artifactsWorn[slot]
  1238. : *artifactsInBackpack.insert(artifactsInBackpack.begin() + (slot - GameConstants::BACKPACK_START), ArtSlotInfo());
  1239. return ret;
  1240. }
  1241. void CArtifactSet::eraseArtSlot(ui16 slot)
  1242. {
  1243. if(slot < GameConstants::BACKPACK_START)
  1244. {
  1245. artifactsWorn.erase(slot);
  1246. }
  1247. else
  1248. {
  1249. slot -= GameConstants::BACKPACK_START;
  1250. artifactsInBackpack.erase(artifactsInBackpack.begin() + slot);
  1251. }
  1252. }
  1253. ArtSlotInfo & CCreatureArtifactSet::retreiveNewArtSlot(ui16 slot)
  1254. {
  1255. assert(slot); //ke?
  1256. ArtSlotInfo &ret = slot <= GameConstants::CREATURE_ART
  1257. ? activeArtifact
  1258. : *artifactsInBackpack.insert(artifactsInBackpack.begin() + (slot - 1), ArtSlotInfo());
  1259. return ret;
  1260. }
  1261. void CCreatureArtifactSet::eraseArtSlot(ui16 slot)
  1262. {
  1263. if(slot == GameConstants::CREATURE_ART)
  1264. {
  1265. activeArtifact.artifact = NULL; //hmm?
  1266. }
  1267. else
  1268. {
  1269. slot -= 1;
  1270. artifactsInBackpack.erase(artifactsInBackpack.begin() + slot);
  1271. }
  1272. }
  1273. const ArtSlotInfo * CCreatureArtifactSet::getSlot(ui16 pos) const
  1274. {
  1275. if (pos == GameConstants::CREATURE_ART)
  1276. return &activeArtifact;
  1277. else if(pos > GameConstants::CREATURE_ART)
  1278. {
  1279. int backpackPos = (int)pos - 1;
  1280. if(backpackPos < 0 || backpackPos >= artifactsInBackpack.size())
  1281. return NULL;
  1282. else
  1283. return &artifactsInBackpack[backpackPos];
  1284. }
  1285. return NULL;
  1286. }
  1287. si32 CCreatureArtifactSet::getArtPos(int aid, bool onlyWorn) const
  1288. {
  1289. if (aid == activeArtifact.artifact->artType->id )
  1290. return GameConstants::CREATURE_ART;
  1291. if(onlyWorn)
  1292. return -1;
  1293. for(int i = 0; i < artifactsInBackpack.size(); i++)
  1294. {
  1295. if(artifactsInBackpack[i].artifact->artType->id == aid)
  1296. return i + 1;
  1297. }
  1298. return -1;
  1299. }
  1300. si32 CCreatureArtifactSet::getArtPos(const CArtifactInstance *art) const
  1301. {
  1302. if (activeArtifact.artifact == art)
  1303. return GameConstants::CREATURE_ART;
  1304. for(int i = 0; i < artifactsInBackpack.size(); i++)
  1305. if(artifactsInBackpack[i].artifact == art)
  1306. return GameConstants::BACKPACK_START + i;
  1307. return -1;
  1308. }
  1309. const CArtifactInstance * CCreatureArtifactSet::getArtByInstanceId(int artInstId) const
  1310. {
  1311. if (activeArtifact.artifact->id == artInstId)
  1312. return activeArtifact.artifact;
  1313. for(int i = 0; i < artifactsInBackpack.size(); i++)
  1314. if(artifactsInBackpack[i].artifact->id == artInstId)
  1315. return artifactsInBackpack[i].artifact;
  1316. return NULL;
  1317. }
  1318. si32 CCreatureArtifactSet::getArtTypeId(ui16 pos) const
  1319. {
  1320. const CArtifactInstance * const a = getArt(pos);
  1321. if(!a)
  1322. {
  1323. tlog2 << "Stack has no artifact at " << pos << " (getArtTypeId)\n";
  1324. return -1;
  1325. }
  1326. return a->artType->id;
  1327. }