CCreatureSet.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848
  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. int CCreatureSet::getStackCount(TSlot slot) const
  73. {
  74. TSlots::const_iterator i = stacks.find(slot);
  75. if (i != stacks.end())
  76. return i->second->count;
  77. else
  78. return 0; //TODO? consider issuing a warning
  79. }
  80. bool CCreatureSet::mergableStacks(std::pair<TSlot, TSlot> &out, TSlot preferable /*= -1*/) const /*looks for two same stacks, returns slot positions */
  81. {
  82. //try to match creature to our preferred stack
  83. if(preferable >= 0 && vstd::contains(stacks, preferable))
  84. {
  85. const CCreature *cr = stacks.find(preferable)->second->type;
  86. for(TSlots::const_iterator j=stacks.begin(); j!=stacks.end(); ++j)
  87. {
  88. if(cr == j->second->type && j->first != preferable)
  89. {
  90. out.first = preferable;
  91. out.second = j->first;
  92. return true;
  93. }
  94. }
  95. }
  96. for(TSlots::const_iterator i=stacks.begin(); i!=stacks.end(); ++i)
  97. {
  98. for(TSlots::const_iterator j=stacks.begin(); j!=stacks.end(); ++j)
  99. {
  100. if(i->second->type == j->second->type && i->first != j->first)
  101. {
  102. out.first = i->first;
  103. out.second = j->first;
  104. return true;
  105. }
  106. }
  107. }
  108. return false;
  109. }
  110. void CCreatureSet::sweep()
  111. {
  112. for(TSlots::iterator i=stacks.begin(); i!=stacks.end(); ++i)
  113. {
  114. if(!i->second->count)
  115. {
  116. stacks.erase(i);
  117. sweep();
  118. break;
  119. }
  120. }
  121. }
  122. void CCreatureSet::addToSlot(TSlot slot, TCreature cre, TQuantity count, bool allowMerging/* = true*/)
  123. {
  124. const CCreature *c = VLC->creh->creatures[cre];
  125. if(!hasStackAtSlot(slot))
  126. {
  127. setCreature(slot, cre, count);
  128. }
  129. else if(getCreature(slot) == c && allowMerging) //that slot was empty or contained same type creature
  130. {
  131. setStackCount(slot, getStackCount(slot) + count);
  132. }
  133. else
  134. {
  135. tlog1 << "Failed adding to slot!\n";
  136. }
  137. }
  138. void CCreatureSet::addToSlot(TSlot slot, CStackInstance *stack, bool allowMerging/* = true*/)
  139. {
  140. assert(stack->valid(true));
  141. if(!hasStackAtSlot(slot))
  142. {
  143. putStack(slot, stack);
  144. }
  145. else if(allowMerging && stack->type == getCreature(slot))
  146. {
  147. joinStack(slot, stack);
  148. }
  149. else
  150. {
  151. tlog1 << "Cannot add to slot " << slot << " stack " << *stack << std::endl;
  152. }
  153. }
  154. bool CCreatureSet::validTypes(bool allowUnrandomized /*= false*/) const
  155. {
  156. for(TSlots::const_iterator i=stacks.begin(); i!=stacks.end(); ++i)
  157. {
  158. if(!i->second->valid(allowUnrandomized))
  159. return false;
  160. }
  161. return true;
  162. }
  163. bool CCreatureSet::slotEmpty(TSlot slot) const
  164. {
  165. return !hasStackAtSlot(slot);
  166. }
  167. bool CCreatureSet::needsLastStack() const
  168. {
  169. return false;
  170. }
  171. int CCreatureSet::getArmyStrength() const
  172. {
  173. int ret = 0;
  174. for(TSlots::const_iterator i = stacks.begin(); i != stacks.end(); i++)
  175. ret += i->second->type->AIValue * i->second->count;
  176. return ret;
  177. }
  178. ui64 CCreatureSet::getPower (TSlot slot) const
  179. {
  180. return getCreature(slot)->AIValue * getStackCount(slot);
  181. }
  182. std::string CCreatureSet::getRoughAmount (TSlot slot) const
  183. {
  184. return VLC->generaltexth->arraytxt[174 + 3*CCreature::getQuantityID(getStackCount(slot))];
  185. }
  186. int CCreatureSet::stacksCount() const
  187. {
  188. return stacks.size();
  189. }
  190. void CCreatureSet::setFormation(bool tight)
  191. {
  192. formation = tight;
  193. }
  194. void CCreatureSet::setStackCount(TSlot slot, TQuantity count)
  195. {
  196. assert(hasStackAtSlot(slot));
  197. assert(count > 0);
  198. if (STACK_EXP)
  199. stacks[slot]->experience *= ((stacks[slot]->count + count)/(float)stacks[slot]->count);
  200. stacks[slot]->count = count;
  201. armyChanged();
  202. }
  203. void CCreatureSet::giveStackExp(expType exp)
  204. {
  205. for(TSlots::const_iterator i = stacks.begin(); i != stacks.end(); i++)
  206. i->second->giveStackExp(exp);
  207. }
  208. void CCreatureSet::clear()
  209. {
  210. while(!stacks.empty())
  211. {
  212. eraseStack(stacks.begin()->first);
  213. }
  214. }
  215. const CStackInstance& CCreatureSet::getStack(TSlot slot) const
  216. {
  217. assert(hasStackAtSlot(slot));
  218. return *stacks.find(slot)->second;
  219. }
  220. void CCreatureSet::eraseStack(TSlot slot)
  221. {
  222. assert(hasStackAtSlot(slot));
  223. CStackInstance *toErase = detachStack(slot);
  224. delNull(toErase);
  225. }
  226. bool CCreatureSet::contains(const CStackInstance *stack) const
  227. {
  228. if(!stack)
  229. return false;
  230. for(TSlots::const_iterator i = stacks.begin(); i != stacks.end(); ++i)
  231. if(i->second == stack)
  232. return true;
  233. return false;
  234. }
  235. TSlot CCreatureSet::findStack(const CStackInstance *stack) const
  236. {
  237. if(!stack)
  238. return -1;
  239. for(TSlots::const_iterator i = stacks.begin(); i != stacks.end(); ++i)
  240. if(i->second == stack)
  241. return i->first;
  242. return -1;
  243. }
  244. CArmedInstance * CCreatureSet::castToArmyObj()
  245. {
  246. return dynamic_cast<CArmedInstance *>(this);
  247. }
  248. void CCreatureSet::putStack(TSlot slot, CStackInstance *stack)
  249. {
  250. assert(!hasStackAtSlot(slot));
  251. stacks[slot] = stack;
  252. stack->setArmyObj(castToArmyObj());
  253. armyChanged();
  254. }
  255. void CCreatureSet::joinStack(TSlot slot, CStackInstance * stack)
  256. {
  257. const CCreature *c = getCreature(slot);
  258. assert(c == stack->type);
  259. assert(c);
  260. //TODO move stuff
  261. changeStackCount(slot, stack->count);
  262. delNull(stack);
  263. }
  264. void CCreatureSet::changeStackCount(TSlot slot, TQuantity toAdd)
  265. {
  266. setStackCount(slot, getStackCount(slot) + toAdd);
  267. }
  268. CCreatureSet::CCreatureSet()
  269. {
  270. formation = false;
  271. }
  272. CCreatureSet::CCreatureSet(const CCreatureSet&)
  273. {
  274. assert(0);
  275. }
  276. CCreatureSet::~CCreatureSet()
  277. {
  278. clear();
  279. }
  280. void CCreatureSet::setToArmy(CSimpleArmy &src)
  281. {
  282. clear();
  283. while(src)
  284. {
  285. TSimpleSlots::iterator i = src.army.begin();
  286. assert(i->second.type);
  287. assert(i->second.count);
  288. putStack(i->first, new CStackInstance(i->second.type, i->second.count));
  289. src.army.erase(i);
  290. }
  291. }
  292. CStackInstance * CCreatureSet::detachStack(TSlot slot)
  293. {
  294. assert(hasStackAtSlot(slot));
  295. CStackInstance *ret = stacks[slot];
  296. //if(CArmedInstance *armedObj = castToArmyObj())
  297. {
  298. ret->setArmyObj(NULL); //detaches from current armyobj
  299. }
  300. assert(!ret->armyObj); //we failed detaching?
  301. stacks.erase(slot);
  302. armyChanged();
  303. return ret;
  304. }
  305. void CCreatureSet::setStackType(TSlot slot, const CCreature *type)
  306. {
  307. assert(hasStackAtSlot(slot));
  308. CStackInstance *s = stacks[slot];
  309. s->setType(type->idNumber);
  310. armyChanged();
  311. }
  312. bool CCreatureSet::canBeMergedWith(const CCreatureSet &cs, bool allowMergingStacks) const
  313. {
  314. if(!allowMergingStacks)
  315. {
  316. int freeSlots = stacksCount() - ARMY_SIZE;
  317. std::set<const CCreature*> cresToAdd;
  318. for(TSlots::const_iterator i = cs.stacks.begin(); i != cs.stacks.end(); i++)
  319. {
  320. TSlot dest = getSlotFor(i->second->type);
  321. if(dest < 0 || hasStackAtSlot(dest))
  322. cresToAdd.insert(i->second->type);
  323. }
  324. return cresToAdd.size() <= freeSlots;
  325. }
  326. else
  327. {
  328. CCreatureSet cres;
  329. //get types of creatures that need their own slot
  330. for(TSlots::const_iterator i = cs.stacks.begin(); i != cs.stacks.end(); i++)
  331. cres.addToSlot(i->first, i->second->type->idNumber, 1, true);
  332. TSlot j;
  333. for(TSlots::const_iterator i = stacks.begin(); i != stacks.end(); i++)
  334. {
  335. if ((j = cres.getSlotFor(i->second->type)) >= 0)
  336. cres.addToSlot(j, i->second->type->idNumber, 1, true); //merge if possible
  337. else
  338. return false; //no place found
  339. }
  340. return true; //all stacks found their slots
  341. }
  342. }
  343. bool CCreatureSet::hasStackAtSlot(TSlot slot) const
  344. {
  345. return vstd::contains(stacks, slot);
  346. }
  347. CCreatureSet & CCreatureSet::operator=(const CCreatureSet&cs)
  348. {
  349. assert(0);
  350. return *this;
  351. }
  352. void CCreatureSet::armyChanged()
  353. {
  354. }
  355. CStackInstance::CStackInstance()
  356. : armyObj(_armyObj)
  357. {
  358. init();
  359. }
  360. CStackInstance::CStackInstance(TCreature id, TQuantity Count)
  361. : armyObj(_armyObj)
  362. {
  363. init();
  364. setType(id);
  365. count = Count;
  366. }
  367. CStackInstance::CStackInstance(const CCreature *cre, TQuantity Count)
  368. : armyObj(_armyObj)
  369. {
  370. init();
  371. setType(cre);
  372. count = Count;
  373. }
  374. void CStackInstance::init()
  375. {
  376. experience = 0;
  377. count = 0;
  378. type = NULL;
  379. idRand = -1;
  380. _armyObj = NULL;
  381. nodeType = STACK_INSTANCE;
  382. }
  383. int CStackInstance::getQuantityID() const
  384. {
  385. return CCreature::getQuantityID(count);
  386. }
  387. int CStackInstance::getExpRank() const
  388. {
  389. int tier = type->level;
  390. if (iswith(tier, 1, 7))
  391. {
  392. for (int i = VLC->creh->expRanks[tier].size()-2; i >-1; --i)//sic!
  393. { //exp values vary from 1st level to max exp at 11th level
  394. if (experience >= VLC->creh->expRanks[tier][i])
  395. return ++i; //faster, but confusing - 0 index mean 1st level of experience
  396. }
  397. return 0;
  398. }
  399. else //higher tier
  400. {
  401. for (int i = VLC->creh->expRanks[0].size()-2; i >-1; --i)
  402. {
  403. if (experience >= VLC->creh->expRanks[0][i])
  404. return ++i;
  405. }
  406. return 0;
  407. }
  408. }
  409. void CStackInstance::giveStackExp(expType exp)
  410. {
  411. int level = type->level;
  412. if (!iswith(level, 1, 7))
  413. level = 0;
  414. CCreatureHandler * creh = VLC->creh;
  415. ui32 maxExp = creh->expRanks[level].back();
  416. amin(exp, (expType)maxExp); //prevent exp overflow due to different types
  417. amin(exp, (maxExp * creh->maxExpPerBattle[level])/100);
  418. amin(experience += exp, maxExp); //can't get more exp than this limit
  419. }
  420. void CStackInstance::setType(int creID)
  421. {
  422. setType(VLC->creh->creatures[creID]);
  423. }
  424. void CStackInstance::setType(const CCreature *c)
  425. {
  426. if(type)
  427. {
  428. detachFrom(const_cast<CCreature*>(type));
  429. if (type->isMyUpgrade(c) && STACK_EXP)
  430. experience *= VLC->creh->expAfterUpgrade / 100.0f;
  431. }
  432. type = c;
  433. attachTo(const_cast<CCreature*>(type));
  434. }
  435. std::string CStackInstance::bonusToString(Bonus *bonus, bool description) const
  436. {
  437. std::map<TBonusType, std::pair<std::string, std::string> >::iterator it = VLC->creh->stackBonuses.find(bonus->type);
  438. if (it != VLC->creh->stackBonuses.end())
  439. {
  440. std::string text;
  441. if (description) //long ability description
  442. {
  443. text = it->second.second;
  444. switch (bonus->type)
  445. {
  446. //no additional modifiers needed
  447. case Bonus::FLYING:
  448. case Bonus::UNLIMITED_RETALIATIONS:
  449. case Bonus::SHOOTER:
  450. case Bonus::FREE_SHOOTING:
  451. case Bonus::NO_SHOTING_PENALTY:
  452. case Bonus::NO_MELEE_PENALTY:
  453. case Bonus::NO_DISTANCE_PENALTY:
  454. case Bonus::NO_OBSTACLES_PENALTY:
  455. case Bonus::JOUSTING: //TODO: percent bonus?
  456. case Bonus::RETURN_AFTER_STRIKE:
  457. case Bonus::BLOCKS_RETALIATION:
  458. case Bonus::TWO_HEX_ATTACK_BREATH:
  459. case Bonus::THREE_HEADED_ATTACK:
  460. case Bonus::ATTACKS_ALL_ADJACENT:
  461. case Bonus::ADDITIONAL_ATTACK: //TODO: what with more than one attack? Axe of Ferocity for example
  462. case Bonus::FULL_HP_REGENERATION:
  463. case Bonus::LIFE_DRAIN: //TODO: chance, hp percentage?
  464. case Bonus::SELF_MORALE:
  465. case Bonus::SELF_LUCK:
  466. case Bonus::FEAR:
  467. case Bonus::FEARLESS:
  468. case Bonus::CHARGE_IMMUNITY:
  469. case Bonus::HEALER:
  470. case Bonus::CATAPULT:
  471. case Bonus::DRAGON_NATURE:
  472. case Bonus::NON_LIVING:
  473. case Bonus::UNDEAD:
  474. break;
  475. //One numeric value
  476. case Bonus::MAGIC_RESISTANCE:
  477. case Bonus::SPELL_RESISTANCE_AURA:
  478. case Bonus::SPELL_DAMAGE_REDUCTION:
  479. case Bonus::LEVEL_SPELL_IMMUNITY:
  480. case Bonus::MANA_DRAIN:
  481. case Bonus::HP_REGENERATION:
  482. case Bonus::ADDITIONAL_RETALIATION:
  483. case Bonus::DOUBLE_DAMAGE_CHANCE:
  484. case Bonus::DARKNESS: //Darkness Dragons any1?
  485. boost::algorithm::replace_first(text, "%d", boost::lexical_cast<std::string>(valOfBonuses(Selector::typeSybtype(bonus->type, bonus->subtype))));
  486. break;
  487. //Complex descriptions
  488. case Bonus::HATE:
  489. boost::algorithm::replace_first(text, "%d", boost::lexical_cast<std::string>(valOfBonuses(Selector::typeSybtype(bonus->type, bonus->subtype))));
  490. boost::algorithm::replace_first(text, "%s", VLC->creh->creatures[bonus->subtype]->namePl);
  491. break;
  492. case Bonus::SPELL_AFTER_ATTACK:
  493. boost::algorithm::replace_first(text, "%d", boost::lexical_cast<std::string>(bonus->additionalInfo));
  494. boost::algorithm::replace_first(text, "%s", VLC->spellh->spells[bonus->subtype]->name);
  495. break;
  496. default:
  497. {}//TODO: allow custom bonus types... someday, somehow
  498. }
  499. }
  500. else //short name
  501. {
  502. text = it->second.first;
  503. switch (bonus->type)
  504. {
  505. case Bonus::MANA_CHANNELING:
  506. case Bonus::MAGIC_MIRROR:
  507. case Bonus::CHANGES_SPELL_COST_FOR_ALLY:
  508. case Bonus::CHANGES_SPELL_COST_FOR_ENEMY:
  509. case Bonus::ENEMY_DEFENCE_REDUCTION:
  510. boost::algorithm::replace_first(text, "%d", boost::lexical_cast<std::string>(valOfBonuses(Selector::typeSybtype(bonus->type, bonus->subtype))));
  511. break;
  512. case Bonus::HATE:
  513. boost::algorithm::replace_first(text, "%s", VLC->creh->creatures[bonus->subtype]->namePl);
  514. break;
  515. case Bonus::LEVEL_SPELL_IMMUNITY:
  516. boost::algorithm::replace_first(text, "%d", boost::lexical_cast<std::string>(bonus->val));
  517. break;
  518. case Bonus::SPELL_AFTER_ATTACK:
  519. boost::algorithm::replace_first(text, "%s", VLC->spellh->spells[bonus->subtype]->name);
  520. break;
  521. case Bonus::SPELL_IMMUNITY:
  522. boost::algorithm::replace_first(text, "%s", VLC->spellh->spells[bonus->subtype]->name);
  523. break;
  524. }
  525. }
  526. return text;
  527. }
  528. else
  529. return "";
  530. }
  531. std::string CStackInstance::bonusToGraphics(Bonus *bonus) const
  532. {
  533. std::string fileName;
  534. switch (bonus->type)
  535. {
  536. //"E_ALIVE.bmp"
  537. //"E_ART.bmp"
  538. //"E_BLESS.bmp"
  539. //"E_BLOCK.bmp"
  540. //"E_BLOCK1.bmp"
  541. //"E_BLOCK2.bmp"
  542. case Bonus::TWO_HEX_ATTACK_BREATH:
  543. fileName = "E_BREATH.bmp"; break;
  544. case Bonus::SPELL_AFTER_ATTACK:
  545. fileName = "E_CAST.bmp"; break;
  546. //"E_CAST1.bmp"
  547. //"E_CAST2.bmp"
  548. //"E_CASTER.bmp"
  549. case Bonus::JOUSTING:
  550. fileName = "E_CHAMP.bmp"; break;
  551. case Bonus::DOUBLE_DAMAGE_CHANCE:
  552. fileName = "E_DBLOW.bmp"; break;
  553. //"E_DEATH.bmp"
  554. //"E_DEFBON.bmp"
  555. case Bonus::NO_DISTANCE_PENALTY:
  556. fileName = "E_DIST.bmp"; break;
  557. case Bonus::ADDITIONAL_ATTACK:
  558. fileName = "E_DOUBLE.bmp"; break;
  559. case Bonus::DRAGON_NATURE:
  560. fileName = "E_DRAGON.bmp"; break;
  561. case Bonus::MAGIC_RESISTANCE:
  562. fileName = "E_DWARF.bmp"; break;
  563. case Bonus::FEAR:
  564. fileName = "E_FEAR.bmp"; break;
  565. case Bonus::FEARLESS:
  566. fileName = "E_FEARL.bmp"; break;
  567. //"E_FIRE.bmp"
  568. case Bonus::FLYING:
  569. fileName = "E_FLY.bmp"; break;
  570. case Bonus::SPELL_DAMAGE_REDUCTION:
  571. fileName = "E_GOLEM.bmp"; break;
  572. case Bonus::RETURN_AFTER_STRIKE:
  573. fileName = "E_HARPY.bmp"; break;
  574. case Bonus::HATE:
  575. fileName = "E_HATE.bmp"; break;
  576. case Bonus::KING1:
  577. fileName = "E_KING1.bmp"; break;
  578. case Bonus::KING2:
  579. fileName = "E_KING2.bmp"; break;
  580. case Bonus::KING3:
  581. fileName = "E_KING3.bmp"; break;
  582. //"E_LIGHT.bmp"
  583. case Bonus::CHANGES_SPELL_COST_FOR_ALLY:
  584. fileName = "E_MANA.bmp"; break;
  585. case Bonus::NO_MELEE_PENALTY:
  586. fileName = "E_MELEE.bmp"; break;
  587. //"E_MIND.bmp"
  588. case Bonus::SELF_MORALE:
  589. fileName = "E_MINOT.bmp"; break;
  590. case Bonus::NO_MORALE:
  591. fileName = "E_MORAL.bmp"; break;
  592. //"E_NOFRIM.bmp"
  593. case Bonus::NO_OBSTACLES_PENALTY:
  594. fileName = "E_OBST.bmp"; break;
  595. case Bonus::ENEMY_DEFENCE_REDUCTION:
  596. fileName = "E_RDEF.bmp"; break;
  597. //"E_REBIRTH.bmp"
  598. case Bonus::BLOCKS_RETALIATION:
  599. fileName = "E_RETAIL.bmp"; break;
  600. case Bonus::ADDITIONAL_RETALIATION:
  601. fileName = "E_RETAIL1.bmp"; break;
  602. case Bonus::ATTACKS_ALL_ADJACENT:
  603. fileName = "E_ROUND.bmp"; break;
  604. //"E_SGNUM.bmp"
  605. //"E_SGTYPE.bmp"
  606. case Bonus::SHOOTER:
  607. fileName = "E_SHOOT.bmp"; break;
  608. case Bonus::FREE_SHOOTING: //shooter is not blocked by enemy
  609. fileName = "E_SHOOTA.bmp"; break;
  610. //"E_SHOOTN.bmp"
  611. //"E_SPAIR.bmp"
  612. //"E_SPAIR1.bmp"
  613. case Bonus::SPELL_IMMUNITY:
  614. {
  615. switch (bonus->subtype)
  616. {
  617. case 74: //Blind
  618. fileName = "E_SPBLIND.bmp"; break;
  619. case 60: //Hypnotize
  620. fileName = "E_SPHYPN.bmp"; break;
  621. case 18: //Implosion
  622. fileName = "E_SPIMP.bmp"; break;
  623. case 59: //Berserk
  624. fileName = "E_SPBERS.bmp"; break;
  625. case 23: //Meteor Shower
  626. fileName = "E_SPMET.bmp"; break;
  627. case 26: //Armageddon
  628. fileName = "E_SPARM.bmp"; break;
  629. case 54: //Slow
  630. fileName = "E_SPSLOW.bmp"; break;
  631. //TODO: some generic spell handling?
  632. }
  633. }
  634. //"E_SPAWILL.bmp"
  635. //"E_SPCOLD.bmp"
  636. //"E_SPDFIRE.bmp"
  637. //"E_SPDIR.bmp"
  638. //"E_SPDISB.bmp"
  639. //"E_SPDISP.bmp"
  640. //"E_SPEATH.bmp"
  641. //"E_SPEATH1.bmp"
  642. //"E_SPFIRE.bmp"
  643. //"E_SPFIRE1.bmp"
  644. case Bonus::LEVEL_SPELL_IMMUNITY:
  645. {
  646. if (iswith(bonus->val, 1 , 5))
  647. {
  648. fileName = "E_SPLVL" + boost::lexical_cast<std::string>(bonus->val) + ".bmp";
  649. }
  650. break;
  651. }
  652. //"E_SPWATER.bmp"
  653. //"E_SPWATER1.bmp"
  654. //"E_SUMMON.bmp"
  655. //"E_SUMMON1.bmp"
  656. //"E_SUMMON2.bmp"
  657. case Bonus::FULL_HP_REGENERATION:
  658. fileName = "E_TROLL.bmp"; break;
  659. case Bonus::UNDEAD:
  660. fileName = "E_UNDEAD.bmp"; break;
  661. case Bonus::SPELL_RESISTANCE_AURA:
  662. fileName = "E_UNIC.bmp"; break;
  663. }
  664. return fileName;
  665. }
  666. void CStackInstance::setArmyObj(const CArmedInstance *ArmyObj)
  667. {
  668. if(_armyObj)
  669. detachFrom(const_cast<CArmedInstance*>(_armyObj));
  670. _armyObj = ArmyObj;
  671. if(ArmyObj)
  672. {
  673. attachTo(const_cast<CArmedInstance*>(_armyObj));
  674. }
  675. }
  676. // void CStackInstance::getParents(TCNodes &out, const CBonusSystemNode *source /*= NULL*/) const
  677. // {
  678. // out.insert(type);
  679. //
  680. // if(source && source != this) //we should be root, if not - do not inherit anything
  681. // return;
  682. //
  683. // if(armyObj)
  684. // out.insert(armyObj);
  685. // else
  686. // out.insert(&IObjectInterface::cb->gameState()->globalEffects);
  687. // }
  688. std::string CStackInstance::getQuantityTXT(bool capitalized /*= true*/) const
  689. {
  690. return VLC->generaltexth->arraytxt[174 + getQuantityID()*3 + 2 - capitalized];
  691. }
  692. bool CStackInstance::valid(bool allowUnrandomized) const
  693. {
  694. bool isRand = (idRand != -1);
  695. if(!isRand)
  696. {
  697. return (type && type == VLC->creh->creatures[type->idNumber]);
  698. }
  699. else
  700. return allowUnrandomized;
  701. }
  702. CStackInstance::~CStackInstance()
  703. {
  704. }
  705. std::string CStackInstance::nodeName() const
  706. {
  707. std::ostringstream oss;
  708. oss << "Stack of " << count << " of ";
  709. if(type)
  710. oss << type->namePl;
  711. else if(idRand)
  712. oss << "[no type, idRand=" << idRand << "]";
  713. else
  714. oss << "[UNDEFINED TYPE]";
  715. return oss.str();
  716. }
  717. void CStackInstance::deserializationFix()
  718. {
  719. const CCreature *backup = type;
  720. type = NULL;
  721. setType(backup);
  722. const CArmedInstance *armyBackup = _armyObj;
  723. _armyObj = NULL;
  724. setArmyObj(armyBackup);
  725. }
  726. CStackBasicDescriptor::CStackBasicDescriptor()
  727. {
  728. type = NULL;
  729. count = -1;
  730. }
  731. CStackBasicDescriptor::CStackBasicDescriptor(TCreature id, TQuantity Count)
  732. : type (VLC->creh->creatures[id]), count(Count)
  733. {
  734. }
  735. CStackBasicDescriptor::CStackBasicDescriptor(const CCreature *c, TQuantity Count)
  736. : type(c), count(Count)
  737. {
  738. }
  739. DLL_EXPORT std::ostream & operator<<(std::ostream & str, const CStackInstance & sth)
  740. {
  741. if(!sth.valid(true))
  742. str << "an invalid stack!";
  743. str << "stack with " << sth.count << " of ";
  744. if(sth.type)
  745. str << sth.type->namePl;
  746. else
  747. str << sth.idRand;
  748. return str;
  749. }
  750. void CSimpleArmy::clear()
  751. {
  752. army.clear();
  753. }
  754. CSimpleArmy::operator bool() const
  755. {
  756. return army.size();
  757. }
  758. bool CSimpleArmy::setCreature(TSlot slot, TCreature cre, TQuantity count)
  759. {
  760. assert(!vstd::contains(army, slot));
  761. army[slot] = CStackBasicDescriptor(cre, count);
  762. return true;
  763. }