HeroBonus.cpp 19 KB

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