CHeroHandler.h 8.0 KB

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