HeroBonus.cpp 25 KB

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