HeroBonus.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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. for(const_iterator i = begin(); i != end(); i++)
  23. {
  24. switch(i->valType)
  25. {
  26. case Bonus::BASE_NUMBER:
  27. base += i->val;
  28. break;
  29. case Bonus::PERCENT_TO_ALL:
  30. percentToAll += i->val;
  31. break;
  32. case Bonus::PERCENT_TO_BASE:
  33. percentToBase += i->val;
  34. break;
  35. case Bonus::ADDITIVE_VALUE:
  36. additive += i->val;
  37. break;
  38. }
  39. }
  40. int modifiedBase = base + (base * percentToBase) / 100;
  41. modifiedBase += additive;
  42. return (modifiedBase * (100 + percentToAll)) / 100;
  43. }
  44. const DLL_EXPORT Bonus * BonusList::getFirst(const CSelector &selector) const
  45. {
  46. for (const_iterator i = begin(); i != end(); i++)
  47. if(selector(*i))
  48. return &*i;
  49. return NULL;
  50. }
  51. DLL_EXPORT Bonus * BonusList::getFirst(const CSelector &select)
  52. {
  53. for (iterator i = begin(); i != end(); i++)
  54. if(select(*i))
  55. return &*i;
  56. return NULL;
  57. }
  58. void DLL_EXPORT BonusList::getModifiersWDescr(TModDescr &out) const
  59. {
  60. for(const_iterator i = begin(); i != end(); i++)
  61. out.push_back(std::make_pair(i->val, i->Description()));
  62. }
  63. void DLL_EXPORT BonusList::getBonuses(BonusList &out, const CSelector &selector, const CBonusSystemNode *source /*= NULL*/) const
  64. {
  65. for(const_iterator i = begin(); i != end(); i++)
  66. if(selector(*i) && i->effectRange == Bonus::NO_LIMIT)
  67. out.push_back(*i);
  68. }
  69. void DLL_EXPORT BonusList::getBonuses(BonusList &out, const CSelector &selector, const CSelector &limit, const CBonusSystemNode *source /*= NULL*/) const
  70. {
  71. for(const_iterator i = begin(); i != end(); i++)
  72. if(selector(*i) && (!limit || limit(*i)))
  73. out.push_back(*i);
  74. }
  75. void BonusList::limit(const CBonusSystemNode &node)
  76. {
  77. for(iterator i = begin(); i != end(); i++)
  78. {
  79. if(i->limiter && i->limiter->limit(*i, node))
  80. {
  81. iterator toErase = i;
  82. i--;
  83. erase(toErase);
  84. }
  85. }
  86. }
  87. int CBonusSystemNode::valOfBonuses(Bonus::BonusType type, int subtype /*= -1*/) const
  88. {
  89. CSelector s = Selector::type(type);
  90. if(subtype != -1)
  91. s = s && Selector::subtype(subtype);
  92. return valOfBonuses(s);
  93. }
  94. int CBonusSystemNode::valOfBonuses(Bonus::BonusType type, const CSelector &selector) const
  95. {
  96. return valOfBonuses(Selector::type(type) && selector);
  97. }
  98. int CBonusSystemNode::valOfBonuses(const CSelector &selector, const CBonusSystemNode *root/* = NULL*/) const
  99. {
  100. BonusList hlp;
  101. getBonuses(hlp, selector, root);
  102. return hlp.totalValue();
  103. }
  104. bool CBonusSystemNode::hasBonus(const CSelector &selector, const CBonusSystemNode *root/* = NULL*/) const
  105. {
  106. return getBonuses(selector).size() > 0;
  107. }
  108. bool CBonusSystemNode::hasBonusOfType(Bonus::BonusType type, int subtype /*= -1*/) const
  109. {
  110. CSelector s = Selector::type(type);
  111. if(subtype != -1)
  112. s = s && Selector::subtype(subtype);
  113. return hasBonus(s);
  114. }
  115. Bonus * CBonusSystemNode::getBonus(const CSelector &selector)
  116. {
  117. Bonus *ret = bonuses.getFirst(selector);
  118. if(ret)
  119. return ret;
  120. FOREACH_PARENT(p, this)
  121. if(ret = p->getBonus(selector))
  122. return ret;
  123. return NULL;
  124. }
  125. void CBonusSystemNode::getModifiersWDescr(TModDescr &out, Bonus::BonusType type, int subtype /*= -1 */) const
  126. {
  127. getModifiersWDescr(out, Selector::typeSybtype(type, subtype));
  128. }
  129. void CBonusSystemNode::getModifiersWDescr(TModDescr &out, const CSelector &selector, const CBonusSystemNode *root /*= NULL*/) const
  130. {
  131. getBonuses(selector).getModifiersWDescr(out);
  132. }
  133. int CBonusSystemNode::getBonusesCount(int from, int id) const
  134. {
  135. return getBonusesCount(Selector::source(from, id));
  136. }
  137. int CBonusSystemNode::getBonusesCount(const CSelector &selector, const CBonusSystemNode *root/* = NULL*/) const
  138. {
  139. return getBonuses(selector, root).size();
  140. }
  141. void CBonusSystemNode::getParents(TCNodes &out, const CBonusSystemNode *root) const /*retreives list of parent nodes (nodes to inherit bonuses from) */
  142. {
  143. return;
  144. }
  145. void CBonusSystemNode::getParents(TNodes &out, const CBonusSystemNode *root /*= NULL*/)
  146. {
  147. //de-constify above
  148. TCNodes hlp;
  149. getParents(hlp, root);
  150. BOOST_FOREACH(const CBonusSystemNode *pname, hlp)
  151. out.insert(const_cast<CBonusSystemNode*>(pname));
  152. }
  153. void CBonusSystemNode::getBonuses(BonusList &out, const CSelector &selector, const CBonusSystemNode *root /*= NULL*/) const
  154. {
  155. bonuses.getBonuses(out, selector);
  156. FOREACH_CONST_PARENT(p, root ? root : this)
  157. p->getBonuses(out, selector, root ? root : this);
  158. if(!root)
  159. out.limit(*this);
  160. }
  161. BonusList CBonusSystemNode::getBonuses(const CSelector &selector, const CBonusSystemNode *root /*= NULL*/) const
  162. {
  163. BonusList ret;
  164. getBonuses(ret, selector, root);
  165. return ret;
  166. }
  167. void CBonusSystemNode::getBonuses(BonusList &out, const CSelector &selector, const CSelector &limit, const CBonusSystemNode *root /*= NULL*/) const
  168. {
  169. bonuses.getBonuses(out, selector, limit);
  170. FOREACH_CONST_PARENT(p, root ? root : this)
  171. p->getBonuses(out, selector, limit, root ? root : this);
  172. if(!root)
  173. out.limit(*this);
  174. }
  175. BonusList CBonusSystemNode::getBonuses(const CSelector &selector, const CSelector &limit, const CBonusSystemNode *root /*= NULL*/) const
  176. {
  177. BonusList ret;
  178. getBonuses(ret, selector, limit, root);
  179. return ret;
  180. }
  181. bool CBonusSystemNode::hasBonusFrom(ui8 source, ui32 sourceID) const
  182. {
  183. return hasBonus(Selector::source(source,sourceID));
  184. }
  185. int CBonusSystemNode::MoraleVal() const
  186. {
  187. if(hasBonusOfType(Bonus::NON_LIVING) || hasBonusOfType(Bonus::UNDEAD) ||
  188. hasBonusOfType(Bonus::NO_MORALE) || hasBonusOfType(Bonus::SIEGE_WEAPON))
  189. return 0;
  190. int ret = valOfBonuses(Selector::type(Bonus::MORALE));
  191. if(hasBonusOfType(Bonus::SELF_MORALE)) //eg. minotaur
  192. amax(ret, +1);
  193. return abetw(ret, -3, +3);
  194. }
  195. int CBonusSystemNode::LuckVal() const
  196. {
  197. if(hasBonusOfType(Bonus::NO_LUCK))
  198. return 0;
  199. int ret = valOfBonuses(Selector::type(Bonus::LUCK));
  200. if(hasBonusOfType(Bonus::SELF_LUCK)) //eg. halfling
  201. amax(ret, +1);
  202. return abetw(ret, -3, +3);
  203. }
  204. si32 CBonusSystemNode::Attack() const
  205. {
  206. si32 ret = valOfBonuses(Bonus::PRIMARY_SKILL, PrimarySkill::ATTACK);
  207. if(int frenzyPower = valOfBonuses(Bonus::IN_FRENZY)) //frenzy for attacker
  208. {
  209. ret += frenzyPower * Defense(false);
  210. }
  211. return ret;
  212. }
  213. si32 CBonusSystemNode::Defense(bool withFrenzy /*= true*/) const
  214. {
  215. si32 ret = valOfBonuses(Bonus::PRIMARY_SKILL, PrimarySkill::DEFENSE);
  216. if(withFrenzy && hasBonusOfType(Bonus::IN_FRENZY)) //frenzy for defender
  217. {
  218. return 0;
  219. }
  220. return ret;
  221. }
  222. ui16 CBonusSystemNode::MaxHealth() const
  223. {
  224. return valOfBonuses(Bonus::STACK_HEALTH);
  225. }
  226. CBonusSystemNode::CBonusSystemNode()
  227. {
  228. nodeType = UNKNOWN;
  229. }
  230. CBonusSystemNode::~CBonusSystemNode()
  231. {
  232. }
  233. int NBonus::valOf(const CBonusSystemNode *obj, Bonus::BonusType type, int subtype /*= -1*/)
  234. {
  235. if(obj)
  236. return obj->valOfBonuses(type, subtype);
  237. return 0;
  238. }
  239. bool NBonus::hasOfType(const CBonusSystemNode *obj, Bonus::BonusType type, int subtype /*= -1*/)
  240. {
  241. if(obj)
  242. return obj->hasBonusOfType(type, subtype);
  243. return false;
  244. }
  245. void NBonus::getModifiersWDescr(const CBonusSystemNode *obj, TModDescr &out, Bonus::BonusType type, int subtype /*= -1 */)
  246. {
  247. if(obj)
  248. return obj->getModifiersWDescr(out, type, subtype);
  249. }
  250. int NBonus::getCount(const CBonusSystemNode *obj, int from, int id)
  251. {
  252. if(obj)
  253. return obj->getBonusesCount(from, id);
  254. return 0;
  255. }
  256. const CSpell * Bonus::sourceSpell() const
  257. {
  258. if(source == SPELL_EFFECT)
  259. return &VLC->spellh->spells[id];
  260. return NULL;
  261. }
  262. std::string Bonus::Description() const
  263. {
  264. if(description.size())
  265. return description;
  266. std::ostringstream str;
  267. if(val < 0)
  268. str << '-';
  269. else if(val > 0)
  270. str << '+';
  271. str << val << " ";
  272. switch(source)
  273. {
  274. case CREATURE_ABILITY:
  275. str << VLC->creh->creatures[id]->namePl;
  276. break;
  277. }
  278. return str.str();
  279. }
  280. Bonus::Bonus(ui8 Dur, ui8 Type, ui8 Src, si32 Val, ui32 ID, std::string Desc, si32 Subtype/*=-1*/)
  281. : duration(Dur), type(Type), subtype(Subtype), source(Src), val(Val), id(ID), description(Desc)
  282. {
  283. additionalInfo = -1;
  284. turnsRemain = 0;
  285. valType = ADDITIVE_VALUE;
  286. effectRange = NO_LIMIT;
  287. limiter = NULL;
  288. boost::algorithm::trim(description);
  289. }
  290. Bonus::Bonus(ui8 Dur, ui8 Type, ui8 Src, si32 Val, ui32 ID, si32 Subtype/*=-1*/, ui8 ValType /*= ADDITIVE_VALUE*/)
  291. : duration(Dur), type(Type), subtype(Subtype), source(Src), val(Val), id(ID), valType(ValType)
  292. {
  293. additionalInfo = -1;
  294. turnsRemain = 0;
  295. effectRange = NO_LIMIT;
  296. limiter = NULL;
  297. }
  298. Bonus::Bonus()
  299. {
  300. subtype = -1;
  301. additionalInfo = -1;
  302. turnsRemain = 0;
  303. valType = ADDITIVE_VALUE;
  304. effectRange = NO_LIMIT;
  305. limiter = NULL;
  306. }
  307. CSelector DLL_EXPORT operator&&(const CSelector &first, const CSelector &second)
  308. {
  309. return CSelectorsConjunction(first, second);
  310. }
  311. namespace Selector
  312. {
  313. DLL_EXPORT CSelectFieldEqual<TBonusType> type(&Bonus::type, 0);
  314. DLL_EXPORT CSelectFieldEqual<TBonusSubtype> subtype(&Bonus::subtype, 0);
  315. DLL_EXPORT CSelectFieldEqual<si32> info(&Bonus::additionalInfo, 0);
  316. DLL_EXPORT CSelectFieldEqual<ui8> sourceType(&Bonus::source, 0);
  317. DLL_EXPORT CSelectFieldEqual<ui8> effectRange(&Bonus::effectRange, Bonus::NO_LIMIT);
  318. DLL_EXPORT CWillLastTurns turns;;
  319. CSelector DLL_EXPORT typeSybtype(TBonusType Type, TBonusSubtype Subtype)
  320. {
  321. return type(Type) && subtype(Subtype);
  322. }
  323. CSelector DLL_EXPORT typeSybtypeInfo(TBonusType type, TBonusSubtype subtype, si32 info)
  324. {
  325. return CSelectFieldEqual<TBonusType>(&Bonus::type, type) && CSelectFieldEqual<TBonusSubtype>(&Bonus::subtype, subtype) && CSelectFieldEqual<si32>(&Bonus::additionalInfo, info);
  326. }
  327. CSelector DLL_EXPORT source(ui8 source, ui32 sourceID)
  328. {
  329. return CSelectFieldEqual<ui8>(&Bonus::source, source) && CSelectFieldEqual<ui32>(&Bonus::id, sourceID);
  330. }
  331. bool DLL_EXPORT matchesType(const CSelector &sel, TBonusType type)
  332. {
  333. Bonus dummy;
  334. dummy.type = type;
  335. return sel(dummy);
  336. }
  337. bool DLL_EXPORT matchesTypeSubtype(const CSelector &sel, TBonusType type, TBonusSubtype subtype)
  338. {
  339. Bonus dummy;
  340. dummy.type = type;
  341. dummy.subtype = subtype;
  342. return sel(dummy);
  343. }
  344. }
  345. DLL_EXPORT std::ostream & operator<<(std::ostream &out, const BonusList &bonusList)
  346. {
  347. int i = 0;
  348. BOOST_FOREACH(const Bonus &b, bonusList)
  349. {
  350. out << "Bonus " << i++ << "\n" << b << std::endl;
  351. }
  352. return out;
  353. }
  354. DLL_EXPORT std::ostream & operator<<(std::ostream &out, const Bonus &bonus)
  355. {
  356. for(std::map<std::string, int>::const_iterator i = bonusNameMap.begin(); i != bonusNameMap.end(); i++)
  357. if(i->second == bonus.type)
  358. out << "\tType: " << i->first << " \t";
  359. #define printField(field) out << "\t" #field ": " << (int)bonus.field << "\n"
  360. printField(val);
  361. printField(subtype);
  362. printField(duration);
  363. printField(source);
  364. printField(id);
  365. printField(additionalInfo);
  366. printField(turnsRemain);
  367. printField(valType);
  368. printField(effectRange);
  369. #undef printField
  370. return out;
  371. }
  372. ILimiter::~ILimiter()
  373. {
  374. }
  375. bool ILimiter::limit(const Bonus &b, const CBonusSystemNode &node) const /*return true to drop the bonus */
  376. {
  377. return false;
  378. }
  379. bool CCreatureTypeLimiter::limit(const Bonus &b, const CBonusSystemNode &node) const
  380. {
  381. if(node.nodeType != CBonusSystemNode::STACK)
  382. return true;
  383. const CCreature *c = (static_cast<const CStackInstance *>(&node))->type;
  384. return c != creature && (!includeUpgrades || !creature->isMyUpgrade(c)); //drop bonus if it's not our creature and (we dont check upgrades or its not our upgrade)
  385. }
  386. CCreatureTypeLimiter::CCreatureTypeLimiter(const CCreature &Creature, ui8 IncludeUpgrades /*= true*/)
  387. :creature(&Creature), includeUpgrades(IncludeUpgrades)
  388. {
  389. }
  390. CCreatureTypeLimiter::CCreatureTypeLimiter()
  391. {
  392. creature = NULL;
  393. includeUpgrades = false;
  394. }