HeroBonus.cpp 34 KB

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