CSkillHandler.h 3.2 KB

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