CArtHandler.cpp 41 KB

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