HeroBonus.cpp 19 KB

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