CHeroHandler.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. bool haveSpellBook;
  62. bool special; // hero is special and won't be placed in game (unless preset on map), e.g. campaign heroes
  63. ui8 sex; // default sex: 0=male, 1=female
  64. /// Localized texts
  65. std::string name; //name of hero
  66. std::string biography;
  67. std::string specName;
  68. std::string specDescr;
  69. std::string specTooltip;
  70. /// Graphics
  71. std::string iconSpecSmall;
  72. std::string iconSpecLarge;
  73. std::string portraitSmall;
  74. std::string portraitLarge;
  75. template <typename Handler> void serialize(Handler &h, const int version)
  76. {
  77. h & ID & imageIndex & initialArmy & heroClass & secSkillsInit & spec & specialty & spells & haveSpellBook & sex & special;
  78. h & name & biography & specName & specDescr & specTooltip;
  79. h & iconSpecSmall & iconSpecLarge & portraitSmall & portraitLarge;
  80. }
  81. };
  82. class DLL_LINKAGE CHeroClass
  83. {
  84. public:
  85. std::string identifier;
  86. std::string name; // translatable
  87. //double aggression; // not used in vcmi.
  88. TFaction faction;
  89. ui8 id;
  90. std::vector<int> primarySkillInitial; // initial primary skills
  91. std::vector<int> primarySkillLowLevel; // probability (%) of getting point of primary skill when getting level
  92. std::vector<int> primarySkillHighLevel;// same for high levels (> 10)
  93. std::vector<int> secSkillProbability; //probabilities of gaining secondary skills (out of 112), in id order
  94. std::map<TFaction, int> selectionProbability; //probability of selection in towns
  95. std::string imageBattleMale;
  96. std::string imageBattleFemale;
  97. std::string imageMapMale;
  98. std::string imageMapFemale;
  99. SecondarySkill chooseSecSkill(const std::set<SecondarySkill> & possibles) const; //picks secondary skill out from given possibilities
  100. template <typename Handler> void serialize(Handler &h, const int version)
  101. {
  102. h & identifier & name & faction;// & aggression;
  103. h & primarySkillInitial & primarySkillLowLevel;
  104. h & primarySkillHighLevel & secSkillProbability;
  105. h & selectionProbability;
  106. h & imageBattleMale & imageBattleFemale & imageMapMale & imageMapFemale;
  107. }
  108. EAlignment::EAlignment getAlignment() const;
  109. };
  110. struct DLL_LINKAGE CObstacleInfo
  111. {
  112. si32 ID;
  113. std::string defName;
  114. std::vector<ETerrainType> allowedTerrains;
  115. std::vector<BFieldType> allowedSpecialBfields;
  116. ui8 isAbsoluteObstacle; //there may only one such obstacle in battle and its position is always the same
  117. si32 width, height; //how much space to the right and up is needed to place obstacle (affects only placement algorithm)
  118. std::vector<si16> blockedTiles; //offsets relative to obstacle position (that is its left bottom corner)
  119. std::vector<BattleHex> getBlocked(BattleHex hex) const; //returns vector of hexes blocked by obstacle when it's placed on hex 'hex'
  120. bool isAppropriate(ETerrainType terrainType, int specialBattlefield = -1) const;
  121. template <typename Handler> void serialize(Handler &h, const int version)
  122. {
  123. h & ID & defName & allowedTerrains & allowedSpecialBfields & isAbsoluteObstacle & width & height & blockedTiles;
  124. }
  125. };
  126. class DLL_LINKAGE CHeroClassHandler
  127. {
  128. public:
  129. std::vector< ConstTransitivePtr<CHeroClass> > heroClasses;
  130. /// load from H3 config
  131. void load();
  132. /// load any number of classes from json
  133. void load(std::string objectID, const JsonNode & classes);
  134. /// load one class from json
  135. CHeroClass * loadClass(const JsonNode & node);
  136. ~CHeroClassHandler();
  137. template <typename Handler> void serialize(Handler &h, const int version)
  138. {
  139. h & heroClasses;
  140. }
  141. };
  142. class DLL_LINKAGE CHeroHandler
  143. {
  144. /// expPerLEvel[i] is amount of exp needed to reach level i;
  145. /// consists of 201 values. Any higher levels require experience larger that ui64 can hold
  146. std::vector<ui64> expPerLevel;
  147. /// helpers for loading to avoid huge load functions
  148. void loadHeroArmy(CHero * hero, const JsonNode & node);
  149. void loadHeroSkills(CHero * hero, const JsonNode & node);
  150. void loadHeroSpecialty(CHero * hero, const JsonNode & node);
  151. public:
  152. CHeroClassHandler classes;
  153. std::vector< ConstTransitivePtr<CHero> > heroes;
  154. //default costs of going through terrains. -1 means terrain is impassable
  155. std::vector<int> terrCosts;
  156. struct SBallisticsLevelInfo
  157. {
  158. ui8 keep, tower, gate, wall; //chance to hit in percent (eg. 87 is 87%)
  159. ui8 shots; //how many shots we have
  160. ui8 noDmg, oneDmg, twoDmg; //chances for shot dealing certain dmg in percent (eg. 87 is 87%); must sum to 100
  161. ui8 sum; //I don't know if it is useful for anything, but it's in config file
  162. template <typename Handler> void serialize(Handler &h, const int version)
  163. {
  164. h & keep & tower & gate & wall & shots & noDmg & oneDmg & twoDmg & sum;
  165. }
  166. };
  167. std::vector<SBallisticsLevelInfo> ballistics; //info about ballistics ability per level; [0] - none; [1] - basic; [2] - adv; [3] - expert
  168. std::map<int, CObstacleInfo> obstacles; //info about obstacles that may be placed on battlefield
  169. std::map<int, CObstacleInfo> absoluteObstacles; //info about obstacles that may be placed on battlefield
  170. ui32 level(ui64 experience) const; //calculates level corresponding to given experience amount
  171. ui64 reqExp(ui32 level) const; //calculates experience required for given level
  172. /// Load multiple heroes from json
  173. void load(std::string objectID, const JsonNode & heroes);
  174. /// Load single hero from json
  175. CHero * loadHero(const JsonNode & node);
  176. /// Load everything (calls functions below + classes.load())
  177. void load();
  178. void loadHeroes();
  179. void loadExperience();
  180. void loadBallistics();
  181. void loadTerrains();
  182. void loadObstacles();
  183. CHeroHandler(); //c-tor
  184. ~CHeroHandler(); //d-tor
  185. /**
  186. * Gets a list of default allowed heroes.
  187. *
  188. * TODO Proposal for hero modding: Replace hero id with a unique machine readable hero name and
  189. * create a JSON config file or merge it with a existing config file which describes which heroes can be used for
  190. * random map generation / map editor(default map settings). (Gelu, ... should be excluded)
  191. *
  192. * @return a list of allowed heroes, the index is the hero id and the value either 0 for not allowed or 1 for allowed
  193. */
  194. std::vector<bool> getDefaultAllowedHeroes() const;
  195. /**
  196. * Gets a list of default allowed abilities. OH3 abilities/skills are all allowed by default.
  197. *
  198. * @return a list of allowed abilities, the index is the ability id
  199. */
  200. std::vector<bool> getDefaultAllowedAbilities() const;
  201. template <typename Handler> void serialize(Handler &h, const int version)
  202. {
  203. h & classes & heroes & expPerLevel & ballistics & terrCosts;
  204. h & obstacles & absoluteObstacles;
  205. }
  206. };