CArtHandler.cpp 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469
  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. setNodeType(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 available 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 available 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 available 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 || type == Bonus::LUCK)
  465. added->description = artifacts[aid]->Name() + (val > 0 ? " +" : " ") + boost::lexical_cast<std::string>(val);
  466. else
  467. added->description = artifacts[aid]->Name();
  468. artifacts[aid]->addNewBonus(added);
  469. }
  470. void CArtHandler::giveArtBonus(int aid, Bonus::BonusType type, int val, int subtype, IPropagator* propagator /*= NULL*/)
  471. {
  472. Bonus *added = new Bonus(Bonus::PERMANENT,type,Bonus::ARTIFACT,val,aid,subtype);
  473. added->valType = Bonus::BASE_NUMBER;
  474. added->propagator.reset(propagator);
  475. if(type == Bonus::MORALE || type == Bonus::LUCK)
  476. added->description = artifacts[aid]->Name() + (val > 0 ? " +" : " ") + boost::lexical_cast<std::string>(val);
  477. else
  478. added->description = artifacts[aid]->Name();
  479. artifacts[aid]->addNewBonus(added);
  480. }
  481. void CArtHandler::addBonuses()
  482. {
  483. #define ART_PRIM_SKILL(ID, whichSkill, val) giveArtBonus(ID,Bonus::PRIMARY_SKILL,val,whichSkill)
  484. #define ART_MORALE(ID, val) giveArtBonus(ID,Bonus::MORALE,val)
  485. #define ART_LUCK(ID, val) giveArtBonus(ID,Bonus::LUCK,val)
  486. #define ART_MORALE_AND_LUCK(ID, val) giveArtBonus(ID,Bonus::MORALE_AND_LUCK,val)
  487. #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)
  488. #define ART_ATTACK_AND_DEFENSE(ID, val) ART_PRIM_SKILL(ID,0,val); ART_PRIM_SKILL(ID,1,val)
  489. #define ART_POWER_AND_KNOWLEDGE(ID, val) ART_PRIM_SKILL(ID,2,val); ART_PRIM_SKILL(ID,3,val)
  490. //Attack bonus artifacts (Weapons)
  491. ART_PRIM_SKILL(7,0,+2); //Centaur Axe
  492. ART_PRIM_SKILL(8,0,+3); //Blackshard of the Dead Knight
  493. ART_PRIM_SKILL(9,0,+4); //Greater Gnoll's Flail
  494. ART_PRIM_SKILL(10,0,+5); //Ogre's Club of Havoc
  495. ART_PRIM_SKILL(11,0,+6); //Sword of Hellfire
  496. ART_PRIM_SKILL(12,0,+12); //Titan's Gladius
  497. ART_PRIM_SKILL(12,1,-3); //Titan's Gladius
  498. //Defense bonus artifacts (Shields)
  499. ART_PRIM_SKILL(13,1,+2); //Shield of the Dwarven Lords
  500. ART_PRIM_SKILL(14,1,+3); //Shield of the Yawning Dead
  501. ART_PRIM_SKILL(15,1,+4); //Buckler of the Gnoll King
  502. ART_PRIM_SKILL(16,1,+5); //Targ of the Rampaging Ogre
  503. ART_PRIM_SKILL(17,1,+6); //Shield of the Damned
  504. ART_PRIM_SKILL(18,1,+12); //Sentinel's Shield
  505. ART_PRIM_SKILL(18,0,-3); //Sentinel's Shield
  506. //Knowledge bonus artifacts (Helmets)
  507. ART_PRIM_SKILL(19,3,+1); //Helm of the Alabaster Unicorn
  508. ART_PRIM_SKILL(20,3,+2); //Skull Helmet
  509. ART_PRIM_SKILL(21,3,+3); //Helm of Chaos
  510. ART_PRIM_SKILL(22,3,+4); //Crown of the Supreme Magi
  511. ART_PRIM_SKILL(23,3,+5); //Hellstorm Helmet
  512. ART_PRIM_SKILL(24,3,+10); //Thunder Helmet
  513. ART_PRIM_SKILL(24,2,-2); //Thunder Helmet
  514. //Spell power bonus artifacts (Armours)
  515. ART_PRIM_SKILL(25,2,+1); //Breastplate of Petrified Wood
  516. ART_PRIM_SKILL(26,2,+2); //Rib Cage
  517. ART_PRIM_SKILL(27,2,+3); //Scales of the Greater Basilisk
  518. ART_PRIM_SKILL(28,2,+4); //Tunic of the Cyclops King
  519. ART_PRIM_SKILL(29,2,+5); //Breastplate of Brimstone
  520. ART_PRIM_SKILL(30,2,+10); //Titan's Cuirass
  521. ART_PRIM_SKILL(30,3,-2); //Titan's Cuirass
  522. //All primary skills (various)
  523. ART_ALL_PRIM_SKILLS(31,+1); //Armor of Wonder
  524. ART_ALL_PRIM_SKILLS(32,+2); //Sandals of the Saint
  525. ART_ALL_PRIM_SKILLS(33,+3); //Celestial Necklace of Bliss
  526. ART_ALL_PRIM_SKILLS(34,+4); //Lion's Shield of Courage
  527. ART_ALL_PRIM_SKILLS(35,+5); //Sword of Judgement
  528. ART_ALL_PRIM_SKILLS(36,+6); //Helm of Heavenly Enlightenment
  529. //Attack and Defense (various)
  530. ART_ATTACK_AND_DEFENSE(37,+1); //Quiet Eye of the Dragon
  531. ART_ATTACK_AND_DEFENSE(38,+2); //Red Dragon Flame Tongue
  532. ART_ATTACK_AND_DEFENSE(39,+3); //Dragon Scale Shield
  533. ART_ATTACK_AND_DEFENSE(40,+4); //Dragon Scale Armor
  534. //Spell power and Knowledge (various)
  535. ART_POWER_AND_KNOWLEDGE(41,+1); //Dragonbone Greaves
  536. ART_POWER_AND_KNOWLEDGE(42,+2); //Dragon Wing Tabard
  537. ART_POWER_AND_KNOWLEDGE(43,+3); //Necklace of Dragonteeth
  538. ART_POWER_AND_KNOWLEDGE(44,+4); //Crown of Dragontooth
  539. //Luck and morale
  540. ART_MORALE(45,+1); //Still Eye of the Dragon
  541. ART_LUCK(45,+1); //Still Eye of the Dragon
  542. ART_LUCK(46,+1); //Clover of Fortune
  543. ART_LUCK(47,+1); //Cards of Prophecy
  544. ART_LUCK(48,+1); //Ladybird of Luck
  545. ART_MORALE(49,+1); //Badge of Courage -> +1 morale and immunity to hostile mind spells:
  546. giveArtBonus(49,Bonus::SPELL_IMMUNITY,0,50);//sorrow
  547. giveArtBonus(49,Bonus::SPELL_IMMUNITY,0,59);//berserk
  548. giveArtBonus(49,Bonus::SPELL_IMMUNITY,0,60);//hypnotize
  549. giveArtBonus(49,Bonus::SPELL_IMMUNITY,0,61);//forgetfulness
  550. giveArtBonus(49,Bonus::SPELL_IMMUNITY,0,62);//blind
  551. ART_MORALE(50,+1); //Crest of Valor
  552. ART_MORALE(51,+1); //Glyph of Gallantry
  553. giveArtBonus(52,Bonus::SIGHT_RADIOUS,+1);//Speculum
  554. giveArtBonus(53,Bonus::SIGHT_RADIOUS,+1);//Spyglass
  555. //necromancy bonus
  556. giveArtBonus(54,Bonus::SECONDARY_SKILL_PREMY,+5, CGHeroInstance::NECROMANCY, Bonus::ADDITIVE_VALUE);//Amulet of the Undertaker
  557. giveArtBonus(55,Bonus::SECONDARY_SKILL_PREMY,+10, CGHeroInstance::NECROMANCY, Bonus::ADDITIVE_VALUE);//Vampire's Cowl
  558. giveArtBonus(56,Bonus::SECONDARY_SKILL_PREMY,+15, CGHeroInstance::NECROMANCY, Bonus::ADDITIVE_VALUE);//Dead Man's Boots
  559. giveArtBonus(57,Bonus::MAGIC_RESISTANCE,+5, 0);//Garniture of Interference
  560. giveArtBonus(58,Bonus::MAGIC_RESISTANCE,+10, 0);//Surcoat of Counterpoise
  561. giveArtBonus(59,Bonus::MAGIC_RESISTANCE,+15, 0);//Boots of Polarity
  562. //archery bonus
  563. giveArtBonus(60,Bonus::SECONDARY_SKILL_PREMY,+5, CGHeroInstance::ARCHERY, Bonus::ADDITIVE_VALUE);//Bow of Elven Cherrywood
  564. giveArtBonus(61,Bonus::SECONDARY_SKILL_PREMY,+10,CGHeroInstance::ARCHERY, Bonus::ADDITIVE_VALUE);//Bowstring of the Unicorn's Mane
  565. giveArtBonus(62,Bonus::SECONDARY_SKILL_PREMY,+15,CGHeroInstance::ARCHERY, Bonus::ADDITIVE_VALUE);//Angel Feather Arrows
  566. //eagle eye bonus
  567. giveArtBonus(63,Bonus::SECONDARY_SKILL_PREMY,+5, CGHeroInstance::EAGLE_EYE, Bonus::ADDITIVE_VALUE);//Bird of Perception
  568. giveArtBonus(64,Bonus::SECONDARY_SKILL_PREMY,+10, CGHeroInstance::EAGLE_EYE, Bonus::ADDITIVE_VALUE);//Stoic Watchman
  569. giveArtBonus(65,Bonus::SECONDARY_SKILL_PREMY,+15, CGHeroInstance::EAGLE_EYE, Bonus::ADDITIVE_VALUE);//Emblem of Cognizance
  570. //reducing cost of surrendering
  571. giveArtBonus(66,Bonus::SURRENDER_DISCOUNT,+10);//Statesman's Medal
  572. giveArtBonus(67,Bonus::SURRENDER_DISCOUNT,+10);//Diplomat's Ring
  573. giveArtBonus(68,Bonus::SURRENDER_DISCOUNT,+10);//Ambassador's Sash
  574. giveArtBonus(69,Bonus::STACKS_SPEED,+1);//Ring of the Wayfarer
  575. giveArtBonus(70,Bonus::LAND_MOVEMENT,+300);//Equestrian's Gloves
  576. giveArtBonus(71,Bonus::SEA_MOVEMENT,+1000);//Necklace of Ocean Guidance
  577. giveArtBonus(72,Bonus::FLYING_MOVEMENT, 0, 1);//Angel Wings
  578. giveArtBonus(73,Bonus::MANA_REGENERATION,+1);//Charm of Mana
  579. giveArtBonus(74,Bonus::MANA_REGENERATION,+2);//Talisman of Mana
  580. giveArtBonus(75,Bonus::MANA_REGENERATION,+3);//Mystic Orb of Mana
  581. giveArtBonus(76,Bonus::SPELL_DURATION,+1);//Collar of Conjuring
  582. giveArtBonus(77,Bonus::SPELL_DURATION,+2);//Ring of Conjuring
  583. giveArtBonus(78,Bonus::SPELL_DURATION,+3);//Cape of Conjuring
  584. giveArtBonus(79,Bonus::AIR_SPELL_DMG_PREMY,+50);//Orb of the Firmament
  585. giveArtBonus(80,Bonus::EARTH_SPELL_DMG_PREMY,+50);//Orb of Silt
  586. giveArtBonus(81,Bonus::FIRE_SPELL_DMG_PREMY,+50);//Orb of Tempestuous Fire
  587. giveArtBonus(82,Bonus::WATER_SPELL_DMG_PREMY,+50);//Orb of Driving Rain
  588. giveArtBonus(83,Bonus::LEVEL_SPELL_IMMUNITY,3,-1,Bonus::INDEPENDENT_MAX);//Recanter's Cloak
  589. giveArtBonus(84,Bonus::BLOCK_MORALE,0);//Spirit of Oppression
  590. giveArtBonus(85,Bonus::BLOCK_LUCK,0);//Hourglass of the Evil Hour
  591. giveArtBonus(86,Bonus::FIRE_SPELLS,0);//Tome of Fire Magic
  592. giveArtBonus(87,Bonus::AIR_SPELLS,0);//Tome of Air Magic
  593. giveArtBonus(88,Bonus::WATER_SPELLS,0);//Tome of Water Magic
  594. giveArtBonus(89,Bonus::EARTH_SPELLS,0);//Tome of Earth Magic
  595. giveArtBonus(90,Bonus::WATER_WALKING, 0, 1);//Boots of Levitation
  596. giveArtBonus(91,Bonus::NO_DISTANCE_PENALTY,0, 0, 0, new HasAnotherBonusLimiter(Bonus::SHOOTER));//Golden Bow
  597. giveArtBonus(92,Bonus::SPELL_IMMUNITY,0,35);//Sphere of Permanence
  598. giveArtBonus(93,Bonus::NEGATE_ALL_NATURAL_IMMUNITIES,0);//Orb of Vulnerability
  599. giveArtBonus(94,Bonus::STACK_HEALTH,+1);//Ring of Vitality
  600. giveArtBonus(95,Bonus::STACK_HEALTH,+1);//Ring of Life
  601. giveArtBonus(96,Bonus::STACK_HEALTH,+2);//Vial of Lifeblood
  602. giveArtBonus(97,Bonus::STACKS_SPEED,+1);//Necklace of Swiftness
  603. giveArtBonus(98,Bonus::LAND_MOVEMENT,+600);//Boots of Speed
  604. giveArtBonus(99,Bonus::STACKS_SPEED,+2);//Cape of Velocity
  605. giveArtBonus(100,Bonus::SPELL_IMMUNITY,0,59);//Pendant of Dispassion
  606. giveArtBonus(101,Bonus::SPELL_IMMUNITY,0,62);//Pendant of Second Sight
  607. giveArtBonus(102,Bonus::SPELL_IMMUNITY,0,42);//Pendant of Holiness
  608. giveArtBonus(103,Bonus::SPELL_IMMUNITY,0,24);//Pendant of Life
  609. giveArtBonus(104,Bonus::SPELL_IMMUNITY,0,25, 1, new HasAnotherBonusLimiter(Bonus::UNDEAD));//Pendant of Death does not display info for living stacks
  610. giveArtBonus(105,Bonus::SPELL_IMMUNITY,0,60);//Pendant of Free Will
  611. giveArtBonus(106,Bonus::SPELL_IMMUNITY,0,17);//Pendant of Negativity
  612. giveArtBonus(107,Bonus::SPELL_IMMUNITY,0,61);//Pendant of Total Recall
  613. giveArtBonus(108,Bonus::MORALE,+3);//Pendant of Courage
  614. giveArtBonus(108,Bonus::LUCK,+3);//Pendant of Courage
  615. giveArtBonus(109,Bonus::GENERATE_RESOURCE,+1,4); //Everflowing Crystal Cloak
  616. giveArtBonus(110,Bonus::GENERATE_RESOURCE,+1,5); //Ring of Infinite Gems
  617. giveArtBonus(111,Bonus::GENERATE_RESOURCE,+1,1); //Everpouring Vial of Mercury
  618. giveArtBonus(112,Bonus::GENERATE_RESOURCE,+1,2); //Inexhaustible Cart of Ore
  619. giveArtBonus(113,Bonus::GENERATE_RESOURCE,+1,3); //Eversmoking Ring of Sulfur
  620. giveArtBonus(114,Bonus::GENERATE_RESOURCE,+1,0); //Inexhaustible Cart of Lumber
  621. giveArtBonus(115,Bonus::GENERATE_RESOURCE,+1000, Res::GOLD); //Endless Sack of Gold
  622. giveArtBonus(116,Bonus::GENERATE_RESOURCE,+750, Res::GOLD); //Endless Bag of Gold
  623. giveArtBonus(117,Bonus::GENERATE_RESOURCE,+500, Res::GOLD); //Endless Purse of Gold
  624. giveArtBonus(118,Bonus::CREATURE_GROWTH,+5,1, new CPropagatorNodeType(CBonusSystemNode::TOWN_AND_VISITOR)); //Legs of Legion
  625. giveArtBonus(119,Bonus::CREATURE_GROWTH,+4,2, new CPropagatorNodeType(CBonusSystemNode::TOWN_AND_VISITOR)); //Loins of Legion
  626. giveArtBonus(120,Bonus::CREATURE_GROWTH,+3,3, new CPropagatorNodeType(CBonusSystemNode::TOWN_AND_VISITOR)); //Torso of Legion
  627. giveArtBonus(121,Bonus::CREATURE_GROWTH,+2,4, new CPropagatorNodeType(CBonusSystemNode::TOWN_AND_VISITOR)); //Arms of Legion
  628. giveArtBonus(122,Bonus::CREATURE_GROWTH,+1,5, new CPropagatorNodeType(CBonusSystemNode::TOWN_AND_VISITOR)); //Head of Legion
  629. //Sea Captain's Hat
  630. giveArtBonus(123,Bonus::WHIRLPOOL_PROTECTION,0);
  631. giveArtBonus(123,Bonus::SEA_MOVEMENT,+500);
  632. giveArtBonus(123,Bonus::SPELL,3,0, Bonus::INDEPENDENT_MAX);
  633. giveArtBonus(123,Bonus::SPELL,3,1, Bonus::INDEPENDENT_MAX);
  634. giveArtBonus(124,Bonus::SPELLS_OF_LEVEL,3,1); //Spellbinder's Hat
  635. giveArtBonus(125,Bonus::ENEMY_CANT_ESCAPE,0); //Shackles of War
  636. giveArtBonus(126,Bonus::LEVEL_SPELL_IMMUNITY,SPELL_LEVELS,-1,Bonus::INDEPENDENT_MAX);//Orb of Inhibition
  637. //vial of dragon blood
  638. giveArtBonus(127, Bonus::PRIMARY_SKILL, +5, PrimarySkill::ATTACK, Bonus::BASE_NUMBER, new HasAnotherBonusLimiter(Bonus::DRAGON_NATURE));
  639. giveArtBonus(127, Bonus::PRIMARY_SKILL, +5, PrimarySkill::DEFENSE, Bonus::BASE_NUMBER, new HasAnotherBonusLimiter(Bonus::DRAGON_NATURE));
  640. //Armageddon's Blade
  641. giveArtBonus(128, Bonus::SPELL, 3, 26, Bonus::INDEPENDENT_MAX);
  642. giveArtBonus(128, Bonus::SPELL_IMMUNITY,0, 26);
  643. ART_ATTACK_AND_DEFENSE(128, +3);
  644. ART_PRIM_SKILL(128, 2, +3);
  645. ART_PRIM_SKILL(128, 3, +6);
  646. //Angelic Alliance
  647. giveArtBonus(129, Bonus::NONEVIL_ALIGNMENT_MIX, 0);
  648. giveArtBonus(129, Bonus::OPENING_BATTLE_SPELL, 10, 48); // Prayer
  649. //Cloak of the Undead King
  650. giveArtBonus(130, Bonus::IMPROVED_NECROMANCY, 0);
  651. //Elixir of Life
  652. giveArtBonus(131, Bonus::STACK_HEALTH, +25, -1, Bonus::PERCENT_TO_BASE);
  653. giveArtBonus(131, Bonus::HP_REGENERATION, +50);
  654. //Armor of the Damned
  655. giveArtBonus(132, Bonus::OPENING_BATTLE_SPELL, 50, 54); // Slow
  656. giveArtBonus(132, Bonus::OPENING_BATTLE_SPELL, 50, 47); // Disrupting Ray
  657. giveArtBonus(132, Bonus::OPENING_BATTLE_SPELL, 50, 45); // Weakness
  658. giveArtBonus(132, Bonus::OPENING_BATTLE_SPELL, 50, 52); // Misfortune
  659. // Statue of Legion - gives only 50% growth
  660. giveArtBonus(133, Bonus::CREATURE_GROWTH_PERCENT, 50, -1, new CPropagatorNodeType(CBonusSystemNode::PLAYER));
  661. //Power of the Dragon Father
  662. giveArtBonus(134, Bonus::LEVEL_SPELL_IMMUNITY, 4, -1, Bonus::INDEPENDENT_MAX);
  663. //Titan's Thunder
  664. // FIXME: should also add a permanent spell book, somehow.
  665. giveArtBonus(135, Bonus::SPELL, 3, 57);
  666. //Admiral's Hat
  667. giveArtBonus(136, Bonus::FREE_SHIP_BOARDING, 0);
  668. //Bow of the Sharpshooter
  669. giveArtBonus(137, Bonus::NO_DISTANCE_PENALTY, 0, 0, 0, new HasAnotherBonusLimiter(Bonus::SHOOTER));
  670. giveArtBonus(137, Bonus::NO_OBSTACLES_PENALTY, 0, 0, 0, new HasAnotherBonusLimiter(Bonus::SHOOTER));
  671. giveArtBonus(137, Bonus::FREE_SHOOTING, 0, 0, 0, new HasAnotherBonusLimiter(Bonus::SHOOTER));
  672. //Wizard's Well
  673. giveArtBonus(138, Bonus::FULL_MANA_REGENERATION, 0);
  674. //Ring of the Magi
  675. giveArtBonus(139, Bonus::SPELL_DURATION, +50);
  676. //Cornucopia
  677. giveArtBonus(140, Bonus::GENERATE_RESOURCE, +4, Res::MERCURY);
  678. giveArtBonus(140, Bonus::GENERATE_RESOURCE, +4, Res::SULFUR);
  679. giveArtBonus(140, Bonus::GENERATE_RESOURCE, +4, Res::CRYSTAL);
  680. giveArtBonus(140, Bonus::GENERATE_RESOURCE, +4, Res::GEMS);
  681. }
  682. void CArtHandler::clear()
  683. {
  684. BOOST_FOREACH(CArtifact *art, artifacts)
  685. delete art;
  686. artifacts.clear();
  687. clearHlpLists();
  688. }
  689. // /**
  690. // * Locally equips an artifact to a hero's worn slots. Unequips an already present artifact.
  691. // * Does not test if the operation is legal.
  692. // * @param artifWorn A hero's set of worn artifacts.
  693. // * @param bonuses Optional list of bonuses to update.
  694. // */
  695. // void CArtHandler::equipArtifact( std::map<ui16, const CArtifact*> &artifWorn, ui16 slotID, const CArtifact* art ) const
  696. // {
  697. // unequipArtifact(artifWorn, slotID);
  698. //
  699. // if (art) //false when artifact is NULL -> slot set to empty
  700. // {
  701. // const CArtifact &artifact = *art;
  702. //
  703. // // Add artifact.
  704. // artifWorn[slotID] = art;
  705. //
  706. // // Add locks, in reverse order of being removed.
  707. // if (artifact.constituents != NULL)
  708. // {
  709. // bool destConsumed = false; // Determines which constituent that will be counted for together with the artifact.
  710. //
  711. // BOOST_FOREACH(ui32 constituentID, *artifact.constituents)
  712. // {
  713. // const CArtifact &constituent = *artifacts[constituentID];
  714. //
  715. // if (!destConsumed && vstd::contains(constituent.possibleSlots, slotID))
  716. // {
  717. // destConsumed = true;
  718. // }
  719. // else
  720. // {
  721. // BOOST_FOREACH(ui16 slot, constituent.possibleSlots)
  722. // {
  723. // if (!vstd::contains(artifWorn, slot))
  724. // {
  725. // artifWorn[slot] = VLC->arth->artifacts[145]; //lock
  726. // break;
  727. // }
  728. // }
  729. // }
  730. // }
  731. // }
  732. // }
  733. // }
  734. //
  735. // /**
  736. // * Locally unequips an artifact from a hero's worn slots.
  737. // * Does not test if the operation is legal.
  738. // * @param artifWorn A hero's set of worn artifacts.
  739. // * @param bonuses Optional list of bonuses to update.
  740. // */
  741. // void CArtHandler::unequipArtifact(std::map<ui16, const CArtifact*> &artifWorn, ui16 slotID) const
  742. // {
  743. // if (!vstd::contains(artifWorn, slotID))
  744. // return;
  745. //
  746. // const CArtifact &artifact = *artifWorn[slotID];
  747. //
  748. // // Remove artifact, if it's not already removed.
  749. // artifWorn.erase(slotID);
  750. //
  751. // // Remove locks, in reverse order of being added.
  752. // if (artifact.constituents != NULL)
  753. // {
  754. // bool destConsumed = false;
  755. //
  756. // BOOST_FOREACH(ui32 constituentID, *artifact.constituents)
  757. // {
  758. // const CArtifact &constituent = *artifacts[constituentID];
  759. //
  760. // if (!destConsumed && vstd::contains(constituent.possibleSlots, slotID))
  761. // {
  762. // destConsumed = true;
  763. // }
  764. // else
  765. // {
  766. // BOOST_REVERSE_FOREACH(ui16 slot, constituent.possibleSlots)
  767. // {
  768. // if (vstd::contains(artifWorn, slot) && artifWorn[slot]->id == 145)
  769. // {
  770. // artifWorn.erase(slot);
  771. // break;
  772. // }
  773. // }
  774. // }
  775. // }
  776. // }
  777. // }
  778. void CArtHandler::clearHlpLists()
  779. {
  780. treasures.clear();
  781. minors.clear();
  782. majors.clear();
  783. relics.clear();
  784. }
  785. void CArtHandler::initAllowedArtifactsList(const std::vector<ui8> &allowed)
  786. {
  787. allowedArtifacts.clear();
  788. clearHlpLists();
  789. for (int i=0; i<144; ++i) //yes, 144
  790. {
  791. if (allowed[i])
  792. allowedArtifacts.push_back(artifacts[i]);
  793. }
  794. }
  795. CArtifactInstance::CArtifactInstance()
  796. {
  797. init();
  798. }
  799. CArtifactInstance::CArtifactInstance( CArtifact *Art)
  800. {
  801. init();
  802. setType(Art);
  803. }
  804. // CArtifactInstance::CArtifactInstance(int aid)
  805. // {
  806. // init();
  807. // setType(VLC->arth->artifacts[aid]);
  808. // }
  809. void CArtifactInstance::setType( CArtifact *Art )
  810. {
  811. artType = Art;
  812. attachTo(Art);
  813. }
  814. std::string CArtifactInstance::nodeName() const
  815. {
  816. return "Artifact instance of " + (artType ? artType->Name() : std::string("uninitialized")) + " type";
  817. }
  818. CArtifactInstance * CArtifactInstance::createScroll( const CSpell *s)
  819. {
  820. CArtifactInstance *ret = new CArtifactInstance(VLC->arth->artifacts[1]);
  821. Bonus *b = new Bonus(Bonus::PERMANENT, Bonus::SPELL, Bonus::ARTIFACT_INSTANCE, -1, 1, s->id);
  822. ret->addNewBonus(b);
  823. return ret;
  824. }
  825. void CArtifactInstance::init()
  826. {
  827. id = -1;
  828. setNodeType(ARTIFACT_INSTANCE);
  829. }
  830. int CArtifactInstance::firstAvailableSlot(const CGHeroInstance *h) const
  831. {
  832. BOOST_FOREACH(ui16 slot, artType->possibleSlots)
  833. {
  834. if(canBePutAt(ArtifactLocation(h, slot))) //if(artType->fitsAt(h->artifWorn, slot))
  835. {
  836. //we've found a free suitable slot.
  837. return slot;
  838. }
  839. }
  840. //if haven't find proper slot, use backpack
  841. return firstBackpackSlot(h);
  842. }
  843. int CArtifactInstance::firstBackpackSlot(const CGHeroInstance *h) const
  844. {
  845. if(!artType->isBig()) //discard big artifact
  846. return Arts::BACKPACK_START + h->artifactsInBackpack.size();
  847. return -1;
  848. }
  849. bool CArtifactInstance::canBePutAt(const ArtifactLocation &al, bool assumeDestRemoved /*= false*/) const
  850. {
  851. if(al.slot >= Arts::BACKPACK_START)
  852. {
  853. if(artType->isBig())
  854. return false;
  855. //TODO backpack limit
  856. return true;
  857. }
  858. if(!vstd::contains(artType->possibleSlots, al.slot))
  859. return false;
  860. return al.hero->isPositionFree(al.slot, assumeDestRemoved);
  861. }
  862. void CArtifactInstance::putAt(CGHeroInstance *h, ui16 slot)
  863. {
  864. assert(canBePutAt(ArtifactLocation(h, slot)));
  865. h->setNewArtSlot(slot, this, false);
  866. if(slot < Arts::BACKPACK_START)
  867. h->attachTo(this);
  868. }
  869. void CArtifactInstance::removeFrom(CGHeroInstance *h, ui16 slot)
  870. {
  871. assert(h->CArtifactSet::getArt(slot) == this);
  872. h->eraseArtSlot(slot);
  873. if(slot < Arts::BACKPACK_START)
  874. h->detachFrom(this);
  875. //TODO delete me?
  876. }
  877. bool CArtifactInstance::canBeDisassembled() const
  878. {
  879. return artType->constituents && artType->constituentOf->size();
  880. }
  881. std::vector<const CArtifact *> CArtifactInstance::assemblyPossibilities(const CGHeroInstance *h) const
  882. {
  883. std::vector<const CArtifact *> ret;
  884. if(!artType->constituentOf //not a part of combined artifact
  885. || artType->constituents) //combined artifact already: no combining of combined artifacts... for now.
  886. return ret;
  887. BOOST_FOREACH(ui32 possibleCombinedArt, *artType->constituentOf)
  888. {
  889. const CArtifact * const artifact = VLC->arth->artifacts[possibleCombinedArt];
  890. assert(artifact->constituents);
  891. bool possible = true;
  892. BOOST_FOREACH(ui32 constituentID, *artifact->constituents) //check if all constituents are available
  893. {
  894. if(!h->hasArt(constituentID, true)) //constituent must be equipped
  895. {
  896. possible = false;
  897. break;
  898. }
  899. }
  900. if(possible)
  901. ret.push_back(artifact);
  902. }
  903. return ret;
  904. }
  905. void CArtifactInstance::move(ArtifactLocation &src, ArtifactLocation &dst)
  906. {
  907. removeFrom(src.hero, src.slot);
  908. putAt(dst.hero, dst.slot);
  909. if (artType->id == 135 && dst.slot == Arts::RIGHT_HAND && !dst.hero->hasSpellbook()) //Titan's Thunder creates new spellbook on equip
  910. dst.hero->giveArtifact(0);
  911. }
  912. CArtifactInstance * CArtifactInstance::createNewArtifactInstance(CArtifact *Art)
  913. {
  914. if(!Art->constituents)
  915. return new CArtifactInstance(Art);
  916. else
  917. {
  918. CCombinedArtifactInstance * ret = new CCombinedArtifactInstance(Art);
  919. ret->createConstituents();
  920. return ret;
  921. }
  922. }
  923. CArtifactInstance * CArtifactInstance::createNewArtifactInstance(int aid)
  924. {
  925. return createNewArtifactInstance(VLC->arth->artifacts[aid]);
  926. }
  927. void CArtifactInstance::deserializationFix()
  928. {
  929. setType(artType);
  930. }
  931. int CArtifactInstance::getGivenSpellID() const
  932. {
  933. const Bonus * b = getBonus(Selector::type(Bonus::SPELL));
  934. if(!b)
  935. {
  936. tlog3 << "Warning: " << nodeName() << " doesn't bear any spell!\n";
  937. return -1;
  938. }
  939. return b->subtype;
  940. }
  941. bool CArtifactInstance::isPart(const CArtifactInstance *supposedPart) const
  942. {
  943. return supposedPart == this;
  944. }
  945. bool CCombinedArtifactInstance::canBePutAt(const ArtifactLocation &al, bool assumeDestRemoved /*= false*/) const
  946. {
  947. bool canMainArtifactBePlaced = CArtifactInstance::canBePutAt(al, assumeDestRemoved);
  948. if(!canMainArtifactBePlaced)
  949. return false; //no is no...
  950. if(al.slot >= Arts::BACKPACK_START)
  951. return true; //we can always remove combined art to the backapck
  952. assert(artType->constituents);
  953. std::vector<ConstituentInfo> constituentsToBePlaced = constituentsInfo; //we'll remove constituents from that list, as we find a suitable slot for them
  954. //it may be that we picked a combined artifact in hero screen (though technically it's still there) to move it
  955. //so we remove from the list all constituents that are already present on dst hero in the form of locks
  956. BOOST_FOREACH(const ConstituentInfo &constituent, constituentsInfo)
  957. {
  958. if(constituent.art == al.hero->getArt(constituent.slot, false)) //no need to worry about locked constituent
  959. constituentsToBePlaced -= constituent;
  960. }
  961. //we iterate over all active slots and check if constituents fits them
  962. for (int i = 0; i < Arts::BACKPACK_START; i++)
  963. {
  964. for(std::vector<ConstituentInfo>::iterator art = constituentsToBePlaced.begin(); art != constituentsToBePlaced.end(); art++)
  965. {
  966. 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
  967. {
  968. constituentsToBePlaced.erase(art);
  969. break;
  970. }
  971. }
  972. }
  973. return constituentsToBePlaced.empty();
  974. }
  975. bool CCombinedArtifactInstance::canBeDisassembled() const
  976. {
  977. return true;
  978. }
  979. CCombinedArtifactInstance::CCombinedArtifactInstance(CArtifact *Art)
  980. : CArtifactInstance(Art) //TODO: seems unued, but need to be written
  981. {
  982. }
  983. CCombinedArtifactInstance::CCombinedArtifactInstance()
  984. {
  985. }
  986. void CCombinedArtifactInstance::createConstituents()
  987. {
  988. assert(artType);
  989. assert(artType->constituents);
  990. BOOST_FOREACH(ui32 a, *artType->constituents)
  991. {
  992. addAsConstituent(CArtifactInstance::createNewArtifactInstance(a), -1);
  993. }
  994. }
  995. void CCombinedArtifactInstance::addAsConstituent(CArtifactInstance *art, int slot)
  996. {
  997. assert(vstd::contains(*artType->constituents, art->artType->id));
  998. assert(art->getParentNodes().size() == 1 && art->getParentNodes().front() == art->artType);
  999. constituentsInfo.push_back(ConstituentInfo(art, slot));
  1000. attachTo(art);
  1001. }
  1002. void CCombinedArtifactInstance::putAt(CGHeroInstance *h, ui16 slot)
  1003. {
  1004. if(slot >= Arts::BACKPACK_START)
  1005. {
  1006. CArtifactInstance::putAt(h, slot);
  1007. BOOST_FOREACH(ConstituentInfo &ci, constituentsInfo)
  1008. ci.slot = -1;
  1009. }
  1010. else
  1011. {
  1012. CArtifactInstance *mainConstituent = figureMainConstituent(slot); //it'll be replaced with combined artifact, not a lock
  1013. CArtifactInstance::putAt(h, slot); //puts combined art (this)
  1014. BOOST_FOREACH(ConstituentInfo &ci, constituentsInfo)
  1015. {
  1016. if(ci.art != mainConstituent)
  1017. {
  1018. int pos = -1;
  1019. if(isbetw(ci.slot, 0, Arts::BACKPACK_START) && ci.art->canBePutAt(ArtifactLocation(h, ci.slot))) //there is a valid suggestion where to place lock
  1020. pos = ci.slot;
  1021. else
  1022. ci.slot = pos = ci.art->firstAvailableSlot(h);
  1023. assert(pos < Arts::BACKPACK_START);
  1024. h->setNewArtSlot(pos, ci.art, true); //sets as lock
  1025. }
  1026. else
  1027. {
  1028. ci.slot = -1;
  1029. }
  1030. }
  1031. }
  1032. }
  1033. void CCombinedArtifactInstance::removeFrom(CGHeroInstance *h, ui16 slot)
  1034. {
  1035. if(slot >= Arts::BACKPACK_START)
  1036. {
  1037. CArtifactInstance::removeFrom(h, slot);
  1038. }
  1039. else
  1040. {
  1041. BOOST_FOREACH(ConstituentInfo &ci, constituentsInfo)
  1042. {
  1043. if(ci.slot >= 0)
  1044. {
  1045. h->eraseArtSlot(ci.slot);
  1046. ci.slot = -1;
  1047. }
  1048. else
  1049. {
  1050. //main constituent
  1051. CArtifactInstance::removeFrom(h, slot);
  1052. }
  1053. }
  1054. }
  1055. }
  1056. CArtifactInstance * CCombinedArtifactInstance::figureMainConstituent(ui16 slot)
  1057. {
  1058. CArtifactInstance *mainConstituent = NULL; //it'll be replaced with combined artifact, not a lock
  1059. BOOST_FOREACH(ConstituentInfo &ci, constituentsInfo)
  1060. if(ci.slot == slot)
  1061. mainConstituent = ci.art;
  1062. if(!mainConstituent)
  1063. {
  1064. BOOST_FOREACH(ConstituentInfo &ci, constituentsInfo)
  1065. {
  1066. if(vstd::contains(ci.art->artType->possibleSlots, slot))
  1067. {
  1068. mainConstituent = ci.art;
  1069. }
  1070. }
  1071. }
  1072. return mainConstituent;
  1073. }
  1074. void CCombinedArtifactInstance::deserializationFix()
  1075. {
  1076. BOOST_FOREACH(ConstituentInfo &ci, constituentsInfo)
  1077. attachTo(ci.art);
  1078. }
  1079. bool CCombinedArtifactInstance::isPart(const CArtifactInstance *supposedPart) const
  1080. {
  1081. bool me = CArtifactInstance::isPart(supposedPart);
  1082. if(me)
  1083. return true;
  1084. //check for constituents
  1085. BOOST_FOREACH(const ConstituentInfo &constituent, constituentsInfo)
  1086. if(constituent.art == supposedPart)
  1087. return true;
  1088. return false;
  1089. }
  1090. CCombinedArtifactInstance::ConstituentInfo::ConstituentInfo(CArtifactInstance *Art /*= NULL*/, ui16 Slot /*= -1*/)
  1091. {
  1092. art = Art;
  1093. slot = Slot;
  1094. }
  1095. bool CCombinedArtifactInstance::ConstituentInfo::operator==(const ConstituentInfo &rhs) const
  1096. {
  1097. return art == rhs.art && slot == rhs.slot;
  1098. }
  1099. const CArtifactInstance* IArtifactSetBase::getArt(ui16 pos, bool excludeLocked) const
  1100. {
  1101. if(const ArtSlotInfo *si = getSlot(pos))
  1102. {
  1103. if(si->artifact && (!excludeLocked || !si->locked))
  1104. return si->artifact;
  1105. }
  1106. return NULL;
  1107. }
  1108. bool IArtifactSetBase::hasArt(ui32 aid, bool onlyWorn) const
  1109. {
  1110. return getArtPos(aid, onlyWorn) != -1;
  1111. }
  1112. bool IArtifactSetBase::isPositionFree(ui16 pos, bool onlyLockCheck) const
  1113. {
  1114. if(const ArtSlotInfo *s = getSlot(pos))
  1115. return (onlyLockCheck || !s->artifact) && !s->locked;
  1116. return true; //no slot means not used
  1117. }
  1118. void IArtifactSetBase::setNewArtSlot(ui16 slot, CArtifactInstance *art, bool locked)
  1119. {
  1120. ArtSlotInfo &asi = retreiveNewArtSlot(slot);
  1121. asi.artifact = art;
  1122. asi.locked = locked;
  1123. }
  1124. CArtifactInstance* IArtifactSetBase::getArt(ui16 pos, bool excludeLocked /*= true*/)
  1125. {
  1126. return const_cast<CArtifactInstance*>((const_cast<const IArtifactSetBase*>(this))->getArt(pos, excludeLocked));
  1127. }
  1128. si32 CArtifactSet::getArtPos(int aid, bool onlyWorn /*= true*/) const
  1129. {
  1130. for(std::map<ui16, ArtSlotInfo>::const_iterator i = artifactsWorn.begin(); i != artifactsWorn.end(); i++)
  1131. if(i->second.artifact->artType->id == aid)
  1132. return i->first;
  1133. if(onlyWorn)
  1134. return -1;
  1135. for(int i = 0; i < artifactsInBackpack.size(); i++)
  1136. if(artifactsInBackpack[i].artifact->artType->id == aid)
  1137. return Arts::BACKPACK_START + i;
  1138. return -1;
  1139. }
  1140. si32 CArtifactSet::getArtPos(const CArtifactInstance *art) const
  1141. {
  1142. for(std::map<ui16, ArtSlotInfo>::const_iterator i = artifactsWorn.begin(); i != artifactsWorn.end(); i++)
  1143. if(i->second.artifact == art)
  1144. return i->first;
  1145. for(int i = 0; i < artifactsInBackpack.size(); i++)
  1146. if(artifactsInBackpack[i].artifact == art)
  1147. return Arts::BACKPACK_START + i;
  1148. return -1;
  1149. }
  1150. const CArtifactInstance * CArtifactSet::getArtByInstanceId(int artInstId) const
  1151. {
  1152. for(std::map<ui16, ArtSlotInfo>::const_iterator i = artifactsWorn.begin(); i != artifactsWorn.end(); i++)
  1153. if(i->second.artifact->id == artInstId)
  1154. return i->second.artifact;
  1155. for(int i = 0; i < artifactsInBackpack.size(); i++)
  1156. if(artifactsInBackpack[i].artifact->id == artInstId)
  1157. return artifactsInBackpack[i].artifact;
  1158. return NULL;
  1159. }
  1160. const ArtSlotInfo * CArtifactSet::getSlot(ui16 pos) const
  1161. {
  1162. if(vstd::contains(artifactsWorn, pos))
  1163. return &artifactsWorn[pos];
  1164. if(pos >= Arts::AFTER_LAST )
  1165. {
  1166. int backpackPos = (int)pos - Arts::BACKPACK_START;
  1167. if(backpackPos < 0 || backpackPos >= artifactsInBackpack.size())
  1168. return NULL;
  1169. else
  1170. return &artifactsInBackpack[backpackPos];
  1171. }
  1172. return NULL;
  1173. }
  1174. si32 CArtifactSet::getArtTypeId(ui16 pos) const
  1175. {
  1176. const CArtifactInstance * const a = getArt(pos);
  1177. if(!a)
  1178. {
  1179. tlog2 << (dynamic_cast<const CGHeroInstance*>(this))->name << " has no artifact at " << pos << " (getArtTypeId)\n";
  1180. return -1;
  1181. }
  1182. return a->artType->id;
  1183. }
  1184. CArtifactSet::~CArtifactSet()
  1185. {
  1186. }
  1187. ArtSlotInfo & CArtifactSet::retreiveNewArtSlot(ui16 slot)
  1188. {
  1189. assert(!vstd::contains(artifactsWorn, slot));
  1190. ArtSlotInfo &ret = slot < Arts::BACKPACK_START
  1191. ? artifactsWorn[slot]
  1192. : *artifactsInBackpack.insert(artifactsInBackpack.begin() + (slot - Arts::BACKPACK_START), ArtSlotInfo());
  1193. return ret;
  1194. }
  1195. void CArtifactSet::eraseArtSlot(ui16 slot)
  1196. {
  1197. if(slot < Arts::BACKPACK_START)
  1198. {
  1199. artifactsWorn.erase(slot);
  1200. }
  1201. else
  1202. {
  1203. slot -= Arts::BACKPACK_START;
  1204. artifactsInBackpack.erase(artifactsInBackpack.begin() + slot);
  1205. }
  1206. }
  1207. ArtSlotInfo & CCreatureArtifactSet::retreiveNewArtSlot(ui16 slot)
  1208. {
  1209. assert(slot); //ke?
  1210. ArtSlotInfo &ret = slot <= Arts::CREATURE_ART
  1211. ? activeArtifact
  1212. : *artifactsInBackpack.insert(artifactsInBackpack.begin() + (slot - 1), ArtSlotInfo());
  1213. return ret;
  1214. }
  1215. void CCreatureArtifactSet::eraseArtSlot(ui16 slot)
  1216. {
  1217. if(slot == Arts::CREATURE_ART)
  1218. {
  1219. activeArtifact.artifact = NULL; //hmm?
  1220. }
  1221. else
  1222. {
  1223. slot -= 1;
  1224. artifactsInBackpack.erase(artifactsInBackpack.begin() + slot);
  1225. }
  1226. }
  1227. const ArtSlotInfo * CCreatureArtifactSet::getSlot(ui16 pos) const
  1228. {
  1229. if (pos == Arts::CREATURE_ART)
  1230. return &activeArtifact;
  1231. else if(pos > Arts::CREATURE_ART)
  1232. {
  1233. int backpackPos = (int)pos - 1;
  1234. if(backpackPos < 0 || backpackPos >= artifactsInBackpack.size())
  1235. return NULL;
  1236. else
  1237. return &artifactsInBackpack[backpackPos];
  1238. }
  1239. return NULL;
  1240. }
  1241. si32 CCreatureArtifactSet::getArtPos(int aid, bool onlyWorn) const
  1242. {
  1243. if (aid == activeArtifact.artifact->artType->id )
  1244. return Arts::CREATURE_ART;
  1245. if(onlyWorn)
  1246. return -1;
  1247. for(int i = 0; i < artifactsInBackpack.size(); i++)
  1248. {
  1249. if(artifactsInBackpack[i].artifact->artType->id == aid)
  1250. return i + 1;
  1251. }
  1252. return -1;
  1253. }
  1254. si32 CCreatureArtifactSet::getArtPos(const CArtifactInstance *art) const
  1255. {
  1256. if (activeArtifact.artifact == art)
  1257. return Arts::CREATURE_ART;
  1258. for(int i = 0; i < artifactsInBackpack.size(); i++)
  1259. if(artifactsInBackpack[i].artifact == art)
  1260. return Arts::BACKPACK_START + i;
  1261. return -1;
  1262. }
  1263. const CArtifactInstance * CCreatureArtifactSet::getArtByInstanceId(int artInstId) const
  1264. {
  1265. if (activeArtifact.artifact->id == artInstId)
  1266. return activeArtifact.artifact;
  1267. for(int i = 0; i < artifactsInBackpack.size(); i++)
  1268. if(artifactsInBackpack[i].artifact->id == artInstId)
  1269. return artifactsInBackpack[i].artifact;
  1270. return NULL;
  1271. }
  1272. si32 CCreatureArtifactSet::getArtTypeId(ui16 pos) const
  1273. {
  1274. const CArtifactInstance * const a = getArt(pos);
  1275. if(!a)
  1276. {
  1277. tlog2 << "Stack has no artifact at " << pos << " (getArtTypeId)\n";
  1278. return -1;
  1279. }
  1280. return a->artType->id;
  1281. }