HeroBonus.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062
  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. amax(ret, 0);
  233. return ret;
  234. }
  235. si32 IBonusBearer::Defense(bool withFrenzy /*= true*/) const
  236. {
  237. si32 ret = valOfBonuses(Bonus::PRIMARY_SKILL, PrimarySkill::DEFENSE);
  238. if(withFrenzy && hasBonusOfType(Bonus::IN_FRENZY)) //frenzy for defender
  239. {
  240. return 0;
  241. }
  242. amax(ret, 0);
  243. return ret;
  244. }
  245. ui16 IBonusBearer::MaxHealth() const
  246. {
  247. return std::max(1, valOfBonuses(Bonus::STACK_HEALTH)); //never 0 or negative
  248. }
  249. ui32 IBonusBearer::getMinDamage() const
  250. {
  251. return valOfBonuses(Selector::typeSybtype(Bonus::CREATURE_DAMAGE, 0) || Selector::typeSybtype(Bonus::CREATURE_DAMAGE, 1));
  252. }
  253. ui32 IBonusBearer::getMaxDamage() const
  254. {
  255. return valOfBonuses(Selector::typeSybtype(Bonus::CREATURE_DAMAGE, 0) || Selector::typeSybtype(Bonus::CREATURE_DAMAGE, 2));
  256. }
  257. si32 IBonusBearer::manaLimit() const
  258. {
  259. return si32(getPrimSkillLevel(3) * (100.0f + valOfBonuses(Bonus::SECONDARY_SKILL_PREMY, 24)) / 10.0f);
  260. }
  261. int IBonusBearer::getPrimSkillLevel(int id) const
  262. {
  263. int ret = 0;
  264. if(id == PrimarySkill::ATTACK)
  265. ret = Attack();
  266. else if(id == PrimarySkill::DEFENSE)
  267. ret = Defense();
  268. else
  269. ret = valOfBonuses(Bonus::PRIMARY_SKILL, id);
  270. amax(ret, id/2); //minimal value is 0 for attack and defense and 1 for spell power and knowledge
  271. return ret;
  272. }
  273. Bonus * CBonusSystemNode::getBonus(const CSelector &selector)
  274. {
  275. Bonus *ret = bonuses.getFirst(selector);
  276. if(ret)
  277. return ret;
  278. FOREACH_PARENT(pname)
  279. {
  280. ret = pname->getBonus(selector);
  281. if (ret)
  282. return ret;
  283. }
  284. return NULL;
  285. }
  286. const Bonus * CBonusSystemNode::getBonus( const CSelector &selector ) const
  287. {
  288. return (const_cast<CBonusSystemNode*>(this))->getBonus(selector);
  289. }
  290. void CBonusSystemNode::getParents(TCNodes &out) const /*retreives list of parent nodes (nodes to inherit bonuses from) */
  291. {
  292. BOOST_FOREACH(const CBonusSystemNode *parent, parents)
  293. out.insert(parent);
  294. }
  295. void CBonusSystemNode::getParents(TNodes &out)
  296. {
  297. BOOST_FOREACH(const CBonusSystemNode *parent, parents)
  298. out.insert(const_cast<CBonusSystemNode*>(parent));
  299. }
  300. void CBonusSystemNode::getAllBonuses(BonusList &out, const CSelector &selector, const CSelector &limit, const CBonusSystemNode *root /*= NULL*/) const
  301. {
  302. FOREACH_CONST_PARENT(p)
  303. p->getBonuses(out, selector, limit, root ? root : this);
  304. bonuses.getBonuses(out, selector, limit);
  305. if(!root)
  306. out.limit(*this);
  307. }
  308. CBonusSystemNode::CBonusSystemNode()
  309. {
  310. nodeType = UNKNOWN;
  311. }
  312. CBonusSystemNode::~CBonusSystemNode()
  313. {
  314. detachFromAll();
  315. if(children.size())
  316. {
  317. tlog2 << "Warning: an orphaned child!\n";
  318. while(children.size())
  319. children.front()->detachFrom(this);
  320. }
  321. BOOST_FOREACH(Bonus *b, exportedBonuses)
  322. delete b;
  323. }
  324. void CBonusSystemNode::attachTo(CBonusSystemNode *parent)
  325. {
  326. assert(!vstd::contains(parents, parent));
  327. parents.push_back(parent);
  328. if(parent->actsAsBonusSourceOnly())
  329. parent->newRedDescendant(this);
  330. else
  331. newRedDescendant(parent);
  332. parent->newChildAttached(this);
  333. }
  334. void CBonusSystemNode::detachFrom(CBonusSystemNode *parent)
  335. {
  336. assert(vstd::contains(parents, parent));
  337. if(parent->actsAsBonusSourceOnly())
  338. parent->removedRedDescendant(this);
  339. else
  340. removedRedDescendant(parent);
  341. parents -= parent;
  342. parent->childDetached(this);
  343. }
  344. void CBonusSystemNode::popBonuses(const CSelector &s)
  345. {
  346. BonusList bl;
  347. exportedBonuses.getBonuses(bl, s);
  348. BOOST_FOREACH(Bonus *b, bl)
  349. removeBonus(b);
  350. BOOST_FOREACH(CBonusSystemNode *child, children)
  351. child->popBonuses(s);
  352. }
  353. // void CBonusSystemNode::addNewBonus(const Bonus &b)
  354. // {
  355. // addNewBonus(new Bonus(b));
  356. // }
  357. void CBonusSystemNode::addNewBonus(Bonus *b)
  358. {
  359. assert(!vstd::contains(exportedBonuses,b));
  360. exportedBonuses.push_back(b);
  361. exportBonus(b);
  362. }
  363. void CBonusSystemNode::removeBonus(Bonus *b)
  364. {
  365. exportedBonuses -= b;
  366. if(b->propagator)
  367. unpropagateBonus(b);
  368. else
  369. bonuses -= b;
  370. delNull(b);
  371. }
  372. bool CBonusSystemNode::isLimitedOnUs(Bonus *b) const
  373. {
  374. return b->limiter && b->limiter->limit(b, *this);
  375. }
  376. bool CBonusSystemNode::actsAsBonusSourceOnly() const
  377. {
  378. switch(nodeType)
  379. {
  380. case CREATURE:
  381. case ARTIFACT:
  382. case ARTIFACT_INSTANCE:
  383. return true;
  384. default:
  385. return false;
  386. }
  387. }
  388. void CBonusSystemNode::propagateBonus(Bonus * b)
  389. {
  390. if(b->propagator->shouldBeAttached(this))
  391. {
  392. bonuses.push_back(b);
  393. BONUS_LOG_LINE("#$# " << b->Description() << " #propagated to# " << nodeName());
  394. }
  395. FOREACH_RED_CHILD(child)
  396. child->propagateBonus(b);
  397. }
  398. void CBonusSystemNode::unpropagateBonus(Bonus * b)
  399. {
  400. if(b->propagator->shouldBeAttached(this))
  401. {
  402. bonuses -= b;
  403. BONUS_LOG_LINE("#$#" << b->Description() << " #is no longer propagated to# " << nodeName());
  404. }
  405. FOREACH_RED_CHILD(child)
  406. child->unpropagateBonus(b);
  407. }
  408. void CBonusSystemNode::newChildAttached(CBonusSystemNode *child)
  409. {
  410. assert(!vstd::contains(children, child));
  411. children.push_back(child);
  412. BONUS_LOG_LINE(child->nodeName() << " #attached to# " << nodeName());
  413. }
  414. void CBonusSystemNode::childDetached(CBonusSystemNode *child)
  415. {
  416. assert(vstd::contains(children, child));
  417. children -= child;
  418. BONUS_LOG_LINE(child->nodeName() << " #detached from# " << nodeName());
  419. }
  420. void CBonusSystemNode::detachFromAll()
  421. {
  422. while(parents.size())
  423. detachFrom(parents.front());
  424. }
  425. bool CBonusSystemNode::isIndependentNode() const
  426. {
  427. return parents.empty() && children.empty();
  428. }
  429. std::string CBonusSystemNode::nodeName() const
  430. {
  431. return description.size()
  432. ? description
  433. : std::string("Bonus system node of type ") + typeid(*this).name();
  434. }
  435. void CBonusSystemNode::deserializationFix()
  436. {
  437. exportBonuses();
  438. }
  439. void CBonusSystemNode::getRedParents(TNodes &out)
  440. {
  441. FOREACH_PARENT(pname)
  442. {
  443. if(pname->actsAsBonusSourceOnly())
  444. {
  445. out.insert(pname);
  446. }
  447. }
  448. if(!actsAsBonusSourceOnly())
  449. {
  450. BOOST_FOREACH(CBonusSystemNode *child, children)
  451. {
  452. out.insert(child);
  453. }
  454. }
  455. }
  456. void CBonusSystemNode::getRedChildren(TNodes &out)
  457. {
  458. FOREACH_PARENT(pname)
  459. {
  460. if(!pname->actsAsBonusSourceOnly())
  461. {
  462. out.insert(pname);
  463. }
  464. }
  465. if(actsAsBonusSourceOnly())
  466. {
  467. BOOST_FOREACH(CBonusSystemNode *child, children)
  468. {
  469. out.insert(child);
  470. }
  471. }
  472. }
  473. void CBonusSystemNode::newRedDescendant(CBonusSystemNode *descendant)
  474. {
  475. BOOST_FOREACH(Bonus *b, exportedBonuses)
  476. if(b->propagator)
  477. descendant->propagateBonus(b);
  478. FOREACH_RED_PARENT(parent)
  479. parent->newRedDescendant(descendant);
  480. }
  481. void CBonusSystemNode::removedRedDescendant(CBonusSystemNode *descendant)
  482. {
  483. BOOST_FOREACH(Bonus *b, exportedBonuses)
  484. if(b->propagator)
  485. descendant->unpropagateBonus(b);
  486. FOREACH_RED_PARENT(parent)
  487. parent->removedRedDescendant(descendant);
  488. }
  489. void CBonusSystemNode::getRedAncestors(TNodes &out)
  490. {
  491. getRedParents(out);
  492. FOREACH_RED_PARENT(p)
  493. p->getRedAncestors(out);
  494. }
  495. void CBonusSystemNode::getRedDescendants(TNodes &out)
  496. {
  497. getRedChildren(out);
  498. FOREACH_RED_CHILD(c)
  499. c->getRedChildren(out);
  500. }
  501. void CBonusSystemNode::battleTurnPassed()
  502. {
  503. BonusList bonusesCpy = exportedBonuses; //copy, because removing bonuses invalidates iters
  504. BOOST_FOREACH(Bonus *b, bonusesCpy)
  505. {
  506. if(b->duration & Bonus::N_TURNS)
  507. {
  508. b->turnsRemain--;
  509. if(b->turnsRemain <= 0)
  510. removeBonus(b);
  511. }
  512. }
  513. }
  514. void CBonusSystemNode::exportBonus(Bonus * b)
  515. {
  516. if(b->propagator)
  517. propagateBonus(b);
  518. else
  519. bonuses.push_back(b);
  520. }
  521. void CBonusSystemNode::exportBonuses()
  522. {
  523. BOOST_FOREACH(Bonus *b, exportedBonuses)
  524. exportBonus(b);
  525. }
  526. int NBonus::valOf(const CBonusSystemNode *obj, Bonus::BonusType type, int subtype /*= -1*/)
  527. {
  528. if(obj)
  529. return obj->valOfBonuses(type, subtype);
  530. return 0;
  531. }
  532. bool NBonus::hasOfType(const CBonusSystemNode *obj, Bonus::BonusType type, int subtype /*= -1*/)
  533. {
  534. if(obj)
  535. return obj->hasBonusOfType(type, subtype);
  536. return false;
  537. }
  538. void NBonus::getModifiersWDescr(const CBonusSystemNode *obj, TModDescr &out, Bonus::BonusType type, int subtype /*= -1 */)
  539. {
  540. if(obj)
  541. return obj->getModifiersWDescr(out, type, subtype);
  542. }
  543. int NBonus::getCount(const CBonusSystemNode *obj, int from, int id)
  544. {
  545. if(obj)
  546. return obj->getBonusesCount(from, id);
  547. return 0;
  548. }
  549. const CSpell * Bonus::sourceSpell() const
  550. {
  551. if(source == SPELL_EFFECT)
  552. return VLC->spellh->spells[sid];
  553. return NULL;
  554. }
  555. std::string Bonus::Description() const
  556. {
  557. if(description.size())
  558. return description;
  559. std::ostringstream str;
  560. str << std::showpos << val << " ";
  561. switch(source)
  562. {
  563. case ARTIFACT:
  564. str << VLC->arth->artifacts[sid]->Name();
  565. break;;
  566. case SPELL_EFFECT:
  567. str << VLC->spellh->spells[sid]->name;
  568. break;
  569. case CREATURE_ABILITY:
  570. str << VLC->creh->creatures[sid]->namePl;
  571. break;
  572. case SECONDARY_SKILL:
  573. str << VLC->generaltexth->skillName[sid]/* << " secondary skill"*/;
  574. break;
  575. }
  576. return str.str();
  577. }
  578. Bonus::Bonus(ui16 Dur, ui8 Type, ui8 Src, si32 Val, ui32 ID, std::string Desc, si32 Subtype/*=-1*/)
  579. : duration(Dur), type(Type), subtype(Subtype), source(Src), val(Val), sid(ID), description(Desc)
  580. {
  581. additionalInfo = -1;
  582. turnsRemain = 0;
  583. valType = ADDITIVE_VALUE;
  584. effectRange = NO_LIMIT;
  585. boost::algorithm::trim(description);
  586. }
  587. Bonus::Bonus(ui16 Dur, ui8 Type, ui8 Src, si32 Val, ui32 ID, si32 Subtype/*=-1*/, ui8 ValType /*= ADDITIVE_VALUE*/)
  588. : duration(Dur), type(Type), subtype(Subtype), source(Src), val(Val), sid(ID), valType(ValType)
  589. {
  590. additionalInfo = -1;
  591. turnsRemain = 0;
  592. effectRange = NO_LIMIT;
  593. }
  594. Bonus::Bonus()
  595. {
  596. subtype = -1;
  597. additionalInfo = -1;
  598. turnsRemain = 0;
  599. valType = ADDITIVE_VALUE;
  600. effectRange = NO_LIMIT;
  601. }
  602. Bonus::~Bonus()
  603. {
  604. }
  605. Bonus * Bonus::addLimiter(ILimiter *Limiter)
  606. {
  607. return addLimiter(boost::shared_ptr<ILimiter>(Limiter));
  608. }
  609. Bonus * Bonus::addLimiter(boost::shared_ptr<ILimiter> Limiter)
  610. {
  611. limiter = Limiter;
  612. return this;
  613. }
  614. Bonus * Bonus::addPropagator(IPropagator *Propagator)
  615. {
  616. return addPropagator(boost::shared_ptr<IPropagator>(Propagator));
  617. }
  618. Bonus * Bonus::addPropagator(boost::shared_ptr<IPropagator> Propagator)
  619. {
  620. propagator = Propagator;
  621. return this;
  622. }
  623. CSelector DLL_EXPORT operator&&(const CSelector &first, const CSelector &second)
  624. {
  625. return CSelectorsConjunction(first, second);
  626. }
  627. CSelector DLL_EXPORT operator||(const CSelector &first, const CSelector &second)
  628. {
  629. return CSelectorsAlternative(first, second);
  630. }
  631. namespace Selector
  632. {
  633. DLL_EXPORT CSelectFieldEqual<TBonusType> type(&Bonus::type, 0);
  634. DLL_EXPORT CSelectFieldEqual<TBonusSubtype> subtype(&Bonus::subtype, 0);
  635. DLL_EXPORT CSelectFieldEqual<si32> info(&Bonus::additionalInfo, 0);
  636. DLL_EXPORT CSelectFieldEqual<ui16> duration(&Bonus::duration, 0);
  637. DLL_EXPORT CSelectFieldEqual<ui8> sourceType(&Bonus::source, 0);
  638. DLL_EXPORT CSelectFieldEqual<ui8> effectRange(&Bonus::effectRange, Bonus::NO_LIMIT);
  639. DLL_EXPORT CWillLastTurns turns;
  640. CSelector DLL_EXPORT typeSybtype(TBonusType Type, TBonusSubtype Subtype)
  641. {
  642. return type(Type) && subtype(Subtype);
  643. }
  644. CSelector DLL_EXPORT typeSybtypeInfo(TBonusType type, TBonusSubtype subtype, si32 info)
  645. {
  646. return CSelectFieldEqual<TBonusType>(&Bonus::type, type) && CSelectFieldEqual<TBonusSubtype>(&Bonus::subtype, subtype) && CSelectFieldEqual<si32>(&Bonus::additionalInfo, info);
  647. }
  648. CSelector DLL_EXPORT source(ui8 source, ui32 sourceID)
  649. {
  650. return CSelectFieldEqual<ui8>(&Bonus::source, source) && CSelectFieldEqual<ui32>(&Bonus::sid, sourceID);
  651. }
  652. CSelector DLL_EXPORT durationType(ui16 duration)
  653. {
  654. return CSelectFieldEqual<ui16>(&Bonus::duration, duration);
  655. }
  656. CSelector DLL_EXPORT sourceTypeSel(ui8 source)
  657. {
  658. return CSelectFieldEqual<ui8>(&Bonus::source, source);
  659. }
  660. bool DLL_EXPORT matchesType(const CSelector &sel, TBonusType type)
  661. {
  662. Bonus dummy;
  663. dummy.type = type;
  664. return sel(&dummy);
  665. }
  666. bool DLL_EXPORT matchesTypeSubtype(const CSelector &sel, TBonusType type, TBonusSubtype subtype)
  667. {
  668. Bonus dummy;
  669. dummy.type = type;
  670. dummy.subtype = subtype;
  671. return sel(&dummy);
  672. }
  673. bool DLL_EXPORT positiveSpellEffects(const Bonus *b)
  674. {
  675. if(b->source == Bonus::SPELL_EFFECT)
  676. {
  677. CSpell *sp = VLC->spellh->spells[b->sid];
  678. return sp->positiveness == 1;
  679. }
  680. return false; //not a spell effect
  681. }
  682. }
  683. const CStack * retreiveStackBattle(const CBonusSystemNode *node)
  684. {
  685. switch(node->nodeType)
  686. {
  687. case CBonusSystemNode::STACK_BATTLE:
  688. return static_cast<const CStack*>(node);
  689. default:
  690. return NULL;
  691. }
  692. }
  693. const CStackInstance * retreiveStackInstance(const CBonusSystemNode *node)
  694. {
  695. switch(node->nodeType)
  696. {
  697. case CBonusSystemNode::STACK_INSTANCE:
  698. return (static_cast<const CStackInstance *>(node));
  699. case CBonusSystemNode::STACK_BATTLE:
  700. return (static_cast<const CStack*>(node))->base;
  701. default:
  702. return NULL;
  703. }
  704. }
  705. const CCreature * retrieveCreature(const CBonusSystemNode *node)
  706. {
  707. switch(node->nodeType)
  708. {
  709. case CBonusSystemNode::CREATURE:
  710. return (static_cast<const CCreature *>(node));
  711. default:
  712. const CStackInstance *csi = retreiveStackInstance(node);
  713. if(csi)
  714. return csi->type;
  715. return NULL;
  716. }
  717. }
  718. DLL_EXPORT std::ostream & operator<<(std::ostream &out, const BonusList &bonusList)
  719. {
  720. int i = 0;
  721. BOOST_FOREACH(const Bonus *b, bonusList)
  722. {
  723. out << "Bonus " << i++ << "\n" << *b << std::endl;
  724. }
  725. return out;
  726. }
  727. DLL_EXPORT std::ostream & operator<<(std::ostream &out, const Bonus &bonus)
  728. {
  729. for(std::map<std::string, int>::const_iterator i = bonusNameMap.begin(); i != bonusNameMap.end(); i++)
  730. if(i->second == bonus.type)
  731. out << "\tType: " << i->first << " \t";
  732. #define printField(field) out << "\t" #field ": " << (int)bonus.field << "\n"
  733. printField(val);
  734. printField(subtype);
  735. printField(duration);
  736. printField(source);
  737. printField(sid);
  738. printField(additionalInfo);
  739. printField(turnsRemain);
  740. printField(valType);
  741. printField(effectRange);
  742. #undef printField
  743. return out;
  744. }
  745. ILimiter::~ILimiter()
  746. {
  747. }
  748. bool ILimiter::limit(const Bonus *b, const CBonusSystemNode &node) const /*return true to drop the bonus */
  749. {
  750. return false;
  751. }
  752. bool CCreatureTypeLimiter::limit(const Bonus *b, const CBonusSystemNode &node) const
  753. {
  754. const CCreature *c = retrieveCreature(&node);
  755. if(!c)
  756. return true;
  757. return c != creature && (!includeUpgrades || !creature->isMyUpgrade(c));
  758. //drop bonus if it's not our creature and (we dont check upgrades or its not our upgrade)
  759. }
  760. CCreatureTypeLimiter::CCreatureTypeLimiter(const CCreature &Creature, ui8 IncludeUpgrades /*= true*/)
  761. :creature(&Creature), includeUpgrades(IncludeUpgrades)
  762. {
  763. }
  764. CCreatureTypeLimiter::CCreatureTypeLimiter()
  765. {
  766. creature = NULL;
  767. includeUpgrades = false;
  768. }
  769. HasAnotherBonusLimiter::HasAnotherBonusLimiter( TBonusType bonus )
  770. : type(bonus), subtype(0), isSubtypeRelevant(false)
  771. {
  772. }
  773. HasAnotherBonusLimiter::HasAnotherBonusLimiter( TBonusType bonus, TBonusSubtype _subtype )
  774. : type(bonus), subtype(_subtype), isSubtypeRelevant(true)
  775. {
  776. }
  777. bool HasAnotherBonusLimiter::limit( const Bonus *b, const CBonusSystemNode &node ) const
  778. {
  779. if(isSubtypeRelevant)
  780. {
  781. return !node.hasBonusOfType(static_cast<Bonus::BonusType>(type), subtype);
  782. }
  783. else
  784. {
  785. return !node.hasBonusOfType(static_cast<Bonus::BonusType>(type));
  786. }
  787. }
  788. IPropagator::~IPropagator()
  789. {
  790. }
  791. // CBonusSystemNode * IPropagator::getDestNode(CBonusSystemNode *source, CBonusSystemNode *redParent, CBonusSystemNode *redChild)
  792. // {
  793. // tlog1 << "IPropagator::getDestNode called!\n";
  794. // return source;
  795. // }
  796. bool IPropagator::shouldBeAttached(CBonusSystemNode *dest)
  797. {
  798. return false;
  799. }
  800. // CBonusSystemNode * CPropagatorNodeType::getDestNode(CBonusSystemNode *source, CBonusSystemNode *redParent, CBonusSystemNode *redChild)
  801. // {
  802. // return NULL;
  803. // }
  804. CPropagatorNodeType::CPropagatorNodeType()
  805. {
  806. }
  807. CPropagatorNodeType::CPropagatorNodeType(ui8 NodeType)
  808. : nodeType(NodeType)
  809. {
  810. }
  811. bool CPropagatorNodeType::shouldBeAttached(CBonusSystemNode *dest)
  812. {
  813. return nodeType == dest->nodeType;
  814. }
  815. CreatureNativeTerrainLimiter::CreatureNativeTerrainLimiter(int TerrainType)
  816. : terrainType(TerrainType)
  817. {
  818. }
  819. CreatureNativeTerrainLimiter::CreatureNativeTerrainLimiter()
  820. {
  821. }
  822. bool CreatureNativeTerrainLimiter::limit(const Bonus *b, const CBonusSystemNode &node) const
  823. {
  824. const CCreature *c = retrieveCreature(&node);
  825. return !c || !iswith(c->faction, 0, 9) || VLC->heroh->nativeTerrains[c->faction] != terrainType; //drop bonus for non-creatures or non-native residents
  826. //TODO neutral creatues
  827. }
  828. CreatureFactionLimiter::CreatureFactionLimiter(int Faction)
  829. : faction(Faction)
  830. {
  831. }
  832. CreatureFactionLimiter::CreatureFactionLimiter()
  833. {
  834. }
  835. bool CreatureFactionLimiter::limit(const Bonus *b, const CBonusSystemNode &node) const
  836. {
  837. const CCreature *c = retrieveCreature(&node);
  838. return !c || c->faction != faction; //drop bonus for non-creatures or non-native residents
  839. }
  840. CreatureAlignmentLimiter::CreatureAlignmentLimiter()
  841. {
  842. }
  843. CreatureAlignmentLimiter::CreatureAlignmentLimiter(si8 Alignment)
  844. : alignment(Alignment)
  845. {
  846. }
  847. bool CreatureAlignmentLimiter::limit(const Bonus *b, const CBonusSystemNode &node) const
  848. {
  849. const CCreature *c = retrieveCreature(&node);
  850. if(!c)
  851. return true;
  852. switch(alignment)
  853. {
  854. case GOOD:
  855. return !c->isGood(); //if not good -> return true (drop bonus)
  856. case NEUTRAL:
  857. return c->isEvil() || c->isGood();
  858. case EVIL:
  859. return !c->isEvil();
  860. default:
  861. tlog1 << "Warning: illegal alignment in limiter!\n";
  862. return true;
  863. }
  864. }
  865. RankRangeLimiter::RankRangeLimiter(ui8 Min, ui8 Max)
  866. :minRank(Min), maxRank(Max)
  867. {
  868. }
  869. RankRangeLimiter::RankRangeLimiter()
  870. {
  871. minRank = maxRank = -1;
  872. }
  873. bool RankRangeLimiter::limit( const Bonus *b, const CBonusSystemNode &node ) const
  874. {
  875. const CStackInstance *csi = retreiveStackInstance(&node);
  876. if(csi)
  877. return csi->getExpRank() < minRank || csi->getExpRank() > maxRank;
  878. return true;
  879. }
  880. bool StackOwnerLimiter::limit(const Bonus *b, const CBonusSystemNode &node) const
  881. {
  882. const CStack *s = retreiveStackBattle(&node);
  883. if(s)
  884. return s->owner != owner;
  885. const CStackInstance *csi = retreiveStackInstance(&node);
  886. if(csi && csi->armyObj)
  887. return csi->armyObj->tempOwner != owner;
  888. return true;
  889. }
  890. StackOwnerLimiter::StackOwnerLimiter()
  891. : owner(-1)
  892. {
  893. }
  894. StackOwnerLimiter::StackOwnerLimiter(ui8 Owner)
  895. : owner(Owner)
  896. {
  897. }