HeroBonus.cpp 35 KB

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