CCreatureSet.cpp 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079
  1. #include "StdInc.h"
  2. #include "CCreatureSet.h"
  3. #include "CCreatureHandler.h"
  4. #include "VCMI_Lib.h"
  5. #include "CObjectHandler.h"
  6. #include "IGameCallback.h"
  7. #include "CGameState.h"
  8. #include "CGeneralTextHandler.h"
  9. #include "CSpellHandler.h"
  10. #include "CHeroHandler.h"
  11. const CStackInstance &CCreatureSet::operator[](TSlot slot) const
  12. {
  13. TSlots::const_iterator i = stacks.find(slot);
  14. if (i != stacks.end())
  15. return *i->second;
  16. else
  17. throw std::runtime_error("That slot is empty!");
  18. }
  19. const CCreature* CCreatureSet::getCreature(TSlot slot) const
  20. {
  21. TSlots::const_iterator i = stacks.find(slot);
  22. if (i != stacks.end())
  23. return i->second->type;
  24. else
  25. return NULL;
  26. }
  27. bool CCreatureSet::setCreature(TSlot slot, TCreature type, TQuantity quantity) /*slots 0 to 6 */
  28. {
  29. if(slot > 6 || slot < 0)
  30. {
  31. tlog1 << "Cannot set slot " << slot << std::endl;
  32. return false;
  33. }
  34. if(!quantity)
  35. {
  36. tlog2 << "Using set creature to delete stack?\n";
  37. eraseStack(slot);
  38. return true;
  39. }
  40. if(hasStackAtSlot(slot)) //remove old creature
  41. eraseStack(slot);
  42. putStack(slot, new CStackInstance(type, quantity));
  43. return true;
  44. }
  45. TSlot CCreatureSet::getSlotFor(TCreature creature, ui32 slotsAmount/*=7*/) const /*returns -1 if no slot available */
  46. {
  47. return getSlotFor(VLC->creh->creatures[creature], slotsAmount);
  48. }
  49. TSlot CCreatureSet::getSlotFor(const CCreature *c, ui32 slotsAmount/*=ARMY_SIZE*/) const
  50. {
  51. assert(c->valid());
  52. for(TSlots::const_iterator i=stacks.begin(); i!=stacks.end(); ++i)
  53. {
  54. assert(i->second->type->valid());
  55. if(i->second->type == c)
  56. {
  57. return i->first; //if there is already such creature we return its slot id
  58. }
  59. }
  60. for(ui32 i=0; i<slotsAmount; i++)
  61. {
  62. if(stacks.find(i) == stacks.end())
  63. {
  64. return i; //return first free slot
  65. }
  66. }
  67. return -1; //no slot available
  68. }
  69. TSlot CCreatureSet::getFreeSlot(ui32 slotsAmount/*=ARMY_SIZE*/) const
  70. {
  71. for (ui32 i = 0; i < slotsAmount; i++)
  72. {
  73. if(stacks.find(i) == stacks.end())
  74. {
  75. return i; //return first free slot
  76. }
  77. }
  78. return -1; //no slot available
  79. }
  80. int CCreatureSet::getStackCount(TSlot slot) const
  81. {
  82. TSlots::const_iterator i = stacks.find(slot);
  83. if (i != stacks.end())
  84. return i->second->count;
  85. else
  86. return 0; //TODO? consider issuing a warning
  87. }
  88. expType CCreatureSet::getStackExperience(TSlot slot) const
  89. {
  90. TSlots::const_iterator i = stacks.find(slot);
  91. if (i != stacks.end())
  92. return i->second->experience;
  93. else
  94. return 0; //TODO? consider issuing a warning
  95. }
  96. bool CCreatureSet::mergableStacks(std::pair<TSlot, TSlot> &out, TSlot preferable /*= -1*/) const /*looks for two same stacks, returns slot positions */
  97. {
  98. //try to match creature to our preferred stack
  99. if(preferable >= 0 && vstd::contains(stacks, preferable))
  100. {
  101. const CCreature *cr = stacks.find(preferable)->second->type;
  102. for(TSlots::const_iterator j=stacks.begin(); j!=stacks.end(); ++j)
  103. {
  104. if(cr == j->second->type && j->first != preferable)
  105. {
  106. out.first = preferable;
  107. out.second = j->first;
  108. return true;
  109. }
  110. }
  111. }
  112. for(TSlots::const_iterator i=stacks.begin(); i!=stacks.end(); ++i)
  113. {
  114. for(TSlots::const_iterator j=stacks.begin(); j!=stacks.end(); ++j)
  115. {
  116. if(i->second->type == j->second->type && i->first != j->first)
  117. {
  118. out.first = i->first;
  119. out.second = j->first;
  120. return true;
  121. }
  122. }
  123. }
  124. return false;
  125. }
  126. void CCreatureSet::sweep()
  127. {
  128. for(TSlots::iterator i=stacks.begin(); i!=stacks.end(); ++i)
  129. {
  130. if(!i->second->count)
  131. {
  132. stacks.erase(i);
  133. sweep();
  134. break;
  135. }
  136. }
  137. }
  138. void CCreatureSet::addToSlot(TSlot slot, TCreature cre, TQuantity count, bool allowMerging/* = true*/)
  139. {
  140. const CCreature *c = VLC->creh->creatures[cre];
  141. if(!hasStackAtSlot(slot))
  142. {
  143. setCreature(slot, cre, count);
  144. }
  145. else if(getCreature(slot) == c && allowMerging) //that slot was empty or contained same type creature
  146. {
  147. setStackCount(slot, getStackCount(slot) + count);
  148. }
  149. else
  150. {
  151. tlog1 << "Failed adding to slot!\n";
  152. }
  153. }
  154. void CCreatureSet::addToSlot(TSlot slot, CStackInstance *stack, bool allowMerging/* = true*/)
  155. {
  156. assert(stack->valid(true));
  157. if(!hasStackAtSlot(slot))
  158. {
  159. putStack(slot, stack);
  160. }
  161. else if(allowMerging && stack->type == getCreature(slot))
  162. {
  163. joinStack(slot, stack);
  164. }
  165. else
  166. {
  167. tlog1 << "Cannot add to slot " << slot << " stack " << *stack << std::endl;
  168. }
  169. }
  170. bool CCreatureSet::validTypes(bool allowUnrandomized /*= false*/) const
  171. {
  172. for(TSlots::const_iterator i=stacks.begin(); i!=stacks.end(); ++i)
  173. {
  174. if(!i->second->valid(allowUnrandomized))
  175. return false;
  176. }
  177. return true;
  178. }
  179. bool CCreatureSet::slotEmpty(TSlot slot) const
  180. {
  181. return !hasStackAtSlot(slot);
  182. }
  183. bool CCreatureSet::needsLastStack() const
  184. {
  185. return false;
  186. }
  187. ui64 CCreatureSet::getArmyStrength() const
  188. {
  189. ui64 ret = 0;
  190. for(TSlots::const_iterator i = stacks.begin(); i != stacks.end(); i++)
  191. ret += i->second->getPower();
  192. return ret;
  193. }
  194. ui64 CCreatureSet::getPower (TSlot slot) const
  195. {
  196. return getStack(slot).getPower();
  197. }
  198. std::string CCreatureSet::getRoughAmount (TSlot slot) const
  199. {
  200. return VLC->generaltexth->arraytxt[174 + 3*CCreature::getQuantityID(getStackCount(slot))];
  201. }
  202. int CCreatureSet::stacksCount() const
  203. {
  204. return stacks.size();
  205. }
  206. void CCreatureSet::setFormation(bool tight)
  207. {
  208. formation = tight;
  209. }
  210. void CCreatureSet::setStackCount(TSlot slot, TQuantity count)
  211. {
  212. assert(hasStackAtSlot(slot));
  213. assert(stacks[slot]->count + count > 0);
  214. if (GameConstants::STACK_EXP && count > stacks[slot]->count)
  215. stacks[slot]->experience *= (count / static_cast<double>(stacks[slot]->count));
  216. stacks[slot]->count = count;
  217. armyChanged();
  218. }
  219. void CCreatureSet::giveStackExp(expType exp)
  220. {
  221. for(TSlots::const_iterator i = stacks.begin(); i != stacks.end(); i++)
  222. i->second->giveStackExp(exp);
  223. }
  224. void CCreatureSet::setStackExp(TSlot slot, expType exp)
  225. {
  226. assert(hasStackAtSlot(slot));
  227. stacks[slot]->experience = exp;
  228. }
  229. void CCreatureSet::clear()
  230. {
  231. while(!stacks.empty())
  232. {
  233. eraseStack(stacks.begin()->first);
  234. }
  235. }
  236. const CStackInstance& CCreatureSet::getStack(TSlot slot) const
  237. {
  238. assert(hasStackAtSlot(slot));
  239. return *getStackPtr(slot);
  240. }
  241. const CStackInstance* CCreatureSet::getStackPtr(TSlot slot) const
  242. {
  243. if(hasStackAtSlot(slot))
  244. return stacks.find(slot)->second;
  245. else return NULL;
  246. }
  247. void CCreatureSet::eraseStack(TSlot slot)
  248. {
  249. assert(hasStackAtSlot(slot));
  250. CStackInstance *toErase = detachStack(slot);
  251. vstd::clear_pointer(toErase);
  252. }
  253. bool CCreatureSet::contains(const CStackInstance *stack) const
  254. {
  255. if(!stack)
  256. return false;
  257. for(TSlots::const_iterator i = stacks.begin(); i != stacks.end(); ++i)
  258. if(i->second == stack)
  259. return true;
  260. return false;
  261. }
  262. TSlot CCreatureSet::findStack(const CStackInstance *stack) const
  263. {
  264. if(!stack)
  265. return -1;
  266. for(TSlots::const_iterator i = stacks.begin(); i != stacks.end(); ++i)
  267. if(i->second == stack)
  268. return i->first;
  269. return -1;
  270. }
  271. CArmedInstance * CCreatureSet::castToArmyObj()
  272. {
  273. return dynamic_cast<CArmedInstance *>(this);
  274. }
  275. void CCreatureSet::putStack(TSlot slot, CStackInstance *stack)
  276. {
  277. assert(!hasStackAtSlot(slot));
  278. stacks[slot] = stack;
  279. stack->setArmyObj(castToArmyObj());
  280. armyChanged();
  281. }
  282. void CCreatureSet::joinStack(TSlot slot, CStackInstance * stack)
  283. {
  284. const CCreature *c = getCreature(slot);
  285. assert(c == stack->type);
  286. assert(c);
  287. //TODO move stuff
  288. changeStackCount(slot, stack->count);
  289. vstd::clear_pointer(stack);
  290. }
  291. void CCreatureSet::changeStackCount(TSlot slot, TQuantity toAdd)
  292. {
  293. setStackCount(slot, getStackCount(slot) + toAdd);
  294. }
  295. CCreatureSet::CCreatureSet()
  296. {
  297. formation = false;
  298. }
  299. CCreatureSet::CCreatureSet(const CCreatureSet&)
  300. {
  301. assert(0);
  302. }
  303. CCreatureSet::~CCreatureSet()
  304. {
  305. clear();
  306. }
  307. void CCreatureSet::setToArmy(CSimpleArmy &src)
  308. {
  309. clear();
  310. while(src)
  311. {
  312. TSimpleSlots::iterator i = src.army.begin();
  313. assert(i->second.type);
  314. assert(i->second.count);
  315. putStack(i->first, new CStackInstance(i->second.type, i->second.count));
  316. src.army.erase(i);
  317. }
  318. }
  319. CStackInstance * CCreatureSet::detachStack(TSlot slot)
  320. {
  321. assert(hasStackAtSlot(slot));
  322. CStackInstance *ret = stacks[slot];
  323. //if(CArmedInstance *armedObj = castToArmyObj())
  324. {
  325. ret->setArmyObj(NULL); //detaches from current armyobj
  326. }
  327. assert(!ret->armyObj); //we failed detaching?
  328. stacks.erase(slot);
  329. armyChanged();
  330. return ret;
  331. }
  332. void CCreatureSet::setStackType(TSlot slot, const CCreature *type)
  333. {
  334. assert(hasStackAtSlot(slot));
  335. CStackInstance *s = stacks[slot];
  336. s->setType(type->idNumber);
  337. armyChanged();
  338. }
  339. bool CCreatureSet::canBeMergedWith(const CCreatureSet &cs, bool allowMergingStacks) const
  340. {
  341. if(!allowMergingStacks)
  342. {
  343. int freeSlots = stacksCount() - GameConstants::ARMY_SIZE;
  344. std::set<const CCreature*> cresToAdd;
  345. for(TSlots::const_iterator i = cs.stacks.begin(); i != cs.stacks.end(); i++)
  346. {
  347. TSlot dest = getSlotFor(i->second->type);
  348. if(dest < 0 || hasStackAtSlot(dest))
  349. cresToAdd.insert(i->second->type);
  350. }
  351. return cresToAdd.size() <= freeSlots;
  352. }
  353. else
  354. {
  355. CCreatureSet cres;
  356. //get types of creatures that need their own slot
  357. for(TSlots::const_iterator i = cs.stacks.begin(); i != cs.stacks.end(); i++)
  358. cres.addToSlot(i->first, i->second->type->idNumber, 1, true);
  359. TSlot j;
  360. for(TSlots::const_iterator i = stacks.begin(); i != stacks.end(); i++)
  361. {
  362. if ((j = cres.getSlotFor(i->second->type)) >= 0)
  363. cres.addToSlot(j, i->second->type->idNumber, 1, true); //merge if possible
  364. else
  365. return false; //no place found
  366. }
  367. return true; //all stacks found their slots
  368. }
  369. }
  370. bool CCreatureSet::hasStackAtSlot(TSlot slot) const
  371. {
  372. return vstd::contains(stacks, slot);
  373. }
  374. CCreatureSet & CCreatureSet::operator=(const CCreatureSet&cs)
  375. {
  376. assert(0);
  377. return *this;
  378. }
  379. void CCreatureSet::armyChanged()
  380. {
  381. }
  382. CStackInstance::CStackInstance()
  383. : armyObj(_armyObj)
  384. {
  385. init();
  386. }
  387. CStackInstance::CStackInstance(TCreature id, TQuantity Count)
  388. : armyObj(_armyObj)
  389. {
  390. init();
  391. setType(id);
  392. count = Count;
  393. }
  394. CStackInstance::CStackInstance(const CCreature *cre, TQuantity Count)
  395. : armyObj(_armyObj)
  396. {
  397. init();
  398. setType(cre);
  399. count = Count;
  400. }
  401. void CStackInstance::init()
  402. {
  403. experience = 0;
  404. count = 0;
  405. type = NULL;
  406. idRand = -1;
  407. _armyObj = NULL;
  408. setNodeType(STACK_INSTANCE);
  409. }
  410. int CStackInstance::getQuantityID() const
  411. {
  412. return CCreature::getQuantityID(count);
  413. }
  414. int CStackInstance::getExpRank() const
  415. {
  416. int tier = type->level;
  417. if (vstd::iswithin(tier, 1, 7))
  418. {
  419. for (int i = VLC->creh->expRanks[tier].size()-2; i >-1; --i)//sic!
  420. { //exp values vary from 1st level to max exp at 11th level
  421. if (experience >= VLC->creh->expRanks[tier][i])
  422. return ++i; //faster, but confusing - 0 index mean 1st level of experience
  423. }
  424. return 0;
  425. }
  426. else //higher tier
  427. {
  428. for (int i = VLC->creh->expRanks[0].size()-2; i >-1; --i)
  429. {
  430. if (experience >= VLC->creh->expRanks[0][i])
  431. return ++i;
  432. }
  433. return 0;
  434. }
  435. }
  436. si32 CStackInstance::magicResistance() const
  437. {
  438. si32 val = valOfBonuses(Selector::type(Bonus::MAGIC_RESISTANCE));
  439. if (const CGHeroInstance * hero = dynamic_cast<const CGHeroInstance *>(_armyObj))
  440. {
  441. //resistance skill
  442. val += hero->valOfBonuses(Bonus::SECONDARY_SKILL_PREMY, CGHeroInstance::RESISTANCE);
  443. }
  444. vstd::amin (val, 100);
  445. return val;
  446. }
  447. void CStackInstance::giveStackExp(expType exp)
  448. {
  449. int level = type->level;
  450. if (!vstd::iswithin(level, 1, 7))
  451. level = 0;
  452. CCreatureHandler * creh = VLC->creh;
  453. ui32 maxExp = creh->expRanks[level].back();
  454. vstd::amin(exp, (expType)maxExp); //prevent exp overflow due to different types
  455. vstd::amin(exp, (maxExp * creh->maxExpPerBattle[level])/100);
  456. vstd::amin(experience += exp, maxExp); //can't get more exp than this limit
  457. }
  458. void CStackInstance::setType(int creID)
  459. {
  460. if(creID >= 0 && creID < VLC->creh->creatures.size())
  461. setType(VLC->creh->creatures[creID]);
  462. else
  463. setType((const CCreature*)NULL);
  464. }
  465. void CStackInstance::setType(const CCreature *c)
  466. {
  467. if(type)
  468. {
  469. detachFrom(const_cast<CCreature*>(type));
  470. if (type->isMyUpgrade(c) && GameConstants::STACK_EXP)
  471. experience *= VLC->creh->expAfterUpgrade / 100.0;
  472. }
  473. type = c;
  474. if(type)
  475. attachTo(const_cast<CCreature*>(type));
  476. }
  477. std::string CStackInstance::bonusToString(Bonus *bonus, bool description) const
  478. {
  479. std::map<TBonusType, std::pair<std::string, std::string> >::iterator it = VLC->creh->stackBonuses.find(bonus->type);
  480. if (it != VLC->creh->stackBonuses.end())
  481. {
  482. std::string text;
  483. if (description) //long ability description
  484. {
  485. text = it->second.second;
  486. switch (bonus->type)
  487. {
  488. //no additional modifiers needed
  489. case Bonus::FLYING:
  490. case Bonus::UNLIMITED_RETALIATIONS:
  491. case Bonus::SHOOTER:
  492. case Bonus::FREE_SHOOTING:
  493. case Bonus::NO_MELEE_PENALTY:
  494. case Bonus::NO_DISTANCE_PENALTY:
  495. case Bonus::NO_WALL_PENALTY:
  496. case Bonus::JOUSTING: //TODO: percent bonus?
  497. case Bonus::RETURN_AFTER_STRIKE:
  498. case Bonus::BLOCKS_RETALIATION:
  499. case Bonus::TWO_HEX_ATTACK_BREATH:
  500. case Bonus::THREE_HEADED_ATTACK:
  501. case Bonus::ATTACKS_ALL_ADJACENT:
  502. case Bonus::ADDITIONAL_ATTACK: //TODO: what with more than one attack? Axe of Ferocity for example
  503. case Bonus::FULL_HP_REGENERATION:
  504. case Bonus::REBIRTH:
  505. case Bonus::LIFE_DRAIN: //TODO: chance, hp percentage?
  506. case Bonus::SELF_MORALE:
  507. case Bonus::SELF_LUCK:
  508. case Bonus::FEAR:
  509. case Bonus::FEARLESS:
  510. case Bonus::CHARGE_IMMUNITY:
  511. case Bonus::HEALER:
  512. case Bonus::CATAPULT:
  513. case Bonus::DRAGON_NATURE:
  514. case Bonus::NON_LIVING:
  515. case Bonus::UNDEAD:
  516. case Bonus::FIRE_IMMUNITY:
  517. case Bonus::WATER_IMMUNITY:
  518. case Bonus::AIR_IMMUNITY:
  519. case Bonus::EARTH_IMMUNITY:
  520. case Bonus::RECEPTIVE:
  521. case Bonus::DIRECT_DAMAGE_IMMUNITY:
  522. break;
  523. //One numeric value. magic resistance handled separately
  524. case Bonus::SPELL_RESISTANCE_AURA:
  525. case Bonus::SPELL_DAMAGE_REDUCTION:
  526. case Bonus::LEVEL_SPELL_IMMUNITY:
  527. case Bonus::MANA_DRAIN:
  528. case Bonus::HP_REGENERATION:
  529. case Bonus::ADDITIONAL_RETALIATION:
  530. case Bonus::DEFENSIVE_STANCE:
  531. case Bonus::DOUBLE_DAMAGE_CHANCE:
  532. case Bonus::DARKNESS: //Darkness Dragons any1?
  533. boost::algorithm::replace_first(text, "%d", boost::lexical_cast<std::string>(valOfBonuses(Selector::typeSubtype(bonus->type, bonus->subtype))));
  534. break;
  535. //Complex descriptions
  536. //case Bonus::SECONDARY_SKILL_PREMY: //only if there's no simple MR
  537. // if (bonus->subtype == CGHeroInstance::RESISTANCE)
  538. // {
  539. // if (!hasBonusOfType(Bonus::MAGIC_RESISTANCE))
  540. // boost::algorithm::replace_first(text, "%d", boost::lexical_cast<std::string>( magicResistance() ));
  541. // }
  542. // break;
  543. //case Bonus::MAGIC_RESISTANCE:
  544. // boost::algorithm::replace_first(text, "%d", boost::lexical_cast<std::string>( magicResistance() ));
  545. // break;
  546. case Bonus::HATE:
  547. boost::algorithm::replace_first(text, "%d", boost::lexical_cast<std::string>(valOfBonuses(Selector::typeSubtype(bonus->type, bonus->subtype))));
  548. boost::algorithm::replace_first(text, "%s", VLC->creh->creatures[bonus->subtype]->namePl);
  549. break;
  550. case Bonus::SPELL_AFTER_ATTACK:
  551. case Bonus::SPELL_BEFORE_ATTACK:
  552. {
  553. boost::algorithm::replace_first(text, "%d", boost::lexical_cast<std::string>(valOfBonuses(Selector::typeSubtype(bonus->type, bonus->subtype))));
  554. boost::algorithm::replace_first(text, "%s", VLC->spellh->spells[bonus->subtype]->name);
  555. break;
  556. }
  557. default:
  558. {}//TODO: allow custom bonus types... someday, somehow
  559. }
  560. }
  561. else //short name
  562. {
  563. text = it->second.first;
  564. switch (bonus->type)
  565. {
  566. case Bonus::MANA_CHANNELING:
  567. case Bonus::MAGIC_MIRROR:
  568. case Bonus::CHANGES_SPELL_COST_FOR_ALLY:
  569. case Bonus::CHANGES_SPELL_COST_FOR_ENEMY:
  570. case Bonus::ENEMY_DEFENCE_REDUCTION:
  571. case Bonus::REBIRTH:
  572. case Bonus::DEATH_STARE:
  573. boost::algorithm::replace_first(text, "%d", boost::lexical_cast<std::string>(valOfBonuses(Selector::typeSubtype(bonus->type, bonus->subtype))));
  574. break;
  575. case Bonus::HATE:
  576. case Bonus::DAEMON_SUMMONING:
  577. boost::algorithm::replace_first(text, "%s", VLC->creh->creatures[bonus->subtype]->namePl);
  578. break;
  579. case Bonus::LEVEL_SPELL_IMMUNITY:
  580. boost::algorithm::replace_first(text, "%d", boost::lexical_cast<std::string>(bonus->val));
  581. break;
  582. case Bonus::SPELL_AFTER_ATTACK:
  583. case Bonus::SPELL_BEFORE_ATTACK:
  584. case Bonus::SPELL_IMMUNITY:
  585. case Bonus::SPELLCASTER:
  586. case Bonus::ENCHANTER:
  587. boost::algorithm::replace_first(text, "%s", VLC->spellh->spells[bonus->subtype]->name);
  588. break;
  589. case Bonus::MAGIC_RESISTANCE:
  590. text = ""; //handled separately
  591. break;
  592. //case Bonus::SECONDARY_SKILL_PREMY:
  593. // if (bonus->subtype != CGHeroInstance::RESISTANCE || hasBonusOfType(Bonus::MAGIC_RESISTANCE)) //handle it there
  594. // text = "";
  595. // break;
  596. }
  597. }
  598. return text;
  599. }
  600. else
  601. return "";
  602. }
  603. std::string CStackInstance::bonusToGraphics(Bonus *bonus) const
  604. {
  605. std::string fileName;
  606. switch (bonus->type)
  607. {
  608. //"E_ALIVE.bmp"
  609. //"E_ART.bmp"
  610. //"E_BLESS.bmp"
  611. //"E_BLOCK.bmp"
  612. //"E_BLOCK1.bmp"
  613. //"E_BLOCK2.bmp"
  614. case Bonus::TWO_HEX_ATTACK_BREATH:
  615. fileName = "E_BREATH.bmp"; break;
  616. case Bonus::SPELL_AFTER_ATTACK:
  617. fileName = "E_CAST.bmp"; break;
  618. case Bonus::ENCHANTER:
  619. fileName = "E_CAST1.bmp"; break;
  620. case Bonus::RANDOM_SPELLCASTER:
  621. fileName = "RandomBoost.bmp"; break;
  622. case Bonus::SPELL_BEFORE_ATTACK:
  623. fileName ="E_CAST2.bmp"; break;
  624. case Bonus::SPELLCASTER:
  625. fileName = "E_CASTER.bmp"; break;
  626. case Bonus::JOUSTING:
  627. fileName = "E_CHAMP.bmp"; break;
  628. case Bonus::DOUBLE_DAMAGE_CHANCE:
  629. fileName = "E_DBLOW.bmp"; break;
  630. case Bonus::DEATH_STARE:
  631. fileName = "E_DEATH.bmp"; break;
  632. case Bonus::DEFENSIVE_STANCE:
  633. fileName = "E_DEFBON.bmp"; break;
  634. case Bonus::NO_DISTANCE_PENALTY:
  635. fileName = "E_DIST.bmp"; break;
  636. case Bonus::ADDITIONAL_ATTACK:
  637. fileName = "E_DOUBLE.bmp"; break;
  638. case Bonus::DRAGON_NATURE:
  639. fileName = "E_DRAGON.bmp"; break;
  640. case Bonus::MAGIC_RESISTANCE:
  641. fileName = "E_DWARF.bmp"; break;
  642. case Bonus::SECONDARY_SKILL_PREMY:
  643. if (bonus->subtype == CGHeroInstance::RESISTANCE)
  644. {
  645. fileName = "E_DWARF.bmp";
  646. }
  647. break;
  648. case Bonus::FEAR:
  649. fileName = "E_FEAR.bmp"; break;
  650. case Bonus::FEARLESS:
  651. fileName = "E_FEARL.bmp"; break;
  652. case Bonus::FLYING:
  653. fileName = "E_FLY.bmp"; break;
  654. case Bonus::SPELL_DAMAGE_REDUCTION:
  655. fileName = "E_GOLEM.bmp"; break;
  656. case Bonus::RETURN_AFTER_STRIKE:
  657. fileName = "E_HARPY.bmp"; break;
  658. case Bonus::HATE:
  659. fileName = "E_HATE.bmp"; break;
  660. case Bonus::KING1:
  661. fileName = "E_KING1.bmp"; break;
  662. case Bonus::KING2:
  663. fileName = "E_KING2.bmp"; break;
  664. case Bonus::KING3:
  665. fileName = "E_KING3.bmp"; break;
  666. case Bonus::CHANGES_SPELL_COST_FOR_ALLY:
  667. fileName = "E_MANA.bmp"; break;
  668. case Bonus::CHANGES_SPELL_COST_FOR_ENEMY:
  669. fileName = "MagicDamper.bpm"; break;
  670. case Bonus::NO_MELEE_PENALTY:
  671. fileName = "E_MELEE.bmp"; break;
  672. case Bonus::MIND_IMMUNITY:
  673. fileName = "E_MIND.bmp"; break;
  674. case Bonus::SELF_MORALE:
  675. fileName = "E_MINOT.bmp"; break;
  676. case Bonus::NO_MORALE:
  677. fileName = "E_MORAL.bmp"; break;
  678. case Bonus::RECEPTIVE:
  679. fileName = "E_NOFRIM.bmp"; break;
  680. case Bonus::NO_WALL_PENALTY:
  681. fileName = "E_OBST.bmp"; break;
  682. case Bonus::ENEMY_DEFENCE_REDUCTION:
  683. fileName = "E_RDEF.bmp"; break;
  684. case Bonus::REBIRTH:
  685. fileName = "E_REBIRTH.bmp"; break;
  686. case Bonus::BLOCKS_RETALIATION:
  687. fileName = "E_RETAIL.bmp"; break;
  688. case Bonus::UNLIMITED_RETALIATIONS:
  689. case Bonus::ADDITIONAL_RETALIATION:
  690. fileName = "E_RETAIL1.bmp"; break;
  691. case Bonus::ATTACKS_ALL_ADJACENT:
  692. fileName = "E_ROUND.bmp"; break;
  693. //"E_SGNUM.bmp"
  694. //"E_SGTYPE.bmp"
  695. case Bonus::SHOOTER:
  696. fileName = "E_SHOOT.bmp"; break;
  697. case Bonus::FREE_SHOOTING: //shooter is not blocked by enemy
  698. fileName = "E_SHOOTA.bmp"; break;
  699. //"E_SHOOTN.bmp"
  700. case Bonus::SPELL_IMMUNITY:
  701. {
  702. switch (bonus->subtype)
  703. {
  704. case 62: //Blind
  705. fileName = "E_SPBLIND.bmp"; break;
  706. case 35: // Dispell
  707. fileName = "E_SPDISP.bmp"; break;
  708. case 78: // Dispell beneficial spells
  709. fileName = "E_SPDISB.bmp"; break;
  710. case 60: //Hypnotize
  711. fileName = "E_SPHYPN.bmp"; break;
  712. case 18: //Implosion
  713. fileName = "E_SPIMP.bmp"; break;
  714. case 59: //Berserk
  715. fileName = "E_SPBERS.bmp"; break;
  716. case 23: //Meteor Shower
  717. fileName = "E_SPMET.bmp"; break;
  718. case 26: //Armageddon
  719. fileName = "E_SPARM.bmp"; break;
  720. case 54: //Slow
  721. fileName = "E_SPSLOW.bmp"; break;
  722. //TODO: some generic spell handling?
  723. }
  724. break;
  725. }
  726. //"E_SPAWILL.bmp"
  727. case Bonus::DIRECT_DAMAGE_IMMUNITY:
  728. fileName = "E_SPDIR.bmp"; break;
  729. //"E_SPDISB.bmp"
  730. //"E_SPDISP.bmp"
  731. //"E_SPEATH.bmp"
  732. //"E_SPEATH1.bmp"
  733. case Bonus::FIRE_IMMUNITY:
  734. switch (bonus->subtype)
  735. {
  736. case 0:
  737. fileName = "E_SPFIRE.bmp"; break; //all
  738. case 1:
  739. fileName = "E_SPFIRE1.bmp"; break; //not positive
  740. case 2:
  741. fileName = "E_FIRE.bmp"; break; //direct damage
  742. }
  743. break;
  744. case Bonus::WATER_IMMUNITY:
  745. switch (bonus->subtype)
  746. {
  747. case 0:
  748. fileName = "E_SPWATER.bmp"; break; //all
  749. case 1:
  750. fileName = "E_SPWATER1.bmp"; break; //not positive
  751. case 2:
  752. fileName = "E_SPCOLD.bmp"; break; //direct damage
  753. }
  754. break;
  755. case Bonus::AIR_IMMUNITY:
  756. switch (bonus->subtype)
  757. {
  758. case 0:
  759. fileName = "E_SPAIR.bmp"; break; //all
  760. case 1:
  761. fileName = "E_SPAIR1.bmp"; break; //not positive
  762. case 2:
  763. fileName = "E_LIGHT.bmp"; break;//direct damage
  764. }
  765. break;
  766. case Bonus::EARTH_IMMUNITY:
  767. switch (bonus->subtype)
  768. {
  769. case 0:
  770. fileName = "E_SPEATH.bmp"; break; //all
  771. case 1:
  772. case 2: //no specific icon for direct damage immunity
  773. fileName = "E_SPEATH1.bmp"; break; //not positive
  774. }
  775. break;
  776. case Bonus::LEVEL_SPELL_IMMUNITY:
  777. {
  778. if (vstd::iswithin(bonus->val, 1 , 5))
  779. {
  780. fileName = "E_SPLVL" + boost::lexical_cast<std::string>(bonus->val) + ".bmp";
  781. }
  782. break;
  783. }
  784. //"E_SUMMON.bmp"
  785. //"E_SUMMON1.bmp"
  786. //"E_SUMMON2.bmp"
  787. case Bonus::FULL_HP_REGENERATION:
  788. case Bonus::HP_REGENERATION:
  789. fileName = "E_TROLL.bmp"; break;
  790. case Bonus::UNDEAD:
  791. fileName = "E_UNDEAD.bmp"; break;
  792. case Bonus::SPELL_RESISTANCE_AURA:
  793. fileName = "E_UNIC.bmp"; break;
  794. case Bonus::THREE_HEADED_ATTACK:
  795. fileName = "ThreeHeaded.bmp"; break;
  796. case Bonus::DAEMON_SUMMONING:
  797. fileName = "RiseDemons.bmp"; break;
  798. case Bonus::CHARGE_IMMUNITY:
  799. fileName = "ChargeImmune.bmp"; break;
  800. case Bonus::HEALER:
  801. fileName = "Healer.bmp"; break;
  802. case Bonus::CATAPULT:
  803. fileName = "Catapult.bmp"; break;
  804. case Bonus::MANA_CHANNELING:
  805. fileName = "ManaChannel.bmp"; break;
  806. case Bonus::MANA_DRAIN:
  807. fileName = "ManaDrain.bmp"; break;
  808. }
  809. if(!fileName.empty())
  810. fileName = "zvs/Lib1.res/" + fileName;
  811. return fileName;
  812. }
  813. void CStackInstance::setArmyObj(const CArmedInstance *ArmyObj)
  814. {
  815. if(_armyObj)
  816. detachFrom(const_cast<CArmedInstance*>(_armyObj));
  817. _armyObj = ArmyObj;
  818. if(ArmyObj)
  819. {
  820. attachTo(const_cast<CArmedInstance*>(_armyObj));
  821. }
  822. }
  823. std::string CStackInstance::getQuantityTXT(bool capitalized /*= true*/) const
  824. {
  825. return VLC->generaltexth->arraytxt[174 + getQuantityID()*3 + 2 - capitalized];
  826. }
  827. bool CStackInstance::valid(bool allowUnrandomized) const
  828. {
  829. bool isRand = (idRand != -1);
  830. if(!isRand)
  831. {
  832. return (type && type == VLC->creh->creatures[type->idNumber]);
  833. }
  834. else
  835. return allowUnrandomized;
  836. }
  837. CStackInstance::~CStackInstance()
  838. {
  839. }
  840. std::string CStackInstance::nodeName() const
  841. {
  842. std::ostringstream oss;
  843. oss << "Stack of " << count << " of ";
  844. if(type)
  845. oss << type->namePl;
  846. else if(idRand)
  847. oss << "[no type, idRand=" << idRand << "]";
  848. else
  849. oss << "[UNDEFINED TYPE]";
  850. return oss.str();
  851. }
  852. void CStackInstance::deserializationFix()
  853. {
  854. const CCreature *backup = type;
  855. type = NULL;
  856. setType(backup);
  857. const CArmedInstance *armyBackup = _armyObj;
  858. _armyObj = NULL;
  859. setArmyObj(armyBackup);
  860. artDeserializationFix(this);
  861. }
  862. int CStackInstance::getCreatureID() const
  863. {
  864. if(type)
  865. return type->idNumber;
  866. else
  867. return -1;
  868. }
  869. std::string CStackInstance::getName() const
  870. {
  871. return (count > 1) ? type->namePl : type->nameSing;
  872. }
  873. ui64 CStackInstance::getPower() const
  874. {
  875. assert(type);
  876. return type->AIValue * count;
  877. }
  878. ui8 CStackInstance::bearerType() const
  879. {
  880. return ArtBearer::CREATURE;
  881. }
  882. CCommanderInstance::CCommanderInstance()
  883. {
  884. init();
  885. name = "Unnamed";
  886. }
  887. CCommanderInstance::CCommanderInstance (TCreature id)
  888. {
  889. init();
  890. setType(id);
  891. name = "Commando"; //TODO - parse them
  892. }
  893. void CCommanderInstance::init()
  894. {
  895. alive = true;
  896. experience = 0;
  897. level = 1;
  898. count = 1;
  899. type = NULL;
  900. idRand = -1;
  901. _armyObj = NULL;
  902. setNodeType (Bonus::COMMANDER);
  903. secondarySkills.resize (ECommander::SPELL_POWER + 1);
  904. }
  905. CCommanderInstance::~CCommanderInstance()
  906. {
  907. }
  908. void CCommanderInstance::setAlive (bool Alive)
  909. {
  910. //TODO: helm of immortality
  911. alive = Alive;
  912. if (!alive)
  913. {
  914. //remove all bonuses from artifacts
  915. }
  916. }
  917. void CCommanderInstance::giveStackExp (expType exp)
  918. {
  919. if (alive)
  920. experience += exp;
  921. }
  922. int CCommanderInstance::getExpRank() const
  923. {
  924. return VLC->heroh->level (experience);
  925. }
  926. void CCommanderInstance::levelUp ()
  927. {
  928. level++;
  929. BOOST_FOREACH (auto bonus, VLC->creh->commanderLevelPremy)
  930. { //grant all regular level-up bonuses
  931. accumulateBonus (*bonus);
  932. }
  933. }
  934. ui8 CCommanderInstance::bearerType() const
  935. {
  936. return ArtBearer::COMMANDER;
  937. }
  938. CStackBasicDescriptor::CStackBasicDescriptor()
  939. {
  940. type = NULL;
  941. count = -1;
  942. }
  943. CStackBasicDescriptor::CStackBasicDescriptor(TCreature id, TQuantity Count)
  944. : type (VLC->creh->creatures[id]), count(Count)
  945. {
  946. }
  947. CStackBasicDescriptor::CStackBasicDescriptor(const CCreature *c, TQuantity Count)
  948. : type(c), count(Count)
  949. {
  950. }
  951. DLL_LINKAGE std::ostream & operator<<(std::ostream & str, const CStackInstance & sth)
  952. {
  953. if(!sth.valid(true))
  954. str << "an invalid stack!";
  955. str << "stack with " << sth.count << " of ";
  956. if(sth.type)
  957. str << sth.type->namePl;
  958. else
  959. str << sth.idRand;
  960. return str;
  961. }
  962. void CSimpleArmy::clear()
  963. {
  964. army.clear();
  965. }
  966. CSimpleArmy::operator bool() const
  967. {
  968. return army.size();
  969. }
  970. bool CSimpleArmy::setCreature(TSlot slot, TCreature cre, TQuantity count)
  971. {
  972. assert(!vstd::contains(army, slot));
  973. army[slot] = CStackBasicDescriptor(cre, count);
  974. return true;
  975. }