HeroBonus.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  1. #define VCMI_DLL
  2. #include "HeroBonus.h"
  3. #include <boost/foreach.hpp>
  4. #include "VCMI_Lib.h"
  5. #include "../hch/CSpellHandler.h"
  6. #include <sstream>
  7. #include "../hch/CCreatureHandler.h"
  8. #include <boost/assign/list_of.hpp>
  9. #include "CCreatureSet.h"
  10. #include <boost/algorithm/string/trim.hpp>
  11. #define FOREACH_CONST_PARENT(pname, source) TCNodes parents; getParents(parents, source); BOOST_FOREACH(const CBonusSystemNode *pname, parents)
  12. #define FOREACH_PARENT(pname, source) TNodes parents; getParents(parents, source); BOOST_FOREACH(CBonusSystemNode *pname, parents)
  13. #define BONUS_NAME(x) ( #x, Bonus::x )
  14. DLL_EXPORT const std::map<std::string, int> bonusNameMap = boost::assign::map_list_of BONUS_LIST;
  15. #undef BONUS_NAME
  16. int DLL_EXPORT BonusList::totalValue() const
  17. {
  18. int base = 0;
  19. int percentToBase = 0;
  20. int percentToAll = 0;
  21. int additive = 0;
  22. int indepMax = 0;
  23. bool hasIndepMax = false;
  24. for(const_iterator i = begin(); i != end(); i++)
  25. {
  26. switch(i->valType)
  27. {
  28. case Bonus::BASE_NUMBER:
  29. base += i->val;
  30. break;
  31. case Bonus::PERCENT_TO_ALL:
  32. percentToAll += i->val;
  33. break;
  34. case Bonus::PERCENT_TO_BASE:
  35. percentToBase += i->val;
  36. break;
  37. case Bonus::ADDITIVE_VALUE:
  38. additive += i->val;
  39. break;
  40. case Bonus::INDEPENDENT_MAX:
  41. if (!indepMax)
  42. {
  43. indepMax = i->val;
  44. hasIndepMax = true;
  45. }
  46. else
  47. {
  48. amax(indepMax, i->val);
  49. }
  50. break;
  51. }
  52. }
  53. int modifiedBase = base + (base * percentToBase) / 100;
  54. modifiedBase += additive;
  55. int valFirst = (modifiedBase * (100 + percentToAll)) / 100;
  56. if (hasIndepMax)
  57. return std::max(valFirst, indepMax);
  58. else
  59. return valFirst;
  60. }
  61. const DLL_EXPORT Bonus * BonusList::getFirst(const CSelector &selector) const
  62. {
  63. for (const_iterator i = begin(); i != end(); i++)
  64. if(selector(*i))
  65. return &*i;
  66. return NULL;
  67. }
  68. DLL_EXPORT Bonus * BonusList::getFirst(const CSelector &select)
  69. {
  70. for (iterator i = begin(); i != end(); i++)
  71. if(select(*i))
  72. return &*i;
  73. return NULL;
  74. }
  75. void DLL_EXPORT BonusList::getModifiersWDescr(TModDescr &out) const
  76. {
  77. for(const_iterator i = begin(); i != end(); i++)
  78. out.push_back(std::make_pair(i->val, i->Description()));
  79. }
  80. void DLL_EXPORT BonusList::getBonuses(BonusList &out, const CSelector &selector, const CBonusSystemNode *source /*= NULL*/) const
  81. {
  82. for(const_iterator i = begin(); i != end(); i++)
  83. if(selector(*i) && i->effectRange == Bonus::NO_LIMIT)
  84. out.push_back(*i);
  85. }
  86. void DLL_EXPORT BonusList::getBonuses(BonusList &out, const CSelector &selector, const CSelector &limit, const CBonusSystemNode *source /*= NULL*/) const
  87. {
  88. for(const_iterator i = begin(); i != end(); i++)
  89. if(selector(*i) && (!limit || limit(*i)))
  90. out.push_back(*i);
  91. }
  92. namespace HHLP
  93. {
  94. class SourceComp
  95. {
  96. public:
  97. Bonus::BonusSource src;
  98. SourceComp(Bonus::BonusSource _src) : src(src)
  99. {
  100. }
  101. bool operator()(const Bonus & bon)
  102. {
  103. return bon.source == src;
  104. }
  105. };
  106. }
  107. void DLL_EXPORT BonusList::removeSpells(Bonus::BonusSource sourceType)
  108. {
  109. std::remove_if(begin(), end(), HHLP::SourceComp(sourceType));
  110. }
  111. void BonusList::limit(const CBonusSystemNode &node)
  112. {
  113. limit_start:
  114. for(iterator i = begin(); i != end(); i++)
  115. {
  116. if(i->limiter && i->limiter->limit(*i, node))
  117. {
  118. iterator toErase = i;
  119. if(i != begin())
  120. {
  121. i--;
  122. erase(toErase);
  123. }
  124. else
  125. {
  126. erase(toErase);
  127. goto limit_start;
  128. }
  129. }
  130. }
  131. }
  132. int CBonusSystemNode::valOfBonuses(Bonus::BonusType type, const CSelector &selector) const
  133. {
  134. return valOfBonuses(Selector::type(type) && selector);
  135. }
  136. int CBonusSystemNode::valOfBonuses(Bonus::BonusType type, int subtype /*= -1*/) const
  137. {
  138. CSelector s = Selector::type(type);
  139. if(subtype != -1)
  140. s = s && Selector::subtype(subtype);
  141. return valOfBonuses(s);
  142. }
  143. int CBonusSystemNode::valOfBonuses(const CSelector &selector, const CBonusSystemNode *root/* = NULL*/) const
  144. {
  145. BonusList hlp;
  146. getBonuses(hlp, selector, root);
  147. return hlp.totalValue();
  148. }
  149. bool CBonusSystemNode::hasBonus(const CSelector &selector, const CBonusSystemNode *root/* = NULL*/) const
  150. {
  151. return getBonuses(selector).size() > 0;
  152. }
  153. bool CBonusSystemNode::hasBonusOfType(Bonus::BonusType type, int subtype /*= -1*/) const
  154. {
  155. CSelector s = Selector::type(type);
  156. if(subtype != -1)
  157. s = s && Selector::subtype(subtype);
  158. return hasBonus(s);
  159. }
  160. Bonus * CBonusSystemNode::getBonus(const CSelector &selector)
  161. {
  162. Bonus *ret = bonuses.getFirst(selector);
  163. if(ret)
  164. return ret;
  165. //FOREACH_PARENT(p, this)
  166. TNodes parents;
  167. getParents (parents, this);
  168. BOOST_FOREACH (CBonusSystemNode *pname, parents)
  169. {
  170. if(ret = pname->getBonus(selector))
  171. return ret;
  172. }
  173. return NULL;
  174. }
  175. void CBonusSystemNode::getModifiersWDescr(TModDescr &out, Bonus::BonusType type, int subtype /*= -1 */) const
  176. {
  177. getModifiersWDescr(out, Selector::typeSybtype(type, subtype));
  178. }
  179. void CBonusSystemNode::getModifiersWDescr(TModDescr &out, const CSelector &selector, const CBonusSystemNode *root /*= NULL*/) const
  180. {
  181. getBonuses(selector).getModifiersWDescr(out);
  182. }
  183. int CBonusSystemNode::getBonusesCount(int from, int id) const
  184. {
  185. return getBonusesCount(Selector::source(from, id));
  186. }
  187. int CBonusSystemNode::getBonusesCount(const CSelector &selector, const CBonusSystemNode *root/* = NULL*/) const
  188. {
  189. return getBonuses(selector, root).size();
  190. }
  191. void CBonusSystemNode::getParents(TCNodes &out, const CBonusSystemNode *root) const /*retreives list of parent nodes (nodes to inherit bonuses from) */
  192. {
  193. return;
  194. }
  195. void CBonusSystemNode::getParents(TNodes &out, const CBonusSystemNode *root /*= NULL*/)
  196. {
  197. //de-constify above
  198. TCNodes hlp;
  199. getParents(hlp, root);
  200. BOOST_FOREACH(const CBonusSystemNode *pname, hlp)
  201. out.insert(const_cast<CBonusSystemNode*>(pname));
  202. }
  203. void CBonusSystemNode::getBonuses(BonusList &out, const CSelector &selector, const CBonusSystemNode *root /*= NULL*/) const
  204. {
  205. //FOREACH_CONST_PARENT(p, root ? root : this) //unwinded macro
  206. TCNodes parents;
  207. getParents(parents, root ? root : this);
  208. BOOST_FOREACH(const CBonusSystemNode *p, parents)
  209. p->getBonuses(out, selector, root ? root : this);
  210. bonuses.getBonuses(out, selector);
  211. if(!root)
  212. out.limit(*this);
  213. }
  214. BonusList CBonusSystemNode::getBonuses(const CSelector &selector, const CBonusSystemNode *root /*= NULL*/) const
  215. {
  216. BonusList ret;
  217. getBonuses(ret, selector, root);
  218. return ret;
  219. }
  220. void CBonusSystemNode::getBonuses(BonusList &out, const CSelector &selector, const CSelector &limit, const CBonusSystemNode *root /*= NULL*/) const
  221. {
  222. //FOREACH_CONST_PARENT(p, root ? root : this) //unwinded macro
  223. TCNodes parents;
  224. getParents(parents, root ? root : this);
  225. BOOST_FOREACH(const CBonusSystemNode *p, parents)
  226. p->getBonuses(out, selector, limit, root ? root : this);
  227. bonuses.getBonuses(out, selector, limit);
  228. if(!root)
  229. out.limit(*this);
  230. }
  231. BonusList CBonusSystemNode::getBonuses(const CSelector &selector, const CSelector &limit, const CBonusSystemNode *root /*= NULL*/) const
  232. {
  233. BonusList ret;
  234. getBonuses(ret, selector, limit, root);
  235. return ret;
  236. }
  237. bool CBonusSystemNode::hasBonusFrom(ui8 source, ui32 sourceID) const
  238. {
  239. return hasBonus(Selector::source(source,sourceID));
  240. }
  241. int CBonusSystemNode::MoraleVal() const
  242. {
  243. if(hasBonusOfType(Bonus::NON_LIVING) || hasBonusOfType(Bonus::UNDEAD) ||
  244. hasBonusOfType(Bonus::NO_MORALE) || hasBonusOfType(Bonus::SIEGE_WEAPON))
  245. return 0;
  246. int ret = valOfBonuses(Selector::type(Bonus::MORALE));
  247. if(hasBonusOfType(Bonus::SELF_MORALE)) //eg. minotaur
  248. amax(ret, +1);
  249. return abetw(ret, -3, +3);
  250. }
  251. int CBonusSystemNode::LuckVal() const
  252. {
  253. if(hasBonusOfType(Bonus::NO_LUCK))
  254. return 0;
  255. int ret = valOfBonuses(Selector::type(Bonus::LUCK));
  256. if(hasBonusOfType(Bonus::SELF_LUCK)) //eg. halfling
  257. amax(ret, +1);
  258. return abetw(ret, -3, +3);
  259. }
  260. si32 CBonusSystemNode::Attack() const
  261. {
  262. si32 ret = valOfBonuses(Bonus::PRIMARY_SKILL, PrimarySkill::ATTACK);
  263. if(int frenzyPower = valOfBonuses(Bonus::IN_FRENZY)) //frenzy for attacker
  264. {
  265. ret += frenzyPower * Defense(false);
  266. }
  267. return ret;
  268. }
  269. si32 CBonusSystemNode::Defense(bool withFrenzy /*= true*/) const
  270. {
  271. si32 ret = valOfBonuses(Bonus::PRIMARY_SKILL, PrimarySkill::DEFENSE);
  272. if(withFrenzy && hasBonusOfType(Bonus::IN_FRENZY)) //frenzy for defender
  273. {
  274. return 0;
  275. }
  276. return ret;
  277. }
  278. ui16 CBonusSystemNode::MaxHealth() const
  279. {
  280. return valOfBonuses(Bonus::STACK_HEALTH);
  281. }
  282. ui32 CBonusSystemNode::getMinDamage() const
  283. {
  284. return valOfBonuses(Selector::typeSybtype(Bonus::CREATURE_DAMAGE, 0) || Selector::typeSybtype(Bonus::CREATURE_DAMAGE, 1));
  285. }
  286. ui32 CBonusSystemNode::getMaxDamage() const
  287. {
  288. return valOfBonuses(Selector::typeSybtype(Bonus::CREATURE_DAMAGE, 0) || Selector::typeSybtype(Bonus::CREATURE_DAMAGE, 2));
  289. }
  290. CBonusSystemNode::CBonusSystemNode()
  291. {
  292. nodeType = UNKNOWN;
  293. }
  294. CBonusSystemNode::~CBonusSystemNode()
  295. {
  296. }
  297. int NBonus::valOf(const CBonusSystemNode *obj, Bonus::BonusType type, int subtype /*= -1*/)
  298. {
  299. if(obj)
  300. return obj->valOfBonuses(type, subtype);
  301. return 0;
  302. }
  303. bool NBonus::hasOfType(const CBonusSystemNode *obj, Bonus::BonusType type, int subtype /*= -1*/)
  304. {
  305. if(obj)
  306. return obj->hasBonusOfType(type, subtype);
  307. return false;
  308. }
  309. void NBonus::getModifiersWDescr(const CBonusSystemNode *obj, TModDescr &out, Bonus::BonusType type, int subtype /*= -1 */)
  310. {
  311. if(obj)
  312. return obj->getModifiersWDescr(out, type, subtype);
  313. }
  314. int NBonus::getCount(const CBonusSystemNode *obj, int from, int id)
  315. {
  316. if(obj)
  317. return obj->getBonusesCount(from, id);
  318. return 0;
  319. }
  320. const CSpell * Bonus::sourceSpell() const
  321. {
  322. if(source == SPELL_EFFECT)
  323. return &VLC->spellh->spells[id];
  324. return NULL;
  325. }
  326. std::string Bonus::Description() const
  327. {
  328. if(description.size())
  329. return description;
  330. std::ostringstream str;
  331. if(val < 0)
  332. str << '-';
  333. else if(val > 0)
  334. str << '+';
  335. str << val << " ";
  336. switch(source)
  337. {
  338. case CREATURE_ABILITY:
  339. str << VLC->creh->creatures[id]->namePl;
  340. break;
  341. }
  342. return str.str();
  343. }
  344. Bonus::Bonus(ui8 Dur, ui8 Type, ui8 Src, si32 Val, ui32 ID, std::string Desc, si32 Subtype/*=-1*/)
  345. : duration(Dur), type(Type), subtype(Subtype), source(Src), val(Val), id(ID), description(Desc)
  346. {
  347. additionalInfo = -1;
  348. turnsRemain = 0;
  349. valType = ADDITIVE_VALUE;
  350. effectRange = NO_LIMIT;
  351. limiter = NULL;
  352. boost::algorithm::trim(description);
  353. }
  354. Bonus::Bonus(ui8 Dur, ui8 Type, ui8 Src, si32 Val, ui32 ID, si32 Subtype/*=-1*/, ui8 ValType /*= ADDITIVE_VALUE*/)
  355. : duration(Dur), type(Type), subtype(Subtype), source(Src), val(Val), id(ID), valType(ValType)
  356. {
  357. additionalInfo = -1;
  358. turnsRemain = 0;
  359. effectRange = NO_LIMIT;
  360. limiter = NULL;
  361. }
  362. Bonus::Bonus()
  363. {
  364. subtype = -1;
  365. additionalInfo = -1;
  366. turnsRemain = 0;
  367. valType = ADDITIVE_VALUE;
  368. effectRange = NO_LIMIT;
  369. limiter = NULL;
  370. }
  371. CSelector DLL_EXPORT operator&&(const CSelector &first, const CSelector &second)
  372. {
  373. return CSelectorsConjunction(first, second);
  374. }
  375. CSelector DLL_EXPORT operator||(const CSelector &first, const CSelector &second)
  376. {
  377. return CSelectorsAlternative(first, second);
  378. }
  379. namespace Selector
  380. {
  381. DLL_EXPORT CSelectFieldEqual<TBonusType> type(&Bonus::type, 0);
  382. DLL_EXPORT CSelectFieldEqual<TBonusSubtype> subtype(&Bonus::subtype, 0);
  383. DLL_EXPORT CSelectFieldEqual<si32> info(&Bonus::additionalInfo, 0);
  384. DLL_EXPORT CSelectFieldEqual<ui8> sourceType(&Bonus::source, 0);
  385. DLL_EXPORT CSelectFieldEqual<ui8> effectRange(&Bonus::effectRange, Bonus::NO_LIMIT);
  386. DLL_EXPORT CWillLastTurns turns;;
  387. CSelector DLL_EXPORT typeSybtype(TBonusType Type, TBonusSubtype Subtype)
  388. {
  389. return type(Type) && subtype(Subtype);
  390. }
  391. CSelector DLL_EXPORT typeSybtypeInfo(TBonusType type, TBonusSubtype subtype, si32 info)
  392. {
  393. return CSelectFieldEqual<TBonusType>(&Bonus::type, type) && CSelectFieldEqual<TBonusSubtype>(&Bonus::subtype, subtype) && CSelectFieldEqual<si32>(&Bonus::additionalInfo, info);
  394. }
  395. CSelector DLL_EXPORT source(ui8 source, ui32 sourceID)
  396. {
  397. return CSelectFieldEqual<ui8>(&Bonus::source, source) && CSelectFieldEqual<ui32>(&Bonus::id, sourceID);
  398. }
  399. CSelector DLL_EXPORT sourceTypeSel(ui8 source)
  400. {
  401. return CSelectFieldEqual<ui8>(&Bonus::source, source);
  402. }
  403. bool DLL_EXPORT matchesType(const CSelector &sel, TBonusType type)
  404. {
  405. Bonus dummy;
  406. dummy.type = type;
  407. return sel(dummy);
  408. }
  409. bool DLL_EXPORT matchesTypeSubtype(const CSelector &sel, TBonusType type, TBonusSubtype subtype)
  410. {
  411. Bonus dummy;
  412. dummy.type = type;
  413. dummy.subtype = subtype;
  414. return sel(dummy);
  415. }
  416. }
  417. DLL_EXPORT std::ostream & operator<<(std::ostream &out, const BonusList &bonusList)
  418. {
  419. int i = 0;
  420. BOOST_FOREACH(const Bonus &b, bonusList)
  421. {
  422. out << "Bonus " << i++ << "\n" << b << std::endl;
  423. }
  424. return out;
  425. }
  426. DLL_EXPORT std::ostream & operator<<(std::ostream &out, const Bonus &bonus)
  427. {
  428. for(std::map<std::string, int>::const_iterator i = bonusNameMap.begin(); i != bonusNameMap.end(); i++)
  429. if(i->second == bonus.type)
  430. out << "\tType: " << i->first << " \t";
  431. #define printField(field) out << "\t" #field ": " << (int)bonus.field << "\n"
  432. printField(val);
  433. printField(subtype);
  434. printField(duration);
  435. printField(source);
  436. printField(id);
  437. printField(additionalInfo);
  438. printField(turnsRemain);
  439. printField(valType);
  440. printField(effectRange);
  441. #undef printField
  442. return out;
  443. }
  444. ILimiter::~ILimiter()
  445. {
  446. }
  447. bool ILimiter::limit(const Bonus &b, const CBonusSystemNode &node) const /*return true to drop the bonus */
  448. {
  449. return false;
  450. }
  451. bool CCreatureTypeLimiter::limit(const Bonus &b, const CBonusSystemNode &node) const
  452. {
  453. switch (node.nodeType)
  454. {
  455. case CBonusSystemNode::STACK:
  456. {
  457. const CCreature *c = (static_cast<const CStackInstance *>(&node))->type;
  458. return c != creature && (!includeUpgrades || !creature->isMyUpgrade(c));
  459. } //drop bonus if it's not our creature and (we dont check upgrades or its not our upgrade)
  460. break;
  461. case CBonusSystemNode::CREATURE:
  462. {
  463. const CCreature *c = (static_cast<const CCreature *>(&node));
  464. return c != creature && (!includeUpgrades || !creature->isMyUpgrade(c));
  465. }
  466. break;
  467. default:
  468. return true;
  469. }
  470. }
  471. CCreatureTypeLimiter::CCreatureTypeLimiter(const CCreature &Creature, ui8 IncludeUpgrades /*= true*/)
  472. :creature(&Creature), includeUpgrades(IncludeUpgrades)
  473. {
  474. }
  475. CCreatureTypeLimiter::CCreatureTypeLimiter()
  476. {
  477. creature = NULL;
  478. includeUpgrades = false;
  479. }
  480. HasAnotherBonusLimiter::HasAnotherBonusLimiter( TBonusType bonus )
  481. : type(bonus), subtype(0), isSubtypeRelevant(false)
  482. {
  483. }
  484. HasAnotherBonusLimiter::HasAnotherBonusLimiter( TBonusType bonus, TBonusSubtype _subtype )
  485. : type(bonus), subtype(_subtype), isSubtypeRelevant(true)
  486. {
  487. }
  488. bool HasAnotherBonusLimiter::limit( const Bonus &b, const CBonusSystemNode &node ) const
  489. {
  490. if(isSubtypeRelevant)
  491. {
  492. return !node.hasBonusOfType(static_cast<Bonus::BonusType>(type), subtype);
  493. }
  494. else
  495. {
  496. return !node.hasBonusOfType(static_cast<Bonus::BonusType>(type));
  497. }
  498. }