HeroBonus.cpp 12 KB

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