CHeroHandler.h 8.1 KB

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