CHeroHandler.h 7.2 KB

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