HeroBonus.cpp 38 KB

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