CHeroHandler.h 8.0 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. 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. std::string 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. HeroTypeID getId() const override;
  74. void registerIcons(const IconRegistar & cb) const override;
  75. std::string getNameTranslated() const override;
  76. std::string getBiographyTranslated() const override;
  77. std::string getSpecialtyNameTranslated() const override;
  78. std::string getSpecialtyDescriptionTranslated() const override;
  79. std::string getSpecialtyTooltipTranslated() const override;
  80. std::string getNameTextID() const override;
  81. std::string getBiographyTextID() const override;
  82. std::string getSpecialtyNameTextID() const override;
  83. std::string getSpecialtyDescriptionTextID() const override;
  84. std::string getSpecialtyTooltipTextID() const override;
  85. void updateFrom(const JsonNode & data);
  86. void serializeJson(JsonSerializeFormat & handler);
  87. template <typename Handler> void serialize(Handler &h, const int version)
  88. {
  89. h & ID;
  90. h & imageIndex;
  91. h & initialArmy;
  92. h & heroClass;
  93. h & secSkillsInit;
  94. h & specialty;
  95. h & spells;
  96. h & haveSpellBook;
  97. h & gender;
  98. h & special;
  99. h & iconSpecSmall;
  100. h & iconSpecLarge;
  101. h & portraitSmall;
  102. h & portraitLarge;
  103. h & identifier;
  104. h & modScope;
  105. h & battleImage;
  106. }
  107. };
  108. class DLL_LINKAGE CHeroClass : public HeroClass
  109. {
  110. friend class CHeroClassHandler;
  111. HeroClassID id; // use getId instead
  112. std::string modScope;
  113. std::string identifier; // use getJsonKey instead
  114. public:
  115. enum EClassAffinity
  116. {
  117. MIGHT,
  118. MAGIC
  119. };
  120. //double aggression; // not used in vcmi.
  121. FactionID faction;
  122. ui8 affinity; // affinity, using EClassAffinity enum
  123. // default chance for hero of specific class to appear in tavern, if field "tavern" was not set
  124. // resulting chance = sqrt(town.chance * heroClass.chance)
  125. ui32 defaultTavernChance;
  126. CCreature * commander;
  127. std::vector<int> primarySkillInitial; // initial primary skills
  128. std::vector<int> primarySkillLowLevel; // probability (%) of getting point of primary skill when getting level
  129. std::vector<int> primarySkillHighLevel;// same for high levels (> 10)
  130. std::vector<int> secSkillProbability; //probabilities of gaining secondary skills (out of 112), in id order
  131. std::map<FactionID, int> selectionProbability; //probability of selection in towns
  132. std::string imageBattleMale;
  133. std::string imageBattleFemale;
  134. std::string imageMapMale;
  135. std::string imageMapFemale;
  136. CHeroClass();
  137. int32_t getIndex() const override;
  138. int32_t getIconIndex() const override;
  139. std::string getJsonKey() const override;
  140. HeroClassID getId() const override;
  141. void registerIcons(const IconRegistar & cb) const override;
  142. std::string getNameTranslated() const override;
  143. std::string getNameTextID() const override;
  144. bool isMagicHero() const;
  145. SecondarySkill chooseSecSkill(const std::set<SecondarySkill> & possibles, CRandomGenerator & rand) const; //picks secondary skill out from given possibilities
  146. void updateFrom(const JsonNode & data);
  147. void serializeJson(JsonSerializeFormat & handler);
  148. template <typename Handler> void serialize(Handler & h, const int version)
  149. {
  150. h & modScope;
  151. h & identifier;
  152. h & faction;
  153. h & id;
  154. h & defaultTavernChance;
  155. h & primarySkillInitial;
  156. h & primarySkillLowLevel;
  157. h & primarySkillHighLevel;
  158. h & secSkillProbability;
  159. h & selectionProbability;
  160. h & affinity;
  161. h & commander;
  162. h & imageBattleMale;
  163. h & imageBattleFemale;
  164. h & imageMapMale;
  165. h & imageMapFemale;
  166. if(!h.saving)
  167. {
  168. for(int & i : secSkillProbability)
  169. vstd::amax(i, 0);
  170. }
  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::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. //default costs of going through terrains. -1 means terrain is impassable
  204. std::map<TerrainId, int> terrCosts;
  205. ui32 level(ui64 experience) const; //calculates level corresponding to given experience amount
  206. ui64 reqExp(ui32 level) const; //calculates experience required for given level
  207. std::vector<JsonNode> loadLegacyData() override;
  208. void beforeValidate(JsonNode & object) override;
  209. void loadObject(std::string scope, std::string name, const JsonNode & data) override;
  210. void loadObject(std::string scope, std::string name, const JsonNode & data, size_t index) override;
  211. void afterLoadFinalization() override;
  212. CHeroHandler();
  213. ~CHeroHandler();
  214. std::vector<bool> getDefaultAllowed() const override;
  215. template <typename Handler> void serialize(Handler &h, const int version)
  216. {
  217. h & classes;
  218. h & objects;
  219. h & expPerLevel;
  220. h & terrCosts;
  221. }
  222. protected:
  223. const std::vector<std::string> & getTypeNames() const override;
  224. CHero * loadFromJson(const std::string & scope, const JsonNode & node, const std::string & identifier, size_t index) override;
  225. };
  226. VCMI_LIB_NAMESPACE_END