CCreatureSet.cpp 25 KB

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