HeroBonus.cpp 38 KB

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