CHeroHandler.h 9.2 KB

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