CHeroHandler.h 7.9 KB

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