CCreatureSet.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932
  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. int CCreatureSet::getArmyStrength() const
  191. {
  192. int 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 *stacks.find(slot)->second;
  243. }
  244. void CCreatureSet::eraseStack(TSlot slot)
  245. {
  246. assert(hasStackAtSlot(slot));
  247. CStackInstance *toErase = detachStack(slot);
  248. delNull(toErase);
  249. }
  250. bool CCreatureSet::contains(const CStackInstance *stack) const
  251. {
  252. if(!stack)
  253. return false;
  254. for(TSlots::const_iterator i = stacks.begin(); i != stacks.end(); ++i)
  255. if(i->second == stack)
  256. return true;
  257. return false;
  258. }
  259. TSlot CCreatureSet::findStack(const CStackInstance *stack) const
  260. {
  261. if(!stack)
  262. return -1;
  263. for(TSlots::const_iterator i = stacks.begin(); i != stacks.end(); ++i)
  264. if(i->second == stack)
  265. return i->first;
  266. return -1;
  267. }
  268. CArmedInstance * CCreatureSet::castToArmyObj()
  269. {
  270. return dynamic_cast<CArmedInstance *>(this);
  271. }
  272. void CCreatureSet::putStack(TSlot slot, CStackInstance *stack)
  273. {
  274. assert(!hasStackAtSlot(slot));
  275. stacks[slot] = stack;
  276. stack->setArmyObj(castToArmyObj());
  277. armyChanged();
  278. }
  279. void CCreatureSet::joinStack(TSlot slot, CStackInstance * stack)
  280. {
  281. const CCreature *c = getCreature(slot);
  282. assert(c == stack->type);
  283. assert(c);
  284. //TODO move stuff
  285. changeStackCount(slot, stack->count);
  286. delNull(stack);
  287. }
  288. void CCreatureSet::changeStackCount(TSlot slot, TQuantity toAdd)
  289. {
  290. setStackCount(slot, getStackCount(slot) + toAdd);
  291. }
  292. CCreatureSet::CCreatureSet()
  293. {
  294. formation = false;
  295. }
  296. CCreatureSet::CCreatureSet(const CCreatureSet&)
  297. {
  298. assert(0);
  299. }
  300. CCreatureSet::~CCreatureSet()
  301. {
  302. clear();
  303. }
  304. void CCreatureSet::setToArmy(CSimpleArmy &src)
  305. {
  306. clear();
  307. while(src)
  308. {
  309. TSimpleSlots::iterator i = src.army.begin();
  310. assert(i->second.type);
  311. assert(i->second.count);
  312. putStack(i->first, new CStackInstance(i->second.type, i->second.count));
  313. src.army.erase(i);
  314. }
  315. }
  316. CStackInstance * CCreatureSet::detachStack(TSlot slot)
  317. {
  318. assert(hasStackAtSlot(slot));
  319. CStackInstance *ret = stacks[slot];
  320. //if(CArmedInstance *armedObj = castToArmyObj())
  321. {
  322. ret->setArmyObj(NULL); //detaches from current armyobj
  323. }
  324. assert(!ret->armyObj); //we failed detaching?
  325. stacks.erase(slot);
  326. armyChanged();
  327. return ret;
  328. }
  329. void CCreatureSet::setStackType(TSlot slot, const CCreature *type)
  330. {
  331. assert(hasStackAtSlot(slot));
  332. CStackInstance *s = stacks[slot];
  333. s->setType(type->idNumber);
  334. armyChanged();
  335. }
  336. bool CCreatureSet::canBeMergedWith(const CCreatureSet &cs, bool allowMergingStacks) const
  337. {
  338. if(!allowMergingStacks)
  339. {
  340. int freeSlots = stacksCount() - ARMY_SIZE;
  341. std::set<const CCreature*> cresToAdd;
  342. for(TSlots::const_iterator i = cs.stacks.begin(); i != cs.stacks.end(); i++)
  343. {
  344. TSlot dest = getSlotFor(i->second->type);
  345. if(dest < 0 || hasStackAtSlot(dest))
  346. cresToAdd.insert(i->second->type);
  347. }
  348. return cresToAdd.size() <= freeSlots;
  349. }
  350. else
  351. {
  352. CCreatureSet cres;
  353. //get types of creatures that need their own slot
  354. for(TSlots::const_iterator i = cs.stacks.begin(); i != cs.stacks.end(); i++)
  355. cres.addToSlot(i->first, i->second->type->idNumber, 1, true);
  356. TSlot j;
  357. for(TSlots::const_iterator i = stacks.begin(); i != stacks.end(); i++)
  358. {
  359. if ((j = cres.getSlotFor(i->second->type)) >= 0)
  360. cres.addToSlot(j, i->second->type->idNumber, 1, true); //merge if possible
  361. else
  362. return false; //no place found
  363. }
  364. return true; //all stacks found their slots
  365. }
  366. }
  367. bool CCreatureSet::hasStackAtSlot(TSlot slot) const
  368. {
  369. return vstd::contains(stacks, slot);
  370. }
  371. CCreatureSet & CCreatureSet::operator=(const CCreatureSet&cs)
  372. {
  373. assert(0);
  374. return *this;
  375. }
  376. void CCreatureSet::armyChanged()
  377. {
  378. }
  379. CStackInstance::CStackInstance()
  380. : armyObj(_armyObj)
  381. {
  382. init();
  383. }
  384. CStackInstance::CStackInstance(TCreature id, TQuantity Count)
  385. : armyObj(_armyObj)
  386. {
  387. init();
  388. setType(id);
  389. count = Count;
  390. }
  391. CStackInstance::CStackInstance(const CCreature *cre, TQuantity Count)
  392. : armyObj(_armyObj)
  393. {
  394. init();
  395. setType(cre);
  396. count = Count;
  397. }
  398. void CStackInstance::init()
  399. {
  400. experience = 0;
  401. count = 0;
  402. type = NULL;
  403. idRand = -1;
  404. _armyObj = NULL;
  405. nodeType = STACK_INSTANCE;
  406. }
  407. int CStackInstance::getQuantityID() const
  408. {
  409. return CCreature::getQuantityID(count);
  410. }
  411. int CStackInstance::getExpRank() const
  412. {
  413. int tier = type->level;
  414. if (iswith(tier, 1, 7))
  415. {
  416. for (int i = VLC->creh->expRanks[tier].size()-2; i >-1; --i)//sic!
  417. { //exp values vary from 1st level to max exp at 11th level
  418. if (experience >= VLC->creh->expRanks[tier][i])
  419. return ++i; //faster, but confusing - 0 index mean 1st level of experience
  420. }
  421. return 0;
  422. }
  423. else //higher tier
  424. {
  425. for (int i = VLC->creh->expRanks[0].size()-2; i >-1; --i)
  426. {
  427. if (experience >= VLC->creh->expRanks[0][i])
  428. return ++i;
  429. }
  430. return 0;
  431. }
  432. }
  433. void CStackInstance::giveStackExp(expType exp)
  434. {
  435. int level = type->level;
  436. if (!iswith(level, 1, 7))
  437. level = 0;
  438. CCreatureHandler * creh = VLC->creh;
  439. ui32 maxExp = creh->expRanks[level].back();
  440. amin(exp, (expType)maxExp); //prevent exp overflow due to different types
  441. amin(exp, (maxExp * creh->maxExpPerBattle[level])/100);
  442. amin(experience += exp, maxExp); //can't get more exp than this limit
  443. }
  444. void CStackInstance::setType(int creID)
  445. {
  446. if(creID >= 0 && creID < VLC->creh->creatures.size())
  447. setType(VLC->creh->creatures[creID]);
  448. else
  449. setType((const CCreature*)NULL);
  450. }
  451. void CStackInstance::setType(const CCreature *c)
  452. {
  453. if(type)
  454. {
  455. detachFrom(const_cast<CCreature*>(type));
  456. if (type->isMyUpgrade(c) && STACK_EXP)
  457. experience *= VLC->creh->expAfterUpgrade / 100.0f;
  458. }
  459. type = c;
  460. if(type)
  461. attachTo(const_cast<CCreature*>(type));
  462. }
  463. std::string CStackInstance::bonusToString(Bonus *bonus, bool description) const
  464. {
  465. std::map<TBonusType, std::pair<std::string, std::string> >::iterator it = VLC->creh->stackBonuses.find(bonus->type);
  466. if (it != VLC->creh->stackBonuses.end())
  467. {
  468. std::string text;
  469. if (description) //long ability description
  470. {
  471. text = it->second.second;
  472. switch (bonus->type)
  473. {
  474. //no additional modifiers needed
  475. case Bonus::FLYING:
  476. case Bonus::UNLIMITED_RETALIATIONS:
  477. case Bonus::SHOOTER:
  478. case Bonus::FREE_SHOOTING:
  479. case Bonus::NO_SHOTING_PENALTY:
  480. case Bonus::NO_MELEE_PENALTY:
  481. case Bonus::NO_DISTANCE_PENALTY:
  482. case Bonus::NO_OBSTACLES_PENALTY:
  483. case Bonus::JOUSTING: //TODO: percent bonus?
  484. case Bonus::RETURN_AFTER_STRIKE:
  485. case Bonus::BLOCKS_RETALIATION:
  486. case Bonus::TWO_HEX_ATTACK_BREATH:
  487. case Bonus::THREE_HEADED_ATTACK:
  488. case Bonus::ATTACKS_ALL_ADJACENT:
  489. case Bonus::ADDITIONAL_ATTACK: //TODO: what with more than one attack? Axe of Ferocity for example
  490. case Bonus::FULL_HP_REGENERATION:
  491. case Bonus::LIFE_DRAIN: //TODO: chance, hp percentage?
  492. case Bonus::SELF_MORALE:
  493. case Bonus::SELF_LUCK:
  494. case Bonus::FEAR:
  495. case Bonus::FEARLESS:
  496. case Bonus::CHARGE_IMMUNITY:
  497. case Bonus::HEALER:
  498. case Bonus::CATAPULT:
  499. case Bonus::DRAGON_NATURE:
  500. case Bonus::NON_LIVING:
  501. case Bonus::UNDEAD:
  502. case Bonus::FIRE_IMMUNITY: //TODO: what about direct, hostile and total immunity?
  503. case Bonus::WATER_IMMUNITY:
  504. case Bonus::AIR_IMMUNITY:
  505. case Bonus::EARTH_IMMUNITY:
  506. case Bonus::RECEPTIVE:
  507. case Bonus::DIRECT_DAMAGE_IMMUNITY:
  508. break;
  509. //One numeric value
  510. case Bonus::MAGIC_RESISTANCE:
  511. case Bonus::SPELL_RESISTANCE_AURA:
  512. case Bonus::SPELL_DAMAGE_REDUCTION:
  513. case Bonus::LEVEL_SPELL_IMMUNITY:
  514. case Bonus::MANA_DRAIN:
  515. case Bonus::HP_REGENERATION:
  516. case Bonus::ADDITIONAL_RETALIATION:
  517. case Bonus::DOUBLE_DAMAGE_CHANCE:
  518. case Bonus::DARKNESS: //Darkness Dragons any1?
  519. boost::algorithm::replace_first(text, "%d", boost::lexical_cast<std::string>(valOfBonuses(Selector::typeSybtype(bonus->type, bonus->subtype))));
  520. break;
  521. //Complex descriptions
  522. case Bonus::HATE:
  523. boost::algorithm::replace_first(text, "%d", boost::lexical_cast<std::string>(valOfBonuses(Selector::typeSybtype(bonus->type, bonus->subtype))));
  524. boost::algorithm::replace_first(text, "%s", VLC->creh->creatures[bonus->subtype]->namePl);
  525. break;
  526. case Bonus::SPELL_AFTER_ATTACK:
  527. {
  528. boost::algorithm::replace_first(text, "%d", boost::lexical_cast<std::string>(valOfBonuses(Selector::typeSybtype(bonus->type, bonus->subtype))));
  529. boost::algorithm::replace_first(text, "%s", VLC->spellh->spells[bonus->subtype]->name);
  530. break;
  531. }
  532. default:
  533. {}//TODO: allow custom bonus types... someday, somehow
  534. }
  535. }
  536. else //short name
  537. {
  538. text = it->second.first;
  539. switch (bonus->type)
  540. {
  541. case Bonus::MANA_CHANNELING:
  542. case Bonus::MAGIC_MIRROR:
  543. case Bonus::CHANGES_SPELL_COST_FOR_ALLY:
  544. case Bonus::CHANGES_SPELL_COST_FOR_ENEMY:
  545. case Bonus::ENEMY_DEFENCE_REDUCTION:
  546. case Bonus::DEATH_STARE:
  547. boost::algorithm::replace_first(text, "%d", boost::lexical_cast<std::string>(valOfBonuses(Selector::typeSybtype(bonus->type, bonus->subtype))));
  548. break;
  549. case Bonus::HATE:
  550. boost::algorithm::replace_first(text, "%s", VLC->creh->creatures[bonus->subtype]->namePl);
  551. break;
  552. case Bonus::LEVEL_SPELL_IMMUNITY:
  553. boost::algorithm::replace_first(text, "%d", boost::lexical_cast<std::string>(bonus->val));
  554. break;
  555. case Bonus::SPELL_AFTER_ATTACK:
  556. boost::algorithm::replace_first(text, "%s", VLC->spellh->spells[bonus->subtype]->name);
  557. break;
  558. case Bonus::SPELL_IMMUNITY:
  559. boost::algorithm::replace_first(text, "%s", VLC->spellh->spells[bonus->subtype]->name);
  560. break;
  561. }
  562. }
  563. return text;
  564. }
  565. else
  566. return "";
  567. }
  568. std::string CStackInstance::bonusToGraphics(Bonus *bonus) const
  569. {
  570. std::string fileName;
  571. switch (bonus->type)
  572. {
  573. //"E_ALIVE.bmp"
  574. //"E_ART.bmp"
  575. //"E_BLESS.bmp"
  576. //"E_BLOCK.bmp"
  577. //"E_BLOCK1.bmp"
  578. //"E_BLOCK2.bmp"
  579. case Bonus::TWO_HEX_ATTACK_BREATH:
  580. fileName = "E_BREATH.bmp"; break;
  581. case Bonus::SPELL_AFTER_ATTACK:
  582. fileName = "E_CAST.bmp"; break;
  583. //"E_CAST1.bmp"
  584. //"E_CAST2.bmp"
  585. //"E_CASTER.bmp"
  586. case Bonus::JOUSTING:
  587. fileName = "E_CHAMP.bmp"; break;
  588. case Bonus::DOUBLE_DAMAGE_CHANCE:
  589. fileName = "E_DBLOW.bmp"; break;
  590. case Bonus::DEATH_STARE:
  591. fileName = "E_DEATH.bmp"; break;
  592. //"E_DEFBON.bmp"
  593. case Bonus::NO_DISTANCE_PENALTY:
  594. fileName = "E_DIST.bmp"; break;
  595. case Bonus::ADDITIONAL_ATTACK:
  596. fileName = "E_DOUBLE.bmp"; break;
  597. case Bonus::DRAGON_NATURE:
  598. fileName = "E_DRAGON.bmp"; break;
  599. case Bonus::MAGIC_RESISTANCE:
  600. fileName = "E_DWARF.bmp"; break;
  601. case Bonus::FEAR:
  602. fileName = "E_FEAR.bmp"; break;
  603. case Bonus::FEARLESS:
  604. fileName = "E_FEARL.bmp"; break;
  605. case Bonus::FLYING:
  606. fileName = "E_FLY.bmp"; break;
  607. case Bonus::SPELL_DAMAGE_REDUCTION:
  608. fileName = "E_GOLEM.bmp"; break;
  609. case Bonus::RETURN_AFTER_STRIKE:
  610. fileName = "E_HARPY.bmp"; break;
  611. case Bonus::HATE:
  612. fileName = "E_HATE.bmp"; break;
  613. case Bonus::KING1:
  614. fileName = "E_KING1.bmp"; break;
  615. case Bonus::KING2:
  616. fileName = "E_KING2.bmp"; break;
  617. case Bonus::KING3:
  618. fileName = "E_KING3.bmp"; break;
  619. case Bonus::CHANGES_SPELL_COST_FOR_ALLY:
  620. fileName = "E_MANA.bmp"; break;
  621. case Bonus::NO_MELEE_PENALTY:
  622. fileName = "E_MELEE.bmp"; break;
  623. //"E_MIND.bmp"
  624. case Bonus::SELF_MORALE:
  625. fileName = "E_MINOT.bmp"; break;
  626. case Bonus::NO_MORALE:
  627. fileName = "E_MORAL.bmp"; break;
  628. case Bonus::RECEPTIVE:
  629. fileName = "E_NOFRIM.bmp"; break;
  630. case Bonus::NO_OBSTACLES_PENALTY:
  631. fileName = "E_OBST.bmp"; break;
  632. case Bonus::ENEMY_DEFENCE_REDUCTION:
  633. fileName = "E_RDEF.bmp"; break;
  634. //"E_REBIRTH.bmp"
  635. case Bonus::BLOCKS_RETALIATION:
  636. fileName = "E_RETAIL.bmp"; break;
  637. case Bonus::ADDITIONAL_RETALIATION:
  638. fileName = "E_RETAIL1.bmp"; break;
  639. case Bonus::ATTACKS_ALL_ADJACENT:
  640. fileName = "E_ROUND.bmp"; break;
  641. //"E_SGNUM.bmp"
  642. //"E_SGTYPE.bmp"
  643. case Bonus::SHOOTER:
  644. fileName = "E_SHOOT.bmp"; break;
  645. case Bonus::FREE_SHOOTING: //shooter is not blocked by enemy
  646. fileName = "E_SHOOTA.bmp"; break;
  647. //"E_SHOOTN.bmp"
  648. case Bonus::SPELL_IMMUNITY:
  649. {
  650. switch (bonus->subtype)
  651. {
  652. case 62: //Blind
  653. fileName = "E_SPBLIND.bmp"; break;
  654. case 35: // Dispell
  655. fileName = "E_SPDISP.bmp"; break;
  656. case 78: // Dispell beneficial spells
  657. fileName = "E_SPDISB.bmp"; break;
  658. case 60: //Hypnotize
  659. fileName = "E_SPHYPN.bmp"; break;
  660. case 18: //Implosion
  661. fileName = "E_SPIMP.bmp"; break;
  662. case 59: //Berserk
  663. fileName = "E_SPBERS.bmp"; break;
  664. case 23: //Meteor Shower
  665. fileName = "E_SPMET.bmp"; break;
  666. case 26: //Armageddon
  667. fileName = "E_SPARM.bmp"; break;
  668. case 54: //Slow
  669. fileName = "E_SPSLOW.bmp"; break;
  670. //TODO: some generic spell handling?
  671. }
  672. break;
  673. }
  674. //"E_SPAWILL.bmp"
  675. case Bonus::DIRECT_DAMAGE_IMMUNITY:
  676. fileName = "E_SPDIR.bmp"; break;
  677. //"E_SPDISB.bmp"
  678. //"E_SPDISP.bmp"
  679. //"E_SPEATH.bmp"
  680. //"E_SPEATH1.bmp"
  681. case Bonus::FIRE_IMMUNITY:
  682. switch (bonus->subtype)
  683. {
  684. case 0:
  685. fileName = "E_SPFIRE.bmp"; break; //all
  686. case 1:
  687. fileName = "E_SPFIRE1.bmp"; break; //not positive
  688. case 2:
  689. fileName = "E_FIRE.bmp"; break; //direct damage
  690. }
  691. break;
  692. case Bonus::WATER_IMMUNITY:
  693. switch (bonus->subtype)
  694. {
  695. case 0:
  696. fileName = "E_SPWATER.bmp"; break; //all
  697. case 1:
  698. fileName = "E_SPWATER1.bmp"; break; //not positive
  699. case 2:
  700. fileName = "E_SPCOLD.bmp"; break; //direct damage
  701. }
  702. break;
  703. case Bonus::AIR_IMMUNITY:
  704. switch (bonus->subtype)
  705. {
  706. case 0:
  707. fileName = "E_SPAIR.bmp"; break; //all
  708. case 1:
  709. fileName = "E_SPAIR1.bmp"; break; //not positive
  710. case 2:
  711. fileName = "E_LIGHT.bmp"; break;//direct damage
  712. }
  713. break;
  714. case Bonus::EARTH_IMMUNITY:
  715. switch (bonus->subtype)
  716. {
  717. case 0:
  718. fileName = "E_SPEATH.bmp"; break; //all
  719. case 1:
  720. case 2: //no specific icon for direct damage immunity
  721. fileName = "E_SPEATH1.bmp"; break; //not positive
  722. }
  723. break;
  724. case Bonus::LEVEL_SPELL_IMMUNITY:
  725. {
  726. if (iswith(bonus->val, 1 , 5))
  727. {
  728. fileName = "E_SPLVL" + boost::lexical_cast<std::string>(bonus->val) + ".bmp";
  729. }
  730. break;
  731. }
  732. //"E_SPWATER.bmp"
  733. //"E_SPWATER1.bmp"
  734. //"E_SUMMON.bmp"
  735. //"E_SUMMON1.bmp"
  736. //"E_SUMMON2.bmp"
  737. case Bonus::FULL_HP_REGENERATION:
  738. fileName = "E_TROLL.bmp"; break;
  739. case Bonus::UNDEAD:
  740. fileName = "E_UNDEAD.bmp"; break;
  741. case Bonus::SPELL_RESISTANCE_AURA:
  742. fileName = "E_UNIC.bmp"; break;
  743. }
  744. if(!fileName.empty())
  745. fileName = "zvs/Lib1.res/" + fileName;
  746. return fileName;
  747. }
  748. void CStackInstance::setArmyObj(const CArmedInstance *ArmyObj)
  749. {
  750. if(_armyObj)
  751. detachFrom(const_cast<CArmedInstance*>(_armyObj));
  752. _armyObj = ArmyObj;
  753. if(ArmyObj)
  754. {
  755. attachTo(const_cast<CArmedInstance*>(_armyObj));
  756. }
  757. }
  758. // void CStackInstance::getParents(TCNodes &out, const CBonusSystemNode *source /*= NULL*/) const
  759. // {
  760. // out.insert(type);
  761. //
  762. // if(source && source != this) //we should be root, if not - do not inherit anything
  763. // return;
  764. //
  765. // if(armyObj)
  766. // out.insert(armyObj);
  767. // else
  768. // out.insert(&IObjectInterface::cb->gameState()->globalEffects);
  769. // }
  770. std::string CStackInstance::getQuantityTXT(bool capitalized /*= true*/) const
  771. {
  772. return VLC->generaltexth->arraytxt[174 + getQuantityID()*3 + 2 - capitalized];
  773. }
  774. bool CStackInstance::valid(bool allowUnrandomized) const
  775. {
  776. bool isRand = (idRand != -1);
  777. if(!isRand)
  778. {
  779. return (type && type == VLC->creh->creatures[type->idNumber]);
  780. }
  781. else
  782. return allowUnrandomized;
  783. }
  784. CStackInstance::~CStackInstance()
  785. {
  786. }
  787. std::string CStackInstance::nodeName() const
  788. {
  789. std::ostringstream oss;
  790. oss << "Stack of " << count << " of ";
  791. if(type)
  792. oss << type->namePl;
  793. else if(idRand)
  794. oss << "[no type, idRand=" << idRand << "]";
  795. else
  796. oss << "[UNDEFINED TYPE]";
  797. return oss.str();
  798. }
  799. void CStackInstance::deserializationFix()
  800. {
  801. const CCreature *backup = type;
  802. type = NULL;
  803. setType(backup);
  804. const CArmedInstance *armyBackup = _armyObj;
  805. _armyObj = NULL;
  806. setArmyObj(armyBackup);
  807. }
  808. CStackBasicDescriptor::CStackBasicDescriptor()
  809. {
  810. type = NULL;
  811. count = -1;
  812. }
  813. CStackBasicDescriptor::CStackBasicDescriptor(TCreature id, TQuantity Count)
  814. : type (VLC->creh->creatures[id]), count(Count)
  815. {
  816. }
  817. CStackBasicDescriptor::CStackBasicDescriptor(const CCreature *c, TQuantity Count)
  818. : type(c), count(Count)
  819. {
  820. }
  821. DLL_EXPORT std::ostream & operator<<(std::ostream & str, const CStackInstance & sth)
  822. {
  823. if(!sth.valid(true))
  824. str << "an invalid stack!";
  825. str << "stack with " << sth.count << " of ";
  826. if(sth.type)
  827. str << sth.type->namePl;
  828. else
  829. str << sth.idRand;
  830. return str;
  831. }
  832. void CSimpleArmy::clear()
  833. {
  834. army.clear();
  835. }
  836. CSimpleArmy::operator bool() const
  837. {
  838. return army.size();
  839. }
  840. bool CSimpleArmy::setCreature(TSlot slot, TCreature cre, TQuantity count)
  841. {
  842. assert(!vstd::contains(army, slot));
  843. army[slot] = CStackBasicDescriptor(cre, count);
  844. return true;
  845. }