CArtHandler.cpp 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180
  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 arts are avaliable
  431. {
  432. out.resize (64);
  433. std::fill_n (out.begin(), 64, artifacts[2]); //magic
  434. }
  435. }
  436. void CArtHandler::getAllowedArts(std::vector<ConstTransitivePtr<CArtifact> > &out, std::vector<CArtifact*> *arts, int flag)
  437. {
  438. if (arts->empty()) //restock avaliable arts
  439. {
  440. for (int i = 0; i < allowedArtifacts.size(); ++i)
  441. {
  442. if (allowedArtifacts[i]->aClass == flag)
  443. arts->push_back(allowedArtifacts[i]);
  444. }
  445. }
  446. for (int i = 0; i < arts->size(); ++i)
  447. {
  448. CArtifact *art = (*arts)[i];
  449. out.push_back(art);
  450. }
  451. }
  452. void CArtHandler::giveArtBonus( int aid, Bonus::BonusType type, int val, int subtype, int valType, ILimiter * limiter )
  453. {
  454. Bonus *added = new Bonus(Bonus::PERMANENT,type,Bonus::ARTIFACT,val,aid,subtype);
  455. added->valType = valType;
  456. added->limiter.reset(limiter);
  457. if(type == Bonus::MORALE || Bonus::LUCK)
  458. added->description = artifacts[aid]->Name() + (val > 0 ? " +" : " ") + boost::lexical_cast<std::string>(val);
  459. artifacts[aid]->addNewBonus(added);
  460. }
  461. void CArtHandler::addBonuses()
  462. {
  463. #define ART_PRIM_SKILL(ID, whichSkill, val) giveArtBonus(ID,Bonus::PRIMARY_SKILL,val,whichSkill)
  464. #define ART_MORALE(ID, val) giveArtBonus(ID,Bonus::MORALE,val)
  465. #define ART_LUCK(ID, val) giveArtBonus(ID,Bonus::LUCK,val)
  466. #define ART_MORALE_AND_LUCK(ID, val) giveArtBonus(ID,Bonus::MORALE_AND_LUCK,val)
  467. #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)
  468. #define ART_ATTACK_AND_DEFENSE(ID, val) ART_PRIM_SKILL(ID,0,val); ART_PRIM_SKILL(ID,1,val)
  469. #define ART_POWER_AND_KNOWLEDGE(ID, val) ART_PRIM_SKILL(ID,2,val); ART_PRIM_SKILL(ID,3,val)
  470. //Attack bonus artifacts (Weapons)
  471. ART_PRIM_SKILL(7,0,+2); //Centaur Axe
  472. ART_PRIM_SKILL(8,0,+3); //Blackshard of the Dead Knight
  473. ART_PRIM_SKILL(9,0,+4); //Greater Gnoll's Flail
  474. ART_PRIM_SKILL(10,0,+5); //Ogre's Club of Havoc
  475. ART_PRIM_SKILL(11,0,+6); //Sword of Hellfire
  476. ART_PRIM_SKILL(12,0,+12); //Titan's Gladius
  477. ART_PRIM_SKILL(12,1,-3); //Titan's Gladius
  478. //Defense bonus artifacts (Shields)
  479. ART_PRIM_SKILL(13,1,+2); //Shield of the Dwarven Lords
  480. ART_PRIM_SKILL(14,1,+3); //Shield of the Yawning Dead
  481. ART_PRIM_SKILL(15,1,+4); //Buckler of the Gnoll King
  482. ART_PRIM_SKILL(16,1,+5); //Targ of the Rampaging Ogre
  483. ART_PRIM_SKILL(17,1,+6); //Shield of the Damned
  484. ART_PRIM_SKILL(18,1,+12); //Sentinel's Shield
  485. ART_PRIM_SKILL(18,0,-3); //Sentinel's Shield
  486. //Knowledge bonus artifacts (Helmets)
  487. ART_PRIM_SKILL(19,3,+1); //Helm of the Alabaster Unicorn
  488. ART_PRIM_SKILL(20,3,+2); //Skull Helmet
  489. ART_PRIM_SKILL(21,3,+3); //Helm of Chaos
  490. ART_PRIM_SKILL(22,3,+4); //Crown of the Supreme Magi
  491. ART_PRIM_SKILL(23,3,+5); //Hellstorm Helmet
  492. ART_PRIM_SKILL(24,3,+10); //Thunder Helmet
  493. ART_PRIM_SKILL(24,2,-2); //Thunder Helmet
  494. //Spell power bonus artifacts (Armours)
  495. ART_PRIM_SKILL(25,2,+1); //Breastplate of Petrified Wood
  496. ART_PRIM_SKILL(26,2,+2); //Rib Cage
  497. ART_PRIM_SKILL(27,2,+3); //Scales of the Greater Basilisk
  498. ART_PRIM_SKILL(28,2,+4); //Tunic of the Cyclops King
  499. ART_PRIM_SKILL(29,2,+5); //Breastplate of Brimstone
  500. ART_PRIM_SKILL(30,2,+10); //Titan's Cuirass
  501. ART_PRIM_SKILL(30,3,-2); //Titan's Cuirass
  502. //All primary skills (various)
  503. ART_ALL_PRIM_SKILLS(31,+1); //Armor of Wonder
  504. ART_ALL_PRIM_SKILLS(32,+2); //Sandals of the Saint
  505. ART_ALL_PRIM_SKILLS(33,+3); //Celestial Necklace of Bliss
  506. ART_ALL_PRIM_SKILLS(34,+4); //Lion's Shield of Courage
  507. ART_ALL_PRIM_SKILLS(35,+5); //Sword of Judgement
  508. ART_ALL_PRIM_SKILLS(36,+6); //Helm of Heavenly Enlightenment
  509. //Attack and Defense (various)
  510. ART_ATTACK_AND_DEFENSE(37,+1); //Quiet Eye of the Dragon
  511. ART_ATTACK_AND_DEFENSE(38,+2); //Red Dragon Flame Tongue
  512. ART_ATTACK_AND_DEFENSE(39,+3); //Dragon Scale Shield
  513. ART_ATTACK_AND_DEFENSE(40,+4); //Dragon Scale Armor
  514. //Spell power and Knowledge (various)
  515. ART_POWER_AND_KNOWLEDGE(41,+1); //Dragonbone Greaves
  516. ART_POWER_AND_KNOWLEDGE(42,+2); //Dragon Wing Tabard
  517. ART_POWER_AND_KNOWLEDGE(43,+3); //Necklace of Dragonteeth
  518. ART_POWER_AND_KNOWLEDGE(44,+4); //Crown of Dragontooth
  519. //Luck and morale
  520. ART_MORALE(45,+1); //Still Eye of the Dragon
  521. ART_LUCK(45,+1); //Still Eye of the Dragon
  522. ART_LUCK(46,+1); //Clover of Fortune
  523. ART_LUCK(47,+1); //Cards of Prophecy
  524. ART_LUCK(48,+1); //Ladybird of Luck
  525. ART_MORALE(49,+1); //Badge of Courage -> +1 morale and immunity to hostile mind spells:
  526. giveArtBonus(49,Bonus::SPELL_IMMUNITY,0,50);//sorrow
  527. giveArtBonus(49,Bonus::SPELL_IMMUNITY,0,59);//berserk
  528. giveArtBonus(49,Bonus::SPELL_IMMUNITY,0,60);//hypnotize
  529. giveArtBonus(49,Bonus::SPELL_IMMUNITY,0,61);//forgetfulness
  530. giveArtBonus(49,Bonus::SPELL_IMMUNITY,0,62);//blind
  531. ART_MORALE(50,+1); //Crest of Valor
  532. ART_MORALE(51,+1); //Glyph of Gallantry
  533. giveArtBonus(52,Bonus::SIGHT_RADIOUS,+1);//Speculum
  534. giveArtBonus(53,Bonus::SIGHT_RADIOUS,+1);//Spyglass
  535. //necromancy bonus
  536. giveArtBonus(54,Bonus::SECONDARY_SKILL_PREMY,+5, CGHeroInstance::NECROMANCY, Bonus::ADDITIVE_VALUE);//Amulet of the Undertaker
  537. giveArtBonus(55,Bonus::SECONDARY_SKILL_PREMY,+10, CGHeroInstance::NECROMANCY, Bonus::ADDITIVE_VALUE);//Vampire's Cowl
  538. giveArtBonus(56,Bonus::SECONDARY_SKILL_PREMY,+15, CGHeroInstance::NECROMANCY, Bonus::ADDITIVE_VALUE);//Dead Man's Boots
  539. giveArtBonus(57,Bonus::MAGIC_RESISTANCE,+5);//Garniture of Interference
  540. giveArtBonus(58,Bonus::MAGIC_RESISTANCE,+10);//Surcoat of Counterpoise
  541. giveArtBonus(59,Bonus::MAGIC_RESISTANCE,+15);//Boots of Polarity
  542. //archery bonus
  543. giveArtBonus(60,Bonus::SECONDARY_SKILL_PREMY,+5, CGHeroInstance::ARCHERY, Bonus::ADDITIVE_VALUE);//Bow of Elven Cherrywood
  544. giveArtBonus(61,Bonus::SECONDARY_SKILL_PREMY,+10,CGHeroInstance::ARCHERY, Bonus::ADDITIVE_VALUE);//Bowstring of the Unicorn's Mane
  545. giveArtBonus(62,Bonus::SECONDARY_SKILL_PREMY,+15,CGHeroInstance::ARCHERY, Bonus::ADDITIVE_VALUE);//Angel Feather Arrows
  546. //eagle eye bonus
  547. giveArtBonus(63,Bonus::SECONDARY_SKILL_PREMY,+5, CGHeroInstance::EAGLE_EYE, Bonus::ADDITIVE_VALUE);//Bird of Perception
  548. giveArtBonus(64,Bonus::SECONDARY_SKILL_PREMY,+10, CGHeroInstance::EAGLE_EYE, Bonus::ADDITIVE_VALUE);//Stoic Watchman
  549. giveArtBonus(65,Bonus::SECONDARY_SKILL_PREMY,+15, CGHeroInstance::EAGLE_EYE, Bonus::ADDITIVE_VALUE);//Emblem of Cognizance
  550. //reducing cost of surrendering
  551. giveArtBonus(66,Bonus::SURRENDER_DISCOUNT,+10);//Statesman's Medal
  552. giveArtBonus(67,Bonus::SURRENDER_DISCOUNT,+10);//Diplomat's Ring
  553. giveArtBonus(68,Bonus::SURRENDER_DISCOUNT,+10);//Ambassador's Sash
  554. giveArtBonus(69,Bonus::STACKS_SPEED,+1);//Ring of the Wayfarer
  555. giveArtBonus(70,Bonus::LAND_MOVEMENT,+300);//Equestrian's Gloves
  556. giveArtBonus(71,Bonus::SEA_MOVEMENT,+1000);//Necklace of Ocean Guidance
  557. giveArtBonus(72,Bonus::FLYING_MOVEMENT, 0, 1);//Angel Wings
  558. giveArtBonus(73,Bonus::MANA_REGENERATION,+1);//Charm of Mana
  559. giveArtBonus(74,Bonus::MANA_REGENERATION,+2);//Talisman of Mana
  560. giveArtBonus(75,Bonus::MANA_REGENERATION,+3);//Mystic Orb of Mana
  561. giveArtBonus(76,Bonus::SPELL_DURATION,+1);//Collar of Conjuring
  562. giveArtBonus(77,Bonus::SPELL_DURATION,+2);//Ring of Conjuring
  563. giveArtBonus(78,Bonus::SPELL_DURATION,+3);//Cape of Conjuring
  564. giveArtBonus(79,Bonus::AIR_SPELL_DMG_PREMY,+50);//Orb of the Firmament
  565. giveArtBonus(80,Bonus::EARTH_SPELL_DMG_PREMY,+50);//Orb of Silt
  566. giveArtBonus(81,Bonus::FIRE_SPELL_DMG_PREMY,+50);//Orb of Tempestuous Fire
  567. giveArtBonus(82,Bonus::WATER_SPELL_DMG_PREMY,+50);//Orb of Driving Rain
  568. giveArtBonus(83,Bonus::BLOCK_SPELLS_ABOVE_LEVEL,3,-1,Bonus::INDEPENDENT_MIN);//Recanter's Cloak
  569. giveArtBonus(84,Bonus::BLOCK_MORALE,0);//Spirit of Oppression
  570. giveArtBonus(85,Bonus::BLOCK_LUCK,0);//Hourglass of the Evil Hour
  571. giveArtBonus(86,Bonus::FIRE_SPELLS,0);//Tome of Fire Magic
  572. giveArtBonus(87,Bonus::AIR_SPELLS,0);//Tome of Air Magic
  573. giveArtBonus(88,Bonus::WATER_SPELLS,0);//Tome of Water Magic
  574. giveArtBonus(89,Bonus::EARTH_SPELLS,0);//Tome of Earth Magic
  575. giveArtBonus(90,Bonus::WATER_WALKING, 0, 1);//Boots of Levitation
  576. giveArtBonus(91,Bonus::NO_SHOTING_PENALTY,0);//Golden Bow
  577. giveArtBonus(92,Bonus::SPELL_IMMUNITY,0,35);//Sphere of Permanence
  578. giveArtBonus(93,Bonus::NEGATE_ALL_NATURAL_IMMUNITIES,0);//Orb of Vulnerability
  579. giveArtBonus(94,Bonus::STACK_HEALTH,+1);//Ring of Vitality
  580. giveArtBonus(95,Bonus::STACK_HEALTH,+1);//Ring of Life
  581. giveArtBonus(96,Bonus::STACK_HEALTH,+2);//Vial of Lifeblood
  582. giveArtBonus(97,Bonus::STACKS_SPEED,+1);//Necklace of Swiftness
  583. giveArtBonus(98,Bonus::LAND_MOVEMENT,+600);//Boots of Speed
  584. giveArtBonus(99,Bonus::STACKS_SPEED,+2);//Cape of Velocity
  585. giveArtBonus(100,Bonus::SPELL_IMMUNITY,0,59);//Pendant of Dispassion
  586. giveArtBonus(101,Bonus::SPELL_IMMUNITY,0,62);//Pendant of Second Sight
  587. giveArtBonus(102,Bonus::SPELL_IMMUNITY,0,42);//Pendant of Holiness
  588. giveArtBonus(103,Bonus::SPELL_IMMUNITY,0,24);//Pendant of Life
  589. giveArtBonus(104,Bonus::SPELL_IMMUNITY,0,25);//Pendant of Death
  590. giveArtBonus(105,Bonus::SPELL_IMMUNITY,0,60);//Pendant of Free Will
  591. giveArtBonus(106,Bonus::SPELL_IMMUNITY,0,17);//Pendant of Negativity
  592. giveArtBonus(107,Bonus::SPELL_IMMUNITY,0,61);//Pendant of Total Recall
  593. giveArtBonus(108,Bonus::MORALE,+3);//Pendant of Courage
  594. giveArtBonus(108,Bonus::LUCK,+3);//Pendant of Courage
  595. giveArtBonus(109,Bonus::GENERATE_RESOURCE,+1,4); //Everflowing Crystal Cloak
  596. giveArtBonus(110,Bonus::GENERATE_RESOURCE,+1,5); //Ring of Infinite Gems
  597. giveArtBonus(111,Bonus::GENERATE_RESOURCE,+1,1); //Everpouring Vial of Mercury
  598. giveArtBonus(112,Bonus::GENERATE_RESOURCE,+1,2); //Inexhaustible Cart of Ore
  599. giveArtBonus(113,Bonus::GENERATE_RESOURCE,+1,3); //Eversmoking Ring of Sulfur
  600. giveArtBonus(114,Bonus::GENERATE_RESOURCE,+1,0); //Inexhaustible Cart of Lumber
  601. giveArtBonus(115,Bonus::GENERATE_RESOURCE,+1000,6); //Endless Sack of Gold
  602. giveArtBonus(116,Bonus::GENERATE_RESOURCE,+750,6); //Endless Bag of Gold
  603. giveArtBonus(117,Bonus::GENERATE_RESOURCE,+500,6); //Endless Purse of Gold
  604. giveArtBonus(118,Bonus::CREATURE_GROWTH,+5,1); //Legs of Legion
  605. giveArtBonus(119,Bonus::CREATURE_GROWTH,+4,2); //Loins of Legion
  606. giveArtBonus(120,Bonus::CREATURE_GROWTH,+3,3); //Torso of Legion
  607. giveArtBonus(121,Bonus::CREATURE_GROWTH,+2,4); //Arms of Legion
  608. giveArtBonus(122,Bonus::CREATURE_GROWTH,+1,5); //Head of Legion
  609. //Sea Captain's Hat
  610. giveArtBonus(123,Bonus::WHIRLPOOL_PROTECTION,0);
  611. giveArtBonus(123,Bonus::SEA_MOVEMENT,+500);
  612. giveArtBonus(123,Bonus::SPELL,3,0, Bonus::INDEPENDENT_MAX);
  613. giveArtBonus(123,Bonus::SPELL,3,1, Bonus::INDEPENDENT_MAX);
  614. giveArtBonus(124,Bonus::SPELLS_OF_LEVEL,3,1); //Spellbinder's Hat
  615. giveArtBonus(125,Bonus::ENEMY_CANT_ESCAPE,0); //Shackles of War
  616. giveArtBonus(126,Bonus::BLOCK_SPELLS_ABOVE_LEVEL,0,-1,Bonus::INDEPENDENT_MIN);//Orb of Inhibition
  617. //vial of dragon blood
  618. giveArtBonus(127, Bonus::PRIMARY_SKILL, +5, PrimarySkill::ATTACK, Bonus::BASE_NUMBER, new HasAnotherBonusLimiter(Bonus::DRAGON_NATURE));
  619. giveArtBonus(127, Bonus::PRIMARY_SKILL, +5, PrimarySkill::DEFENSE, Bonus::BASE_NUMBER, new HasAnotherBonusLimiter(Bonus::DRAGON_NATURE));
  620. //Armageddon's Blade
  621. giveArtBonus(128, Bonus::SPELL, 3, 26, Bonus::INDEPENDENT_MAX);
  622. giveArtBonus(128, Bonus::SPELL_IMMUNITY,0, 26);
  623. ART_ATTACK_AND_DEFENSE(128, +3);
  624. ART_PRIM_SKILL(128, 2, +3);
  625. ART_PRIM_SKILL(128, 3, +6);
  626. //Angelic Alliance
  627. giveArtBonus(129, Bonus::NONEVIL_ALIGNMENT_MIX, 0);
  628. giveArtBonus(129, Bonus::OPENING_BATTLE_SPELL, 10, 48); // Prayer
  629. //Cloak of the Undead King
  630. giveArtBonus(130, Bonus::IMPROVED_NECROMANCY, 0);
  631. //Elixir of Life
  632. giveArtBonus(131, Bonus::STACK_HEALTH, +25, -1, Bonus::PERCENT_TO_BASE);
  633. giveArtBonus(131, Bonus::HP_REGENERATION, +50);
  634. //Armor of the Damned
  635. giveArtBonus(132, Bonus::OPENING_BATTLE_SPELL, 50, 54); // Slow
  636. giveArtBonus(132, Bonus::OPENING_BATTLE_SPELL, 50, 47); // Disrupting Ray
  637. giveArtBonus(132, Bonus::OPENING_BATTLE_SPELL, 50, 45); // Weakness
  638. giveArtBonus(132, Bonus::OPENING_BATTLE_SPELL, 50, 52); // Misfortune
  639. // Statue of Legion - gives only 50% growth
  640. giveArtBonus(133, Bonus::CREATURE_GROWTH_PERCENT, 50, -1);
  641. //Power of the Dragon Father
  642. giveArtBonus(134, Bonus::LEVEL_SPELL_IMMUNITY, 4);
  643. //Titan's Thunder
  644. // FIXME: should also add a permanent spell book, somehow.
  645. giveArtBonus(135, Bonus::SPELL, 3, 57);
  646. //Admiral's Hat
  647. giveArtBonus(136, Bonus::FREE_SHIP_BOARDING, 0);
  648. //Bow of the Sharpshooter
  649. giveArtBonus(137, Bonus::NO_SHOTING_PENALTY, 0);
  650. giveArtBonus(137, Bonus::FREE_SHOOTING, 0);
  651. //Wizard's Well
  652. giveArtBonus(138, Bonus::FULL_MANA_REGENERATION, 0);
  653. //Ring of the Magi
  654. giveArtBonus(139, Bonus::SPELL_DURATION, +50);
  655. //Cornucopia
  656. giveArtBonus(140, Bonus::GENERATE_RESOURCE, +4, 1);
  657. giveArtBonus(140, Bonus::GENERATE_RESOURCE, +4, 3);
  658. giveArtBonus(140, Bonus::GENERATE_RESOURCE, +4, 4);
  659. giveArtBonus(140, Bonus::GENERATE_RESOURCE, +4, 5);
  660. }
  661. void CArtHandler::clear()
  662. {
  663. BOOST_FOREACH(CArtifact *art, artifacts)
  664. delete art;
  665. artifacts.clear();
  666. clearHlpLists();
  667. }
  668. // /**
  669. // * Locally equips an artifact to a hero's worn slots. Unequips an already present artifact.
  670. // * Does not test if the operation is legal.
  671. // * @param artifWorn A hero's set of worn artifacts.
  672. // * @param bonuses Optional list of bonuses to update.
  673. // */
  674. // void CArtHandler::equipArtifact( std::map<ui16, const CArtifact*> &artifWorn, ui16 slotID, const CArtifact* art ) const
  675. // {
  676. // unequipArtifact(artifWorn, slotID);
  677. //
  678. // if (art) //false when artifact is NULL -> slot set to empty
  679. // {
  680. // const CArtifact &artifact = *art;
  681. //
  682. // // Add artifact.
  683. // artifWorn[slotID] = art;
  684. //
  685. // // Add locks, in reverse order of being removed.
  686. // if (artifact.constituents != NULL)
  687. // {
  688. // bool destConsumed = false; // Determines which constituent that will be counted for together with the artifact.
  689. //
  690. // BOOST_FOREACH(ui32 constituentID, *artifact.constituents)
  691. // {
  692. // const CArtifact &constituent = *artifacts[constituentID];
  693. //
  694. // if (!destConsumed && vstd::contains(constituent.possibleSlots, slotID))
  695. // {
  696. // destConsumed = true;
  697. // }
  698. // else
  699. // {
  700. // BOOST_FOREACH(ui16 slot, constituent.possibleSlots)
  701. // {
  702. // if (!vstd::contains(artifWorn, slot))
  703. // {
  704. // artifWorn[slot] = VLC->arth->artifacts[145]; //lock
  705. // break;
  706. // }
  707. // }
  708. // }
  709. // }
  710. // }
  711. // }
  712. // }
  713. //
  714. // /**
  715. // * Locally unequips an artifact from a hero's worn slots.
  716. // * Does not test if the operation is legal.
  717. // * @param artifWorn A hero's set of worn artifacts.
  718. // * @param bonuses Optional list of bonuses to update.
  719. // */
  720. // void CArtHandler::unequipArtifact(std::map<ui16, const CArtifact*> &artifWorn, ui16 slotID) const
  721. // {
  722. // if (!vstd::contains(artifWorn, slotID))
  723. // return;
  724. //
  725. // const CArtifact &artifact = *artifWorn[slotID];
  726. //
  727. // // Remove artifact, if it's not already removed.
  728. // artifWorn.erase(slotID);
  729. //
  730. // // Remove locks, in reverse order of being added.
  731. // if (artifact.constituents != NULL)
  732. // {
  733. // bool destConsumed = false;
  734. //
  735. // BOOST_FOREACH(ui32 constituentID, *artifact.constituents)
  736. // {
  737. // const CArtifact &constituent = *artifacts[constituentID];
  738. //
  739. // if (!destConsumed && vstd::contains(constituent.possibleSlots, slotID))
  740. // {
  741. // destConsumed = true;
  742. // }
  743. // else
  744. // {
  745. // BOOST_REVERSE_FOREACH(ui16 slot, constituent.possibleSlots)
  746. // {
  747. // if (vstd::contains(artifWorn, slot) && artifWorn[slot]->id == 145)
  748. // {
  749. // artifWorn.erase(slot);
  750. // break;
  751. // }
  752. // }
  753. // }
  754. // }
  755. // }
  756. // }
  757. void CArtHandler::clearHlpLists()
  758. {
  759. treasures.clear();
  760. minors.clear();
  761. majors.clear();
  762. relics.clear();
  763. }
  764. void CArtHandler::initAllowedArtifactsList(const std::vector<ui8> &allowed)
  765. {
  766. allowedArtifacts.clear();
  767. clearHlpLists();
  768. for (int i=0; i<144; ++i) //yes, 144
  769. {
  770. if (allowed[i])
  771. allowedArtifacts.push_back(artifacts[i]);
  772. }
  773. }
  774. CArtifactInstance::CArtifactInstance()
  775. {
  776. init();
  777. }
  778. CArtifactInstance::CArtifactInstance( CArtifact *Art)
  779. {
  780. init();
  781. setType(Art);
  782. }
  783. // CArtifactInstance::CArtifactInstance(int aid)
  784. // {
  785. // init();
  786. // setType(VLC->arth->artifacts[aid]);
  787. // }
  788. void CArtifactInstance::setType( CArtifact *Art )
  789. {
  790. artType = Art;
  791. attachTo(Art);
  792. }
  793. std::string CArtifactInstance::nodeName() const
  794. {
  795. return "Artifact instance of " + (artType ? artType->Name() : std::string("uninitialized")) + " type";
  796. }
  797. CArtifactInstance * CArtifactInstance::createScroll( const CSpell *s)
  798. {
  799. CArtifactInstance *ret = new CArtifactInstance(VLC->arth->artifacts[1]);
  800. Bonus *b = new Bonus(Bonus::PERMANENT, Bonus::SPELL, Bonus::ARTIFACT_INSTANCE, -1, 1, s->id);
  801. ret->addNewBonus(b);
  802. return ret;
  803. }
  804. void CArtifactInstance::init()
  805. {
  806. id = -1;
  807. }
  808. int CArtifactInstance::firstAvailableSlot(const CGHeroInstance *h) const
  809. {
  810. BOOST_FOREACH(ui16 slot, artType->possibleSlots)
  811. {
  812. if(canBePutAt(ArtifactLocation(h, slot))) //if(artType->fitsAt(h->artifWorn, slot))
  813. {
  814. //we've found a free suitable slot.
  815. return slot;
  816. }
  817. }
  818. //if haven't find proper slot, use backpack
  819. return firstBackpackSlot(h);
  820. }
  821. int CArtifactInstance::firstBackpackSlot(const CGHeroInstance *h) const
  822. {
  823. if(!artType->isBig()) //discard big artifact
  824. return Arts::BACKPACK_START + h->artifactsInBackpack.size();
  825. return -1;
  826. }
  827. bool CArtifactInstance::canBePutAt(const ArtifactLocation &al, bool assumeDestRemoved /*= false*/) const
  828. {
  829. if(al.slot >= Arts::BACKPACK_START)
  830. {
  831. if(artType->isBig())
  832. return false;
  833. //TODO backpack limit
  834. return true;
  835. }
  836. if(!vstd::contains(artType->possibleSlots, al.slot))
  837. return false;
  838. return al.hero->isPositionFree(al.slot, assumeDestRemoved);
  839. }
  840. void CArtifactInstance::putAt(CGHeroInstance *h, ui16 slot)
  841. {
  842. assert(canBePutAt(ArtifactLocation(h, slot)));
  843. h->setNewArtSlot(slot, this, false);
  844. if(slot < Arts::BACKPACK_START)
  845. h->attachTo(this);
  846. }
  847. void CArtifactInstance::removeFrom(CGHeroInstance *h, ui16 slot)
  848. {
  849. assert(h->CArtifactSet::getArt(slot) == this);
  850. h->eraseArtSlot(slot);
  851. if(slot < Arts::BACKPACK_START)
  852. h->detachFrom(this);
  853. //TODO delete me?
  854. }
  855. bool CArtifactInstance::canBeDisassembled() const
  856. {
  857. return artType->constituents && artType->constituentOf->size();
  858. }
  859. std::vector<const CArtifact *> CArtifactInstance::assemblyPossibilities(const CGHeroInstance *h) const
  860. {
  861. std::vector<const CArtifact *> ret;
  862. if(!artType->constituentOf //not a part of combined artifact
  863. || artType->constituents) //combined artifact already: no combining of combined artifacts... for now.
  864. return ret;
  865. BOOST_FOREACH(ui32 possibleCombinedArt, *artType->constituentOf)
  866. {
  867. const CArtifact * const artifact = VLC->arth->artifacts[possibleCombinedArt];
  868. assert(artifact->constituents);
  869. bool possible = true;
  870. BOOST_FOREACH(ui32 constituentID, *artifact->constituents) //check if all constituents are available
  871. {
  872. if(!h->hasArt(constituentID, true)) //constituent must be equipped
  873. {
  874. possible = false;
  875. break;
  876. }
  877. }
  878. if(possible)
  879. ret.push_back(artifact);
  880. }
  881. return ret;
  882. }
  883. void CArtifactInstance::move(ArtifactLocation &src, ArtifactLocation &dst)
  884. {
  885. removeFrom(src.hero, src.slot);
  886. putAt(dst.hero, dst.slot);
  887. }
  888. CArtifactInstance * CArtifactInstance::createNewArtifactInstance(CArtifact *Art)
  889. {
  890. if(!Art->constituents)
  891. return new CArtifactInstance(Art);
  892. else
  893. {
  894. CCombinedArtifactInstance * ret = new CCombinedArtifactInstance(Art);
  895. ret->createConstituents();
  896. return ret;
  897. }
  898. }
  899. CArtifactInstance * CArtifactInstance::createNewArtifactInstance(int aid)
  900. {
  901. return createNewArtifactInstance(VLC->arth->artifacts[aid]);
  902. }
  903. void CArtifactInstance::deserializationFix()
  904. {
  905. setType(artType);
  906. }
  907. bool CCombinedArtifactInstance::canBePutAt(const ArtifactLocation &al, bool assumeDestRemoved /*= false*/) const
  908. {
  909. bool canMainArtifactBePlaced = CArtifactInstance::canBePutAt(al, assumeDestRemoved);
  910. if(!canMainArtifactBePlaced)
  911. return false; //no is no...
  912. if(al.slot >= Arts::BACKPACK_START)
  913. return true; //we can always remove combined art to the backapck
  914. assert(artType->constituents);
  915. std::vector<ConstituentInfo> constituentsToBePlaced = constituentsInfo; //we'll remove constituents from that list, as we find a suitable slot for them
  916. //we iterate over all active slots and check if constituents fits them
  917. for (int i = 0; i < Arts::BACKPACK_START; i++)
  918. {
  919. for(std::vector<ConstituentInfo>::iterator art = constituentsToBePlaced.begin(); art != constituentsToBePlaced.end(); art++)
  920. {
  921. 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
  922. {
  923. constituentsToBePlaced.erase(art);
  924. break;
  925. }
  926. }
  927. }
  928. return constituentsToBePlaced.empty();
  929. }
  930. bool CCombinedArtifactInstance::canBeDisassembled() const
  931. {
  932. return true;
  933. }
  934. CCombinedArtifactInstance::CCombinedArtifactInstance(CArtifact *Art)
  935. : CArtifactInstance(Art)
  936. {
  937. }
  938. CCombinedArtifactInstance::CCombinedArtifactInstance()
  939. {
  940. }
  941. void CCombinedArtifactInstance::createConstituents()
  942. {
  943. assert(artType);
  944. assert(artType->constituents);
  945. BOOST_FOREACH(ui32 a, *artType->constituents)
  946. {
  947. addAsConstituent(CArtifactInstance::createNewArtifactInstance(a), -1);
  948. }
  949. }
  950. void CCombinedArtifactInstance::addAsConstituent(CArtifactInstance *art, int slot)
  951. {
  952. assert(vstd::contains(*artType->constituents, art->artType->id));
  953. assert(art->parents.size() == 1 && art->parents.front() == art->artType);
  954. constituentsInfo.push_back(ConstituentInfo(art, slot));
  955. attachTo(art);
  956. }
  957. void CCombinedArtifactInstance::putAt(CGHeroInstance *h, ui16 slot)
  958. {
  959. if(slot >= Arts::BACKPACK_START)
  960. {
  961. CArtifactInstance::putAt(h, slot);
  962. BOOST_FOREACH(ConstituentInfo &ci, constituentsInfo)
  963. ci.slot = -1;
  964. }
  965. else
  966. {
  967. CArtifactInstance *mainConstituent = figureMainConstituent(slot); //it'll be replaced with combined artifact, not a lock
  968. CArtifactInstance::putAt(h, slot); //puts combined art (this)
  969. BOOST_FOREACH(ConstituentInfo &ci, constituentsInfo)
  970. {
  971. if(ci.art != mainConstituent)
  972. {
  973. int pos = -1;
  974. if(isbetw(ci.slot, 0, Arts::BACKPACK_START) && ci.art->canBePutAt(ArtifactLocation(h, ci.slot))) //there is a valid suggestion where to place lock
  975. pos = ci.slot;
  976. else
  977. ci.slot = pos = ci.art->firstAvailableSlot(h);
  978. assert(pos < Arts::BACKPACK_START);
  979. h->setNewArtSlot(pos, ci.art, true); //sets as lock
  980. }
  981. else
  982. {
  983. ci.slot = -1;
  984. }
  985. }
  986. }
  987. }
  988. void CCombinedArtifactInstance::removeFrom(CGHeroInstance *h, ui16 slot)
  989. {
  990. if(slot >= Arts::BACKPACK_START)
  991. {
  992. CArtifactInstance::removeFrom(h, slot);
  993. }
  994. else
  995. {
  996. BOOST_FOREACH(ConstituentInfo &ci, constituentsInfo)
  997. {
  998. if(ci.slot >= 0)
  999. {
  1000. h->eraseArtSlot(ci.slot);
  1001. ci.slot = -1;
  1002. }
  1003. else
  1004. {
  1005. //main constituent
  1006. CArtifactInstance::removeFrom(h, slot);
  1007. }
  1008. }
  1009. }
  1010. }
  1011. CArtifactInstance * CCombinedArtifactInstance::figureMainConstituent(ui16 slot)
  1012. {
  1013. CArtifactInstance *mainConstituent = NULL; //it'll be replaced with combined artifact, not a lock
  1014. BOOST_FOREACH(ConstituentInfo &ci, constituentsInfo)
  1015. if(ci.slot == slot)
  1016. mainConstituent = ci.art;
  1017. if(!mainConstituent)
  1018. {
  1019. BOOST_FOREACH(ConstituentInfo &ci, constituentsInfo)
  1020. {
  1021. if(vstd::contains(ci.art->artType->possibleSlots, slot))
  1022. {
  1023. mainConstituent = ci.art;
  1024. }
  1025. }
  1026. }
  1027. return mainConstituent;
  1028. }
  1029. void CCombinedArtifactInstance::deserializationFix()
  1030. {
  1031. BOOST_FOREACH(ConstituentInfo &ci, constituentsInfo)
  1032. attachTo(ci.art);
  1033. }
  1034. CCombinedArtifactInstance::ConstituentInfo::ConstituentInfo(CArtifactInstance *Art /*= NULL*/, ui16 Slot /*= -1*/)
  1035. {
  1036. art = Art;
  1037. slot = Slot;
  1038. }