HeroBonus.cpp 37 KB

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