CCreatureSet.cpp 27 KB

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