2
0

CCreatureSet.cpp 27 KB

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