CHeroHandler.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. * CHeroHandler.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/HeroClass.h>
  12. #include <vcmi/HeroClassService.h>
  13. #include <vcmi/HeroType.h>
  14. #include <vcmi/HeroTypeService.h>
  15. #include "ConstTransitivePtr.h"
  16. #include "GameConstants.h"
  17. #include "bonuses/Bonus.h"
  18. #include "bonuses/BonusList.h"
  19. #include "IHandlerBase.h"
  20. #include "filesystem/ResourcePath.h"
  21. VCMI_LIB_NAMESPACE_BEGIN
  22. namespace vstd
  23. {
  24. class RNG;
  25. }
  26. class CHeroClass;
  27. class CGHeroInstance;
  28. struct BattleHex;
  29. class JsonNode;
  30. class JsonSerializeFormat;
  31. class BattleField;
  32. enum class EHeroGender : int8_t
  33. {
  34. DEFAULT = -1, // from h3m, instance has same gender as hero type
  35. MALE = 0,
  36. FEMALE = 1,
  37. };
  38. class DLL_LINKAGE CHero : public HeroType
  39. {
  40. friend class CHeroHandler;
  41. HeroTypeID ID;
  42. std::string identifier;
  43. std::string modScope;
  44. public:
  45. struct InitialArmyStack
  46. {
  47. ui32 minAmount;
  48. ui32 maxAmount;
  49. CreatureID creature;
  50. };
  51. si32 imageIndex = 0;
  52. std::vector<InitialArmyStack> initialArmy;
  53. const CHeroClass * heroClass = nullptr;
  54. std::vector<std::pair<SecondarySkill, ui8> > secSkillsInit; //initial secondary skills; first - ID of skill, second - level of skill (1 - basic, 2 - adv., 3 - expert)
  55. BonusList specialty;
  56. std::set<SpellID> spells;
  57. bool haveSpellBook = false;
  58. bool special = false; // hero is special and won't be placed in game (unless preset on map), e.g. campaign heroes
  59. bool onlyOnWaterMap; // hero will be placed only if the map contains water
  60. bool onlyOnMapWithoutWater; // hero will be placed only if the map does not contain water
  61. EHeroGender gender = EHeroGender::MALE; // default sex: 0=male, 1=female
  62. /// Graphics
  63. std::string iconSpecSmall;
  64. std::string iconSpecLarge;
  65. std::string portraitSmall;
  66. std::string portraitLarge;
  67. AnimationPath battleImage;
  68. CHero();
  69. virtual ~CHero();
  70. int32_t getIndex() const override;
  71. int32_t getIconIndex() const override;
  72. std::string getJsonKey() const override;
  73. HeroTypeID getId() const override;
  74. void registerIcons(const IconRegistar & cb) const override;
  75. std::string getNameTranslated() const override;
  76. std::string getBiographyTranslated() const override;
  77. std::string getSpecialtyNameTranslated() const override;
  78. std::string getSpecialtyDescriptionTranslated() const override;
  79. std::string getSpecialtyTooltipTranslated() const override;
  80. std::string getNameTextID() const override;
  81. std::string getBiographyTextID() const override;
  82. std::string getSpecialtyNameTextID() const override;
  83. std::string getSpecialtyDescriptionTextID() const override;
  84. std::string getSpecialtyTooltipTextID() const override;
  85. void updateFrom(const JsonNode & data);
  86. void serializeJson(JsonSerializeFormat & handler);
  87. };
  88. class DLL_LINKAGE CHeroClass : public HeroClass
  89. {
  90. friend class CHeroClassHandler;
  91. HeroClassID id; // use getId instead
  92. std::string modScope;
  93. std::string identifier; // use getJsonKey instead
  94. public:
  95. enum EClassAffinity
  96. {
  97. MIGHT,
  98. MAGIC
  99. };
  100. //double aggression; // not used in vcmi.
  101. FactionID faction;
  102. ui8 affinity; // affinity, using EClassAffinity enum
  103. // default chance for hero of specific class to appear in tavern, if field "tavern" was not set
  104. // resulting chance = sqrt(town.chance * heroClass.chance)
  105. ui32 defaultTavernChance;
  106. CreatureID commander;
  107. std::vector<int> primarySkillInitial; // initial primary skills
  108. std::vector<int> primarySkillLowLevel; // probability (%) of getting point of primary skill when getting level
  109. std::vector<int> primarySkillHighLevel;// same for high levels (> 10)
  110. std::map<SecondarySkill, int> secSkillProbability; //probabilities of gaining secondary skills (out of 112), in id order
  111. std::map<FactionID, int> selectionProbability; //probability of selection in towns
  112. AnimationPath imageBattleMale;
  113. AnimationPath imageBattleFemale;
  114. std::string imageMapMale;
  115. std::string imageMapFemale;
  116. CHeroClass();
  117. int32_t getIndex() const override;
  118. int32_t getIconIndex() const override;
  119. std::string getJsonKey() const override;
  120. HeroClassID getId() const override;
  121. void registerIcons(const IconRegistar & cb) const override;
  122. std::string getNameTranslated() const override;
  123. std::string getNameTextID() const override;
  124. bool isMagicHero() const;
  125. SecondarySkill chooseSecSkill(const std::set<SecondarySkill> & possibles, vstd::RNG & rand) const; //picks secondary skill out from given possibilities
  126. void updateFrom(const JsonNode & data);
  127. void serializeJson(JsonSerializeFormat & handler);
  128. EAlignment getAlignment() const;
  129. int tavernProbability(FactionID faction) const;
  130. };
  131. class DLL_LINKAGE CHeroClassHandler : public CHandlerBase<HeroClassID, HeroClass, CHeroClass, HeroClassService>
  132. {
  133. void fillPrimarySkillData(const JsonNode & node, CHeroClass * heroClass, PrimarySkill pSkill) const;
  134. public:
  135. std::vector<JsonNode> loadLegacyData() override;
  136. void afterLoadFinalization() override;
  137. ~CHeroClassHandler();
  138. protected:
  139. const std::vector<std::string> & getTypeNames() const override;
  140. std::shared_ptr<CHeroClass> loadFromJson(const std::string & scope, const JsonNode & node, const std::string & identifier, size_t index) override;
  141. };
  142. class DLL_LINKAGE CHeroHandler : public CHandlerBase<HeroTypeID, HeroType, CHero, HeroTypeService>
  143. {
  144. /// expPerLEvel[i] is amount of exp needed to reach level i;
  145. /// consists of 196 values. Any higher levels require experience larger that TExpType can hold
  146. std::vector<TExpType> expPerLevel;
  147. /// helpers for loading to avoid huge load functions
  148. void loadHeroArmy(CHero * hero, const JsonNode & node) const;
  149. void loadHeroSkills(CHero * hero, const JsonNode & node) const;
  150. void loadHeroSpecialty(CHero * hero, const JsonNode & node);
  151. void loadExperience();
  152. std::vector<std::function<void()>> callAfterLoadFinalization;
  153. public:
  154. ui32 level(TExpType experience) const; //calculates level corresponding to given experience amount
  155. TExpType reqExp(ui32 level) const; //calculates experience required for given level
  156. ui32 maxSupportedLevel() const;
  157. std::vector<JsonNode> loadLegacyData() override;
  158. void beforeValidate(JsonNode & object) override;
  159. void loadObject(std::string scope, std::string name, const JsonNode & data) override;
  160. void loadObject(std::string scope, std::string name, const JsonNode & data, size_t index) override;
  161. void afterLoadFinalization() override;
  162. CHeroHandler();
  163. ~CHeroHandler();
  164. std::set<HeroTypeID> getDefaultAllowed() const;
  165. protected:
  166. const std::vector<std::string> & getTypeNames() const override;
  167. std::shared_ptr<CHero> loadFromJson(const std::string & scope, const JsonNode & node, const std::string & identifier, size_t index) override;
  168. };
  169. VCMI_LIB_NAMESPACE_END