CSkillHandler.h 3.2 KB

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