HeroBonus.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772
  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. while(children.size())
  265. children.front()->detachFrom(this);
  266. }
  267. }
  268. void CBonusSystemNode::attachTo(CBonusSystemNode *parent)
  269. {
  270. assert(!vstd::contains(parents, parent));
  271. parents.push_back(parent);
  272. // BOOST_FOREACH(Bonus *b, exportedBonuses)
  273. // propagateBonus(b);
  274. //
  275. // if(parent->weActAsBonusSourceOnly())
  276. // {
  277. //
  278. // }
  279. parent->newChildAttached(this);
  280. }
  281. void CBonusSystemNode::detachFrom(CBonusSystemNode *parent)
  282. {
  283. assert(vstd::contains(parents, parent));
  284. parents -= parent;
  285. //unpropagate bonus
  286. parent->childDetached(this);
  287. }
  288. void CBonusSystemNode::popBonuses(const CSelector &s)
  289. {
  290. //TODO
  291. //prop
  292. BonusList bl;
  293. exportedBonuses.getBonuses(bl, s);
  294. BOOST_FOREACH(Bonus *b, bl)
  295. removeBonus(b);
  296. BOOST_FOREACH(CBonusSystemNode *child, children)
  297. child->popBonuses(s);
  298. }
  299. // void CBonusSystemNode::addNewBonus(const Bonus &b)
  300. // {
  301. // addNewBonus(new Bonus(b));
  302. // }
  303. void CBonusSystemNode::addNewBonus(Bonus *b)
  304. {
  305. exportedBonuses.push_back(b);
  306. propagateBonus(b);
  307. }
  308. void CBonusSystemNode::removeBonus(Bonus *b)
  309. {
  310. exportedBonuses -= b;
  311. CBonusSystemNode *whereIsOurBonus = whereToPropagate(b);
  312. whereIsOurBonus->bonuses -= b;
  313. delNull(b);
  314. }
  315. CBonusSystemNode * CBonusSystemNode::whereToPropagate(Bonus *b)
  316. {
  317. if(b->propagator)
  318. return b->propagator->getDestNode(this);
  319. else
  320. return this;
  321. }
  322. bool CBonusSystemNode::isLimitedOnUs(Bonus *b) const
  323. {
  324. return b->limiter && b->limiter->limit(b, *this);
  325. }
  326. bool CBonusSystemNode::weActAsBonusSourceOnly() const
  327. {
  328. switch(nodeType)
  329. {
  330. case CREATURE:
  331. case ARTIFACT:
  332. case ARTIFACT_INSTANCE:
  333. return true;
  334. default:
  335. return false;
  336. }
  337. }
  338. TNodesVector & CBonusSystemNode::nodesOnWhichWePropagate()
  339. {
  340. return weActAsBonusSourceOnly() ? children : parents;
  341. }
  342. void CBonusSystemNode::propagateBonus(Bonus * b)
  343. {
  344. whereToPropagate(b)->bonuses.push_back(b);
  345. }
  346. void CBonusSystemNode::newChildAttached(CBonusSystemNode *child)
  347. {
  348. assert(!vstd::contains(children, child));
  349. children.push_back(child);
  350. BONUS_LOG_LINE(child->nodeName() << " #attached to# " << nodeName());
  351. }
  352. void CBonusSystemNode::childDetached(CBonusSystemNode *child)
  353. {
  354. assert(vstd::contains(children, child));
  355. children -= child;
  356. BONUS_LOG_LINE(child->nodeName() << " #detached from# " << nodeName());
  357. }
  358. void CBonusSystemNode::detachFromAll()
  359. {
  360. while(parents.size())
  361. detachFrom(parents.front());
  362. }
  363. bool CBonusSystemNode::isIndependentNode() const
  364. {
  365. return parents.empty() && children.empty();
  366. }
  367. std::string CBonusSystemNode::nodeName() const
  368. {
  369. return std::string("Bonus system node of type ") + typeid(*this).name();
  370. }
  371. void CBonusSystemNode::deserializationFix()
  372. {
  373. tlog2 << "Deserialization fix called on bare CBSN? Shouldn't be...\n";
  374. }
  375. int NBonus::valOf(const CBonusSystemNode *obj, Bonus::BonusType type, int subtype /*= -1*/)
  376. {
  377. if(obj)
  378. return obj->valOfBonuses(type, subtype);
  379. return 0;
  380. }
  381. bool NBonus::hasOfType(const CBonusSystemNode *obj, Bonus::BonusType type, int subtype /*= -1*/)
  382. {
  383. if(obj)
  384. return obj->hasBonusOfType(type, subtype);
  385. return false;
  386. }
  387. void NBonus::getModifiersWDescr(const CBonusSystemNode *obj, TModDescr &out, Bonus::BonusType type, int subtype /*= -1 */)
  388. {
  389. if(obj)
  390. return obj->getModifiersWDescr(out, type, subtype);
  391. }
  392. int NBonus::getCount(const CBonusSystemNode *obj, int from, int id)
  393. {
  394. if(obj)
  395. return obj->getBonusesCount(from, id);
  396. return 0;
  397. }
  398. const CSpell * Bonus::sourceSpell() const
  399. {
  400. if(source == SPELL_EFFECT)
  401. return VLC->spellh->spells[id];
  402. return NULL;
  403. }
  404. std::string Bonus::Description() const
  405. {
  406. if(description.size())
  407. return description;
  408. std::ostringstream str;
  409. if(val < 0)
  410. str << '-';
  411. else if(val > 0)
  412. str << '+';
  413. str << val << " ";
  414. switch(source)
  415. {
  416. case CREATURE_ABILITY:
  417. str << VLC->creh->creatures[id]->namePl;
  418. break;
  419. case SECONDARY_SKILL:
  420. str << VLC->generaltexth->skillName[id] << " secondary skill";
  421. break;
  422. }
  423. return str.str();
  424. }
  425. Bonus::Bonus(ui8 Dur, ui8 Type, ui8 Src, si32 Val, ui32 ID, std::string Desc, si32 Subtype/*=-1*/)
  426. : duration(Dur), type(Type), subtype(Subtype), source(Src), val(Val), id(ID), description(Desc)
  427. {
  428. additionalInfo = -1;
  429. turnsRemain = 0;
  430. valType = ADDITIVE_VALUE;
  431. effectRange = NO_LIMIT;
  432. boost::algorithm::trim(description);
  433. }
  434. Bonus::Bonus(ui8 Dur, ui8 Type, ui8 Src, si32 Val, ui32 ID, si32 Subtype/*=-1*/, ui8 ValType /*= ADDITIVE_VALUE*/)
  435. : duration(Dur), type(Type), subtype(Subtype), source(Src), val(Val), id(ID), valType(ValType)
  436. {
  437. additionalInfo = -1;
  438. turnsRemain = 0;
  439. effectRange = NO_LIMIT;
  440. }
  441. Bonus::Bonus()
  442. {
  443. subtype = -1;
  444. additionalInfo = -1;
  445. turnsRemain = 0;
  446. valType = ADDITIVE_VALUE;
  447. effectRange = NO_LIMIT;
  448. }
  449. Bonus::~Bonus()
  450. {
  451. }
  452. Bonus * Bonus::addLimiter(ILimiter *Limiter)
  453. {
  454. limiter.reset(Limiter);
  455. return this;
  456. }
  457. CSelector DLL_EXPORT operator&&(const CSelector &first, const CSelector &second)
  458. {
  459. return CSelectorsConjunction(first, second);
  460. }
  461. CSelector DLL_EXPORT operator||(const CSelector &first, const CSelector &second)
  462. {
  463. return CSelectorsAlternative(first, second);
  464. }
  465. namespace Selector
  466. {
  467. DLL_EXPORT CSelectFieldEqual<TBonusType> type(&Bonus::type, 0);
  468. DLL_EXPORT CSelectFieldEqual<TBonusSubtype> subtype(&Bonus::subtype, 0);
  469. DLL_EXPORT CSelectFieldEqual<si32> info(&Bonus::additionalInfo, 0);
  470. DLL_EXPORT CSelectFieldEqual<ui8> sourceType(&Bonus::source, 0);
  471. DLL_EXPORT CSelectFieldEqual<ui8> effectRange(&Bonus::effectRange, Bonus::NO_LIMIT);
  472. DLL_EXPORT CWillLastTurns turns;;
  473. CSelector DLL_EXPORT typeSybtype(TBonusType Type, TBonusSubtype Subtype)
  474. {
  475. return type(Type) && subtype(Subtype);
  476. }
  477. CSelector DLL_EXPORT typeSybtypeInfo(TBonusType type, TBonusSubtype subtype, si32 info)
  478. {
  479. return CSelectFieldEqual<TBonusType>(&Bonus::type, type) && CSelectFieldEqual<TBonusSubtype>(&Bonus::subtype, subtype) && CSelectFieldEqual<si32>(&Bonus::additionalInfo, info);
  480. }
  481. CSelector DLL_EXPORT source(ui8 source, ui32 sourceID)
  482. {
  483. return CSelectFieldEqual<ui8>(&Bonus::source, source) && CSelectFieldEqual<ui32>(&Bonus::id, sourceID);
  484. }
  485. CSelector DLL_EXPORT sourceTypeSel(ui8 source)
  486. {
  487. return CSelectFieldEqual<ui8>(&Bonus::source, source);
  488. }
  489. bool DLL_EXPORT matchesType(const CSelector &sel, TBonusType type)
  490. {
  491. Bonus dummy;
  492. dummy.type = type;
  493. return sel(&dummy);
  494. }
  495. bool DLL_EXPORT matchesTypeSubtype(const CSelector &sel, TBonusType type, TBonusSubtype subtype)
  496. {
  497. Bonus dummy;
  498. dummy.type = type;
  499. dummy.subtype = subtype;
  500. return sel(&dummy);
  501. }
  502. }
  503. const CCreature * retrieveCreature(const CBonusSystemNode *node)
  504. {
  505. switch(node->nodeType)
  506. {
  507. case CBonusSystemNode::CREATURE:
  508. return (static_cast<const CCreature *>(node));
  509. case CBonusSystemNode::STACK:
  510. return (static_cast<const CStackInstance *>(node))->type;
  511. default:
  512. return NULL;
  513. }
  514. }
  515. DLL_EXPORT std::ostream & operator<<(std::ostream &out, const BonusList &bonusList)
  516. {
  517. int i = 0;
  518. BOOST_FOREACH(const Bonus *b, bonusList)
  519. {
  520. out << "Bonus " << i++ << "\n" << *b << std::endl;
  521. }
  522. return out;
  523. }
  524. DLL_EXPORT std::ostream & operator<<(std::ostream &out, const Bonus &bonus)
  525. {
  526. for(std::map<std::string, int>::const_iterator i = bonusNameMap.begin(); i != bonusNameMap.end(); i++)
  527. if(i->second == bonus.type)
  528. out << "\tType: " << i->first << " \t";
  529. #define printField(field) out << "\t" #field ": " << (int)bonus.field << "\n"
  530. printField(val);
  531. printField(subtype);
  532. printField(duration);
  533. printField(source);
  534. printField(id);
  535. printField(additionalInfo);
  536. printField(turnsRemain);
  537. printField(valType);
  538. printField(effectRange);
  539. #undef printField
  540. return out;
  541. }
  542. ILimiter::~ILimiter()
  543. {
  544. }
  545. bool ILimiter::limit(const Bonus *b, const CBonusSystemNode &node) const /*return true to drop the bonus */
  546. {
  547. return false;
  548. }
  549. bool CCreatureTypeLimiter::limit(const Bonus *b, const CBonusSystemNode &node) const
  550. {
  551. switch (node.nodeType)
  552. {
  553. case CBonusSystemNode::STACK:
  554. {
  555. const CCreature *c = (static_cast<const CStackInstance *>(&node))->type;
  556. return c != creature && (!includeUpgrades || !creature->isMyUpgrade(c));
  557. } //drop bonus if it's not our creature and (we dont check upgrades or its not our upgrade)
  558. break;
  559. case CBonusSystemNode::CREATURE:
  560. {
  561. const CCreature *c = (static_cast<const CCreature *>(&node));
  562. return c != creature && (!includeUpgrades || !creature->isMyUpgrade(c));
  563. }
  564. break;
  565. default:
  566. return true;
  567. }
  568. }
  569. CCreatureTypeLimiter::CCreatureTypeLimiter(const CCreature &Creature, ui8 IncludeUpgrades /*= true*/)
  570. :creature(&Creature), includeUpgrades(IncludeUpgrades)
  571. {
  572. }
  573. CCreatureTypeLimiter::CCreatureTypeLimiter()
  574. {
  575. creature = NULL;
  576. includeUpgrades = false;
  577. }
  578. HasAnotherBonusLimiter::HasAnotherBonusLimiter( TBonusType bonus )
  579. : type(bonus), subtype(0), isSubtypeRelevant(false)
  580. {
  581. }
  582. HasAnotherBonusLimiter::HasAnotherBonusLimiter( TBonusType bonus, TBonusSubtype _subtype )
  583. : type(bonus), subtype(_subtype), isSubtypeRelevant(true)
  584. {
  585. }
  586. bool HasAnotherBonusLimiter::limit( const Bonus *b, const CBonusSystemNode &node ) const
  587. {
  588. if(isSubtypeRelevant)
  589. {
  590. return !node.hasBonusOfType(static_cast<Bonus::BonusType>(type), subtype);
  591. }
  592. else
  593. {
  594. return !node.hasBonusOfType(static_cast<Bonus::BonusType>(type));
  595. }
  596. }
  597. IPropagator::~IPropagator()
  598. {
  599. }
  600. CBonusSystemNode * IPropagator::getDestNode(CBonusSystemNode *source)
  601. {
  602. return source;
  603. }
  604. CreatureNativeTerrainLimiter::CreatureNativeTerrainLimiter(int TerrainType)
  605. : terrainType(TerrainType)
  606. {
  607. }
  608. CreatureNativeTerrainLimiter::CreatureNativeTerrainLimiter()
  609. {
  610. }
  611. bool CreatureNativeTerrainLimiter::limit(const Bonus *b, const CBonusSystemNode &node) const
  612. {
  613. const CCreature *c = retrieveCreature(&node);
  614. return !c || VLC->heroh->nativeTerrains[c->faction] != terrainType; //drop bonus for non-creatures or non-native residents
  615. }
  616. CreatureFactionLimiter::CreatureFactionLimiter(int Faction)
  617. : faction(Faction)
  618. {
  619. }
  620. CreatureFactionLimiter::CreatureFactionLimiter()
  621. {
  622. }
  623. bool CreatureFactionLimiter::limit(const Bonus *b, const CBonusSystemNode &node) const
  624. {
  625. const CCreature *c = retrieveCreature(&node);
  626. return !c || c->faction != faction; //drop bonus for non-creatures or non-native residents
  627. }
  628. CreatureAlignmentLimiter::CreatureAlignmentLimiter()
  629. {
  630. }
  631. CreatureAlignmentLimiter::CreatureAlignmentLimiter(si8 Alignment)
  632. : alignment(Alignment)
  633. {
  634. }
  635. bool CreatureAlignmentLimiter::limit(const Bonus *b, const CBonusSystemNode &node) const
  636. {
  637. const CCreature *c = retrieveCreature(&node);
  638. if(!c)
  639. return true;
  640. switch(alignment)
  641. {
  642. case GOOD:
  643. return !c->isGood(); //if not good -> return true (drop bonus)
  644. case NEUTRAL:
  645. return c->isEvil() || c->isGood();
  646. case EVIL:
  647. return !c->isEvil();
  648. default:
  649. tlog1 << "Warning: illegal alignment in limiter!\n";
  650. return true;
  651. }
  652. }