CCreatureSet.cpp 25 KB

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