HeroBonus.h 38 KB

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