CHeroHandler.h 8.2 KB

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