Bonus.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 "Limiters.h"
  14. #include "../serializer/Serializeable.h"
  15. #include "../texts/MetaString.h"
  16. #include "../filesystem/ResourcePath.h"
  17. VCMI_LIB_NAMESPACE_BEGIN
  18. class IBonusBearer;
  19. class IPropagator;
  20. class IUpdater;
  21. class CSelector;
  22. class IGameInfoCallback;
  23. using TBonusListPtr = std::shared_ptr<BonusList>;
  24. using TConstBonusListPtr = std::shared_ptr<const BonusList>;
  25. using TPropagatorPtr = std::shared_ptr<const IPropagator>;
  26. using TUpdaterPtr = std::shared_ptr<const 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. /// 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>, public Serializeable
  42. {
  43. BonusDuration::Type duration = BonusDuration::PERMANENT; //uses BonusDuration values - 2 bytes
  44. si32 val = 0;
  45. si16 turnsRemain = 0; //used if duration is N_TURNS, N_DAYS or ONE_WEEK
  46. BonusValueType valType = BonusValueType::ADDITIVE_VALUE; // 1 byte
  47. BonusSource source = BonusSource::OTHER; //source type" uses BonusSource values - what gave that bonus - 1 byte
  48. BonusSource targetSourceType = BonusSource::OTHER;//Bonuses of what origin this amplifies, uses BonusSource values. Needed for PERCENT_TO_TARGET_TYPE. - 1 byte
  49. BonusLimitEffect effectRange = BonusLimitEffect::NO_LIMIT; // 1 byte
  50. BonusType type = BonusType::NONE; //uses BonusType values - says to what is this bonus - 2 bytes
  51. BonusSubtypeID subtype;
  52. BonusSourceID sid; //source id: id of object/artifact/spell
  53. std::string stacking; // bonuses with the same stacking value don't stack (e.g. Angel/Archangel morale bonus)
  54. CAddInfo additionalInfo;
  55. TLimiterPtr limiter;
  56. TPropagatorPtr propagator;
  57. TUpdaterPtr updater;
  58. TUpdaterPtr propagationUpdater;
  59. ImagePath customIconPath;
  60. MetaString description;
  61. PlayerColor bonusOwner = PlayerColor::CANNOT_DETERMINE;
  62. Bonus(BonusDuration::Type Duration, BonusType Type, BonusSource Src, si32 Val, BonusSourceID sourceID);
  63. Bonus(BonusDuration::Type Duration, BonusType Type, BonusSource Src, si32 Val, BonusSourceID sourceID, BonusSubtypeID subtype);
  64. Bonus(BonusDuration::Type Duration, BonusType Type, BonusSource Src, si32 Val, BonusSourceID sourceID, BonusSubtypeID subtype, BonusValueType ValType);
  65. Bonus(const Bonus & inst, const BonusSourceID & sourceId);
  66. Bonus() = default;
  67. template <typename Handler> void serialize(Handler &h)
  68. {
  69. h & duration;
  70. h & type;
  71. h & subtype;
  72. h & source;
  73. h & val;
  74. h & sid;
  75. h & description;
  76. if (h.hasFeature(Handler::Version::CUSTOM_BONUS_ICONS))
  77. h & customIconPath;
  78. h & additionalInfo;
  79. h & turnsRemain;
  80. h & valType;
  81. h & stacking;
  82. h & effectRange;
  83. h & limiter;
  84. h & propagator;
  85. h & updater;
  86. h & propagationUpdater;
  87. h & targetSourceType;
  88. }
  89. template <typename Ptr>
  90. static bool compareByAdditionalInfo(const Ptr& a, const Ptr& b)
  91. {
  92. return a->additionalInfo < b->additionalInfo;
  93. }
  94. static bool NDays(const Bonus *hb)
  95. {
  96. auto set = hb->duration & BonusDuration::N_DAYS;
  97. return set != 0;
  98. }
  99. static bool NTurns(const Bonus *hb)
  100. {
  101. auto set = hb->duration & BonusDuration::N_TURNS;
  102. return set != 0;
  103. }
  104. static bool OneDay(const Bonus *hb)
  105. {
  106. auto set = hb->duration & BonusDuration::ONE_DAY;
  107. return set != 0;
  108. }
  109. static bool OneWeek(const Bonus *hb)
  110. {
  111. auto set = hb->duration & BonusDuration::ONE_WEEK;
  112. return set != 0;
  113. }
  114. static bool OneBattle(const Bonus *hb)
  115. {
  116. auto set = hb->duration & BonusDuration::ONE_BATTLE;
  117. return set != 0;
  118. }
  119. static bool Permanent(const Bonus *hb)
  120. {
  121. auto set = hb->duration & BonusDuration::PERMANENT;
  122. return set != 0;
  123. }
  124. static bool UntilGetsTurn(const Bonus *hb)
  125. {
  126. auto set = hb->duration & BonusDuration::STACK_GETS_TURN;
  127. return set != 0;
  128. }
  129. static bool UntilAttack(const Bonus *hb)
  130. {
  131. auto set = hb->duration & BonusDuration::UNTIL_ATTACK;
  132. return set != 0;
  133. }
  134. static bool UntilBeingAttacked(const Bonus *hb)
  135. {
  136. auto set = hb->duration & BonusDuration::UNTIL_BEING_ATTACKED;
  137. return set != 0;
  138. }
  139. static bool UntilCommanderKilled(const Bonus *hb)
  140. {
  141. auto set = hb->duration & BonusDuration::COMMANDER_KILLED;
  142. return set != 0;
  143. }
  144. static bool UntilOwnAttack(const Bonus *hb)
  145. {
  146. auto set = hb->duration & BonusDuration::UNTIL_OWN_ATTACK;
  147. return set != 0;
  148. }
  149. inline bool operator == (const BonusType & cf) const
  150. {
  151. return type == cf;
  152. }
  153. inline void operator += (const ui32 Val) //no return
  154. {
  155. val += Val;
  156. }
  157. std::string Description(const IGameInfoCallback * cb, std::optional<si32> customValue = {}) const;
  158. JsonNode toJsonNode() const;
  159. std::shared_ptr<Bonus> addLimiter(const TLimiterPtr & Limiter); //returns this for convenient chain-calls
  160. std::shared_ptr<Bonus> addPropagator(const TPropagatorPtr & Propagator); //returns this for convenient chain-calls
  161. std::shared_ptr<Bonus> addUpdater(const TUpdaterPtr & Updater); //returns this for convenient chain-calls
  162. };
  163. DLL_LINKAGE std::ostream & operator<<(std::ostream &out, const Bonus &bonus);
  164. VCMI_LIB_NAMESPACE_END