CHeroHandler.h 10 KB

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