Bonus.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. #include "../filesystem/ResourcePath.h"
  18. VCMI_LIB_NAMESPACE_BEGIN
  19. struct Bonus;
  20. class IBonusBearer;
  21. class CBonusSystemNode;
  22. class ILimiter;
  23. class IPropagator;
  24. class IUpdater;
  25. class BonusList;
  26. class CSelector;
  27. class IGameInfoCallback;
  28. using BonusSubtypeID = VariantIdentifier<BonusCustomSubtype, SpellID, CreatureID, PrimarySkill, TerrainId, GameResID, SpellSchool>;
  29. using BonusSourceID = VariantIdentifier<BonusCustomSource, SpellID, CreatureID, ArtifactID, CampaignScenarioID, SecondarySkill, HeroTypeID, Obj, ObjectInstanceID, BuildingTypeUniqueID, BattleField>;
  30. using TBonusListPtr = std::shared_ptr<BonusList>;
  31. using TConstBonusListPtr = std::shared_ptr<const BonusList>;
  32. using TLimiterPtr = std::shared_ptr<ILimiter>;
  33. using TPropagatorPtr = std::shared_ptr<IPropagator>;
  34. using TUpdaterPtr = std::shared_ptr<IUpdater>;
  35. class DLL_LINKAGE CAddInfo : public std::vector<si32>
  36. {
  37. public:
  38. enum { NONE = -1 };
  39. CAddInfo();
  40. CAddInfo(si32 value);
  41. bool operator==(si32 value) const;
  42. bool operator!=(si32 value) const;
  43. si32 & operator[](size_type pos);
  44. si32 operator[](size_type pos) const;
  45. std::string toString() const;
  46. JsonNode toJsonNode() const;
  47. };
  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. ImagePath customIconPath;
  69. MetaString description;
  70. Bonus(BonusDuration::Type Duration, BonusType Type, BonusSource Src, si32 Val, BonusSourceID sourceID);
  71. Bonus(BonusDuration::Type Duration, BonusType Type, BonusSource Src, si32 Val, BonusSourceID sourceID, BonusSubtypeID subtype);
  72. Bonus(BonusDuration::Type Duration, BonusType Type, BonusSource Src, si32 Val, BonusSourceID sourceID, BonusSubtypeID subtype, BonusValueType ValType);
  73. Bonus() = default;
  74. template <typename Handler> void serialize(Handler &h)
  75. {
  76. h & duration;
  77. h & type;
  78. h & subtype;
  79. h & source;
  80. h & val;
  81. h & sid;
  82. h & description;
  83. if (h.hasFeature(Handler::Version::CUSTOM_BONUS_ICONS))
  84. h & customIconPath;
  85. h & additionalInfo;
  86. h & turnsRemain;
  87. h & valType;
  88. h & stacking;
  89. h & effectRange;
  90. h & limiter;
  91. h & propagator;
  92. h & updater;
  93. h & propagationUpdater;
  94. h & targetSourceType;
  95. }
  96. template <typename Ptr>
  97. static bool compareByAdditionalInfo(const Ptr& a, const Ptr& b)
  98. {
  99. return a->additionalInfo < b->additionalInfo;
  100. }
  101. static bool NDays(const Bonus *hb)
  102. {
  103. auto set = hb->duration & BonusDuration::N_DAYS;
  104. return set != 0;
  105. }
  106. static bool NTurns(const Bonus *hb)
  107. {
  108. auto set = hb->duration & BonusDuration::N_TURNS;
  109. return set != 0;
  110. }
  111. static bool OneDay(const Bonus *hb)
  112. {
  113. auto set = hb->duration & BonusDuration::ONE_DAY;
  114. return set != 0;
  115. }
  116. static bool OneWeek(const Bonus *hb)
  117. {
  118. auto set = hb->duration & BonusDuration::ONE_WEEK;
  119. return set != 0;
  120. }
  121. static bool OneBattle(const Bonus *hb)
  122. {
  123. auto set = hb->duration & BonusDuration::ONE_BATTLE;
  124. return set != 0;
  125. }
  126. static bool Permanent(const Bonus *hb)
  127. {
  128. auto set = hb->duration & BonusDuration::PERMANENT;
  129. return set != 0;
  130. }
  131. static bool UntilGetsTurn(const Bonus *hb)
  132. {
  133. auto set = hb->duration & BonusDuration::STACK_GETS_TURN;
  134. return set != 0;
  135. }
  136. static bool UntilAttack(const Bonus *hb)
  137. {
  138. auto set = hb->duration & BonusDuration::UNTIL_ATTACK;
  139. return set != 0;
  140. }
  141. static bool UntilBeingAttacked(const Bonus *hb)
  142. {
  143. auto set = hb->duration & BonusDuration::UNTIL_BEING_ATTACKED;
  144. return set != 0;
  145. }
  146. static bool UntilCommanderKilled(const Bonus *hb)
  147. {
  148. auto set = hb->duration & BonusDuration::COMMANDER_KILLED;
  149. return set != 0;
  150. }
  151. static bool UntilOwnAttack(const Bonus *hb)
  152. {
  153. auto set = hb->duration & BonusDuration::UNTIL_OWN_ATTACK;
  154. return set != 0;
  155. }
  156. inline bool operator == (const BonusType & cf) const
  157. {
  158. return type == cf;
  159. }
  160. inline void operator += (const ui32 Val) //no return
  161. {
  162. val += Val;
  163. }
  164. std::string Description(const IGameInfoCallback * cb, std::optional<si32> customValue = {}) const;
  165. JsonNode toJsonNode() const;
  166. std::shared_ptr<Bonus> addLimiter(const TLimiterPtr & Limiter); //returns this for convenient chain-calls
  167. std::shared_ptr<Bonus> addPropagator(const TPropagatorPtr & Propagator); //returns this for convenient chain-calls
  168. std::shared_ptr<Bonus> addUpdater(const TUpdaterPtr & Updater); //returns this for convenient chain-calls
  169. };
  170. DLL_LINKAGE std::ostream & operator<<(std::ostream &out, const Bonus &bonus);
  171. VCMI_LIB_NAMESPACE_END