Bonus.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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.smartPointerSerialization) 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. if (h.version < Handler::Version::BONUS_META_STRING)
  81. {
  82. std::string oldDescription;
  83. h & oldDescription;
  84. description = MetaString::createFromRawString(oldDescription);
  85. }
  86. else
  87. h & description;
  88. h & additionalInfo;
  89. h & turnsRemain;
  90. h & valType;
  91. h & stacking;
  92. h & effectRange;
  93. h & limiter;
  94. h & propagator;
  95. h & updater;
  96. h & propagationUpdater;
  97. h & targetSourceType;
  98. if (h.version < Handler::Version::MANA_LIMIT && type == BonusType::MANA_PER_KNOWLEDGE_PERCENTAGE)
  99. {
  100. if (valType == BonusValueType::ADDITIVE_VALUE || valType == BonusValueType::BASE_NUMBER)
  101. val *= 100;
  102. }
  103. }
  104. template <typename Ptr>
  105. static bool compareByAdditionalInfo(const Ptr& a, const Ptr& b)
  106. {
  107. return a->additionalInfo < b->additionalInfo;
  108. }
  109. static bool NDays(const Bonus *hb)
  110. {
  111. auto set = hb->duration & BonusDuration::N_DAYS;
  112. return set != 0;
  113. }
  114. static bool NTurns(const Bonus *hb)
  115. {
  116. auto set = hb->duration & BonusDuration::N_TURNS;
  117. return set != 0;
  118. }
  119. static bool OneDay(const Bonus *hb)
  120. {
  121. auto set = hb->duration & BonusDuration::ONE_DAY;
  122. return set != 0;
  123. }
  124. static bool OneWeek(const Bonus *hb)
  125. {
  126. auto set = hb->duration & BonusDuration::ONE_WEEK;
  127. return set != 0;
  128. }
  129. static bool OneBattle(const Bonus *hb)
  130. {
  131. auto set = hb->duration & BonusDuration::ONE_BATTLE;
  132. return set != 0;
  133. }
  134. static bool Permanent(const Bonus *hb)
  135. {
  136. auto set = hb->duration & BonusDuration::PERMANENT;
  137. return set != 0;
  138. }
  139. static bool UntilGetsTurn(const Bonus *hb)
  140. {
  141. auto set = hb->duration & BonusDuration::STACK_GETS_TURN;
  142. return set != 0;
  143. }
  144. static bool UntilAttack(const Bonus *hb)
  145. {
  146. auto set = hb->duration & BonusDuration::UNTIL_ATTACK;
  147. return set != 0;
  148. }
  149. static bool UntilBeingAttacked(const Bonus *hb)
  150. {
  151. auto set = hb->duration & BonusDuration::UNTIL_BEING_ATTACKED;
  152. return set != 0;
  153. }
  154. static bool UntilCommanderKilled(const Bonus *hb)
  155. {
  156. auto set = hb->duration & BonusDuration::COMMANDER_KILLED;
  157. return set != 0;
  158. }
  159. static bool UntilOwnAttack(const Bonus *hb)
  160. {
  161. auto set = hb->duration & BonusDuration::UNTIL_OWN_ATTACK;
  162. return set != 0;
  163. }
  164. inline bool operator == (const BonusType & cf) const
  165. {
  166. return type == cf;
  167. }
  168. inline void operator += (const ui32 Val) //no return
  169. {
  170. val += Val;
  171. }
  172. std::string Description(std::optional<si32> customValue = {}) const;
  173. JsonNode toJsonNode() const;
  174. std::shared_ptr<Bonus> addLimiter(const TLimiterPtr & Limiter); //returns this for convenient chain-calls
  175. std::shared_ptr<Bonus> addPropagator(const TPropagatorPtr & Propagator); //returns this for convenient chain-calls
  176. std::shared_ptr<Bonus> addUpdater(const TUpdaterPtr & Updater); //returns this for convenient chain-calls
  177. };
  178. DLL_LINKAGE std::ostream & operator<<(std::ostream &out, const Bonus &bonus);
  179. VCMI_LIB_NAMESPACE_END