Bonus.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. VCMI_LIB_NAMESPACE_BEGIN
  13. struct Bonus;
  14. class IBonusBearer;
  15. class CBonusSystemNode;
  16. class ILimiter;
  17. class IPropagator;
  18. class IUpdater;
  19. class BonusList;
  20. class CSelector;
  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::Type 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::Type Duration, BonusType Type, BonusSource Src, si32 Val, ui32 ID, std::string Desc, si32 Subtype=-1);
  62. Bonus(BonusDuration::Type 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. auto set = hb->duration & BonusDuration::N_DAYS;
  92. return set.any();
  93. }
  94. static bool NTurns(const Bonus *hb)
  95. {
  96. auto set = hb->duration & BonusDuration::N_TURNS;
  97. return set.any();
  98. }
  99. static bool OneDay(const Bonus *hb)
  100. {
  101. auto set = hb->duration & BonusDuration::ONE_DAY;
  102. return set.any();
  103. }
  104. static bool OneWeek(const Bonus *hb)
  105. {
  106. auto set = hb->duration & BonusDuration::ONE_WEEK;
  107. return set.any();
  108. }
  109. static bool OneBattle(const Bonus *hb)
  110. {
  111. auto set = hb->duration & BonusDuration::ONE_BATTLE;
  112. return set.any();
  113. }
  114. static bool Permanent(const Bonus *hb)
  115. {
  116. auto set = hb->duration & BonusDuration::PERMANENT;
  117. return set.any();
  118. }
  119. static bool UntilGetsTurn(const Bonus *hb)
  120. {
  121. auto set = hb->duration & BonusDuration::STACK_GETS_TURN;
  122. return set.any();
  123. }
  124. static bool UntilAttack(const Bonus *hb)
  125. {
  126. auto set = hb->duration & BonusDuration::UNTIL_ATTACK;
  127. return set.any();
  128. }
  129. static bool UntilBeingAttacked(const Bonus *hb)
  130. {
  131. auto set = hb->duration & BonusDuration::UNTIL_BEING_ATTACKED;
  132. return set.any();
  133. }
  134. static bool UntilCommanderKilled(const Bonus *hb)
  135. {
  136. auto set = hb->duration & BonusDuration::COMMANDER_KILLED;
  137. return set.any();
  138. }
  139. inline bool operator == (const BonusType & cf) const
  140. {
  141. return type == cf;
  142. }
  143. inline void operator += (const ui32 Val) //no return
  144. {
  145. val += Val;
  146. }
  147. STRONG_INLINE static ui32 getSid32(ui32 high, ui32 low)
  148. {
  149. return (high << 16) + low;
  150. }
  151. STRONG_INLINE static ui32 getHighFromSid32(ui32 sid)
  152. {
  153. return sid >> 16;
  154. }
  155. STRONG_INLINE static ui32 getLowFromSid32(ui32 sid)
  156. {
  157. return sid & 0x0000FFFF;
  158. }
  159. std::string Description(std::optional<si32> customValue = {}) const;
  160. JsonNode toJsonNode() const;
  161. std::shared_ptr<Bonus> addLimiter(const TLimiterPtr & Limiter); //returns this for convenient chain-calls
  162. std::shared_ptr<Bonus> addPropagator(const TPropagatorPtr & Propagator); //returns this for convenient chain-calls
  163. std::shared_ptr<Bonus> addUpdater(const TUpdaterPtr & Updater); //returns this for convenient chain-calls
  164. };
  165. DLL_LINKAGE std::ostream & operator<<(std::ostream &out, const Bonus &bonus);
  166. VCMI_LIB_NAMESPACE_END