Bonus.h 5.4 KB

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