HeroBonus.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  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. void BonusList::limit(const CBonusSystemNode &node)
  93. {
  94. limit_start:
  95. for(iterator i = begin(); i != end(); i++)
  96. {
  97. if(i->limiter && i->limiter->limit(*i, node))
  98. {
  99. iterator toErase = i;
  100. if(i != begin())
  101. {
  102. i--;
  103. erase(toErase);
  104. }
  105. else
  106. {
  107. erase(toErase);
  108. goto limit_start;
  109. }
  110. }
  111. }
  112. }
  113. int CBonusSystemNode::valOfBonuses(Bonus::BonusType type, const CSelector &selector) const
  114. {
  115. return valOfBonuses(Selector::type(type) && selector);
  116. }
  117. int CBonusSystemNode::valOfBonuses(Bonus::BonusType type, int subtype /*= -1*/) const
  118. {
  119. CSelector s = Selector::type(type);
  120. if(subtype != -1)
  121. s = s && Selector::subtype(subtype);
  122. return valOfBonuses(s);
  123. }
  124. int CBonusSystemNode::valOfBonuses(const CSelector &selector, const CBonusSystemNode *root/* = NULL*/) const
  125. {
  126. BonusList hlp;
  127. getBonuses(hlp, selector, root);
  128. return hlp.totalValue();
  129. }
  130. bool CBonusSystemNode::hasBonus(const CSelector &selector, const CBonusSystemNode *root/* = NULL*/) const
  131. {
  132. return getBonuses(selector).size() > 0;
  133. }
  134. bool CBonusSystemNode::hasBonusOfType(Bonus::BonusType type, int subtype /*= -1*/) const
  135. {
  136. CSelector s = Selector::type(type);
  137. if(subtype != -1)
  138. s = s && Selector::subtype(subtype);
  139. return hasBonus(s);
  140. }
  141. Bonus * CBonusSystemNode::getBonus(const CSelector &selector)
  142. {
  143. Bonus *ret = bonuses.getFirst(selector);
  144. if(ret)
  145. return ret;
  146. FOREACH_PARENT(p, this)
  147. if(ret = p->getBonus(selector))
  148. return ret;
  149. return NULL;
  150. }
  151. void CBonusSystemNode::getModifiersWDescr(TModDescr &out, Bonus::BonusType type, int subtype /*= -1 */) const
  152. {
  153. getModifiersWDescr(out, Selector::typeSybtype(type, subtype));
  154. }
  155. void CBonusSystemNode::getModifiersWDescr(TModDescr &out, const CSelector &selector, const CBonusSystemNode *root /*= NULL*/) const
  156. {
  157. getBonuses(selector).getModifiersWDescr(out);
  158. }
  159. int CBonusSystemNode::getBonusesCount(int from, int id) const
  160. {
  161. return getBonusesCount(Selector::source(from, id));
  162. }
  163. int CBonusSystemNode::getBonusesCount(const CSelector &selector, const CBonusSystemNode *root/* = NULL*/) const
  164. {
  165. return getBonuses(selector, root).size();
  166. }
  167. void CBonusSystemNode::getParents(TCNodes &out, const CBonusSystemNode *root) const /*retreives list of parent nodes (nodes to inherit bonuses from) */
  168. {
  169. return;
  170. }
  171. void CBonusSystemNode::getParents(TNodes &out, const CBonusSystemNode *root /*= NULL*/)
  172. {
  173. //de-constify above
  174. TCNodes hlp;
  175. getParents(hlp, root);
  176. BOOST_FOREACH(const CBonusSystemNode *pname, hlp)
  177. out.insert(const_cast<CBonusSystemNode*>(pname));
  178. }
  179. void CBonusSystemNode::getBonuses(BonusList &out, const CSelector &selector, const CBonusSystemNode *root /*= NULL*/) const
  180. {
  181. bonuses.getBonuses(out, selector);
  182. FOREACH_CONST_PARENT(p, root ? root : this)
  183. p->getBonuses(out, selector, root ? root : this);
  184. if(!root)
  185. out.limit(*this);
  186. }
  187. BonusList CBonusSystemNode::getBonuses(const CSelector &selector, const CBonusSystemNode *root /*= NULL*/) const
  188. {
  189. BonusList ret;
  190. getBonuses(ret, selector, root);
  191. return ret;
  192. }
  193. void CBonusSystemNode::getBonuses(BonusList &out, const CSelector &selector, const CSelector &limit, const CBonusSystemNode *root /*= NULL*/) const
  194. {
  195. bonuses.getBonuses(out, selector, limit);
  196. FOREACH_CONST_PARENT(p, root ? root : this)
  197. p->getBonuses(out, selector, limit, root ? root : this);
  198. if(!root)
  199. out.limit(*this);
  200. }
  201. BonusList CBonusSystemNode::getBonuses(const CSelector &selector, const CSelector &limit, const CBonusSystemNode *root /*= NULL*/) const
  202. {
  203. BonusList ret;
  204. getBonuses(ret, selector, limit, root);
  205. return ret;
  206. }
  207. bool CBonusSystemNode::hasBonusFrom(ui8 source, ui32 sourceID) const
  208. {
  209. return hasBonus(Selector::source(source,sourceID));
  210. }
  211. int CBonusSystemNode::MoraleVal() const
  212. {
  213. if(hasBonusOfType(Bonus::NON_LIVING) || hasBonusOfType(Bonus::UNDEAD) ||
  214. hasBonusOfType(Bonus::NO_MORALE) || hasBonusOfType(Bonus::SIEGE_WEAPON))
  215. return 0;
  216. int ret = valOfBonuses(Selector::type(Bonus::MORALE));
  217. if(hasBonusOfType(Bonus::SELF_MORALE)) //eg. minotaur
  218. amax(ret, +1);
  219. return abetw(ret, -3, +3);
  220. }
  221. int CBonusSystemNode::LuckVal() const
  222. {
  223. if(hasBonusOfType(Bonus::NO_LUCK))
  224. return 0;
  225. int ret = valOfBonuses(Selector::type(Bonus::LUCK));
  226. if(hasBonusOfType(Bonus::SELF_LUCK)) //eg. halfling
  227. amax(ret, +1);
  228. return abetw(ret, -3, +3);
  229. }
  230. si32 CBonusSystemNode::Attack() const
  231. {
  232. si32 ret = valOfBonuses(Bonus::PRIMARY_SKILL, PrimarySkill::ATTACK);
  233. if(int frenzyPower = valOfBonuses(Bonus::IN_FRENZY)) //frenzy for attacker
  234. {
  235. ret += frenzyPower * Defense(false);
  236. }
  237. return ret;
  238. }
  239. si32 CBonusSystemNode::Defense(bool withFrenzy /*= true*/) const
  240. {
  241. si32 ret = valOfBonuses(Bonus::PRIMARY_SKILL, PrimarySkill::DEFENSE);
  242. if(withFrenzy && hasBonusOfType(Bonus::IN_FRENZY)) //frenzy for defender
  243. {
  244. return 0;
  245. }
  246. return ret;
  247. }
  248. ui16 CBonusSystemNode::MaxHealth() const
  249. {
  250. return valOfBonuses(Bonus::STACK_HEALTH);
  251. }
  252. CBonusSystemNode::CBonusSystemNode()
  253. {
  254. nodeType = UNKNOWN;
  255. }
  256. CBonusSystemNode::~CBonusSystemNode()
  257. {
  258. }
  259. int NBonus::valOf(const CBonusSystemNode *obj, Bonus::BonusType type, int subtype /*= -1*/)
  260. {
  261. if(obj)
  262. return obj->valOfBonuses(type, subtype);
  263. return 0;
  264. }
  265. bool NBonus::hasOfType(const CBonusSystemNode *obj, Bonus::BonusType type, int subtype /*= -1*/)
  266. {
  267. if(obj)
  268. return obj->hasBonusOfType(type, subtype);
  269. return false;
  270. }
  271. void NBonus::getModifiersWDescr(const CBonusSystemNode *obj, TModDescr &out, Bonus::BonusType type, int subtype /*= -1 */)
  272. {
  273. if(obj)
  274. return obj->getModifiersWDescr(out, type, subtype);
  275. }
  276. int NBonus::getCount(const CBonusSystemNode *obj, int from, int id)
  277. {
  278. if(obj)
  279. return obj->getBonusesCount(from, id);
  280. return 0;
  281. }
  282. const CSpell * Bonus::sourceSpell() const
  283. {
  284. if(source == SPELL_EFFECT)
  285. return &VLC->spellh->spells[id];
  286. return NULL;
  287. }
  288. std::string Bonus::Description() const
  289. {
  290. if(description.size())
  291. return description;
  292. std::ostringstream str;
  293. if(val < 0)
  294. str << '-';
  295. else if(val > 0)
  296. str << '+';
  297. str << val << " ";
  298. switch(source)
  299. {
  300. case CREATURE_ABILITY:
  301. str << VLC->creh->creatures[id]->namePl;
  302. break;
  303. }
  304. return str.str();
  305. }
  306. Bonus::Bonus(ui8 Dur, ui8 Type, ui8 Src, si32 Val, ui32 ID, std::string Desc, si32 Subtype/*=-1*/)
  307. : duration(Dur), type(Type), subtype(Subtype), source(Src), val(Val), id(ID), description(Desc)
  308. {
  309. additionalInfo = -1;
  310. turnsRemain = 0;
  311. valType = ADDITIVE_VALUE;
  312. effectRange = NO_LIMIT;
  313. limiter = NULL;
  314. boost::algorithm::trim(description);
  315. }
  316. Bonus::Bonus(ui8 Dur, ui8 Type, ui8 Src, si32 Val, ui32 ID, si32 Subtype/*=-1*/, ui8 ValType /*= ADDITIVE_VALUE*/)
  317. : duration(Dur), type(Type), subtype(Subtype), source(Src), val(Val), id(ID), valType(ValType)
  318. {
  319. additionalInfo = -1;
  320. turnsRemain = 0;
  321. effectRange = NO_LIMIT;
  322. limiter = NULL;
  323. }
  324. Bonus::Bonus()
  325. {
  326. subtype = -1;
  327. additionalInfo = -1;
  328. turnsRemain = 0;
  329. valType = ADDITIVE_VALUE;
  330. effectRange = NO_LIMIT;
  331. limiter = NULL;
  332. }
  333. CSelector DLL_EXPORT operator&&(const CSelector &first, const CSelector &second)
  334. {
  335. return CSelectorsConjunction(first, second);
  336. }
  337. namespace Selector
  338. {
  339. DLL_EXPORT CSelectFieldEqual<TBonusType> type(&Bonus::type, 0);
  340. DLL_EXPORT CSelectFieldEqual<TBonusSubtype> subtype(&Bonus::subtype, 0);
  341. DLL_EXPORT CSelectFieldEqual<si32> info(&Bonus::additionalInfo, 0);
  342. DLL_EXPORT CSelectFieldEqual<ui8> sourceType(&Bonus::source, 0);
  343. DLL_EXPORT CSelectFieldEqual<ui8> effectRange(&Bonus::effectRange, Bonus::NO_LIMIT);
  344. DLL_EXPORT CWillLastTurns turns;;
  345. CSelector DLL_EXPORT typeSybtype(TBonusType Type, TBonusSubtype Subtype)
  346. {
  347. return type(Type) && subtype(Subtype);
  348. }
  349. CSelector DLL_EXPORT typeSybtypeInfo(TBonusType type, TBonusSubtype subtype, si32 info)
  350. {
  351. return CSelectFieldEqual<TBonusType>(&Bonus::type, type) && CSelectFieldEqual<TBonusSubtype>(&Bonus::subtype, subtype) && CSelectFieldEqual<si32>(&Bonus::additionalInfo, info);
  352. }
  353. CSelector DLL_EXPORT source(ui8 source, ui32 sourceID)
  354. {
  355. return CSelectFieldEqual<ui8>(&Bonus::source, source) && CSelectFieldEqual<ui32>(&Bonus::id, sourceID);
  356. }
  357. bool DLL_EXPORT matchesType(const CSelector &sel, TBonusType type)
  358. {
  359. Bonus dummy;
  360. dummy.type = type;
  361. return sel(dummy);
  362. }
  363. bool DLL_EXPORT matchesTypeSubtype(const CSelector &sel, TBonusType type, TBonusSubtype subtype)
  364. {
  365. Bonus dummy;
  366. dummy.type = type;
  367. dummy.subtype = subtype;
  368. return sel(dummy);
  369. }
  370. }
  371. DLL_EXPORT std::ostream & operator<<(std::ostream &out, const BonusList &bonusList)
  372. {
  373. int i = 0;
  374. BOOST_FOREACH(const Bonus &b, bonusList)
  375. {
  376. out << "Bonus " << i++ << "\n" << b << std::endl;
  377. }
  378. return out;
  379. }
  380. DLL_EXPORT std::ostream & operator<<(std::ostream &out, const Bonus &bonus)
  381. {
  382. for(std::map<std::string, int>::const_iterator i = bonusNameMap.begin(); i != bonusNameMap.end(); i++)
  383. if(i->second == bonus.type)
  384. out << "\tType: " << i->first << " \t";
  385. #define printField(field) out << "\t" #field ": " << (int)bonus.field << "\n"
  386. printField(val);
  387. printField(subtype);
  388. printField(duration);
  389. printField(source);
  390. printField(id);
  391. printField(additionalInfo);
  392. printField(turnsRemain);
  393. printField(valType);
  394. printField(effectRange);
  395. #undef printField
  396. return out;
  397. }
  398. ILimiter::~ILimiter()
  399. {
  400. }
  401. bool ILimiter::limit(const Bonus &b, const CBonusSystemNode &node) const /*return true to drop the bonus */
  402. {
  403. return false;
  404. }
  405. bool CCreatureTypeLimiter::limit(const Bonus &b, const CBonusSystemNode &node) const
  406. {
  407. switch (node.nodeType)
  408. {
  409. case CBonusSystemNode::STACK:
  410. {
  411. const CCreature *c = (static_cast<const CStackInstance *>(&node))->type;
  412. return c != creature && (!includeUpgrades || !creature->isMyUpgrade(c));
  413. } //drop bonus if it's not our creature and (we dont check upgrades or its not our upgrade)
  414. break;
  415. case CBonusSystemNode::CREATURE:
  416. {
  417. const CCreature *c = (static_cast<const CCreature *>(&node));
  418. return c != creature && (!includeUpgrades || !creature->isMyUpgrade(c));
  419. }
  420. break;
  421. default:
  422. //return false; //don't delete it if it's other type of node
  423. return true;
  424. }
  425. }
  426. CCreatureTypeLimiter::CCreatureTypeLimiter(const CCreature &Creature, ui8 IncludeUpgrades /*= true*/)
  427. :creature(&Creature), includeUpgrades(IncludeUpgrades)
  428. {
  429. }
  430. CCreatureTypeLimiter::CCreatureTypeLimiter()
  431. {
  432. creature = NULL;
  433. includeUpgrades = false;
  434. }
  435. HasAnotherBonusLimiter::HasAnotherBonusLimiter( TBonusType bonus )
  436. : type(bonus), subtype(0), isSubtypeRelevant(false)
  437. {
  438. }
  439. HasAnotherBonusLimiter::HasAnotherBonusLimiter( TBonusType bonus, TBonusSubtype _subtype )
  440. : type(bonus), subtype(_subtype), isSubtypeRelevant(true)
  441. {
  442. }
  443. bool HasAnotherBonusLimiter::limit( const Bonus &b, const CBonusSystemNode &node ) const
  444. {
  445. if(isSubtypeRelevant)
  446. {
  447. return !node.hasBonusOfType(static_cast<Bonus::BonusType>(type), subtype);
  448. }
  449. else
  450. {
  451. return !node.hasBonusOfType(static_cast<Bonus::BonusType>(type));
  452. }
  453. }