CHeroHandler.h 9.3 KB

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