CSkillHandler.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 "../lib/HeroBonus.h"
  12. #include "GameConstants.h"
  13. #include "IHandlerBase.h"
  14. class DLL_LINKAGE CSkill // secondary skill
  15. {
  16. public:
  17. struct LevelInfo
  18. {
  19. std::string description; //descriptions of spell for skill level
  20. std::string iconSmall;
  21. std::string iconMedium;
  22. std::string iconLarge;
  23. std::vector<std::shared_ptr<Bonus>> effects;
  24. LevelInfo();
  25. ~LevelInfo();
  26. template <typename Handler> void serialize(Handler & h, const int version)
  27. {
  28. h & description;
  29. if(version >= 785)
  30. {
  31. h & iconSmall;
  32. h & iconMedium;
  33. h & iconLarge;
  34. }
  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. const LevelInfo & at(int level) const;
  45. LevelInfo & at(int level);
  46. std::string toString() const;
  47. SecondarySkill id;
  48. std::string identifier;
  49. std::string name; //as displayed in GUI
  50. std::array<si32, 2> gainChance; // gainChance[0/1] = default gain chance on level-up for might/magic heroes
  51. template <typename Handler> void serialize(Handler & h, const int version)
  52. {
  53. h & id;
  54. h & identifier;
  55. h & name;
  56. if(version >= 785)
  57. {
  58. h & gainChance;
  59. }
  60. h & levels;
  61. }
  62. friend class CSkillHandler;
  63. friend DLL_LINKAGE std::ostream & operator<<(std::ostream & out, const CSkill & skill);
  64. friend DLL_LINKAGE std::ostream & operator<<(std::ostream & out, const CSkill::LevelInfo & info);
  65. };
  66. class DLL_LINKAGE CSkillHandler: public CHandlerBase<SecondarySkill, CSkill>
  67. {
  68. public:
  69. CSkillHandler();
  70. virtual ~CSkillHandler();
  71. ///IHandler base
  72. std::vector<JsonNode> loadLegacyData(size_t dataSize) override;
  73. void afterLoadFinalization() override;
  74. void beforeValidate(JsonNode & object) override;
  75. std::vector<bool> getDefaultAllowed() const override;
  76. const std::vector<std::string> & getTypeNames() const override;
  77. const std::string & skillInfo(int skill, int level) const;
  78. const std::string & skillName(int skill) const;
  79. ///json serialization helpers
  80. static si32 decodeSkill(const std::string & identifier);
  81. static std::string encodeSkill(const si32 index);
  82. static std::string encodeSkillWithType(const si32 index);
  83. template <typename Handler> void serialize(Handler & h, const int version)
  84. {
  85. h & objects;
  86. }
  87. protected:
  88. CSkill * loadFromJson(const JsonNode & json, const std::string & identifier, size_t index) override;
  89. };