HeroBonus.cpp 37 KB

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