HeroBonus.cpp 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374
  1. #include "StdInc.h"
  2. #include "HeroBonus.h"
  3. #include "VCMI_Lib.h"
  4. #include "CSpellHandler.h"
  5. #include "CCreatureHandler.h"
  6. #include "CCreatureSet.h"
  7. #include "CHeroHandler.h"
  8. #include "CGeneralTextHandler.h"
  9. #include "BattleState.h"
  10. #include "CArtHandler.h"
  11. #include "GameConstants.h"
  12. #define FOREACH_PARENT(pname) TNodes lparents; getParents(lparents); BOOST_FOREACH(CBonusSystemNode *pname, lparents)
  13. #define FOREACH_CPARENT(pname) TCNodes lparents; getParents(lparents); BOOST_FOREACH(const CBonusSystemNode *pname, lparents)
  14. #define FOREACH_RED_CHILD(pname) TNodes lchildren; getRedChildren(lchildren); BOOST_FOREACH(CBonusSystemNode *pname, lchildren)
  15. #define FOREACH_RED_PARENT(pname) TNodes lparents; getRedParents(lparents); BOOST_FOREACH(CBonusSystemNode *pname, lparents)
  16. #define BONUS_NAME(x) ( #x, Bonus::x )
  17. DLL_LINKAGE const std::map<std::string, int> bonusNameMap = boost::assign::map_list_of BONUS_LIST;
  18. #undef BONUS_NAME
  19. #define BONUS_LOG_LINE(x) tlog5 << x << std::endl
  20. int CBonusSystemNode::treeChanged = 1;
  21. const bool CBonusSystemNode::cachingEnabled = true;
  22. BonusList::BonusList(bool BelongsToTree /* =false */) : belongsToTree(BelongsToTree)
  23. {
  24. }
  25. BonusList::BonusList(const BonusList &bonusList)
  26. {
  27. bonuses.resize(bonusList.size());
  28. std::copy(bonusList.begin(), bonusList.end(), bonuses.begin());
  29. belongsToTree = false;
  30. }
  31. BonusList& BonusList::operator=(const BonusList &bonusList)
  32. {
  33. bonuses.resize(bonusList.size());
  34. std::copy(bonusList.begin(), bonusList.end(), bonuses.begin());
  35. belongsToTree = false;
  36. return *this;
  37. }
  38. int BonusList::totalValue() const
  39. {
  40. int base = 0;
  41. int percentToBase = 0;
  42. int percentToAll = 0;
  43. int additive = 0;
  44. int indepMax = 0;
  45. bool hasIndepMax = false;
  46. int indepMin = 0;
  47. bool hasIndepMin = false;
  48. for (size_t i = 0; i < bonuses.size(); i++)
  49. {
  50. Bonus *b = bonuses[i];
  51. switch(b->valType)
  52. {
  53. case Bonus::BASE_NUMBER:
  54. base += b->val;
  55. break;
  56. case Bonus::PERCENT_TO_ALL:
  57. percentToAll += b->val;
  58. break;
  59. case Bonus::PERCENT_TO_BASE:
  60. percentToBase += b->val;
  61. break;
  62. case Bonus::ADDITIVE_VALUE:
  63. additive += b->val;
  64. break;
  65. case Bonus::INDEPENDENT_MAX:
  66. if (!hasIndepMax)
  67. {
  68. indepMax = b->val;
  69. hasIndepMax = true;
  70. }
  71. else
  72. {
  73. vstd::amax(indepMax, b->val);
  74. }
  75. break;
  76. case Bonus::INDEPENDENT_MIN:
  77. if (!hasIndepMin)
  78. {
  79. indepMin = b->val;
  80. hasIndepMin = true;
  81. }
  82. else
  83. {
  84. vstd::amin(indepMin, b->val);
  85. }
  86. break;
  87. }
  88. }
  89. int modifiedBase = base + (base * percentToBase) / 100;
  90. modifiedBase += additive;
  91. int valFirst = (modifiedBase * (100 + percentToAll)) / 100;
  92. if(hasIndepMin && hasIndepMax)
  93. assert(indepMin < indepMax);
  94. if (hasIndepMax)
  95. vstd::amax(valFirst, indepMax);
  96. if (hasIndepMin)
  97. vstd::amin(valFirst, indepMin);
  98. return valFirst;
  99. }
  100. const Bonus * BonusList::getFirst(const CSelector &selector) const
  101. {
  102. for (ui32 i = 0; i < bonuses.size(); i++)
  103. {
  104. const Bonus *b = bonuses[i];
  105. if(selector(b))
  106. return &*b;
  107. }
  108. return NULL;
  109. }
  110. Bonus * BonusList::getFirst(const CSelector &select)
  111. {
  112. for (ui32 i = 0; i < bonuses.size(); i++)
  113. {
  114. Bonus *b = bonuses[i];
  115. if(select(b))
  116. return &*b;
  117. }
  118. return NULL;
  119. }
  120. void BonusList::getModifiersWDescr(TModDescr &out) const
  121. {
  122. for (size_t i = 0; i < bonuses.size(); i++)
  123. {
  124. Bonus *b = bonuses[i];
  125. out.push_back(std::make_pair(b->val, b->Description()));
  126. }
  127. }
  128. void BonusList::getBonuses(BonusList & out, const CSelector &selector) const
  129. {
  130. // BOOST_FOREACH(Bonus *i, *this)
  131. // if(selector(i) && i->effectRange == Bonus::NO_LIMIT)
  132. // out.push_back(i);
  133. getBonuses(out, selector, 0);
  134. }
  135. void BonusList::getBonuses(BonusList & out, const CSelector &selector, const CSelector &limit) const
  136. {
  137. for (ui32 i = 0; i < bonuses.size(); i++)
  138. {
  139. Bonus *b = bonuses[i];
  140. //add matching bonuses that matches limit predicate or have NO_LIMIT if no given predicate
  141. if(selector(b) && ((!limit && b->effectRange == Bonus::NO_LIMIT) || (limit && limit(b))))
  142. out.push_back(b);
  143. }
  144. }
  145. void BonusList::getAllBonuses(BonusList &out) const
  146. {
  147. BOOST_FOREACH(Bonus *b, bonuses)
  148. out.push_back(b);
  149. }
  150. int BonusList::valOfBonuses(const CSelector &select) const
  151. {
  152. BonusList ret;
  153. CSelector limit = 0;
  154. getBonuses(ret, select, limit);
  155. ret.eliminateDuplicates();
  156. return ret.totalValue();
  157. }
  158. // void BonusList::limit(const CBonusSystemNode &node)
  159. // {
  160. // remove_if(boost::bind(&CBonusSystemNode::isLimitedOnUs, boost::ref(node), _1));
  161. // }
  162. void BonusList::eliminateDuplicates()
  163. {
  164. sort( bonuses.begin(), bonuses.end() );
  165. bonuses.erase( unique( bonuses.begin(), bonuses.end() ), bonuses.end() );
  166. }
  167. void BonusList::push_back(Bonus* const &x)
  168. {
  169. bonuses.push_back(x);
  170. if (belongsToTree)
  171. CBonusSystemNode::incrementTreeChangedNum();
  172. }
  173. std::vector<Bonus*>::iterator BonusList::erase(const int position)
  174. {
  175. if (belongsToTree)
  176. CBonusSystemNode::incrementTreeChangedNum();
  177. return bonuses.erase(bonuses.begin() + position);
  178. }
  179. void BonusList::clear()
  180. {
  181. bonuses.clear();
  182. if (belongsToTree)
  183. CBonusSystemNode::incrementTreeChangedNum();
  184. }
  185. std::vector<BonusList*>::size_type BonusList::operator-=(Bonus* const &i)
  186. {
  187. std::vector<Bonus*>::iterator itr = std::find(bonuses.begin(), bonuses.end(), i);
  188. if(itr == bonuses.end())
  189. return false;
  190. bonuses.erase(itr);
  191. if (belongsToTree)
  192. CBonusSystemNode::incrementTreeChangedNum();
  193. return true;
  194. }
  195. void BonusList::resize(std::vector<Bonus*>::size_type sz, Bonus* c )
  196. {
  197. bonuses.resize(sz, c);
  198. if (belongsToTree)
  199. CBonusSystemNode::incrementTreeChangedNum();
  200. }
  201. void BonusList::insert(std::vector<Bonus*>::iterator position, std::vector<Bonus*>::size_type n, Bonus* const &x)
  202. {
  203. bonuses.insert(position, n, x);
  204. if (belongsToTree)
  205. CBonusSystemNode::incrementTreeChangedNum();
  206. }
  207. int IBonusBearer::valOfBonuses(Bonus::BonusType type, const CSelector &selector) const
  208. {
  209. return valOfBonuses(Selector::type(type) && selector);
  210. }
  211. int IBonusBearer::valOfBonuses(Bonus::BonusType type, int subtype /*= -1*/) const
  212. {
  213. std::stringstream cachingStr;
  214. cachingStr << "type_" << type << "s_" << subtype;
  215. CSelector s = Selector::type(type);
  216. if(subtype != -1)
  217. s = s && Selector::subtype(subtype);
  218. return valOfBonuses(s, cachingStr.str());
  219. }
  220. int IBonusBearer::valOfBonuses(const CSelector &selector, const std::string &cachingStr) const
  221. {
  222. CSelector limit = 0;
  223. TBonusListPtr hlp = getAllBonuses(selector, limit, NULL, cachingStr);
  224. return hlp->totalValue();
  225. }
  226. bool IBonusBearer::hasBonus(const CSelector &selector, const std::string &cachingStr /*= ""*/) const
  227. {
  228. return getBonuses(selector, cachingStr)->size() > 0;
  229. }
  230. bool IBonusBearer::hasBonusOfType(Bonus::BonusType type, int subtype /*= -1*/) const
  231. {
  232. std::stringstream cachingStr;
  233. cachingStr << "type_" << type << "s_" << subtype;
  234. CSelector s = Selector::type(type);
  235. if(subtype != -1)
  236. s = s && Selector::subtype(subtype);
  237. return hasBonus(s, cachingStr.str());
  238. }
  239. void IBonusBearer::getModifiersWDescr(TModDescr &out, Bonus::BonusType type, int subtype /*= -1 */) const
  240. {
  241. std::stringstream cachingStr;
  242. cachingStr << "type_" << type << "s_" << subtype;
  243. getModifiersWDescr(out, subtype != -1 ? Selector::typeSubtype(type, subtype) : Selector::type(type), cachingStr.str());
  244. }
  245. void IBonusBearer::getModifiersWDescr(TModDescr &out, const CSelector &selector, const std::string &cachingStr /* =""*/) const
  246. {
  247. getBonuses(selector, cachingStr)->getModifiersWDescr(out);
  248. }
  249. int IBonusBearer::getBonusesCount(int from, int id) const
  250. {
  251. std::stringstream cachingStr;
  252. cachingStr << "source_" << from << "id_" << id;
  253. return getBonusesCount(Selector::source(from, id), cachingStr.str());
  254. }
  255. int IBonusBearer::getBonusesCount(const CSelector &selector, const std::string &cachingStr /* =""*/) const
  256. {
  257. return getBonuses(selector, cachingStr)->size();
  258. }
  259. const TBonusListPtr IBonusBearer::getBonuses(const CSelector &selector, const std::string &cachingStr /*= ""*/) const
  260. {
  261. return getAllBonuses(selector, 0, NULL, cachingStr);
  262. }
  263. const TBonusListPtr IBonusBearer::getBonuses(const CSelector &selector, const CSelector &limit, const std::string &cachingStr /*= ""*/) const
  264. {
  265. return getAllBonuses(selector, limit, NULL, cachingStr);
  266. }
  267. bool IBonusBearer::hasBonusFrom(ui8 source, ui32 sourceID) const
  268. {
  269. std::stringstream cachingStr;
  270. cachingStr << "source_" << source << "id_" << sourceID;
  271. return hasBonus(Selector::source(source,sourceID), cachingStr.str());
  272. }
  273. int IBonusBearer::MoraleVal() const
  274. {
  275. if(hasBonusOfType(Bonus::NON_LIVING) || hasBonusOfType(Bonus::UNDEAD) ||
  276. hasBonusOfType(Bonus::NO_MORALE) || hasBonusOfType(Bonus::SIEGE_WEAPON))
  277. return 0;
  278. int ret = valOfBonuses(Bonus::MORALE);
  279. if(hasBonusOfType(Bonus::SELF_MORALE)) //eg. minotaur
  280. vstd::amax(ret, +1);
  281. return vstd::abetween(ret, -3, +3);
  282. }
  283. int IBonusBearer::LuckVal() const
  284. {
  285. if(hasBonusOfType(Bonus::NO_LUCK))
  286. return 0;
  287. int ret = valOfBonuses(Bonus::LUCK);
  288. if(hasBonusOfType(Bonus::SELF_LUCK)) //eg. halfling
  289. vstd::amax(ret, +1);
  290. return vstd::abetween(ret, -3, +3);
  291. }
  292. si32 IBonusBearer::Attack() const
  293. {
  294. si32 ret = valOfBonuses(Bonus::PRIMARY_SKILL, PrimarySkill::ATTACK);
  295. if (int frenzyPower = valOfBonuses(Bonus::IN_FRENZY)) //frenzy for attacker
  296. {
  297. ret += frenzyPower * Defense(false);
  298. }
  299. vstd::amax(ret, 0);
  300. return ret;
  301. }
  302. si32 IBonusBearer::Defense(bool withFrenzy /*= true*/) const
  303. {
  304. si32 ret = valOfBonuses(Bonus::PRIMARY_SKILL, PrimarySkill::DEFENSE);
  305. if(withFrenzy && hasBonusOfType(Bonus::IN_FRENZY)) //frenzy for defender
  306. {
  307. return 0;
  308. }
  309. vstd::amax(ret, 0);
  310. return ret;
  311. }
  312. ui32 IBonusBearer::MaxHealth() const
  313. {
  314. return std::max(1, valOfBonuses(Bonus::STACK_HEALTH)); //never 0
  315. }
  316. ui32 IBonusBearer::getMinDamage() const
  317. {
  318. std::stringstream cachingStr;
  319. cachingStr << "type_" << Bonus::CREATURE_DAMAGE << "s_0Otype_" << Bonus::CREATURE_DAMAGE << "s_1";
  320. return valOfBonuses(Selector::typeSubtype(Bonus::CREATURE_DAMAGE, 0) || Selector::typeSubtype(Bonus::CREATURE_DAMAGE, 1), cachingStr.str());
  321. }
  322. ui32 IBonusBearer::getMaxDamage() const
  323. {
  324. std::stringstream cachingStr;
  325. cachingStr << "type_" << Bonus::CREATURE_DAMAGE << "s_0Otype_" << Bonus::CREATURE_DAMAGE << "s_2";
  326. return valOfBonuses(Selector::typeSubtype(Bonus::CREATURE_DAMAGE, 0) || Selector::typeSubtype(Bonus::CREATURE_DAMAGE, 2), cachingStr.str());
  327. }
  328. si32 IBonusBearer::manaLimit() const
  329. {
  330. return si32(getPrimSkillLevel(3) * (100.0 + valOfBonuses(Bonus::SECONDARY_SKILL_PREMY, 24)) / 10.0);
  331. }
  332. int IBonusBearer::getPrimSkillLevel(int id) const
  333. {
  334. int ret = 0;
  335. if(id == PrimarySkill::ATTACK)
  336. ret = Attack();
  337. else if(id == PrimarySkill::DEFENSE)
  338. ret = Defense();
  339. else
  340. ret = valOfBonuses(Bonus::PRIMARY_SKILL, id);
  341. vstd::amax(ret, id/2); //minimal value is 0 for attack and defense and 1 for spell power and knowledge
  342. return ret;
  343. }
  344. si32 IBonusBearer::magicResistance() const
  345. {
  346. return valOfBonuses(Bonus::MAGIC_RESISTANCE);
  347. }
  348. bool IBonusBearer::isLiving() const //TODO: theoreticaly there exists "LIVING" bonus in stack experience documentation
  349. {
  350. std::stringstream cachingStr;
  351. cachingStr << "type_" << Bonus::UNDEAD << "s_-1Otype_" << Bonus::NON_LIVING << "s_-11type_" << Bonus::SIEGE_WEAPON; //I don't relaly get what string labels mean?
  352. return(!hasBonus(Selector::type(Bonus::UNDEAD) || Selector::type(Bonus::NON_LIVING) || Selector::type(Bonus::SIEGE_WEAPON), cachingStr.str()));
  353. }
  354. const TBonusListPtr IBonusBearer::getSpellBonuses() const
  355. {
  356. std::stringstream cachingStr;
  357. cachingStr << "source_" << Bonus::SPELL_EFFECT;
  358. return getBonuses(Selector::sourceType(Bonus::SPELL_EFFECT), cachingStr.str());
  359. }
  360. Bonus * CBonusSystemNode::getBonus(const CSelector &selector)
  361. {
  362. Bonus *ret = bonuses.getFirst(selector);
  363. if(ret)
  364. return ret;
  365. FOREACH_PARENT(pname)
  366. {
  367. ret = pname->getBonus(selector);
  368. if (ret)
  369. return ret;
  370. }
  371. return NULL;
  372. }
  373. const Bonus * CBonusSystemNode::getBonus( const CSelector &selector ) const
  374. {
  375. return (const_cast<CBonusSystemNode*>(this))->getBonus(selector);
  376. }
  377. void CBonusSystemNode::getParents(TCNodes &out) const /*retreives list of parent nodes (nodes to inherit bonuses from) */
  378. {
  379. for (ui32 i = 0; i < parents.size(); i++)
  380. {
  381. const CBonusSystemNode *parent = parents[i];
  382. out.insert(parent);
  383. }
  384. }
  385. void CBonusSystemNode::getParents(TNodes &out)
  386. {
  387. for (ui32 i = 0; i < parents.size(); i++)
  388. {
  389. const CBonusSystemNode *parent = parents[i];
  390. out.insert(const_cast<CBonusSystemNode*>(parent));
  391. }
  392. }
  393. void CBonusSystemNode::getBonusesRec(BonusList &out, const CSelector &selector, const CSelector &limit) const
  394. {
  395. FOREACH_CPARENT(p)
  396. {
  397. p->getBonusesRec(out, selector, limit);
  398. }
  399. bonuses.getBonuses(out, selector, limit);
  400. }
  401. void CBonusSystemNode::getAllBonusesRec(BonusList &out) const
  402. {
  403. FOREACH_CPARENT(p)
  404. {
  405. p->getAllBonusesRec(out);
  406. }
  407. bonuses.getAllBonuses(out);
  408. }
  409. const TBonusListPtr CBonusSystemNode::getAllBonuses(const CSelector &selector, const CSelector &limit, const CBonusSystemNode *root /*= NULL*/, const std::string &cachingStr /*= ""*/) const
  410. {
  411. bool limitOnUs = (!root || root == this); //caching won't work when we want to limit bonuses against an external node
  412. if (CBonusSystemNode::cachingEnabled && limitOnUs)
  413. {
  414. // Exclusive access for one thread
  415. static boost::mutex m;
  416. boost::mutex::scoped_lock lock(m);
  417. // If the bonus system tree changes(state of a single node or the relations to each other) then
  418. // cache all bonus objects. Selector objects doesn't matter.
  419. if (cachedLast != treeChanged)
  420. {
  421. cachedBonuses.clear();
  422. cachedRequests.clear();
  423. BonusList allBonuses;
  424. getAllBonusesRec(allBonuses);
  425. allBonuses.eliminateDuplicates();
  426. limitBonuses(allBonuses, cachedBonuses);
  427. cachedLast = treeChanged;
  428. }
  429. // If a bonus system request comes with a caching string then look up in the map if there are any
  430. // pre-calculated bonus results. Limiters can't be cached so they have to be calculated.
  431. if (cachingStr != "")
  432. {
  433. auto it = cachedRequests.find(cachingStr);
  434. if(it != cachedRequests.end())
  435. {
  436. //Cached list contains bonuses for our query with applied limiters
  437. return it->second;
  438. }
  439. }
  440. //We still don't have the bonuses (didn't returned them from cache)
  441. //Perform bonus selection
  442. auto ret = make_shared<BonusList>();
  443. cachedBonuses.getBonuses(*ret, selector, limit);
  444. // Save the results in the cache
  445. if(cachingStr != "")
  446. cachedRequests[cachingStr] = ret;
  447. return ret;
  448. }
  449. else
  450. {
  451. return getAllBonusesWithoutCaching(selector, limit, root);
  452. }
  453. }
  454. const TBonusListPtr CBonusSystemNode::getAllBonusesWithoutCaching(const CSelector &selector, const CSelector &limit, const CBonusSystemNode *root /*= NULL*/) const
  455. {
  456. auto ret = make_shared<BonusList>();
  457. // Get bonus results without caching enabled.
  458. BonusList beforeLimiting, afterLimiting;
  459. getAllBonusesRec(beforeLimiting);
  460. beforeLimiting.eliminateDuplicates();
  461. if(!root || root == this)
  462. {
  463. limitBonuses(beforeLimiting, afterLimiting);
  464. afterLimiting.getBonuses(*ret, selector, limit);
  465. }
  466. else if(root)
  467. {
  468. //We want to limit our query against an external node. We get all its bonuses,
  469. // add the ones we're considering and see if they're cut out by limiters
  470. BonusList rootBonuses, limitedRootBonuses;
  471. getAllBonusesRec(rootBonuses);
  472. BOOST_FOREACH(Bonus *b, beforeLimiting)
  473. rootBonuses.push_back(b);
  474. rootBonuses.eliminateDuplicates();
  475. root->limitBonuses(rootBonuses, limitedRootBonuses);
  476. BOOST_FOREACH(Bonus *b, beforeLimiting)
  477. if(vstd::contains(limitedRootBonuses, b))
  478. afterLimiting.push_back(b);
  479. afterLimiting.getBonuses(*ret, selector, limit);
  480. }
  481. else
  482. beforeLimiting.getBonuses(*ret, selector, limit);
  483. return ret;
  484. }
  485. CBonusSystemNode::CBonusSystemNode() : bonuses(true), exportedBonuses(true), nodeType(UNKNOWN), cachedLast(0)
  486. {
  487. }
  488. CBonusSystemNode::~CBonusSystemNode()
  489. {
  490. detachFromAll();
  491. if(children.size())
  492. {
  493. tlog2 << "Warning: an orphaned child!\n";
  494. while(children.size())
  495. children.front()->detachFrom(this);
  496. }
  497. BOOST_FOREACH(Bonus *b, exportedBonuses)
  498. delete b;
  499. }
  500. void CBonusSystemNode::attachTo(CBonusSystemNode *parent)
  501. {
  502. assert(!vstd::contains(parents, parent));
  503. parents.push_back(parent);
  504. if(parent->actsAsBonusSourceOnly())
  505. parent->newRedDescendant(this);
  506. else
  507. newRedDescendant(parent);
  508. parent->newChildAttached(this);
  509. CBonusSystemNode::treeChanged++;
  510. }
  511. void CBonusSystemNode::detachFrom(CBonusSystemNode *parent)
  512. {
  513. assert(vstd::contains(parents, parent));
  514. if(parent->actsAsBonusSourceOnly())
  515. parent->removedRedDescendant(this);
  516. else
  517. removedRedDescendant(parent);
  518. parents -= parent;
  519. parent->childDetached(this);
  520. CBonusSystemNode::treeChanged++;
  521. }
  522. void CBonusSystemNode::popBonuses(const CSelector &s)
  523. {
  524. BonusList bl;
  525. exportedBonuses.getBonuses(bl, s);
  526. BOOST_FOREACH(Bonus *b, bl)
  527. removeBonus(b);
  528. BOOST_FOREACH(CBonusSystemNode *child, children)
  529. child->popBonuses(s);
  530. }
  531. // void CBonusSystemNode::addNewBonus(const Bonus &b)
  532. // {
  533. // addNewBonus(new Bonus(b));
  534. // }
  535. void CBonusSystemNode::addNewBonus(Bonus *b)
  536. {
  537. assert(!vstd::contains(exportedBonuses,b));
  538. exportedBonuses.push_back(b);
  539. exportBonus(b);
  540. CBonusSystemNode::treeChanged++;
  541. }
  542. void CBonusSystemNode::removeBonus(Bonus *b)
  543. {
  544. exportedBonuses -= b;
  545. if(b->propagator)
  546. unpropagateBonus(b);
  547. else
  548. bonuses -= b;
  549. vstd::clear_pointer(b);
  550. CBonusSystemNode::treeChanged++;
  551. }
  552. bool CBonusSystemNode::actsAsBonusSourceOnly() const
  553. {
  554. switch(nodeType)
  555. {
  556. case CREATURE:
  557. case ARTIFACT:
  558. case ARTIFACT_INSTANCE:
  559. return true;
  560. default:
  561. return false;
  562. }
  563. }
  564. void CBonusSystemNode::propagateBonus(Bonus * b)
  565. {
  566. if(b->propagator->shouldBeAttached(this))
  567. {
  568. bonuses.push_back(b);
  569. BONUS_LOG_LINE("#$# " << b->Description() << " #propagated to# " << nodeName());
  570. }
  571. FOREACH_RED_CHILD(child)
  572. child->propagateBonus(b);
  573. }
  574. void CBonusSystemNode::unpropagateBonus(Bonus * b)
  575. {
  576. if(b->propagator->shouldBeAttached(this))
  577. {
  578. bonuses -= b;
  579. BONUS_LOG_LINE("#$#" << b->Description() << " #is no longer propagated to# " << nodeName());
  580. }
  581. FOREACH_RED_CHILD(child)
  582. child->unpropagateBonus(b);
  583. }
  584. void CBonusSystemNode::newChildAttached(CBonusSystemNode *child)
  585. {
  586. assert(!vstd::contains(children, child));
  587. children.push_back(child);
  588. BONUS_LOG_LINE(child->nodeName() << " #attached to# " << nodeName());
  589. }
  590. void CBonusSystemNode::childDetached(CBonusSystemNode *child)
  591. {
  592. assert(vstd::contains(children, child));
  593. children -= child;
  594. BONUS_LOG_LINE(child->nodeName() << " #detached from# " << nodeName());
  595. }
  596. void CBonusSystemNode::detachFromAll()
  597. {
  598. while(parents.size())
  599. detachFrom(parents.front());
  600. }
  601. bool CBonusSystemNode::isIndependentNode() const
  602. {
  603. return parents.empty() && children.empty();
  604. }
  605. std::string CBonusSystemNode::nodeName() const
  606. {
  607. return description.size()
  608. ? description
  609. : std::string("Bonus system node of type ") + typeid(*this).name();
  610. }
  611. void CBonusSystemNode::deserializationFix()
  612. {
  613. exportBonuses();
  614. }
  615. void CBonusSystemNode::getRedParents(TNodes &out)
  616. {
  617. FOREACH_PARENT(pname)
  618. {
  619. if(pname->actsAsBonusSourceOnly())
  620. {
  621. out.insert(pname);
  622. }
  623. }
  624. if(!actsAsBonusSourceOnly())
  625. {
  626. BOOST_FOREACH(CBonusSystemNode *child, children)
  627. {
  628. out.insert(child);
  629. }
  630. }
  631. }
  632. void CBonusSystemNode::getRedChildren(TNodes &out)
  633. {
  634. FOREACH_PARENT(pname)
  635. {
  636. if(!pname->actsAsBonusSourceOnly())
  637. {
  638. out.insert(pname);
  639. }
  640. }
  641. if(actsAsBonusSourceOnly())
  642. {
  643. BOOST_FOREACH(CBonusSystemNode *child, children)
  644. {
  645. out.insert(child);
  646. }
  647. }
  648. }
  649. void CBonusSystemNode::newRedDescendant(CBonusSystemNode *descendant)
  650. {
  651. BOOST_FOREACH(Bonus *b, exportedBonuses)
  652. if(b->propagator)
  653. descendant->propagateBonus(b);
  654. FOREACH_RED_PARENT(parent)
  655. parent->newRedDescendant(descendant);
  656. }
  657. void CBonusSystemNode::removedRedDescendant(CBonusSystemNode *descendant)
  658. {
  659. BOOST_FOREACH(Bonus *b, exportedBonuses)
  660. if(b->propagator)
  661. descendant->unpropagateBonus(b);
  662. FOREACH_RED_PARENT(parent)
  663. parent->removedRedDescendant(descendant);
  664. }
  665. void CBonusSystemNode::getRedAncestors(TNodes &out)
  666. {
  667. getRedParents(out);
  668. FOREACH_RED_PARENT(p)
  669. p->getRedAncestors(out);
  670. }
  671. void CBonusSystemNode::getRedDescendants(TNodes &out)
  672. {
  673. getRedChildren(out);
  674. FOREACH_RED_CHILD(c)
  675. c->getRedChildren(out);
  676. }
  677. void CBonusSystemNode::battleTurnPassed()
  678. {
  679. BonusList bonusesCpy = exportedBonuses; //copy, because removing bonuses invalidates iters
  680. for (ui32 i = 0; i < bonusesCpy.size(); i++)
  681. {
  682. Bonus *b = bonusesCpy[i];
  683. if(b->duration & Bonus::N_TURNS)
  684. {
  685. b->turnsRemain--;
  686. if(b->turnsRemain <= 0)
  687. removeBonus(b);
  688. }
  689. }
  690. }
  691. void CBonusSystemNode::exportBonus(Bonus * b)
  692. {
  693. if(b->propagator)
  694. propagateBonus(b);
  695. else
  696. bonuses.push_back(b);
  697. CBonusSystemNode::treeChanged++;
  698. }
  699. void CBonusSystemNode::exportBonuses()
  700. {
  701. BOOST_FOREACH(Bonus *b, exportedBonuses)
  702. exportBonus(b);
  703. }
  704. const ui8 CBonusSystemNode::getNodeType() const
  705. {
  706. return nodeType;
  707. }
  708. BonusList& CBonusSystemNode::getBonusList()
  709. {
  710. return bonuses;
  711. }
  712. const BonusList& CBonusSystemNode::getBonusList() const
  713. {
  714. return bonuses;
  715. }
  716. const TNodesVector& CBonusSystemNode::getParentNodes() const
  717. {
  718. return parents;
  719. }
  720. const TNodesVector& CBonusSystemNode::getChildrenNodes() const
  721. {
  722. return children;
  723. }
  724. void CBonusSystemNode::setNodeType(ui8 type)
  725. {
  726. nodeType = type;
  727. }
  728. BonusList& CBonusSystemNode::getExportedBonusList()
  729. {
  730. return exportedBonuses;
  731. }
  732. const std::string& CBonusSystemNode::getDescription() const
  733. {
  734. return description;
  735. }
  736. void CBonusSystemNode::setDescription(const std::string &description)
  737. {
  738. this->description = description;
  739. }
  740. void CBonusSystemNode::incrementTreeChangedNum()
  741. {
  742. treeChanged++;
  743. }
  744. void CBonusSystemNode::limitBonuses(const BonusList &allBonuses, BonusList &out) const
  745. {
  746. assert(&allBonuses != &out); //todo should it work in-place?
  747. BonusList undecided = allBonuses,
  748. &accepted = out;
  749. while(true)
  750. {
  751. int undecidedCount = undecided.size();
  752. for(int i = 0; i < undecided.size(); i++)
  753. {
  754. Bonus *b = undecided[i];
  755. BonusLimitationContext context = {b, *this, out};
  756. int decision = b->limiter ? b->limiter->limit(context) : ILimiter::ACCEPT; //bonuses without limiters will be accepted by default
  757. if(decision == ILimiter::DISCARD)
  758. {
  759. undecided.erase(i);
  760. i--; continue;
  761. }
  762. else if(decision == ILimiter::ACCEPT)
  763. {
  764. accepted.push_back(b);
  765. undecided.erase(i);
  766. i--; continue;
  767. }
  768. else
  769. assert(decision == ILimiter::NOT_SURE);
  770. }
  771. if(undecided.size() == undecidedCount) //we haven't moved a single bonus -> limiters reached a stable state
  772. return;
  773. }
  774. }
  775. TBonusListPtr CBonusSystemNode::limitBonuses(const BonusList &allBonuses) const
  776. {
  777. auto ret = make_shared<BonusList>();
  778. limitBonuses(allBonuses, *ret);
  779. return ret;
  780. }
  781. void CBonusSystemNode::treeHasChanged()
  782. {
  783. treeChanged++;
  784. }
  785. int NBonus::valOf(const CBonusSystemNode *obj, Bonus::BonusType type, int subtype /*= -1*/)
  786. {
  787. if(obj)
  788. return obj->valOfBonuses(type, subtype);
  789. return 0;
  790. }
  791. bool NBonus::hasOfType(const CBonusSystemNode *obj, Bonus::BonusType type, int subtype /*= -1*/)
  792. {
  793. if(obj)
  794. return obj->hasBonusOfType(type, subtype);
  795. return false;
  796. }
  797. void NBonus::getModifiersWDescr(const CBonusSystemNode *obj, TModDescr &out, Bonus::BonusType type, int subtype /*= -1 */)
  798. {
  799. if(obj)
  800. return obj->getModifiersWDescr(out, type, subtype);
  801. }
  802. int NBonus::getCount(const CBonusSystemNode *obj, int from, int id)
  803. {
  804. if(obj)
  805. return obj->getBonusesCount(from, id);
  806. return 0;
  807. }
  808. const CSpell * Bonus::sourceSpell() const
  809. {
  810. if(source == SPELL_EFFECT)
  811. return VLC->spellh->spells[sid];
  812. return NULL;
  813. }
  814. std::string Bonus::Description() const
  815. {
  816. if(description.size())
  817. return description;
  818. std::ostringstream str;
  819. str << std::showpos << val << " ";
  820. switch(source)
  821. {
  822. case ARTIFACT:
  823. str << VLC->arth->artifacts[sid]->Name();
  824. break;;
  825. case SPELL_EFFECT:
  826. str << VLC->spellh->spells[sid]->name;
  827. break;
  828. case CREATURE_ABILITY:
  829. str << VLC->creh->creatures[sid]->namePl;
  830. break;
  831. case SECONDARY_SKILL:
  832. str << VLC->generaltexth->skillName[sid]/* << " secondary skill"*/;
  833. break;
  834. }
  835. return str.str();
  836. }
  837. Bonus::Bonus(ui16 Dur, ui8 Type, ui8 Src, si32 Val, ui32 ID, std::string Desc, si32 Subtype/*=-1*/)
  838. : duration(Dur), type(Type), subtype(Subtype), source(Src), val(Val), sid(ID), description(Desc)
  839. {
  840. additionalInfo = -1;
  841. turnsRemain = 0;
  842. valType = ADDITIVE_VALUE;
  843. effectRange = NO_LIMIT;
  844. boost::algorithm::trim(description);
  845. }
  846. Bonus::Bonus(ui16 Dur, ui8 Type, ui8 Src, si32 Val, ui32 ID, si32 Subtype/*=-1*/, ui8 ValType /*= ADDITIVE_VALUE*/)
  847. : duration(Dur), type(Type), subtype(Subtype), source(Src), val(Val), sid(ID), valType(ValType)
  848. {
  849. additionalInfo = -1;
  850. turnsRemain = 0;
  851. effectRange = NO_LIMIT;
  852. }
  853. Bonus::Bonus()
  854. {
  855. subtype = -1;
  856. additionalInfo = -1;
  857. turnsRemain = 0;
  858. valType = ADDITIVE_VALUE;
  859. effectRange = NO_LIMIT;
  860. }
  861. Bonus::~Bonus()
  862. {
  863. }
  864. Bonus * Bonus::addLimiter(TLimiterPtr Limiter)
  865. {
  866. limiter = Limiter;
  867. return this;
  868. }
  869. Bonus * Bonus::addPropagator(TPropagatorPtr Propagator)
  870. {
  871. propagator = Propagator;
  872. return this;
  873. }
  874. CSelector DLL_LINKAGE operator&&(const CSelector &first, const CSelector &second)
  875. {
  876. return CSelectorsConjunction(first, second);
  877. }
  878. CSelector DLL_LINKAGE operator||(const CSelector &first, const CSelector &second)
  879. {
  880. return CSelectorsAlternative(first, second);
  881. }
  882. namespace Selector
  883. {
  884. DLL_LINKAGE CSelectFieldEqual<TBonusType> type(&Bonus::type, 0);
  885. DLL_LINKAGE CSelectFieldEqual<TBonusSubtype> subtype(&Bonus::subtype, 0);
  886. DLL_LINKAGE CSelectFieldEqual<si32> info(&Bonus::additionalInfo, 0);
  887. DLL_LINKAGE CSelectFieldEqual<ui16> duration(&Bonus::duration, 0);
  888. DLL_LINKAGE CSelectFieldEqual<ui8> sourceType(&Bonus::source, 0);
  889. DLL_LINKAGE CSelectFieldEqual<ui8> effectRange(&Bonus::effectRange, Bonus::NO_LIMIT);
  890. DLL_LINKAGE CWillLastTurns turns;
  891. CSelector DLL_LINKAGE typeSubtype(TBonusType Type, TBonusSubtype Subtype)
  892. {
  893. return type(Type) && subtype(Subtype);
  894. }
  895. CSelector DLL_LINKAGE typeSubtypeInfo(TBonusType type, TBonusSubtype subtype, si32 info)
  896. {
  897. return CSelectFieldEqual<TBonusType>(&Bonus::type, type) && CSelectFieldEqual<TBonusSubtype>(&Bonus::subtype, subtype) && CSelectFieldEqual<si32>(&Bonus::additionalInfo, info);
  898. }
  899. CSelector DLL_LINKAGE source(ui8 source, ui32 sourceID)
  900. {
  901. return CSelectFieldEqual<ui8>(&Bonus::source, source) && CSelectFieldEqual<ui32>(&Bonus::sid, sourceID);
  902. }
  903. CSelector DLL_EXPORT durationType(ui16 duration)
  904. {
  905. return CSelectFieldEqual<ui16>(&Bonus::duration, duration);
  906. }
  907. CSelector DLL_LINKAGE sourceTypeSel(ui8 source)
  908. {
  909. return CSelectFieldEqual<ui8>(&Bonus::source, source);
  910. }
  911. bool DLL_LINKAGE matchesType(const CSelector &sel, TBonusType type)
  912. {
  913. Bonus dummy;
  914. dummy.type = type;
  915. return sel(&dummy);
  916. }
  917. bool DLL_LINKAGE matchesTypeSubtype(const CSelector &sel, TBonusType type, TBonusSubtype subtype)
  918. {
  919. Bonus dummy;
  920. dummy.type = type;
  921. dummy.subtype = subtype;
  922. return sel(&dummy);
  923. }
  924. bool DLL_LINKAGE positiveSpellEffects(const Bonus *b)
  925. {
  926. if(b->source == Bonus::SPELL_EFFECT)
  927. {
  928. CSpell *sp = VLC->spellh->spells[b->sid];
  929. return sp->isPositive();
  930. }
  931. return false; //not a spell effect
  932. }
  933. }
  934. const CStack * retreiveStackBattle(const CBonusSystemNode *node)
  935. {
  936. switch(node->getNodeType())
  937. {
  938. case CBonusSystemNode::STACK_BATTLE:
  939. return static_cast<const CStack*>(node);
  940. default:
  941. return NULL;
  942. }
  943. }
  944. const CStackInstance * retreiveStackInstance(const CBonusSystemNode *node)
  945. {
  946. switch(node->getNodeType())
  947. {
  948. case CBonusSystemNode::STACK_INSTANCE:
  949. return (static_cast<const CStackInstance *>(node));
  950. case CBonusSystemNode::STACK_BATTLE:
  951. return (static_cast<const CStack*>(node))->base;
  952. default:
  953. return NULL;
  954. }
  955. }
  956. const CCreature * retrieveCreature(const CBonusSystemNode *node)
  957. {
  958. switch(node->getNodeType())
  959. {
  960. case CBonusSystemNode::CREATURE:
  961. return (static_cast<const CCreature *>(node));
  962. default:
  963. const CStackInstance *csi = retreiveStackInstance(node);
  964. if(csi)
  965. return csi->type;
  966. return NULL;
  967. }
  968. }
  969. DLL_LINKAGE std::ostream & operator<<(std::ostream &out, const BonusList &bonusList)
  970. {
  971. for (ui32 i = 0; i < bonusList.size(); i++)
  972. {
  973. Bonus *b = bonusList[i];
  974. out << "Bonus " << i << "\n" << *b << std::endl;
  975. }
  976. return out;
  977. }
  978. DLL_LINKAGE std::ostream & operator<<(std::ostream &out, const Bonus &bonus)
  979. {
  980. for(std::map<std::string, int>::const_iterator i = bonusNameMap.begin(); i != bonusNameMap.end(); i++)
  981. if(i->second == bonus.type)
  982. out << "\tType: " << i->first << " \t";
  983. #define printField(field) out << "\t" #field ": " << (int)bonus.field << "\n"
  984. printField(val);
  985. printField(subtype);
  986. printField(duration);
  987. printField(source);
  988. printField(sid);
  989. printField(additionalInfo);
  990. printField(turnsRemain);
  991. printField(valType);
  992. printField(effectRange);
  993. #undef printField
  994. return out;
  995. }
  996. ILimiter::~ILimiter()
  997. {
  998. }
  999. int ILimiter::limit(const BonusLimitationContext &context) const /*return true to drop the bonus */
  1000. {
  1001. return false;
  1002. }
  1003. int CCreatureTypeLimiter::limit(const BonusLimitationContext &context) const
  1004. {
  1005. const CCreature *c = retrieveCreature(&context.node);
  1006. if(!c)
  1007. return true;
  1008. return c != creature && (!includeUpgrades || !creature->isMyUpgrade(c));
  1009. //drop bonus if it's not our creature and (we dont check upgrades or its not our upgrade)
  1010. }
  1011. CCreatureTypeLimiter::CCreatureTypeLimiter(const CCreature &Creature, ui8 IncludeUpgrades /*= true*/)
  1012. :creature(&Creature), includeUpgrades(IncludeUpgrades)
  1013. {
  1014. }
  1015. CCreatureTypeLimiter::CCreatureTypeLimiter()
  1016. {
  1017. creature = NULL;
  1018. includeUpgrades = false;
  1019. }
  1020. HasAnotherBonusLimiter::HasAnotherBonusLimiter( TBonusType bonus )
  1021. : type(bonus), subtype(0), isSubtypeRelevant(false)
  1022. {
  1023. }
  1024. HasAnotherBonusLimiter::HasAnotherBonusLimiter( TBonusType bonus, TBonusSubtype _subtype )
  1025. : type(bonus), subtype(_subtype), isSubtypeRelevant(true)
  1026. {
  1027. }
  1028. int HasAnotherBonusLimiter::limit(const BonusLimitationContext &context) const
  1029. {
  1030. CSelector mySelector = isSubtypeRelevant
  1031. ? Selector::typeSubtype(type, subtype)
  1032. : Selector::type(type);
  1033. //if we have a bonus of required type accepted, limiter should accept also this bonus
  1034. if(context.alreadyAccepted.getFirst(mySelector))
  1035. return ACCEPT;
  1036. //do not accept for now but it may change if more bonuses gets included
  1037. return NOT_SURE;
  1038. }
  1039. IPropagator::~IPropagator()
  1040. {
  1041. }
  1042. // CBonusSystemNode * IPropagator::getDestNode(CBonusSystemNode *source, CBonusSystemNode *redParent, CBonusSystemNode *redChild)
  1043. // {
  1044. // tlog1 << "IPropagator::getDestNode called!\n";
  1045. // return source;
  1046. // }
  1047. bool IPropagator::shouldBeAttached(CBonusSystemNode *dest)
  1048. {
  1049. return false;
  1050. }
  1051. // CBonusSystemNode * CPropagatorNodeType::getDestNode(CBonusSystemNode *source, CBonusSystemNode *redParent, CBonusSystemNode *redChild)
  1052. // {
  1053. // return NULL;
  1054. // }
  1055. CPropagatorNodeType::CPropagatorNodeType()
  1056. {
  1057. }
  1058. CPropagatorNodeType::CPropagatorNodeType(ui8 NodeType)
  1059. : nodeType(NodeType)
  1060. {
  1061. }
  1062. bool CPropagatorNodeType::shouldBeAttached(CBonusSystemNode *dest)
  1063. {
  1064. return nodeType == dest->getNodeType();
  1065. }
  1066. CreatureNativeTerrainLimiter::CreatureNativeTerrainLimiter(int TerrainType)
  1067. : terrainType(TerrainType)
  1068. {
  1069. }
  1070. CreatureNativeTerrainLimiter::CreatureNativeTerrainLimiter()
  1071. {
  1072. }
  1073. int CreatureNativeTerrainLimiter::limit(const BonusLimitationContext &context) const
  1074. {
  1075. const CCreature *c = retrieveCreature(&context.node);
  1076. return !c || !vstd::iswithin(c->faction, 0, 9) || VLC->heroh->nativeTerrains[c->faction] != terrainType; //drop bonus for non-creatures or non-native residents
  1077. //TODO neutral creatues
  1078. }
  1079. CreatureFactionLimiter::CreatureFactionLimiter(int Faction)
  1080. : faction(Faction)
  1081. {
  1082. }
  1083. CreatureFactionLimiter::CreatureFactionLimiter()
  1084. {
  1085. }
  1086. int CreatureFactionLimiter::limit(const BonusLimitationContext &context) const
  1087. {
  1088. const CCreature *c = retrieveCreature(&context.node);
  1089. return !c || c->faction != faction; //drop bonus for non-creatures or non-native residents
  1090. }
  1091. CreatureAlignmentLimiter::CreatureAlignmentLimiter()
  1092. {
  1093. }
  1094. CreatureAlignmentLimiter::CreatureAlignmentLimiter(si8 Alignment)
  1095. : alignment(Alignment)
  1096. {
  1097. }
  1098. int CreatureAlignmentLimiter::limit(const BonusLimitationContext &context) const
  1099. {
  1100. const CCreature *c = retrieveCreature(&context.node);
  1101. if(!c)
  1102. return true;
  1103. switch(alignment)
  1104. {
  1105. case EAlignment::GOOD:
  1106. return !c->isGood(); //if not good -> return true (drop bonus)
  1107. case EAlignment::NEUTRAL:
  1108. return c->isEvil() || c->isGood();
  1109. case EAlignment::EVIL:
  1110. return !c->isEvil();
  1111. default:
  1112. tlog1 << "Warning: illegal alignment in limiter!\n";
  1113. return true;
  1114. }
  1115. }
  1116. RankRangeLimiter::RankRangeLimiter(ui8 Min, ui8 Max)
  1117. :minRank(Min), maxRank(Max)
  1118. {
  1119. }
  1120. RankRangeLimiter::RankRangeLimiter()
  1121. {
  1122. minRank = maxRank = -1;
  1123. }
  1124. int RankRangeLimiter::limit(const BonusLimitationContext &context) const
  1125. {
  1126. const CStackInstance *csi = retreiveStackInstance(&context.node);
  1127. if(csi)
  1128. return csi->getExpRank() < minRank || csi->getExpRank() > maxRank;
  1129. return true;
  1130. }
  1131. int StackOwnerLimiter::limit(const BonusLimitationContext &context) const
  1132. {
  1133. const CStack *s = retreiveStackBattle(&context.node);
  1134. if(s)
  1135. return s->owner != owner;
  1136. const CStackInstance *csi = retreiveStackInstance(&context.node);
  1137. if(csi && csi->armyObj)
  1138. return csi->armyObj->tempOwner != owner;
  1139. return true;
  1140. }
  1141. StackOwnerLimiter::StackOwnerLimiter()
  1142. : owner(-1)
  1143. {
  1144. }
  1145. StackOwnerLimiter::StackOwnerLimiter(ui8 Owner)
  1146. : owner(Owner)
  1147. {
  1148. }