CArtHandler.cpp 34 KB

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