CHeroHandler.h 9.4 KB

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