HeroBonus.cpp 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067
  1. #define VCMI_DLL
  2. #include "HeroBonus.h"
  3. #include <boost/foreach.hpp>
  4. #include "VCMI_Lib.h"
  5. #include "CSpellHandler.h"
  6. #include <sstream>
  7. #include "CCreatureHandler.h"
  8. #include <boost/assign/list_of.hpp>
  9. #include "CCreatureSet.h"
  10. #include <boost/algorithm/string/trim.hpp>
  11. #include <boost/bind.hpp>
  12. #include "CHeroHandler.h"
  13. #include "CGeneralTextHandler.h"
  14. #include "BattleState.h"
  15. #include "CArtHandler.h"
  16. #define FOREACH_CONST_PARENT(pname) TCNodes lparents; getParents(lparents); BOOST_FOREACH(const CBonusSystemNode *pname, lparents)
  17. #define FOREACH_PARENT(pname) TNodes lparents; getParents(lparents); BOOST_FOREACH(CBonusSystemNode *pname, lparents)
  18. #define FOREACH_RED_CHILD(pname) TNodes lchildren; getRedChildren(lchildren); BOOST_FOREACH(CBonusSystemNode *pname, lchildren)
  19. #define FOREACH_RED_PARENT(pname) TNodes lparents; getRedParents(lparents); BOOST_FOREACH(CBonusSystemNode *pname, lparents)
  20. #define BONUS_NAME(x) ( #x, Bonus::x )
  21. DLL_EXPORT const std::map<std::string, int> bonusNameMap = boost::assign::map_list_of BONUS_LIST;
  22. #undef BONUS_NAME
  23. #define BONUS_LOG_LINE(x) tlog5 << x << std::endl
  24. int DLL_EXPORT BonusList::totalValue() const
  25. {
  26. int base = 0;
  27. int percentToBase = 0;
  28. int percentToAll = 0;
  29. int additive = 0;
  30. int indepMax = 0;
  31. bool hasIndepMax = false;
  32. int indepMin = 0;
  33. bool hasIndepMin = false;
  34. BOOST_FOREACH(Bonus *i, *this)
  35. {
  36. switch(i->valType)
  37. {
  38. case Bonus::BASE_NUMBER:
  39. base += i->val;
  40. break;
  41. case Bonus::PERCENT_TO_ALL:
  42. percentToAll += i->val;
  43. break;
  44. case Bonus::PERCENT_TO_BASE:
  45. percentToBase += i->val;
  46. break;
  47. case Bonus::ADDITIVE_VALUE:
  48. additive += i->val;
  49. break;
  50. case Bonus::INDEPENDENT_MAX:
  51. if (!hasIndepMax)
  52. {
  53. indepMax = i->val;
  54. hasIndepMax = true;
  55. }
  56. else
  57. {
  58. amax(indepMax, i->val);
  59. }
  60. break;
  61. case Bonus::INDEPENDENT_MIN:
  62. if (!hasIndepMin)
  63. {
  64. indepMin = i->val;
  65. hasIndepMin = true;
  66. }
  67. else
  68. {
  69. amin(indepMin, i->val);
  70. }
  71. break;
  72. }
  73. }
  74. int modifiedBase = base + (base * percentToBase) / 100;
  75. modifiedBase += additive;
  76. int valFirst = (modifiedBase * (100 + percentToAll)) / 100;
  77. if(hasIndepMin && hasIndepMax)
  78. assert(indepMin < indepMax);
  79. if (hasIndepMax)
  80. amax(valFirst, indepMax);
  81. if (hasIndepMin)
  82. amin(valFirst, indepMin);
  83. return valFirst;
  84. }
  85. const DLL_EXPORT Bonus * BonusList::getFirst(const CSelector &selector) const
  86. {
  87. BOOST_FOREACH(Bonus *i, *this)
  88. if(selector(i))
  89. return &*i;
  90. return NULL;
  91. }
  92. DLL_EXPORT Bonus * BonusList::getFirst(const CSelector &select)
  93. {
  94. BOOST_FOREACH(Bonus *i, *this)
  95. if(select(i))
  96. return &*i;
  97. return NULL;
  98. }
  99. void DLL_EXPORT BonusList::getModifiersWDescr(TModDescr &out) const
  100. {
  101. BOOST_FOREACH(Bonus *i, *this)
  102. out.push_back(std::make_pair(i->val, i->Description()));
  103. }
  104. void DLL_EXPORT BonusList::getBonuses(BonusList &out, const CSelector &selector) const
  105. {
  106. // BOOST_FOREACH(Bonus *i, *this)
  107. // if(selector(i) && i->effectRange == Bonus::NO_LIMIT)
  108. // out.push_back(i);
  109. getBonuses(out, selector, 0);
  110. }
  111. void DLL_EXPORT BonusList::getBonuses(BonusList &out, const CSelector &selector, const CSelector &limit) const
  112. {
  113. BOOST_FOREACH(Bonus *i, *this)
  114. if(selector(i) && ((!limit && i->effectRange == Bonus::NO_LIMIT) || (limit && limit(i)))) //add matching bonuses that matches limit predicate or have NO_LIMIT if no given predicate
  115. out.push_back(i);
  116. }
  117. void BonusList::limit(const CBonusSystemNode &node)
  118. {
  119. remove_if(boost::bind(&CBonusSystemNode::isLimitedOnUs, boost::ref(node), _1));
  120. }
  121. void DLL_EXPORT BonusList::eliminateDuplicates()
  122. {
  123. sort();
  124. unique();
  125. }
  126. int IBonusBearer::valOfBonuses(Bonus::BonusType type, const CSelector &selector) const
  127. {
  128. return valOfBonuses(Selector::type(type) && selector);
  129. }
  130. int IBonusBearer::valOfBonuses(Bonus::BonusType type, int subtype /*= -1*/) const
  131. {
  132. CSelector s = Selector::type(type);
  133. if(subtype != -1)
  134. s = s && Selector::subtype(subtype);
  135. return valOfBonuses(s);
  136. }
  137. int IBonusBearer::valOfBonuses(const CSelector &selector) const
  138. {
  139. BonusList hlp;
  140. getBonuses(hlp, selector);
  141. return hlp.totalValue();
  142. }
  143. bool IBonusBearer::hasBonus(const CSelector &selector) const
  144. {
  145. return getBonuses(selector).size() > 0;
  146. }
  147. bool IBonusBearer::hasBonusOfType(Bonus::BonusType type, int subtype /*= -1*/) const
  148. {
  149. CSelector s = Selector::type(type);
  150. if(subtype != -1)
  151. s = s && Selector::subtype(subtype);
  152. return hasBonus(s);
  153. }
  154. void IBonusBearer::getModifiersWDescr(TModDescr &out, Bonus::BonusType type, int subtype /*= -1 */) const
  155. {
  156. getModifiersWDescr(out, subtype != -1 ? Selector::typeSubtype(type, subtype) : Selector::type(type));
  157. }
  158. void IBonusBearer::getModifiersWDescr(TModDescr &out, const CSelector &selector) const
  159. {
  160. getBonuses(selector).getModifiersWDescr(out);
  161. }
  162. int IBonusBearer::getBonusesCount(int from, int id) const
  163. {
  164. return getBonusesCount(Selector::source(from, id));
  165. }
  166. int IBonusBearer::getBonusesCount(const CSelector &selector) const
  167. {
  168. return getBonuses(selector).size();
  169. }
  170. void IBonusBearer::getBonuses(BonusList &out, const CSelector &selector, const CBonusSystemNode *root /*= NULL*/) const
  171. {
  172. getBonuses(out, selector, 0, root);
  173. // FOREACH_CONST_PARENT(p)
  174. // p->getBonuses(out, selector, root ? root : this);
  175. //
  176. // bonuses.getBonuses(out, selector);
  177. //
  178. // if(!root)
  179. // out.limit(*this);
  180. }
  181. BonusList IBonusBearer::getBonuses(const CSelector &selector) const
  182. {
  183. BonusList ret;
  184. getBonuses(ret, selector);
  185. return ret;
  186. }
  187. void IBonusBearer::getBonuses(BonusList &out, const CSelector &selector, const CSelector &limit, const CBonusSystemNode *root /*= NULL*/) const
  188. {
  189. getAllBonuses(out, selector, limit, root);
  190. out.eliminateDuplicates();
  191. //
  192. // getBonuses(out, selector); //first get all the bonuses
  193. // out.remove_if(std::not1(limit)); //now remove the ones we don't like
  194. // out.limit(*this); //apply bonuses' limiters
  195. }
  196. BonusList IBonusBearer::getBonuses(const CSelector &selector, const CSelector &limit) const
  197. {
  198. BonusList ret;
  199. getBonuses(ret, selector, limit);
  200. return ret;
  201. }
  202. bool IBonusBearer::hasBonusFrom(ui8 source, ui32 sourceID) const
  203. {
  204. return hasBonus(Selector::source(source,sourceID));
  205. }
  206. int IBonusBearer::MoraleVal() const
  207. {
  208. if(hasBonusOfType(Bonus::NON_LIVING) || hasBonusOfType(Bonus::UNDEAD) ||
  209. hasBonusOfType(Bonus::NO_MORALE) || hasBonusOfType(Bonus::SIEGE_WEAPON))
  210. return 0;
  211. int ret = valOfBonuses(Selector::type(Bonus::MORALE));
  212. if(hasBonusOfType(Bonus::SELF_MORALE)) //eg. minotaur
  213. amax(ret, +1);
  214. return abetw(ret, -3, +3);
  215. }
  216. int IBonusBearer::LuckVal() const
  217. {
  218. if(hasBonusOfType(Bonus::NO_LUCK))
  219. return 0;
  220. int ret = valOfBonuses(Selector::type(Bonus::LUCK));
  221. if(hasBonusOfType(Bonus::SELF_LUCK)) //eg. halfling
  222. amax(ret, +1);
  223. return abetw(ret, -3, +3);
  224. }
  225. si32 IBonusBearer::Attack() const
  226. {
  227. si32 ret = valOfBonuses(Bonus::PRIMARY_SKILL, PrimarySkill::ATTACK);
  228. if(int frenzyPower = valOfBonuses(Bonus::IN_FRENZY)) //frenzy for attacker
  229. {
  230. ret += frenzyPower * Defense(false);
  231. }
  232. amax(ret, 0);
  233. return ret;
  234. }
  235. si32 IBonusBearer::Defense(bool withFrenzy /*= true*/) const
  236. {
  237. si32 ret = valOfBonuses(Bonus::PRIMARY_SKILL, PrimarySkill::DEFENSE);
  238. if(withFrenzy && hasBonusOfType(Bonus::IN_FRENZY)) //frenzy for defender
  239. {
  240. return 0;
  241. }
  242. amax(ret, 0);
  243. return ret;
  244. }
  245. ui16 IBonusBearer::MaxHealth() const
  246. {
  247. return std::max(1, valOfBonuses(Bonus::STACK_HEALTH)); //never 0 or negative
  248. }
  249. ui32 IBonusBearer::getMinDamage() const
  250. {
  251. return valOfBonuses(Selector::typeSubtype(Bonus::CREATURE_DAMAGE, 0) || Selector::typeSubtype(Bonus::CREATURE_DAMAGE, 1));
  252. }
  253. ui32 IBonusBearer::getMaxDamage() const
  254. {
  255. return valOfBonuses(Selector::typeSubtype(Bonus::CREATURE_DAMAGE, 0) || Selector::typeSubtype(Bonus::CREATURE_DAMAGE, 2));
  256. }
  257. si32 IBonusBearer::manaLimit() const
  258. {
  259. return si32(getPrimSkillLevel(3) * (100.0f + valOfBonuses(Bonus::SECONDARY_SKILL_PREMY, 24)) / 10.0f);
  260. }
  261. int IBonusBearer::getPrimSkillLevel(int id) const
  262. {
  263. int ret = 0;
  264. if(id == PrimarySkill::ATTACK)
  265. ret = Attack();
  266. else if(id == PrimarySkill::DEFENSE)
  267. ret = Defense();
  268. else
  269. ret = valOfBonuses(Bonus::PRIMARY_SKILL, id);
  270. amax(ret, id/2); //minimal value is 0 for attack and defense and 1 for spell power and knowledge
  271. return ret;
  272. }
  273. si32 IBonusBearer::magicResistance() const
  274. {
  275. return valOfBonuses(Selector::type(Bonus::MAGIC_RESISTANCE));
  276. }
  277. Bonus * CBonusSystemNode::getBonus(const CSelector &selector)
  278. {
  279. Bonus *ret = bonuses.getFirst(selector);
  280. if(ret)
  281. return ret;
  282. FOREACH_PARENT(pname)
  283. {
  284. ret = pname->getBonus(selector);
  285. if (ret)
  286. return ret;
  287. }
  288. return NULL;
  289. }
  290. const Bonus * CBonusSystemNode::getBonus( const CSelector &selector ) const
  291. {
  292. return (const_cast<CBonusSystemNode*>(this))->getBonus(selector);
  293. }
  294. void CBonusSystemNode::getParents(TCNodes &out) const /*retreives list of parent nodes (nodes to inherit bonuses from) */
  295. {
  296. BOOST_FOREACH(const CBonusSystemNode *parent, parents)
  297. out.insert(parent);
  298. }
  299. void CBonusSystemNode::getParents(TNodes &out)
  300. {
  301. BOOST_FOREACH(const CBonusSystemNode *parent, parents)
  302. out.insert(const_cast<CBonusSystemNode*>(parent));
  303. }
  304. void CBonusSystemNode::getAllBonuses(BonusList &out, const CSelector &selector, const CSelector &limit, const CBonusSystemNode *root /*= NULL*/) const
  305. {
  306. FOREACH_CONST_PARENT(p)
  307. p->getBonuses(out, selector, limit, root ? root : this);
  308. bonuses.getBonuses(out, selector, limit);
  309. if(!root)
  310. out.limit(*this);
  311. }
  312. CBonusSystemNode::CBonusSystemNode()
  313. {
  314. nodeType = UNKNOWN;
  315. }
  316. CBonusSystemNode::~CBonusSystemNode()
  317. {
  318. detachFromAll();
  319. if(children.size())
  320. {
  321. tlog2 << "Warning: an orphaned child!\n";
  322. while(children.size())
  323. children.front()->detachFrom(this);
  324. }
  325. BOOST_FOREACH(Bonus *b, exportedBonuses)
  326. delete b;
  327. }
  328. void CBonusSystemNode::attachTo(CBonusSystemNode *parent)
  329. {
  330. assert(!vstd::contains(parents, parent));
  331. parents.push_back(parent);
  332. if(parent->actsAsBonusSourceOnly())
  333. parent->newRedDescendant(this);
  334. else
  335. newRedDescendant(parent);
  336. parent->newChildAttached(this);
  337. }
  338. void CBonusSystemNode::detachFrom(CBonusSystemNode *parent)
  339. {
  340. assert(vstd::contains(parents, parent));
  341. if(parent->actsAsBonusSourceOnly())
  342. parent->removedRedDescendant(this);
  343. else
  344. removedRedDescendant(parent);
  345. parents -= parent;
  346. parent->childDetached(this);
  347. }
  348. void CBonusSystemNode::popBonuses(const CSelector &s)
  349. {
  350. BonusList bl;
  351. exportedBonuses.getBonuses(bl, s);
  352. BOOST_FOREACH(Bonus *b, bl)
  353. removeBonus(b);
  354. BOOST_FOREACH(CBonusSystemNode *child, children)
  355. child->popBonuses(s);
  356. }
  357. // void CBonusSystemNode::addNewBonus(const Bonus &b)
  358. // {
  359. // addNewBonus(new Bonus(b));
  360. // }
  361. void CBonusSystemNode::addNewBonus(Bonus *b)
  362. {
  363. assert(!vstd::contains(exportedBonuses,b));
  364. exportedBonuses.push_back(b);
  365. exportBonus(b);
  366. }
  367. void CBonusSystemNode::removeBonus(Bonus *b)
  368. {
  369. exportedBonuses -= b;
  370. if(b->propagator)
  371. unpropagateBonus(b);
  372. else
  373. bonuses -= b;
  374. delNull(b);
  375. }
  376. bool CBonusSystemNode::isLimitedOnUs(Bonus *b) const
  377. {
  378. return b->limiter && b->limiter->limit(b, *this);
  379. }
  380. bool CBonusSystemNode::actsAsBonusSourceOnly() const
  381. {
  382. switch(nodeType)
  383. {
  384. case CREATURE:
  385. case ARTIFACT:
  386. case ARTIFACT_INSTANCE:
  387. return true;
  388. default:
  389. return false;
  390. }
  391. }
  392. void CBonusSystemNode::propagateBonus(Bonus * b)
  393. {
  394. if(b->propagator->shouldBeAttached(this))
  395. {
  396. bonuses.push_back(b);
  397. BONUS_LOG_LINE("#$# " << b->Description() << " #propagated to# " << nodeName());
  398. }
  399. FOREACH_RED_CHILD(child)
  400. child->propagateBonus(b);
  401. }
  402. void CBonusSystemNode::unpropagateBonus(Bonus * b)
  403. {
  404. if(b->propagator->shouldBeAttached(this))
  405. {
  406. bonuses -= b;
  407. BONUS_LOG_LINE("#$#" << b->Description() << " #is no longer propagated to# " << nodeName());
  408. }
  409. FOREACH_RED_CHILD(child)
  410. child->unpropagateBonus(b);
  411. }
  412. void CBonusSystemNode::newChildAttached(CBonusSystemNode *child)
  413. {
  414. assert(!vstd::contains(children, child));
  415. children.push_back(child);
  416. BONUS_LOG_LINE(child->nodeName() << " #attached to# " << nodeName());
  417. }
  418. void CBonusSystemNode::childDetached(CBonusSystemNode *child)
  419. {
  420. assert(vstd::contains(children, child));
  421. children -= child;
  422. BONUS_LOG_LINE(child->nodeName() << " #detached from# " << nodeName());
  423. }
  424. void CBonusSystemNode::detachFromAll()
  425. {
  426. while(parents.size())
  427. detachFrom(parents.front());
  428. }
  429. bool CBonusSystemNode::isIndependentNode() const
  430. {
  431. return parents.empty() && children.empty();
  432. }
  433. std::string CBonusSystemNode::nodeName() const
  434. {
  435. return description.size()
  436. ? description
  437. : std::string("Bonus system node of type ") + typeid(*this).name();
  438. }
  439. void CBonusSystemNode::deserializationFix()
  440. {
  441. exportBonuses();
  442. }
  443. void CBonusSystemNode::getRedParents(TNodes &out)
  444. {
  445. FOREACH_PARENT(pname)
  446. {
  447. if(pname->actsAsBonusSourceOnly())
  448. {
  449. out.insert(pname);
  450. }
  451. }
  452. if(!actsAsBonusSourceOnly())
  453. {
  454. BOOST_FOREACH(CBonusSystemNode *child, children)
  455. {
  456. out.insert(child);
  457. }
  458. }
  459. }
  460. void CBonusSystemNode::getRedChildren(TNodes &out)
  461. {
  462. FOREACH_PARENT(pname)
  463. {
  464. if(!pname->actsAsBonusSourceOnly())
  465. {
  466. out.insert(pname);
  467. }
  468. }
  469. if(actsAsBonusSourceOnly())
  470. {
  471. BOOST_FOREACH(CBonusSystemNode *child, children)
  472. {
  473. out.insert(child);
  474. }
  475. }
  476. }
  477. void CBonusSystemNode::newRedDescendant(CBonusSystemNode *descendant)
  478. {
  479. BOOST_FOREACH(Bonus *b, exportedBonuses)
  480. if(b->propagator)
  481. descendant->propagateBonus(b);
  482. FOREACH_RED_PARENT(parent)
  483. parent->newRedDescendant(descendant);
  484. }
  485. void CBonusSystemNode::removedRedDescendant(CBonusSystemNode *descendant)
  486. {
  487. BOOST_FOREACH(Bonus *b, exportedBonuses)
  488. if(b->propagator)
  489. descendant->unpropagateBonus(b);
  490. FOREACH_RED_PARENT(parent)
  491. parent->removedRedDescendant(descendant);
  492. }
  493. void CBonusSystemNode::getRedAncestors(TNodes &out)
  494. {
  495. getRedParents(out);
  496. FOREACH_RED_PARENT(p)
  497. p->getRedAncestors(out);
  498. }
  499. void CBonusSystemNode::getRedDescendants(TNodes &out)
  500. {
  501. getRedChildren(out);
  502. FOREACH_RED_CHILD(c)
  503. c->getRedChildren(out);
  504. }
  505. void CBonusSystemNode::battleTurnPassed()
  506. {
  507. BonusList bonusesCpy = exportedBonuses; //copy, because removing bonuses invalidates iters
  508. BOOST_FOREACH(Bonus *b, bonusesCpy)
  509. {
  510. if(b->duration & Bonus::N_TURNS)
  511. {
  512. b->turnsRemain--;
  513. if(b->turnsRemain <= 0)
  514. removeBonus(b);
  515. }
  516. }
  517. }
  518. void CBonusSystemNode::exportBonus(Bonus * b)
  519. {
  520. if(b->propagator)
  521. propagateBonus(b);
  522. else
  523. bonuses.push_back(b);
  524. }
  525. void CBonusSystemNode::exportBonuses()
  526. {
  527. BOOST_FOREACH(Bonus *b, exportedBonuses)
  528. exportBonus(b);
  529. }
  530. int NBonus::valOf(const CBonusSystemNode *obj, Bonus::BonusType type, int subtype /*= -1*/)
  531. {
  532. if(obj)
  533. return obj->valOfBonuses(type, subtype);
  534. return 0;
  535. }
  536. bool NBonus::hasOfType(const CBonusSystemNode *obj, Bonus::BonusType type, int subtype /*= -1*/)
  537. {
  538. if(obj)
  539. return obj->hasBonusOfType(type, subtype);
  540. return false;
  541. }
  542. void NBonus::getModifiersWDescr(const CBonusSystemNode *obj, TModDescr &out, Bonus::BonusType type, int subtype /*= -1 */)
  543. {
  544. if(obj)
  545. return obj->getModifiersWDescr(out, type, subtype);
  546. }
  547. int NBonus::getCount(const CBonusSystemNode *obj, int from, int id)
  548. {
  549. if(obj)
  550. return obj->getBonusesCount(from, id);
  551. return 0;
  552. }
  553. const CSpell * Bonus::sourceSpell() const
  554. {
  555. if(source == SPELL_EFFECT)
  556. return VLC->spellh->spells[sid];
  557. return NULL;
  558. }
  559. std::string Bonus::Description() const
  560. {
  561. if(description.size())
  562. return description;
  563. std::ostringstream str;
  564. str << std::showpos << val << " ";
  565. switch(source)
  566. {
  567. case ARTIFACT:
  568. str << VLC->arth->artifacts[sid]->Name();
  569. break;;
  570. case SPELL_EFFECT:
  571. str << VLC->spellh->spells[sid]->name;
  572. break;
  573. case CREATURE_ABILITY:
  574. str << VLC->creh->creatures[sid]->namePl;
  575. break;
  576. case SECONDARY_SKILL:
  577. str << VLC->generaltexth->skillName[sid]/* << " secondary skill"*/;
  578. break;
  579. }
  580. return str.str();
  581. }
  582. Bonus::Bonus(ui16 Dur, ui8 Type, ui8 Src, si32 Val, ui32 ID, std::string Desc, si32 Subtype/*=-1*/)
  583. : duration(Dur), type(Type), subtype(Subtype), source(Src), val(Val), sid(ID), description(Desc)
  584. {
  585. additionalInfo = -1;
  586. turnsRemain = 0;
  587. valType = ADDITIVE_VALUE;
  588. effectRange = NO_LIMIT;
  589. boost::algorithm::trim(description);
  590. }
  591. Bonus::Bonus(ui16 Dur, ui8 Type, ui8 Src, si32 Val, ui32 ID, si32 Subtype/*=-1*/, ui8 ValType /*= ADDITIVE_VALUE*/)
  592. : duration(Dur), type(Type), subtype(Subtype), source(Src), val(Val), sid(ID), valType(ValType)
  593. {
  594. additionalInfo = -1;
  595. turnsRemain = 0;
  596. effectRange = NO_LIMIT;
  597. }
  598. Bonus::Bonus()
  599. {
  600. subtype = -1;
  601. additionalInfo = -1;
  602. turnsRemain = 0;
  603. valType = ADDITIVE_VALUE;
  604. effectRange = NO_LIMIT;
  605. }
  606. Bonus::~Bonus()
  607. {
  608. }
  609. Bonus * Bonus::addLimiter(ILimiter *Limiter)
  610. {
  611. return addLimiter(boost::shared_ptr<ILimiter>(Limiter));
  612. }
  613. Bonus * Bonus::addLimiter(boost::shared_ptr<ILimiter> Limiter)
  614. {
  615. limiter = Limiter;
  616. return this;
  617. }
  618. Bonus * Bonus::addPropagator(IPropagator *Propagator)
  619. {
  620. return addPropagator(boost::shared_ptr<IPropagator>(Propagator));
  621. }
  622. Bonus * Bonus::addPropagator(boost::shared_ptr<IPropagator> Propagator)
  623. {
  624. propagator = Propagator;
  625. return this;
  626. }
  627. CSelector DLL_EXPORT operator&&(const CSelector &first, const CSelector &second)
  628. {
  629. return CSelectorsConjunction(first, second);
  630. }
  631. CSelector DLL_EXPORT operator||(const CSelector &first, const CSelector &second)
  632. {
  633. return CSelectorsAlternative(first, second);
  634. }
  635. namespace Selector
  636. {
  637. DLL_EXPORT CSelectFieldEqual<TBonusType> type(&Bonus::type, 0);
  638. DLL_EXPORT CSelectFieldEqual<TBonusSubtype> subtype(&Bonus::subtype, 0);
  639. DLL_EXPORT CSelectFieldEqual<si32> info(&Bonus::additionalInfo, 0);
  640. DLL_EXPORT CSelectFieldEqual<ui16> duration(&Bonus::duration, 0);
  641. DLL_EXPORT CSelectFieldEqual<ui8> sourceType(&Bonus::source, 0);
  642. DLL_EXPORT CSelectFieldEqual<ui8> effectRange(&Bonus::effectRange, Bonus::NO_LIMIT);
  643. DLL_EXPORT CWillLastTurns turns;
  644. CSelector DLL_EXPORT typeSubtype(TBonusType Type, TBonusSubtype Subtype)
  645. {
  646. return type(Type) && subtype(Subtype);
  647. }
  648. CSelector DLL_EXPORT typeSubtypeInfo(TBonusType type, TBonusSubtype subtype, si32 info)
  649. {
  650. return CSelectFieldEqual<TBonusType>(&Bonus::type, type) && CSelectFieldEqual<TBonusSubtype>(&Bonus::subtype, subtype) && CSelectFieldEqual<si32>(&Bonus::additionalInfo, info);
  651. }
  652. CSelector DLL_EXPORT source(ui8 source, ui32 sourceID)
  653. {
  654. return CSelectFieldEqual<ui8>(&Bonus::source, source) && CSelectFieldEqual<ui32>(&Bonus::sid, sourceID);
  655. }
  656. CSelector DLL_EXPORT durationType(ui16 duration)
  657. {
  658. return CSelectFieldEqual<ui16>(&Bonus::duration, duration);
  659. }
  660. CSelector DLL_EXPORT sourceTypeSel(ui8 source)
  661. {
  662. return CSelectFieldEqual<ui8>(&Bonus::source, source);
  663. }
  664. bool DLL_EXPORT matchesType(const CSelector &sel, TBonusType type)
  665. {
  666. Bonus dummy;
  667. dummy.type = type;
  668. return sel(&dummy);
  669. }
  670. bool DLL_EXPORT matchesTypeSubtype(const CSelector &sel, TBonusType type, TBonusSubtype subtype)
  671. {
  672. Bonus dummy;
  673. dummy.type = type;
  674. dummy.subtype = subtype;
  675. return sel(&dummy);
  676. }
  677. bool DLL_EXPORT positiveSpellEffects(const Bonus *b)
  678. {
  679. if(b->source == Bonus::SPELL_EFFECT)
  680. {
  681. CSpell *sp = VLC->spellh->spells[b->sid];
  682. return sp->positiveness == 1;
  683. }
  684. return false; //not a spell effect
  685. }
  686. }
  687. const CStack * retreiveStackBattle(const CBonusSystemNode *node)
  688. {
  689. switch(node->nodeType)
  690. {
  691. case CBonusSystemNode::STACK_BATTLE:
  692. return static_cast<const CStack*>(node);
  693. default:
  694. return NULL;
  695. }
  696. }
  697. const CStackInstance * retreiveStackInstance(const CBonusSystemNode *node)
  698. {
  699. switch(node->nodeType)
  700. {
  701. case CBonusSystemNode::STACK_INSTANCE:
  702. return (static_cast<const CStackInstance *>(node));
  703. case CBonusSystemNode::STACK_BATTLE:
  704. return (static_cast<const CStack*>(node))->base;
  705. default:
  706. return NULL;
  707. }
  708. }
  709. const CCreature * retrieveCreature(const CBonusSystemNode *node)
  710. {
  711. switch(node->nodeType)
  712. {
  713. case CBonusSystemNode::CREATURE:
  714. return (static_cast<const CCreature *>(node));
  715. default:
  716. const CStackInstance *csi = retreiveStackInstance(node);
  717. if(csi)
  718. return csi->type;
  719. return NULL;
  720. }
  721. }
  722. DLL_EXPORT std::ostream & operator<<(std::ostream &out, const BonusList &bonusList)
  723. {
  724. int i = 0;
  725. BOOST_FOREACH(const Bonus *b, bonusList)
  726. {
  727. out << "Bonus " << i++ << "\n" << *b << std::endl;
  728. }
  729. return out;
  730. }
  731. DLL_EXPORT std::ostream & operator<<(std::ostream &out, const Bonus &bonus)
  732. {
  733. for(std::map<std::string, int>::const_iterator i = bonusNameMap.begin(); i != bonusNameMap.end(); i++)
  734. if(i->second == bonus.type)
  735. out << "\tType: " << i->first << " \t";
  736. #define printField(field) out << "\t" #field ": " << (int)bonus.field << "\n"
  737. printField(val);
  738. printField(subtype);
  739. printField(duration);
  740. printField(source);
  741. printField(sid);
  742. printField(additionalInfo);
  743. printField(turnsRemain);
  744. printField(valType);
  745. printField(effectRange);
  746. #undef printField
  747. return out;
  748. }
  749. ILimiter::~ILimiter()
  750. {
  751. }
  752. bool ILimiter::limit(const Bonus *b, const CBonusSystemNode &node) const /*return true to drop the bonus */
  753. {
  754. return false;
  755. }
  756. bool CCreatureTypeLimiter::limit(const Bonus *b, const CBonusSystemNode &node) const
  757. {
  758. const CCreature *c = retrieveCreature(&node);
  759. if(!c)
  760. return true;
  761. return c != creature && (!includeUpgrades || !creature->isMyUpgrade(c));
  762. //drop bonus if it's not our creature and (we dont check upgrades or its not our upgrade)
  763. }
  764. CCreatureTypeLimiter::CCreatureTypeLimiter(const CCreature &Creature, ui8 IncludeUpgrades /*= true*/)
  765. :creature(&Creature), includeUpgrades(IncludeUpgrades)
  766. {
  767. }
  768. CCreatureTypeLimiter::CCreatureTypeLimiter()
  769. {
  770. creature = NULL;
  771. includeUpgrades = false;
  772. }
  773. HasAnotherBonusLimiter::HasAnotherBonusLimiter( TBonusType bonus )
  774. : type(bonus), subtype(0), isSubtypeRelevant(false)
  775. {
  776. }
  777. HasAnotherBonusLimiter::HasAnotherBonusLimiter( TBonusType bonus, TBonusSubtype _subtype )
  778. : type(bonus), subtype(_subtype), isSubtypeRelevant(true)
  779. {
  780. }
  781. bool HasAnotherBonusLimiter::limit( const Bonus *b, const CBonusSystemNode &node ) const
  782. {
  783. if(isSubtypeRelevant)
  784. {
  785. return !node.hasBonusOfType(static_cast<Bonus::BonusType>(type), subtype);
  786. }
  787. else
  788. {
  789. return !node.hasBonusOfType(static_cast<Bonus::BonusType>(type));
  790. }
  791. }
  792. IPropagator::~IPropagator()
  793. {
  794. }
  795. // CBonusSystemNode * IPropagator::getDestNode(CBonusSystemNode *source, CBonusSystemNode *redParent, CBonusSystemNode *redChild)
  796. // {
  797. // tlog1 << "IPropagator::getDestNode called!\n";
  798. // return source;
  799. // }
  800. bool IPropagator::shouldBeAttached(CBonusSystemNode *dest)
  801. {
  802. return false;
  803. }
  804. // CBonusSystemNode * CPropagatorNodeType::getDestNode(CBonusSystemNode *source, CBonusSystemNode *redParent, CBonusSystemNode *redChild)
  805. // {
  806. // return NULL;
  807. // }
  808. CPropagatorNodeType::CPropagatorNodeType()
  809. {
  810. }
  811. CPropagatorNodeType::CPropagatorNodeType(ui8 NodeType)
  812. : nodeType(NodeType)
  813. {
  814. }
  815. bool CPropagatorNodeType::shouldBeAttached(CBonusSystemNode *dest)
  816. {
  817. return nodeType == dest->nodeType;
  818. }
  819. CreatureNativeTerrainLimiter::CreatureNativeTerrainLimiter(int TerrainType)
  820. : terrainType(TerrainType)
  821. {
  822. }
  823. CreatureNativeTerrainLimiter::CreatureNativeTerrainLimiter()
  824. {
  825. }
  826. bool CreatureNativeTerrainLimiter::limit(const Bonus *b, const CBonusSystemNode &node) const
  827. {
  828. const CCreature *c = retrieveCreature(&node);
  829. return !c || !iswith(c->faction, 0, 9) || VLC->heroh->nativeTerrains[c->faction] != terrainType; //drop bonus for non-creatures or non-native residents
  830. //TODO neutral creatues
  831. }
  832. CreatureFactionLimiter::CreatureFactionLimiter(int Faction)
  833. : faction(Faction)
  834. {
  835. }
  836. CreatureFactionLimiter::CreatureFactionLimiter()
  837. {
  838. }
  839. bool CreatureFactionLimiter::limit(const Bonus *b, const CBonusSystemNode &node) const
  840. {
  841. const CCreature *c = retrieveCreature(&node);
  842. return !c || c->faction != faction; //drop bonus for non-creatures or non-native residents
  843. }
  844. CreatureAlignmentLimiter::CreatureAlignmentLimiter()
  845. {
  846. }
  847. CreatureAlignmentLimiter::CreatureAlignmentLimiter(si8 Alignment)
  848. : alignment(Alignment)
  849. {
  850. }
  851. bool CreatureAlignmentLimiter::limit(const Bonus *b, const CBonusSystemNode &node) const
  852. {
  853. const CCreature *c = retrieveCreature(&node);
  854. if(!c)
  855. return true;
  856. switch(alignment)
  857. {
  858. case GOOD:
  859. return !c->isGood(); //if not good -> return true (drop bonus)
  860. case NEUTRAL:
  861. return c->isEvil() || c->isGood();
  862. case EVIL:
  863. return !c->isEvil();
  864. default:
  865. tlog1 << "Warning: illegal alignment in limiter!\n";
  866. return true;
  867. }
  868. }
  869. RankRangeLimiter::RankRangeLimiter(ui8 Min, ui8 Max)
  870. :minRank(Min), maxRank(Max)
  871. {
  872. }
  873. RankRangeLimiter::RankRangeLimiter()
  874. {
  875. minRank = maxRank = -1;
  876. }
  877. bool RankRangeLimiter::limit( const Bonus *b, const CBonusSystemNode &node ) const
  878. {
  879. const CStackInstance *csi = retreiveStackInstance(&node);
  880. if(csi)
  881. return csi->getExpRank() < minRank || csi->getExpRank() > maxRank;
  882. return true;
  883. }
  884. bool StackOwnerLimiter::limit(const Bonus *b, const CBonusSystemNode &node) const
  885. {
  886. const CStack *s = retreiveStackBattle(&node);
  887. if(s)
  888. return s->owner != owner;
  889. const CStackInstance *csi = retreiveStackInstance(&node);
  890. if(csi && csi->armyObj)
  891. return csi->armyObj->tempOwner != owner;
  892. return true;
  893. }
  894. StackOwnerLimiter::StackOwnerLimiter()
  895. : owner(-1)
  896. {
  897. }
  898. StackOwnerLimiter::StackOwnerLimiter(ui8 Owner)
  899. : owner(Owner)
  900. {
  901. }