Bonus.h 5.5 KB

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