BonusParameters.h 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * BonusParameters.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 "../serializer/Serializeable.h"
  12. #include "Bonus.h"
  13. VCMI_LIB_NAMESPACE_BEGIN
  14. struct BonusParametersOnCombatEvent
  15. {
  16. struct CombatEffectBonus
  17. {
  18. std::shared_ptr<const Bonus> bonus;
  19. bool targetEnemy = false;
  20. template <class H>
  21. void serialize(H& h)
  22. {
  23. h & bonus;
  24. h & targetEnemy;
  25. }
  26. };
  27. struct CombatEffectSpell
  28. {
  29. SpellID spell;
  30. int masteryLevel = 0;
  31. bool targetEnemy = false;
  32. template <class H>
  33. void serialize(H& h)
  34. {
  35. h & spell;
  36. h & masteryLevel;
  37. h & targetEnemy;
  38. }
  39. };
  40. using CombatEffect = std::variant<CombatEffectBonus, CombatEffectSpell>;
  41. std::vector<CombatEffect> effects;
  42. template <class H>
  43. void serialize(H& h)
  44. {
  45. h & effects;
  46. }
  47. };
  48. class DLL_LINKAGE BonusParameters final : public Serializeable
  49. {
  50. public:
  51. using storage_type = std::variant<int32_t, CreatureID, SpellID, std::vector<int32_t>, BonusParametersOnCombatEvent>;
  52. BonusParameters() = default;
  53. template<typename DataType>
  54. BonusParameters(const DataType & value)
  55. :data_(value)
  56. {}
  57. std::string toString() const;
  58. JsonNode toJsonNode() const;
  59. CreatureID toCreature() const
  60. {
  61. return toCustom<CreatureID>();
  62. }
  63. SpellID toSpell() const
  64. {
  65. return toCustom<SpellID>();
  66. }
  67. int32_t toNumber() const
  68. {
  69. return toCustom<int32_t>();
  70. }
  71. const std::vector<int32_t> & toVector() const
  72. {
  73. return toCustom<std::vector<int32_t>>();
  74. }
  75. template<typename CustomType>
  76. const CustomType & toCustom() const
  77. {
  78. auto * result = std::get_if<CustomType>(&data_);
  79. if (result)
  80. return *result;
  81. throw std::runtime_error("Invalid addInfo type access!");
  82. }
  83. template<typename CustomType>
  84. CustomType & toCustom()
  85. {
  86. auto * result = std::get_if<CustomType>(&data_);
  87. if (result)
  88. return *result;
  89. throw std::runtime_error("Invalid addInfo type access!");
  90. }
  91. template <class H>
  92. void serialize(H& h)
  93. {
  94. h & data_;
  95. }
  96. private:
  97. storage_type data_;
  98. };
  99. VCMI_LIB_NAMESPACE_END