CHeroHandler.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. template <typename Handler> void serialize(Handler &h, const int version)
  48. {
  49. h & minAmount;
  50. h & maxAmount;
  51. h & creature;
  52. }
  53. };
  54. si32 imageIndex = 0;
  55. std::vector<InitialArmyStack> initialArmy;
  56. CHeroClass * heroClass{};
  57. std::vector<std::pair<SecondarySkill, ui8> > secSkillsInit; //initial secondary skills; first - ID of skill, second - level of skill (1 - basic, 2 - adv., 3 - expert)
  58. BonusList specialty;
  59. std::set<SpellID> spells;
  60. bool haveSpellBook = false;
  61. bool special = false; // hero is special and won't be placed in game (unless preset on map), e.g. campaign heroes
  62. bool onlyOnWaterMap; // hero will be placed only if the map contains water
  63. bool onlyOnMapWithoutWater; // hero will be placed only if the map does not contain water
  64. EHeroGender gender = EHeroGender::MALE; // default sex: 0=male, 1=female
  65. /// Graphics
  66. std::string iconSpecSmall;
  67. std::string iconSpecLarge;
  68. std::string portraitSmall;
  69. std::string portraitLarge;
  70. AnimationPath battleImage;
  71. CHero();
  72. virtual ~CHero();
  73. int32_t getIndex() const override;
  74. int32_t getIconIndex() const override;
  75. std::string getJsonKey() const override;
  76. HeroTypeID getId() const override;
  77. void registerIcons(const IconRegistar & cb) const override;
  78. std::string getNameTranslated() const override;
  79. std::string getBiographyTranslated() const override;
  80. std::string getSpecialtyNameTranslated() const override;
  81. std::string getSpecialtyDescriptionTranslated() const override;
  82. std::string getSpecialtyTooltipTranslated() const override;
  83. std::string getNameTextID() const override;
  84. std::string getBiographyTextID() const override;
  85. std::string getSpecialtyNameTextID() const override;
  86. std::string getSpecialtyDescriptionTextID() const override;
  87. std::string getSpecialtyTooltipTextID() const override;
  88. void updateFrom(const JsonNode & data);
  89. void serializeJson(JsonSerializeFormat & handler);
  90. template <typename Handler> void serialize(Handler &h, const int version)
  91. {
  92. h & ID;
  93. h & imageIndex;
  94. h & initialArmy;
  95. h & heroClass;
  96. h & secSkillsInit;
  97. h & specialty;
  98. h & spells;
  99. h & haveSpellBook;
  100. h & gender;
  101. h & special;
  102. h & onlyOnWaterMap;
  103. h & onlyOnMapWithoutWater;
  104. h & iconSpecSmall;
  105. h & iconSpecLarge;
  106. h & portraitSmall;
  107. h & portraitLarge;
  108. h & identifier;
  109. h & modScope;
  110. h & battleImage;
  111. }
  112. };
  113. class DLL_LINKAGE CHeroClass : public HeroClass
  114. {
  115. friend class CHeroClassHandler;
  116. HeroClassID id; // use getId instead
  117. std::string modScope;
  118. std::string identifier; // use getJsonKey instead
  119. public:
  120. enum EClassAffinity
  121. {
  122. MIGHT,
  123. MAGIC
  124. };
  125. //double aggression; // not used in vcmi.
  126. FactionID faction;
  127. ui8 affinity; // affinity, using EClassAffinity enum
  128. // default chance for hero of specific class to appear in tavern, if field "tavern" was not set
  129. // resulting chance = sqrt(town.chance * heroClass.chance)
  130. ui32 defaultTavernChance;
  131. CCreature * commander;
  132. std::vector<int> primarySkillInitial; // initial primary skills
  133. std::vector<int> primarySkillLowLevel; // probability (%) of getting point of primary skill when getting level
  134. std::vector<int> primarySkillHighLevel;// same for high levels (> 10)
  135. std::map<SecondarySkill, int> secSkillProbability; //probabilities of gaining secondary skills (out of 112), in id order
  136. std::map<FactionID, int> selectionProbability; //probability of selection in towns
  137. AnimationPath imageBattleMale;
  138. AnimationPath imageBattleFemale;
  139. std::string imageMapMale;
  140. std::string imageMapFemale;
  141. CHeroClass();
  142. int32_t getIndex() const override;
  143. int32_t getIconIndex() const override;
  144. std::string getJsonKey() const override;
  145. HeroClassID getId() const override;
  146. void registerIcons(const IconRegistar & cb) const override;
  147. std::string getNameTranslated() const override;
  148. std::string getNameTextID() const override;
  149. bool isMagicHero() const;
  150. SecondarySkill chooseSecSkill(const std::set<SecondarySkill> & possibles, CRandomGenerator & rand) const; //picks secondary skill out from given possibilities
  151. void updateFrom(const JsonNode & data);
  152. void serializeJson(JsonSerializeFormat & handler);
  153. template <typename Handler> void serialize(Handler & h, const int version)
  154. {
  155. h & modScope;
  156. h & identifier;
  157. h & faction;
  158. h & id;
  159. h & defaultTavernChance;
  160. h & primarySkillInitial;
  161. h & primarySkillLowLevel;
  162. h & primarySkillHighLevel;
  163. h & secSkillProbability;
  164. h & selectionProbability;
  165. h & affinity;
  166. h & commander;
  167. h & imageBattleMale;
  168. h & imageBattleFemale;
  169. h & imageMapMale;
  170. h & imageMapFemale;
  171. }
  172. EAlignment getAlignment() const;
  173. };
  174. class DLL_LINKAGE CHeroClassHandler : public CHandlerBase<HeroClassID, HeroClass, CHeroClass, HeroClassService>
  175. {
  176. void fillPrimarySkillData(const JsonNode & node, CHeroClass * heroClass, PrimarySkill pSkill) const;
  177. public:
  178. std::vector<JsonNode> loadLegacyData() override;
  179. void afterLoadFinalization() override;
  180. std::vector<bool> getDefaultAllowed() const override;
  181. ~CHeroClassHandler();
  182. template <typename Handler> void serialize(Handler &h, const int version)
  183. {
  184. h & objects;
  185. }
  186. protected:
  187. const std::vector<std::string> & getTypeNames() const override;
  188. CHeroClass * loadFromJson(const std::string & scope, const JsonNode & node, const std::string & identifier, size_t index) override;
  189. };
  190. class DLL_LINKAGE CHeroHandler : public CHandlerBase<HeroTypeID, HeroType, CHero, HeroTypeService>
  191. {
  192. /// expPerLEvel[i] is amount of exp needed to reach level i;
  193. /// consists of 201 values. Any higher levels require experience larger that ui64 can hold
  194. std::vector<ui64> expPerLevel;
  195. /// helpers for loading to avoid huge load functions
  196. void loadHeroArmy(CHero * hero, const JsonNode & node) const;
  197. void loadHeroSkills(CHero * hero, const JsonNode & node) const;
  198. void loadHeroSpecialty(CHero * hero, const JsonNode & node);
  199. void loadExperience();
  200. std::vector<std::function<void()>> callAfterLoadFinalization;
  201. public:
  202. CHeroClassHandler classes;
  203. ui32 level(ui64 experience) const; //calculates level corresponding to given experience amount
  204. ui64 reqExp(ui32 level) const; //calculates experience required for given level
  205. std::vector<JsonNode> loadLegacyData() override;
  206. void beforeValidate(JsonNode & object) override;
  207. void loadObject(std::string scope, std::string name, const JsonNode & data) override;
  208. void loadObject(std::string scope, std::string name, const JsonNode & data, size_t index) override;
  209. void afterLoadFinalization() override;
  210. CHeroHandler();
  211. ~CHeroHandler();
  212. std::vector<bool> getDefaultAllowed() const override;
  213. template <typename Handler> void serialize(Handler &h, const int version)
  214. {
  215. h & classes;
  216. h & objects;
  217. h & expPerLevel;
  218. }
  219. protected:
  220. const std::vector<std::string> & getTypeNames() const override;
  221. CHero * loadFromJson(const std::string & scope, const JsonNode & node, const std::string & identifier, size_t index) override;
  222. };
  223. VCMI_LIB_NAMESPACE_END