CHeroHandler.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. #pragma once
  2. #include "../lib/ConstTransitivePtr.h"
  3. #include "GameConstants.h"
  4. #include "HeroBonus.h"
  5. /*
  6. * CHeroHandler.h, part of VCMI engine
  7. *
  8. * Authors: listed in file AUTHORS in main folder
  9. *
  10. * License: GNU General Public License v2.0 or later
  11. * Full text of license available in license.txt file, in main folder
  12. *
  13. */
  14. class CHeroClass;
  15. class CDefHandler;
  16. class CGameInfo;
  17. class CGHeroInstance;
  18. struct BattleHex;
  19. class JsonNode;
  20. struct SSpecialtyInfo
  21. { si32 type;
  22. si32 val;
  23. si32 subtype;
  24. si32 additionalinfo;
  25. template <typename Handler> void serialize(Handler &h, const int version)
  26. {
  27. h & type & val & subtype & additionalinfo;
  28. }
  29. };
  30. struct SSpecialtyBonus
  31. /// temporary hold
  32. {
  33. ui8 growsWithLevel;
  34. BonusList bonuses;
  35. template <typename Handler> void serialize(Handler &h, const int version)
  36. {
  37. h & growsWithLevel & bonuses;
  38. }
  39. };
  40. class DLL_LINKAGE CHero
  41. {
  42. public:
  43. struct InitialArmyStack
  44. {
  45. ui32 minAmount;
  46. ui32 maxAmount;
  47. CreatureID creature;
  48. template <typename Handler> void serialize(Handler &h, const int version)
  49. {
  50. h & minAmount & maxAmount & creature;
  51. }
  52. };
  53. si32 ID;
  54. si32 imageIndex;
  55. std::vector<InitialArmyStack> initialArmy;
  56. CHeroClass * heroClass;
  57. std::vector<std::pair<SecondarySkill, ui8> > secSkillsInit; //initial secondary skills; first - ID of skill, second - level of skill (1 - basic, 2 - adv., 3 - expert)
  58. std::vector<SSpecialtyInfo> spec;
  59. std::vector<SSpecialtyBonus> specialty;
  60. std::set<SpellID> spells;
  61. ui8 sex; // default sex: 0=male, 1=female
  62. ui8 special; // hero is special and won't be placed in game (unless preset on map), e.g. campaign heroes
  63. /// Localized texts
  64. std::string name; //name of hero
  65. std::string biography;
  66. std::string specName;
  67. std::string specDescr;
  68. std::string specTooltip;
  69. /// Graphics
  70. std::string iconSpecSmall;
  71. std::string iconSpecLarge;
  72. std::string portraitSmall;
  73. std::string portraitLarge;
  74. template <typename Handler> void serialize(Handler &h, const int version)
  75. {
  76. h & ID & imageIndex & initialArmy & heroClass & secSkillsInit & spec & specialty & spells & sex;
  77. h & name & biography & specName & specDescr & specTooltip;
  78. h & iconSpecSmall & iconSpecLarge & portraitSmall & portraitLarge;
  79. }
  80. };
  81. class DLL_LINKAGE CHeroClass
  82. {
  83. public:
  84. std::string identifier;
  85. std::string name; // translatable
  86. double aggression;
  87. TFaction faction;
  88. ui8 id;
  89. std::vector<int> primarySkillInitial; // initial primary skills
  90. std::vector<int> primarySkillLowLevel; // probability (%) of getting point of primary skill when getting level
  91. std::vector<int> primarySkillHighLevel;// same for high levels (> 10)
  92. std::vector<int> secSkillProbability; //probabilities of gaining secondary skills (out of 112), in id order
  93. std::map<TFaction, int> selectionProbability; //probability of selection in towns
  94. std::string imageBattleMale;
  95. std::string imageBattleFemale;
  96. std::string imageMapMale;
  97. std::string imageMapFemale;
  98. SecondarySkill chooseSecSkill(const std::set<SecondarySkill> & possibles) const; //picks secondary skill out from given possibilities
  99. template <typename Handler> void serialize(Handler &h, const int version)
  100. {
  101. h & identifier & name & faction & aggression;
  102. h & primarySkillInitial & primarySkillLowLevel;
  103. h & primarySkillHighLevel & secSkillProbability;
  104. h & selectionProbability;
  105. h & imageBattleMale & imageBattleFemale & imageMapMale & imageMapFemale;
  106. }
  107. EAlignment::EAlignment getAlignment() const;
  108. };
  109. struct DLL_LINKAGE CObstacleInfo
  110. {
  111. si32 ID;
  112. std::string defName;
  113. std::vector<ETerrainType> allowedTerrains;
  114. std::vector<BFieldType> allowedSpecialBfields;
  115. ui8 isAbsoluteObstacle; //there may only one such obstacle in battle and its position is always the same
  116. si32 width, height; //how much space to the right and up is needed to place obstacle (affects only placement algorithm)
  117. std::vector<si16> blockedTiles; //offsets relative to obstacle position (that is its left bottom corner)
  118. std::vector<BattleHex> getBlocked(BattleHex hex) const; //returns vector of hexes blocked by obstacle when it's placed on hex 'hex'
  119. bool isAppropriate(ETerrainType terrainType, int specialBattlefield = -1) const;
  120. template <typename Handler> void serialize(Handler &h, const int version)
  121. {
  122. h & ID & defName & allowedTerrains & allowedSpecialBfields & isAbsoluteObstacle & width & height & blockedTiles;
  123. }
  124. };
  125. class DLL_LINKAGE CHeroClassHandler
  126. {
  127. public:
  128. std::vector< ConstTransitivePtr<CHeroClass> > heroClasses;
  129. /// load from H3 config
  130. void load();
  131. /// load any number of classes from json
  132. void load(const JsonNode & classes);
  133. /// load one class from json
  134. CHeroClass * loadClass(const JsonNode & node);
  135. ~CHeroClassHandler();
  136. template <typename Handler> void serialize(Handler &h, const int version)
  137. {
  138. h & heroClasses;
  139. }
  140. };
  141. class DLL_LINKAGE CHeroHandler
  142. {
  143. /// expPerLEvel[i] is amount of exp needed to reach level i;
  144. /// consists of 201 values. Any higher levels require experience larger that ui64 can hold
  145. std::vector<ui64> expPerLevel;
  146. /// common function for loading heroes from mods and from H3
  147. void loadHeroJson(CHero * hero, const JsonNode & node);
  148. public:
  149. CHeroClassHandler classes;
  150. std::vector< ConstTransitivePtr<CHero> > heroes;
  151. //default costs of going through terrains. -1 means terrain is impassable
  152. std::vector<int> terrCosts;
  153. struct SBallisticsLevelInfo
  154. {
  155. ui8 keep, tower, gate, wall; //chance to hit in percent (eg. 87 is 87%)
  156. ui8 shots; //how many shots we have
  157. ui8 noDmg, oneDmg, twoDmg; //chances for shot dealing certain dmg in percent (eg. 87 is 87%); must sum to 100
  158. ui8 sum; //I don't know if it is useful for anything, but it's in config file
  159. template <typename Handler> void serialize(Handler &h, const int version)
  160. {
  161. h & keep & tower & gate & wall & shots & noDmg & oneDmg & twoDmg & sum;
  162. }
  163. };
  164. std::vector<SBallisticsLevelInfo> ballistics; //info about ballistics ability per level; [0] - none; [1] - basic; [2] - adv; [3] - expert
  165. std::map<int, CObstacleInfo> obstacles; //info about obstacles that may be placed on battlefield
  166. std::map<int, CObstacleInfo> absoluteObstacles; //info about obstacles that may be placed on battlefield
  167. ui32 level(ui64 experience) const; //calculates level corresponding to given experience amount
  168. ui64 reqExp(ui32 level) const; //calculates experience required for given level
  169. /// Load multiple heroes from json
  170. void load(const JsonNode & heroes);
  171. /// Load single hero from json
  172. CHero * loadHero(const JsonNode & node);
  173. /// Load everything (calls functions below + classes.load())
  174. void load();
  175. void loadHeroes();
  176. void loadHeroTexts();
  177. void loadExperience();
  178. void loadBallistics();
  179. void loadTerrains();
  180. void loadObstacles();
  181. CHeroHandler(); //c-tor
  182. ~CHeroHandler(); //d-tor
  183. /**
  184. * Gets a list of default allowed heroes.
  185. *
  186. * TODO Proposal for hero modding: Replace hero id with a unique machine readable hero name and
  187. * create a JSON config file or merge it with a existing config file which describes which heroes can be used for
  188. * random map generation / map editor(default map settings). (Gelu, ... should be excluded)
  189. *
  190. * @return a list of allowed heroes, the index is the hero id and the value either 0 for not allowed or 1 for allowed
  191. */
  192. std::vector<bool> getDefaultAllowedHeroes() const;
  193. /**
  194. * Gets a list of default allowed abilities. OH3 abilities/skills are all allowed by default.
  195. *
  196. * @return a list of allowed abilities, the index is the ability id
  197. */
  198. std::vector<bool> getDefaultAllowedAbilities() const;
  199. template <typename Handler> void serialize(Handler &h, const int version)
  200. {
  201. h & classes & heroes & expPerLevel & ballistics & terrCosts;
  202. h & obstacles & absoluteObstacles;
  203. }
  204. };