CCreatureSet.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977
  1. #define VCMI_DLL
  2. #include "CCreatureSet.h"
  3. #include "CCreatureHandler.h"
  4. #include "VCMI_Lib.h"
  5. #include <assert.h>
  6. #include "CObjectHandler.h"
  7. #include "IGameCallback.h"
  8. #include "CGameState.h"
  9. #include "CGeneralTextHandler.h"
  10. #include <sstream>
  11. #include "CSpellHandler.h"
  12. #include <boost/lexical_cast.hpp>
  13. #include <boost/algorithm/string/replace.hpp>
  14. const CStackInstance &CCreatureSet::operator[](TSlot slot) const
  15. {
  16. TSlots::const_iterator i = stacks.find(slot);
  17. if (i != stacks.end())
  18. return *i->second;
  19. else
  20. throw std::string("That slot is empty!");
  21. }
  22. const CCreature* CCreatureSet::getCreature(TSlot slot) const
  23. {
  24. TSlots::const_iterator i = stacks.find(slot);
  25. if (i != stacks.end())
  26. return i->second->type;
  27. else
  28. return NULL;
  29. }
  30. bool CCreatureSet::setCreature(TSlot slot, TCreature type, TQuantity quantity) /*slots 0 to 6 */
  31. {
  32. if(slot > 6 || slot < 0)
  33. {
  34. tlog1 << "Cannot set slot " << slot << std::endl;
  35. return false;
  36. }
  37. if(!quantity)
  38. {
  39. tlog2 << "Using set creature to delete stack?\n";
  40. eraseStack(slot);
  41. return true;
  42. }
  43. if(hasStackAtSlot(slot)) //remove old creature
  44. eraseStack(slot);
  45. putStack(slot, new CStackInstance(type, quantity));
  46. return true;
  47. }
  48. TSlot CCreatureSet::getSlotFor(TCreature creature, ui32 slotsAmount/*=7*/) const /*returns -1 if no slot available */
  49. {
  50. return getSlotFor(VLC->creh->creatures[creature], slotsAmount);
  51. }
  52. TSlot CCreatureSet::getSlotFor(const CCreature *c, ui32 slotsAmount/*=ARMY_SIZE*/) const
  53. {
  54. assert(c->valid());
  55. for(TSlots::const_iterator i=stacks.begin(); i!=stacks.end(); ++i)
  56. {
  57. assert(i->second->type->valid());
  58. if(i->second->type == c)
  59. {
  60. return i->first; //if there is already such creature we return its slot id
  61. }
  62. }
  63. for(ui32 i=0; i<slotsAmount; i++)
  64. {
  65. if(stacks.find(i) == stacks.end())
  66. {
  67. return i; //return first free slot
  68. }
  69. }
  70. return -1; //no slot available
  71. }
  72. TSlot CCreatureSet::getFreeSlot(ui32 slotsAmount/*=ARMY_SIZE*/) const
  73. {
  74. for (ui32 i = 0; i < slotsAmount; i++)
  75. {
  76. if(stacks.find(i) == stacks.end())
  77. {
  78. return i; //return first free slot
  79. }
  80. }
  81. return -1; //no slot available
  82. }
  83. int CCreatureSet::getStackCount(TSlot slot) const
  84. {
  85. TSlots::const_iterator i = stacks.find(slot);
  86. if (i != stacks.end())
  87. return i->second->count;
  88. else
  89. return 0; //TODO? consider issuing a warning
  90. }
  91. expType CCreatureSet::getStackExperience(TSlot slot) const
  92. {
  93. TSlots::const_iterator i = stacks.find(slot);
  94. if (i != stacks.end())
  95. return i->second->experience;
  96. else
  97. return 0; //TODO? consider issuing a warning
  98. }
  99. bool CCreatureSet::mergableStacks(std::pair<TSlot, TSlot> &out, TSlot preferable /*= -1*/) const /*looks for two same stacks, returns slot positions */
  100. {
  101. //try to match creature to our preferred stack
  102. if(preferable >= 0 && vstd::contains(stacks, preferable))
  103. {
  104. const CCreature *cr = stacks.find(preferable)->second->type;
  105. for(TSlots::const_iterator j=stacks.begin(); j!=stacks.end(); ++j)
  106. {
  107. if(cr == j->second->type && j->first != preferable)
  108. {
  109. out.first = preferable;
  110. out.second = j->first;
  111. return true;
  112. }
  113. }
  114. }
  115. for(TSlots::const_iterator i=stacks.begin(); i!=stacks.end(); ++i)
  116. {
  117. for(TSlots::const_iterator j=stacks.begin(); j!=stacks.end(); ++j)
  118. {
  119. if(i->second->type == j->second->type && i->first != j->first)
  120. {
  121. out.first = i->first;
  122. out.second = j->first;
  123. return true;
  124. }
  125. }
  126. }
  127. return false;
  128. }
  129. void CCreatureSet::sweep()
  130. {
  131. for(TSlots::iterator i=stacks.begin(); i!=stacks.end(); ++i)
  132. {
  133. if(!i->second->count)
  134. {
  135. stacks.erase(i);
  136. sweep();
  137. break;
  138. }
  139. }
  140. }
  141. void CCreatureSet::addToSlot(TSlot slot, TCreature cre, TQuantity count, bool allowMerging/* = true*/)
  142. {
  143. const CCreature *c = VLC->creh->creatures[cre];
  144. if(!hasStackAtSlot(slot))
  145. {
  146. setCreature(slot, cre, count);
  147. }
  148. else if(getCreature(slot) == c && allowMerging) //that slot was empty or contained same type creature
  149. {
  150. setStackCount(slot, getStackCount(slot) + count);
  151. }
  152. else
  153. {
  154. tlog1 << "Failed adding to slot!\n";
  155. }
  156. }
  157. void CCreatureSet::addToSlot(TSlot slot, CStackInstance *stack, bool allowMerging/* = true*/)
  158. {
  159. assert(stack->valid(true));
  160. if(!hasStackAtSlot(slot))
  161. {
  162. putStack(slot, stack);
  163. }
  164. else if(allowMerging && stack->type == getCreature(slot))
  165. {
  166. joinStack(slot, stack);
  167. }
  168. else
  169. {
  170. tlog1 << "Cannot add to slot " << slot << " stack " << *stack << std::endl;
  171. }
  172. }
  173. bool CCreatureSet::validTypes(bool allowUnrandomized /*= false*/) const
  174. {
  175. for(TSlots::const_iterator i=stacks.begin(); i!=stacks.end(); ++i)
  176. {
  177. if(!i->second->valid(allowUnrandomized))
  178. return false;
  179. }
  180. return true;
  181. }
  182. bool CCreatureSet::slotEmpty(TSlot slot) const
  183. {
  184. return !hasStackAtSlot(slot);
  185. }
  186. bool CCreatureSet::needsLastStack() const
  187. {
  188. return false;
  189. }
  190. ui64 CCreatureSet::getArmyStrength() const
  191. {
  192. ui64 ret = 0;
  193. for(TSlots::const_iterator i = stacks.begin(); i != stacks.end(); i++)
  194. ret += i->second->type->AIValue * i->second->count;
  195. return ret;
  196. }
  197. ui64 CCreatureSet::getPower (TSlot slot) const
  198. {
  199. return getCreature(slot)->AIValue * getStackCount(slot);
  200. }
  201. std::string CCreatureSet::getRoughAmount (TSlot slot) const
  202. {
  203. return VLC->generaltexth->arraytxt[174 + 3*CCreature::getQuantityID(getStackCount(slot))];
  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 (STACK_EXP && count > stacks[slot]->count)
  218. stacks[slot]->experience *= (count/(float)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. delNull(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. delNull(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() - 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. nodeType = 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 (iswith(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. amin (val, 100);
  448. return val;
  449. }
  450. void CStackInstance::giveStackExp(expType exp)
  451. {
  452. int level = type->level;
  453. if (!iswith(level, 1, 7))
  454. level = 0;
  455. CCreatureHandler * creh = VLC->creh;
  456. ui32 maxExp = creh->expRanks[level].back();
  457. amin(exp, (expType)maxExp); //prevent exp overflow due to different types
  458. amin(exp, (maxExp * creh->maxExpPerBattle[level])/100);
  459. 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) && STACK_EXP)
  474. experience *= VLC->creh->expAfterUpgrade / 100.0f;
  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_OBSTACLES_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::LIFE_DRAIN: //TODO: chance, hp percentage?
  508. case Bonus::SELF_MORALE:
  509. case Bonus::SELF_LUCK:
  510. case Bonus::FEAR:
  511. case Bonus::FEARLESS:
  512. case Bonus::CHARGE_IMMUNITY:
  513. case Bonus::HEALER:
  514. case Bonus::CATAPULT:
  515. case Bonus::DRAGON_NATURE:
  516. case Bonus::NON_LIVING:
  517. case Bonus::UNDEAD:
  518. case Bonus::FIRE_IMMUNITY: //TODO: what about direct, hostile and total immunity?
  519. case Bonus::WATER_IMMUNITY:
  520. case Bonus::AIR_IMMUNITY:
  521. case Bonus::EARTH_IMMUNITY:
  522. case Bonus::RECEPTIVE:
  523. case Bonus::DIRECT_DAMAGE_IMMUNITY:
  524. break;
  525. //One numeric value. magic resistance handled separately
  526. case Bonus::SPELL_RESISTANCE_AURA:
  527. case Bonus::SPELL_DAMAGE_REDUCTION:
  528. case Bonus::LEVEL_SPELL_IMMUNITY:
  529. case Bonus::MANA_DRAIN:
  530. case Bonus::HP_REGENERATION:
  531. case Bonus::ADDITIONAL_RETALIATION:
  532. case Bonus::DOUBLE_DAMAGE_CHANCE:
  533. case Bonus::DARKNESS: //Darkness Dragons any1?
  534. boost::algorithm::replace_first(text, "%d", boost::lexical_cast<std::string>(valOfBonuses(Selector::typeSubtype(bonus->type, bonus->subtype))));
  535. break;
  536. //Complex descriptions
  537. case Bonus::SECONDARY_SKILL_PREMY: //only if there's no simple MR
  538. if (bonus->subtype == CGHeroInstance::RESISTANCE)
  539. {
  540. if (!hasBonusOfType(Bonus::MAGIC_RESISTANCE))
  541. boost::algorithm::replace_first(text, "%d", boost::lexical_cast<std::string>( magicResistance() ));
  542. }
  543. break;
  544. case Bonus::MAGIC_RESISTANCE:
  545. boost::algorithm::replace_first(text, "%d", boost::lexical_cast<std::string>( magicResistance() ));
  546. break;
  547. case Bonus::HATE:
  548. boost::algorithm::replace_first(text, "%d", boost::lexical_cast<std::string>(valOfBonuses(Selector::typeSubtype(bonus->type, bonus->subtype))));
  549. boost::algorithm::replace_first(text, "%s", VLC->creh->creatures[bonus->subtype]->namePl);
  550. break;
  551. case Bonus::SPELL_AFTER_ATTACK:
  552. {
  553. boost::algorithm::replace_first(text, "%d", boost::lexical_cast<std::string>(valOfBonuses(Selector::typeSubtype(bonus->type, bonus->subtype))));
  554. boost::algorithm::replace_first(text, "%s", VLC->spellh->spells[bonus->subtype]->name);
  555. break;
  556. }
  557. default:
  558. {}//TODO: allow custom bonus types... someday, somehow
  559. }
  560. }
  561. else //short name
  562. {
  563. text = it->second.first;
  564. switch (bonus->type)
  565. {
  566. case Bonus::MANA_CHANNELING:
  567. case Bonus::MAGIC_MIRROR:
  568. case Bonus::CHANGES_SPELL_COST_FOR_ALLY:
  569. case Bonus::CHANGES_SPELL_COST_FOR_ENEMY:
  570. case Bonus::ENEMY_DEFENCE_REDUCTION:
  571. case Bonus::DEATH_STARE:
  572. boost::algorithm::replace_first(text, "%d", boost::lexical_cast<std::string>(valOfBonuses(Selector::typeSubtype(bonus->type, bonus->subtype))));
  573. break;
  574. case Bonus::HATE:
  575. boost::algorithm::replace_first(text, "%s", VLC->creh->creatures[bonus->subtype]->namePl);
  576. break;
  577. case Bonus::LEVEL_SPELL_IMMUNITY:
  578. boost::algorithm::replace_first(text, "%d", boost::lexical_cast<std::string>(bonus->val));
  579. break;
  580. case Bonus::SPELL_AFTER_ATTACK:
  581. boost::algorithm::replace_first(text, "%s", VLC->spellh->spells[bonus->subtype]->name);
  582. break;
  583. case Bonus::SPELL_IMMUNITY:
  584. boost::algorithm::replace_first(text, "%s", VLC->spellh->spells[bonus->subtype]->name);
  585. break;
  586. case Bonus::SECONDARY_SKILL_PREMY:
  587. if (bonus->subtype != CGHeroInstance::RESISTANCE || hasBonusOfType(Bonus::MAGIC_RESISTANCE)) //handle it there
  588. text = "";
  589. break;
  590. }
  591. }
  592. return text;
  593. }
  594. else
  595. return "";
  596. }
  597. std::string CStackInstance::bonusToGraphics(Bonus *bonus) const
  598. {
  599. std::string fileName;
  600. switch (bonus->type)
  601. {
  602. //"E_ALIVE.bmp"
  603. //"E_ART.bmp"
  604. //"E_BLESS.bmp"
  605. //"E_BLOCK.bmp"
  606. //"E_BLOCK1.bmp"
  607. //"E_BLOCK2.bmp"
  608. case Bonus::TWO_HEX_ATTACK_BREATH:
  609. fileName = "E_BREATH.bmp"; break;
  610. case Bonus::SPELL_AFTER_ATTACK:
  611. fileName = "E_CAST.bmp"; break;
  612. //"E_CAST1.bmp"
  613. //"E_CAST2.bmp"
  614. //"E_CASTER.bmp"
  615. case Bonus::JOUSTING:
  616. fileName = "E_CHAMP.bmp"; break;
  617. case Bonus::DOUBLE_DAMAGE_CHANCE:
  618. fileName = "E_DBLOW.bmp"; break;
  619. case Bonus::DEATH_STARE:
  620. fileName = "E_DEATH.bmp"; break;
  621. //"E_DEFBON.bmp"
  622. case Bonus::NO_DISTANCE_PENALTY:
  623. fileName = "E_DIST.bmp"; break;
  624. case Bonus::ADDITIONAL_ATTACK:
  625. fileName = "E_DOUBLE.bmp"; break;
  626. case Bonus::DRAGON_NATURE:
  627. fileName = "E_DRAGON.bmp"; break;
  628. case Bonus::MAGIC_RESISTANCE:
  629. fileName = "E_DWARF.bmp"; break;
  630. case Bonus::SECONDARY_SKILL_PREMY:
  631. if (bonus->subtype == CGHeroInstance::RESISTANCE)
  632. {
  633. fileName = "E_DWARF.bmp";
  634. }
  635. break;
  636. case Bonus::FEAR:
  637. fileName = "E_FEAR.bmp"; break;
  638. case Bonus::FEARLESS:
  639. fileName = "E_FEARL.bmp"; break;
  640. case Bonus::FLYING:
  641. fileName = "E_FLY.bmp"; break;
  642. case Bonus::SPELL_DAMAGE_REDUCTION:
  643. fileName = "E_GOLEM.bmp"; break;
  644. case Bonus::RETURN_AFTER_STRIKE:
  645. fileName = "E_HARPY.bmp"; break;
  646. case Bonus::HATE:
  647. fileName = "E_HATE.bmp"; break;
  648. case Bonus::KING1:
  649. fileName = "E_KING1.bmp"; break;
  650. case Bonus::KING2:
  651. fileName = "E_KING2.bmp"; break;
  652. case Bonus::KING3:
  653. fileName = "E_KING3.bmp"; break;
  654. case Bonus::CHANGES_SPELL_COST_FOR_ALLY:
  655. fileName = "E_MANA.bmp"; break;
  656. case Bonus::NO_MELEE_PENALTY:
  657. fileName = "E_MELEE.bmp"; break;
  658. //"E_MIND.bmp"
  659. case Bonus::SELF_MORALE:
  660. fileName = "E_MINOT.bmp"; break;
  661. case Bonus::NO_MORALE:
  662. fileName = "E_MORAL.bmp"; break;
  663. case Bonus::RECEPTIVE:
  664. fileName = "E_NOFRIM.bmp"; break;
  665. case Bonus::NO_OBSTACLES_PENALTY:
  666. fileName = "E_OBST.bmp"; break;
  667. case Bonus::ENEMY_DEFENCE_REDUCTION:
  668. fileName = "E_RDEF.bmp"; break;
  669. //"E_REBIRTH.bmp"
  670. case Bonus::BLOCKS_RETALIATION:
  671. fileName = "E_RETAIL.bmp"; break;
  672. case Bonus::ADDITIONAL_RETALIATION:
  673. fileName = "E_RETAIL1.bmp"; break;
  674. case Bonus::ATTACKS_ALL_ADJACENT:
  675. fileName = "E_ROUND.bmp"; break;
  676. //"E_SGNUM.bmp"
  677. //"E_SGTYPE.bmp"
  678. case Bonus::SHOOTER:
  679. fileName = "E_SHOOT.bmp"; break;
  680. case Bonus::FREE_SHOOTING: //shooter is not blocked by enemy
  681. fileName = "E_SHOOTA.bmp"; break;
  682. //"E_SHOOTN.bmp"
  683. case Bonus::SPELL_IMMUNITY:
  684. {
  685. switch (bonus->subtype)
  686. {
  687. case 62: //Blind
  688. fileName = "E_SPBLIND.bmp"; break;
  689. case 35: // Dispell
  690. fileName = "E_SPDISP.bmp"; break;
  691. case 78: // Dispell beneficial spells
  692. fileName = "E_SPDISB.bmp"; break;
  693. case 60: //Hypnotize
  694. fileName = "E_SPHYPN.bmp"; break;
  695. case 18: //Implosion
  696. fileName = "E_SPIMP.bmp"; break;
  697. case 59: //Berserk
  698. fileName = "E_SPBERS.bmp"; break;
  699. case 23: //Meteor Shower
  700. fileName = "E_SPMET.bmp"; break;
  701. case 26: //Armageddon
  702. fileName = "E_SPARM.bmp"; break;
  703. case 54: //Slow
  704. fileName = "E_SPSLOW.bmp"; break;
  705. //TODO: some generic spell handling?
  706. }
  707. break;
  708. }
  709. //"E_SPAWILL.bmp"
  710. case Bonus::DIRECT_DAMAGE_IMMUNITY:
  711. fileName = "E_SPDIR.bmp"; break;
  712. //"E_SPDISB.bmp"
  713. //"E_SPDISP.bmp"
  714. //"E_SPEATH.bmp"
  715. //"E_SPEATH1.bmp"
  716. case Bonus::FIRE_IMMUNITY:
  717. switch (bonus->subtype)
  718. {
  719. case 0:
  720. fileName = "E_SPFIRE.bmp"; break; //all
  721. case 1:
  722. fileName = "E_SPFIRE1.bmp"; break; //not positive
  723. case 2:
  724. fileName = "E_FIRE.bmp"; break; //direct damage
  725. }
  726. break;
  727. case Bonus::WATER_IMMUNITY:
  728. switch (bonus->subtype)
  729. {
  730. case 0:
  731. fileName = "E_SPWATER.bmp"; break; //all
  732. case 1:
  733. fileName = "E_SPWATER1.bmp"; break; //not positive
  734. case 2:
  735. fileName = "E_SPCOLD.bmp"; break; //direct damage
  736. }
  737. break;
  738. case Bonus::AIR_IMMUNITY:
  739. switch (bonus->subtype)
  740. {
  741. case 0:
  742. fileName = "E_SPAIR.bmp"; break; //all
  743. case 1:
  744. fileName = "E_SPAIR1.bmp"; break; //not positive
  745. case 2:
  746. fileName = "E_LIGHT.bmp"; break;//direct damage
  747. }
  748. break;
  749. case Bonus::EARTH_IMMUNITY:
  750. switch (bonus->subtype)
  751. {
  752. case 0:
  753. fileName = "E_SPEATH.bmp"; break; //all
  754. case 1:
  755. case 2: //no specific icon for direct damage immunity
  756. fileName = "E_SPEATH1.bmp"; break; //not positive
  757. }
  758. break;
  759. case Bonus::LEVEL_SPELL_IMMUNITY:
  760. {
  761. if (iswith(bonus->val, 1 , 5))
  762. {
  763. fileName = "E_SPLVL" + boost::lexical_cast<std::string>(bonus->val) + ".bmp";
  764. }
  765. break;
  766. }
  767. //"E_SPWATER.bmp"
  768. //"E_SPWATER1.bmp"
  769. //"E_SUMMON.bmp"
  770. //"E_SUMMON1.bmp"
  771. //"E_SUMMON2.bmp"
  772. case Bonus::FULL_HP_REGENERATION:
  773. fileName = "E_TROLL.bmp"; break;
  774. case Bonus::UNDEAD:
  775. fileName = "E_UNDEAD.bmp"; break;
  776. case Bonus::SPELL_RESISTANCE_AURA:
  777. fileName = "E_UNIC.bmp"; break;
  778. }
  779. if(!fileName.empty())
  780. fileName = "zvs/Lib1.res/" + fileName;
  781. return fileName;
  782. }
  783. void CStackInstance::setArmyObj(const CArmedInstance *ArmyObj)
  784. {
  785. if(_armyObj)
  786. detachFrom(const_cast<CArmedInstance*>(_armyObj));
  787. _armyObj = ArmyObj;
  788. if(ArmyObj)
  789. {
  790. attachTo(const_cast<CArmedInstance*>(_armyObj));
  791. }
  792. }
  793. // void CStackInstance::getParents(TCNodes &out, const CBonusSystemNode *source /*= NULL*/) const
  794. // {
  795. // out.insert(type);
  796. //
  797. // if(source && source != this) //we should be root, if not - do not inherit anything
  798. // return;
  799. //
  800. // if(armyObj)
  801. // out.insert(armyObj);
  802. // else
  803. // out.insert(&IObjectInterface::cb->gameState()->globalEffects);
  804. // }
  805. std::string CStackInstance::getQuantityTXT(bool capitalized /*= true*/) const
  806. {
  807. return VLC->generaltexth->arraytxt[174 + getQuantityID()*3 + 2 - capitalized];
  808. }
  809. bool CStackInstance::valid(bool allowUnrandomized) const
  810. {
  811. bool isRand = (idRand != -1);
  812. if(!isRand)
  813. {
  814. return (type && type == VLC->creh->creatures[type->idNumber]);
  815. }
  816. else
  817. return allowUnrandomized;
  818. }
  819. CStackInstance::~CStackInstance()
  820. {
  821. }
  822. std::string CStackInstance::nodeName() const
  823. {
  824. std::ostringstream oss;
  825. oss << "Stack of " << count << " of ";
  826. if(type)
  827. oss << type->namePl;
  828. else if(idRand)
  829. oss << "[no type, idRand=" << idRand << "]";
  830. else
  831. oss << "[UNDEFINED TYPE]";
  832. return oss.str();
  833. }
  834. void CStackInstance::deserializationFix()
  835. {
  836. const CCreature *backup = type;
  837. type = NULL;
  838. setType(backup);
  839. const CArmedInstance *armyBackup = _armyObj;
  840. _armyObj = NULL;
  841. setArmyObj(armyBackup);
  842. }
  843. int CStackInstance::getCreatureID() const
  844. {
  845. if(type)
  846. return type->idNumber;
  847. else
  848. return -1;
  849. }
  850. CStackBasicDescriptor::CStackBasicDescriptor()
  851. {
  852. type = NULL;
  853. count = -1;
  854. }
  855. CStackBasicDescriptor::CStackBasicDescriptor(TCreature id, TQuantity Count)
  856. : type (VLC->creh->creatures[id]), count(Count)
  857. {
  858. }
  859. CStackBasicDescriptor::CStackBasicDescriptor(const CCreature *c, TQuantity Count)
  860. : type(c), count(Count)
  861. {
  862. }
  863. DLL_EXPORT std::ostream & operator<<(std::ostream & str, const CStackInstance & sth)
  864. {
  865. if(!sth.valid(true))
  866. str << "an invalid stack!";
  867. str << "stack with " << sth.count << " of ";
  868. if(sth.type)
  869. str << sth.type->namePl;
  870. else
  871. str << sth.idRand;
  872. return str;
  873. }
  874. void CSimpleArmy::clear()
  875. {
  876. army.clear();
  877. }
  878. CSimpleArmy::operator bool() const
  879. {
  880. return army.size();
  881. }
  882. bool CSimpleArmy::setCreature(TSlot slot, TCreature cre, TQuantity count)
  883. {
  884. assert(!vstd::contains(army, slot));
  885. army[slot] = CStackBasicDescriptor(cre, count);
  886. return true;
  887. }