HeroBonus.cpp 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060
  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. exportBonuses();
  436. }
  437. void CBonusSystemNode::getRedParents(TNodes &out)
  438. {
  439. FOREACH_PARENT(pname)
  440. {
  441. if(pname->actsAsBonusSourceOnly())
  442. {
  443. out.insert(pname);
  444. }
  445. }
  446. if(!actsAsBonusSourceOnly())
  447. {
  448. BOOST_FOREACH(CBonusSystemNode *child, children)
  449. {
  450. out.insert(child);
  451. }
  452. }
  453. }
  454. void CBonusSystemNode::getRedChildren(TNodes &out)
  455. {
  456. FOREACH_PARENT(pname)
  457. {
  458. if(!pname->actsAsBonusSourceOnly())
  459. {
  460. out.insert(pname);
  461. }
  462. }
  463. if(actsAsBonusSourceOnly())
  464. {
  465. BOOST_FOREACH(CBonusSystemNode *child, children)
  466. {
  467. out.insert(child);
  468. }
  469. }
  470. }
  471. void CBonusSystemNode::newRedDescendant(CBonusSystemNode *descendant)
  472. {
  473. BOOST_FOREACH(Bonus *b, exportedBonuses)
  474. if(b->propagator)
  475. descendant->propagateBonus(b);
  476. FOREACH_RED_PARENT(parent)
  477. parent->newRedDescendant(descendant);
  478. }
  479. void CBonusSystemNode::removedRedDescendant(CBonusSystemNode *descendant)
  480. {
  481. BOOST_FOREACH(Bonus *b, exportedBonuses)
  482. if(b->propagator)
  483. descendant->unpropagateBonus(b);
  484. FOREACH_RED_PARENT(parent)
  485. parent->removedRedDescendant(descendant);
  486. }
  487. void CBonusSystemNode::getRedAncestors(TNodes &out)
  488. {
  489. getRedParents(out);
  490. FOREACH_RED_PARENT(p)
  491. p->getRedAncestors(out);
  492. }
  493. void CBonusSystemNode::getRedDescendants(TNodes &out)
  494. {
  495. getRedChildren(out);
  496. FOREACH_RED_CHILD(c)
  497. c->getRedChildren(out);
  498. }
  499. void CBonusSystemNode::battleTurnPassed()
  500. {
  501. BonusList bonusesCpy = exportedBonuses; //copy, because removing bonuses invalidates iters
  502. BOOST_FOREACH(Bonus *b, bonusesCpy)
  503. {
  504. if(b->duration & Bonus::N_TURNS)
  505. {
  506. b->turnsRemain--;
  507. if(b->turnsRemain <= 0)
  508. removeBonus(b);
  509. }
  510. }
  511. }
  512. void CBonusSystemNode::exportBonus(Bonus * b)
  513. {
  514. if(b->propagator)
  515. propagateBonus(b);
  516. else
  517. bonuses.push_back(b);
  518. }
  519. void CBonusSystemNode::exportBonuses()
  520. {
  521. BOOST_FOREACH(Bonus *b, exportedBonuses)
  522. exportBonus(b);
  523. }
  524. int NBonus::valOf(const CBonusSystemNode *obj, Bonus::BonusType type, int subtype /*= -1*/)
  525. {
  526. if(obj)
  527. return obj->valOfBonuses(type, subtype);
  528. return 0;
  529. }
  530. bool NBonus::hasOfType(const CBonusSystemNode *obj, Bonus::BonusType type, int subtype /*= -1*/)
  531. {
  532. if(obj)
  533. return obj->hasBonusOfType(type, subtype);
  534. return false;
  535. }
  536. void NBonus::getModifiersWDescr(const CBonusSystemNode *obj, TModDescr &out, Bonus::BonusType type, int subtype /*= -1 */)
  537. {
  538. if(obj)
  539. return obj->getModifiersWDescr(out, type, subtype);
  540. }
  541. int NBonus::getCount(const CBonusSystemNode *obj, int from, int id)
  542. {
  543. if(obj)
  544. return obj->getBonusesCount(from, id);
  545. return 0;
  546. }
  547. const CSpell * Bonus::sourceSpell() const
  548. {
  549. if(source == SPELL_EFFECT)
  550. return VLC->spellh->spells[sid];
  551. return NULL;
  552. }
  553. std::string Bonus::Description() const
  554. {
  555. if(description.size())
  556. return description;
  557. std::ostringstream str;
  558. str << std::showpos << val << " ";
  559. switch(source)
  560. {
  561. case ARTIFACT:
  562. str << VLC->arth->artifacts[sid]->Name();
  563. break;;
  564. case SPELL_EFFECT:
  565. str << VLC->spellh->spells[sid]->name;
  566. break;
  567. case CREATURE_ABILITY:
  568. str << VLC->creh->creatures[sid]->namePl;
  569. break;
  570. case SECONDARY_SKILL:
  571. str << VLC->generaltexth->skillName[sid]/* << " secondary skill"*/;
  572. break;
  573. }
  574. return str.str();
  575. }
  576. Bonus::Bonus(ui16 Dur, ui8 Type, ui8 Src, si32 Val, ui32 ID, std::string Desc, si32 Subtype/*=-1*/)
  577. : duration(Dur), type(Type), subtype(Subtype), source(Src), val(Val), sid(ID), description(Desc)
  578. {
  579. additionalInfo = -1;
  580. turnsRemain = 0;
  581. valType = ADDITIVE_VALUE;
  582. effectRange = NO_LIMIT;
  583. boost::algorithm::trim(description);
  584. }
  585. Bonus::Bonus(ui16 Dur, ui8 Type, ui8 Src, si32 Val, ui32 ID, si32 Subtype/*=-1*/, ui8 ValType /*= ADDITIVE_VALUE*/)
  586. : duration(Dur), type(Type), subtype(Subtype), source(Src), val(Val), sid(ID), valType(ValType)
  587. {
  588. additionalInfo = -1;
  589. turnsRemain = 0;
  590. effectRange = NO_LIMIT;
  591. }
  592. Bonus::Bonus()
  593. {
  594. subtype = -1;
  595. additionalInfo = -1;
  596. turnsRemain = 0;
  597. valType = ADDITIVE_VALUE;
  598. effectRange = NO_LIMIT;
  599. }
  600. Bonus::~Bonus()
  601. {
  602. }
  603. Bonus * Bonus::addLimiter(ILimiter *Limiter)
  604. {
  605. return addLimiter(boost::shared_ptr<ILimiter>(Limiter));
  606. }
  607. Bonus * Bonus::addLimiter(boost::shared_ptr<ILimiter> Limiter)
  608. {
  609. limiter = Limiter;
  610. return this;
  611. }
  612. Bonus * Bonus::addPropagator(IPropagator *Propagator)
  613. {
  614. return addPropagator(boost::shared_ptr<IPropagator>(Propagator));
  615. }
  616. Bonus * Bonus::addPropagator(boost::shared_ptr<IPropagator> Propagator)
  617. {
  618. propagator = Propagator;
  619. return this;
  620. }
  621. CSelector DLL_EXPORT operator&&(const CSelector &first, const CSelector &second)
  622. {
  623. return CSelectorsConjunction(first, second);
  624. }
  625. CSelector DLL_EXPORT operator||(const CSelector &first, const CSelector &second)
  626. {
  627. return CSelectorsAlternative(first, second);
  628. }
  629. namespace Selector
  630. {
  631. DLL_EXPORT CSelectFieldEqual<TBonusType> type(&Bonus::type, 0);
  632. DLL_EXPORT CSelectFieldEqual<TBonusSubtype> subtype(&Bonus::subtype, 0);
  633. DLL_EXPORT CSelectFieldEqual<si32> info(&Bonus::additionalInfo, 0);
  634. DLL_EXPORT CSelectFieldEqual<ui16> duration(&Bonus::duration, 0);
  635. DLL_EXPORT CSelectFieldEqual<ui8> sourceType(&Bonus::source, 0);
  636. DLL_EXPORT CSelectFieldEqual<ui8> effectRange(&Bonus::effectRange, Bonus::NO_LIMIT);
  637. DLL_EXPORT CWillLastTurns turns;
  638. CSelector DLL_EXPORT typeSybtype(TBonusType Type, TBonusSubtype Subtype)
  639. {
  640. return type(Type) && subtype(Subtype);
  641. }
  642. CSelector DLL_EXPORT typeSybtypeInfo(TBonusType type, TBonusSubtype subtype, si32 info)
  643. {
  644. return CSelectFieldEqual<TBonusType>(&Bonus::type, type) && CSelectFieldEqual<TBonusSubtype>(&Bonus::subtype, subtype) && CSelectFieldEqual<si32>(&Bonus::additionalInfo, info);
  645. }
  646. CSelector DLL_EXPORT source(ui8 source, ui32 sourceID)
  647. {
  648. return CSelectFieldEqual<ui8>(&Bonus::source, source) && CSelectFieldEqual<ui32>(&Bonus::sid, sourceID);
  649. }
  650. CSelector DLL_EXPORT durationType(ui16 duration)
  651. {
  652. return CSelectFieldEqual<ui16>(&Bonus::duration, duration);
  653. }
  654. CSelector DLL_EXPORT sourceTypeSel(ui8 source)
  655. {
  656. return CSelectFieldEqual<ui8>(&Bonus::source, source);
  657. }
  658. bool DLL_EXPORT matchesType(const CSelector &sel, TBonusType type)
  659. {
  660. Bonus dummy;
  661. dummy.type = type;
  662. return sel(&dummy);
  663. }
  664. bool DLL_EXPORT matchesTypeSubtype(const CSelector &sel, TBonusType type, TBonusSubtype subtype)
  665. {
  666. Bonus dummy;
  667. dummy.type = type;
  668. dummy.subtype = subtype;
  669. return sel(&dummy);
  670. }
  671. bool DLL_EXPORT positiveSpellEffects(const Bonus *b)
  672. {
  673. if(b->source == Bonus::SPELL_EFFECT)
  674. {
  675. CSpell *sp = VLC->spellh->spells[b->sid];
  676. return sp->positiveness == 1;
  677. }
  678. return false; //not a spell effect
  679. }
  680. }
  681. const CStack * retreiveStackBattle(const CBonusSystemNode *node)
  682. {
  683. switch(node->nodeType)
  684. {
  685. case CBonusSystemNode::STACK_BATTLE:
  686. return static_cast<const CStack*>(node);
  687. default:
  688. return NULL;
  689. }
  690. }
  691. const CStackInstance * retreiveStackInstance(const CBonusSystemNode *node)
  692. {
  693. switch(node->nodeType)
  694. {
  695. case CBonusSystemNode::STACK_INSTANCE:
  696. return (static_cast<const CStackInstance *>(node));
  697. case CBonusSystemNode::STACK_BATTLE:
  698. return (static_cast<const CStack*>(node))->base;
  699. default:
  700. return NULL;
  701. }
  702. }
  703. const CCreature * retrieveCreature(const CBonusSystemNode *node)
  704. {
  705. switch(node->nodeType)
  706. {
  707. case CBonusSystemNode::CREATURE:
  708. return (static_cast<const CCreature *>(node));
  709. default:
  710. const CStackInstance *csi = retreiveStackInstance(node);
  711. if(csi)
  712. return csi->type;
  713. return NULL;
  714. }
  715. }
  716. DLL_EXPORT std::ostream & operator<<(std::ostream &out, const BonusList &bonusList)
  717. {
  718. int i = 0;
  719. BOOST_FOREACH(const Bonus *b, bonusList)
  720. {
  721. out << "Bonus " << i++ << "\n" << *b << std::endl;
  722. }
  723. return out;
  724. }
  725. DLL_EXPORT std::ostream & operator<<(std::ostream &out, const Bonus &bonus)
  726. {
  727. for(std::map<std::string, int>::const_iterator i = bonusNameMap.begin(); i != bonusNameMap.end(); i++)
  728. if(i->second == bonus.type)
  729. out << "\tType: " << i->first << " \t";
  730. #define printField(field) out << "\t" #field ": " << (int)bonus.field << "\n"
  731. printField(val);
  732. printField(subtype);
  733. printField(duration);
  734. printField(source);
  735. printField(sid);
  736. printField(additionalInfo);
  737. printField(turnsRemain);
  738. printField(valType);
  739. printField(effectRange);
  740. #undef printField
  741. return out;
  742. }
  743. ILimiter::~ILimiter()
  744. {
  745. }
  746. bool ILimiter::limit(const Bonus *b, const CBonusSystemNode &node) const /*return true to drop the bonus */
  747. {
  748. return false;
  749. }
  750. bool CCreatureTypeLimiter::limit(const Bonus *b, const CBonusSystemNode &node) const
  751. {
  752. const CCreature *c = retrieveCreature(&node);
  753. if(!c)
  754. return true;
  755. return c != creature && (!includeUpgrades || !creature->isMyUpgrade(c));
  756. //drop bonus if it's not our creature and (we dont check upgrades or its not our upgrade)
  757. }
  758. CCreatureTypeLimiter::CCreatureTypeLimiter(const CCreature &Creature, ui8 IncludeUpgrades /*= true*/)
  759. :creature(&Creature), includeUpgrades(IncludeUpgrades)
  760. {
  761. }
  762. CCreatureTypeLimiter::CCreatureTypeLimiter()
  763. {
  764. creature = NULL;
  765. includeUpgrades = false;
  766. }
  767. HasAnotherBonusLimiter::HasAnotherBonusLimiter( TBonusType bonus )
  768. : type(bonus), subtype(0), isSubtypeRelevant(false)
  769. {
  770. }
  771. HasAnotherBonusLimiter::HasAnotherBonusLimiter( TBonusType bonus, TBonusSubtype _subtype )
  772. : type(bonus), subtype(_subtype), isSubtypeRelevant(true)
  773. {
  774. }
  775. bool HasAnotherBonusLimiter::limit( const Bonus *b, const CBonusSystemNode &node ) const
  776. {
  777. if(isSubtypeRelevant)
  778. {
  779. return !node.hasBonusOfType(static_cast<Bonus::BonusType>(type), subtype);
  780. }
  781. else
  782. {
  783. return !node.hasBonusOfType(static_cast<Bonus::BonusType>(type));
  784. }
  785. }
  786. IPropagator::~IPropagator()
  787. {
  788. }
  789. // CBonusSystemNode * IPropagator::getDestNode(CBonusSystemNode *source, CBonusSystemNode *redParent, CBonusSystemNode *redChild)
  790. // {
  791. // tlog1 << "IPropagator::getDestNode called!\n";
  792. // return source;
  793. // }
  794. bool IPropagator::shouldBeAttached(CBonusSystemNode *dest)
  795. {
  796. return false;
  797. }
  798. // CBonusSystemNode * CPropagatorNodeType::getDestNode(CBonusSystemNode *source, CBonusSystemNode *redParent, CBonusSystemNode *redChild)
  799. // {
  800. // return NULL;
  801. // }
  802. CPropagatorNodeType::CPropagatorNodeType()
  803. {
  804. }
  805. CPropagatorNodeType::CPropagatorNodeType(ui8 NodeType)
  806. : nodeType(NodeType)
  807. {
  808. }
  809. bool CPropagatorNodeType::shouldBeAttached(CBonusSystemNode *dest)
  810. {
  811. return nodeType == dest->nodeType;
  812. }
  813. CreatureNativeTerrainLimiter::CreatureNativeTerrainLimiter(int TerrainType)
  814. : terrainType(TerrainType)
  815. {
  816. }
  817. CreatureNativeTerrainLimiter::CreatureNativeTerrainLimiter()
  818. {
  819. }
  820. bool CreatureNativeTerrainLimiter::limit(const Bonus *b, const CBonusSystemNode &node) const
  821. {
  822. const CCreature *c = retrieveCreature(&node);
  823. return !c || !iswith(c->faction, 0, 9) || VLC->heroh->nativeTerrains[c->faction] != terrainType; //drop bonus for non-creatures or non-native residents
  824. //TODO neutral creatues
  825. }
  826. CreatureFactionLimiter::CreatureFactionLimiter(int Faction)
  827. : faction(Faction)
  828. {
  829. }
  830. CreatureFactionLimiter::CreatureFactionLimiter()
  831. {
  832. }
  833. bool CreatureFactionLimiter::limit(const Bonus *b, const CBonusSystemNode &node) const
  834. {
  835. const CCreature *c = retrieveCreature(&node);
  836. return !c || c->faction != faction; //drop bonus for non-creatures or non-native residents
  837. }
  838. CreatureAlignmentLimiter::CreatureAlignmentLimiter()
  839. {
  840. }
  841. CreatureAlignmentLimiter::CreatureAlignmentLimiter(si8 Alignment)
  842. : alignment(Alignment)
  843. {
  844. }
  845. bool CreatureAlignmentLimiter::limit(const Bonus *b, const CBonusSystemNode &node) const
  846. {
  847. const CCreature *c = retrieveCreature(&node);
  848. if(!c)
  849. return true;
  850. switch(alignment)
  851. {
  852. case GOOD:
  853. return !c->isGood(); //if not good -> return true (drop bonus)
  854. case NEUTRAL:
  855. return c->isEvil() || c->isGood();
  856. case EVIL:
  857. return !c->isEvil();
  858. default:
  859. tlog1 << "Warning: illegal alignment in limiter!\n";
  860. return true;
  861. }
  862. }
  863. RankRangeLimiter::RankRangeLimiter(ui8 Min, ui8 Max)
  864. :minRank(Min), maxRank(Max)
  865. {
  866. }
  867. RankRangeLimiter::RankRangeLimiter()
  868. {
  869. minRank = maxRank = -1;
  870. }
  871. bool RankRangeLimiter::limit( const Bonus *b, const CBonusSystemNode &node ) const
  872. {
  873. const CStackInstance *csi = retreiveStackInstance(&node);
  874. if(csi)
  875. return csi->getExpRank() < minRank || csi->getExpRank() > maxRank;
  876. return true;
  877. }
  878. bool StackOwnerLimiter::limit(const Bonus *b, const CBonusSystemNode &node) const
  879. {
  880. const CStack *s = retreiveStackBattle(&node);
  881. if(s)
  882. return s->owner != owner;
  883. const CStackInstance *csi = retreiveStackInstance(&node);
  884. if(csi && csi->armyObj)
  885. return csi->armyObj->tempOwner != owner;
  886. return true;
  887. }
  888. StackOwnerLimiter::StackOwnerLimiter()
  889. : owner(-1)
  890. {
  891. }
  892. StackOwnerLimiter::StackOwnerLimiter(ui8 Owner)
  893. : owner(Owner)
  894. {
  895. }