Bonus.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*
  2. * Bonus.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 "BonusEnum.h"
  12. #include "BonusCustomTypes.h"
  13. #include "../constants/VariantIdentifier.h"
  14. #include "../constants/EntityIdentifiers.h"
  15. #include "../MetaString.h"
  16. VCMI_LIB_NAMESPACE_BEGIN
  17. struct Bonus;
  18. class IBonusBearer;
  19. class CBonusSystemNode;
  20. class ILimiter;
  21. class IPropagator;
  22. class IUpdater;
  23. class BonusList;
  24. class CSelector;
  25. using BonusSubtypeID = VariantIdentifier<BonusCustomSubtype, SpellID, CreatureID, PrimarySkill, TerrainId, GameResID, SpellSchool>;
  26. using BonusSourceID = VariantIdentifier<BonusCustomSource, SpellID, CreatureID, ArtifactID, CampaignScenarioID, SecondarySkill, HeroTypeID, Obj, ObjectInstanceID, BuildingTypeUniqueID, BattleField>;
  27. using TBonusListPtr = std::shared_ptr<BonusList>;
  28. using TConstBonusListPtr = std::shared_ptr<const BonusList>;
  29. using TLimiterPtr = std::shared_ptr<ILimiter>;
  30. using TPropagatorPtr = std::shared_ptr<IPropagator>;
  31. using TUpdaterPtr = std::shared_ptr<IUpdater>;
  32. class DLL_LINKAGE CAddInfo : public std::vector<si32>
  33. {
  34. public:
  35. enum { NONE = -1 };
  36. CAddInfo();
  37. CAddInfo(si32 value);
  38. bool operator==(si32 value) const;
  39. bool operator!=(si32 value) const;
  40. si32 & operator[](size_type pos);
  41. si32 operator[](size_type pos) const;
  42. std::string toString() const;
  43. JsonNode toJsonNode() const;
  44. };
  45. #define BONUS_TREE_DESERIALIZATION_FIX if(!h.saving && h.smartPointerSerialization) deserializationFix();
  46. /// Struct for handling bonuses of several types. Can be transferred to any hero
  47. struct DLL_LINKAGE Bonus : public std::enable_shared_from_this<Bonus>
  48. {
  49. BonusDuration::Type duration = BonusDuration::PERMANENT; //uses BonusDuration values
  50. si16 turnsRemain = 0; //used if duration is N_TURNS, N_DAYS or ONE_WEEK
  51. BonusType type = BonusType::NONE; //uses BonusType values - says to what is this bonus - 1 byte
  52. BonusSubtypeID subtype;
  53. BonusSource source = BonusSource::OTHER; //source type" uses BonusSource values - what gave that bonus
  54. BonusSource targetSourceType;//Bonuses of what origin this amplifies, uses BonusSource values. Needed for PERCENT_TO_TARGET_TYPE.
  55. si32 val = 0;
  56. BonusSourceID sid; //source id: id of object/artifact/spell
  57. BonusValueType valType = BonusValueType::ADDITIVE_VALUE;
  58. std::string stacking; // bonuses with the same stacking value don't stack (e.g. Angel/Archangel morale bonus)
  59. CAddInfo additionalInfo;
  60. BonusLimitEffect effectRange = BonusLimitEffect::NO_LIMIT;
  61. TLimiterPtr limiter;
  62. TPropagatorPtr propagator;
  63. TUpdaterPtr updater;
  64. TUpdaterPtr propagationUpdater;
  65. MetaString description;
  66. Bonus(BonusDuration::Type Duration, BonusType Type, BonusSource Src, si32 Val, BonusSourceID sourceID);
  67. Bonus(BonusDuration::Type Duration, BonusType Type, BonusSource Src, si32 Val, BonusSourceID sourceID, BonusSubtypeID subtype);
  68. Bonus(BonusDuration::Type Duration, BonusType Type, BonusSource Src, si32 Val, BonusSourceID sourceID, BonusSubtypeID subtype, BonusValueType ValType);
  69. Bonus() = default;
  70. template <typename Handler> void serialize(Handler &h)
  71. {
  72. h & duration;
  73. h & type;
  74. h & subtype;
  75. h & source;
  76. h & val;
  77. h & sid;
  78. if (h.version < Handler::Version::BONUS_META_STRING)
  79. {
  80. std::string oldDescription;
  81. h & oldDescription;
  82. description = MetaString::createFromRawString(oldDescription);
  83. }
  84. else
  85. h & description;
  86. h & additionalInfo;
  87. h & turnsRemain;
  88. h & valType;
  89. h & stacking;
  90. h & effectRange;
  91. h & limiter;
  92. h & propagator;
  93. h & updater;
  94. h & propagationUpdater;
  95. h & targetSourceType;
  96. if (h.version < Handler::Version::MANA_LIMIT && type == BonusType::MANA_PER_KNOWLEDGE_PERCENTAGE)
  97. {
  98. if (valType == BonusValueType::ADDITIVE_VALUE || valType == BonusValueType::BASE_NUMBER)
  99. val *= 100;
  100. }
  101. }
  102. template <typename Ptr>
  103. static bool compareByAdditionalInfo(const Ptr& a, const Ptr& b)
  104. {
  105. return a->additionalInfo < b->additionalInfo;
  106. }
  107. static bool NDays(const Bonus *hb)
  108. {
  109. auto set = hb->duration & BonusDuration::N_DAYS;
  110. return set.any();
  111. }
  112. static bool NTurns(const Bonus *hb)
  113. {
  114. auto set = hb->duration & BonusDuration::N_TURNS;
  115. return set.any();
  116. }
  117. static bool OneDay(const Bonus *hb)
  118. {
  119. auto set = hb->duration & BonusDuration::ONE_DAY;
  120. return set.any();
  121. }
  122. static bool OneWeek(const Bonus *hb)
  123. {
  124. auto set = hb->duration & BonusDuration::ONE_WEEK;
  125. return set.any();
  126. }
  127. static bool OneBattle(const Bonus *hb)
  128. {
  129. auto set = hb->duration & BonusDuration::ONE_BATTLE;
  130. return set.any();
  131. }
  132. static bool Permanent(const Bonus *hb)
  133. {
  134. auto set = hb->duration & BonusDuration::PERMANENT;
  135. return set.any();
  136. }
  137. static bool UntilGetsTurn(const Bonus *hb)
  138. {
  139. auto set = hb->duration & BonusDuration::STACK_GETS_TURN;
  140. return set.any();
  141. }
  142. static bool UntilAttack(const Bonus *hb)
  143. {
  144. auto set = hb->duration & BonusDuration::UNTIL_ATTACK;
  145. return set.any();
  146. }
  147. static bool UntilBeingAttacked(const Bonus *hb)
  148. {
  149. auto set = hb->duration & BonusDuration::UNTIL_BEING_ATTACKED;
  150. return set.any();
  151. }
  152. static bool UntilCommanderKilled(const Bonus *hb)
  153. {
  154. auto set = hb->duration & BonusDuration::COMMANDER_KILLED;
  155. return set.any();
  156. }
  157. static bool UntilOwnAttack(const Bonus *hb)
  158. {
  159. auto set = hb->duration & BonusDuration::UNTIL_OWN_ATTACK;
  160. return set.any();
  161. }
  162. inline bool operator == (const BonusType & cf) const
  163. {
  164. return type == cf;
  165. }
  166. inline void operator += (const ui32 Val) //no return
  167. {
  168. val += Val;
  169. }
  170. std::string Description(std::optional<si32> customValue = {}) const;
  171. JsonNode toJsonNode() const;
  172. std::shared_ptr<Bonus> addLimiter(const TLimiterPtr & Limiter); //returns this for convenient chain-calls
  173. std::shared_ptr<Bonus> addPropagator(const TPropagatorPtr & Propagator); //returns this for convenient chain-calls
  174. std::shared_ptr<Bonus> addUpdater(const TUpdaterPtr & Updater); //returns this for convenient chain-calls
  175. };
  176. DLL_LINKAGE std::ostream & operator<<(std::ostream &out, const Bonus &bonus);
  177. VCMI_LIB_NAMESPACE_END