CHeroHandler.h 9.5 KB

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