CCreatureSet.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092
  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. expType 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(expType 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, expType 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(!hasStackAtSlot(slot));
  285. stacks[slot] = stack;
  286. stack->setArmyObj(castToArmyObj());
  287. armyChanged();
  288. }
  289. void CCreatureSet::joinStack(TSlot slot, CStackInstance * stack)
  290. {
  291. const CCreature *c = getCreature(slot);
  292. assert(c == stack->type);
  293. assert(c);
  294. //TODO move stuff
  295. changeStackCount(slot, stack->count);
  296. vstd::clear_pointer(stack);
  297. }
  298. void CCreatureSet::changeStackCount(TSlot slot, TQuantity toAdd)
  299. {
  300. setStackCount(slot, getStackCount(slot) + toAdd);
  301. }
  302. CCreatureSet::CCreatureSet()
  303. {
  304. formation = false;
  305. }
  306. CCreatureSet::CCreatureSet(const CCreatureSet&)
  307. {
  308. assert(0);
  309. }
  310. CCreatureSet::~CCreatureSet()
  311. {
  312. clear();
  313. }
  314. void CCreatureSet::setToArmy(CSimpleArmy &src)
  315. {
  316. clear();
  317. while(src)
  318. {
  319. TSimpleSlots::iterator i = src.army.begin();
  320. assert(i->second.type);
  321. assert(i->second.count);
  322. putStack(i->first, new CStackInstance(i->second.type, i->second.count));
  323. src.army.erase(i);
  324. }
  325. }
  326. CStackInstance * CCreatureSet::detachStack(TSlot slot)
  327. {
  328. assert(hasStackAtSlot(slot));
  329. CStackInstance *ret = stacks[slot];
  330. //if(CArmedInstance *armedObj = castToArmyObj())
  331. {
  332. ret->setArmyObj(NULL); //detaches from current armyobj
  333. }
  334. assert(!ret->armyObj); //we failed detaching?
  335. stacks.erase(slot);
  336. armyChanged();
  337. return ret;
  338. }
  339. void CCreatureSet::setStackType(TSlot slot, const CCreature *type)
  340. {
  341. assert(hasStackAtSlot(slot));
  342. CStackInstance *s = stacks[slot];
  343. s->setType(type->idNumber);
  344. armyChanged();
  345. }
  346. bool CCreatureSet::canBeMergedWith(const CCreatureSet &cs, bool allowMergingStacks) const
  347. {
  348. if(!allowMergingStacks)
  349. {
  350. int freeSlots = stacksCount() - GameConstants::ARMY_SIZE;
  351. std::set<const CCreature*> cresToAdd;
  352. for(TSlots::const_iterator i = cs.stacks.begin(); i != cs.stacks.end(); i++)
  353. {
  354. TSlot dest = getSlotFor(i->second->type);
  355. if(dest < 0 || hasStackAtSlot(dest))
  356. cresToAdd.insert(i->second->type);
  357. }
  358. return cresToAdd.size() <= freeSlots;
  359. }
  360. else
  361. {
  362. CCreatureSet cres;
  363. //get types of creatures that need their own slot
  364. for(TSlots::const_iterator i = cs.stacks.begin(); i != cs.stacks.end(); i++)
  365. cres.addToSlot(i->first, i->second->type->idNumber, 1, true);
  366. TSlot j;
  367. for(TSlots::const_iterator i = stacks.begin(); i != stacks.end(); i++)
  368. {
  369. if ((j = cres.getSlotFor(i->second->type)) >= 0)
  370. cres.addToSlot(j, i->second->type->idNumber, 1, true); //merge if possible
  371. else
  372. return false; //no place found
  373. }
  374. return true; //all stacks found their slots
  375. }
  376. }
  377. bool CCreatureSet::hasStackAtSlot(TSlot slot) const
  378. {
  379. return vstd::contains(stacks, slot);
  380. }
  381. CCreatureSet & CCreatureSet::operator=(const CCreatureSet&cs)
  382. {
  383. assert(0);
  384. return *this;
  385. }
  386. void CCreatureSet::armyChanged()
  387. {
  388. }
  389. CStackInstance::CStackInstance()
  390. : armyObj(_armyObj)
  391. {
  392. init();
  393. }
  394. CStackInstance::CStackInstance(TCreature id, TQuantity Count)
  395. : armyObj(_armyObj)
  396. {
  397. init();
  398. setType(id);
  399. count = Count;
  400. }
  401. CStackInstance::CStackInstance(const CCreature *cre, TQuantity Count)
  402. : armyObj(_armyObj)
  403. {
  404. init();
  405. setType(cre);
  406. count = Count;
  407. }
  408. void CStackInstance::init()
  409. {
  410. experience = 0;
  411. count = 0;
  412. type = NULL;
  413. idRand = -1;
  414. _armyObj = NULL;
  415. setNodeType(STACK_INSTANCE);
  416. }
  417. int CStackInstance::getQuantityID() const
  418. {
  419. return CCreature::getQuantityID(count);
  420. }
  421. int CStackInstance::getExpRank() const
  422. {
  423. int tier = type->level;
  424. if (vstd::iswithin(tier, 1, 7))
  425. {
  426. for (int i = VLC->creh->expRanks[tier].size()-2; i >-1; --i)//sic!
  427. { //exp values vary from 1st level to max exp at 11th level
  428. if (experience >= VLC->creh->expRanks[tier][i])
  429. return ++i; //faster, but confusing - 0 index mean 1st level of experience
  430. }
  431. return 0;
  432. }
  433. else //higher tier
  434. {
  435. for (int i = VLC->creh->expRanks[0].size()-2; i >-1; --i)
  436. {
  437. if (experience >= VLC->creh->expRanks[0][i])
  438. return ++i;
  439. }
  440. return 0;
  441. }
  442. }
  443. si32 CStackInstance::magicResistance() const
  444. {
  445. si32 val = valOfBonuses(Selector::type(Bonus::MAGIC_RESISTANCE));
  446. if (const CGHeroInstance * hero = dynamic_cast<const CGHeroInstance *>(_armyObj))
  447. {
  448. //resistance skill
  449. val += hero->valOfBonuses(Bonus::SECONDARY_SKILL_PREMY, CGHeroInstance::RESISTANCE);
  450. }
  451. vstd::amin (val, 100);
  452. return val;
  453. }
  454. void CStackInstance::giveStackExp(expType exp)
  455. {
  456. int level = type->level;
  457. if (!vstd::iswithin(level, 1, 7))
  458. level = 0;
  459. CCreatureHandler * creh = VLC->creh;
  460. ui32 maxExp = creh->expRanks[level].back();
  461. vstd::amin(exp, (expType)maxExp); //prevent exp overflow due to different types
  462. vstd::amin(exp, (maxExp * creh->maxExpPerBattle[level])/100);
  463. vstd::amin(experience += exp, maxExp); //can't get more exp than this limit
  464. }
  465. void CStackInstance::setType(int creID)
  466. {
  467. if(creID >= 0 && creID < VLC->creh->creatures.size())
  468. setType(VLC->creh->creatures[creID]);
  469. else
  470. setType((const CCreature*)NULL);
  471. }
  472. void CStackInstance::setType(const CCreature *c)
  473. {
  474. if(type)
  475. {
  476. detachFrom(const_cast<CCreature*>(type));
  477. if (type->isMyUpgrade(c) && VLC->modh->modules.STACK_EXP)
  478. experience *= VLC->creh->expAfterUpgrade / 100.0;
  479. }
  480. type = c;
  481. if(type)
  482. attachTo(const_cast<CCreature*>(type));
  483. }
  484. std::string CStackInstance::bonusToString(Bonus *bonus, bool description) const
  485. {
  486. std::map<TBonusType, std::pair<std::string, std::string> >::iterator it = VLC->creh->stackBonuses.find(bonus->type);
  487. if (it != VLC->creh->stackBonuses.end())
  488. {
  489. std::string text;
  490. if (description) //long ability description
  491. {
  492. text = it->second.second;
  493. switch (bonus->type)
  494. {
  495. //no additional modifiers needed
  496. case Bonus::FLYING:
  497. case Bonus::UNLIMITED_RETALIATIONS:
  498. case Bonus::SHOOTER:
  499. case Bonus::FREE_SHOOTING:
  500. case Bonus::NO_MELEE_PENALTY:
  501. case Bonus::NO_DISTANCE_PENALTY:
  502. case Bonus::NO_WALL_PENALTY:
  503. case Bonus::JOUSTING: //TODO: percent bonus?
  504. case Bonus::RETURN_AFTER_STRIKE:
  505. case Bonus::BLOCKS_RETALIATION:
  506. case Bonus::TWO_HEX_ATTACK_BREATH:
  507. case Bonus::THREE_HEADED_ATTACK:
  508. case Bonus::ATTACKS_ALL_ADJACENT:
  509. case Bonus::ADDITIONAL_ATTACK: //TODO: what with more than one attack? Axe of Ferocity for example
  510. case Bonus::FULL_HP_REGENERATION:
  511. case Bonus::REBIRTH:
  512. case Bonus::LIFE_DRAIN: //TODO: chance, hp percentage?
  513. case Bonus::SELF_MORALE:
  514. case Bonus::SELF_LUCK:
  515. case Bonus::FEAR:
  516. case Bonus::FEARLESS:
  517. case Bonus::CHARGE_IMMUNITY:
  518. case Bonus::HEALER:
  519. case Bonus::CATAPULT:
  520. case Bonus::DRAGON_NATURE:
  521. case Bonus::NON_LIVING:
  522. case Bonus::UNDEAD:
  523. case Bonus::FIRE_IMMUNITY:
  524. case Bonus::WATER_IMMUNITY:
  525. case Bonus::AIR_IMMUNITY:
  526. case Bonus::EARTH_IMMUNITY:
  527. case Bonus::RECEPTIVE:
  528. case Bonus::DIRECT_DAMAGE_IMMUNITY:
  529. break;
  530. //One numeric value. magic resistance handled separately
  531. case Bonus::SPELL_RESISTANCE_AURA:
  532. case Bonus::SPELL_DAMAGE_REDUCTION:
  533. case Bonus::LEVEL_SPELL_IMMUNITY:
  534. case Bonus::MANA_DRAIN:
  535. case Bonus::HP_REGENERATION:
  536. case Bonus::ADDITIONAL_RETALIATION:
  537. case Bonus::DEFENSIVE_STANCE:
  538. case Bonus::DOUBLE_DAMAGE_CHANCE:
  539. case Bonus::DARKNESS: //Darkness Dragons any1?
  540. boost::algorithm::replace_first(text, "%d", boost::lexical_cast<std::string>(valOfBonuses(Selector::typeSubtype(bonus->type, bonus->subtype))));
  541. break;
  542. //Complex descriptions
  543. //case Bonus::SECONDARY_SKILL_PREMY: //only if there's no simple MR
  544. // if (bonus->subtype == CGHeroInstance::RESISTANCE)
  545. // {
  546. // if (!hasBonusOfType(Bonus::MAGIC_RESISTANCE))
  547. // boost::algorithm::replace_first(text, "%d", boost::lexical_cast<std::string>( magicResistance() ));
  548. // }
  549. // break;
  550. //case Bonus::MAGIC_RESISTANCE:
  551. // boost::algorithm::replace_first(text, "%d", boost::lexical_cast<std::string>( magicResistance() ));
  552. // break;
  553. case Bonus::HATE:
  554. boost::algorithm::replace_first(text, "%d", boost::lexical_cast<std::string>(valOfBonuses(Selector::typeSubtype(bonus->type, bonus->subtype))));
  555. boost::algorithm::replace_first(text, "%s", VLC->creh->creatures[bonus->subtype]->namePl);
  556. break;
  557. case Bonus::SPELL_AFTER_ATTACK:
  558. case Bonus::SPELL_BEFORE_ATTACK:
  559. {
  560. boost::algorithm::replace_first(text, "%d", boost::lexical_cast<std::string>(valOfBonuses(Selector::typeSubtype(bonus->type, bonus->subtype))));
  561. boost::algorithm::replace_first(text, "%s", VLC->spellh->spells[bonus->subtype]->name);
  562. break;
  563. }
  564. default:
  565. {}//TODO: allow custom bonus types... someday, somehow
  566. }
  567. }
  568. else //short name
  569. {
  570. text = it->second.first;
  571. switch (bonus->type)
  572. {
  573. case Bonus::MANA_CHANNELING:
  574. case Bonus::MAGIC_MIRROR:
  575. case Bonus::CHANGES_SPELL_COST_FOR_ALLY:
  576. case Bonus::CHANGES_SPELL_COST_FOR_ENEMY:
  577. case Bonus::ENEMY_DEFENCE_REDUCTION:
  578. case Bonus::REBIRTH:
  579. case Bonus::DEATH_STARE:
  580. boost::algorithm::replace_first(text, "%d", boost::lexical_cast<std::string>(valOfBonuses(Selector::typeSubtype(bonus->type, bonus->subtype))));
  581. break;
  582. case Bonus::HATE:
  583. case Bonus::DAEMON_SUMMONING:
  584. boost::algorithm::replace_first(text, "%s", VLC->creh->creatures[bonus->subtype]->namePl);
  585. break;
  586. case Bonus::LEVEL_SPELL_IMMUNITY:
  587. boost::algorithm::replace_first(text, "%d", boost::lexical_cast<std::string>(bonus->val));
  588. break;
  589. case Bonus::SPELL_AFTER_ATTACK:
  590. case Bonus::SPELL_BEFORE_ATTACK:
  591. case Bonus::SPELL_IMMUNITY:
  592. case Bonus::SPELLCASTER:
  593. case Bonus::ENCHANTER:
  594. boost::algorithm::replace_first(text, "%s", VLC->spellh->spells[bonus->subtype]->name);
  595. break;
  596. case Bonus::MAGIC_RESISTANCE:
  597. text = ""; //handled separately
  598. break;
  599. //case Bonus::SECONDARY_SKILL_PREMY:
  600. // if (bonus->subtype != CGHeroInstance::RESISTANCE || hasBonusOfType(Bonus::MAGIC_RESISTANCE)) //handle it there
  601. // text = "";
  602. // break;
  603. }
  604. }
  605. return text;
  606. }
  607. else
  608. return "";
  609. }
  610. std::string CStackInstance::bonusToGraphics(Bonus *bonus) const
  611. {
  612. std::string fileName;
  613. switch (bonus->type)
  614. {
  615. //"E_ALIVE.bmp"
  616. //"E_ART.bmp"
  617. //"E_BLESS.bmp"
  618. //"E_BLOCK.bmp"
  619. //"E_BLOCK1.bmp"
  620. //"E_BLOCK2.bmp"
  621. case Bonus::TWO_HEX_ATTACK_BREATH:
  622. fileName = "E_BREATH.bmp"; break;
  623. case Bonus::SPELL_AFTER_ATTACK:
  624. fileName = "E_CAST.bmp"; break;
  625. case Bonus::ENCHANTER:
  626. fileName = "E_CAST1.bmp"; break;
  627. case Bonus::RANDOM_SPELLCASTER:
  628. fileName = "RandomBoost.bmp"; break;
  629. case Bonus::SPELL_BEFORE_ATTACK:
  630. fileName ="E_CAST2.bmp"; break;
  631. case Bonus::SPELLCASTER:
  632. fileName = "E_CASTER.bmp"; break;
  633. case Bonus::JOUSTING:
  634. fileName = "E_CHAMP.bmp"; break;
  635. case Bonus::DOUBLE_DAMAGE_CHANCE:
  636. fileName = "E_DBLOW.bmp"; break;
  637. case Bonus::DEATH_STARE:
  638. fileName = "E_DEATH.bmp"; break;
  639. case Bonus::DEFENSIVE_STANCE:
  640. fileName = "E_DEFBON.bmp"; break;
  641. case Bonus::NO_DISTANCE_PENALTY:
  642. fileName = "E_DIST.bmp"; break;
  643. case Bonus::ADDITIONAL_ATTACK:
  644. fileName = "E_DOUBLE.bmp"; break;
  645. case Bonus::DRAGON_NATURE:
  646. fileName = "E_DRAGON.bmp"; break;
  647. case Bonus::MAGIC_RESISTANCE:
  648. fileName = "E_DWARF.bmp"; break;
  649. case Bonus::SECONDARY_SKILL_PREMY:
  650. if (bonus->subtype == CGHeroInstance::RESISTANCE)
  651. {
  652. fileName = "E_DWARF.bmp";
  653. }
  654. break;
  655. case Bonus::FEAR:
  656. fileName = "E_FEAR.bmp"; break;
  657. case Bonus::FEARLESS:
  658. fileName = "E_FEARL.bmp"; break;
  659. case Bonus::FLYING:
  660. fileName = "E_FLY.bmp"; break;
  661. case Bonus::SPELL_DAMAGE_REDUCTION:
  662. fileName = "E_GOLEM.bmp"; break;
  663. case Bonus::RETURN_AFTER_STRIKE:
  664. fileName = "E_HARPY.bmp"; break;
  665. case Bonus::HATE:
  666. fileName = "E_HATE.bmp"; break;
  667. case Bonus::KING1:
  668. fileName = "E_KING1.bmp"; break;
  669. case Bonus::KING2:
  670. fileName = "E_KING2.bmp"; break;
  671. case Bonus::KING3:
  672. fileName = "E_KING3.bmp"; break;
  673. case Bonus::CHANGES_SPELL_COST_FOR_ALLY:
  674. fileName = "E_MANA.bmp"; break;
  675. case Bonus::CHANGES_SPELL_COST_FOR_ENEMY:
  676. fileName = "MagicDamper.bpm"; break;
  677. case Bonus::NO_MELEE_PENALTY:
  678. fileName = "E_MELEE.bmp"; break;
  679. case Bonus::MIND_IMMUNITY:
  680. fileName = "E_MIND.bmp"; break;
  681. case Bonus::SELF_MORALE:
  682. fileName = "E_MINOT.bmp"; break;
  683. case Bonus::NO_MORALE:
  684. fileName = "E_MORAL.bmp"; break;
  685. case Bonus::RECEPTIVE:
  686. fileName = "E_NOFRIM.bmp"; break;
  687. case Bonus::NO_WALL_PENALTY:
  688. fileName = "E_OBST.bmp"; break;
  689. case Bonus::ENEMY_DEFENCE_REDUCTION:
  690. fileName = "E_RDEF.bmp"; break;
  691. case Bonus::REBIRTH:
  692. fileName = "E_REBIRTH.bmp"; break;
  693. case Bonus::BLOCKS_RETALIATION:
  694. fileName = "E_RETAIL.bmp"; break;
  695. case Bonus::UNLIMITED_RETALIATIONS:
  696. case Bonus::ADDITIONAL_RETALIATION:
  697. fileName = "E_RETAIL1.bmp"; break;
  698. case Bonus::ATTACKS_ALL_ADJACENT:
  699. fileName = "E_ROUND.bmp"; break;
  700. //"E_SGNUM.bmp"
  701. //"E_SGTYPE.bmp"
  702. case Bonus::SHOOTER:
  703. fileName = "E_SHOOT.bmp"; break;
  704. case Bonus::FREE_SHOOTING: //shooter is not blocked by enemy
  705. fileName = "E_SHOOTA.bmp"; break;
  706. //"E_SHOOTN.bmp"
  707. case Bonus::SPELL_IMMUNITY:
  708. {
  709. switch (bonus->subtype)
  710. {
  711. case 62: //Blind
  712. fileName = "E_SPBLIND.bmp"; break;
  713. case 35: // Dispell
  714. fileName = "E_SPDISP.bmp"; break;
  715. case 78: // Dispell beneficial spells
  716. fileName = "E_SPDISB.bmp"; break;
  717. case 60: //Hypnotize
  718. fileName = "E_SPHYPN.bmp"; break;
  719. case 18: //Implosion
  720. fileName = "E_SPIMP.bmp"; break;
  721. case 59: //Berserk
  722. fileName = "E_SPBERS.bmp"; break;
  723. case 23: //Meteor Shower
  724. fileName = "E_SPMET.bmp"; break;
  725. case 26: //Armageddon
  726. fileName = "E_SPARM.bmp"; break;
  727. case 54: //Slow
  728. fileName = "E_SPSLOW.bmp"; break;
  729. //TODO: some generic spell handling?
  730. }
  731. break;
  732. }
  733. //"E_SPAWILL.bmp"
  734. case Bonus::DIRECT_DAMAGE_IMMUNITY:
  735. fileName = "E_SPDIR.bmp"; break;
  736. //"E_SPDISB.bmp"
  737. //"E_SPDISP.bmp"
  738. //"E_SPEATH.bmp"
  739. //"E_SPEATH1.bmp"
  740. case Bonus::FIRE_IMMUNITY:
  741. switch (bonus->subtype)
  742. {
  743. case 0:
  744. fileName = "E_SPFIRE.bmp"; break; //all
  745. case 1:
  746. fileName = "E_SPFIRE1.bmp"; break; //not positive
  747. case 2:
  748. fileName = "E_FIRE.bmp"; break; //direct damage
  749. }
  750. break;
  751. case Bonus::WATER_IMMUNITY:
  752. switch (bonus->subtype)
  753. {
  754. case 0:
  755. fileName = "E_SPWATER.bmp"; break; //all
  756. case 1:
  757. fileName = "E_SPWATER1.bmp"; break; //not positive
  758. case 2:
  759. fileName = "E_SPCOLD.bmp"; break; //direct damage
  760. }
  761. break;
  762. case Bonus::AIR_IMMUNITY:
  763. switch (bonus->subtype)
  764. {
  765. case 0:
  766. fileName = "E_SPAIR.bmp"; break; //all
  767. case 1:
  768. fileName = "E_SPAIR1.bmp"; break; //not positive
  769. case 2:
  770. fileName = "E_LIGHT.bmp"; break;//direct damage
  771. }
  772. break;
  773. case Bonus::EARTH_IMMUNITY:
  774. switch (bonus->subtype)
  775. {
  776. case 0:
  777. fileName = "E_SPEATH.bmp"; break; //all
  778. case 1:
  779. case 2: //no specific icon for direct damage immunity
  780. fileName = "E_SPEATH1.bmp"; break; //not positive
  781. }
  782. break;
  783. case Bonus::LEVEL_SPELL_IMMUNITY:
  784. {
  785. if (vstd::iswithin(bonus->val, 1 , 5))
  786. {
  787. fileName = "E_SPLVL" + boost::lexical_cast<std::string>(bonus->val) + ".bmp";
  788. }
  789. break;
  790. }
  791. //"E_SUMMON.bmp"
  792. //"E_SUMMON1.bmp"
  793. //"E_SUMMON2.bmp"
  794. case Bonus::FULL_HP_REGENERATION:
  795. case Bonus::HP_REGENERATION:
  796. fileName = "E_TROLL.bmp"; break;
  797. case Bonus::UNDEAD:
  798. fileName = "E_UNDEAD.bmp"; break;
  799. case Bonus::SPELL_RESISTANCE_AURA:
  800. fileName = "E_UNIC.bmp"; break;
  801. case Bonus::THREE_HEADED_ATTACK:
  802. fileName = "ThreeHeaded.bmp"; break;
  803. case Bonus::DAEMON_SUMMONING:
  804. fileName = "RiseDemons.bmp"; break;
  805. case Bonus::CHARGE_IMMUNITY:
  806. fileName = "ChargeImmune.bmp"; break;
  807. case Bonus::HEALER:
  808. fileName = "Healer.bmp"; break;
  809. case Bonus::CATAPULT:
  810. fileName = "Catapult.bmp"; break;
  811. case Bonus::MANA_CHANNELING:
  812. fileName = "ManaChannel.bmp"; break;
  813. case Bonus::MANA_DRAIN:
  814. fileName = "ManaDrain.bmp"; break;
  815. }
  816. if(!fileName.empty())
  817. fileName = "zvs/Lib1.res/" + fileName;
  818. return fileName;
  819. }
  820. void CStackInstance::setArmyObj(const CArmedInstance *ArmyObj)
  821. {
  822. if(_armyObj)
  823. detachFrom(const_cast<CArmedInstance*>(_armyObj));
  824. _armyObj = ArmyObj;
  825. if(ArmyObj)
  826. {
  827. attachTo(const_cast<CArmedInstance*>(_armyObj));
  828. }
  829. }
  830. std::string CStackInstance::getQuantityTXT(bool capitalized /*= true*/) const
  831. {
  832. int quantity = getQuantityID();
  833. if (quantity)
  834. return VLC->generaltexth->arraytxt[174 + quantity*3 - 1 - capitalized];
  835. else
  836. return "";
  837. }
  838. bool CStackInstance::valid(bool allowUnrandomized) const
  839. {
  840. bool isRand = (idRand != -1);
  841. if(!isRand)
  842. {
  843. return (type && type == VLC->creh->creatures[type->idNumber]);
  844. }
  845. else
  846. return allowUnrandomized;
  847. }
  848. CStackInstance::~CStackInstance()
  849. {
  850. }
  851. std::string CStackInstance::nodeName() const
  852. {
  853. std::ostringstream oss;
  854. oss << "Stack of " << count << " of ";
  855. if(type)
  856. oss << type->namePl;
  857. else if(idRand)
  858. oss << "[no type, idRand=" << idRand << "]";
  859. else
  860. oss << "[UNDEFINED TYPE]";
  861. return oss.str();
  862. }
  863. void CStackInstance::deserializationFix()
  864. {
  865. const CCreature *backup = type;
  866. type = NULL;
  867. setType(backup);
  868. const CArmedInstance *armyBackup = _armyObj;
  869. _armyObj = NULL;
  870. setArmyObj(armyBackup);
  871. artDeserializationFix(this);
  872. }
  873. int CStackInstance::getCreatureID() const
  874. {
  875. if(type)
  876. return type->idNumber;
  877. else
  878. return -1;
  879. }
  880. std::string CStackInstance::getName() const
  881. {
  882. return (count > 1) ? type->namePl : type->nameSing;
  883. }
  884. ui64 CStackInstance::getPower() const
  885. {
  886. assert(type);
  887. return type->AIValue * count;
  888. }
  889. ui8 CStackInstance::bearerType() const
  890. {
  891. return ArtBearer::CREATURE;
  892. }
  893. CCommanderInstance::CCommanderInstance()
  894. {
  895. init();
  896. name = "Unnamed";
  897. }
  898. CCommanderInstance::CCommanderInstance (TCreature id)
  899. {
  900. init();
  901. setType(id);
  902. name = "Commando"; //TODO - parse them
  903. }
  904. void CCommanderInstance::init()
  905. {
  906. alive = true;
  907. experience = 0;
  908. level = 1;
  909. count = 1;
  910. type = NULL;
  911. idRand = -1;
  912. _armyObj = NULL;
  913. setNodeType (Bonus::COMMANDER);
  914. secondarySkills.resize (ECommander::SPELL_POWER + 1);
  915. }
  916. CCommanderInstance::~CCommanderInstance()
  917. {
  918. }
  919. void CCommanderInstance::setAlive (bool Alive)
  920. {
  921. //TODO: helm of immortality
  922. alive = Alive;
  923. if (!alive)
  924. {
  925. getBonusList().remove_if (Bonus::UntilCommanderKilled);
  926. }
  927. }
  928. void CCommanderInstance::giveStackExp (expType exp)
  929. {
  930. if (alive)
  931. experience += exp;
  932. }
  933. int CCommanderInstance::getExpRank() const
  934. {
  935. return VLC->heroh->level (experience);
  936. }
  937. void CCommanderInstance::levelUp ()
  938. {
  939. level++;
  940. BOOST_FOREACH (auto bonus, VLC->creh->commanderLevelPremy)
  941. { //grant all regular level-up bonuses
  942. accumulateBonus (*bonus);
  943. }
  944. }
  945. ui8 CCommanderInstance::bearerType() const
  946. {
  947. return ArtBearer::COMMANDER;
  948. }
  949. CStackBasicDescriptor::CStackBasicDescriptor()
  950. {
  951. type = NULL;
  952. count = -1;
  953. }
  954. CStackBasicDescriptor::CStackBasicDescriptor(TCreature id, TQuantity Count)
  955. : type (VLC->creh->creatures[id]), count(Count)
  956. {
  957. }
  958. CStackBasicDescriptor::CStackBasicDescriptor(const CCreature *c, TQuantity Count)
  959. : type(c), count(Count)
  960. {
  961. }
  962. DLL_LINKAGE std::ostream & operator<<(std::ostream & str, const CStackInstance & sth)
  963. {
  964. if(!sth.valid(true))
  965. str << "an invalid stack!";
  966. str << "stack with " << sth.count << " of ";
  967. if(sth.type)
  968. str << sth.type->namePl;
  969. else
  970. str << sth.idRand;
  971. return str;
  972. }
  973. void CSimpleArmy::clear()
  974. {
  975. army.clear();
  976. }
  977. CSimpleArmy::operator bool() const
  978. {
  979. return army.size();
  980. }
  981. bool CSimpleArmy::setCreature(TSlot slot, TCreature cre, TQuantity count)
  982. {
  983. assert(!vstd::contains(army, slot));
  984. army[slot] = CStackBasicDescriptor(cre, count);
  985. return true;
  986. }