CHeroHandler.h 10 KB

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