CCreatureSet.cpp 27 KB

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