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