CCreatureSet.cpp 27 KB

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