HeroBonus.cpp 14 KB

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