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