CCreatureSet.cpp 27 KB

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