Bonus.h 6.0 KB

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