CHeroHandler.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. std::string getModScope() const override;
  74. HeroTypeID getId() const override;
  75. void registerIcons(const IconRegistar & cb) const override;
  76. std::string getNameTranslated() const override;
  77. std::string getBiographyTranslated() const override;
  78. std::string getSpecialtyNameTranslated() const override;
  79. std::string getSpecialtyDescriptionTranslated() const override;
  80. std::string getSpecialtyTooltipTranslated() const override;
  81. std::string getNameTextID() const override;
  82. std::string getBiographyTextID() const override;
  83. std::string getSpecialtyNameTextID() const override;
  84. std::string getSpecialtyDescriptionTextID() const override;
  85. std::string getSpecialtyTooltipTextID() const override;
  86. void updateFrom(const JsonNode & data);
  87. void serializeJson(JsonSerializeFormat & handler);
  88. };
  89. class DLL_LINKAGE CHeroClass : public HeroClass
  90. {
  91. friend class CHeroClassHandler;
  92. HeroClassID id; // use getId instead
  93. std::string modScope;
  94. std::string identifier; // use getJsonKey instead
  95. public:
  96. enum EClassAffinity
  97. {
  98. MIGHT,
  99. MAGIC
  100. };
  101. //double aggression; // not used in vcmi.
  102. FactionID faction;
  103. ui8 affinity; // affinity, using EClassAffinity enum
  104. // default chance for hero of specific class to appear in tavern, if field "tavern" was not set
  105. // resulting chance = sqrt(town.chance * heroClass.chance)
  106. ui32 defaultTavernChance;
  107. CreatureID commander;
  108. std::vector<int> primarySkillInitial; // initial primary skills
  109. std::vector<int> primarySkillLowLevel; // probability (%) of getting point of primary skill when getting level
  110. std::vector<int> primarySkillHighLevel;// same for high levels (> 10)
  111. std::map<SecondarySkill, int> secSkillProbability; //probabilities of gaining secondary skills (out of 112), in id order
  112. std::map<FactionID, int> selectionProbability; //probability of selection in towns
  113. AnimationPath imageBattleMale;
  114. AnimationPath imageBattleFemale;
  115. std::string imageMapMale;
  116. std::string imageMapFemale;
  117. CHeroClass();
  118. int32_t getIndex() const override;
  119. int32_t getIconIndex() const override;
  120. std::string getJsonKey() const override;
  121. std::string getModScope() const override;
  122. HeroClassID getId() const override;
  123. void registerIcons(const IconRegistar & cb) const override;
  124. std::string getNameTranslated() const override;
  125. std::string getNameTextID() const override;
  126. bool isMagicHero() const;
  127. SecondarySkill chooseSecSkill(const std::set<SecondarySkill> & possibles, vstd::RNG & rand) const; //picks secondary skill out from given possibilities
  128. void updateFrom(const JsonNode & data);
  129. void serializeJson(JsonSerializeFormat & handler);
  130. EAlignment getAlignment() const;
  131. int tavernProbability(FactionID faction) const;
  132. };
  133. class DLL_LINKAGE CHeroClassHandler : public CHandlerBase<HeroClassID, HeroClass, CHeroClass, HeroClassService>
  134. {
  135. void fillPrimarySkillData(const JsonNode & node, CHeroClass * heroClass, PrimarySkill pSkill) const;
  136. public:
  137. std::vector<JsonNode> loadLegacyData() override;
  138. void afterLoadFinalization() override;
  139. ~CHeroClassHandler();
  140. protected:
  141. const std::vector<std::string> & getTypeNames() const override;
  142. std::shared_ptr<CHeroClass> loadFromJson(const std::string & scope, const JsonNode & node, const std::string & identifier, size_t index) override;
  143. };
  144. class DLL_LINKAGE CHeroHandler : public CHandlerBase<HeroTypeID, HeroType, CHero, HeroTypeService>
  145. {
  146. /// expPerLEvel[i] is amount of exp needed to reach level i;
  147. /// consists of 196 values. Any higher levels require experience larger that TExpType can hold
  148. std::vector<TExpType> expPerLevel;
  149. /// helpers for loading to avoid huge load functions
  150. void loadHeroArmy(CHero * hero, const JsonNode & node) const;
  151. void loadHeroSkills(CHero * hero, const JsonNode & node) const;
  152. void loadHeroSpecialty(CHero * hero, const JsonNode & node);
  153. void loadExperience();
  154. std::vector<std::function<void()>> callAfterLoadFinalization;
  155. public:
  156. ui32 level(TExpType experience) const; //calculates level corresponding to given experience amount
  157. TExpType reqExp(ui32 level) const; //calculates experience required for given level
  158. ui32 maxSupportedLevel() const;
  159. std::vector<JsonNode> loadLegacyData() override;
  160. void beforeValidate(JsonNode & object) override;
  161. void loadObject(std::string scope, std::string name, const JsonNode & data) override;
  162. void loadObject(std::string scope, std::string name, const JsonNode & data, size_t index) override;
  163. void afterLoadFinalization() override;
  164. CHeroHandler();
  165. ~CHeroHandler();
  166. std::set<HeroTypeID> getDefaultAllowed() const;
  167. protected:
  168. const std::vector<std::string> & getTypeNames() const override;
  169. std::shared_ptr<CHero> loadFromJson(const std::string & scope, const JsonNode & node, const std::string & identifier, size_t index) override;
  170. };
  171. VCMI_LIB_NAMESPACE_END