CHeroHandler.h 6.5 KB

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