HeroBonus.cpp 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141
  1. #define VCMI_DLL
  2. #include "HeroBonus.h"
  3. #include <boost/foreach.hpp>
  4. #include "VCMI_Lib.h"
  5. #include "CSpellHandler.h"
  6. #include <sstream>
  7. #include "CCreatureHandler.h"
  8. #include <boost/assign/list_of.hpp>
  9. #include "CCreatureSet.h"
  10. #include <boost/algorithm/string/trim.hpp>
  11. #include <boost/bind.hpp>
  12. #include "CHeroHandler.h"
  13. #include "CGeneralTextHandler.h"
  14. #include "BattleState.h"
  15. #include "CArtHandler.h"
  16. #define FOREACH_PARENT(pname) TNodes lparents; getParents(lparents); BOOST_FOREACH(CBonusSystemNode *pname, lparents)
  17. #define FOREACH_RED_CHILD(pname) TNodes lchildren; getRedChildren(lchildren); BOOST_FOREACH(CBonusSystemNode *pname, lchildren)
  18. #define FOREACH_RED_PARENT(pname) TNodes lparents; getRedParents(lparents); BOOST_FOREACH(CBonusSystemNode *pname, lparents)
  19. #define BONUS_NAME(x) ( #x, Bonus::x )
  20. DLL_EXPORT const std::map<std::string, int> bonusNameMap = boost::assign::map_list_of BONUS_LIST;
  21. #undef BONUS_NAME
  22. #define BONUS_LOG_LINE(x) tlog5 << x << std::endl
  23. int CBonusSystemNode::treeChanged = 1;
  24. const bool CBonusSystemNode::cachingEnabled = true;
  25. int DLL_EXPORT BonusList::totalValue() const
  26. {
  27. int base = 0;
  28. int percentToBase = 0;
  29. int percentToAll = 0;
  30. int additive = 0;
  31. int indepMax = 0;
  32. bool hasIndepMax = false;
  33. int indepMin = 0;
  34. bool hasIndepMin = false;
  35. BOOST_FOREACH(Bonus *i, *this)
  36. {
  37. switch(i->valType)
  38. {
  39. case Bonus::BASE_NUMBER:
  40. base += i->val;
  41. break;
  42. case Bonus::PERCENT_TO_ALL:
  43. percentToAll += i->val;
  44. break;
  45. case Bonus::PERCENT_TO_BASE:
  46. percentToBase += i->val;
  47. break;
  48. case Bonus::ADDITIVE_VALUE:
  49. additive += i->val;
  50. break;
  51. case Bonus::INDEPENDENT_MAX:
  52. if (!hasIndepMax)
  53. {
  54. indepMax = i->val;
  55. hasIndepMax = true;
  56. }
  57. else
  58. {
  59. amax(indepMax, i->val);
  60. }
  61. break;
  62. case Bonus::INDEPENDENT_MIN:
  63. if (!hasIndepMin)
  64. {
  65. indepMin = i->val;
  66. hasIndepMin = true;
  67. }
  68. else
  69. {
  70. amin(indepMin, i->val);
  71. }
  72. break;
  73. }
  74. }
  75. int modifiedBase = base + (base * percentToBase) / 100;
  76. modifiedBase += additive;
  77. int valFirst = (modifiedBase * (100 + percentToAll)) / 100;
  78. if(hasIndepMin && hasIndepMax)
  79. assert(indepMin < indepMax);
  80. if (hasIndepMax)
  81. amax(valFirst, indepMax);
  82. if (hasIndepMin)
  83. amin(valFirst, indepMin);
  84. return valFirst;
  85. }
  86. const DLL_EXPORT Bonus * BonusList::getFirst(const CSelector &selector) const
  87. {
  88. for (int i = 0; i < this->size(); i++)
  89. {
  90. const Bonus *b = (*this)[i];
  91. if(selector(b))
  92. return &*b;
  93. }
  94. return NULL;
  95. }
  96. DLL_EXPORT Bonus * BonusList::getFirst(const CSelector &select)
  97. {
  98. for (int i = 0; i < this->size(); i++)
  99. {
  100. Bonus *b = (*this)[i];
  101. if(select(b))
  102. return &*b;
  103. }
  104. return NULL;
  105. }
  106. void DLL_EXPORT BonusList::getModifiersWDescr(TModDescr &out) const
  107. {
  108. BOOST_FOREACH(Bonus *i, *this)
  109. out.push_back(std::make_pair(i->val, i->Description()));
  110. }
  111. void DLL_EXPORT BonusList::getBonuses(boost::shared_ptr<BonusList> out, const CSelector &selector) const
  112. {
  113. // BOOST_FOREACH(Bonus *i, *this)
  114. // if(selector(i) && i->effectRange == Bonus::NO_LIMIT)
  115. // out.push_back(i);
  116. getBonuses(out, selector, 0);
  117. }
  118. void DLL_EXPORT BonusList::getBonuses(boost::shared_ptr<BonusList> out, const CSelector &selector, const CSelector &limit, const bool caching /*= false*/) const
  119. {
  120. for (int i = 0; i < this->size(); i++)
  121. {
  122. Bonus *b = (*this)[i];
  123. //add matching bonuses that matches limit predicate or have NO_LIMIT if no given predicate
  124. if(caching || (selector(b) && ((!limit && b->effectRange == Bonus::NO_LIMIT) || (limit && limit(b)))))
  125. out->push_back(b);
  126. }
  127. }
  128. void BonusList::limit(const CBonusSystemNode &node)
  129. {
  130. remove_if(boost::bind(&CBonusSystemNode::isLimitedOnUs, boost::ref(node), _1));
  131. }
  132. void DLL_EXPORT BonusList::eliminateDuplicates()
  133. {
  134. sort( begin(), end() );
  135. erase( unique( begin(), end() ), end() );
  136. }
  137. int IBonusBearer::valOfBonuses(Bonus::BonusType type, const CSelector &selector) const
  138. {
  139. return valOfBonuses(Selector::type(type) && selector);
  140. }
  141. int IBonusBearer::valOfBonuses(Bonus::BonusType type, int subtype /*= -1*/) const
  142. {
  143. CSelector s = Selector::type(type);
  144. if(subtype != -1)
  145. s = s && Selector::subtype(subtype);
  146. return valOfBonuses(s);
  147. }
  148. int IBonusBearer::valOfBonuses(const CSelector &selector) const
  149. {
  150. CSelector limit = 0;
  151. boost::shared_ptr<BonusList> hlp = getAllBonuses(selector, limit, NULL);
  152. return hlp->totalValue();
  153. }
  154. bool IBonusBearer::hasBonus(const CSelector &selector) const
  155. {
  156. return getBonuses(selector)->size() > 0;
  157. }
  158. bool IBonusBearer::hasBonusOfType(Bonus::BonusType type, int subtype /*= -1*/) const
  159. {
  160. CSelector s = Selector::type(type);
  161. if(subtype != -1)
  162. s = s && Selector::subtype(subtype);
  163. else
  164. {
  165. std::string str = "type_";
  166. str += ((char) type);
  167. setCachingStr(str);
  168. }
  169. return hasBonus(s);
  170. }
  171. void IBonusBearer::getModifiersWDescr(TModDescr &out, Bonus::BonusType type, int subtype /*= -1 */) const
  172. {
  173. getModifiersWDescr(out, subtype != -1 ? Selector::typeSubtype(type, subtype) : Selector::type(type));
  174. }
  175. void IBonusBearer::getModifiersWDescr(TModDescr &out, const CSelector &selector) const
  176. {
  177. getBonuses(selector)->getModifiersWDescr(out);
  178. }
  179. int IBonusBearer::getBonusesCount(int from, int id) const
  180. {
  181. return getBonusesCount(Selector::source(from, id));
  182. }
  183. int IBonusBearer::getBonusesCount(const CSelector &selector) const
  184. {
  185. return getBonuses(selector)->size();
  186. }
  187. const boost::shared_ptr<BonusList> IBonusBearer::getBonuses(const CSelector &selector) const
  188. {
  189. return getAllBonuses(selector, 0, NULL);
  190. }
  191. const boost::shared_ptr<BonusList> IBonusBearer::getBonuses(const CSelector &selector, const CSelector &limit) const
  192. {
  193. return getAllBonuses(selector, limit, NULL);
  194. }
  195. bool IBonusBearer::hasBonusFrom(ui8 source, ui32 sourceID) const
  196. {
  197. return hasBonus(Selector::source(source,sourceID));
  198. }
  199. int IBonusBearer::MoraleVal() const
  200. {
  201. if(hasBonusOfType(Bonus::NON_LIVING) || hasBonusOfType(Bonus::UNDEAD) ||
  202. hasBonusOfType(Bonus::NO_MORALE) || hasBonusOfType(Bonus::SIEGE_WEAPON))
  203. return 0;
  204. int ret = valOfBonuses(Selector::type(Bonus::MORALE));
  205. if(hasBonusOfType(Bonus::SELF_MORALE)) //eg. minotaur
  206. amax(ret, +1);
  207. return abetw(ret, -3, +3);
  208. }
  209. int IBonusBearer::LuckVal() const
  210. {
  211. if(hasBonusOfType(Bonus::NO_LUCK))
  212. return 0;
  213. int ret = valOfBonuses(Selector::type(Bonus::LUCK));
  214. if(hasBonusOfType(Bonus::SELF_LUCK)) //eg. halfling
  215. amax(ret, +1);
  216. return abetw(ret, -3, +3);
  217. }
  218. si32 IBonusBearer::Attack() const
  219. {
  220. si32 ret = valOfBonuses(Bonus::PRIMARY_SKILL, PrimarySkill::ATTACK);
  221. if(int frenzyPower = valOfBonuses(Bonus::IN_FRENZY)) //frenzy for attacker
  222. {
  223. ret += frenzyPower * Defense(false);
  224. }
  225. amax(ret, 0);
  226. return ret;
  227. }
  228. si32 IBonusBearer::Defense(bool withFrenzy /*= true*/) const
  229. {
  230. si32 ret = valOfBonuses(Bonus::PRIMARY_SKILL, PrimarySkill::DEFENSE);
  231. if(withFrenzy && hasBonusOfType(Bonus::IN_FRENZY)) //frenzy for defender
  232. {
  233. return 0;
  234. }
  235. amax(ret, 0);
  236. return ret;
  237. }
  238. ui16 IBonusBearer::MaxHealth() const
  239. {
  240. return std::max(1, valOfBonuses(Bonus::STACK_HEALTH)); //never 0 or negative
  241. }
  242. ui32 IBonusBearer::getMinDamage() const
  243. {
  244. return valOfBonuses(Selector::typeSubtype(Bonus::CREATURE_DAMAGE, 0) || Selector::typeSubtype(Bonus::CREATURE_DAMAGE, 1));
  245. }
  246. ui32 IBonusBearer::getMaxDamage() const
  247. {
  248. return valOfBonuses(Selector::typeSubtype(Bonus::CREATURE_DAMAGE, 0) || Selector::typeSubtype(Bonus::CREATURE_DAMAGE, 2));
  249. }
  250. si32 IBonusBearer::manaLimit() const
  251. {
  252. return si32(getPrimSkillLevel(3) * (100.0f + valOfBonuses(Bonus::SECONDARY_SKILL_PREMY, 24)) / 10.0f);
  253. }
  254. int IBonusBearer::getPrimSkillLevel(int id) const
  255. {
  256. int ret = 0;
  257. if(id == PrimarySkill::ATTACK)
  258. ret = Attack();
  259. else if(id == PrimarySkill::DEFENSE)
  260. ret = Defense();
  261. else
  262. ret = valOfBonuses(Bonus::PRIMARY_SKILL, id);
  263. amax(ret, id/2); //minimal value is 0 for attack and defense and 1 for spell power and knowledge
  264. return ret;
  265. }
  266. si32 IBonusBearer::magicResistance() const
  267. {
  268. return valOfBonuses(Selector::type(Bonus::MAGIC_RESISTANCE));
  269. }
  270. bool IBonusBearer::isLiving() const //TODO: theoreticaly there exists "LIVING" bonus in stack experience documentation
  271. {
  272. return(!hasBonus(Selector::type(Bonus::UNDEAD) || Selector::type(Bonus::NON_LIVING)));
  273. }
  274. void IBonusBearer::setCachingStr(const std::string &request) const
  275. {
  276. }
  277. const boost::shared_ptr<BonusList> IBonusBearer::getSpellBonuses() const
  278. {
  279. std::string str = "source_";
  280. str += (char) Bonus::SPELL_EFFECT;
  281. setCachingStr(str);
  282. return getBonuses(Selector::sourceType(Bonus::SPELL_EFFECT));
  283. }
  284. void CBonusSystemNode::setCachingStr(const std::string &request) const
  285. {
  286. cachingStr = request;
  287. }
  288. Bonus * CBonusSystemNode::getBonus(const CSelector &selector)
  289. {
  290. Bonus *ret = bonuses.getFirst(selector);
  291. if(ret)
  292. return ret;
  293. FOREACH_PARENT(pname)
  294. {
  295. ret = pname->getBonus(selector);
  296. if (ret)
  297. return ret;
  298. }
  299. return NULL;
  300. }
  301. const Bonus * CBonusSystemNode::getBonus( const CSelector &selector ) const
  302. {
  303. return (const_cast<CBonusSystemNode*>(this))->getBonus(selector);
  304. }
  305. void CBonusSystemNode::getParents(TCNodes &out) const /*retreives list of parent nodes (nodes to inherit bonuses from) */
  306. {
  307. for (int i = 0; i < parents.size(); i++)
  308. {
  309. const CBonusSystemNode *parent = parents[i];
  310. out.insert(parent);
  311. }
  312. }
  313. void CBonusSystemNode::getParents(TNodes &out)
  314. {
  315. for (int i = 0; i < parents.size(); i++)
  316. {
  317. const CBonusSystemNode *parent = parents[i];
  318. out.insert(const_cast<CBonusSystemNode*>(parent));
  319. }
  320. }
  321. void CBonusSystemNode::getAllBonusesRec(boost::shared_ptr<BonusList> out, const CSelector &selector, const CSelector &limit, const CBonusSystemNode *root /*= NULL*/, const bool caching /*= false*/) const
  322. {
  323. TCNodes lparents;
  324. getParents(lparents);
  325. BOOST_FOREACH(const CBonusSystemNode *p, lparents)
  326. p->getAllBonusesRec(out, selector, limit, root ? root : this, caching);
  327. bonuses.getBonuses(out, selector, limit, caching);
  328. if(!root)
  329. out->limit(*this);
  330. }
  331. const boost::shared_ptr<BonusList> CBonusSystemNode::getAllBonuses(const CSelector &selector, const CSelector &limit, const CBonusSystemNode *root /*= NULL*/) const
  332. {
  333. boost::shared_ptr<BonusList> ret(new BonusList());
  334. if (CBonusSystemNode::cachingEnabled)
  335. {
  336. if (cachedLast != treeChanged)
  337. {
  338. getAllBonusesRec(ret, selector, limit, this, true);
  339. ret->eliminateDuplicates();
  340. cachedBonuses = *ret;
  341. ret->clear();
  342. cachedRequests.clear();
  343. cachedLast = treeChanged;
  344. }
  345. if (cachingStr != "")
  346. {
  347. std::map<std::string, boost::shared_ptr<BonusList>>::iterator it(cachedRequests.find(cachingStr));
  348. if (cachedRequests.size() > 0 && it != cachedRequests.end())
  349. {
  350. ret = it->second;
  351. cachingStr = "";
  352. return ret;
  353. }
  354. }
  355. cachedBonuses.getBonuses(ret, selector, limit, false);
  356. if (!root)
  357. ret->limit(*this);
  358. if (cachingStr != "")
  359. cachedRequests[cachingStr] = ret;
  360. cachingStr = "";
  361. return ret;
  362. }
  363. else
  364. {
  365. getAllBonusesRec(ret, selector, limit, root, false);
  366. ret->eliminateDuplicates();
  367. return ret;
  368. }
  369. }
  370. CBonusSystemNode::CBonusSystemNode() : nodeType(UNKNOWN), cachedLast(0)
  371. {
  372. }
  373. CBonusSystemNode::~CBonusSystemNode()
  374. {
  375. detachFromAll();
  376. if(children.size())
  377. {
  378. tlog2 << "Warning: an orphaned child!\n";
  379. while(children.size())
  380. children.front()->detachFrom(this);
  381. }
  382. BOOST_FOREACH(Bonus *b, exportedBonuses)
  383. delete b;
  384. }
  385. void CBonusSystemNode::attachTo(CBonusSystemNode *parent)
  386. {
  387. assert(!vstd::contains(parents, parent));
  388. parents.push_back(parent);
  389. if(parent->actsAsBonusSourceOnly())
  390. parent->newRedDescendant(this);
  391. else
  392. newRedDescendant(parent);
  393. parent->newChildAttached(this);
  394. CBonusSystemNode::treeChanged++;
  395. }
  396. void CBonusSystemNode::detachFrom(CBonusSystemNode *parent)
  397. {
  398. assert(vstd::contains(parents, parent));
  399. if(parent->actsAsBonusSourceOnly())
  400. parent->removedRedDescendant(this);
  401. else
  402. removedRedDescendant(parent);
  403. parents -= parent;
  404. parent->childDetached(this);
  405. CBonusSystemNode::treeChanged++;
  406. }
  407. void CBonusSystemNode::popBonuses(const CSelector &s)
  408. {
  409. boost::shared_ptr<BonusList> bl(new BonusList);
  410. exportedBonuses.getBonuses(bl, s);
  411. BOOST_FOREACH(Bonus *b, *bl)
  412. removeBonus(b);
  413. BOOST_FOREACH(CBonusSystemNode *child, children)
  414. child->popBonuses(s);
  415. }
  416. // void CBonusSystemNode::addNewBonus(const Bonus &b)
  417. // {
  418. // addNewBonus(new Bonus(b));
  419. // }
  420. void CBonusSystemNode::addNewBonus(Bonus *b)
  421. {
  422. assert(!vstd::contains(exportedBonuses,b));
  423. exportedBonuses.push_back(b);
  424. exportBonus(b);
  425. CBonusSystemNode::treeChanged++;
  426. }
  427. void CBonusSystemNode::removeBonus(Bonus *b)
  428. {
  429. exportedBonuses -= b;
  430. if(b->propagator)
  431. unpropagateBonus(b);
  432. else
  433. bonuses -= b;
  434. delNull(b);
  435. CBonusSystemNode::treeChanged++;
  436. }
  437. bool CBonusSystemNode::isLimitedOnUs(Bonus *b) const
  438. {
  439. return b->limiter && b->limiter->limit(b, *this);
  440. }
  441. bool CBonusSystemNode::actsAsBonusSourceOnly() const
  442. {
  443. switch(nodeType)
  444. {
  445. case CREATURE:
  446. case ARTIFACT:
  447. case ARTIFACT_INSTANCE:
  448. return true;
  449. default:
  450. return false;
  451. }
  452. }
  453. void CBonusSystemNode::propagateBonus(Bonus * b)
  454. {
  455. if(b->propagator->shouldBeAttached(this))
  456. {
  457. bonuses.push_back(b);
  458. BONUS_LOG_LINE("#$# " << b->Description() << " #propagated to# " << nodeName());
  459. }
  460. FOREACH_RED_CHILD(child)
  461. child->propagateBonus(b);
  462. }
  463. void CBonusSystemNode::unpropagateBonus(Bonus * b)
  464. {
  465. if(b->propagator->shouldBeAttached(this))
  466. {
  467. bonuses -= b;
  468. BONUS_LOG_LINE("#$#" << b->Description() << " #is no longer propagated to# " << nodeName());
  469. }
  470. FOREACH_RED_CHILD(child)
  471. child->unpropagateBonus(b);
  472. }
  473. void CBonusSystemNode::newChildAttached(CBonusSystemNode *child)
  474. {
  475. assert(!vstd::contains(children, child));
  476. children.push_back(child);
  477. BONUS_LOG_LINE(child->nodeName() << " #attached to# " << nodeName());
  478. }
  479. void CBonusSystemNode::childDetached(CBonusSystemNode *child)
  480. {
  481. assert(vstd::contains(children, child));
  482. children -= child;
  483. BONUS_LOG_LINE(child->nodeName() << " #detached from# " << nodeName());
  484. }
  485. void CBonusSystemNode::detachFromAll()
  486. {
  487. while(parents.size())
  488. detachFrom(parents.front());
  489. }
  490. bool CBonusSystemNode::isIndependentNode() const
  491. {
  492. return parents.empty() && children.empty();
  493. }
  494. std::string CBonusSystemNode::nodeName() const
  495. {
  496. return description.size()
  497. ? description
  498. : std::string("Bonus system node of type ") + typeid(*this).name();
  499. }
  500. void CBonusSystemNode::deserializationFix()
  501. {
  502. exportBonuses();
  503. }
  504. void CBonusSystemNode::getRedParents(TNodes &out)
  505. {
  506. FOREACH_PARENT(pname)
  507. {
  508. if(pname->actsAsBonusSourceOnly())
  509. {
  510. out.insert(pname);
  511. }
  512. }
  513. if(!actsAsBonusSourceOnly())
  514. {
  515. BOOST_FOREACH(CBonusSystemNode *child, children)
  516. {
  517. out.insert(child);
  518. }
  519. }
  520. }
  521. void CBonusSystemNode::getRedChildren(TNodes &out)
  522. {
  523. FOREACH_PARENT(pname)
  524. {
  525. if(!pname->actsAsBonusSourceOnly())
  526. {
  527. out.insert(pname);
  528. }
  529. }
  530. if(actsAsBonusSourceOnly())
  531. {
  532. BOOST_FOREACH(CBonusSystemNode *child, children)
  533. {
  534. out.insert(child);
  535. }
  536. }
  537. }
  538. void CBonusSystemNode::newRedDescendant(CBonusSystemNode *descendant)
  539. {
  540. BOOST_FOREACH(Bonus *b, exportedBonuses)
  541. if(b->propagator)
  542. descendant->propagateBonus(b);
  543. FOREACH_RED_PARENT(parent)
  544. parent->newRedDescendant(descendant);
  545. }
  546. void CBonusSystemNode::removedRedDescendant(CBonusSystemNode *descendant)
  547. {
  548. BOOST_FOREACH(Bonus *b, exportedBonuses)
  549. if(b->propagator)
  550. descendant->unpropagateBonus(b);
  551. FOREACH_RED_PARENT(parent)
  552. parent->removedRedDescendant(descendant);
  553. }
  554. void CBonusSystemNode::getRedAncestors(TNodes &out)
  555. {
  556. getRedParents(out);
  557. FOREACH_RED_PARENT(p)
  558. p->getRedAncestors(out);
  559. }
  560. void CBonusSystemNode::getRedDescendants(TNodes &out)
  561. {
  562. getRedChildren(out);
  563. FOREACH_RED_CHILD(c)
  564. c->getRedChildren(out);
  565. }
  566. void CBonusSystemNode::battleTurnPassed()
  567. {
  568. BonusList bonusesCpy = exportedBonuses; //copy, because removing bonuses invalidates iters
  569. for (int i = 0; i < bonusesCpy.size(); i++)
  570. {
  571. Bonus *b = bonusesCpy[i];
  572. if(b->duration & Bonus::N_TURNS)
  573. {
  574. b->turnsRemain--;
  575. if(b->turnsRemain <= 0)
  576. removeBonus(b);
  577. }
  578. }
  579. }
  580. void CBonusSystemNode::exportBonus(Bonus * b)
  581. {
  582. if(b->propagator)
  583. propagateBonus(b);
  584. else
  585. bonuses.push_back(b);
  586. }
  587. void CBonusSystemNode::exportBonuses()
  588. {
  589. BOOST_FOREACH(Bonus *b, exportedBonuses)
  590. exportBonus(b);
  591. }
  592. int NBonus::valOf(const CBonusSystemNode *obj, Bonus::BonusType type, int subtype /*= -1*/)
  593. {
  594. if(obj)
  595. return obj->valOfBonuses(type, subtype);
  596. return 0;
  597. }
  598. bool NBonus::hasOfType(const CBonusSystemNode *obj, Bonus::BonusType type, int subtype /*= -1*/)
  599. {
  600. if(obj)
  601. return obj->hasBonusOfType(type, subtype);
  602. return false;
  603. }
  604. void NBonus::getModifiersWDescr(const CBonusSystemNode *obj, TModDescr &out, Bonus::BonusType type, int subtype /*= -1 */)
  605. {
  606. if(obj)
  607. return obj->getModifiersWDescr(out, type, subtype);
  608. }
  609. int NBonus::getCount(const CBonusSystemNode *obj, int from, int id)
  610. {
  611. if(obj)
  612. return obj->getBonusesCount(from, id);
  613. return 0;
  614. }
  615. const CSpell * Bonus::sourceSpell() const
  616. {
  617. if(source == SPELL_EFFECT)
  618. return VLC->spellh->spells[sid];
  619. return NULL;
  620. }
  621. std::string Bonus::Description() const
  622. {
  623. if(description.size())
  624. return description;
  625. std::ostringstream str;
  626. str << std::showpos << val << " ";
  627. switch(source)
  628. {
  629. case ARTIFACT:
  630. str << VLC->arth->artifacts[sid]->Name();
  631. break;;
  632. case SPELL_EFFECT:
  633. str << VLC->spellh->spells[sid]->name;
  634. break;
  635. case CREATURE_ABILITY:
  636. str << VLC->creh->creatures[sid]->namePl;
  637. break;
  638. case SECONDARY_SKILL:
  639. str << VLC->generaltexth->skillName[sid]/* << " secondary skill"*/;
  640. break;
  641. }
  642. return str.str();
  643. }
  644. Bonus::Bonus(ui16 Dur, ui8 Type, ui8 Src, si32 Val, ui32 ID, std::string Desc, si32 Subtype/*=-1*/)
  645. : duration(Dur), type(Type), subtype(Subtype), source(Src), val(Val), sid(ID), description(Desc)
  646. {
  647. additionalInfo = -1;
  648. turnsRemain = 0;
  649. valType = ADDITIVE_VALUE;
  650. effectRange = NO_LIMIT;
  651. boost::algorithm::trim(description);
  652. }
  653. Bonus::Bonus(ui16 Dur, ui8 Type, ui8 Src, si32 Val, ui32 ID, si32 Subtype/*=-1*/, ui8 ValType /*= ADDITIVE_VALUE*/)
  654. : duration(Dur), type(Type), subtype(Subtype), source(Src), val(Val), sid(ID), valType(ValType)
  655. {
  656. additionalInfo = -1;
  657. turnsRemain = 0;
  658. effectRange = NO_LIMIT;
  659. }
  660. Bonus::Bonus()
  661. {
  662. subtype = -1;
  663. additionalInfo = -1;
  664. turnsRemain = 0;
  665. valType = ADDITIVE_VALUE;
  666. effectRange = NO_LIMIT;
  667. }
  668. Bonus::~Bonus()
  669. {
  670. }
  671. Bonus * Bonus::addLimiter(ILimiter *Limiter)
  672. {
  673. return addLimiter(boost::shared_ptr<ILimiter>(Limiter));
  674. }
  675. Bonus * Bonus::addLimiter(boost::shared_ptr<ILimiter> Limiter)
  676. {
  677. limiter = Limiter;
  678. return this;
  679. }
  680. Bonus * Bonus::addPropagator(IPropagator *Propagator)
  681. {
  682. return addPropagator(boost::shared_ptr<IPropagator>(Propagator));
  683. }
  684. Bonus * Bonus::addPropagator(boost::shared_ptr<IPropagator> Propagator)
  685. {
  686. propagator = Propagator;
  687. return this;
  688. }
  689. CSelector DLL_EXPORT operator&&(const CSelector &first, const CSelector &second)
  690. {
  691. return CSelectorsConjunction(first, second);
  692. }
  693. CSelector DLL_EXPORT operator||(const CSelector &first, const CSelector &second)
  694. {
  695. return CSelectorsAlternative(first, second);
  696. }
  697. namespace Selector
  698. {
  699. DLL_EXPORT CSelectFieldEqual<TBonusType> type(&Bonus::type, 0);
  700. DLL_EXPORT CSelectFieldEqual<TBonusSubtype> subtype(&Bonus::subtype, 0);
  701. DLL_EXPORT CSelectFieldEqual<si32> info(&Bonus::additionalInfo, 0);
  702. DLL_EXPORT CSelectFieldEqual<ui16> duration(&Bonus::duration, 0);
  703. DLL_EXPORT CSelectFieldEqual<ui8> sourceType(&Bonus::source, 0);
  704. DLL_EXPORT CSelectFieldEqual<ui8> effectRange(&Bonus::effectRange, Bonus::NO_LIMIT);
  705. DLL_EXPORT CWillLastTurns turns;
  706. CSelector DLL_EXPORT typeSubtype(TBonusType Type, TBonusSubtype Subtype)
  707. {
  708. return type(Type) && subtype(Subtype);
  709. }
  710. CSelector DLL_EXPORT typeSubtypeInfo(TBonusType type, TBonusSubtype subtype, si32 info)
  711. {
  712. return CSelectFieldEqual<TBonusType>(&Bonus::type, type) && CSelectFieldEqual<TBonusSubtype>(&Bonus::subtype, subtype) && CSelectFieldEqual<si32>(&Bonus::additionalInfo, info);
  713. }
  714. CSelector DLL_EXPORT source(ui8 source, ui32 sourceID)
  715. {
  716. return CSelectFieldEqual<ui8>(&Bonus::source, source) && CSelectFieldEqual<ui32>(&Bonus::sid, sourceID);
  717. }
  718. CSelector DLL_EXPORT durationType(ui16 duration)
  719. {
  720. return CSelectFieldEqual<ui16>(&Bonus::duration, duration);
  721. }
  722. CSelector DLL_EXPORT sourceTypeSel(ui8 source)
  723. {
  724. return CSelectFieldEqual<ui8>(&Bonus::source, source);
  725. }
  726. bool DLL_EXPORT matchesType(const CSelector &sel, TBonusType type)
  727. {
  728. Bonus dummy;
  729. dummy.type = type;
  730. return sel(&dummy);
  731. }
  732. bool DLL_EXPORT matchesTypeSubtype(const CSelector &sel, TBonusType type, TBonusSubtype subtype)
  733. {
  734. Bonus dummy;
  735. dummy.type = type;
  736. dummy.subtype = subtype;
  737. return sel(&dummy);
  738. }
  739. bool DLL_EXPORT positiveSpellEffects(const Bonus *b)
  740. {
  741. if(b->source == Bonus::SPELL_EFFECT)
  742. {
  743. CSpell *sp = VLC->spellh->spells[b->sid];
  744. return sp->positiveness == 1;
  745. }
  746. return false; //not a spell effect
  747. }
  748. }
  749. const CStack * retreiveStackBattle(const CBonusSystemNode *node)
  750. {
  751. switch(node->nodeType)
  752. {
  753. case CBonusSystemNode::STACK_BATTLE:
  754. return static_cast<const CStack*>(node);
  755. default:
  756. return NULL;
  757. }
  758. }
  759. const CStackInstance * retreiveStackInstance(const CBonusSystemNode *node)
  760. {
  761. switch(node->nodeType)
  762. {
  763. case CBonusSystemNode::STACK_INSTANCE:
  764. return (static_cast<const CStackInstance *>(node));
  765. case CBonusSystemNode::STACK_BATTLE:
  766. return (static_cast<const CStack*>(node))->base;
  767. default:
  768. return NULL;
  769. }
  770. }
  771. const CCreature * retrieveCreature(const CBonusSystemNode *node)
  772. {
  773. switch(node->nodeType)
  774. {
  775. case CBonusSystemNode::CREATURE:
  776. return (static_cast<const CCreature *>(node));
  777. default:
  778. const CStackInstance *csi = retreiveStackInstance(node);
  779. if(csi)
  780. return csi->type;
  781. return NULL;
  782. }
  783. }
  784. DLL_EXPORT std::ostream & operator<<(std::ostream &out, const BonusList &bonusList)
  785. {
  786. for (int i = 0; i < bonusList.size(); i++)
  787. {
  788. Bonus *b = bonusList[i];
  789. out << "Bonus " << i << "\n" << *b << std::endl;
  790. }
  791. return out;
  792. }
  793. DLL_EXPORT std::ostream & operator<<(std::ostream &out, const Bonus &bonus)
  794. {
  795. for(std::map<std::string, int>::const_iterator i = bonusNameMap.begin(); i != bonusNameMap.end(); i++)
  796. if(i->second == bonus.type)
  797. out << "\tType: " << i->first << " \t";
  798. #define printField(field) out << "\t" #field ": " << (int)bonus.field << "\n"
  799. printField(val);
  800. printField(subtype);
  801. printField(duration);
  802. printField(source);
  803. printField(sid);
  804. printField(additionalInfo);
  805. printField(turnsRemain);
  806. printField(valType);
  807. printField(effectRange);
  808. #undef printField
  809. return out;
  810. }
  811. ILimiter::~ILimiter()
  812. {
  813. }
  814. bool ILimiter::limit(const Bonus *b, const CBonusSystemNode &node) const /*return true to drop the bonus */
  815. {
  816. return false;
  817. }
  818. bool CCreatureTypeLimiter::limit(const Bonus *b, const CBonusSystemNode &node) const
  819. {
  820. const CCreature *c = retrieveCreature(&node);
  821. if(!c)
  822. return true;
  823. return c != creature && (!includeUpgrades || !creature->isMyUpgrade(c));
  824. //drop bonus if it's not our creature and (we dont check upgrades or its not our upgrade)
  825. }
  826. CCreatureTypeLimiter::CCreatureTypeLimiter(const CCreature &Creature, ui8 IncludeUpgrades /*= true*/)
  827. :creature(&Creature), includeUpgrades(IncludeUpgrades)
  828. {
  829. }
  830. CCreatureTypeLimiter::CCreatureTypeLimiter()
  831. {
  832. creature = NULL;
  833. includeUpgrades = false;
  834. }
  835. HasAnotherBonusLimiter::HasAnotherBonusLimiter( TBonusType bonus )
  836. : type(bonus), subtype(0), isSubtypeRelevant(false)
  837. {
  838. }
  839. HasAnotherBonusLimiter::HasAnotherBonusLimiter( TBonusType bonus, TBonusSubtype _subtype )
  840. : type(bonus), subtype(_subtype), isSubtypeRelevant(true)
  841. {
  842. }
  843. bool HasAnotherBonusLimiter::limit( const Bonus *b, const CBonusSystemNode &node ) const
  844. {
  845. if(isSubtypeRelevant)
  846. {
  847. return !node.hasBonusOfType(static_cast<Bonus::BonusType>(type), subtype);
  848. }
  849. else
  850. {
  851. return !node.hasBonusOfType(static_cast<Bonus::BonusType>(type));
  852. }
  853. }
  854. IPropagator::~IPropagator()
  855. {
  856. }
  857. // CBonusSystemNode * IPropagator::getDestNode(CBonusSystemNode *source, CBonusSystemNode *redParent, CBonusSystemNode *redChild)
  858. // {
  859. // tlog1 << "IPropagator::getDestNode called!\n";
  860. // return source;
  861. // }
  862. bool IPropagator::shouldBeAttached(CBonusSystemNode *dest)
  863. {
  864. return false;
  865. }
  866. // CBonusSystemNode * CPropagatorNodeType::getDestNode(CBonusSystemNode *source, CBonusSystemNode *redParent, CBonusSystemNode *redChild)
  867. // {
  868. // return NULL;
  869. // }
  870. CPropagatorNodeType::CPropagatorNodeType()
  871. {
  872. }
  873. CPropagatorNodeType::CPropagatorNodeType(ui8 NodeType)
  874. : nodeType(NodeType)
  875. {
  876. }
  877. bool CPropagatorNodeType::shouldBeAttached(CBonusSystemNode *dest)
  878. {
  879. return nodeType == dest->nodeType;
  880. }
  881. CreatureNativeTerrainLimiter::CreatureNativeTerrainLimiter(int TerrainType)
  882. : terrainType(TerrainType)
  883. {
  884. }
  885. CreatureNativeTerrainLimiter::CreatureNativeTerrainLimiter()
  886. {
  887. }
  888. bool CreatureNativeTerrainLimiter::limit(const Bonus *b, const CBonusSystemNode &node) const
  889. {
  890. const CCreature *c = retrieveCreature(&node);
  891. return !c || !iswith(c->faction, 0, 9) || VLC->heroh->nativeTerrains[c->faction] != terrainType; //drop bonus for non-creatures or non-native residents
  892. //TODO neutral creatues
  893. }
  894. CreatureFactionLimiter::CreatureFactionLimiter(int Faction)
  895. : faction(Faction)
  896. {
  897. }
  898. CreatureFactionLimiter::CreatureFactionLimiter()
  899. {
  900. }
  901. bool CreatureFactionLimiter::limit(const Bonus *b, const CBonusSystemNode &node) const
  902. {
  903. const CCreature *c = retrieveCreature(&node);
  904. return !c || c->faction != faction; //drop bonus for non-creatures or non-native residents
  905. }
  906. CreatureAlignmentLimiter::CreatureAlignmentLimiter()
  907. {
  908. }
  909. CreatureAlignmentLimiter::CreatureAlignmentLimiter(si8 Alignment)
  910. : alignment(Alignment)
  911. {
  912. }
  913. bool CreatureAlignmentLimiter::limit(const Bonus *b, const CBonusSystemNode &node) const
  914. {
  915. const CCreature *c = retrieveCreature(&node);
  916. if(!c)
  917. return true;
  918. switch(alignment)
  919. {
  920. case GOOD:
  921. return !c->isGood(); //if not good -> return true (drop bonus)
  922. case NEUTRAL:
  923. return c->isEvil() || c->isGood();
  924. case EVIL:
  925. return !c->isEvil();
  926. default:
  927. tlog1 << "Warning: illegal alignment in limiter!\n";
  928. return true;
  929. }
  930. }
  931. RankRangeLimiter::RankRangeLimiter(ui8 Min, ui8 Max)
  932. :minRank(Min), maxRank(Max)
  933. {
  934. }
  935. RankRangeLimiter::RankRangeLimiter()
  936. {
  937. minRank = maxRank = -1;
  938. }
  939. bool RankRangeLimiter::limit( const Bonus *b, const CBonusSystemNode &node ) const
  940. {
  941. const CStackInstance *csi = retreiveStackInstance(&node);
  942. if(csi)
  943. return csi->getExpRank() < minRank || csi->getExpRank() > maxRank;
  944. return true;
  945. }
  946. bool StackOwnerLimiter::limit(const Bonus *b, const CBonusSystemNode &node) const
  947. {
  948. const CStack *s = retreiveStackBattle(&node);
  949. if(s)
  950. return s->owner != owner;
  951. const CStackInstance *csi = retreiveStackInstance(&node);
  952. if(csi && csi->armyObj)
  953. return csi->armyObj->tempOwner != owner;
  954. return true;
  955. }
  956. StackOwnerLimiter::StackOwnerLimiter()
  957. : owner(-1)
  958. {
  959. }
  960. StackOwnerLimiter::StackOwnerLimiter(ui8 Owner)
  961. : owner(Owner)
  962. {
  963. }