CSkillHandler.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * CSkillHandler.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 <vcmi/Skill.h>
  12. #include <vcmi/SkillService.h>
  13. #include "../lib/bonuses/HeroBonus.h"
  14. #include "GameConstants.h"
  15. #include "IHandlerBase.h"
  16. VCMI_LIB_NAMESPACE_BEGIN
  17. class JsonSerializeFormat;
  18. class DLL_LINKAGE CSkill : public Skill
  19. {
  20. public:
  21. struct LevelInfo
  22. {
  23. std::string iconSmall;
  24. std::string iconMedium;
  25. std::string iconLarge;
  26. std::vector<std::shared_ptr<Bonus>> effects;
  27. template <typename Handler> void serialize(Handler & h, const int version)
  28. {
  29. h & iconSmall;
  30. h & iconMedium;
  31. h & iconLarge;
  32. h & effects;
  33. }
  34. };
  35. private:
  36. std::vector<LevelInfo> levels; // bonuses provided by basic, advanced and expert level
  37. void addNewBonus(const std::shared_ptr<Bonus> & b, int level);
  38. SecondarySkill id;
  39. std::string modScope;
  40. std::string identifier;
  41. public:
  42. CSkill(const SecondarySkill & id = SecondarySkill::DEFAULT, std::string identifier = "default", bool obligatoryMajor = false, bool obligatoryMinor = false);
  43. ~CSkill() = default;
  44. enum class Obligatory : ui8
  45. {
  46. MAJOR = 0,
  47. MINOR = 1,
  48. };
  49. int32_t getIndex() const override;
  50. int32_t getIconIndex() const override;
  51. std::string getJsonKey() const override;
  52. void registerIcons(const IconRegistar & cb) const override;
  53. SecondarySkill getId() const override;
  54. std::string getNameTextID() const override;
  55. std::string getNameTranslated() const override;
  56. std::string getDescriptionTextID(int level) const override;
  57. std::string getDescriptionTranslated(int level) const override;
  58. const LevelInfo & at(int level) const;
  59. LevelInfo & at(int level);
  60. std::string toString() const;
  61. bool obligatory(Obligatory val) const { return val == Obligatory::MAJOR ? obligatoryMajor : obligatoryMinor; };
  62. std::array<si32, 2> gainChance; // gainChance[0/1] = default gain chance on level-up for might/magic heroes
  63. void updateFrom(const JsonNode & data);
  64. void serializeJson(JsonSerializeFormat & handler);
  65. template <typename Handler> void serialize(Handler & h, const int version)
  66. {
  67. h & id;
  68. h & identifier;
  69. h & gainChance;
  70. h & levels;
  71. h & obligatoryMajor;
  72. h & obligatoryMinor;
  73. }
  74. friend class CSkillHandler;
  75. friend DLL_LINKAGE std::ostream & operator<<(std::ostream & out, const CSkill & skill);
  76. friend DLL_LINKAGE std::ostream & operator<<(std::ostream & out, const CSkill::LevelInfo & info);
  77. private:
  78. bool obligatoryMajor;
  79. bool obligatoryMinor;
  80. };
  81. class DLL_LINKAGE CSkillHandler: public CHandlerBase<SecondarySkill, Skill, CSkill, SkillService>
  82. {
  83. public:
  84. ///IHandler base
  85. std::vector<JsonNode> loadLegacyData() override;
  86. void afterLoadFinalization() override;
  87. void beforeValidate(JsonNode & object) override;
  88. std::vector<bool> getDefaultAllowed() const override;
  89. ///json serialization helpers
  90. static si32 decodeSkill(const std::string & identifier);
  91. static std::string encodeSkill(const si32 index);
  92. static std::string encodeSkillWithType(const si32 index);
  93. template <typename Handler> void serialize(Handler & h, const int version)
  94. {
  95. h & objects;
  96. }
  97. protected:
  98. const std::vector<std::string> & getTypeNames() const override;
  99. CSkill * loadFromJson(const std::string & scope, const JsonNode & json, const std::string & identifier, size_t index) override;
  100. };
  101. VCMI_LIB_NAMESPACE_END