CHeroHandler.h 8.3 KB

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