HeroBonus.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  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. ret = pname->getBonus(selector);
  171. if (ret)
  172. return ret;
  173. }
  174. return NULL;
  175. }
  176. void CBonusSystemNode::getModifiersWDescr(TModDescr &out, Bonus::BonusType type, int subtype /*= -1 */) const
  177. {
  178. getModifiersWDescr(out, Selector::typeSybtype(type, subtype));
  179. }
  180. void CBonusSystemNode::getModifiersWDescr(TModDescr &out, const CSelector &selector, const CBonusSystemNode *root /*= NULL*/) const
  181. {
  182. getBonuses(selector).getModifiersWDescr(out);
  183. }
  184. int CBonusSystemNode::getBonusesCount(int from, int id) const
  185. {
  186. return getBonusesCount(Selector::source(from, id));
  187. }
  188. int CBonusSystemNode::getBonusesCount(const CSelector &selector, const CBonusSystemNode *root/* = NULL*/) const
  189. {
  190. return getBonuses(selector, root).size();
  191. }
  192. void CBonusSystemNode::getParents(TCNodes &out, const CBonusSystemNode *root) const /*retreives list of parent nodes (nodes to inherit bonuses from) */
  193. {
  194. return;
  195. }
  196. void CBonusSystemNode::getParents(TNodes &out, const CBonusSystemNode *root /*= NULL*/)
  197. {
  198. //de-constify above
  199. TCNodes hlp;
  200. getParents(hlp, root);
  201. BOOST_FOREACH(const CBonusSystemNode *pname, hlp)
  202. out.insert(const_cast<CBonusSystemNode*>(pname));
  203. }
  204. void CBonusSystemNode::getBonuses(BonusList &out, const CSelector &selector, const CBonusSystemNode *root /*= NULL*/) const
  205. {
  206. //FOREACH_CONST_PARENT(p, root ? root : this) //unwinded macro
  207. TCNodes parents;
  208. getParents(parents, root ? root : this);
  209. BOOST_FOREACH(const CBonusSystemNode *p, parents)
  210. p->getBonuses(out, selector, root ? root : this);
  211. bonuses.getBonuses(out, selector);
  212. if(!root)
  213. out.limit(*this);
  214. }
  215. BonusList CBonusSystemNode::getBonuses(const CSelector &selector, const CBonusSystemNode *root /*= NULL*/) const
  216. {
  217. BonusList ret;
  218. getBonuses(ret, selector, root);
  219. return ret;
  220. }
  221. void CBonusSystemNode::getBonuses(BonusList &out, const CSelector &selector, const CSelector &limit, const CBonusSystemNode *root /*= NULL*/) const
  222. {
  223. //FOREACH_CONST_PARENT(p, root ? root : this) //unwinded macro
  224. TCNodes parents;
  225. getParents(parents, root ? root : this);
  226. BOOST_FOREACH(const CBonusSystemNode *p, parents)
  227. p->getBonuses(out, selector, limit, root ? root : this);
  228. bonuses.getBonuses(out, selector, limit);
  229. if(!root)
  230. out.limit(*this);
  231. }
  232. BonusList CBonusSystemNode::getBonuses(const CSelector &selector, const CSelector &limit, const CBonusSystemNode *root /*= NULL*/) const
  233. {
  234. BonusList ret;
  235. getBonuses(ret, selector, limit, root);
  236. return ret;
  237. }
  238. bool CBonusSystemNode::hasBonusFrom(ui8 source, ui32 sourceID) const
  239. {
  240. return hasBonus(Selector::source(source,sourceID));
  241. }
  242. int CBonusSystemNode::MoraleVal() const
  243. {
  244. if(hasBonusOfType(Bonus::NON_LIVING) || hasBonusOfType(Bonus::UNDEAD) ||
  245. hasBonusOfType(Bonus::NO_MORALE) || hasBonusOfType(Bonus::SIEGE_WEAPON))
  246. return 0;
  247. int ret = valOfBonuses(Selector::type(Bonus::MORALE));
  248. if(hasBonusOfType(Bonus::SELF_MORALE)) //eg. minotaur
  249. amax(ret, +1);
  250. return abetw(ret, -3, +3);
  251. }
  252. int CBonusSystemNode::LuckVal() const
  253. {
  254. if(hasBonusOfType(Bonus::NO_LUCK))
  255. return 0;
  256. int ret = valOfBonuses(Selector::type(Bonus::LUCK));
  257. if(hasBonusOfType(Bonus::SELF_LUCK)) //eg. halfling
  258. amax(ret, +1);
  259. return abetw(ret, -3, +3);
  260. }
  261. si32 CBonusSystemNode::Attack() const
  262. {
  263. si32 ret = valOfBonuses(Bonus::PRIMARY_SKILL, PrimarySkill::ATTACK);
  264. if(int frenzyPower = valOfBonuses(Bonus::IN_FRENZY)) //frenzy for attacker
  265. {
  266. ret += frenzyPower * Defense(false);
  267. }
  268. return ret;
  269. }
  270. si32 CBonusSystemNode::Defense(bool withFrenzy /*= true*/) const
  271. {
  272. si32 ret = valOfBonuses(Bonus::PRIMARY_SKILL, PrimarySkill::DEFENSE);
  273. if(withFrenzy && hasBonusOfType(Bonus::IN_FRENZY)) //frenzy for defender
  274. {
  275. return 0;
  276. }
  277. return ret;
  278. }
  279. ui16 CBonusSystemNode::MaxHealth() const
  280. {
  281. return valOfBonuses(Bonus::STACK_HEALTH);
  282. }
  283. ui32 CBonusSystemNode::getMinDamage() const
  284. {
  285. return valOfBonuses(Selector::typeSybtype(Bonus::CREATURE_DAMAGE, 0) || Selector::typeSybtype(Bonus::CREATURE_DAMAGE, 1));
  286. }
  287. ui32 CBonusSystemNode::getMaxDamage() const
  288. {
  289. return valOfBonuses(Selector::typeSybtype(Bonus::CREATURE_DAMAGE, 0) || Selector::typeSybtype(Bonus::CREATURE_DAMAGE, 2));
  290. }
  291. CBonusSystemNode::CBonusSystemNode()
  292. {
  293. nodeType = UNKNOWN;
  294. }
  295. CBonusSystemNode::~CBonusSystemNode()
  296. {
  297. }
  298. int NBonus::valOf(const CBonusSystemNode *obj, Bonus::BonusType type, int subtype /*= -1*/)
  299. {
  300. if(obj)
  301. return obj->valOfBonuses(type, subtype);
  302. return 0;
  303. }
  304. bool NBonus::hasOfType(const CBonusSystemNode *obj, Bonus::BonusType type, int subtype /*= -1*/)
  305. {
  306. if(obj)
  307. return obj->hasBonusOfType(type, subtype);
  308. return false;
  309. }
  310. void NBonus::getModifiersWDescr(const CBonusSystemNode *obj, TModDescr &out, Bonus::BonusType type, int subtype /*= -1 */)
  311. {
  312. if(obj)
  313. return obj->getModifiersWDescr(out, type, subtype);
  314. }
  315. int NBonus::getCount(const CBonusSystemNode *obj, int from, int id)
  316. {
  317. if(obj)
  318. return obj->getBonusesCount(from, id);
  319. return 0;
  320. }
  321. const CSpell * Bonus::sourceSpell() const
  322. {
  323. if(source == SPELL_EFFECT)
  324. return &VLC->spellh->spells[id];
  325. return NULL;
  326. }
  327. std::string Bonus::Description() const
  328. {
  329. if(description.size())
  330. return description;
  331. std::ostringstream str;
  332. if(val < 0)
  333. str << '-';
  334. else if(val > 0)
  335. str << '+';
  336. str << val << " ";
  337. switch(source)
  338. {
  339. case CREATURE_ABILITY:
  340. str << VLC->creh->creatures[id]->namePl;
  341. break;
  342. }
  343. return str.str();
  344. }
  345. Bonus::Bonus(ui8 Dur, ui8 Type, ui8 Src, si32 Val, ui32 ID, std::string Desc, si32 Subtype/*=-1*/)
  346. : duration(Dur), type(Type), subtype(Subtype), source(Src), val(Val), id(ID), description(Desc)
  347. {
  348. additionalInfo = -1;
  349. turnsRemain = 0;
  350. valType = ADDITIVE_VALUE;
  351. effectRange = NO_LIMIT;
  352. limiter = NULL;
  353. boost::algorithm::trim(description);
  354. }
  355. Bonus::Bonus(ui8 Dur, ui8 Type, ui8 Src, si32 Val, ui32 ID, si32 Subtype/*=-1*/, ui8 ValType /*= ADDITIVE_VALUE*/)
  356. : duration(Dur), type(Type), subtype(Subtype), source(Src), val(Val), id(ID), valType(ValType)
  357. {
  358. additionalInfo = -1;
  359. turnsRemain = 0;
  360. effectRange = NO_LIMIT;
  361. limiter = NULL;
  362. }
  363. Bonus::Bonus()
  364. {
  365. subtype = -1;
  366. additionalInfo = -1;
  367. turnsRemain = 0;
  368. valType = ADDITIVE_VALUE;
  369. effectRange = NO_LIMIT;
  370. limiter = NULL;
  371. }
  372. CSelector DLL_EXPORT operator&&(const CSelector &first, const CSelector &second)
  373. {
  374. return CSelectorsConjunction(first, second);
  375. }
  376. CSelector DLL_EXPORT operator||(const CSelector &first, const CSelector &second)
  377. {
  378. return CSelectorsAlternative(first, second);
  379. }
  380. namespace Selector
  381. {
  382. DLL_EXPORT CSelectFieldEqual<TBonusType> type(&Bonus::type, 0);
  383. DLL_EXPORT CSelectFieldEqual<TBonusSubtype> subtype(&Bonus::subtype, 0);
  384. DLL_EXPORT CSelectFieldEqual<si32> info(&Bonus::additionalInfo, 0);
  385. DLL_EXPORT CSelectFieldEqual<ui8> sourceType(&Bonus::source, 0);
  386. DLL_EXPORT CSelectFieldEqual<ui8> effectRange(&Bonus::effectRange, Bonus::NO_LIMIT);
  387. DLL_EXPORT CWillLastTurns turns;;
  388. CSelector DLL_EXPORT typeSybtype(TBonusType Type, TBonusSubtype Subtype)
  389. {
  390. return type(Type) && subtype(Subtype);
  391. }
  392. CSelector DLL_EXPORT typeSybtypeInfo(TBonusType type, TBonusSubtype subtype, si32 info)
  393. {
  394. return CSelectFieldEqual<TBonusType>(&Bonus::type, type) && CSelectFieldEqual<TBonusSubtype>(&Bonus::subtype, subtype) && CSelectFieldEqual<si32>(&Bonus::additionalInfo, info);
  395. }
  396. CSelector DLL_EXPORT source(ui8 source, ui32 sourceID)
  397. {
  398. return CSelectFieldEqual<ui8>(&Bonus::source, source) && CSelectFieldEqual<ui32>(&Bonus::id, sourceID);
  399. }
  400. CSelector DLL_EXPORT sourceTypeSel(ui8 source)
  401. {
  402. return CSelectFieldEqual<ui8>(&Bonus::source, source);
  403. }
  404. bool DLL_EXPORT matchesType(const CSelector &sel, TBonusType type)
  405. {
  406. Bonus dummy;
  407. dummy.type = type;
  408. return sel(dummy);
  409. }
  410. bool DLL_EXPORT matchesTypeSubtype(const CSelector &sel, TBonusType type, TBonusSubtype subtype)
  411. {
  412. Bonus dummy;
  413. dummy.type = type;
  414. dummy.subtype = subtype;
  415. return sel(dummy);
  416. }
  417. }
  418. DLL_EXPORT std::ostream & operator<<(std::ostream &out, const BonusList &bonusList)
  419. {
  420. int i = 0;
  421. BOOST_FOREACH(const Bonus &b, bonusList)
  422. {
  423. out << "Bonus " << i++ << "\n" << b << std::endl;
  424. }
  425. return out;
  426. }
  427. DLL_EXPORT std::ostream & operator<<(std::ostream &out, const Bonus &bonus)
  428. {
  429. for(std::map<std::string, int>::const_iterator i = bonusNameMap.begin(); i != bonusNameMap.end(); i++)
  430. if(i->second == bonus.type)
  431. out << "\tType: " << i->first << " \t";
  432. #define printField(field) out << "\t" #field ": " << (int)bonus.field << "\n"
  433. printField(val);
  434. printField(subtype);
  435. printField(duration);
  436. printField(source);
  437. printField(id);
  438. printField(additionalInfo);
  439. printField(turnsRemain);
  440. printField(valType);
  441. printField(effectRange);
  442. #undef printField
  443. return out;
  444. }
  445. ILimiter::~ILimiter()
  446. {
  447. }
  448. bool ILimiter::limit(const Bonus &b, const CBonusSystemNode &node) const /*return true to drop the bonus */
  449. {
  450. return false;
  451. }
  452. bool CCreatureTypeLimiter::limit(const Bonus &b, const CBonusSystemNode &node) const
  453. {
  454. switch (node.nodeType)
  455. {
  456. case CBonusSystemNode::STACK:
  457. {
  458. const CCreature *c = (static_cast<const CStackInstance *>(&node))->type;
  459. return c != creature && (!includeUpgrades || !creature->isMyUpgrade(c));
  460. } //drop bonus if it's not our creature and (we dont check upgrades or its not our upgrade)
  461. break;
  462. case CBonusSystemNode::CREATURE:
  463. {
  464. const CCreature *c = (static_cast<const CCreature *>(&node));
  465. return c != creature && (!includeUpgrades || !creature->isMyUpgrade(c));
  466. }
  467. break;
  468. default:
  469. return true;
  470. }
  471. }
  472. CCreatureTypeLimiter::CCreatureTypeLimiter(const CCreature &Creature, ui8 IncludeUpgrades /*= true*/)
  473. :creature(&Creature), includeUpgrades(IncludeUpgrades)
  474. {
  475. }
  476. CCreatureTypeLimiter::CCreatureTypeLimiter()
  477. {
  478. creature = NULL;
  479. includeUpgrades = false;
  480. }
  481. HasAnotherBonusLimiter::HasAnotherBonusLimiter( TBonusType bonus )
  482. : type(bonus), subtype(0), isSubtypeRelevant(false)
  483. {
  484. }
  485. HasAnotherBonusLimiter::HasAnotherBonusLimiter( TBonusType bonus, TBonusSubtype _subtype )
  486. : type(bonus), subtype(_subtype), isSubtypeRelevant(true)
  487. {
  488. }
  489. bool HasAnotherBonusLimiter::limit( const Bonus &b, const CBonusSystemNode &node ) const
  490. {
  491. if(isSubtypeRelevant)
  492. {
  493. return !node.hasBonusOfType(static_cast<Bonus::BonusType>(type), subtype);
  494. }
  495. else
  496. {
  497. return !node.hasBonusOfType(static_cast<Bonus::BonusType>(type));
  498. }
  499. }