HeroBonus.cpp 35 KB

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