CHeroHandler.h 8.0 KB

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