Bonus.h 5.9 KB

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