CHeroHandler.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. std::string identifier;
  87. std::string name; // translatable
  88. //double aggression; // not used in vcmi.
  89. TFaction faction;
  90. ui8 id;
  91. std::vector<int> primarySkillInitial; // initial primary skills
  92. std::vector<int> primarySkillLowLevel; // probability (%) of getting point of primary skill when getting level
  93. std::vector<int> primarySkillHighLevel;// same for high levels (> 10)
  94. std::vector<int> secSkillProbability; //probabilities of gaining secondary skills (out of 112), in id order
  95. std::map<TFaction, int> selectionProbability; //probability of selection in towns
  96. std::string imageBattleMale;
  97. std::string imageBattleFemale;
  98. std::string imageMapMale;
  99. std::string imageMapFemale;
  100. bool isMagicHero() const;
  101. SecondarySkill chooseSecSkill(const std::set<SecondarySkill> & possibles) const; //picks secondary skill out from given possibilities
  102. template <typename Handler> void serialize(Handler &h, const int version)
  103. {
  104. h & identifier & name & faction & id;// & aggression;
  105. h & primarySkillInitial & primarySkillLowLevel;
  106. h & primarySkillHighLevel & secSkillProbability;
  107. h & selectionProbability;
  108. h & imageBattleMale & imageBattleFemale & imageMapMale & imageMapFemale;
  109. }
  110. EAlignment::EAlignment getAlignment() const;
  111. };
  112. struct DLL_LINKAGE CObstacleInfo
  113. {
  114. si32 ID;
  115. std::string defName;
  116. std::vector<ETerrainType> allowedTerrains;
  117. std::vector<BFieldType> allowedSpecialBfields;
  118. ui8 isAbsoluteObstacle; //there may only one such obstacle in battle and its position is always the same
  119. si32 width, height; //how much space to the right and up is needed to place obstacle (affects only placement algorithm)
  120. std::vector<si16> blockedTiles; //offsets relative to obstacle position (that is its left bottom corner)
  121. std::vector<BattleHex> getBlocked(BattleHex hex) const; //returns vector of hexes blocked by obstacle when it's placed on hex 'hex'
  122. bool isAppropriate(ETerrainType terrainType, int specialBattlefield = -1) const;
  123. template <typename Handler> void serialize(Handler &h, const int version)
  124. {
  125. h & ID & defName & allowedTerrains & allowedSpecialBfields & isAbsoluteObstacle & width & height & blockedTiles;
  126. }
  127. };
  128. class DLL_LINKAGE CHeroClassHandler : public IHandlerBase
  129. {
  130. CHeroClass *loadFromJson(const JsonNode & node);
  131. public:
  132. std::vector< ConstTransitivePtr<CHeroClass> > heroClasses;
  133. std::vector<JsonNode> loadLegacyData(size_t dataSize) override;
  134. void loadObject(std::string scope, std::string name, const JsonNode & data) override;
  135. void loadObject(std::string scope, std::string name, const JsonNode & data, size_t index) override;
  136. std::vector<bool> getDefaultAllowed() const;
  137. ~CHeroClassHandler();
  138. template <typename Handler> void serialize(Handler &h, const int version)
  139. {
  140. h & heroClasses;
  141. }
  142. };
  143. class DLL_LINKAGE CHeroHandler : public IHandlerBase
  144. {
  145. /// expPerLEvel[i] is amount of exp needed to reach level i;
  146. /// consists of 201 values. Any higher levels require experience larger that ui64 can hold
  147. std::vector<ui64> expPerLevel;
  148. /// helpers for loading to avoid huge load functions
  149. void loadHeroArmy(CHero * hero, const JsonNode & node);
  150. void loadHeroSkills(CHero * hero, const JsonNode & node);
  151. void loadHeroSpecialty(CHero * hero, const JsonNode & node);
  152. void loadExperience();
  153. void loadBallistics();
  154. void loadTerrains();
  155. void loadObstacles();
  156. /// Load single hero from json
  157. CHero * loadFromJson(const JsonNode & node);
  158. public:
  159. CHeroClassHandler classes;
  160. std::vector< ConstTransitivePtr<CHero> > heroes;
  161. //default costs of going through terrains. -1 means terrain is impassable
  162. std::vector<int> terrCosts;
  163. struct SBallisticsLevelInfo
  164. {
  165. ui8 keep, tower, gate, wall; //chance to hit in percent (eg. 87 is 87%)
  166. ui8 shots; //how many shots we have
  167. ui8 noDmg, oneDmg, twoDmg; //chances for shot dealing certain dmg in percent (eg. 87 is 87%); must sum to 100
  168. ui8 sum; //I don't know if it is useful for anything, but it's in config file
  169. template <typename Handler> void serialize(Handler &h, const int version)
  170. {
  171. h & keep & tower & gate & wall & shots & noDmg & oneDmg & twoDmg & sum;
  172. }
  173. };
  174. std::vector<SBallisticsLevelInfo> ballistics; //info about ballistics ability per level; [0] - none; [1] - basic; [2] - adv; [3] - expert
  175. std::map<int, CObstacleInfo> obstacles; //info about obstacles that may be placed on battlefield
  176. std::map<int, CObstacleInfo> absoluteObstacles; //info about obstacles that may be placed on battlefield
  177. ui32 level(ui64 experience) const; //calculates level corresponding to given experience amount
  178. ui64 reqExp(ui32 level) const; //calculates experience required for given level
  179. std::vector<JsonNode> loadLegacyData(size_t dataSize) override;
  180. void loadObject(std::string scope, std::string name, const JsonNode & data) override;
  181. void loadObject(std::string scope, std::string name, const JsonNode & data, size_t index) override;
  182. CHeroHandler(); //c-tor
  183. ~CHeroHandler(); //d-tor
  184. std::vector<bool> getDefaultAllowed() const;
  185. /**
  186. * Gets a list of default allowed abilities. OH3 abilities/skills are all allowed by default.
  187. *
  188. * @return a list of allowed abilities, the index is the ability id
  189. */
  190. std::vector<bool> getDefaultAllowedAbilities() const;
  191. template <typename Handler> void serialize(Handler &h, const int version)
  192. {
  193. h & classes & heroes & expPerLevel & ballistics & terrCosts;
  194. h & obstacles & absoluteObstacles;
  195. }
  196. };