HeroBonus.h 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723
  1. /*
  2. * HeroBonus.h, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #pragma once
  11. #include "../GameConstants.h"
  12. #include "../JsonNode.h"
  13. VCMI_LIB_NAMESPACE_BEGIN
  14. struct Bonus;
  15. class IBonusBearer;
  16. class CBonusSystemNode;
  17. class ILimiter;
  18. class IPropagator;
  19. class IUpdater;
  20. class BonusList;
  21. using TBonusListPtr = std::shared_ptr<BonusList>;
  22. using TConstBonusListPtr = std::shared_ptr<const BonusList>;
  23. using TLimiterPtr = std::shared_ptr<ILimiter>;
  24. using TPropagatorPtr = std::shared_ptr<IPropagator>;
  25. using TUpdaterPtr = std::shared_ptr<IUpdater>;
  26. class CSelector : std::function<bool(const Bonus*)>
  27. {
  28. using TBase = std::function<bool(const Bonus*)>;
  29. public:
  30. CSelector() = default;
  31. template<typename T>
  32. CSelector(const T &t, //SFINAE trick -> include this c-tor in overload resolution only if parameter is class
  33. //(includes functors, lambdas) or function. Without that VC is going mad about ambiguities.
  34. typename std::enable_if < boost::mpl::or_ < std::is_class<T>, std::is_function<T >> ::value>::type *dummy = nullptr)
  35. : TBase(t)
  36. {}
  37. CSelector(std::nullptr_t)
  38. {}
  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. CSelector Not() const
  51. {
  52. auto thisCopy = *this;
  53. return [thisCopy](const Bonus *b) mutable { return !thisCopy(b); };
  54. }
  55. bool operator()(const Bonus *b) const
  56. {
  57. return TBase::operator()(b);
  58. }
  59. operator bool() const
  60. {
  61. return !!static_cast<const TBase&>(*this);
  62. }
  63. };
  64. class DLL_LINKAGE CAddInfo : public std::vector<si32>
  65. {
  66. public:
  67. enum { NONE = -1 };
  68. CAddInfo();
  69. CAddInfo(si32 value);
  70. bool operator==(si32 value) const;
  71. bool operator!=(si32 value) const;
  72. si32 & operator[](size_type pos);
  73. si32 operator[](size_type pos) const;
  74. std::string toString() const;
  75. JsonNode toJsonNode() const;
  76. };
  77. #define BONUS_TREE_DESERIALIZATION_FIX if(!h.saving && h.smartPointerSerialization) deserializationFix();
  78. #define BONUS_LIST \
  79. BONUS_NAME(NONE) \
  80. BONUS_NAME(LEVEL_COUNTER) /* for commander artifacts*/ \
  81. BONUS_NAME(MOVEMENT) /*Subtype is 1 - land, 0 - sea*/ \
  82. BONUS_NAME(MORALE) \
  83. BONUS_NAME(LUCK) \
  84. BONUS_NAME(PRIMARY_SKILL) /*uses subtype to pick skill; additional info if set: 1 - only melee, 2 - only distance*/ \
  85. BONUS_NAME(SIGHT_RADIUS) \
  86. BONUS_NAME(MANA_REGENERATION) /*points per turn apart from normal (1 + mysticism)*/ \
  87. BONUS_NAME(FULL_MANA_REGENERATION) /*all mana points are replenished every day*/ \
  88. BONUS_NAME(NONEVIL_ALIGNMENT_MIX) /*good and neutral creatures can be mixed without morale penalty*/ \
  89. BONUS_NAME(SURRENDER_DISCOUNT) /*%*/ \
  90. BONUS_NAME(STACKS_SPEED) /*additional info - percent of speed bonus applied after direct bonuses; >0 - added, <0 - subtracted to this part*/ \
  91. BONUS_NAME(FLYING_MOVEMENT) /*value - penalty percentage*/ \
  92. BONUS_NAME(SPELL_DURATION) \
  93. BONUS_NAME(AIR_SPELL_DMG_PREMY) \
  94. BONUS_NAME(EARTH_SPELL_DMG_PREMY) \
  95. BONUS_NAME(FIRE_SPELL_DMG_PREMY) \
  96. BONUS_NAME(WATER_SPELL_DMG_PREMY) \
  97. BONUS_NAME(WATER_WALKING) /*value - penalty percentage*/ \
  98. BONUS_NAME(NEGATE_ALL_NATURAL_IMMUNITIES) \
  99. BONUS_NAME(STACK_HEALTH) \
  100. BONUS_NAME(FIRE_SPELLS) \
  101. BONUS_NAME(AIR_SPELLS) \
  102. BONUS_NAME(WATER_SPELLS) \
  103. BONUS_NAME(EARTH_SPELLS) \
  104. BONUS_NAME(GENERATE_RESOURCE) /*daily value, uses subtype (resource type)*/ \
  105. BONUS_NAME(CREATURE_GROWTH) /*for legion artifacts: value - week growth bonus, subtype - monster level if aplicable*/ \
  106. BONUS_NAME(WHIRLPOOL_PROTECTION) /*hero won't lose army when teleporting through whirlpool*/ \
  107. BONUS_NAME(SPELL) /*hero knows spell, val - skill level (0 - 3), subtype - spell id*/ \
  108. BONUS_NAME(SPELLS_OF_LEVEL) /*hero knows all spells of given level, val - skill level; subtype - level*/ \
  109. BONUS_NAME(BATTLE_NO_FLEEING) /*for shackles of war*/ \
  110. 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*/ \
  111. BONUS_NAME(FREE_SHOOTING) /*stacks can shoot even if otherwise blocked (sharpshooter's bow effect)*/ \
  112. BONUS_NAME(OPENING_BATTLE_SPELL) /*casts a spell at expert level at beginning of battle, val - spell power, subtype - spell id*/ \
  113. BONUS_NAME(IMPROVED_NECROMANCY) /* raise more powerful creatures: subtype - creature type raised, addInfo - [required necromancy level, required stack level], val - necromancy level for this purpose */ \
  114. BONUS_NAME(CREATURE_GROWTH_PERCENT) /*increases growth of all units in all towns, val - percentage*/ \
  115. BONUS_NAME(FREE_SHIP_BOARDING) /*movement points preserved with ship boarding and landing*/ \
  116. BONUS_NAME(FLYING) \
  117. BONUS_NAME(SHOOTER) \
  118. BONUS_NAME(CHARGE_IMMUNITY) \
  119. BONUS_NAME(ADDITIONAL_ATTACK) \
  120. BONUS_NAME(UNLIMITED_RETALIATIONS) \
  121. BONUS_NAME(NO_MELEE_PENALTY) \
  122. BONUS_NAME(JOUSTING) /*for champions*/ \
  123. BONUS_NAME(HATE) /*eg. angels hate devils, subtype - ID of hated creature, val - damage bonus percent */ \
  124. BONUS_NAME(KING) /* val - required slayer bonus val to affect */\
  125. BONUS_NAME(MAGIC_RESISTANCE) /*in % (value)*/ \
  126. BONUS_NAME(CHANGES_SPELL_COST_FOR_ALLY) /*in mana points (value) , eg. mage*/ \
  127. BONUS_NAME(CHANGES_SPELL_COST_FOR_ENEMY) /*in mana points (value) , eg. pegasus */ \
  128. BONUS_NAME(SPELL_AFTER_ATTACK) /* subtype - spell id, value - chance %, addInfo[0] - level, addInfo[1] -> [0 - all attacks, 1 - shot only, 2 - melee only] */ \
  129. BONUS_NAME(SPELL_BEFORE_ATTACK) /* subtype - spell id, value - chance %, addInfo[0] - level, addInfo[1] -> [0 - all attacks, 1 - shot only, 2 - melee only] */ \
  130. BONUS_NAME(SPELL_RESISTANCE_AURA) /*eg. unicorns, value - resistance bonus in % for adjacent creatures*/ \
  131. BONUS_NAME(LEVEL_SPELL_IMMUNITY) /*creature is immune to all spell with level below or equal to value of this bonus */ \
  132. BONUS_NAME(BLOCK_MAGIC_ABOVE) /*blocks casting spells of the level > value */ \
  133. BONUS_NAME(BLOCK_ALL_MAGIC) /*blocks casting spells*/ \
  134. BONUS_NAME(TWO_HEX_ATTACK_BREATH) /*eg. dragons*/ \
  135. BONUS_NAME(SPELL_DAMAGE_REDUCTION) /*eg. golems; value - reduction in %, subtype - spell school; -1 - all, 0 - air, 1 - fire, 2 - water, 3 - earth*/ \
  136. BONUS_NAME(NO_WALL_PENALTY) \
  137. BONUS_NAME(NON_LIVING) /*eg. golems, cannot be rised or healed, only neutral morale */ \
  138. BONUS_NAME(RANDOM_SPELLCASTER) /*eg. master genie, val - level*/ \
  139. BONUS_NAME(BLOCKS_RETALIATION) /*eg. naga*/ \
  140. BONUS_NAME(SPELL_IMMUNITY) /*subid - spell id*/ \
  141. BONUS_NAME(MANA_CHANNELING) /*value in %, eg. familiar*/ \
  142. BONUS_NAME(SPELL_LIKE_ATTACK) /*subtype - spell, value - spell level; range is taken from spell, but damage from creature; eg. magog*/ \
  143. BONUS_NAME(THREE_HEADED_ATTACK) /*eg. cerberus*/ \
  144. BONUS_NAME(GENERAL_DAMAGE_PREMY) \
  145. BONUS_NAME(FIRE_IMMUNITY) /*subtype 0 - all, 1 - all except positive, 2 - only damage spells*/ \
  146. BONUS_NAME(WATER_IMMUNITY) \
  147. BONUS_NAME(EARTH_IMMUNITY) \
  148. BONUS_NAME(AIR_IMMUNITY) \
  149. BONUS_NAME(MIND_IMMUNITY) \
  150. BONUS_NAME(FIRE_SHIELD) \
  151. BONUS_NAME(UNDEAD) \
  152. BONUS_NAME(HP_REGENERATION) /*creature regenerates val HP every new round*/ \
  153. BONUS_NAME(MANA_DRAIN) /*value - spell points per turn*/ \
  154. BONUS_NAME(LIFE_DRAIN) \
  155. BONUS_NAME(DOUBLE_DAMAGE_CHANCE) /*value in %, eg. dread knight*/ \
  156. BONUS_NAME(RETURN_AFTER_STRIKE) \
  157. 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*/ \
  158. BONUS_NAME(CATAPULT) \
  159. BONUS_NAME(ENEMY_DEFENCE_REDUCTION) /*in % (value) eg. behemots*/ \
  160. BONUS_NAME(GENERAL_DAMAGE_REDUCTION) /* shield / air shield effect, also armorer skill/petrify effect for subtype -1*/ \
  161. BONUS_NAME(GENERAL_ATTACK_REDUCTION) /*eg. while stoned or blinded - in %,// subtype not used, use ONLY_MELEE_FIGHT / DISTANCE_FIGHT*/ \
  162. BONUS_NAME(DEFENSIVE_STANCE) /* val - bonus to defense while defending */ \
  163. BONUS_NAME(ATTACKS_ALL_ADJACENT) /*eg. hydra*/ \
  164. BONUS_NAME(MORE_DAMAGE_FROM_SPELL) /*value - damage increase in %, subtype - spell id*/ \
  165. BONUS_NAME(FEAR) \
  166. BONUS_NAME(FEARLESS) \
  167. BONUS_NAME(NO_DISTANCE_PENALTY) \
  168. BONUS_NAME(ENCHANTER)/* for Enchanter spells, val - skill level, subtype - spell id, additionalInfo - cooldown */ \
  169. BONUS_NAME(HEALER) \
  170. BONUS_NAME(SIEGE_WEAPON) \
  171. BONUS_NAME(HYPNOTIZED) \
  172. BONUS_NAME(NO_RETALIATION) /*temporary bonus for basilisk, unicorn and scorpicore paralyze*/\
  173. BONUS_NAME(ADDITIONAL_RETALIATION) /*value - number of additional retaliations*/ \
  174. BONUS_NAME(MAGIC_MIRROR) /* value - chance of redirecting in %*/ \
  175. 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]*/ \
  176. 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 %*/ \
  177. BONUS_NAME(ATTACKS_NEAREST_CREATURE) /*while in berserk*/ \
  178. BONUS_NAME(IN_FRENZY) /*value - level*/ \
  179. BONUS_NAME(SLAYER) /*value - level*/ \
  180. BONUS_NAME(FORGETFULL) /*forgetfulness spell effect, value - level*/ \
  181. BONUS_NAME(NOT_ACTIVE) /* subtype - spell ID (paralyze, blind, stone gaze) for graphical effect*/ \
  182. BONUS_NAME(NO_LUCK) /*eg. when fighting on cursed ground*/ \
  183. BONUS_NAME(NO_MORALE) /*eg. when fighting on cursed ground*/ \
  184. BONUS_NAME(DARKNESS) /*val = radius */ \
  185. BONUS_NAME(SPECIAL_SPELL_LEV) /*subtype = id, val = value per level in percent*/\
  186. BONUS_NAME(SPELL_DAMAGE) /*val = value, now works for sorcery*/\
  187. BONUS_NAME(SPECIFIC_SPELL_DAMAGE) /*subtype = id of spell, val = value*/\
  188. BONUS_NAME(SPECIAL_PECULIAR_ENCHANT) /*blesses and curses with id = val dependent on unit's level, subtype = 0 or 1 for Coronius*/\
  189. BONUS_NAME(SPECIAL_UPGRADE) /*subtype = base, additionalInfo = target */\
  190. BONUS_NAME(DRAGON_NATURE) \
  191. BONUS_NAME(CREATURE_DAMAGE)/*subtype 0 = both, 1 = min, 2 = max*/\
  192. BONUS_NAME(EXP_MULTIPLIER)/* val - percent of additional exp gained by stack/commander (base value 100)*/\
  193. BONUS_NAME(SHOTS)\
  194. BONUS_NAME(DEATH_STARE) /*subtype 0 - gorgon, 1 - commander*/\
  195. BONUS_NAME(POISON) /*val - max health penalty from poison possible*/\
  196. BONUS_NAME(BIND_EFFECT) /*doesn't do anything particular, works as a marker)*/\
  197. BONUS_NAME(ACID_BREATH) /*additional val damage per creature after attack, additional info - chance in percent*/\
  198. BONUS_NAME(RECEPTIVE) /*accepts friendly spells even with immunity*/\
  199. BONUS_NAME(DIRECT_DAMAGE_IMMUNITY) /*direct damage spells, that is*/\
  200. BONUS_NAME(CASTS) /*how many times creature can cast activated spell*/ \
  201. BONUS_NAME(SPECIFIC_SPELL_POWER) /* value used for Thunderbolt and Resurrection cast by units, subtype - spell id */\
  202. BONUS_NAME(CREATURE_SPELL_POWER) /* value per unit, divided by 100 (so faerie Dragons have 800)*/ \
  203. BONUS_NAME(CREATURE_ENCHANT_POWER) /* total duration of spells cast by creature */ \
  204. BONUS_NAME(ENCHANTED) /* permanently enchanted with spell subID of level = val, if val > 3 then spell is mass and has level of val-3*/ \
  205. BONUS_NAME(REBIRTH) /* val - percent of life restored, subtype = 0 - regular, 1 - at least one unit (sacred Phoenix) */\
  206. BONUS_NAME(ADDITIONAL_UNITS) /*val of units with id = subtype will be added to hero's army at the beginning of battle */\
  207. BONUS_NAME(SPOILS_OF_WAR) /*val * 10^-6 * gained exp resources of subtype will be given to hero after battle*/\
  208. BONUS_NAME(BLOCK)\
  209. BONUS_NAME(DISGUISED) /* subtype - spell level */\
  210. BONUS_NAME(VISIONS) /* subtype - spell level */\
  211. BONUS_NAME(NO_TERRAIN_PENALTY) /* subtype - terrain type */\
  212. BONUS_NAME(SOUL_STEAL) /*val - number of units gained per enemy killed, subtype = 0 - gained units survive after battle, 1 - they do not*/ \
  213. BONUS_NAME(TRANSMUTATION) /*val - chance to trigger in %, subtype = 0 - resurrection based on HP, 1 - based on unit count, additional info - target creature ID (attacker default)*/\
  214. BONUS_NAME(SUMMON_GUARDIANS) /*val - amount in % of stack count, subtype = creature ID*/\
  215. BONUS_NAME(CATAPULT_EXTRA_SHOTS) /*val - power of catapult effect, requires CATAPULT bonus to work*/\
  216. BONUS_NAME(RANGED_RETALIATION) /*allows shooters to perform ranged retaliation*/\
  217. BONUS_NAME(BLOCKS_RANGED_RETALIATION) /*disallows ranged retaliation for shooter unit, BLOCKS_RETALIATION bonus is for melee retaliation only*/\
  218. BONUS_NAME(MANUAL_CONTROL) /* manually control warmachine with id = subtype, chance = val */ \
  219. BONUS_NAME(WIDE_BREATH) /* initial desigh: dragon breath affecting multiple nearby hexes */\
  220. BONUS_NAME(FIRST_STRIKE) /* first counterattack, then attack if possible */\
  221. BONUS_NAME(SYNERGY_TARGET) /* dummy skill for alternative upgrades mod */\
  222. BONUS_NAME(SHOOTS_ALL_ADJACENT) /* H4 Cyclops-like shoot (attacks all hexes neighboring with target) without spell-like mechanics */\
  223. BONUS_NAME(BLOCK_MAGIC_BELOW) /*blocks casting spells of the level < value */ \
  224. BONUS_NAME(DESTRUCTION) /*kills extra units after hit, subtype = 0 - kill percentage of units, 1 - kill amount, val = chance in percent to trigger, additional info - amount/percentage to kill*/ \
  225. BONUS_NAME(SPECIAL_CRYSTAL_GENERATION) /*crystal dragon crystal generation*/ \
  226. BONUS_NAME(NO_SPELLCAST_BY_DEFAULT) /*spellcast will not be default attack option for this creature*/ \
  227. BONUS_NAME(GARGOYLE) /* gargoyle is special than NON_LIVING, cannot be rised or healed */ \
  228. BONUS_NAME(SPECIAL_ADD_VALUE_ENCHANT) /*specialty spell like Aenin has, increased effect of spell, additionalInfo = value to add*/\
  229. BONUS_NAME(SPECIAL_FIXED_VALUE_ENCHANT) /*specialty spell like Melody has, constant spell effect (i.e. 3 luck), additionalInfo = value to fix.*/\
  230. BONUS_NAME(TOWN_MAGIC_WELL) /*one-time pseudo-bonus to implement Magic Well in the town*/\
  231. BONUS_NAME(LIMITED_SHOOTING_RANGE) /*limits range of shooting creatures, doesn't adjust any other mechanics (half vs full damage etc). val - range in hexes, additional info - optional new range for broken arrow mechanic */\
  232. BONUS_NAME(LEARN_BATTLE_SPELL_CHANCE) /*skill-agnostic eagle eye chance. subtype = 0 - from enemy, 1 - TODO: from entire battlefield*/\
  233. BONUS_NAME(LEARN_BATTLE_SPELL_LEVEL_LIMIT) /*skill-agnostic eagle eye limit, subtype - school (-1 for all), others TODO*/\
  234. BONUS_NAME(PERCENTAGE_DAMAGE_BOOST) /*skill-agnostic archery and offence, subtype is 0 for offence and 1 for archery*/\
  235. BONUS_NAME(LEARN_MEETING_SPELL_LIMIT) /*skill-agnostic scholar, subtype is -1 for all, TODO for others (> 0)*/\
  236. BONUS_NAME(ROUGH_TERRAIN_DISCOUNT) /*skill-agnostic pathfinding*/\
  237. BONUS_NAME(WANDERING_CREATURES_JOIN_BONUS) /*skill-agnostic diplomacy*/\
  238. BONUS_NAME(BEFORE_BATTLE_REPOSITION) /*skill-agnostic tactics, bonus for allowing tactics*/\
  239. BONUS_NAME(BEFORE_BATTLE_REPOSITION_BLOCK) /*skill-agnostic tactics, bonus for blocking opposite tactics. For now donble side tactics is TODO.*/\
  240. BONUS_NAME(HERO_EXPERIENCE_GAIN_PERCENT) /*skill-agnostic learning, and we can use it as a global effect also*/\
  241. BONUS_NAME(UNDEAD_RAISE_PERCENTAGE) /*Percentage of killed enemy creatures to be raised after battle as undead*/\
  242. BONUS_NAME(MANA_PER_KNOWLEDGE) /*Percentage rate of translating 10 hero knowledge to mana, used to intelligence and global bonus*/\
  243. BONUS_NAME(HERO_GRANTS_ATTACKS) /*If hero can grant additional attacks to creature, value is number of attacks, subtype is creatureID*/\
  244. BONUS_NAME(BONUS_DAMAGE_PERCENTAGE) /*If hero can grant conditional damage to creature, value is percentage, subtype is creatureID*/\
  245. BONUS_NAME(BONUS_DAMAGE_CHANCE) /*If hero can grant additional damage to creature, value is chance, subtype is creatureID*/\
  246. BONUS_NAME(MAX_LEARNABLE_SPELL_LEVEL) /*This can work as wisdom before. val = max learnable spell level*/\
  247. /* end of list */
  248. #define BONUS_SOURCE_LIST \
  249. BONUS_SOURCE(ARTIFACT)\
  250. BONUS_SOURCE(ARTIFACT_INSTANCE)\
  251. BONUS_SOURCE(OBJECT)\
  252. BONUS_SOURCE(CREATURE_ABILITY)\
  253. BONUS_SOURCE(TERRAIN_NATIVE)\
  254. BONUS_SOURCE(TERRAIN_OVERLAY)\
  255. BONUS_SOURCE(SPELL_EFFECT)\
  256. BONUS_SOURCE(TOWN_STRUCTURE)\
  257. BONUS_SOURCE(HERO_BASE_SKILL)\
  258. BONUS_SOURCE(SECONDARY_SKILL)\
  259. BONUS_SOURCE(HERO_SPECIAL)\
  260. BONUS_SOURCE(ARMY)\
  261. BONUS_SOURCE(CAMPAIGN_BONUS)\
  262. BONUS_SOURCE(SPECIAL_WEEK)\
  263. BONUS_SOURCE(STACK_EXPERIENCE)\
  264. BONUS_SOURCE(COMMANDER) /*TODO: consider using simply STACK_INSTANCE */\
  265. BONUS_SOURCE(GLOBAL) /*used for base bonuses which all heroes or all stacks should have*/\
  266. BONUS_SOURCE(OTHER) /*used for defensive stance and default value of spell level limit*/
  267. #define BONUS_VALUE_LIST \
  268. BONUS_VALUE(ADDITIVE_VALUE)\
  269. BONUS_VALUE(BASE_NUMBER)\
  270. BONUS_VALUE(PERCENT_TO_ALL)\
  271. BONUS_VALUE(PERCENT_TO_BASE)\
  272. BONUS_VALUE(PERCENT_TO_SOURCE) /*Adds value only to bonuses with same source*/\
  273. BONUS_VALUE(PERCENT_TO_TARGET_TYPE) /*Adds value only to bonuses with SourceType target*/\
  274. BONUS_VALUE(INDEPENDENT_MAX) /*used for SPELL bonus */\
  275. BONUS_VALUE(INDEPENDENT_MIN) //used for SECONDARY_SKILL_PREMY bonus
  276. /// Struct for handling bonuses of several types. Can be transferred to any hero
  277. struct DLL_LINKAGE Bonus : public std::enable_shared_from_this<Bonus>
  278. {
  279. enum { EVERY_TYPE = -1 };
  280. enum BonusType
  281. {
  282. #define BONUS_NAME(x) x,
  283. BONUS_LIST
  284. #undef BONUS_NAME
  285. };
  286. enum BonusDuration //when bonus is automatically removed
  287. {
  288. PERMANENT = 1,
  289. ONE_BATTLE = 2, //at the end of battle
  290. ONE_DAY = 4, //at the end of day
  291. ONE_WEEK = 8, //at the end of week (bonus lasts till the end of week, thats NOT 7 days
  292. N_TURNS = 16, //used during battles, after battle bonus is always removed
  293. N_DAYS = 32,
  294. UNTIL_BEING_ATTACKED = 64, /*removed after attack and counterattacks are performed*/
  295. UNTIL_ATTACK = 128, /*removed after attack and counterattacks are performed*/
  296. STACK_GETS_TURN = 256, /*removed when stack gets its turn - used for defensive stance*/
  297. COMMANDER_KILLED = 512
  298. };
  299. enum BonusSource
  300. {
  301. #define BONUS_SOURCE(x) x,
  302. BONUS_SOURCE_LIST
  303. #undef BONUS_SOURCE
  304. NUM_BONUS_SOURCE /*This is a dummy value, which will be always last*/
  305. };
  306. enum LimitEffect
  307. {
  308. NO_LIMIT = 0,
  309. ONLY_DISTANCE_FIGHT=1, ONLY_MELEE_FIGHT, //used to mark bonuses for attack/defense primary skills from spells like Precision (distance only)
  310. };
  311. enum ValueType
  312. {
  313. #define BONUS_VALUE(x) x,
  314. BONUS_VALUE_LIST
  315. #undef BONUS_VALUE
  316. };
  317. ui16 duration = PERMANENT; //uses BonusDuration values
  318. si16 turnsRemain = 0; //used if duration is N_TURNS, N_DAYS or ONE_WEEK
  319. BonusType type = NONE; //uses BonusType values - says to what is this bonus - 1 byte
  320. TBonusSubtype subtype = -1; //-1 if not applicable - 4 bytes
  321. BonusSource source = OTHER; //source type" uses BonusSource values - what gave that bonus
  322. BonusSource targetSourceType;//Bonuses of what origin this amplifies, uses BonusSource values. Needed for PERCENT_TO_TARGET_TYPE.
  323. si32 val = 0;
  324. ui32 sid = 0; //source id: id of object/artifact/spell
  325. ValueType valType = ADDITIVE_VALUE;
  326. std::string stacking; // bonuses with the same stacking value don't stack (e.g. Angel/Archangel morale bonus)
  327. CAddInfo additionalInfo;
  328. LimitEffect effectRange = NO_LIMIT; //if not NO_LIMIT, bonus will be omitted by default
  329. TLimiterPtr limiter;
  330. TPropagatorPtr propagator;
  331. TUpdaterPtr updater;
  332. TUpdaterPtr propagationUpdater;
  333. std::string description;
  334. Bonus(BonusDuration Duration, BonusType Type, BonusSource Src, si32 Val, ui32 ID, std::string Desc, si32 Subtype=-1);
  335. Bonus(BonusDuration Duration, BonusType Type, BonusSource Src, si32 Val, ui32 ID, si32 Subtype=-1, ValueType ValType = ADDITIVE_VALUE);
  336. Bonus() = default;
  337. template <typename Handler> void serialize(Handler &h, const int version)
  338. {
  339. h & duration;
  340. h & type;
  341. h & subtype;
  342. h & source;
  343. h & val;
  344. h & sid;
  345. h & description;
  346. h & additionalInfo;
  347. h & turnsRemain;
  348. h & valType;
  349. h & stacking;
  350. h & effectRange;
  351. h & limiter;
  352. h & propagator;
  353. h & updater;
  354. h & propagationUpdater;
  355. h & targetSourceType;
  356. }
  357. template <typename Ptr>
  358. static bool compareByAdditionalInfo(const Ptr& a, const Ptr& b)
  359. {
  360. return a->additionalInfo < b->additionalInfo;
  361. }
  362. static bool NDays(const Bonus *hb)
  363. {
  364. return hb->duration & Bonus::N_DAYS;
  365. }
  366. static bool NTurns(const Bonus *hb)
  367. {
  368. return hb->duration & Bonus::N_TURNS;
  369. }
  370. static bool OneDay(const Bonus *hb)
  371. {
  372. return hb->duration & Bonus::ONE_DAY;
  373. }
  374. static bool OneWeek(const Bonus *hb)
  375. {
  376. return hb->duration & Bonus::ONE_WEEK;
  377. }
  378. static bool OneBattle(const Bonus *hb)
  379. {
  380. return hb->duration & Bonus::ONE_BATTLE;
  381. }
  382. static bool Permanent(const Bonus *hb)
  383. {
  384. return hb->duration & Bonus::PERMANENT;
  385. }
  386. static bool UntilGetsTurn(const Bonus *hb)
  387. {
  388. return hb->duration & Bonus::STACK_GETS_TURN;
  389. }
  390. static bool UntilAttack(const Bonus *hb)
  391. {
  392. return hb->duration & Bonus::UNTIL_ATTACK;
  393. }
  394. static bool UntilBeingAttacked(const Bonus *hb)
  395. {
  396. return hb->duration & Bonus::UNTIL_BEING_ATTACKED;
  397. }
  398. static bool UntilCommanderKilled(const Bonus *hb)
  399. {
  400. return hb->duration & Bonus::COMMANDER_KILLED;
  401. }
  402. inline bool operator == (const BonusType & cf) const
  403. {
  404. return type == cf;
  405. }
  406. inline void operator += (const ui32 Val) //no return
  407. {
  408. val += Val;
  409. }
  410. STRONG_INLINE static ui32 getSid32(ui32 high, ui32 low)
  411. {
  412. return (high << 16) + low;
  413. }
  414. STRONG_INLINE static ui32 getHighFromSid32(ui32 sid)
  415. {
  416. return sid >> 16;
  417. }
  418. STRONG_INLINE static ui32 getLowFromSid32(ui32 sid)
  419. {
  420. return sid & 0x0000FFFF;
  421. }
  422. std::string Description(std::optional<si32> customValue = {}) const;
  423. JsonNode toJsonNode() const;
  424. std::string nameForBonus() const; // generate suitable name for bonus - e.g. for storing in json struct
  425. std::shared_ptr<Bonus> addLimiter(const TLimiterPtr & Limiter); //returns this for convenient chain-calls
  426. std::shared_ptr<Bonus> addPropagator(const TPropagatorPtr & Propagator); //returns this for convenient chain-calls
  427. std::shared_ptr<Bonus> addUpdater(const TUpdaterPtr & Updater); //returns this for convenient chain-calls
  428. };
  429. DLL_LINKAGE std::ostream & operator<<(std::ostream &out, const Bonus &bonus);
  430. class DLL_LINKAGE BonusList
  431. {
  432. public:
  433. using TInternalContainer = std::vector<std::shared_ptr<Bonus>>;
  434. private:
  435. TInternalContainer bonuses;
  436. bool belongsToTree;
  437. void changed() const;
  438. public:
  439. using const_reference = TInternalContainer::const_reference;
  440. using value_type = TInternalContainer::value_type;
  441. using const_iterator = TInternalContainer::const_iterator;
  442. using iterator = TInternalContainer::iterator;
  443. BonusList(bool BelongsToTree = false);
  444. BonusList(const BonusList &bonusList);
  445. BonusList(BonusList && other) noexcept;
  446. BonusList& operator=(const BonusList &bonusList);
  447. // wrapper functions of the STL vector container
  448. TInternalContainer::size_type size() const { return bonuses.size(); }
  449. void push_back(const std::shared_ptr<Bonus> & x);
  450. TInternalContainer::iterator erase (const int position);
  451. void clear();
  452. bool empty() const { return bonuses.empty(); }
  453. void resize(TInternalContainer::size_type sz, const std::shared_ptr<Bonus> & c = nullptr);
  454. void reserve(TInternalContainer::size_type sz);
  455. TInternalContainer::size_type capacity() const { return bonuses.capacity(); }
  456. STRONG_INLINE std::shared_ptr<Bonus> &operator[] (TInternalContainer::size_type n) { return bonuses[n]; }
  457. STRONG_INLINE const std::shared_ptr<Bonus> &operator[] (TInternalContainer::size_type n) const { return bonuses[n]; }
  458. std::shared_ptr<Bonus> &back() { return bonuses.back(); }
  459. std::shared_ptr<Bonus> &front() { return bonuses.front(); }
  460. const std::shared_ptr<Bonus> &back() const { return bonuses.back(); }
  461. const std::shared_ptr<Bonus> &front() const { return bonuses.front(); }
  462. // There should be no non-const access to provide solid,robust bonus caching
  463. TInternalContainer::const_iterator begin() const { return bonuses.begin(); }
  464. TInternalContainer::const_iterator end() const { return bonuses.end(); }
  465. TInternalContainer::size_type operator-=(const std::shared_ptr<Bonus> & i);
  466. // BonusList functions
  467. void stackBonuses();
  468. int totalValue() const;
  469. void getBonuses(BonusList &out, const CSelector &selector, const CSelector &limit = nullptr) const;
  470. void getAllBonuses(BonusList &out) const;
  471. //special find functions
  472. std::shared_ptr<Bonus> getFirst(const CSelector &select);
  473. std::shared_ptr<const Bonus> getFirst(const CSelector &select) const;
  474. int valOfBonuses(const CSelector &select) const;
  475. // conversion / output
  476. JsonNode toJsonNode() const;
  477. // remove_if implementation for STL vector types
  478. template <class Predicate>
  479. void remove_if(Predicate pred)
  480. {
  481. BonusList newList;
  482. for(const auto & b : bonuses)
  483. {
  484. if (!pred(b.get()))
  485. newList.push_back(b);
  486. }
  487. bonuses.clear();
  488. bonuses.resize(newList.size());
  489. std::copy(newList.begin(), newList.end(), bonuses.begin());
  490. }
  491. template <class InputIterator>
  492. void insert(const int position, InputIterator first, InputIterator last);
  493. void insert(TInternalContainer::iterator position, TInternalContainer::size_type n, const std::shared_ptr<Bonus> & x);
  494. template <typename Handler>
  495. void serialize(Handler &h, const int version)
  496. {
  497. h & static_cast<TInternalContainer&>(bonuses);
  498. }
  499. // C++ for range support
  500. auto begin () -> decltype (bonuses.begin())
  501. {
  502. return bonuses.begin();
  503. }
  504. auto end () -> decltype (bonuses.end())
  505. {
  506. return bonuses.end();
  507. }
  508. };
  509. DLL_LINKAGE std::ostream & operator<<(std::ostream &out, const BonusList &bonusList);
  510. class DLL_LINKAGE IBonusBearer
  511. {
  512. public:
  513. //new bonusing node interface
  514. // * selector is predicate that tests if HeroBonus matches our criteria
  515. // * root is node on which call was made (nullptr will be replaced with this)
  516. //interface
  517. IBonusBearer() = default;
  518. virtual ~IBonusBearer() = default;
  519. virtual TConstBonusListPtr getAllBonuses(const CSelector &selector, const CSelector &limit, const CBonusSystemNode *root = nullptr, const std::string &cachingStr = "") const = 0;
  520. int valOfBonuses(const CSelector &selector, const std::string &cachingStr = "") const;
  521. bool hasBonus(const CSelector &selector, const std::string &cachingStr = "") const;
  522. bool hasBonus(const CSelector &selector, const CSelector &limit, const std::string &cachingStr = "") const;
  523. TConstBonusListPtr getBonuses(const CSelector &selector, const CSelector &limit, const std::string &cachingStr = "") const;
  524. TConstBonusListPtr getBonuses(const CSelector &selector, const std::string &cachingStr = "") const;
  525. std::shared_ptr<const Bonus> getBonus(const CSelector &selector) const; //returns any bonus visible on node that matches (or nullptr if none matches)
  526. //Optimized interface (with auto-caching)
  527. int valOfBonuses(Bonus::BonusType type, int subtype = -1) const; //subtype -> subtype of bonus, if -1 then anyt;
  528. bool hasBonusOfType(Bonus::BonusType type, int subtype = -1) const;//determines if hero has a bonus of given type (and optionally subtype)
  529. bool hasBonusFrom(Bonus::BonusSource source, ui32 sourceID) const;
  530. virtual int64_t getTreeVersion() const = 0;
  531. };
  532. template<typename T>
  533. class CSelectFieldEqual
  534. {
  535. T Bonus::*ptr;
  536. public:
  537. CSelectFieldEqual(T Bonus::*Ptr)
  538. : ptr(Ptr)
  539. {
  540. }
  541. CSelector operator()(const T &valueToCompareAgainst) const
  542. {
  543. auto ptr2 = ptr; //We need a COPY because we don't want to reference this (might be outlived by lambda)
  544. return [ptr2, valueToCompareAgainst](const Bonus *bonus)
  545. {
  546. return bonus->*ptr2 == valueToCompareAgainst;
  547. };
  548. }
  549. };
  550. template<typename T> //can be same, needed for subtype field
  551. class CSelectFieldEqualOrEvery
  552. {
  553. T Bonus::*ptr;
  554. T val;
  555. public:
  556. CSelectFieldEqualOrEvery(T Bonus::*Ptr, const T &Val)
  557. : ptr(Ptr), val(Val)
  558. {
  559. }
  560. bool operator()(const Bonus *bonus) const
  561. {
  562. return (bonus->*ptr == val) || (bonus->*ptr == static_cast<T>(Bonus::EVERY_TYPE));
  563. }
  564. CSelectFieldEqualOrEvery& operator()(const T &setVal)
  565. {
  566. val = setVal;
  567. return *this;
  568. }
  569. };
  570. class DLL_LINKAGE CWillLastTurns
  571. {
  572. public:
  573. int turnsRequested;
  574. bool operator()(const Bonus *bonus) const
  575. {
  576. return turnsRequested <= 0 //every present effect will last zero (or "less") turns
  577. || !Bonus::NTurns(bonus) //so do every not expriing after N-turns effect
  578. || bonus->turnsRemain > turnsRequested;
  579. }
  580. CWillLastTurns& operator()(const int &setVal)
  581. {
  582. turnsRequested = setVal;
  583. return *this;
  584. }
  585. };
  586. class DLL_LINKAGE CWillLastDays
  587. {
  588. public:
  589. int daysRequested;
  590. bool operator()(const Bonus *bonus) const
  591. {
  592. if(daysRequested <= 0 || Bonus::Permanent(bonus) || Bonus::OneBattle(bonus))
  593. return true;
  594. else if(Bonus::OneDay(bonus))
  595. return false;
  596. else if(Bonus::NDays(bonus) || Bonus::OneWeek(bonus))
  597. {
  598. return bonus->turnsRemain > daysRequested;
  599. }
  600. return false; // TODO: ONE_WEEK need support for turnsRemain, but for now we'll exclude all unhandled durations
  601. }
  602. CWillLastDays& operator()(const int &setVal)
  603. {
  604. daysRequested = setVal;
  605. return *this;
  606. }
  607. };
  608. namespace Selector
  609. {
  610. extern DLL_LINKAGE CSelectFieldEqual<Bonus::BonusType> & type();
  611. extern DLL_LINKAGE CSelectFieldEqual<TBonusSubtype> & subtype();
  612. extern DLL_LINKAGE CSelectFieldEqual<CAddInfo> & info();
  613. extern DLL_LINKAGE CSelectFieldEqual<Bonus::BonusSource> & sourceType();
  614. extern DLL_LINKAGE CSelectFieldEqual<Bonus::BonusSource> & targetSourceType();
  615. extern DLL_LINKAGE CSelectFieldEqual<Bonus::LimitEffect> & effectRange();
  616. extern DLL_LINKAGE CWillLastTurns turns;
  617. extern DLL_LINKAGE CWillLastDays days;
  618. CSelector DLL_LINKAGE typeSubtype(Bonus::BonusType Type, TBonusSubtype Subtype);
  619. CSelector DLL_LINKAGE typeSubtypeInfo(Bonus::BonusType type, TBonusSubtype subtype, const CAddInfo & info);
  620. CSelector DLL_LINKAGE source(Bonus::BonusSource source, ui32 sourceID);
  621. CSelector DLL_LINKAGE sourceTypeSel(Bonus::BonusSource source);
  622. CSelector DLL_LINKAGE valueType(Bonus::ValueType valType);
  623. /**
  624. * Selects all bonuses
  625. * Usage example: Selector::all.And(<functor>).And(<functor>)...)
  626. */
  627. extern DLL_LINKAGE CSelector all;
  628. /**
  629. * Selects nothing
  630. * Usage example: Selector::none.Or(<functor>).Or(<functor>)...)
  631. */
  632. extern DLL_LINKAGE CSelector none;
  633. }
  634. extern DLL_LINKAGE const std::map<std::string, Bonus::BonusType> bonusNameMap;
  635. extern DLL_LINKAGE const std::map<std::string, Bonus::ValueType> bonusValueMap;
  636. extern DLL_LINKAGE const std::map<std::string, Bonus::BonusSource> bonusSourceMap;
  637. extern DLL_LINKAGE const std::map<std::string, ui16> bonusDurationMap;
  638. extern DLL_LINKAGE const std::map<std::string, Bonus::LimitEffect> bonusLimitEffect;
  639. extern DLL_LINKAGE const std::set<std::string> deprecatedBonusSet;
  640. // BonusList template that requires full interface of CBonusSystemNode
  641. template <class InputIterator>
  642. void BonusList::insert(const int position, InputIterator first, InputIterator last)
  643. {
  644. bonuses.insert(bonuses.begin() + position, first, last);
  645. changed();
  646. }
  647. VCMI_LIB_NAMESPACE_END