CCreatureSet.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996
  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: //TODO: what about direct, hostile and total 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. 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. case Bonus::SPELL_BEFORE_ATTACK:
  582. boost::algorithm::replace_first(text, "%s", VLC->spellh->spells[bonus->subtype]->name);
  583. break;
  584. case Bonus::SPELL_IMMUNITY:
  585. boost::algorithm::replace_first(text, "%s", VLC->spellh->spells[bonus->subtype]->name);
  586. break;
  587. case Bonus::MAGIC_RESISTANCE:
  588. text = ""; //handled separately
  589. break;
  590. //case Bonus::SECONDARY_SKILL_PREMY:
  591. // if (bonus->subtype != CGHeroInstance::RESISTANCE || hasBonusOfType(Bonus::MAGIC_RESISTANCE)) //handle it there
  592. // text = "";
  593. // break;
  594. }
  595. }
  596. return text;
  597. }
  598. else
  599. return "";
  600. }
  601. std::string CStackInstance::bonusToGraphics(Bonus *bonus) const
  602. {
  603. std::string fileName;
  604. switch (bonus->type)
  605. {
  606. //"E_ALIVE.bmp"
  607. //"E_ART.bmp"
  608. //"E_BLESS.bmp"
  609. //"E_BLOCK.bmp"
  610. //"E_BLOCK1.bmp"
  611. //"E_BLOCK2.bmp"
  612. case Bonus::TWO_HEX_ATTACK_BREATH:
  613. fileName = "E_BREATH.bmp"; break;
  614. case Bonus::SPELL_AFTER_ATTACK:
  615. fileName = "E_CAST.bmp"; break;
  616. //"E_CAST1.bmp"
  617. case Bonus::SPELL_BEFORE_ATTACK:
  618. fileName ="E_CAST2.bmp"; break;
  619. //"E_CASTER.bmp"
  620. case Bonus::JOUSTING:
  621. fileName = "E_CHAMP.bmp"; break;
  622. case Bonus::DOUBLE_DAMAGE_CHANCE:
  623. fileName = "E_DBLOW.bmp"; break;
  624. case Bonus::DEATH_STARE:
  625. fileName = "E_DEATH.bmp"; break;
  626. case Bonus::DEFENSIVE_STANCE:
  627. fileName = "E_DEFBON.bmp"; break;
  628. case Bonus::NO_DISTANCE_PENALTY:
  629. fileName = "E_DIST.bmp"; break;
  630. case Bonus::ADDITIONAL_ATTACK:
  631. fileName = "E_DOUBLE.bmp"; break;
  632. case Bonus::DRAGON_NATURE:
  633. fileName = "E_DRAGON.bmp"; break;
  634. case Bonus::MAGIC_RESISTANCE:
  635. fileName = "E_DWARF.bmp"; break;
  636. case Bonus::SECONDARY_SKILL_PREMY:
  637. if (bonus->subtype == CGHeroInstance::RESISTANCE)
  638. {
  639. fileName = "E_DWARF.bmp";
  640. }
  641. break;
  642. case Bonus::FEAR:
  643. fileName = "E_FEAR.bmp"; break;
  644. case Bonus::FEARLESS:
  645. fileName = "E_FEARL.bmp"; break;
  646. case Bonus::FLYING:
  647. fileName = "E_FLY.bmp"; break;
  648. case Bonus::SPELL_DAMAGE_REDUCTION:
  649. fileName = "E_GOLEM.bmp"; break;
  650. case Bonus::RETURN_AFTER_STRIKE:
  651. fileName = "E_HARPY.bmp"; break;
  652. case Bonus::HATE:
  653. fileName = "E_HATE.bmp"; break;
  654. case Bonus::KING1:
  655. fileName = "E_KING1.bmp"; break;
  656. case Bonus::KING2:
  657. fileName = "E_KING2.bmp"; break;
  658. case Bonus::KING3:
  659. fileName = "E_KING3.bmp"; break;
  660. case Bonus::CHANGES_SPELL_COST_FOR_ALLY:
  661. fileName = "E_MANA.bmp"; break;
  662. case Bonus::NO_MELEE_PENALTY:
  663. fileName = "E_MELEE.bmp"; break;
  664. //"E_MIND.bmp"
  665. case Bonus::SELF_MORALE:
  666. fileName = "E_MINOT.bmp"; break;
  667. case Bonus::NO_MORALE:
  668. fileName = "E_MORAL.bmp"; break;
  669. case Bonus::RECEPTIVE:
  670. fileName = "E_NOFRIM.bmp"; break;
  671. case Bonus::NO_WALL_PENALTY:
  672. fileName = "E_OBST.bmp"; break;
  673. case Bonus::ENEMY_DEFENCE_REDUCTION:
  674. fileName = "E_RDEF.bmp"; break;
  675. case Bonus::REBIRTH:
  676. fileName = "E_REBIRTH.bmp"; break;
  677. case Bonus::BLOCKS_RETALIATION:
  678. fileName = "E_RETAIL.bmp"; break;
  679. case Bonus::ADDITIONAL_RETALIATION:
  680. fileName = "E_RETAIL1.bmp"; break;
  681. case Bonus::ATTACKS_ALL_ADJACENT:
  682. fileName = "E_ROUND.bmp"; break;
  683. //"E_SGNUM.bmp"
  684. //"E_SGTYPE.bmp"
  685. case Bonus::SHOOTER:
  686. fileName = "E_SHOOT.bmp"; break;
  687. case Bonus::FREE_SHOOTING: //shooter is not blocked by enemy
  688. fileName = "E_SHOOTA.bmp"; break;
  689. //"E_SHOOTN.bmp"
  690. case Bonus::SPELL_IMMUNITY:
  691. {
  692. switch (bonus->subtype)
  693. {
  694. case 62: //Blind
  695. fileName = "E_SPBLIND.bmp"; break;
  696. case 35: // Dispell
  697. fileName = "E_SPDISP.bmp"; break;
  698. case 78: // Dispell beneficial spells
  699. fileName = "E_SPDISB.bmp"; break;
  700. case 60: //Hypnotize
  701. fileName = "E_SPHYPN.bmp"; break;
  702. case 18: //Implosion
  703. fileName = "E_SPIMP.bmp"; break;
  704. case 59: //Berserk
  705. fileName = "E_SPBERS.bmp"; break;
  706. case 23: //Meteor Shower
  707. fileName = "E_SPMET.bmp"; break;
  708. case 26: //Armageddon
  709. fileName = "E_SPARM.bmp"; break;
  710. case 54: //Slow
  711. fileName = "E_SPSLOW.bmp"; break;
  712. //TODO: some generic spell handling?
  713. }
  714. break;
  715. }
  716. //"E_SPAWILL.bmp"
  717. case Bonus::DIRECT_DAMAGE_IMMUNITY:
  718. fileName = "E_SPDIR.bmp"; break;
  719. //"E_SPDISB.bmp"
  720. //"E_SPDISP.bmp"
  721. //"E_SPEATH.bmp"
  722. //"E_SPEATH1.bmp"
  723. case Bonus::FIRE_IMMUNITY:
  724. switch (bonus->subtype)
  725. {
  726. case 0:
  727. fileName = "E_SPFIRE.bmp"; break; //all
  728. case 1:
  729. fileName = "E_SPFIRE1.bmp"; break; //not positive
  730. case 2:
  731. fileName = "E_FIRE.bmp"; break; //direct damage
  732. }
  733. break;
  734. case Bonus::WATER_IMMUNITY:
  735. switch (bonus->subtype)
  736. {
  737. case 0:
  738. fileName = "E_SPWATER.bmp"; break; //all
  739. case 1:
  740. fileName = "E_SPWATER1.bmp"; break; //not positive
  741. case 2:
  742. fileName = "E_SPCOLD.bmp"; break; //direct damage
  743. }
  744. break;
  745. case Bonus::AIR_IMMUNITY:
  746. switch (bonus->subtype)
  747. {
  748. case 0:
  749. fileName = "E_SPAIR.bmp"; break; //all
  750. case 1:
  751. fileName = "E_SPAIR1.bmp"; break; //not positive
  752. case 2:
  753. fileName = "E_LIGHT.bmp"; break;//direct damage
  754. }
  755. break;
  756. case Bonus::EARTH_IMMUNITY:
  757. switch (bonus->subtype)
  758. {
  759. case 0:
  760. fileName = "E_SPEATH.bmp"; break; //all
  761. case 1:
  762. case 2: //no specific icon for direct damage immunity
  763. fileName = "E_SPEATH1.bmp"; break; //not positive
  764. }
  765. break;
  766. case Bonus::LEVEL_SPELL_IMMUNITY:
  767. {
  768. if (vstd::iswithin(bonus->val, 1 , 5))
  769. {
  770. fileName = "E_SPLVL" + boost::lexical_cast<std::string>(bonus->val) + ".bmp";
  771. }
  772. break;
  773. }
  774. //"E_SPWATER.bmp"
  775. //"E_SPWATER1.bmp"
  776. //"E_SUMMON.bmp"
  777. //"E_SUMMON1.bmp"
  778. //"E_SUMMON2.bmp"
  779. case Bonus::FULL_HP_REGENERATION:
  780. fileName = "E_TROLL.bmp"; break;
  781. case Bonus::UNDEAD:
  782. fileName = "E_UNDEAD.bmp"; break;
  783. case Bonus::SPELL_RESISTANCE_AURA:
  784. fileName = "E_UNIC.bmp"; break;
  785. }
  786. if(!fileName.empty())
  787. fileName = "zvs/Lib1.res/" + fileName;
  788. return fileName;
  789. }
  790. void CStackInstance::setArmyObj(const CArmedInstance *ArmyObj)
  791. {
  792. if(_armyObj)
  793. detachFrom(const_cast<CArmedInstance*>(_armyObj));
  794. _armyObj = ArmyObj;
  795. if(ArmyObj)
  796. {
  797. attachTo(const_cast<CArmedInstance*>(_armyObj));
  798. }
  799. }
  800. // void CStackInstance::getParents(TCNodes &out, const CBonusSystemNode *source /*= NULL*/) const
  801. // {
  802. // out.insert(type);
  803. //
  804. // if(source && source != this) //we should be root, if not - do not inherit anything
  805. // return;
  806. //
  807. // if(armyObj)
  808. // out.insert(armyObj);
  809. // else
  810. // out.insert(&IObjectInterface::cb->gameState()->globalEffects);
  811. // }
  812. std::string CStackInstance::getQuantityTXT(bool capitalized /*= true*/) const
  813. {
  814. return VLC->generaltexth->arraytxt[174 + getQuantityID()*3 + 2 - capitalized];
  815. }
  816. bool CStackInstance::valid(bool allowUnrandomized) const
  817. {
  818. bool isRand = (idRand != -1);
  819. if(!isRand)
  820. {
  821. return (type && type == VLC->creh->creatures[type->idNumber]);
  822. }
  823. else
  824. return allowUnrandomized;
  825. }
  826. CStackInstance::~CStackInstance()
  827. {
  828. }
  829. std::string CStackInstance::nodeName() const
  830. {
  831. std::ostringstream oss;
  832. oss << "Stack of " << count << " of ";
  833. if(type)
  834. oss << type->namePl;
  835. else if(idRand)
  836. oss << "[no type, idRand=" << idRand << "]";
  837. else
  838. oss << "[UNDEFINED TYPE]";
  839. return oss.str();
  840. }
  841. void CStackInstance::deserializationFix()
  842. {
  843. const CCreature *backup = type;
  844. type = NULL;
  845. setType(backup);
  846. const CArmedInstance *armyBackup = _armyObj;
  847. _armyObj = NULL;
  848. setArmyObj(armyBackup);
  849. }
  850. int CStackInstance::getCreatureID() const
  851. {
  852. if(type)
  853. return type->idNumber;
  854. else
  855. return -1;
  856. }
  857. std::string CStackInstance::getName() const
  858. {
  859. return (count > 1) ? type->namePl : type->nameSing;
  860. }
  861. ui64 CStackInstance::getPower() const
  862. {
  863. assert(type);
  864. return type->AIValue * count;
  865. }
  866. CStackBasicDescriptor::CStackBasicDescriptor()
  867. {
  868. type = NULL;
  869. count = -1;
  870. }
  871. CStackBasicDescriptor::CStackBasicDescriptor(TCreature id, TQuantity Count)
  872. : type (VLC->creh->creatures[id]), count(Count)
  873. {
  874. }
  875. CStackBasicDescriptor::CStackBasicDescriptor(const CCreature *c, TQuantity Count)
  876. : type(c), count(Count)
  877. {
  878. }
  879. DLL_LINKAGE std::ostream & operator<<(std::ostream & str, const CStackInstance & sth)
  880. {
  881. if(!sth.valid(true))
  882. str << "an invalid stack!";
  883. str << "stack with " << sth.count << " of ";
  884. if(sth.type)
  885. str << sth.type->namePl;
  886. else
  887. str << sth.idRand;
  888. return str;
  889. }
  890. void CSimpleArmy::clear()
  891. {
  892. army.clear();
  893. }
  894. CSimpleArmy::operator bool() const
  895. {
  896. return army.size();
  897. }
  898. bool CSimpleArmy::setCreature(TSlot slot, TCreature cre, TQuantity count)
  899. {
  900. assert(!vstd::contains(army, slot));
  901. army[slot] = CStackBasicDescriptor(cre, count);
  902. return true;
  903. }