HeroBonus.cpp 12 KB

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