HeroBonus.cpp 38 KB

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