Bonus.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 "../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 TBonusSubtype = int32_t;
  22. using TBonusListPtr = std::shared_ptr<BonusList>;
  23. using TConstBonusListPtr = std::shared_ptr<const BonusList>;
  24. using TLimiterPtr = std::shared_ptr<ILimiter>;
  25. using TPropagatorPtr = std::shared_ptr<IPropagator>;
  26. using TUpdaterPtr = std::shared_ptr<IUpdater>;
  27. class DLL_LINKAGE CAddInfo : public std::vector<si32>
  28. {
  29. public:
  30. enum { NONE = -1 };
  31. CAddInfo();
  32. CAddInfo(si32 value);
  33. bool operator==(si32 value) const;
  34. bool operator!=(si32 value) const;
  35. si32 & operator[](size_type pos);
  36. si32 operator[](size_type pos) const;
  37. std::string toString() const;
  38. JsonNode toJsonNode() const;
  39. };
  40. #define BONUS_TREE_DESERIALIZATION_FIX if(!h.saving && h.smartPointerSerialization) deserializationFix();
  41. /// Struct for handling bonuses of several types. Can be transferred to any hero
  42. struct DLL_LINKAGE Bonus : public std::enable_shared_from_this<Bonus>
  43. {
  44. BonusDuration duration = BonusDuration::PERMANENT; //uses BonusDuration values
  45. si16 turnsRemain = 0; //used if duration is N_TURNS, N_DAYS or ONE_WEEK
  46. BonusType type = BonusType::NONE; //uses BonusType values - says to what is this bonus - 1 byte
  47. TBonusSubtype subtype = -1; //-1 if not applicable - 4 bytes
  48. BonusSource source = BonusSource::OTHER; //source type" uses BonusSource values - what gave that bonus
  49. BonusSource targetSourceType;//Bonuses of what origin this amplifies, uses BonusSource values. Needed for PERCENT_TO_TARGET_TYPE.
  50. si32 val = 0;
  51. ui32 sid = 0; //source id: id of object/artifact/spell
  52. BonusValueType valType = BonusValueType::ADDITIVE_VALUE;
  53. std::string stacking; // bonuses with the same stacking value don't stack (e.g. Angel/Archangel morale bonus)
  54. CAddInfo additionalInfo;
  55. BonusLimitEffect effectRange = BonusLimitEffect::NO_LIMIT; //if not NO_LIMIT, bonus will be omitted by default
  56. TLimiterPtr limiter;
  57. TPropagatorPtr propagator;
  58. TUpdaterPtr updater;
  59. TUpdaterPtr propagationUpdater;
  60. std::string description;
  61. Bonus(BonusDuration Duration, BonusType Type, BonusSource Src, si32 Val, ui32 ID, std::string Desc, si32 Subtype=-1);
  62. Bonus(BonusDuration Duration, BonusType Type, BonusSource Src, si32 Val, ui32 ID, si32 Subtype=-1, BonusValueType ValType = BonusValueType::ADDITIVE_VALUE);
  63. Bonus() = default;
  64. template <typename Handler> void serialize(Handler &h, const int version)
  65. {
  66. h & duration;
  67. h & type;
  68. h & subtype;
  69. h & source;
  70. h & val;
  71. h & sid;
  72. h & description;
  73. h & additionalInfo;
  74. h & turnsRemain;
  75. h & valType;
  76. h & stacking;
  77. h & effectRange;
  78. h & limiter;
  79. h & propagator;
  80. h & updater;
  81. h & propagationUpdater;
  82. h & targetSourceType;
  83. }
  84. template <typename Ptr>
  85. static bool compareByAdditionalInfo(const Ptr& a, const Ptr& b)
  86. {
  87. return a->additionalInfo < b->additionalInfo;
  88. }
  89. static bool NDays(const Bonus *hb)
  90. {
  91. return hb->duration == BonusDuration::N_DAYS;
  92. }
  93. static bool NTurns(const Bonus *hb)
  94. {
  95. return hb->duration == BonusDuration::N_TURNS;
  96. }
  97. static bool OneDay(const Bonus *hb)
  98. {
  99. return hb->duration == BonusDuration::ONE_DAY;
  100. }
  101. static bool OneWeek(const Bonus *hb)
  102. {
  103. return hb->duration == BonusDuration::ONE_WEEK;
  104. }
  105. static bool OneBattle(const Bonus *hb)
  106. {
  107. return hb->duration == BonusDuration::ONE_BATTLE;
  108. }
  109. static bool Permanent(const Bonus *hb)
  110. {
  111. return hb->duration == BonusDuration::PERMANENT;
  112. }
  113. static bool UntilGetsTurn(const Bonus *hb)
  114. {
  115. return hb->duration == BonusDuration::STACK_GETS_TURN;
  116. }
  117. static bool UntilAttack(const Bonus *hb)
  118. {
  119. return hb->duration == BonusDuration::UNTIL_ATTACK;
  120. }
  121. static bool UntilBeingAttacked(const Bonus *hb)
  122. {
  123. return hb->duration == BonusDuration::UNTIL_BEING_ATTACKED;
  124. }
  125. static bool UntilCommanderKilled(const Bonus *hb)
  126. {
  127. return hb->duration == BonusDuration::COMMANDER_KILLED;
  128. }
  129. inline bool operator == (const BonusType & cf) const
  130. {
  131. return type == cf;
  132. }
  133. inline void operator += (const ui32 Val) //no return
  134. {
  135. val += Val;
  136. }
  137. STRONG_INLINE static ui32 getSid32(ui32 high, ui32 low)
  138. {
  139. return (high << 16) + low;
  140. }
  141. STRONG_INLINE static ui32 getHighFromSid32(ui32 sid)
  142. {
  143. return sid >> 16;
  144. }
  145. STRONG_INLINE static ui32 getLowFromSid32(ui32 sid)
  146. {
  147. return sid & 0x0000FFFF;
  148. }
  149. std::string Description(std::optional<si32> customValue = {}) const;
  150. JsonNode toJsonNode() const;
  151. std::shared_ptr<Bonus> addLimiter(const TLimiterPtr & Limiter); //returns this for convenient chain-calls
  152. std::shared_ptr<Bonus> addPropagator(const TPropagatorPtr & Propagator); //returns this for convenient chain-calls
  153. std::shared_ptr<Bonus> addUpdater(const TUpdaterPtr & Updater); //returns this for convenient chain-calls
  154. };
  155. DLL_LINKAGE std::ostream & operator<<(std::ostream &out, const Bonus &bonus);
  156. extern DLL_LINKAGE const std::set<std::string> deprecatedBonusSet;
  157. VCMI_LIB_NAMESPACE_END