HeroBonus.cpp 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379
  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. while(vstd::contains(bonuses, b))
  580. {
  581. tlog1 << "Bonus was duplicated (" << b->Description() << ") at " << nodeName() << std::endl;
  582. bonuses -= b;
  583. }
  584. BONUS_LOG_LINE("#$#" << b->Description() << " #is no longer propagated to# " << nodeName());
  585. }
  586. FOREACH_RED_CHILD(child)
  587. child->unpropagateBonus(b);
  588. }
  589. void CBonusSystemNode::newChildAttached(CBonusSystemNode *child)
  590. {
  591. assert(!vstd::contains(children, child));
  592. children.push_back(child);
  593. BONUS_LOG_LINE(child->nodeName() << " #attached to# " << nodeName());
  594. }
  595. void CBonusSystemNode::childDetached(CBonusSystemNode *child)
  596. {
  597. assert(vstd::contains(children, child));
  598. children -= child;
  599. BONUS_LOG_LINE(child->nodeName() << " #detached from# " << nodeName());
  600. }
  601. void CBonusSystemNode::detachFromAll()
  602. {
  603. while(parents.size())
  604. detachFrom(parents.front());
  605. }
  606. bool CBonusSystemNode::isIndependentNode() const
  607. {
  608. return parents.empty() && children.empty();
  609. }
  610. std::string CBonusSystemNode::nodeName() const
  611. {
  612. return description.size()
  613. ? description
  614. : std::string("Bonus system node of type ") + typeid(*this).name();
  615. }
  616. void CBonusSystemNode::deserializationFix()
  617. {
  618. exportBonuses();
  619. }
  620. void CBonusSystemNode::getRedParents(TNodes &out)
  621. {
  622. FOREACH_PARENT(pname)
  623. {
  624. if(pname->actsAsBonusSourceOnly())
  625. {
  626. out.insert(pname);
  627. }
  628. }
  629. if(!actsAsBonusSourceOnly())
  630. {
  631. BOOST_FOREACH(CBonusSystemNode *child, children)
  632. {
  633. out.insert(child);
  634. }
  635. }
  636. }
  637. void CBonusSystemNode::getRedChildren(TNodes &out)
  638. {
  639. FOREACH_PARENT(pname)
  640. {
  641. if(!pname->actsAsBonusSourceOnly())
  642. {
  643. out.insert(pname);
  644. }
  645. }
  646. if(actsAsBonusSourceOnly())
  647. {
  648. BOOST_FOREACH(CBonusSystemNode *child, children)
  649. {
  650. out.insert(child);
  651. }
  652. }
  653. }
  654. void CBonusSystemNode::newRedDescendant(CBonusSystemNode *descendant)
  655. {
  656. BOOST_FOREACH(Bonus *b, exportedBonuses)
  657. if(b->propagator)
  658. descendant->propagateBonus(b);
  659. FOREACH_RED_PARENT(parent)
  660. parent->newRedDescendant(descendant);
  661. }
  662. void CBonusSystemNode::removedRedDescendant(CBonusSystemNode *descendant)
  663. {
  664. BOOST_FOREACH(Bonus *b, exportedBonuses)
  665. if(b->propagator)
  666. descendant->unpropagateBonus(b);
  667. FOREACH_RED_PARENT(parent)
  668. parent->removedRedDescendant(descendant);
  669. }
  670. void CBonusSystemNode::getRedAncestors(TNodes &out)
  671. {
  672. getRedParents(out);
  673. FOREACH_RED_PARENT(p)
  674. p->getRedAncestors(out);
  675. }
  676. void CBonusSystemNode::getRedDescendants(TNodes &out)
  677. {
  678. getRedChildren(out);
  679. FOREACH_RED_CHILD(c)
  680. c->getRedChildren(out);
  681. }
  682. void CBonusSystemNode::battleTurnPassed()
  683. {
  684. BonusList bonusesCpy = exportedBonuses; //copy, because removing bonuses invalidates iters
  685. for (ui32 i = 0; i < bonusesCpy.size(); i++)
  686. {
  687. Bonus *b = bonusesCpy[i];
  688. if(b->duration & Bonus::N_TURNS)
  689. {
  690. b->turnsRemain--;
  691. if(b->turnsRemain <= 0)
  692. removeBonus(b);
  693. }
  694. }
  695. }
  696. void CBonusSystemNode::exportBonus(Bonus * b)
  697. {
  698. if(b->propagator)
  699. propagateBonus(b);
  700. else
  701. bonuses.push_back(b);
  702. CBonusSystemNode::treeChanged++;
  703. }
  704. void CBonusSystemNode::exportBonuses()
  705. {
  706. BOOST_FOREACH(Bonus *b, exportedBonuses)
  707. exportBonus(b);
  708. }
  709. const ui8 CBonusSystemNode::getNodeType() const
  710. {
  711. return nodeType;
  712. }
  713. BonusList& CBonusSystemNode::getBonusList()
  714. {
  715. return bonuses;
  716. }
  717. const BonusList& CBonusSystemNode::getBonusList() const
  718. {
  719. return bonuses;
  720. }
  721. const TNodesVector& CBonusSystemNode::getParentNodes() const
  722. {
  723. return parents;
  724. }
  725. const TNodesVector& CBonusSystemNode::getChildrenNodes() const
  726. {
  727. return children;
  728. }
  729. void CBonusSystemNode::setNodeType(ui8 type)
  730. {
  731. nodeType = type;
  732. }
  733. BonusList& CBonusSystemNode::getExportedBonusList()
  734. {
  735. return exportedBonuses;
  736. }
  737. const std::string& CBonusSystemNode::getDescription() const
  738. {
  739. return description;
  740. }
  741. void CBonusSystemNode::setDescription(const std::string &description)
  742. {
  743. this->description = description;
  744. }
  745. void CBonusSystemNode::incrementTreeChangedNum()
  746. {
  747. treeChanged++;
  748. }
  749. void CBonusSystemNode::limitBonuses(const BonusList &allBonuses, BonusList &out) const
  750. {
  751. assert(&allBonuses != &out); //todo should it work in-place?
  752. BonusList undecided = allBonuses,
  753. &accepted = out;
  754. while(true)
  755. {
  756. int undecidedCount = undecided.size();
  757. for(int i = 0; i < undecided.size(); i++)
  758. {
  759. Bonus *b = undecided[i];
  760. BonusLimitationContext context = {b, *this, out};
  761. int decision = b->limiter ? b->limiter->limit(context) : ILimiter::ACCEPT; //bonuses without limiters will be accepted by default
  762. if(decision == ILimiter::DISCARD)
  763. {
  764. undecided.erase(i);
  765. i--; continue;
  766. }
  767. else if(decision == ILimiter::ACCEPT)
  768. {
  769. accepted.push_back(b);
  770. undecided.erase(i);
  771. i--; continue;
  772. }
  773. else
  774. assert(decision == ILimiter::NOT_SURE);
  775. }
  776. if(undecided.size() == undecidedCount) //we haven't moved a single bonus -> limiters reached a stable state
  777. return;
  778. }
  779. }
  780. TBonusListPtr CBonusSystemNode::limitBonuses(const BonusList &allBonuses) const
  781. {
  782. auto ret = make_shared<BonusList>();
  783. limitBonuses(allBonuses, *ret);
  784. return ret;
  785. }
  786. void CBonusSystemNode::treeHasChanged()
  787. {
  788. treeChanged++;
  789. }
  790. int NBonus::valOf(const CBonusSystemNode *obj, Bonus::BonusType type, int subtype /*= -1*/)
  791. {
  792. if(obj)
  793. return obj->valOfBonuses(type, subtype);
  794. return 0;
  795. }
  796. bool NBonus::hasOfType(const CBonusSystemNode *obj, Bonus::BonusType type, int subtype /*= -1*/)
  797. {
  798. if(obj)
  799. return obj->hasBonusOfType(type, subtype);
  800. return false;
  801. }
  802. void NBonus::getModifiersWDescr(const CBonusSystemNode *obj, TModDescr &out, Bonus::BonusType type, int subtype /*= -1 */)
  803. {
  804. if(obj)
  805. return obj->getModifiersWDescr(out, type, subtype);
  806. }
  807. int NBonus::getCount(const CBonusSystemNode *obj, int from, int id)
  808. {
  809. if(obj)
  810. return obj->getBonusesCount(from, id);
  811. return 0;
  812. }
  813. const CSpell * Bonus::sourceSpell() const
  814. {
  815. if(source == SPELL_EFFECT)
  816. return VLC->spellh->spells[sid];
  817. return NULL;
  818. }
  819. std::string Bonus::Description() const
  820. {
  821. if(description.size())
  822. return description;
  823. std::ostringstream str;
  824. str << std::showpos << val << " ";
  825. switch(source)
  826. {
  827. case ARTIFACT:
  828. str << VLC->arth->artifacts[sid]->Name();
  829. break;;
  830. case SPELL_EFFECT:
  831. str << VLC->spellh->spells[sid]->name;
  832. break;
  833. case CREATURE_ABILITY:
  834. str << VLC->creh->creatures[sid]->namePl;
  835. break;
  836. case SECONDARY_SKILL:
  837. str << VLC->generaltexth->skillName[sid]/* << " secondary skill"*/;
  838. break;
  839. }
  840. return str.str();
  841. }
  842. Bonus::Bonus(ui16 Dur, ui8 Type, ui8 Src, si32 Val, ui32 ID, std::string Desc, si32 Subtype/*=-1*/)
  843. : duration(Dur), type(Type), subtype(Subtype), source(Src), val(Val), sid(ID), description(Desc)
  844. {
  845. additionalInfo = -1;
  846. turnsRemain = 0;
  847. valType = ADDITIVE_VALUE;
  848. effectRange = NO_LIMIT;
  849. boost::algorithm::trim(description);
  850. }
  851. Bonus::Bonus(ui16 Dur, ui8 Type, ui8 Src, si32 Val, ui32 ID, si32 Subtype/*=-1*/, ui8 ValType /*= ADDITIVE_VALUE*/)
  852. : duration(Dur), type(Type), subtype(Subtype), source(Src), val(Val), sid(ID), valType(ValType)
  853. {
  854. additionalInfo = -1;
  855. turnsRemain = 0;
  856. effectRange = NO_LIMIT;
  857. }
  858. Bonus::Bonus()
  859. {
  860. subtype = -1;
  861. additionalInfo = -1;
  862. turnsRemain = 0;
  863. valType = ADDITIVE_VALUE;
  864. effectRange = NO_LIMIT;
  865. }
  866. Bonus::~Bonus()
  867. {
  868. }
  869. Bonus * Bonus::addLimiter(TLimiterPtr Limiter)
  870. {
  871. limiter = Limiter;
  872. return this;
  873. }
  874. Bonus * Bonus::addPropagator(TPropagatorPtr Propagator)
  875. {
  876. propagator = Propagator;
  877. return this;
  878. }
  879. CSelector DLL_LINKAGE operator&&(const CSelector &first, const CSelector &second)
  880. {
  881. return CSelectorsConjunction(first, second);
  882. }
  883. CSelector DLL_LINKAGE operator||(const CSelector &first, const CSelector &second)
  884. {
  885. return CSelectorsAlternative(first, second);
  886. }
  887. namespace Selector
  888. {
  889. DLL_LINKAGE CSelectFieldEqual<TBonusType> type(&Bonus::type, 0);
  890. DLL_LINKAGE CSelectFieldEqual<TBonusSubtype> subtype(&Bonus::subtype, 0);
  891. DLL_LINKAGE CSelectFieldEqual<si32> info(&Bonus::additionalInfo, 0);
  892. DLL_LINKAGE CSelectFieldEqual<ui16> duration(&Bonus::duration, 0);
  893. DLL_LINKAGE CSelectFieldEqual<ui8> sourceType(&Bonus::source, 0);
  894. DLL_LINKAGE CSelectFieldEqual<ui8> effectRange(&Bonus::effectRange, Bonus::NO_LIMIT);
  895. DLL_LINKAGE CWillLastTurns turns;
  896. CSelector DLL_LINKAGE typeSubtype(TBonusType Type, TBonusSubtype Subtype)
  897. {
  898. return type(Type) && subtype(Subtype);
  899. }
  900. CSelector DLL_LINKAGE typeSubtypeInfo(TBonusType type, TBonusSubtype subtype, si32 info)
  901. {
  902. return CSelectFieldEqual<TBonusType>(&Bonus::type, type) && CSelectFieldEqual<TBonusSubtype>(&Bonus::subtype, subtype) && CSelectFieldEqual<si32>(&Bonus::additionalInfo, info);
  903. }
  904. CSelector DLL_LINKAGE source(ui8 source, ui32 sourceID)
  905. {
  906. return CSelectFieldEqual<ui8>(&Bonus::source, source) && CSelectFieldEqual<ui32>(&Bonus::sid, sourceID);
  907. }
  908. CSelector DLL_EXPORT durationType(ui16 duration)
  909. {
  910. return CSelectFieldEqual<ui16>(&Bonus::duration, duration);
  911. }
  912. CSelector DLL_LINKAGE sourceTypeSel(ui8 source)
  913. {
  914. return CSelectFieldEqual<ui8>(&Bonus::source, source);
  915. }
  916. bool DLL_LINKAGE matchesType(const CSelector &sel, TBonusType type)
  917. {
  918. Bonus dummy;
  919. dummy.type = type;
  920. return sel(&dummy);
  921. }
  922. bool DLL_LINKAGE matchesTypeSubtype(const CSelector &sel, TBonusType type, TBonusSubtype subtype)
  923. {
  924. Bonus dummy;
  925. dummy.type = type;
  926. dummy.subtype = subtype;
  927. return sel(&dummy);
  928. }
  929. bool DLL_LINKAGE positiveSpellEffects(const Bonus *b)
  930. {
  931. if(b->source == Bonus::SPELL_EFFECT)
  932. {
  933. CSpell *sp = VLC->spellh->spells[b->sid];
  934. return sp->isPositive();
  935. }
  936. return false; //not a spell effect
  937. }
  938. }
  939. const CStack * retreiveStackBattle(const CBonusSystemNode *node)
  940. {
  941. switch(node->getNodeType())
  942. {
  943. case CBonusSystemNode::STACK_BATTLE:
  944. return static_cast<const CStack*>(node);
  945. default:
  946. return NULL;
  947. }
  948. }
  949. const CStackInstance * retreiveStackInstance(const CBonusSystemNode *node)
  950. {
  951. switch(node->getNodeType())
  952. {
  953. case CBonusSystemNode::STACK_INSTANCE:
  954. return (static_cast<const CStackInstance *>(node));
  955. case CBonusSystemNode::STACK_BATTLE:
  956. return (static_cast<const CStack*>(node))->base;
  957. default:
  958. return NULL;
  959. }
  960. }
  961. const CCreature * retrieveCreature(const CBonusSystemNode *node)
  962. {
  963. switch(node->getNodeType())
  964. {
  965. case CBonusSystemNode::CREATURE:
  966. return (static_cast<const CCreature *>(node));
  967. default:
  968. const CStackInstance *csi = retreiveStackInstance(node);
  969. if(csi)
  970. return csi->type;
  971. return NULL;
  972. }
  973. }
  974. DLL_LINKAGE std::ostream & operator<<(std::ostream &out, const BonusList &bonusList)
  975. {
  976. for (ui32 i = 0; i < bonusList.size(); i++)
  977. {
  978. Bonus *b = bonusList[i];
  979. out << "Bonus " << i << "\n" << *b << std::endl;
  980. }
  981. return out;
  982. }
  983. DLL_LINKAGE std::ostream & operator<<(std::ostream &out, const Bonus &bonus)
  984. {
  985. for(std::map<std::string, int>::const_iterator i = bonusNameMap.begin(); i != bonusNameMap.end(); i++)
  986. if(i->second == bonus.type)
  987. out << "\tType: " << i->first << " \t";
  988. #define printField(field) out << "\t" #field ": " << (int)bonus.field << "\n"
  989. printField(val);
  990. printField(subtype);
  991. printField(duration);
  992. printField(source);
  993. printField(sid);
  994. printField(additionalInfo);
  995. printField(turnsRemain);
  996. printField(valType);
  997. printField(effectRange);
  998. #undef printField
  999. return out;
  1000. }
  1001. ILimiter::~ILimiter()
  1002. {
  1003. }
  1004. int ILimiter::limit(const BonusLimitationContext &context) const /*return true to drop the bonus */
  1005. {
  1006. return false;
  1007. }
  1008. int CCreatureTypeLimiter::limit(const BonusLimitationContext &context) const
  1009. {
  1010. const CCreature *c = retrieveCreature(&context.node);
  1011. if(!c)
  1012. return true;
  1013. return c != creature && (!includeUpgrades || !creature->isMyUpgrade(c));
  1014. //drop bonus if it's not our creature and (we dont check upgrades or its not our upgrade)
  1015. }
  1016. CCreatureTypeLimiter::CCreatureTypeLimiter(const CCreature &Creature, ui8 IncludeUpgrades /*= true*/)
  1017. :creature(&Creature), includeUpgrades(IncludeUpgrades)
  1018. {
  1019. }
  1020. CCreatureTypeLimiter::CCreatureTypeLimiter()
  1021. {
  1022. creature = NULL;
  1023. includeUpgrades = false;
  1024. }
  1025. HasAnotherBonusLimiter::HasAnotherBonusLimiter( TBonusType bonus )
  1026. : type(bonus), subtype(0), isSubtypeRelevant(false)
  1027. {
  1028. }
  1029. HasAnotherBonusLimiter::HasAnotherBonusLimiter( TBonusType bonus, TBonusSubtype _subtype )
  1030. : type(bonus), subtype(_subtype), isSubtypeRelevant(true)
  1031. {
  1032. }
  1033. int HasAnotherBonusLimiter::limit(const BonusLimitationContext &context) const
  1034. {
  1035. CSelector mySelector = isSubtypeRelevant
  1036. ? Selector::typeSubtype(type, subtype)
  1037. : Selector::type(type);
  1038. //if we have a bonus of required type accepted, limiter should accept also this bonus
  1039. if(context.alreadyAccepted.getFirst(mySelector))
  1040. return ACCEPT;
  1041. //do not accept for now but it may change if more bonuses gets included
  1042. return NOT_SURE;
  1043. }
  1044. IPropagator::~IPropagator()
  1045. {
  1046. }
  1047. // CBonusSystemNode * IPropagator::getDestNode(CBonusSystemNode *source, CBonusSystemNode *redParent, CBonusSystemNode *redChild)
  1048. // {
  1049. // tlog1 << "IPropagator::getDestNode called!\n";
  1050. // return source;
  1051. // }
  1052. bool IPropagator::shouldBeAttached(CBonusSystemNode *dest)
  1053. {
  1054. return false;
  1055. }
  1056. // CBonusSystemNode * CPropagatorNodeType::getDestNode(CBonusSystemNode *source, CBonusSystemNode *redParent, CBonusSystemNode *redChild)
  1057. // {
  1058. // return NULL;
  1059. // }
  1060. CPropagatorNodeType::CPropagatorNodeType()
  1061. {
  1062. }
  1063. CPropagatorNodeType::CPropagatorNodeType(ui8 NodeType)
  1064. : nodeType(NodeType)
  1065. {
  1066. }
  1067. bool CPropagatorNodeType::shouldBeAttached(CBonusSystemNode *dest)
  1068. {
  1069. return nodeType == dest->getNodeType();
  1070. }
  1071. CreatureNativeTerrainLimiter::CreatureNativeTerrainLimiter(int TerrainType)
  1072. : terrainType(TerrainType)
  1073. {
  1074. }
  1075. CreatureNativeTerrainLimiter::CreatureNativeTerrainLimiter()
  1076. {
  1077. }
  1078. int CreatureNativeTerrainLimiter::limit(const BonusLimitationContext &context) const
  1079. {
  1080. const CCreature *c = retrieveCreature(&context.node);
  1081. return !c || !vstd::iswithin(c->faction, 0, 9) || VLC->heroh->nativeTerrains[c->faction] != terrainType; //drop bonus for non-creatures or non-native residents
  1082. //TODO neutral creatues
  1083. }
  1084. CreatureFactionLimiter::CreatureFactionLimiter(int Faction)
  1085. : faction(Faction)
  1086. {
  1087. }
  1088. CreatureFactionLimiter::CreatureFactionLimiter()
  1089. {
  1090. }
  1091. int CreatureFactionLimiter::limit(const BonusLimitationContext &context) const
  1092. {
  1093. const CCreature *c = retrieveCreature(&context.node);
  1094. return !c || c->faction != faction; //drop bonus for non-creatures or non-native residents
  1095. }
  1096. CreatureAlignmentLimiter::CreatureAlignmentLimiter()
  1097. {
  1098. }
  1099. CreatureAlignmentLimiter::CreatureAlignmentLimiter(si8 Alignment)
  1100. : alignment(Alignment)
  1101. {
  1102. }
  1103. int CreatureAlignmentLimiter::limit(const BonusLimitationContext &context) const
  1104. {
  1105. const CCreature *c = retrieveCreature(&context.node);
  1106. if(!c)
  1107. return true;
  1108. switch(alignment)
  1109. {
  1110. case EAlignment::GOOD:
  1111. return !c->isGood(); //if not good -> return true (drop bonus)
  1112. case EAlignment::NEUTRAL:
  1113. return c->isEvil() || c->isGood();
  1114. case EAlignment::EVIL:
  1115. return !c->isEvil();
  1116. default:
  1117. tlog1 << "Warning: illegal alignment in limiter!\n";
  1118. return true;
  1119. }
  1120. }
  1121. RankRangeLimiter::RankRangeLimiter(ui8 Min, ui8 Max)
  1122. :minRank(Min), maxRank(Max)
  1123. {
  1124. }
  1125. RankRangeLimiter::RankRangeLimiter()
  1126. {
  1127. minRank = maxRank = -1;
  1128. }
  1129. int RankRangeLimiter::limit(const BonusLimitationContext &context) const
  1130. {
  1131. const CStackInstance *csi = retreiveStackInstance(&context.node);
  1132. if(csi)
  1133. return csi->getExpRank() < minRank || csi->getExpRank() > maxRank;
  1134. return true;
  1135. }
  1136. int StackOwnerLimiter::limit(const BonusLimitationContext &context) const
  1137. {
  1138. const CStack *s = retreiveStackBattle(&context.node);
  1139. if(s)
  1140. return s->owner != owner;
  1141. const CStackInstance *csi = retreiveStackInstance(&context.node);
  1142. if(csi && csi->armyObj)
  1143. return csi->armyObj->tempOwner != owner;
  1144. return true;
  1145. }
  1146. StackOwnerLimiter::StackOwnerLimiter()
  1147. : owner(-1)
  1148. {
  1149. }
  1150. StackOwnerLimiter::StackOwnerLimiter(ui8 Owner)
  1151. : owner(Owner)
  1152. {
  1153. }