CArtHandler.cpp 46 KB

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