HeroBonus.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761
  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. #define FOREACH_CONST_PARENT(pname) TCNodes parents; getParents(parents); BOOST_FOREACH(const CBonusSystemNode *pname, parents)
  14. #define FOREACH_PARENT(pname) TNodes parents; getParents(parents); BOOST_FOREACH(CBonusSystemNode *pname, parents)
  15. #define BONUS_NAME(x) ( #x, Bonus::x )
  16. DLL_EXPORT const std::map<std::string, int> bonusNameMap = boost::assign::map_list_of BONUS_LIST;
  17. #undef BONUS_NAME
  18. #define BONUS_LOG_LINE(x) tlog0 << x << std::endl
  19. int DLL_EXPORT BonusList::totalValue() const
  20. {
  21. int base = 0;
  22. int percentToBase = 0;
  23. int percentToAll = 0;
  24. int additive = 0;
  25. int indepMax = 0;
  26. bool hasIndepMax = false;
  27. BOOST_FOREACH(Bonus *i, *this)
  28. {
  29. switch(i->valType)
  30. {
  31. case Bonus::BASE_NUMBER:
  32. base += i->val;
  33. break;
  34. case Bonus::PERCENT_TO_ALL:
  35. percentToAll += i->val;
  36. break;
  37. case Bonus::PERCENT_TO_BASE:
  38. percentToBase += i->val;
  39. break;
  40. case Bonus::ADDITIVE_VALUE:
  41. additive += i->val;
  42. break;
  43. case Bonus::INDEPENDENT_MAX:
  44. if (!indepMax)
  45. {
  46. indepMax = i->val;
  47. hasIndepMax = true;
  48. }
  49. else
  50. {
  51. amax(indepMax, i->val);
  52. }
  53. break;
  54. }
  55. }
  56. int modifiedBase = base + (base * percentToBase) / 100;
  57. modifiedBase += additive;
  58. int valFirst = (modifiedBase * (100 + percentToAll)) / 100;
  59. if (hasIndepMax)
  60. return std::max(valFirst, indepMax);
  61. else
  62. return valFirst;
  63. }
  64. const DLL_EXPORT Bonus * BonusList::getFirst(const CSelector &selector) const
  65. {
  66. BOOST_FOREACH(Bonus *i, *this)
  67. if(selector(i))
  68. return &*i;
  69. return NULL;
  70. }
  71. DLL_EXPORT Bonus * BonusList::getFirst(const CSelector &select)
  72. {
  73. BOOST_FOREACH(Bonus *i, *this)
  74. if(select(i))
  75. return &*i;
  76. return NULL;
  77. }
  78. void DLL_EXPORT BonusList::getModifiersWDescr(TModDescr &out) const
  79. {
  80. BOOST_FOREACH(Bonus *i, *this)
  81. out.push_back(std::make_pair(i->val, i->Description()));
  82. }
  83. void DLL_EXPORT BonusList::getBonuses(BonusList &out, const CSelector &selector, const CBonusSystemNode *source /*= NULL*/) const
  84. {
  85. BOOST_FOREACH(Bonus *i, *this)
  86. if(selector(i) && i->effectRange == Bonus::NO_LIMIT)
  87. out.push_back(i);
  88. }
  89. void DLL_EXPORT BonusList::getBonuses(BonusList &out, const CSelector &selector, const CSelector &limit, const CBonusSystemNode *source /*= NULL*/) const
  90. {
  91. BOOST_FOREACH(Bonus *i, *this)
  92. if(selector(i) && (!limit || limit(i)))
  93. out.push_back(i);
  94. }
  95. void DLL_EXPORT BonusList::removeSpells(Bonus::BonusSource sourceType)
  96. {
  97. remove_if(Selector::sourceType(sourceType));
  98. }
  99. void BonusList::limit(const CBonusSystemNode &node)
  100. {
  101. remove_if(boost::bind(&CBonusSystemNode::isLimitedOnUs, boost::ref(node), _1));
  102. }
  103. int CBonusSystemNode::valOfBonuses(Bonus::BonusType type, const CSelector &selector) const
  104. {
  105. return valOfBonuses(Selector::type(type) && selector);
  106. }
  107. int CBonusSystemNode::valOfBonuses(Bonus::BonusType type, int subtype /*= -1*/) const
  108. {
  109. CSelector s = Selector::type(type);
  110. if(subtype != -1)
  111. s = s && Selector::subtype(subtype);
  112. return valOfBonuses(s);
  113. }
  114. int CBonusSystemNode::valOfBonuses(const CSelector &selector) const
  115. {
  116. BonusList hlp;
  117. getBonuses(hlp, selector);
  118. return hlp.totalValue();
  119. }
  120. bool CBonusSystemNode::hasBonus(const CSelector &selector) const
  121. {
  122. return getBonuses(selector).size() > 0;
  123. }
  124. bool CBonusSystemNode::hasBonusOfType(Bonus::BonusType type, int subtype /*= -1*/) const
  125. {
  126. CSelector s = Selector::type(type);
  127. if(subtype != -1)
  128. s = s && Selector::subtype(subtype);
  129. return hasBonus(s);
  130. }
  131. Bonus * CBonusSystemNode::getBonus(const CSelector &selector)
  132. {
  133. Bonus *ret = bonuses.getFirst(selector);
  134. if(ret)
  135. return ret;
  136. FOREACH_PARENT(pname)
  137. {
  138. ret = pname->getBonus(selector);
  139. if (ret)
  140. return ret;
  141. }
  142. return NULL;
  143. }
  144. const Bonus * CBonusSystemNode::getBonus( const CSelector &selector ) const
  145. {
  146. return (const_cast<CBonusSystemNode*>(this))->getBonus(selector);
  147. }
  148. void CBonusSystemNode::getModifiersWDescr(TModDescr &out, Bonus::BonusType type, int subtype /*= -1 */) const
  149. {
  150. getModifiersWDescr(out, Selector::typeSybtype(type, subtype));
  151. }
  152. void CBonusSystemNode::getModifiersWDescr(TModDescr &out, const CSelector &selector) const
  153. {
  154. getBonuses(selector).getModifiersWDescr(out);
  155. }
  156. int CBonusSystemNode::getBonusesCount(int from, int id) const
  157. {
  158. return getBonusesCount(Selector::source(from, id));
  159. }
  160. int CBonusSystemNode::getBonusesCount(const CSelector &selector) const
  161. {
  162. return getBonuses(selector).size();
  163. }
  164. void CBonusSystemNode::getParents(TCNodes &out) const /*retreives list of parent nodes (nodes to inherit bonuses from) */
  165. {
  166. BOOST_FOREACH(const CBonusSystemNode *parent, parents)
  167. out.insert(parent);
  168. }
  169. void CBonusSystemNode::getParents(TNodes &out)
  170. {
  171. BOOST_FOREACH(const CBonusSystemNode *parent, parents)
  172. out.insert(const_cast<CBonusSystemNode*>(parent));
  173. }
  174. void CBonusSystemNode::getBonuses(BonusList &out, const CSelector &selector, const CBonusSystemNode *root /*= NULL*/) const
  175. {
  176. FOREACH_CONST_PARENT(p) //unwinded macro
  177. p->getBonuses(out, selector, root ? root : this);
  178. bonuses.getBonuses(out, selector);
  179. if(!root)
  180. out.limit(*this);
  181. }
  182. BonusList CBonusSystemNode::getBonuses(const CSelector &selector) const
  183. {
  184. BonusList ret;
  185. getBonuses(ret, selector);
  186. return ret;
  187. }
  188. void CBonusSystemNode::getBonuses(BonusList &out, const CSelector &selector, const CSelector &limit) const
  189. {
  190. getBonuses(out, selector); //first get all the bonuses
  191. out.remove_if(std::not1(limit)); //now remove the ones we don't like
  192. out.limit(*this); //apply bonuses' limiters
  193. }
  194. BonusList CBonusSystemNode::getBonuses(const CSelector &selector, const CSelector &limit) const
  195. {
  196. BonusList ret;
  197. getBonuses(ret, selector, limit);
  198. return ret;
  199. }
  200. bool CBonusSystemNode::hasBonusFrom(ui8 source, ui32 sourceID) const
  201. {
  202. return hasBonus(Selector::source(source,sourceID));
  203. }
  204. int CBonusSystemNode::MoraleVal() const
  205. {
  206. if(hasBonusOfType(Bonus::NON_LIVING) || hasBonusOfType(Bonus::UNDEAD) ||
  207. hasBonusOfType(Bonus::NO_MORALE) || hasBonusOfType(Bonus::SIEGE_WEAPON))
  208. return 0;
  209. int ret = valOfBonuses(Selector::type(Bonus::MORALE));
  210. if(hasBonusOfType(Bonus::SELF_MORALE)) //eg. minotaur
  211. amax(ret, +1);
  212. return abetw(ret, -3, +3);
  213. }
  214. int CBonusSystemNode::LuckVal() const
  215. {
  216. if(hasBonusOfType(Bonus::NO_LUCK))
  217. return 0;
  218. int ret = valOfBonuses(Selector::type(Bonus::LUCK));
  219. if(hasBonusOfType(Bonus::SELF_LUCK)) //eg. halfling
  220. amax(ret, +1);
  221. return abetw(ret, -3, +3);
  222. }
  223. si32 CBonusSystemNode::Attack() const
  224. {
  225. si32 ret = valOfBonuses(Bonus::PRIMARY_SKILL, PrimarySkill::ATTACK);
  226. if(int frenzyPower = valOfBonuses(Bonus::IN_FRENZY)) //frenzy for attacker
  227. {
  228. ret += frenzyPower * Defense(false);
  229. }
  230. return ret;
  231. }
  232. si32 CBonusSystemNode::Defense(bool withFrenzy /*= true*/) const
  233. {
  234. si32 ret = valOfBonuses(Bonus::PRIMARY_SKILL, PrimarySkill::DEFENSE);
  235. if(withFrenzy && hasBonusOfType(Bonus::IN_FRENZY)) //frenzy for defender
  236. {
  237. return 0;
  238. }
  239. return ret;
  240. }
  241. ui16 CBonusSystemNode::MaxHealth() const
  242. {
  243. return valOfBonuses(Bonus::STACK_HEALTH);
  244. }
  245. ui32 CBonusSystemNode::getMinDamage() const
  246. {
  247. return valOfBonuses(Selector::typeSybtype(Bonus::CREATURE_DAMAGE, 0) || Selector::typeSybtype(Bonus::CREATURE_DAMAGE, 1));
  248. }
  249. ui32 CBonusSystemNode::getMaxDamage() const
  250. {
  251. return valOfBonuses(Selector::typeSybtype(Bonus::CREATURE_DAMAGE, 0) || Selector::typeSybtype(Bonus::CREATURE_DAMAGE, 2));
  252. }
  253. CBonusSystemNode::CBonusSystemNode()
  254. {
  255. nodeType = UNKNOWN;
  256. }
  257. CBonusSystemNode::~CBonusSystemNode()
  258. {
  259. detachFromAll();
  260. if(children.size())
  261. {
  262. tlog2 << "Warning: an orphaned child!\n";
  263. }
  264. }
  265. void CBonusSystemNode::attachTo(CBonusSystemNode *parent)
  266. {
  267. assert(!vstd::contains(parents, parent));
  268. parents.push_back(parent);
  269. // BOOST_FOREACH(Bonus *b, exportedBonuses)
  270. // propagateBonus(b);
  271. //
  272. // if(parent->weActAsBonusSourceOnly())
  273. // {
  274. //
  275. // }
  276. parent->newChildAttached(this);
  277. }
  278. void CBonusSystemNode::detachFrom(CBonusSystemNode *parent)
  279. {
  280. assert(vstd::contains(parents, parent));
  281. parents -= parent;
  282. //unpropagate bonus
  283. parent->childDetached(this);
  284. }
  285. void CBonusSystemNode::popBonuses(const CSelector &s)
  286. {
  287. //TODO
  288. //prop
  289. BonusList bl;
  290. exportedBonuses.getBonuses(bl, s);
  291. BOOST_FOREACH(Bonus *b, bl)
  292. removeBonus(b);
  293. BOOST_FOREACH(CBonusSystemNode *child, children)
  294. child->popBonuses(s);
  295. }
  296. // void CBonusSystemNode::addNewBonus(const Bonus &b)
  297. // {
  298. // addNewBonus(new Bonus(b));
  299. // }
  300. void CBonusSystemNode::addNewBonus(Bonus *b)
  301. {
  302. exportedBonuses.push_back(b);
  303. propagateBonus(b);
  304. }
  305. void CBonusSystemNode::removeBonus(Bonus *b)
  306. {
  307. exportedBonuses -= b;
  308. CBonusSystemNode *whereIsOurBonus = whereToPropagate(b);
  309. whereIsOurBonus->bonuses -= b;
  310. delNull(b);
  311. }
  312. CBonusSystemNode * CBonusSystemNode::whereToPropagate(Bonus *b)
  313. {
  314. if(b->propagator)
  315. return b->propagator->getDestNode(this);
  316. else
  317. return this;
  318. }
  319. bool CBonusSystemNode::isLimitedOnUs(Bonus *b) const
  320. {
  321. return b->limiter && b->limiter->limit(b, *this);
  322. }
  323. bool CBonusSystemNode::weActAsBonusSourceOnly() const
  324. {
  325. switch(nodeType)
  326. {
  327. case CREATURE:
  328. case ARTIFACT:
  329. case ARTIFACT_INSTANCE:
  330. return true;
  331. default:
  332. return false;
  333. }
  334. }
  335. TNodesVector & CBonusSystemNode::nodesOnWhichWePropagate()
  336. {
  337. return weActAsBonusSourceOnly() ? children : parents;
  338. }
  339. void CBonusSystemNode::propagateBonus(Bonus * b)
  340. {
  341. whereToPropagate(b)->bonuses.push_back(b);
  342. }
  343. void CBonusSystemNode::newChildAttached(CBonusSystemNode *child)
  344. {
  345. assert(!vstd::contains(children, child));
  346. children.push_back(child);
  347. BONUS_LOG_LINE(child->nodeName() << " #attached to# " << nodeName());
  348. }
  349. void CBonusSystemNode::childDetached(CBonusSystemNode *child)
  350. {
  351. assert(vstd::contains(children, child));
  352. children -= child;
  353. BONUS_LOG_LINE(child->nodeName() << " #detached from# " << nodeName());
  354. }
  355. void CBonusSystemNode::detachFromAll()
  356. {
  357. while(parents.size())
  358. detachFrom(parents.front());
  359. }
  360. bool CBonusSystemNode::isIndependentNode() const
  361. {
  362. return parents.empty() && children.empty();
  363. }
  364. std::string CBonusSystemNode::nodeName() const
  365. {
  366. return std::string("Bonus system node of type ") + typeid(*this).name();
  367. }
  368. int NBonus::valOf(const CBonusSystemNode *obj, Bonus::BonusType type, int subtype /*= -1*/)
  369. {
  370. if(obj)
  371. return obj->valOfBonuses(type, subtype);
  372. return 0;
  373. }
  374. bool NBonus::hasOfType(const CBonusSystemNode *obj, Bonus::BonusType type, int subtype /*= -1*/)
  375. {
  376. if(obj)
  377. return obj->hasBonusOfType(type, subtype);
  378. return false;
  379. }
  380. void NBonus::getModifiersWDescr(const CBonusSystemNode *obj, TModDescr &out, Bonus::BonusType type, int subtype /*= -1 */)
  381. {
  382. if(obj)
  383. return obj->getModifiersWDescr(out, type, subtype);
  384. }
  385. int NBonus::getCount(const CBonusSystemNode *obj, int from, int id)
  386. {
  387. if(obj)
  388. return obj->getBonusesCount(from, id);
  389. return 0;
  390. }
  391. const CSpell * Bonus::sourceSpell() const
  392. {
  393. if(source == SPELL_EFFECT)
  394. return VLC->spellh->spells[id];
  395. return NULL;
  396. }
  397. std::string Bonus::Description() const
  398. {
  399. if(description.size())
  400. return description;
  401. std::ostringstream str;
  402. if(val < 0)
  403. str << '-';
  404. else if(val > 0)
  405. str << '+';
  406. str << val << " ";
  407. switch(source)
  408. {
  409. case CREATURE_ABILITY:
  410. str << VLC->creh->creatures[id]->namePl;
  411. break;
  412. }
  413. return str.str();
  414. }
  415. Bonus::Bonus(ui8 Dur, ui8 Type, ui8 Src, si32 Val, ui32 ID, std::string Desc, si32 Subtype/*=-1*/)
  416. : duration(Dur), type(Type), subtype(Subtype), source(Src), val(Val), id(ID), description(Desc)
  417. {
  418. additionalInfo = -1;
  419. turnsRemain = 0;
  420. valType = ADDITIVE_VALUE;
  421. effectRange = NO_LIMIT;
  422. boost::algorithm::trim(description);
  423. }
  424. Bonus::Bonus(ui8 Dur, ui8 Type, ui8 Src, si32 Val, ui32 ID, si32 Subtype/*=-1*/, ui8 ValType /*= ADDITIVE_VALUE*/)
  425. : duration(Dur), type(Type), subtype(Subtype), source(Src), val(Val), id(ID), valType(ValType)
  426. {
  427. additionalInfo = -1;
  428. turnsRemain = 0;
  429. effectRange = NO_LIMIT;
  430. }
  431. Bonus::Bonus()
  432. {
  433. subtype = -1;
  434. additionalInfo = -1;
  435. turnsRemain = 0;
  436. valType = ADDITIVE_VALUE;
  437. effectRange = NO_LIMIT;
  438. }
  439. Bonus::~Bonus()
  440. {
  441. }
  442. Bonus * Bonus::addLimiter(ILimiter *Limiter)
  443. {
  444. limiter.reset(Limiter);
  445. return this;
  446. }
  447. CSelector DLL_EXPORT operator&&(const CSelector &first, const CSelector &second)
  448. {
  449. return CSelectorsConjunction(first, second);
  450. }
  451. CSelector DLL_EXPORT operator||(const CSelector &first, const CSelector &second)
  452. {
  453. return CSelectorsAlternative(first, second);
  454. }
  455. namespace Selector
  456. {
  457. DLL_EXPORT CSelectFieldEqual<TBonusType> type(&Bonus::type, 0);
  458. DLL_EXPORT CSelectFieldEqual<TBonusSubtype> subtype(&Bonus::subtype, 0);
  459. DLL_EXPORT CSelectFieldEqual<si32> info(&Bonus::additionalInfo, 0);
  460. DLL_EXPORT CSelectFieldEqual<ui8> sourceType(&Bonus::source, 0);
  461. DLL_EXPORT CSelectFieldEqual<ui8> effectRange(&Bonus::effectRange, Bonus::NO_LIMIT);
  462. DLL_EXPORT CWillLastTurns turns;;
  463. CSelector DLL_EXPORT typeSybtype(TBonusType Type, TBonusSubtype Subtype)
  464. {
  465. return type(Type) && subtype(Subtype);
  466. }
  467. CSelector DLL_EXPORT typeSybtypeInfo(TBonusType type, TBonusSubtype subtype, si32 info)
  468. {
  469. return CSelectFieldEqual<TBonusType>(&Bonus::type, type) && CSelectFieldEqual<TBonusSubtype>(&Bonus::subtype, subtype) && CSelectFieldEqual<si32>(&Bonus::additionalInfo, info);
  470. }
  471. CSelector DLL_EXPORT source(ui8 source, ui32 sourceID)
  472. {
  473. return CSelectFieldEqual<ui8>(&Bonus::source, source) && CSelectFieldEqual<ui32>(&Bonus::id, sourceID);
  474. }
  475. CSelector DLL_EXPORT sourceTypeSel(ui8 source)
  476. {
  477. return CSelectFieldEqual<ui8>(&Bonus::source, source);
  478. }
  479. bool DLL_EXPORT matchesType(const CSelector &sel, TBonusType type)
  480. {
  481. Bonus dummy;
  482. dummy.type = type;
  483. return sel(&dummy);
  484. }
  485. bool DLL_EXPORT matchesTypeSubtype(const CSelector &sel, TBonusType type, TBonusSubtype subtype)
  486. {
  487. Bonus dummy;
  488. dummy.type = type;
  489. dummy.subtype = subtype;
  490. return sel(&dummy);
  491. }
  492. }
  493. const CCreature * retrieveCreature(const CBonusSystemNode *node)
  494. {
  495. switch(node->nodeType)
  496. {
  497. case CBonusSystemNode::CREATURE:
  498. return (static_cast<const CCreature *>(node));
  499. case CBonusSystemNode::STACK:
  500. return (static_cast<const CStackInstance *>(node))->type;
  501. default:
  502. return NULL;
  503. }
  504. }
  505. DLL_EXPORT std::ostream & operator<<(std::ostream &out, const BonusList &bonusList)
  506. {
  507. int i = 0;
  508. BOOST_FOREACH(const Bonus *b, bonusList)
  509. {
  510. out << "Bonus " << i++ << "\n" << *b << std::endl;
  511. }
  512. return out;
  513. }
  514. DLL_EXPORT std::ostream & operator<<(std::ostream &out, const Bonus &bonus)
  515. {
  516. for(std::map<std::string, int>::const_iterator i = bonusNameMap.begin(); i != bonusNameMap.end(); i++)
  517. if(i->second == bonus.type)
  518. out << "\tType: " << i->first << " \t";
  519. #define printField(field) out << "\t" #field ": " << (int)bonus.field << "\n"
  520. printField(val);
  521. printField(subtype);
  522. printField(duration);
  523. printField(source);
  524. printField(id);
  525. printField(additionalInfo);
  526. printField(turnsRemain);
  527. printField(valType);
  528. printField(effectRange);
  529. #undef printField
  530. return out;
  531. }
  532. ILimiter::~ILimiter()
  533. {
  534. }
  535. bool ILimiter::limit(const Bonus *b, const CBonusSystemNode &node) const /*return true to drop the bonus */
  536. {
  537. return false;
  538. }
  539. bool CCreatureTypeLimiter::limit(const Bonus *b, const CBonusSystemNode &node) const
  540. {
  541. switch (node.nodeType)
  542. {
  543. case CBonusSystemNode::STACK:
  544. {
  545. const CCreature *c = (static_cast<const CStackInstance *>(&node))->type;
  546. return c != creature && (!includeUpgrades || !creature->isMyUpgrade(c));
  547. } //drop bonus if it's not our creature and (we dont check upgrades or its not our upgrade)
  548. break;
  549. case CBonusSystemNode::CREATURE:
  550. {
  551. const CCreature *c = (static_cast<const CCreature *>(&node));
  552. return c != creature && (!includeUpgrades || !creature->isMyUpgrade(c));
  553. }
  554. break;
  555. default:
  556. return true;
  557. }
  558. }
  559. CCreatureTypeLimiter::CCreatureTypeLimiter(const CCreature &Creature, ui8 IncludeUpgrades /*= true*/)
  560. :creature(&Creature), includeUpgrades(IncludeUpgrades)
  561. {
  562. }
  563. CCreatureTypeLimiter::CCreatureTypeLimiter()
  564. {
  565. creature = NULL;
  566. includeUpgrades = false;
  567. }
  568. HasAnotherBonusLimiter::HasAnotherBonusLimiter( TBonusType bonus )
  569. : type(bonus), subtype(0), isSubtypeRelevant(false)
  570. {
  571. }
  572. HasAnotherBonusLimiter::HasAnotherBonusLimiter( TBonusType bonus, TBonusSubtype _subtype )
  573. : type(bonus), subtype(_subtype), isSubtypeRelevant(true)
  574. {
  575. }
  576. bool HasAnotherBonusLimiter::limit( const Bonus *b, const CBonusSystemNode &node ) const
  577. {
  578. if(isSubtypeRelevant)
  579. {
  580. return !node.hasBonusOfType(static_cast<Bonus::BonusType>(type), subtype);
  581. }
  582. else
  583. {
  584. return !node.hasBonusOfType(static_cast<Bonus::BonusType>(type));
  585. }
  586. }
  587. IPropagator::~IPropagator()
  588. {
  589. }
  590. CBonusSystemNode * IPropagator::getDestNode(CBonusSystemNode *source)
  591. {
  592. return source;
  593. }
  594. CreatureNativeTerrainLimiter::CreatureNativeTerrainLimiter(int TerrainType)
  595. : terrainType(TerrainType)
  596. {
  597. }
  598. CreatureNativeTerrainLimiter::CreatureNativeTerrainLimiter()
  599. {
  600. }
  601. bool CreatureNativeTerrainLimiter::limit(const Bonus *b, const CBonusSystemNode &node) const
  602. {
  603. const CCreature *c = retrieveCreature(&node);
  604. return !c || VLC->heroh->nativeTerrains[c->faction] != terrainType; //drop bonus for non-creatures or non-native residents
  605. }
  606. CreatureFactionLimiter::CreatureFactionLimiter(int Faction)
  607. : faction(Faction)
  608. {
  609. }
  610. CreatureFactionLimiter::CreatureFactionLimiter()
  611. {
  612. }
  613. bool CreatureFactionLimiter::limit(const Bonus *b, const CBonusSystemNode &node) const
  614. {
  615. const CCreature *c = retrieveCreature(&node);
  616. return !c || c->faction != faction; //drop bonus for non-creatures or non-native residents
  617. }
  618. CreatureAlignmentLimiter::CreatureAlignmentLimiter()
  619. {
  620. }
  621. CreatureAlignmentLimiter::CreatureAlignmentLimiter(si8 Alignment)
  622. : alignment(Alignment)
  623. {
  624. }
  625. bool CreatureAlignmentLimiter::limit(const Bonus *b, const CBonusSystemNode &node) const
  626. {
  627. const CCreature *c = retrieveCreature(&node);
  628. if(!c)
  629. return true;
  630. switch(alignment)
  631. {
  632. case GOOD:
  633. return !c->isGood(); //if not good -> return true (drop bonus)
  634. case NEUTRAL:
  635. return c->isEvil() || c->isGood();
  636. case EVIL:
  637. return !c->isEvil();
  638. default:
  639. tlog1 << "Warning: illegal alignment in limiter!\n";
  640. return true;
  641. }
  642. }