CHeroHandler.h 9.0 KB

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