CHeroHandler.h 8.7 KB

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