HeroBonus.cpp 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388
  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. return csi->getExpRank() < minRank || csi->getExpRank() > maxRank;
  1142. return true;
  1143. }
  1144. int StackOwnerLimiter::limit(const BonusLimitationContext &context) const
  1145. {
  1146. const CStack *s = retreiveStackBattle(&context.node);
  1147. if(s)
  1148. return s->owner != owner;
  1149. const CStackInstance *csi = retreiveStackInstance(&context.node);
  1150. if(csi && csi->armyObj)
  1151. return csi->armyObj->tempOwner != owner;
  1152. return true;
  1153. }
  1154. StackOwnerLimiter::StackOwnerLimiter()
  1155. : owner(-1)
  1156. {
  1157. }
  1158. StackOwnerLimiter::StackOwnerLimiter(ui8 Owner)
  1159. : owner(Owner)
  1160. {
  1161. }