CSkillHandler.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. class JsonSerializeFormat;
  17. class DLL_LINKAGE CSkill : public Skill
  18. {
  19. public:
  20. struct LevelInfo
  21. {
  22. std::string description; //descriptions of spell for skill level
  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 & description;
  32. if(version >= 785)
  33. {
  34. h & iconSmall;
  35. h & iconMedium;
  36. h & iconLarge;
  37. }
  38. h & effects;
  39. }
  40. };
  41. private:
  42. std::vector<LevelInfo> levels; // bonuses provided by basic, advanced and expert level
  43. void addNewBonus(const std::shared_ptr<Bonus> & b, int level);
  44. public:
  45. CSkill(SecondarySkill id = SecondarySkill::DEFAULT, std::string identifier = "default");
  46. ~CSkill();
  47. int32_t getIndex() const override;
  48. int32_t getIconIndex() const override;
  49. const std::string & getName() const override;
  50. const std::string & getJsonKey() const override;
  51. void registerIcons(const IconRegistar & cb) const override;
  52. SecondarySkill getId() const override;
  53. const LevelInfo & at(int level) const;
  54. LevelInfo & at(int level);
  55. std::string toString() const;
  56. SecondarySkill id;
  57. std::string identifier;
  58. std::string name; //as displayed in GUI
  59. std::array<si32, 2> gainChance; // gainChance[0/1] = default gain chance on level-up for might/magic heroes
  60. void updateFrom(const JsonNode & data);
  61. void serializeJson(JsonSerializeFormat & handler);
  62. template <typename Handler> void serialize(Handler & h, const int version)
  63. {
  64. h & id;
  65. h & identifier;
  66. h & name;
  67. if(version >= 785)
  68. {
  69. h & gainChance;
  70. }
  71. h & levels;
  72. }
  73. friend class CSkillHandler;
  74. friend DLL_LINKAGE std::ostream & operator<<(std::ostream & out, const CSkill & skill);
  75. friend DLL_LINKAGE std::ostream & operator<<(std::ostream & out, const CSkill::LevelInfo & info);
  76. };
  77. class DLL_LINKAGE CSkillHandler: public CHandlerBase<SecondarySkill, Skill, CSkill, SkillService>
  78. {
  79. public:
  80. CSkillHandler();
  81. virtual ~CSkillHandler();
  82. ///IHandler base
  83. std::vector<JsonNode> loadLegacyData(size_t dataSize) override;
  84. void afterLoadFinalization() override;
  85. void beforeValidate(JsonNode & object) override;
  86. std::vector<bool> getDefaultAllowed() const override;
  87. const std::string & skillInfo(int skill, int level) const;
  88. const std::string & skillName(int skill) const;
  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. };