HeroBonus.h 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860
  1. #pragma once
  2. #include "../global.h"
  3. #include <string>
  4. #include <set>
  5. #include <boost/function.hpp>
  6. #include <boost/shared_ptr.hpp>
  7. #include <boost/range.hpp>
  8. #include <boost/thread/mutex.hpp>
  9. /*
  10. * HeroBonus.h, part of VCMI engine
  11. *
  12. * Authors: listed in file AUTHORS in main folder
  13. *
  14. * License: GNU General Public License v2.0 or later
  15. * Full text of license available in license.txt file, in main folder
  16. *
  17. */
  18. class CCreature;
  19. class CSpell;
  20. struct Bonus;
  21. class CBonusSystemNode;
  22. class ILimiter;
  23. class IPropagator;
  24. class BonusList;
  25. typedef boost::shared_ptr<BonusList> TBonusListPtr;
  26. typedef std::vector<std::pair<int,std::string> > TModDescr; //modifiers values and their descriptions
  27. typedef std::set<CBonusSystemNode*> TNodes;
  28. typedef std::set<const CBonusSystemNode*> TCNodes;
  29. typedef std::vector<CBonusSystemNode *> TNodesVector;
  30. typedef boost::function<bool(const Bonus*)> CSelector;
  31. namespace PrimarySkill
  32. {
  33. enum { ATTACK, DEFENSE, SPELL_POWER, KNOWLEDGE};
  34. }
  35. #define BONUS_LIST \
  36. BONUS_NAME(NONE) \
  37. BONUS_NAME(MOVEMENT) /*both water/land*/ \
  38. BONUS_NAME(LAND_MOVEMENT) \
  39. BONUS_NAME(SEA_MOVEMENT) \
  40. BONUS_NAME(MORALE) \
  41. BONUS_NAME(LUCK) \
  42. BONUS_NAME(PRIMARY_SKILL) /*uses subtype to pick skill; additional info if set: 1 - only melee, 2 - only distance*/ \
  43. BONUS_NAME(SIGHT_RADIOUS) \
  44. BONUS_NAME(MANA_REGENERATION) /*points per turn apart from normal (1 + mysticism)*/ \
  45. BONUS_NAME(FULL_MANA_REGENERATION) /*all mana points are replenished every day*/ \
  46. BONUS_NAME(NONEVIL_ALIGNMENT_MIX) /*good and neutral creatures can be mixed without morale penalty*/ \
  47. BONUS_NAME(SECONDARY_SKILL_PREMY) /*%*/ \
  48. BONUS_NAME(SURRENDER_DISCOUNT) /*%*/ \
  49. BONUS_NAME(STACKS_SPEED) /*additional info - percent of speed bonus applied after direct bonuses; >0 - added, <0 - subtracted to this part*/ \
  50. BONUS_NAME(FLYING_MOVEMENT) /*subtype 1 - without penalty, 2 - with penalty*/ \
  51. BONUS_NAME(SPELL_DURATION) \
  52. BONUS_NAME(AIR_SPELL_DMG_PREMY) \
  53. BONUS_NAME(EARTH_SPELL_DMG_PREMY) \
  54. BONUS_NAME(FIRE_SPELL_DMG_PREMY) \
  55. BONUS_NAME(WATER_SPELL_DMG_PREMY) \
  56. BONUS_NAME(WATER_WALKING) /*subtype 1 - without penalty, 2 - with penalty*/ \
  57. BONUS_NAME(NEGATE_ALL_NATURAL_IMMUNITIES) \
  58. BONUS_NAME(STACK_HEALTH) \
  59. BONUS_NAME(BLOCK_MORALE) \
  60. BONUS_NAME(BLOCK_LUCK) \
  61. BONUS_NAME(FIRE_SPELLS) \
  62. BONUS_NAME(AIR_SPELLS) \
  63. BONUS_NAME(WATER_SPELLS) \
  64. BONUS_NAME(EARTH_SPELLS) \
  65. BONUS_NAME(GENERATE_RESOURCE) /*daily value, uses subtype (resource type)*/ \
  66. BONUS_NAME(CREATURE_GROWTH) /*for legion artifacts: value - week growth bonus, subtype - monster level if aplicable*/ \
  67. BONUS_NAME(WHIRLPOOL_PROTECTION) /*hero won't lose army when teleporting through whirlpool*/ \
  68. BONUS_NAME(SPELL) /*hero knows spell, val - skill level (0 - 3), subtype - spell id*/ \
  69. BONUS_NAME(SPELLS_OF_LEVEL) /*hero knows all spells of given level, val - skill level; subtype - level*/ \
  70. BONUS_NAME(ENEMY_CANT_ESCAPE) /*for shackles of war*/ \
  71. BONUS_NAME(MAGIC_SCHOOL_SKILL) /* //eg. for magic plains terrain, subtype: school of magic (0 - all, 1 - fire, 2 - air, 4 - water, 8 - earth), value - level*/ \
  72. BONUS_NAME(FREE_SHOOTING) /*stacks can shoot even if otherwise blocked (sharpshooter's bow effect)*/ \
  73. BONUS_NAME(OPENING_BATTLE_SPELL) /*casts a spell at expert level at beginning of battle, val - spell power, subtype - spell id*/ \
  74. BONUS_NAME(IMPROVED_NECROMANCY) /*allows Necropolis units other than skeletons to be raised by necromancy*/ \
  75. BONUS_NAME(CREATURE_GROWTH_PERCENT) /*increases growth of all units in all towns, val - percentage*/ \
  76. BONUS_NAME(FREE_SHIP_BOARDING) /*movement points preserved with ship boarding and landing*/ \
  77. BONUS_NAME(NO_TYPE) \
  78. BONUS_NAME(FLYING) \
  79. BONUS_NAME(SHOOTER) \
  80. BONUS_NAME(CHARGE_IMMUNITY) \
  81. BONUS_NAME(ADDITIONAL_ATTACK) \
  82. BONUS_NAME(UNLIMITED_RETALIATIONS) \
  83. BONUS_NAME(NO_MELEE_PENALTY) \
  84. BONUS_NAME(JOUSTING) /*for champions*/ \
  85. BONUS_NAME(HATE) /*eg. angels hate devils, subtype - ID of hated creature, val - damage bonus percent */ \
  86. BONUS_NAME(KING1) \
  87. BONUS_NAME(KING2) \
  88. BONUS_NAME(KING3) \
  89. BONUS_NAME(MAGIC_RESISTANCE) /*in % (value)*/ \
  90. BONUS_NAME(CHANGES_SPELL_COST_FOR_ALLY) /*in mana points (value) , eg. mage*/ \
  91. BONUS_NAME(CHANGES_SPELL_COST_FOR_ENEMY) /*in mana points (value) , eg. pegasus */ \
  92. BONUS_NAME(SPELL_AFTER_ATTACK) /* subtype - spell id, value - chance %, additional info % 1000 - level, (additional info)/1000 -> [0 - all attacks, 1 - shot only, 2 - melee only*/ \
  93. BONUS_NAME(SPELL_BEFORE_ATTACK) /* subtype - spell id, value - chance %, additional info % 1000 - level, (additional info)/1000 -> [0 - all attacks, 1 - shot only, 2 - melee only*/ \
  94. BONUS_NAME(SPELL_RESISTANCE_AURA) /*eg. unicorns, value - resistance bonus in % for adjacent creatures*/ \
  95. BONUS_NAME(LEVEL_SPELL_IMMUNITY) /*creature is immune to all spell with level below or equal to value of this bonus*/ \
  96. BONUS_NAME(TWO_HEX_ATTACK_BREATH) /*eg. dragons*/ \
  97. BONUS_NAME(SPELL_DAMAGE_REDUCTION) /*eg. golems; value - reduction in %, subtype - spell school; -1 - all, 0 - air, 1 - fire, 2 - water, 3 - earth*/ \
  98. BONUS_NAME(NO_WALL_PENALTY) \
  99. BONUS_NAME(NON_LIVING) /*eg. gargoyle*/ \
  100. BONUS_NAME(RANDOM_SPELLCASTER) /*eg. master genie, val - level*/ \
  101. BONUS_NAME(BLOCKS_RETALIATION) /*eg. naga*/ \
  102. BONUS_NAME(SPELL_IMMUNITY) /*subid - spell id*/ \
  103. BONUS_NAME(MANA_CHANNELING) /*value in %, eg. familiar*/ \
  104. BONUS_NAME(SPELL_LIKE_ATTACK) /*subtype - spell, value - spell level; range is taken from spell, but damage from creature; eg. magog*/ \
  105. BONUS_NAME(THREE_HEADED_ATTACK) /*eg. cerberus*/ \
  106. BONUS_NAME(DAEMON_SUMMONING) /*pit lord, subtype - type of creatures, val - hp per unit*/ \
  107. BONUS_NAME(FIRE_IMMUNITY) /*subtype 0 - all, 1 - all except positive, 2 - only damage spells*/ \
  108. BONUS_NAME(WATER_IMMUNITY) \
  109. BONUS_NAME(EARTH_IMMUNITY) \
  110. BONUS_NAME(AIR_IMMUNITY) \
  111. BONUS_NAME(FIRE_SHIELD) \
  112. BONUS_NAME(UNDEAD) \
  113. BONUS_NAME(HP_REGENERATION) /*creature regenerates val HP every new round*/ \
  114. BONUS_NAME(FULL_HP_REGENERATION) /*first creature regenerates all HP every new round; subtype 0 - animation 4 (trolllike), 1 - animation 47 (wightlike)*/ \
  115. BONUS_NAME(MANA_DRAIN) /*value - spell points per turn*/ \
  116. BONUS_NAME(LIFE_DRAIN) \
  117. BONUS_NAME(DOUBLE_DAMAGE_CHANCE) /*value in %, eg. dread knight*/ \
  118. BONUS_NAME(RETURN_AFTER_STRIKE) \
  119. BONUS_NAME(SELF_MORALE) /*eg. minotaur*/ \
  120. BONUS_NAME(SPELLCASTER) /*subtype - spell id, value - level of school, additional info - weighted chance. use SPECIFIC_SPELL_POWER, CREATURE_SPELL_POWER or CREATURE_ENCHANT_POWER for calculating the power*/ \
  121. BONUS_NAME(CATAPULT) \
  122. BONUS_NAME(ENEMY_DEFENCE_REDUCTION) /*in % (value) eg. behemots*/ \
  123. BONUS_NAME(GENERAL_DAMAGE_REDUCTION) /* shield / air shield effect */ \
  124. BONUS_NAME(GENERAL_ATTACK_REDUCTION) /*eg. while stoned or blinded - in %, subtype: -1 - any damage, 0 - melee damage, 1 - ranged damage*/ \
  125. BONUS_NAME(DEFENSIVE_STANCE) /* val - bonus to defense while defending */ \
  126. BONUS_NAME(ATTACKS_ALL_ADJACENT) /*eg. hydra*/ \
  127. BONUS_NAME(MORE_DAMAGE_FROM_SPELL) /*value - damage increase in %, subtype - spell id*/ \
  128. BONUS_NAME(FEAR) \
  129. BONUS_NAME(FEARLESS) \
  130. BONUS_NAME(NO_DISTANCE_PENALTY) \
  131. BONUS_NAME(SELF_LUCK) /*halfling*/ \
  132. BONUS_NAME(ENCHANTER)/* for Enchanter spells, val - skill level, subtype - spell id, additionalInfo - cooldown */ \
  133. BONUS_NAME(HEALER) \
  134. BONUS_NAME(SIEGE_WEAPON) \
  135. BONUS_NAME(HYPNOTIZED) \
  136. BONUS_NAME(ADDITIONAL_RETALIATION) /*value - number of additional retaliations*/ \
  137. BONUS_NAME(MAGIC_MIRROR) /* value - chance of redirecting in %*/ \
  138. BONUS_NAME(ALWAYS_MINIMUM_DAMAGE) /*unit does its minimum damage from range; subtype: -1 - any attack, 0 - melee, 1 - ranged, value: additional damage penalty (it'll subtracted from dmg), additional info - multiplicative anti-bonus for dmg in % [eg 20 means that creature will inflict 80% of normal minimal dmg]*/ \
  139. BONUS_NAME(ALWAYS_MAXIMUM_DAMAGE) /*eg. bless effect, subtype: -1 - any attack, 0 - melee, 1 - ranged, value: additional damage, additional info - multiplicative bonus for dmg in %*/ \
  140. BONUS_NAME(ATTACKS_NEAREST_CREATURE) /*while in berserk*/ \
  141. BONUS_NAME(IN_FRENZY) /*value - level*/ \
  142. BONUS_NAME(SLAYER) /*value - level*/ \
  143. BONUS_NAME(FORGETFULL) /*forgetfulness spell effect, value - level*/ \
  144. BONUS_NAME(NOT_ACTIVE) \
  145. BONUS_NAME(NO_LUCK) /*eg. when fighting on cursed ground*/ \
  146. BONUS_NAME(NO_MORALE) /*eg. when fighting on cursed ground*/ \
  147. BONUS_NAME(DARKNESS) /*val = radius */ \
  148. BONUS_NAME(SPECIAL_SECONDARY_SKILL) /*val = id, additionalInfo = value per level in percent*/ \
  149. BONUS_NAME(SPECIAL_SPELL_LEV) /*val = id, additionalInfo = value per level in percent*/\
  150. BONUS_NAME(SPELL_DAMAGE) /*val = value*/\
  151. BONUS_NAME(SPECIFIC_SPELL_DAMAGE) /*subtype = id of spell, val = value*/\
  152. BONUS_NAME(SPECIAL_BLESS_DAMAGE) /*val = spell (bless), additionalInfo = value per level in percent*/\
  153. BONUS_NAME(MAXED_SPELL) /*val = id*/\
  154. BONUS_NAME(SPECIAL_PECULIAR_ENCHANT) /*blesses and curses with id = val dependent on unit's level, subtype = 0 or 1 for Coronius*/\
  155. BONUS_NAME(SPECIAL_UPGRADE) /*val = base, additionalInfo = target */\
  156. BONUS_NAME(DRAGON_NATURE) \
  157. BONUS_NAME(CREATURE_DAMAGE)/*subtype 0 = both, 1 = min, 2 = max*/\
  158. BONUS_NAME(EXP_MULTIPLIER)/* val - percent of additional exp gained by stack/commander (base value 100)*/\
  159. BONUS_NAME(SHOTS)\
  160. BONUS_NAME(DEATH_STARE) /*subtype 0 - gorgon, 1 - commander*/\
  161. BONUS_NAME(POISON) /*val - max health penalty from poison possible*/\
  162. BONUS_NAME(BIND_EFFECT) /*doesn't do anything particular, works as a marker)*/\
  163. BONUS_NAME(ACID_BREATH) /*additional val damage per creature after attack, additional info - chance in percent*/\
  164. BONUS_NAME(RECEPTIVE) /*accepts friendly spells even with immunity*/\
  165. BONUS_NAME(DIRECT_DAMAGE_IMMUNITY) /*direct damage spells, that is*/\
  166. BONUS_NAME(CASTS) /*how many times creature can cast activated spell*/ \
  167. BONUS_NAME(SPECIFIC_SPELL_POWER) /* value used for Thunderbolt and Resurrection casted by units, subtype - spell id */\
  168. BONUS_NAME(CREATURE_SPELL_POWER) /* value per unit, divided by 100 (so faerie Dragons have 800)*/ \
  169. BONUS_NAME(CREATURE_ENCHANT_POWER) /* total duration of spells casted by creature */ \
  170. BONUS_NAME(REBIRTH) /* val - percent of life restored, subtype = 0 - regular, 1 - at least one unit (sacred Phoenix) */
  171. /// Struct for handling bonuses of several types. Can be transferred to any hero
  172. struct DLL_EXPORT Bonus
  173. {
  174. enum BonusType
  175. {
  176. #define BONUS_NAME(x) x,
  177. BONUS_LIST
  178. #undef BONUS_NAME
  179. };
  180. enum BonusDuration //when bonus is automatically removed
  181. {
  182. PERMANENT = 1,
  183. ONE_BATTLE = 2, //at the end of battle
  184. ONE_DAY = 4, //at the end of day
  185. ONE_WEEK = 8, //at the end of week (bonus lasts till the end of week, thats NOT 7 days
  186. N_TURNS = 16, //used during battles, after battle bonus is always removed
  187. N_DAYS = 32,
  188. UNITL_BEING_ATTACKED = 64,/*removed after attack and counterattacks are performed*/
  189. UNTIL_ATTACK = 128, /*removed after attack and counterattacks are performed*/
  190. STACK_GETS_TURN = 256 /*removed when stack gets its turn - used for defensive stance*/
  191. };
  192. enum BonusSource
  193. {
  194. ARTIFACT,
  195. ARTIFACT_INSTANCE,
  196. OBJECT,
  197. CREATURE_ABILITY,
  198. TERRAIN_NATIVE,
  199. TERRAIN_OVERLAY,
  200. SPELL_EFFECT,
  201. TOWN_STRUCTURE,
  202. HERO_BASE_SKILL,
  203. SECONDARY_SKILL,
  204. HERO_SPECIAL,
  205. ARMY,
  206. CAMPAIGN_BONUS,
  207. SPECIAL_WEEK,
  208. STACK_EXPERIENCE,
  209. OTHER /*used for defensive stance and default value of spell level limit*/
  210. };
  211. enum LimitEffect
  212. {
  213. NO_LIMIT = 0,
  214. ONLY_DISTANCE_FIGHT=1, ONLY_MELEE_FIGHT, //used to mark bonuses for attack/defense primary skills from spells like Precision (distance only)
  215. ONLY_ENEMY_ARMY
  216. };
  217. enum ValueType
  218. {
  219. ADDITIVE_VALUE,
  220. BASE_NUMBER,
  221. PERCENT_TO_ALL,
  222. PERCENT_TO_BASE,
  223. INDEPENDENT_MAX, //used for SPELL bonus
  224. INDEPENDENT_MIN //used for SECONDARY_SKILL_PREMY bonus
  225. };
  226. ui16 duration; //uses BonusDuration values
  227. si16 turnsRemain; //used if duration is N_TURNS or N_DAYS
  228. TBonusType type; //uses BonusType values - says to what is this bonus - 1 byte
  229. TBonusSubtype subtype; //-1 if not applicable - 4 bytes
  230. ui8 source;//source type" uses BonusSource values - what gave that bonus
  231. si32 val;
  232. ui32 sid; //source id: id of object/artifact/spell
  233. ui8 valType; //by ValueType enum
  234. si32 additionalInfo;
  235. ui8 effectRange; //if not NO_LIMIT, bonus will be omitted by default
  236. boost::shared_ptr<ILimiter> limiter;
  237. boost::shared_ptr<IPropagator> propagator;
  238. std::string description;
  239. Bonus(ui16 Dur, ui8 Type, ui8 Src, si32 Val, ui32 ID, std::string Desc, si32 Subtype=-1);
  240. Bonus(ui16 Dur, ui8 Type, ui8 Src, si32 Val, ui32 ID, si32 Subtype=-1, ui8 ValType = ADDITIVE_VALUE);
  241. Bonus();
  242. ~Bonus();
  243. // //comparison
  244. // bool operator==(const HeroBonus &other)
  245. // {
  246. // return &other == this;
  247. // //TODO: what is best logic for that?
  248. // }
  249. // bool operator<(const HeroBonus &other)
  250. // {
  251. // return &other < this;
  252. // //TODO: what is best logic for that?
  253. // }
  254. template <typename Handler> void serialize(Handler &h, const int version)
  255. {
  256. h & duration & type & subtype & source & val & sid & description & additionalInfo & turnsRemain & valType & effectRange & limiter & propagator;
  257. }
  258. static bool compareByAdditionalInfo(const Bonus *a, const Bonus *b)
  259. {
  260. return a->additionalInfo < b->additionalInfo;
  261. }
  262. static bool OneDay(const Bonus *hb)
  263. {
  264. return hb->duration & Bonus::ONE_DAY;
  265. }
  266. static bool OneWeek(const Bonus *hb)
  267. {
  268. return hb->duration & Bonus::ONE_WEEK;
  269. }
  270. static bool OneBattle(const Bonus *hb)
  271. {
  272. return hb->duration & Bonus::ONE_BATTLE;
  273. }
  274. static bool UntilGetsTurn(const Bonus *hb)
  275. {
  276. return hb->duration & Bonus::STACK_GETS_TURN;
  277. }
  278. static bool UntilAttack(const Bonus *hb)
  279. {
  280. return hb->duration & Bonus::UNTIL_ATTACK;
  281. }
  282. static bool UntilBeingAttacked(const Bonus *hb)
  283. {
  284. return hb->duration & Bonus::UNITL_BEING_ATTACKED;
  285. }
  286. static bool IsFrom(const Bonus &hb, ui8 source, ui32 id) //if id==0xffffff then id doesn't matter
  287. {
  288. return hb.source==source && (id==0xffffff || hb.sid==id);
  289. }
  290. inline bool operator == (const BonusType & cf) const
  291. {
  292. return type == cf;
  293. }
  294. inline void ChangeBonusVal (const ui32 newVal)
  295. {
  296. val = newVal;
  297. }
  298. inline void operator += (const ui32 Val) //no return
  299. {
  300. val += Val;
  301. }
  302. const CSpell * sourceSpell() const;
  303. std::string Description() const;
  304. Bonus *addLimiter(ILimiter *Limiter); //returns this for convenient chain-calls
  305. Bonus *addPropagator(IPropagator *Propagator); //returns this for convenient chain-calls
  306. Bonus *addLimiter(boost::shared_ptr<ILimiter> Limiter); //returns this for convenient chain-calls
  307. Bonus *addPropagator(boost::shared_ptr<IPropagator> Propagator); //returns this for convenient chain-calls
  308. };
  309. DLL_EXPORT std::ostream & operator<<(std::ostream &out, const Bonus &bonus);
  310. class DLL_EXPORT BonusList
  311. {
  312. private:
  313. std::vector<Bonus*> bonuses;
  314. bool belongsToTree;
  315. public:
  316. BonusList(bool BelongsToTree = false);
  317. BonusList(const BonusList &bonusList);
  318. BonusList& operator=(const BonusList &bonusList);
  319. // wrapper functions of the STL vector container
  320. std::vector<Bonus*>::size_type size() const { return bonuses.size(); }
  321. void push_back(Bonus* const &x);
  322. std::vector<Bonus*>::iterator erase (const int position);
  323. void clear();
  324. void resize(std::vector<Bonus*>::size_type sz, Bonus* c = NULL );
  325. void insert(std::vector<Bonus*>::iterator position, std::vector<Bonus*>::size_type n, Bonus* const &x);
  326. Bonus *const &operator[] (std::vector<Bonus*>::size_type n) { return bonuses[n]; }
  327. Bonus *const &operator[] (std::vector<Bonus*>::size_type n) const { return bonuses[n]; }
  328. Bonus *const &back() { return bonuses.back(); }
  329. Bonus *const &front() { return bonuses.front(); }
  330. Bonus *const &back() const { return bonuses.back(); }
  331. Bonus *const &front() const { return bonuses.front(); }
  332. // There should be no non-const access to provide solid,robust bonus caching
  333. std::vector<Bonus*>::const_iterator begin() const { return bonuses.begin(); }
  334. std::vector<Bonus*>::const_iterator end() const { return bonuses.end(); }
  335. std::vector<Bonus*>::size_type operator-=(Bonus* const &i);
  336. // BonusList functions
  337. int totalValue() const; //subtype -> subtype of bonus, if -1 then any
  338. void getBonuses(TBonusListPtr out, const CSelector &selector, const CSelector &limit, const bool caching = false) const;
  339. void getModifiersWDescr(TModDescr &out) const;
  340. void getBonuses(TBonusListPtr out, const CSelector &selector) const;
  341. //special find functions
  342. Bonus *getFirst(const CSelector &select);
  343. const Bonus *getFirst(const CSelector &select) const;
  344. int valOfBonuses(const CSelector &select) const;
  345. void limit(const CBonusSystemNode &node); //erases bonuses using limitor
  346. void eliminateDuplicates();
  347. // remove_if implementation for STL vector types
  348. template <class Predicate>
  349. void remove_if(Predicate pred)
  350. {
  351. BonusList newList;
  352. for (unsigned int i = 0; i < bonuses.size(); i++)
  353. {
  354. Bonus *b = bonuses[i];
  355. if (!pred(b))
  356. newList.push_back(b);
  357. }
  358. bonuses.clear();
  359. bonuses.resize(newList.size());
  360. std::copy(newList.begin(), newList.end(), bonuses.begin());
  361. }
  362. template <class InputIterator>
  363. void insert(const int position, InputIterator first, InputIterator last);
  364. template <typename Handler> void serialize(Handler &h, const int version)
  365. {
  366. h & static_cast<std::vector<Bonus*>&>(bonuses);
  367. }
  368. friend inline std::vector<Bonus*>::iterator range_begin(BonusList & x);
  369. friend inline std::vector<Bonus*>::iterator range_end(BonusList & x);
  370. };
  371. // Extensions for BOOST_FOREACH to enable iterating of BonusList objects
  372. // Don't touch/call this functions
  373. inline std::vector<Bonus*>::iterator range_begin(BonusList & x)
  374. {
  375. return x.bonuses.begin();
  376. }
  377. inline std::vector<Bonus*>::iterator range_end(BonusList & x)
  378. {
  379. return x.bonuses.end();
  380. }
  381. inline std::vector<Bonus*>::const_iterator range_begin(BonusList const &x)
  382. {
  383. return x.begin();
  384. }
  385. inline std::vector<Bonus*>::const_iterator range_end(BonusList const &x)
  386. {
  387. return x.end();
  388. }
  389. DLL_EXPORT std::ostream & operator<<(std::ostream &out, const BonusList &bonusList);
  390. class DLL_EXPORT IPropagator
  391. {
  392. public:
  393. virtual ~IPropagator();
  394. virtual bool shouldBeAttached(CBonusSystemNode *dest);
  395. //virtual CBonusSystemNode *getDestNode(CBonusSystemNode *source, CBonusSystemNode *redParent, CBonusSystemNode *redChild); //called when red relation between parent-childrem is established / removed
  396. template <typename Handler> void serialize(Handler &h, const int version)
  397. {}
  398. };
  399. class DLL_EXPORT CPropagatorNodeType : public IPropagator
  400. {
  401. ui8 nodeType;
  402. public:
  403. CPropagatorNodeType();
  404. CPropagatorNodeType(ui8 NodeType);
  405. bool shouldBeAttached(CBonusSystemNode *dest);
  406. //CBonusSystemNode *getDestNode(CBonusSystemNode *source, CBonusSystemNode *redParent, CBonusSystemNode *redChild) OVERRIDE;
  407. template <typename Handler> void serialize(Handler &h, const int version)
  408. {
  409. h & nodeType;
  410. }
  411. };
  412. class DLL_EXPORT ILimiter
  413. {
  414. public:
  415. virtual ~ILimiter();
  416. virtual bool limit(const Bonus *b, const CBonusSystemNode &node) const; //return true to drop the bonus
  417. template <typename Handler> void serialize(Handler &h, const int version)
  418. {}
  419. };
  420. class DLL_EXPORT IBonusBearer
  421. {
  422. public:
  423. //new bonusing node interface
  424. // * selector is predicate that tests if HeroBonus matches our criteria
  425. // * root is node on which call was made (NULL will be replaced with this)
  426. //interface
  427. virtual const TBonusListPtr getAllBonuses(const CSelector &selector, const CSelector &limit, const CBonusSystemNode *root = NULL, const std::string &cachingStr = "") const = 0;
  428. void getModifiersWDescr(TModDescr &out, const CSelector &selector, const std::string &cachingStr = "") const; //out: pairs<modifier value, modifier description>
  429. int getBonusesCount(const CSelector &selector, const std::string &cachingStr = "") const;
  430. int valOfBonuses(const CSelector &selector, const std::string &cachingStr = "") const;
  431. bool hasBonus(const CSelector &selector, const std::string &cachingStr = "") const;
  432. const TBonusListPtr getBonuses(const CSelector &selector, const CSelector &limit, const std::string &cachingStr = "") const;
  433. const TBonusListPtr getBonuses(const CSelector &selector, const std::string &cachingStr = "") const;
  434. //legacy interface
  435. int valOfBonuses(Bonus::BonusType type, const CSelector &selector) const;
  436. int valOfBonuses(Bonus::BonusType type, int subtype = -1) const; //subtype -> subtype of bonus, if -1 then anyt;
  437. bool hasBonusOfType(Bonus::BonusType type, int subtype = -1) const;//determines if hero has a bonus of given type (and optionally subtype)
  438. bool hasBonusFrom(ui8 source, ui32 sourceID) const;
  439. void getModifiersWDescr( TModDescr &out, Bonus::BonusType type, int subtype = -1 ) const; //out: pairs<modifier value, modifier description>
  440. int getBonusesCount(int from, int id) const;
  441. //various hlp functions for non-trivial values
  442. ui32 getMinDamage() const; //used for stacks and creatures only
  443. ui32 getMaxDamage() const;
  444. int MoraleVal() const; //range [-3, +3]
  445. int LuckVal() const; //range [-3, +3]
  446. si32 Attack() const; //get attack of stack with all modificators
  447. si32 Defense(bool withFrenzy = true) const; //get defense of stack with all modificators
  448. ui16 MaxHealth() const; //get max HP of stack with all modifiers
  449. bool isLiving() const; //non-undead, non-non living or alive
  450. virtual si32 magicResistance() const;
  451. si32 manaLimit() const; //maximum mana value for this hero (basically 10*knowledge)
  452. int getPrimSkillLevel(int id) const; //0-attack, 1-defence, 2-spell power, 3-knowledge
  453. const TBonusListPtr getSpellBonuses() const;
  454. };
  455. class DLL_EXPORT CBonusSystemNode : public IBonusBearer
  456. {
  457. private:
  458. BonusList bonuses; //wielded bonuses (local or up-propagated here)
  459. BonusList exportedBonuses; //bonuses coming from this node (wielded or propagated away)
  460. TNodesVector parents; //parents -> we inherit bonuses from them, we may attach our bonuses to them
  461. TNodesVector children;
  462. ui8 nodeType;
  463. std::string description;
  464. static const bool cachingEnabled;
  465. mutable BonusList cachedBonuses;
  466. mutable int cachedLast;
  467. static int treeChanged;
  468. // Setting a value to cachingStr before getting any bonuses caches the result for later requests.
  469. // This string needs to be unique, that's why it has to be setted in the following manner:
  470. // [property key]_[value] => only for selector
  471. mutable std::map<std::string, TBonusListPtr > cachedRequests;
  472. void getAllBonusesRec(TBonusListPtr out, const CSelector &selector, const CSelector &limit, const CBonusSystemNode *root = NULL, const bool caching = false) const;
  473. public:
  474. explicit CBonusSystemNode();
  475. virtual ~CBonusSystemNode();
  476. const TBonusListPtr getAllBonuses(const CSelector &selector, const CSelector &limit, const CBonusSystemNode *root = NULL, const std::string &cachingStr = "") const;
  477. void getParents(TCNodes &out) const; //retrieves list of parent nodes (nodes to inherit bonuses from),
  478. const Bonus *getBonus(const CSelector &selector) const;
  479. //non-const interface
  480. void getParents(TNodes &out); //retrieves list of parent nodes (nodes to inherit bonuses from)
  481. void getRedParents(TNodes &out); //retrieves list of red parent nodes (nodes bonuses propagate from)
  482. void getRedAncestors(TNodes &out);
  483. void getRedChildren(TNodes &out);
  484. void getRedDescendants(TNodes &out);
  485. Bonus *getBonus(const CSelector &selector);
  486. void attachTo(CBonusSystemNode *parent);
  487. void detachFrom(CBonusSystemNode *parent);
  488. void detachFromAll();
  489. void addNewBonus(Bonus *b); //b will be deleted with destruction of node
  490. void newChildAttached(CBonusSystemNode *child);
  491. void childDetached(CBonusSystemNode *child);
  492. void propagateBonus(Bonus * b);
  493. void unpropagateBonus(Bonus * b);
  494. //void addNewBonus(const Bonus &b); //b will copied
  495. void removeBonus(Bonus *b);
  496. void newRedDescendant(CBonusSystemNode *descendant); //propagation needed
  497. void removedRedDescendant(CBonusSystemNode *descendant); //de-propagation needed
  498. void battleTurnPassed(); //updates count of remaining turns and removed outdated bonuses
  499. bool isIndependentNode() const; //node is independent when it has no parents nor children
  500. bool actsAsBonusSourceOnly() const;
  501. bool isLimitedOnUs(Bonus *b) const; //if bonus should be removed from list acquired from this node
  502. void popBonuses(const CSelector &s);
  503. virtual std::string bonusToString(Bonus *bonus, bool description) const {return "";}; //description or bonus name
  504. virtual std::string nodeName() const;
  505. void deserializationFix();
  506. void exportBonus(Bonus * b);
  507. void exportBonuses();
  508. static void incrementTreeChangedNum();
  509. BonusList &getBonusList();
  510. const BonusList &getBonusList() const;
  511. BonusList &getExportedBonusList();
  512. const ui8 getNodeType() const;
  513. void setNodeType(ui8 type);
  514. const TNodesVector &getParentNodes() const;
  515. const TNodesVector &getChildrenNodes() const;
  516. const std::string &getDescription() const;
  517. void setDescription(const std::string &description);
  518. template <typename Handler> void serialize(Handler &h, const int version)
  519. {
  520. h & /*bonuses & */nodeType;
  521. h & exportedBonuses;
  522. h & description;
  523. BONUS_TREE_DESERIALIZATION_FIX
  524. //h & parents & children;
  525. }
  526. enum ENodeTypes
  527. {
  528. UNKNOWN, STACK_INSTANCE, STACK_BATTLE, SPECIALITY, ARTIFACT, CREATURE, ARTIFACT_INSTANCE, HERO, PLAYER, TEAM, TOWN_AND_VISITOR
  529. };
  530. };
  531. namespace NBonus
  532. {
  533. //set of methods that may be safely called with NULL objs
  534. DLL_EXPORT int valOf(const CBonusSystemNode *obj, Bonus::BonusType type, int subtype = -1); //subtype -> subtype of bonus, if -1 then any
  535. DLL_EXPORT bool hasOfType(const CBonusSystemNode *obj, Bonus::BonusType type, int subtype = -1);//determines if hero has a bonus of given type (and optionally subtype)
  536. //DLL_EXPORT const HeroBonus * get(const CBonusSystemNode *obj, int from, int id );
  537. DLL_EXPORT void getModifiersWDescr(const CBonusSystemNode *obj, TModDescr &out, Bonus::BonusType type, int subtype = -1 ); //out: pairs<modifier value, modifier description>
  538. DLL_EXPORT int getCount(const CBonusSystemNode *obj, int from, int id);
  539. };
  540. /// generates HeroBonus from given data
  541. inline Bonus makeFeatureVal(Bonus::BonusType type, ui8 duration, si16 subtype, si32 value, Bonus::BonusSource source, ui16 turnsRemain = 0, si32 additionalInfo = 0)
  542. {
  543. Bonus sf;
  544. sf.type = type;
  545. sf.duration = duration;
  546. sf.source = source;
  547. sf.turnsRemain = turnsRemain;
  548. sf.subtype = subtype;
  549. sf.val = value;
  550. sf.additionalInfo = additionalInfo;
  551. return sf;
  552. }
  553. //generates HeroBonus from given data
  554. inline Bonus * makeFeature(Bonus::BonusType type, ui8 duration, si16 subtype, si32 value, Bonus::BonusSource source, ui16 turnsRemain = 0, si32 additionalInfo = 0)
  555. {
  556. return new Bonus(makeFeatureVal(type, duration, subtype, value, source, turnsRemain, additionalInfo));
  557. }
  558. class DLL_EXPORT CSelectorsConjunction
  559. {
  560. const CSelector first, second;
  561. public:
  562. CSelectorsConjunction(const CSelector &First, const CSelector &Second)
  563. :first(First), second(Second)
  564. {
  565. }
  566. bool operator()(const Bonus *bonus) const
  567. {
  568. return first(bonus) && second(bonus);
  569. }
  570. };
  571. CSelector DLL_EXPORT operator&&(const CSelector &first, const CSelector &second);
  572. class DLL_EXPORT CSelectorsAlternative
  573. {
  574. const CSelector first, second;
  575. public:
  576. CSelectorsAlternative(const CSelector &First, const CSelector &Second)
  577. :first(First), second(Second)
  578. {
  579. }
  580. bool operator()(const Bonus *bonus) const
  581. {
  582. return first(bonus) || second(bonus);
  583. }
  584. };
  585. CSelector DLL_EXPORT operator||(const CSelector &first, const CSelector &second);
  586. template<typename T>
  587. class CSelectFieldEqual
  588. {
  589. T Bonus::*ptr;
  590. T val;
  591. public:
  592. CSelectFieldEqual(T Bonus::*Ptr, const T &Val)
  593. : ptr(Ptr), val(Val)
  594. {
  595. }
  596. bool operator()(const Bonus *bonus) const
  597. {
  598. return bonus->*ptr == val;
  599. }
  600. CSelectFieldEqual& operator()(const T &setVal)
  601. {
  602. val = setVal;
  603. return *this;
  604. }
  605. };
  606. class DLL_EXPORT CWillLastTurns
  607. {
  608. public:
  609. int turnsRequested;
  610. bool operator()(const Bonus *bonus) const
  611. {
  612. return turnsRequested <= 0 //every present effect will last zero (or "less") turns
  613. || !(bonus->duration & Bonus::N_TURNS) //so do every not expriing after N-turns effect
  614. || bonus->turnsRemain > turnsRequested;
  615. }
  616. CWillLastTurns& operator()(const int &setVal)
  617. {
  618. turnsRequested = setVal;
  619. return *this;
  620. }
  621. };
  622. class DLL_EXPORT CCreatureTypeLimiter : public ILimiter //affect only stacks of given creature (and optionally it's upgrades)
  623. {
  624. public:
  625. const CCreature *creature;
  626. ui8 includeUpgrades;
  627. CCreatureTypeLimiter();
  628. CCreatureTypeLimiter(const CCreature &Creature, ui8 IncludeUpgrades = true);
  629. bool limit(const Bonus *b, const CBonusSystemNode &node) const OVERRIDE;
  630. template <typename Handler> void serialize(Handler &h, const int version)
  631. {
  632. h & creature & includeUpgrades;
  633. }
  634. };
  635. class DLL_EXPORT HasAnotherBonusLimiter : public ILimiter //applies only to nodes that have another bonus working
  636. {
  637. public:
  638. TBonusType type;
  639. TBonusSubtype subtype;
  640. ui8 isSubtypeRelevant; //check for subtype only if this is true
  641. HasAnotherBonusLimiter(TBonusType bonus = Bonus::NONE);
  642. HasAnotherBonusLimiter(TBonusType bonus, TBonusSubtype _subtype);
  643. bool limit(const Bonus *b, const CBonusSystemNode &node) const OVERRIDE;
  644. template <typename Handler> void serialize(Handler &h, const int version)
  645. {
  646. h & type & subtype & isSubtypeRelevant;
  647. }
  648. };
  649. class DLL_EXPORT CreatureNativeTerrainLimiter : public ILimiter //applies only to creatures that are on their native terrain
  650. {
  651. public:
  652. si8 terrainType;
  653. CreatureNativeTerrainLimiter();
  654. CreatureNativeTerrainLimiter(int TerrainType);
  655. bool limit(const Bonus *b, const CBonusSystemNode &node) const OVERRIDE;
  656. template <typename Handler> void serialize(Handler &h, const int version)
  657. {
  658. h & terrainType;
  659. }
  660. };
  661. class DLL_EXPORT CreatureFactionLimiter : public ILimiter //applies only to creatures of given faction
  662. {
  663. public:
  664. si8 faction;
  665. CreatureFactionLimiter();
  666. CreatureFactionLimiter(int TerrainType);
  667. bool limit(const Bonus *b, const CBonusSystemNode &node) const OVERRIDE;
  668. template <typename Handler> void serialize(Handler &h, const int version)
  669. {
  670. h & faction;
  671. }
  672. };
  673. class DLL_EXPORT CreatureAlignmentLimiter : public ILimiter //applies only to creatures of given alignment
  674. {
  675. public:
  676. si8 alignment;
  677. CreatureAlignmentLimiter();
  678. CreatureAlignmentLimiter(si8 Alignment);
  679. bool limit(const Bonus *b, const CBonusSystemNode &node) const OVERRIDE;
  680. template <typename Handler> void serialize(Handler &h, const int version)
  681. {
  682. h & alignment;
  683. }
  684. };
  685. class DLL_EXPORT StackOwnerLimiter : public ILimiter //applies only to creatures of given alignment
  686. {
  687. public:
  688. ui8 owner;
  689. StackOwnerLimiter();
  690. StackOwnerLimiter(ui8 Owner);
  691. bool limit(const Bonus *b, const CBonusSystemNode &node) const OVERRIDE;
  692. template <typename Handler> void serialize(Handler &h, const int version)
  693. {
  694. h & owner;
  695. }
  696. };
  697. class DLL_EXPORT RankRangeLimiter : public ILimiter //applies to creatures with min <= Rank <= max
  698. {
  699. public:
  700. ui8 minRank, maxRank;
  701. RankRangeLimiter();
  702. RankRangeLimiter(ui8 Min, ui8 Max = 255);
  703. bool limit(const Bonus *b, const CBonusSystemNode &node) const OVERRIDE;
  704. template <typename Handler> void serialize(Handler &h, const int version)
  705. {
  706. h & minRank & maxRank;
  707. }
  708. };
  709. const CCreature *retrieveCreature(const CBonusSystemNode *node);
  710. namespace Selector
  711. {
  712. extern DLL_EXPORT CSelectFieldEqual<TBonusType> type;
  713. extern DLL_EXPORT CSelectFieldEqual<TBonusSubtype> subtype;
  714. extern DLL_EXPORT CSelectFieldEqual<si32> info;
  715. extern DLL_EXPORT CSelectFieldEqual<ui16> duration;
  716. extern DLL_EXPORT CSelectFieldEqual<ui8> sourceType;
  717. extern DLL_EXPORT CSelectFieldEqual<ui8> effectRange;
  718. extern DLL_EXPORT CWillLastTurns turns;
  719. CSelector DLL_EXPORT typeSubtype(TBonusType Type, TBonusSubtype Subtype);
  720. CSelector DLL_EXPORT typeSubtypeInfo(TBonusType type, TBonusSubtype subtype, si32 info);
  721. CSelector DLL_EXPORT source(ui8 source, ui32 sourceID);
  722. CSelector DLL_EXPORT durationType(ui16 duration);
  723. CSelector DLL_EXPORT sourceTypeSel(ui8 source);
  724. bool DLL_EXPORT matchesType(const CSelector &sel, TBonusType type);
  725. bool DLL_EXPORT matchesTypeSubtype(const CSelector &sel, TBonusType type, TBonusSubtype subtype);
  726. bool DLL_EXPORT positiveSpellEffects(const Bonus *b);
  727. }
  728. extern DLL_EXPORT const std::map<std::string, int> bonusNameMap;
  729. // BonusList template that requires full interface of CBonusSystemNode
  730. template <class InputIterator>
  731. void BonusList::insert(const int position, InputIterator first, InputIterator last)
  732. {
  733. bonuses.insert(bonuses.begin() + position, first, last);
  734. if (belongsToTree)
  735. CBonusSystemNode::incrementTreeChangedNum();
  736. }
  737. // Extensions for BOOST_FOREACH to enable iterating of BonusList objects
  738. namespace boost
  739. {
  740. template<>
  741. struct range_mutable_iterator<BonusList>
  742. {
  743. typedef std::vector<Bonus*>::iterator type;
  744. };
  745. template<>
  746. struct range_const_iterator<BonusList>
  747. {
  748. typedef std::vector<Bonus*>::const_iterator type;
  749. };
  750. }