HeroBonus.cpp 38 KB

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