CHeroHandler.h 9.3 KB

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