CCreatureSet.cpp 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097
  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. case Bonus::ENCHANTED:
  598. boost::algorithm::replace_first(text, "%s", VLC->spellh->spells[bonus->subtype]->name);
  599. break;
  600. case Bonus::MAGIC_RESISTANCE:
  601. text = ""; //handled separately
  602. break;
  603. //case Bonus::SECONDARY_SKILL_PREMY:
  604. // if (bonus->subtype != CGHeroInstance::RESISTANCE || hasBonusOfType(Bonus::MAGIC_RESISTANCE)) //handle it there
  605. // text = "";
  606. // break;
  607. }
  608. }
  609. return text;
  610. }
  611. else
  612. return "";
  613. }
  614. std::string CStackInstance::bonusToGraphics(Bonus *bonus) const
  615. {
  616. std::string fileName;
  617. switch (bonus->type)
  618. {
  619. //"E_ALIVE.bmp"
  620. //"E_ART.bmp"
  621. case Bonus::ENCHANTED:
  622. fileName = "E_BLESS.bmp"; break;
  623. //"E_BLOCK.bmp"
  624. //"E_BLOCK1.bmp"
  625. //"E_BLOCK2.bmp"
  626. case Bonus::TWO_HEX_ATTACK_BREATH:
  627. fileName = "E_BREATH.bmp"; break;
  628. case Bonus::SPELL_AFTER_ATTACK:
  629. fileName = "E_CAST.bmp"; break;
  630. case Bonus::ENCHANTER:
  631. fileName = "E_CAST1.bmp"; break;
  632. case Bonus::RANDOM_SPELLCASTER:
  633. fileName = "RandomBoost.bmp"; break;
  634. case Bonus::SPELL_BEFORE_ATTACK:
  635. fileName ="E_CAST2.bmp"; break;
  636. case Bonus::SPELLCASTER:
  637. fileName = "E_CASTER.bmp"; break;
  638. case Bonus::JOUSTING:
  639. fileName = "E_CHAMP.bmp"; break;
  640. case Bonus::DOUBLE_DAMAGE_CHANCE:
  641. fileName = "E_DBLOW.bmp"; break;
  642. case Bonus::DEATH_STARE:
  643. fileName = "E_DEATH.bmp"; break;
  644. case Bonus::DEFENSIVE_STANCE:
  645. fileName = "E_DEFBON.bmp"; break;
  646. case Bonus::NO_DISTANCE_PENALTY:
  647. fileName = "E_DIST.bmp"; break;
  648. case Bonus::ADDITIONAL_ATTACK:
  649. fileName = "E_DOUBLE.bmp"; break;
  650. case Bonus::DRAGON_NATURE:
  651. fileName = "E_DRAGON.bmp"; break;
  652. case Bonus::MAGIC_RESISTANCE:
  653. fileName = "E_DWARF.bmp"; break;
  654. case Bonus::SECONDARY_SKILL_PREMY:
  655. if (bonus->subtype == CGHeroInstance::RESISTANCE)
  656. {
  657. fileName = "E_DWARF.bmp";
  658. }
  659. break;
  660. case Bonus::FEAR:
  661. fileName = "E_FEAR.bmp"; break;
  662. case Bonus::FEARLESS:
  663. fileName = "E_FEARL.bmp"; break;
  664. case Bonus::FLYING:
  665. fileName = "E_FLY.bmp"; break;
  666. case Bonus::SPELL_DAMAGE_REDUCTION:
  667. fileName = "E_GOLEM.bmp"; break;
  668. case Bonus::RETURN_AFTER_STRIKE:
  669. fileName = "E_HARPY.bmp"; break;
  670. case Bonus::HATE:
  671. fileName = "E_HATE.bmp"; break;
  672. case Bonus::KING1:
  673. fileName = "E_KING1.bmp"; break;
  674. case Bonus::KING2:
  675. fileName = "E_KING2.bmp"; break;
  676. case Bonus::KING3:
  677. fileName = "E_KING3.bmp"; break;
  678. case Bonus::CHANGES_SPELL_COST_FOR_ALLY:
  679. fileName = "E_MANA.bmp"; break;
  680. case Bonus::CHANGES_SPELL_COST_FOR_ENEMY:
  681. fileName = "MagicDamper.bpm"; break;
  682. case Bonus::NO_MELEE_PENALTY:
  683. fileName = "E_MELEE.bmp"; break;
  684. case Bonus::MIND_IMMUNITY:
  685. fileName = "E_MIND.bmp"; break;
  686. case Bonus::SELF_MORALE:
  687. fileName = "E_MINOT.bmp"; break;
  688. case Bonus::NO_MORALE:
  689. fileName = "E_MORAL.bmp"; break;
  690. case Bonus::RECEPTIVE:
  691. fileName = "E_NOFRIM.bmp"; break;
  692. case Bonus::NO_WALL_PENALTY:
  693. fileName = "E_OBST.bmp"; break;
  694. case Bonus::ENEMY_DEFENCE_REDUCTION:
  695. fileName = "E_RDEF.bmp"; break;
  696. case Bonus::REBIRTH:
  697. fileName = "E_REBIRTH.bmp"; break;
  698. case Bonus::BLOCKS_RETALIATION:
  699. fileName = "E_RETAIL.bmp"; break;
  700. case Bonus::UNLIMITED_RETALIATIONS:
  701. case Bonus::ADDITIONAL_RETALIATION:
  702. fileName = "E_RETAIL1.bmp"; break;
  703. case Bonus::ATTACKS_ALL_ADJACENT:
  704. fileName = "E_ROUND.bmp"; break;
  705. //"E_SGNUM.bmp"
  706. //"E_SGTYPE.bmp"
  707. case Bonus::SHOOTER:
  708. fileName = "E_SHOOT.bmp"; break;
  709. case Bonus::FREE_SHOOTING: //shooter is not blocked by enemy
  710. fileName = "E_SHOOTA.bmp"; break;
  711. //"E_SHOOTN.bmp"
  712. case Bonus::SPELL_IMMUNITY:
  713. {
  714. switch (bonus->subtype)
  715. {
  716. case 62: //Blind
  717. fileName = "E_SPBLIND.bmp"; break;
  718. case 35: // Dispell
  719. fileName = "E_SPDISP.bmp"; break;
  720. case 78: // Dispell beneficial spells
  721. fileName = "E_SPDISB.bmp"; break;
  722. case 60: //Hypnotize
  723. fileName = "E_SPHYPN.bmp"; break;
  724. case 18: //Implosion
  725. fileName = "E_SPIMP.bmp"; break;
  726. case 59: //Berserk
  727. fileName = "E_SPBERS.bmp"; break;
  728. case 23: //Meteor Shower
  729. fileName = "E_SPMET.bmp"; break;
  730. case 26: //Armageddon
  731. fileName = "E_SPARM.bmp"; break;
  732. case 54: //Slow
  733. fileName = "E_SPSLOW.bmp"; break;
  734. //TODO: some generic spell handling?
  735. }
  736. break;
  737. }
  738. //"E_SPAWILL.bmp"
  739. case Bonus::DIRECT_DAMAGE_IMMUNITY:
  740. fileName = "E_SPDIR.bmp"; break;
  741. //"E_SPDISB.bmp"
  742. //"E_SPDISP.bmp"
  743. //"E_SPEATH.bmp"
  744. //"E_SPEATH1.bmp"
  745. case Bonus::FIRE_IMMUNITY:
  746. switch (bonus->subtype)
  747. {
  748. case 0:
  749. fileName = "E_SPFIRE.bmp"; break; //all
  750. case 1:
  751. fileName = "E_SPFIRE1.bmp"; break; //not positive
  752. case 2:
  753. fileName = "E_FIRE.bmp"; break; //direct damage
  754. }
  755. break;
  756. case Bonus::WATER_IMMUNITY:
  757. switch (bonus->subtype)
  758. {
  759. case 0:
  760. fileName = "E_SPWATER.bmp"; break; //all
  761. case 1:
  762. fileName = "E_SPWATER1.bmp"; break; //not positive
  763. case 2:
  764. fileName = "E_SPCOLD.bmp"; break; //direct damage
  765. }
  766. break;
  767. case Bonus::AIR_IMMUNITY:
  768. switch (bonus->subtype)
  769. {
  770. case 0:
  771. fileName = "E_SPAIR.bmp"; break; //all
  772. case 1:
  773. fileName = "E_SPAIR1.bmp"; break; //not positive
  774. case 2:
  775. fileName = "E_LIGHT.bmp"; break;//direct damage
  776. }
  777. break;
  778. case Bonus::EARTH_IMMUNITY:
  779. switch (bonus->subtype)
  780. {
  781. case 0:
  782. fileName = "E_SPEATH.bmp"; break; //all
  783. case 1:
  784. case 2: //no specific icon for direct damage immunity
  785. fileName = "E_SPEATH1.bmp"; break; //not positive
  786. }
  787. break;
  788. case Bonus::LEVEL_SPELL_IMMUNITY:
  789. {
  790. if (vstd::iswithin(bonus->val, 1 , 5))
  791. {
  792. fileName = "E_SPLVL" + boost::lexical_cast<std::string>(bonus->val) + ".bmp";
  793. }
  794. break;
  795. }
  796. //"E_SUMMON.bmp"
  797. //"E_SUMMON1.bmp"
  798. //"E_SUMMON2.bmp"
  799. case Bonus::FULL_HP_REGENERATION:
  800. case Bonus::HP_REGENERATION:
  801. fileName = "E_TROLL.bmp"; break;
  802. case Bonus::UNDEAD:
  803. fileName = "E_UNDEAD.bmp"; break;
  804. case Bonus::SPELL_RESISTANCE_AURA:
  805. fileName = "E_UNIC.bmp"; break;
  806. case Bonus::THREE_HEADED_ATTACK:
  807. fileName = "ThreeHeaded.bmp"; break;
  808. case Bonus::DAEMON_SUMMONING:
  809. fileName = "RiseDemons.bmp"; break;
  810. case Bonus::CHARGE_IMMUNITY:
  811. fileName = "ChargeImmune.bmp"; break;
  812. case Bonus::HEALER:
  813. fileName = "Healer.bmp"; break;
  814. case Bonus::CATAPULT:
  815. fileName = "Catapult.bmp"; break;
  816. case Bonus::MANA_CHANNELING:
  817. fileName = "ManaChannel.bmp"; break;
  818. case Bonus::MANA_DRAIN:
  819. fileName = "ManaDrain.bmp"; break;
  820. }
  821. if(!fileName.empty())
  822. fileName = "zvs/Lib1.res/" + fileName;
  823. return fileName;
  824. }
  825. void CStackInstance::setArmyObj(const CArmedInstance *ArmyObj)
  826. {
  827. if(_armyObj)
  828. detachFrom(const_cast<CArmedInstance*>(_armyObj));
  829. _armyObj = ArmyObj;
  830. if(ArmyObj)
  831. {
  832. attachTo(const_cast<CArmedInstance*>(_armyObj));
  833. }
  834. }
  835. std::string CStackInstance::getQuantityTXT(bool capitalized /*= true*/) const
  836. {
  837. int quantity = getQuantityID();
  838. if (quantity)
  839. return VLC->generaltexth->arraytxt[174 + quantity*3 - 1 - capitalized];
  840. else
  841. return "";
  842. }
  843. bool CStackInstance::valid(bool allowUnrandomized) const
  844. {
  845. bool isRand = (idRand != -1);
  846. if(!isRand)
  847. {
  848. return (type && type == VLC->creh->creatures[type->idNumber]);
  849. }
  850. else
  851. return allowUnrandomized;
  852. }
  853. CStackInstance::~CStackInstance()
  854. {
  855. }
  856. std::string CStackInstance::nodeName() const
  857. {
  858. std::ostringstream oss;
  859. oss << "Stack of " << count << " of ";
  860. if(type)
  861. oss << type->namePl;
  862. else if(idRand)
  863. oss << "[no type, idRand=" << idRand << "]";
  864. else
  865. oss << "[UNDEFINED TYPE]";
  866. return oss.str();
  867. }
  868. void CStackInstance::deserializationFix()
  869. {
  870. const CCreature *backup = type;
  871. type = NULL;
  872. setType(backup);
  873. const CArmedInstance *armyBackup = _armyObj;
  874. _armyObj = NULL;
  875. setArmyObj(armyBackup);
  876. artDeserializationFix(this);
  877. }
  878. int CStackInstance::getCreatureID() const
  879. {
  880. if(type)
  881. return type->idNumber;
  882. else
  883. return -1;
  884. }
  885. std::string CStackInstance::getName() const
  886. {
  887. return (count > 1) ? type->namePl : type->nameSing;
  888. }
  889. ui64 CStackInstance::getPower() const
  890. {
  891. assert(type);
  892. return type->AIValue * count;
  893. }
  894. ui8 CStackInstance::bearerType() const
  895. {
  896. return ArtBearer::CREATURE;
  897. }
  898. CCommanderInstance::CCommanderInstance()
  899. {
  900. init();
  901. name = "Unnamed";
  902. }
  903. CCommanderInstance::CCommanderInstance (TCreature id)
  904. {
  905. init();
  906. setType(id);
  907. name = "Commando"; //TODO - parse them
  908. }
  909. void CCommanderInstance::init()
  910. {
  911. alive = true;
  912. experience = 0;
  913. level = 1;
  914. count = 1;
  915. type = NULL;
  916. idRand = -1;
  917. _armyObj = NULL;
  918. setNodeType (Bonus::COMMANDER);
  919. secondarySkills.resize (ECommander::SPELL_POWER + 1);
  920. }
  921. CCommanderInstance::~CCommanderInstance()
  922. {
  923. }
  924. void CCommanderInstance::setAlive (bool Alive)
  925. {
  926. //TODO: helm of immortality
  927. alive = Alive;
  928. if (!alive)
  929. {
  930. getBonusList().remove_if (Bonus::UntilCommanderKilled);
  931. }
  932. }
  933. void CCommanderInstance::giveStackExp (TExpType exp)
  934. {
  935. if (alive)
  936. experience += exp;
  937. }
  938. int CCommanderInstance::getExpRank() const
  939. {
  940. return VLC->heroh->level (experience);
  941. }
  942. void CCommanderInstance::levelUp ()
  943. {
  944. level++;
  945. BOOST_FOREACH (auto bonus, VLC->creh->commanderLevelPremy)
  946. { //grant all regular level-up bonuses
  947. accumulateBonus (*bonus);
  948. }
  949. }
  950. ui8 CCommanderInstance::bearerType() const
  951. {
  952. return ArtBearer::COMMANDER;
  953. }
  954. CStackBasicDescriptor::CStackBasicDescriptor()
  955. {
  956. type = NULL;
  957. count = -1;
  958. }
  959. CStackBasicDescriptor::CStackBasicDescriptor(TCreature id, TQuantity Count)
  960. : type (VLC->creh->creatures[id]), count(Count)
  961. {
  962. }
  963. CStackBasicDescriptor::CStackBasicDescriptor(const CCreature *c, TQuantity Count)
  964. : type(c), count(Count)
  965. {
  966. }
  967. DLL_LINKAGE std::ostream & operator<<(std::ostream & str, const CStackInstance & sth)
  968. {
  969. if(!sth.valid(true))
  970. str << "an invalid stack!";
  971. str << "stack with " << sth.count << " of ";
  972. if(sth.type)
  973. str << sth.type->namePl;
  974. else
  975. str << sth.idRand;
  976. return str;
  977. }
  978. void CSimpleArmy::clear()
  979. {
  980. army.clear();
  981. }
  982. CSimpleArmy::operator bool() const
  983. {
  984. return army.size();
  985. }
  986. bool CSimpleArmy::setCreature(TSlot slot, TCreature cre, TQuantity count)
  987. {
  988. assert(!vstd::contains(army, slot));
  989. army[slot] = CStackBasicDescriptor(cre, count);
  990. return true;
  991. }