CCreatureSet.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875
  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. setType(VLC->creh->creatures[creID]);
  447. }
  448. void CStackInstance::setType(const CCreature *c)
  449. {
  450. if(type)
  451. {
  452. detachFrom(const_cast<CCreature*>(type));
  453. if (type->isMyUpgrade(c) && STACK_EXP)
  454. experience *= VLC->creh->expAfterUpgrade / 100.0f;
  455. }
  456. type = c;
  457. attachTo(const_cast<CCreature*>(type));
  458. }
  459. std::string CStackInstance::bonusToString(Bonus *bonus, bool description) const
  460. {
  461. std::map<TBonusType, std::pair<std::string, std::string> >::iterator it = VLC->creh->stackBonuses.find(bonus->type);
  462. if (it != VLC->creh->stackBonuses.end())
  463. {
  464. std::string text;
  465. if (description) //long ability description
  466. {
  467. text = it->second.second;
  468. switch (bonus->type)
  469. {
  470. //no additional modifiers needed
  471. case Bonus::FLYING:
  472. case Bonus::UNLIMITED_RETALIATIONS:
  473. case Bonus::SHOOTER:
  474. case Bonus::FREE_SHOOTING:
  475. case Bonus::NO_SHOTING_PENALTY:
  476. case Bonus::NO_MELEE_PENALTY:
  477. case Bonus::NO_DISTANCE_PENALTY:
  478. case Bonus::NO_OBSTACLES_PENALTY:
  479. case Bonus::JOUSTING: //TODO: percent bonus?
  480. case Bonus::RETURN_AFTER_STRIKE:
  481. case Bonus::BLOCKS_RETALIATION:
  482. case Bonus::TWO_HEX_ATTACK_BREATH:
  483. case Bonus::THREE_HEADED_ATTACK:
  484. case Bonus::ATTACKS_ALL_ADJACENT:
  485. case Bonus::ADDITIONAL_ATTACK: //TODO: what with more than one attack? Axe of Ferocity for example
  486. case Bonus::FULL_HP_REGENERATION:
  487. case Bonus::LIFE_DRAIN: //TODO: chance, hp percentage?
  488. case Bonus::SELF_MORALE:
  489. case Bonus::SELF_LUCK:
  490. case Bonus::FEAR:
  491. case Bonus::FEARLESS:
  492. case Bonus::CHARGE_IMMUNITY:
  493. case Bonus::HEALER:
  494. case Bonus::CATAPULT:
  495. case Bonus::DRAGON_NATURE:
  496. case Bonus::NON_LIVING:
  497. case Bonus::UNDEAD:
  498. break;
  499. //One numeric value
  500. case Bonus::MAGIC_RESISTANCE:
  501. case Bonus::SPELL_RESISTANCE_AURA:
  502. case Bonus::SPELL_DAMAGE_REDUCTION:
  503. case Bonus::LEVEL_SPELL_IMMUNITY:
  504. case Bonus::MANA_DRAIN:
  505. case Bonus::HP_REGENERATION:
  506. case Bonus::ADDITIONAL_RETALIATION:
  507. case Bonus::DOUBLE_DAMAGE_CHANCE:
  508. case Bonus::DARKNESS: //Darkness Dragons any1?
  509. boost::algorithm::replace_first(text, "%d", boost::lexical_cast<std::string>(valOfBonuses(Selector::typeSybtype(bonus->type, bonus->subtype))));
  510. break;
  511. //Complex descriptions
  512. case Bonus::HATE:
  513. boost::algorithm::replace_first(text, "%d", boost::lexical_cast<std::string>(valOfBonuses(Selector::typeSybtype(bonus->type, bonus->subtype))));
  514. boost::algorithm::replace_first(text, "%s", VLC->creh->creatures[bonus->subtype]->namePl);
  515. break;
  516. case Bonus::SPELL_AFTER_ATTACK:
  517. boost::algorithm::replace_first(text, "%d", boost::lexical_cast<std::string>(bonus->additionalInfo));
  518. boost::algorithm::replace_first(text, "%s", VLC->spellh->spells[bonus->subtype]->name);
  519. break;
  520. default:
  521. {}//TODO: allow custom bonus types... someday, somehow
  522. }
  523. }
  524. else //short name
  525. {
  526. text = it->second.first;
  527. switch (bonus->type)
  528. {
  529. case Bonus::MANA_CHANNELING:
  530. case Bonus::MAGIC_MIRROR:
  531. case Bonus::CHANGES_SPELL_COST_FOR_ALLY:
  532. case Bonus::CHANGES_SPELL_COST_FOR_ENEMY:
  533. case Bonus::ENEMY_DEFENCE_REDUCTION:
  534. boost::algorithm::replace_first(text, "%d", boost::lexical_cast<std::string>(valOfBonuses(Selector::typeSybtype(bonus->type, bonus->subtype))));
  535. break;
  536. case Bonus::HATE:
  537. boost::algorithm::replace_first(text, "%s", VLC->creh->creatures[bonus->subtype]->namePl);
  538. break;
  539. case Bonus::LEVEL_SPELL_IMMUNITY:
  540. boost::algorithm::replace_first(text, "%d", boost::lexical_cast<std::string>(bonus->val));
  541. break;
  542. case Bonus::SPELL_AFTER_ATTACK:
  543. boost::algorithm::replace_first(text, "%s", VLC->spellh->spells[bonus->subtype]->name);
  544. break;
  545. case Bonus::SPELL_IMMUNITY:
  546. boost::algorithm::replace_first(text, "%s", VLC->spellh->spells[bonus->subtype]->name);
  547. break;
  548. }
  549. }
  550. return text;
  551. }
  552. else
  553. return "";
  554. }
  555. std::string CStackInstance::bonusToGraphics(Bonus *bonus) const
  556. {
  557. std::string fileName;
  558. switch (bonus->type)
  559. {
  560. //"E_ALIVE.bmp"
  561. //"E_ART.bmp"
  562. //"E_BLESS.bmp"
  563. //"E_BLOCK.bmp"
  564. //"E_BLOCK1.bmp"
  565. //"E_BLOCK2.bmp"
  566. case Bonus::TWO_HEX_ATTACK_BREATH:
  567. fileName = "E_BREATH.bmp"; break;
  568. case Bonus::SPELL_AFTER_ATTACK:
  569. fileName = "E_CAST.bmp"; break;
  570. //"E_CAST1.bmp"
  571. //"E_CAST2.bmp"
  572. //"E_CASTER.bmp"
  573. case Bonus::JOUSTING:
  574. fileName = "E_CHAMP.bmp"; break;
  575. case Bonus::DOUBLE_DAMAGE_CHANCE:
  576. fileName = "E_DBLOW.bmp"; break;
  577. //"E_DEATH.bmp"
  578. //"E_DEFBON.bmp"
  579. case Bonus::NO_DISTANCE_PENALTY:
  580. fileName = "E_DIST.bmp"; break;
  581. case Bonus::ADDITIONAL_ATTACK:
  582. fileName = "E_DOUBLE.bmp"; break;
  583. case Bonus::DRAGON_NATURE:
  584. fileName = "E_DRAGON.bmp"; break;
  585. case Bonus::MAGIC_RESISTANCE:
  586. fileName = "E_DWARF.bmp"; break;
  587. case Bonus::FEAR:
  588. fileName = "E_FEAR.bmp"; break;
  589. case Bonus::FEARLESS:
  590. fileName = "E_FEARL.bmp"; break;
  591. //"E_FIRE.bmp"
  592. case Bonus::FLYING:
  593. fileName = "E_FLY.bmp"; break;
  594. case Bonus::SPELL_DAMAGE_REDUCTION:
  595. fileName = "E_GOLEM.bmp"; break;
  596. case Bonus::RETURN_AFTER_STRIKE:
  597. fileName = "E_HARPY.bmp"; break;
  598. case Bonus::HATE:
  599. fileName = "E_HATE.bmp"; break;
  600. case Bonus::KING1:
  601. fileName = "E_KING1.bmp"; break;
  602. case Bonus::KING2:
  603. fileName = "E_KING2.bmp"; break;
  604. case Bonus::KING3:
  605. fileName = "E_KING3.bmp"; break;
  606. //"E_LIGHT.bmp"
  607. case Bonus::CHANGES_SPELL_COST_FOR_ALLY:
  608. fileName = "E_MANA.bmp"; break;
  609. case Bonus::NO_MELEE_PENALTY:
  610. fileName = "E_MELEE.bmp"; break;
  611. //"E_MIND.bmp"
  612. case Bonus::SELF_MORALE:
  613. fileName = "E_MINOT.bmp"; break;
  614. case Bonus::NO_MORALE:
  615. fileName = "E_MORAL.bmp"; break;
  616. //"E_NOFRIM.bmp"
  617. case Bonus::NO_OBSTACLES_PENALTY:
  618. fileName = "E_OBST.bmp"; break;
  619. case Bonus::ENEMY_DEFENCE_REDUCTION:
  620. fileName = "E_RDEF.bmp"; break;
  621. //"E_REBIRTH.bmp"
  622. case Bonus::BLOCKS_RETALIATION:
  623. fileName = "E_RETAIL.bmp"; break;
  624. case Bonus::ADDITIONAL_RETALIATION:
  625. fileName = "E_RETAIL1.bmp"; break;
  626. case Bonus::ATTACKS_ALL_ADJACENT:
  627. fileName = "E_ROUND.bmp"; break;
  628. //"E_SGNUM.bmp"
  629. //"E_SGTYPE.bmp"
  630. case Bonus::SHOOTER:
  631. fileName = "E_SHOOT.bmp"; break;
  632. case Bonus::FREE_SHOOTING: //shooter is not blocked by enemy
  633. fileName = "E_SHOOTA.bmp"; break;
  634. //"E_SHOOTN.bmp"
  635. //"E_SPAIR.bmp"
  636. //"E_SPAIR1.bmp"
  637. case Bonus::SPELL_IMMUNITY:
  638. {
  639. switch (bonus->subtype)
  640. {
  641. case 62: //Blind
  642. fileName = "E_SPBLIND.bmp"; break;
  643. case 60: //Hypnotize
  644. fileName = "E_SPHYPN.bmp"; break;
  645. case 18: //Implosion
  646. fileName = "E_SPIMP.bmp"; break;
  647. case 59: //Berserk
  648. fileName = "E_SPBERS.bmp"; break;
  649. case 23: //Meteor Shower
  650. fileName = "E_SPMET.bmp"; break;
  651. case 26: //Armageddon
  652. fileName = "E_SPARM.bmp"; break;
  653. case 54: //Slow
  654. fileName = "E_SPSLOW.bmp"; break;
  655. //TODO: some generic spell handling?
  656. }
  657. }
  658. //"E_SPAWILL.bmp"
  659. //"E_SPCOLD.bmp"
  660. //"E_SPDFIRE.bmp"
  661. //"E_SPDIR.bmp"
  662. //"E_SPDISB.bmp"
  663. //"E_SPDISP.bmp"
  664. //"E_SPEATH.bmp"
  665. //"E_SPEATH1.bmp"
  666. //"E_SPFIRE.bmp"
  667. //"E_SPFIRE1.bmp"
  668. case Bonus::LEVEL_SPELL_IMMUNITY:
  669. {
  670. if (iswith(bonus->val, 1 , 5))
  671. {
  672. fileName = "E_SPLVL" + boost::lexical_cast<std::string>(bonus->val) + ".bmp";
  673. }
  674. break;
  675. }
  676. //"E_SPWATER.bmp"
  677. //"E_SPWATER1.bmp"
  678. //"E_SUMMON.bmp"
  679. //"E_SUMMON1.bmp"
  680. //"E_SUMMON2.bmp"
  681. case Bonus::FULL_HP_REGENERATION:
  682. fileName = "E_TROLL.bmp"; break;
  683. case Bonus::UNDEAD:
  684. fileName = "E_UNDEAD.bmp"; break;
  685. case Bonus::SPELL_RESISTANCE_AURA:
  686. fileName = "E_UNIC.bmp"; break;
  687. }
  688. return fileName;
  689. }
  690. void CStackInstance::setArmyObj(const CArmedInstance *ArmyObj)
  691. {
  692. if(_armyObj)
  693. detachFrom(const_cast<CArmedInstance*>(_armyObj));
  694. _armyObj = ArmyObj;
  695. if(ArmyObj)
  696. {
  697. attachTo(const_cast<CArmedInstance*>(_armyObj));
  698. }
  699. }
  700. // void CStackInstance::getParents(TCNodes &out, const CBonusSystemNode *source /*= NULL*/) const
  701. // {
  702. // out.insert(type);
  703. //
  704. // if(source && source != this) //we should be root, if not - do not inherit anything
  705. // return;
  706. //
  707. // if(armyObj)
  708. // out.insert(armyObj);
  709. // else
  710. // out.insert(&IObjectInterface::cb->gameState()->globalEffects);
  711. // }
  712. std::string CStackInstance::getQuantityTXT(bool capitalized /*= true*/) const
  713. {
  714. return VLC->generaltexth->arraytxt[174 + getQuantityID()*3 + 2 - capitalized];
  715. }
  716. bool CStackInstance::valid(bool allowUnrandomized) const
  717. {
  718. bool isRand = (idRand != -1);
  719. if(!isRand)
  720. {
  721. return (type && type == VLC->creh->creatures[type->idNumber]);
  722. }
  723. else
  724. return allowUnrandomized;
  725. }
  726. CStackInstance::~CStackInstance()
  727. {
  728. }
  729. std::string CStackInstance::nodeName() const
  730. {
  731. std::ostringstream oss;
  732. oss << "Stack of " << count << " of ";
  733. if(type)
  734. oss << type->namePl;
  735. else if(idRand)
  736. oss << "[no type, idRand=" << idRand << "]";
  737. else
  738. oss << "[UNDEFINED TYPE]";
  739. return oss.str();
  740. }
  741. void CStackInstance::deserializationFix()
  742. {
  743. const CCreature *backup = type;
  744. type = NULL;
  745. setType(backup);
  746. const CArmedInstance *armyBackup = _armyObj;
  747. _armyObj = NULL;
  748. setArmyObj(armyBackup);
  749. }
  750. CStackBasicDescriptor::CStackBasicDescriptor()
  751. {
  752. type = NULL;
  753. count = -1;
  754. }
  755. CStackBasicDescriptor::CStackBasicDescriptor(TCreature id, TQuantity Count)
  756. : type (VLC->creh->creatures[id]), count(Count)
  757. {
  758. }
  759. CStackBasicDescriptor::CStackBasicDescriptor(const CCreature *c, TQuantity Count)
  760. : type(c), count(Count)
  761. {
  762. }
  763. DLL_EXPORT std::ostream & operator<<(std::ostream & str, const CStackInstance & sth)
  764. {
  765. if(!sth.valid(true))
  766. str << "an invalid stack!";
  767. str << "stack with " << sth.count << " of ";
  768. if(sth.type)
  769. str << sth.type->namePl;
  770. else
  771. str << sth.idRand;
  772. return str;
  773. }
  774. void CSimpleArmy::clear()
  775. {
  776. army.clear();
  777. }
  778. CSimpleArmy::operator bool() const
  779. {
  780. return army.size();
  781. }
  782. bool CSimpleArmy::setCreature(TSlot slot, TCreature cre, TQuantity count)
  783. {
  784. assert(!vstd::contains(army, slot));
  785. army[slot] = CStackBasicDescriptor(cre, count);
  786. return true;
  787. }