HeroBonus.cpp 37 KB

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